feat: default IP, reset reason in /status, seeded default group
With no DHCP answer in 15 seconds the controller takes 192.168.4.1/24, for a cable straight to a PC with a static IP. /status reports why the board last restarted. First boot also seeds a default group that the default zone plays to. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4372ba4105
commit
2f0c26e9b8
12 files changed
+99
-35
No files matched your search
+67
-25
@@ -45,6 +45,23 @@ static const char *TAG = "eth";
|
||||
static httpd_handle_t s_server = NULL;
|
||||
static esp_netif_t *s_eth_netif = NULL;
|
||||
|
||||
static const char *reset_reason(void)
|
||||
{
|
||||
switch (esp_reset_reason()) {
|
||||
case ESP_RST_POWERON: return "power_on";
|
||||
case ESP_RST_EXT: return "external";
|
||||
case ESP_RST_SW: return "software";
|
||||
case ESP_RST_PANIC: return "panic";
|
||||
case ESP_RST_INT_WDT: return "interrupt_watchdog";
|
||||
case ESP_RST_TASK_WDT: return "task_watchdog";
|
||||
case ESP_RST_WDT: return "watchdog";
|
||||
case ESP_RST_BROWNOUT: return "brownout";
|
||||
case ESP_RST_USB: return "usb";
|
||||
case ESP_RST_PWR_GLITCH: return "power_glitch";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
{
|
||||
beat_info_t bi;
|
||||
@@ -63,14 +80,14 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ip_info.ip));
|
||||
snprintf(macs, sizeof(macs), MACSTR, MAC2STR(mac));
|
||||
snprintf(buf, sizeof(buf),
|
||||
"{\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"free_heap\":%lu,\"free_internal\":%lu,"
|
||||
"{\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu,"
|
||||
"\"mic_rms_dbfs\":%.1f,\"mic_peak_dbfs\":%.1f,\"mic_spl_db\":%.1f,"
|
||||
"\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu,"
|
||||
"\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu,"
|
||||
"\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu,"
|
||||
"\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu},"
|
||||
"\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}",
|
||||
ip, macs, (long long)(esp_timer_get_time() / 1000000), (unsigned long)esp_get_free_heap_size(),
|
||||
ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(),
|
||||
(unsigned long)heap_caps_get_free_size(MALLOC_CAP_INTERNAL), level.rms_dbfs, level.peak_dbfs, level.spl_db,
|
||||
bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count,
|
||||
(unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false",
|
||||
@@ -85,6 +102,11 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
|
||||
#define DEFAULT_HOSTNAME "led-controller"
|
||||
|
||||
// Address when no DHCP server answers, as on a cable straight to a PC. The PC needs a static IP on this subnet.
|
||||
#define DEFAULT_IP ESP_IP4TOADDR(192, 168, 4, 1)
|
||||
#define DEFAULT_NETMASK ESP_IP4TOADDR(255, 255, 255, 0)
|
||||
#define DHCP_WAIT_S 15
|
||||
|
||||
static const char *settings_str(const char *key)
|
||||
{
|
||||
cJSON *v = cJSON_GetObjectItem(store_settings(), key);
|
||||
@@ -92,7 +114,7 @@ static const char *settings_str(const char *key)
|
||||
}
|
||||
|
||||
// "static_ips" setting: a list of {ip, netmask, gw, dns} tried in order on every link-up. The first whose
|
||||
// gateway answers ARP is kept; if none does, the controller falls back to DHCP.
|
||||
// gateway answers ARP is kept; if none does, the controller falls back to DHCP, then to DEFAULT_IP.
|
||||
static bool parse_static(cJSON *e, esp_netif_ip_info_t *info, esp_netif_dns_info_t *dns)
|
||||
{
|
||||
const char *ip = NULL, *mask = "255.255.255.0", *gw = NULL, *d = NULL;
|
||||
@@ -140,16 +162,19 @@ static bool gateway_answers(esp_netif_t *netif, const esp_ip4_addr_t *gw)
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void static_ip_task(void *arg)
|
||||
static bool have_static_ips(void)
|
||||
{
|
||||
static bool running;
|
||||
if (running) {
|
||||
vTaskDelete(NULL);
|
||||
cJSON *l = cJSON_GetObjectItem(store_settings(), "static_ips");
|
||||
return cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0;
|
||||
}
|
||||
|
||||
static bool try_static_ips(void)
|
||||
{
|
||||
if (!have_static_ips()) {
|
||||
return false;
|
||||
}
|
||||
running = true;
|
||||
cJSON *list = cJSON_GetObjectItem(store_settings(), "static_ips");
|
||||
esp_netif_dhcpc_stop(s_eth_netif);
|
||||
bool done = false;
|
||||
cJSON *e;
|
||||
cJSON_ArrayForEach(e, list) {
|
||||
esp_netif_ip_info_t info;
|
||||
@@ -166,27 +191,46 @@ static void static_ip_task(void *arg)
|
||||
}
|
||||
if (info.gw.addr == 0 || gateway_answers(s_eth_netif, &info.gw)) {
|
||||
ESP_LOGI(TAG, "Static IP " IPSTR " (gw " IPSTR ")", IP2STR(&info.ip), IP2STR(&info.gw));
|
||||
done = true;
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
ESP_LOGI(TAG, "Static IP " IPSTR ": gateway " IPSTR " did not answer", IP2STR(&info.ip), IP2STR(&info.gw));
|
||||
}
|
||||
if (!done) {
|
||||
ESP_LOGW(TAG, "No static IP matched this network, using DHCP");
|
||||
esp_netif_ip_info_t none = { 0 };
|
||||
esp_netif_set_ip_info(s_eth_netif, &none);
|
||||
esp_netif_dhcpc_start(s_eth_netif);
|
||||
ESP_LOGW(TAG, "No static IP matched this network, using DHCP");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start DHCP and wait for a lease. A lost link also ends the wait: the next link-up starts over.
|
||||
static bool dhcp_settled(void)
|
||||
{
|
||||
esp_netif_dhcpc_start(s_eth_netif);
|
||||
for (int i = 0; i < DHCP_WAIT_S * 10; i++) {
|
||||
esp_netif_ip_info_t ip;
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
esp_netif_get_ip_info(s_eth_netif, &ip);
|
||||
if (ip.ip.addr != 0 || !esp_netif_is_netif_up(s_eth_netif)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void link_up_task(void *arg)
|
||||
{
|
||||
static bool running;
|
||||
if (running) {
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
running = true;
|
||||
if (!try_static_ips() && !dhcp_settled()) {
|
||||
ESP_LOGW(TAG, "No DHCP server, using the default IP");
|
||||
esp_netif_ip_info_t info = { .ip.addr = DEFAULT_IP, .netmask.addr = DEFAULT_NETMASK };
|
||||
esp_netif_dhcpc_stop(s_eth_netif);
|
||||
esp_netif_set_ip_info(s_eth_netif, &info);
|
||||
}
|
||||
running = false;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static bool have_static_ips(void)
|
||||
{
|
||||
cJSON *l = cJSON_GetObjectItem(store_settings(), "static_ips");
|
||||
return cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0;
|
||||
}
|
||||
|
||||
static void start_mdns(void)
|
||||
{
|
||||
const char *host = settings_str("hostname");
|
||||
@@ -243,9 +287,7 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed);
|
||||
esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &duplex);
|
||||
ESP_LOGI(TAG, "Ethernet Link Up, MAC " MACSTR, MAC2STR(mac));
|
||||
if (have_static_ips()) {
|
||||
xTaskCreate(static_ip_task, "static_ip", 4096, NULL, 5, NULL);
|
||||
}
|
||||
xTaskCreate(link_up_task, "link_up", 4096, NULL, 5, NULL);
|
||||
ESP_LOGI(TAG, "%s Mbps %s Duplex",
|
||||
speed == ETH_SPEED_100M ? "100" : "10",
|
||||
duplex == ETH_DUPLEX_FULL ? "Full" : "Half");
|
||||
|
||||
Reference in new issue
Block a user