Files
desklock/firmware/main/net.c
T
jpmschweitzerandClaude Fable 5 b8974ec84f
Test, Build and Push / test-gateway (push) Successful in 10s
Test, Build and Push / release (push) Skipped
Test, Build and Push / build-gateway (push) Skipped
Wi-Fi data path FIXED: esp_hosted 2.12 on both chips + C6 SDIO auto-OTA
Root cause of the dead data path (assoc/scan/RPC fine, zero data
frames): esp-hosted 1.4.x is formally incompatible with IDF 5.5
(esp-hosted-mcu#47) and the factory C6 slave firmware was ancient
(couldn't even answer a version RPC). Waveshare's examples pin 1.4.* —
do not follow them.

Fix:
- host: espressif/esp_hosted ^2.12 (+ esp_wifi_remote 1.6)
- slave: 2.12.11 network_adapter.bin embedded in the app (c6_ota.c
  streams it to the C6 over the SDIO RPC channel at boot when the
  reported version is < 2.x; ~10s, no wires, idempotent)
- RAM diet for hosted 2.x's footprint (MEMPOOL_PREFER_SPIRAM,
  reduced WIFI_RMT buffers) — without it internal SRAM famine
  boot-loops in xTaskCreateStaticPinnedToCore before app_main
- conservative 20MHz SDIO clock for first verified data path

Verified on hardware: DHCP lease (even that healed), 8/8 pings to
router and tower-of-joy at 1-5ms, WebSocket to the gateway connected,
device registered at /devices as desklock-p4 with fw version.

Also: wifi_diag.c L1 SoftAP diagnostic mode (DESKLOCK-DIAG) with
review fixes, boot ping ladder in net.c, static-IP fallback,
face_status() line, docs for the whole failure taxonomy.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 00:26:28 +02:00

184 lines
6.2 KiB
C

/* Wi-Fi STA via ESP-Hosted (C6 radio) + SNTP, then start the gateway link. */
#include <string.h>
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_netif_sntp.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "ping/ping_sock.h"
#include "desklock.h"
#include "secrets.h"
static const char *TAG = "net";
static esp_netif_t *s_netif;
static volatile bool s_got_ip;
static void start_ping_diag(void);
static void ping_diag_task(void *arg);
/* Router DHCP has been observed to go silent for this MAC (flap throttling).
* After 15 s without a lease, claim the previously granted address statically. */
static void static_ip_fallback(void *arg)
{
(void)arg;
vTaskDelay(pdMS_TO_TICKS(15000));
if (!s_got_ip) {
ESP_LOGW(TAG, "no DHCP lease after 15s — falling back to static 192.168.86.53");
esp_netif_dhcpc_stop(s_netif);
esp_netif_ip_info_t ip = { 0 };
ip.ip.addr = esp_ip4addr_aton("192.168.86.53");
ip.gw.addr = esp_ip4addr_aton("192.168.86.1");
ip.netmask.addr = esp_ip4addr_aton("255.255.255.0");
esp_netif_set_ip_info(s_netif, &ip);
esp_netif_dns_info_t dns = { 0 };
dns.ip.u_addr.ip4.addr = esp_ip4addr_aton("192.168.86.1");
dns.ip.type = ESP_IPADDR_TYPE_V4;
esp_netif_set_dns_info(s_netif, ESP_NETIF_DNS_MAIN, &dns);
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
tzset();
esp_sntp_config_t sntp = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
esp_netif_sntp_init(&sntp);
gw_start();
start_ping_diag();
}
vTaskDelete(NULL);
}
static void scan_then_connect(void *arg)
{
(void)arg;
ESP_LOGI(TAG, "scanning for APs…");
if (esp_wifi_scan_start(NULL, true) == ESP_OK) {
uint16_t n = 12;
wifi_ap_record_t recs[12];
if (esp_wifi_scan_get_ap_records(&n, recs) == ESP_OK) {
for (int i = 0; i < n; i++) {
ESP_LOGI(TAG, " ap: '%s' rssi=%d ch=%d auth=%d",
(const char *)recs[i].ssid, recs[i].rssi,
recs[i].primary, recs[i].authmode);
}
}
}
esp_wifi_connect();
vTaskDelete(NULL);
}
static void wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data)
{
(void)arg;
(void)data;
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
wifi_event_sta_disconnected_t *dc = (wifi_event_sta_disconnected_t *)data;
ESP_LOGW(TAG, "wifi disconnected (reason %d), retrying", dc ? dc->reason : -1);
face_set(FACE_ERROR);
vTaskDelay(pdMS_TO_TICKS(3000));
esp_wifi_connect();
} else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *ev = (ip_event_got_ip_t *)data;
s_got_ip = true;
ESP_LOGI(TAG, "got ip " IPSTR, IP2STR(&ev->ip_info.ip));
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
tzset();
esp_sntp_config_t sntp = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
esp_netif_sntp_init(&sntp);
gw_start();
start_ping_diag();
}
}
static void ping_ok(esp_ping_handle_t h, void *arg)
{
(void)arg;
uint32_t seq = 0, ms = 0;
ip_addr_t target;
esp_ping_get_profile(h, ESP_PING_PROF_SEQNO, &seq, sizeof(seq));
esp_ping_get_profile(h, ESP_PING_PROF_TIMEGAP, &ms, sizeof(ms));
esp_ping_get_profile(h, ESP_PING_PROF_IPADDR, &target, sizeof(target));
ESP_LOGI(TAG, "PING %s seq=%u %ums OK", ipaddr_ntoa(&target), (unsigned)seq, (unsigned)ms);
}
static void ping_timeout(esp_ping_handle_t h, void *arg)
{
(void)arg;
uint32_t seq = 0;
ip_addr_t target;
esp_ping_get_profile(h, ESP_PING_PROF_SEQNO, &seq, sizeof(seq));
esp_ping_get_profile(h, ESP_PING_PROF_IPADDR, &target, sizeof(target));
ESP_LOGW(TAG, "PING %s seq=%u TIMEOUT", ipaddr_ntoa(&target), (unsigned)seq);
}
static void ping_target(const char *ip_str)
{
esp_ping_config_t pc = ESP_PING_DEFAULT_CONFIG();
pc.count = 4;
pc.interval_ms = 500;
ipaddr_aton(ip_str, &pc.target_addr);
esp_ping_callbacks_t cbs = {
.on_ping_success = ping_ok,
.on_ping_timeout = ping_timeout,
};
esp_ping_handle_t h;
if (esp_ping_new_session(&pc, &cbs, &h) == ESP_OK) {
esp_ping_start(h);
}
}
static void start_ping_diag(void)
{
static bool started;
if (!started) {
started = true;
xTaskCreate(ping_diag_task, "pingdiag", 4096, NULL, 3, NULL);
}
}
/* boot diagnosis: can the device reach the router at all? then the server? */
static void ping_diag_task(void *arg)
{
(void)arg;
vTaskDelay(pdMS_TO_TICKS(3000));
ESP_LOGI(TAG, "== ping diagnosis: router ==");
ping_target("192.168.86.1");
vTaskDelay(pdMS_TO_TICKS(6000));
ESP_LOGI(TAG, "== ping diagnosis: tower-of-joy ==");
ping_target("192.168.86.149");
vTaskDelay(pdMS_TO_TICKS(6000));
ESP_LOGI(TAG, "== slave wifi statistics dump ==");
esp_wifi_statis_dump(0xFFFFFFFF);
vTaskDelete(NULL);
}
void net_start(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
s_netif = esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event, NULL));
wifi_config_t wc = { 0 };
wc.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
strlcpy((char *)wc.sta.ssid, WIFI_SSID, sizeof(wc.sta.ssid));
strlcpy((char *)wc.sta.password, WIFI_PASS, sizeof(wc.sta.password));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wc));
ESP_ERROR_CHECK(esp_wifi_start());
xTaskCreate(static_ip_fallback, "ipfb", 4096, NULL, 3, NULL);
ESP_LOGI(TAG, "wifi starting, ssid=%s", WIFI_SSID);
}