/* * SPDX-License-Identifier: Apache-2.0 */ #include #include #include "esp_event.h" #include "esp_log.h" #include "esp_mac.h" #include "esp_netif.h" #include "esp_timer.h" #include "esp_wifi.h" #include "mdns.h" #include "net.h" static const char *TAG = "net"; #define AP_CHANNEL 1 #define AP_MAX_CLIENTS 4 #define MDNS_HOSTNAME "led" #define RETRY_MAX_MS 30000 static esp_netif_t *s_ap_netif; static esp_netif_t *s_sta_netif; static esp_timer_handle_t s_retry_timer; static volatile bool s_sta_up; static esp_ip4_addr_t s_sta_ip; static int s_retries; // Set the STA config from credentials. Empty password means an open network. static void apply_sta_config(const wifi_creds_t *c) { wifi_config_t cfg = {}; strncpy((char *)cfg.sta.ssid, c->ssid, sizeof(cfg.sta.ssid) - 1); strncpy((char *)cfg.sta.password, c->pass, sizeof(cfg.sta.password) - 1); cfg.sta.threshold.authmode = c->pass[0] ? WIFI_AUTH_WPA2_PSK : WIFI_AUTH_OPEN; esp_wifi_set_config(WIFI_IF_STA, &cfg); } static void retry_cb(void *arg) { (void)arg; esp_wifi_connect(); } static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data) { (void)arg; (void)base; (void)data; switch (id) { case WIFI_EVENT_STA_START: { wifi_creds_t c; if (settings_load_wifi(&c) == ESP_OK && c.ssid[0]) { esp_wifi_connect(); } break; } case WIFI_EVENT_STA_DISCONNECTED: { // Back off 1 s, 2 s, 4 s ... up to RETRY_MAX_MS s_sta_up = false; uint32_t delay_ms = 1000u << (s_retries < 5 ? s_retries : 5); if (delay_ms > RETRY_MAX_MS) delay_ms = RETRY_MAX_MS; s_retries++; esp_timer_stop(s_retry_timer); esp_timer_start_once(s_retry_timer, (uint64_t)delay_ms * 1000); break; } default: break; } } static void on_ip_event(void *arg, esp_event_base_t base, int32_t id, void *data) { (void)arg; (void)base; (void)id; ip_event_got_ip_t *e = (ip_event_got_ip_t *)data; s_sta_ip = e->ip_info.ip; s_sta_up = true; s_retries = 0; ESP_LOGI(TAG, "Client IP " IPSTR, IP2STR(&e->ip_info.ip)); } void net_ap_ssid(char *buf, size_t len) { uint8_t mac[6]; esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); snprintf(buf, len, "led-%02X%02X%02X", mac[3], mac[4], mac[5]); } esp_err_t net_start(void) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); s_ap_netif = esp_netif_create_default_wifi_ap(); s_sta_netif = esp_netif_create_default_wifi_sta(); wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&init_cfg)); ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, on_wifi_event, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, on_ip_event, NULL, NULL)); const esp_timer_create_args_t timer_args = { .callback = retry_cb, .arg = NULL, .dispatch_method = ESP_TIMER_TASK, .name = "wifi_retry", .skip_unhandled_events = false, }; ESP_ERROR_CHECK(esp_timer_create(&timer_args, &s_retry_timer)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); // Open access point named led-XXXXXX char ssid[16]; net_ap_ssid(ssid, sizeof(ssid)); wifi_config_t ap = {}; strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1); ap.ap.ssid_len = strlen(ssid); ap.ap.channel = AP_CHANNEL; ap.ap.max_connection = AP_MAX_CLIENTS; ap.ap.authmode = WIFI_AUTH_OPEN; ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap)); // Client credentials, if any were saved wifi_creds_t c; if (settings_load_wifi(&c) == ESP_OK && c.ssid[0]) { apply_sta_config(&c); } ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "AP %s started", ssid); // mDNS: led.local, with an HTTP service on port 80 ESP_ERROR_CHECK(mdns_init()); ESP_ERROR_CHECK(mdns_hostname_set(MDNS_HOSTNAME)); ESP_ERROR_CHECK(mdns_instance_name_set("LED controller")); ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0)); ESP_LOGI(TAG, "mDNS: %s.local", MDNS_HOSTNAME); return ESP_OK; } bool net_sta_status(char *ip, size_t ip_len) { if (ip_len) { ip[0] = '\0'; } if (!s_sta_up) { return false; } snprintf(ip, ip_len, IPSTR, IP2STR(&s_sta_ip)); return true; } esp_err_t net_sta_connect(const wifi_creds_t *c) { esp_err_t err = settings_save_wifi(c); if (err != ESP_OK) { return err; } esp_timer_stop(s_retry_timer); esp_wifi_disconnect(); s_retries = 0; apply_sta_config(c); return esp_wifi_connect(); } esp_err_t net_scan(wifi_ap_record_t *recs, uint16_t *count) { esp_err_t err = esp_wifi_scan_start(NULL, true); if (err != ESP_OK) { return err; } uint16_t max = *count; err = esp_wifi_scan_get_ap_records(&max, recs); *count = max; return err; }