- Device dropdown: All devices, this device by name, then drivers found by mDNS (tagged app=led-strip) and any added by hand. - POST /api/relay: a driver calls another driver's API for the page, so the browser only talks to its own board. Limited to /api/*, /ota and /reboot. - All devices sends brightness and preset plays to every device. - Settings: sync this device's presets, order and startup to the others. - CORS for LAN and localhost origins, and an OPTIONS handler for /ota. - Header labels are removed; the controls keep aria-labels. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
245 lines
7.2 KiB
C++
245 lines
7.2 KiB
C++
/*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
#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 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);
|
|
}
|
|
|
|
// Client address: DHCP, or the static address from settings. Called before the client connects.
|
|
static esp_err_t apply_client_ip(const net_cfg_t *c)
|
|
{
|
|
if (!c->use_static) {
|
|
esp_err_t err = esp_netif_dhcpc_start(s_sta_netif);
|
|
// Already running is fine
|
|
return (err == ESP_OK || err == ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) ? ESP_OK : err;
|
|
}
|
|
esp_netif_ip_info_t info = {};
|
|
if (esp_netif_str_to_ip4(c->ip, &info.ip) != ESP_OK ||
|
|
esp_netif_str_to_ip4(c->netmask, &info.netmask) != ESP_OK ||
|
|
esp_netif_str_to_ip4(c->gateway, &info.gw) != ESP_OK) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
esp_netif_dhcpc_stop(s_sta_netif); // ignore "not started"
|
|
esp_err_t err = esp_netif_set_ip_info(s_sta_netif, &info);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
esp_netif_dns_info_t dns = {};
|
|
const char *dns_str = c->dns[0] ? c->dns : c->gateway;
|
|
if (esp_netif_str_to_ip4(dns_str, &dns.ip.u_addr.ip4) == ESP_OK) {
|
|
dns.ip.type = ESP_IPADDR_TYPE_V4;
|
|
esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
|
|
}
|
|
ESP_LOGI(TAG, "Client static IP %s/%s gw %s", c->ip, c->netmask, c->gateway);
|
|
return ESP_OK;
|
|
}
|
|
|
|
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]) {
|
|
net_cfg_t n;
|
|
settings_load_net(&n);
|
|
if (apply_client_ip(&n) != ESP_OK) {
|
|
ESP_LOGW(TAG, "Static IP settings are invalid; using DHCP");
|
|
n.use_static = false;
|
|
apply_client_ip(&n);
|
|
}
|
|
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: <hostname>.local, with an HTTP service on port 80
|
|
net_cfg_t n;
|
|
settings_load_net(&n);
|
|
ESP_ERROR_CHECK(mdns_init());
|
|
ESP_ERROR_CHECK(mdns_hostname_set(n.hostname));
|
|
ESP_ERROR_CHECK(mdns_instance_name_set("LED controller"));
|
|
// app=led-strip lets other drivers' discovery (GET /api/devices) pick out LED drivers from other HTTP devices
|
|
static mdns_txt_item_t txt[] = { { "app", "led-strip" } };
|
|
ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, txt, 1));
|
|
ESP_LOGI(TAG, "mDNS: %s.local", n.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_save_network(const net_cfg_t *c)
|
|
{
|
|
esp_err_t err = settings_save_net(c);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
err = mdns_hostname_set(c->hostname);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
// Reconnect with the new address settings if a client is configured
|
|
wifi_creds_t w;
|
|
if (settings_load_wifi(&w) != ESP_OK || !w.ssid[0]) {
|
|
return ESP_OK;
|
|
}
|
|
esp_timer_stop(s_retry_timer);
|
|
esp_wifi_disconnect();
|
|
s_retries = 0;
|
|
err = apply_client_ip(c);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
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;
|
|
}
|