feat(v2): move web UI into v2, remove legacy MicroPython app
Move the web assets and templates under v2/web, drop the old src/, lib/ and scripts, and update firmware, tools, docs and db files. Add the audio page, LED test scripts and VPN autoconnect script. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6c16998691
commit
71bd320661
193 files changed
+5605
-18860
No files matched your search
+138
@@ -17,6 +17,13 @@
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_system.h"
|
||||
#include "mdns.h"
|
||||
#include "lwip/ip4_addr.h"
|
||||
#include "lwip/etharp.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "esp_netif_net_stack.h"
|
||||
#include "cJSON.h"
|
||||
#include "mic.h"
|
||||
#include "beat.h"
|
||||
#include "espnow_tx.h"
|
||||
@@ -71,6 +78,126 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
return httpd_resp_sendstr(req, buf);
|
||||
}
|
||||
|
||||
#define DEFAULT_HOSTNAME "led-controller"
|
||||
|
||||
static const char *settings_str(const char *key)
|
||||
{
|
||||
cJSON *v = cJSON_GetObjectItem(store_settings(), key);
|
||||
return cJSON_IsString(v) && v->valuestring[0] ? v->valuestring : NULL;
|
||||
}
|
||||
|
||||
// "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.
|
||||
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;
|
||||
cJSON *v;
|
||||
if (!cJSON_IsObject(e)) return false;
|
||||
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "ip"))) ip = v->valuestring;
|
||||
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "netmask"))) mask = v->valuestring;
|
||||
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "gw"))) gw = v->valuestring;
|
||||
if (cJSON_IsString(v = cJSON_GetObjectItem(e, "dns"))) d = v->valuestring;
|
||||
memset(info, 0, sizeof(*info));
|
||||
memset(dns, 0, sizeof(*dns));
|
||||
if (!ip || esp_netif_str_to_ip4(ip, &info->ip) != ESP_OK || esp_netif_str_to_ip4(mask, &info->netmask) != ESP_OK ||
|
||||
(gw && esp_netif_str_to_ip4(gw, &info->gw) != ESP_OK)) {
|
||||
return false;
|
||||
}
|
||||
if (d && esp_netif_str_to_ip4(d, &dns->ip.u_addr.ip4) == ESP_OK) {
|
||||
dns->ip.type = ESP_IPADDR_TYPE_V4;
|
||||
} else if (gw) {
|
||||
dns->ip.u_addr.ip4 = info->gw;
|
||||
dns->ip.type = ESP_IPADDR_TYPE_V4;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Poke the gateway so lwIP ARPs for it, then wait for the entry to show up
|
||||
static bool gateway_answers(esp_netif_t *netif, const esp_ip4_addr_t *gw)
|
||||
{
|
||||
struct netif *nif = esp_netif_get_netif_impl(netif);
|
||||
ip4_addr_t g = { .addr = gw->addr };
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (!nif || sock < 0) {
|
||||
if (sock >= 0) close(sock);
|
||||
return false;
|
||||
}
|
||||
struct sockaddr_in to = { .sin_family = AF_INET, .sin_port = htons(9), .sin_addr.s_addr = gw->addr };
|
||||
bool ok = false;
|
||||
for (int i = 0; i < 15 && !ok; i++) {
|
||||
sendto(sock, "", 0, 0, (struct sockaddr *)&to, sizeof(to));
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
struct eth_addr *eth = NULL;
|
||||
const ip4_addr_t *ipr = NULL;
|
||||
ok = etharp_find_addr(nif, &g, ð, &ipr) >= 0;
|
||||
}
|
||||
close(sock);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void static_ip_task(void *arg)
|
||||
{
|
||||
static bool running;
|
||||
if (running) {
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
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;
|
||||
esp_netif_dns_info_t dns;
|
||||
if (!parse_static(e, &info, &dns)) {
|
||||
ESP_LOGW(TAG, "static_ips: skipping an invalid entry");
|
||||
continue;
|
||||
}
|
||||
if (esp_netif_set_ip_info(s_eth_netif, &info) != ESP_OK) {
|
||||
continue;
|
||||
}
|
||||
if (dns.ip.type == ESP_IPADDR_TYPE_V4) {
|
||||
esp_netif_set_dns_info(s_eth_netif, ESP_NETIF_DNS_MAIN, &dns);
|
||||
}
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
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");
|
||||
if (!host) {
|
||||
host = DEFAULT_HOSTNAME;
|
||||
}
|
||||
if (mdns_init() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "mDNS init failed");
|
||||
return;
|
||||
}
|
||||
mdns_hostname_set(host);
|
||||
mdns_instance_name_set("LED controller");
|
||||
mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
|
||||
ESP_LOGI(TAG, "mDNS: %s.local", host);
|
||||
}
|
||||
|
||||
static void start_webserver(void)
|
||||
{
|
||||
if (s_server) {
|
||||
@@ -108,6 +235,9 @@ 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);
|
||||
}
|
||||
ESP_LOGI(TAG, "%s Mbps %s Duplex",
|
||||
speed == ETH_SPEED_100M ? "100" : "10",
|
||||
duplex == ETH_DUPLEX_FULL ? "Full" : "Half");
|
||||
@@ -139,6 +269,11 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
|
||||
|
||||
// Server binds to all interfaces, so it keeps working across lease changes
|
||||
start_webserver();
|
||||
static bool mdns_started;
|
||||
if (!mdns_started) {
|
||||
mdns_started = true;
|
||||
start_mdns();
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t w5500_init(esp_eth_handle_t *eth_handle_out)
|
||||
@@ -229,6 +364,9 @@ void app_main(void)
|
||||
s_eth_netif = esp_netif_new(&netif_cfg);
|
||||
ESP_ERROR_CHECK(esp_netif_attach(s_eth_netif, esp_eth_new_netif_glue(eth_handle)));
|
||||
|
||||
const char *host = settings_str("hostname");
|
||||
esp_netif_set_hostname(s_eth_netif, host ? host : DEFAULT_HOSTNAME);
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
|
||||
|
||||
|
||||
Reference in new issue
Block a user