|
@@ -1,16 +1,21 @@
|
|
#include "common.h"
|
|
#include "common.h"
|
|
-#include "WiFi.h"
|
|
|
|
#include "wifi.h"
|
|
#include "wifi.h"
|
|
#include "config.h"
|
|
#include "config.h"
|
|
#include "httpd.h"
|
|
#include "httpd.h"
|
|
#include "led.h"
|
|
#include "led.h"
|
|
|
|
|
|
|
|
+#include <WiFi.h>
|
|
|
|
+
|
|
|
|
+#include <mdns.h>
|
|
|
|
+
|
|
#include <lwip/dns.h>
|
|
#include <lwip/dns.h>
|
|
#include <lwip/inet.h>
|
|
#include <lwip/inet.h>
|
|
#include <lwip/apps/sntp.h>
|
|
#include <lwip/apps/sntp.h>
|
|
#include <esp_sntp.h>
|
|
#include <esp_sntp.h>
|
|
#include <esp_wifi.h>
|
|
#include <esp_wifi.h>
|
|
|
|
|
|
|
|
+WiFiUDP UDP;
|
|
|
|
+
|
|
static const char *ssid, *password, *hostname, *dnsserver;
|
|
static const char *ssid, *password, *hostname, *dnsserver;
|
|
static TimerHandle_t sta_failure_timer;
|
|
static TimerHandle_t sta_failure_timer;
|
|
|
|
|
|
@@ -70,7 +75,6 @@ static void sta_timeout_disable(void)
|
|
|
|
|
|
static void sntp_sync_cb(struct timeval *tv)
|
|
static void sntp_sync_cb(struct timeval *tv)
|
|
{
|
|
{
|
|
- static uint8_t prev_sync_status = SNTP_SYNC_STATUS_RESET;
|
|
|
|
uint8_t sync_status = sntp_get_sync_status();
|
|
uint8_t sync_status = sntp_get_sync_status();
|
|
|
|
|
|
switch (sync_status) {
|
|
switch (sync_status) {
|
|
@@ -85,8 +89,6 @@ static void sntp_sync_cb(struct timeval *tv)
|
|
default:
|
|
default:
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
-
|
|
|
|
- prev_sync_status = sync_status;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
static void my_sntp_start(void)
|
|
static void my_sntp_start(void)
|
|
@@ -159,10 +161,10 @@ static void sntp_set_server(const char *name)
|
|
sntp_server_found(name, &addr, NULL);
|
|
sntp_server_found(name, &addr, NULL);
|
|
}
|
|
}
|
|
|
|
|
|
-static void start_services(void)
|
|
|
|
|
|
+static void dns_setup(void)
|
|
{
|
|
{
|
|
- /* Always run after (re)connect */
|
|
|
|
const ip_addr_t *dns_ip = dns_getserver(0);
|
|
const ip_addr_t *dns_ip = dns_getserver(0);
|
|
|
|
+
|
|
if (invalid_ip(dns_ip) || getenv_bool("ip4.dhcp.nodns")) {
|
|
if (invalid_ip(dns_ip) || getenv_bool("ip4.dhcp.nodns")) {
|
|
/* Static DNS server configuration */
|
|
/* Static DNS server configuration */
|
|
ip_addr_t addr;
|
|
ip_addr_t addr;
|
|
@@ -172,12 +174,70 @@ static void start_services(void)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- {
|
|
|
|
- dns_ip = dns_getserver(0);
|
|
|
|
- const char *dns_server_str = inet_ntoa(*dns_ip);
|
|
|
|
- printf("[DNS] DNS server: %s\n", dns_server_str);
|
|
|
|
- setenv_cond("status.net.dns.server", dns_server_str);
|
|
|
|
|
|
+ dns_ip = dns_getserver(0);
|
|
|
|
+ const char *dns_server_str = inet_ntoa(*dns_ip);
|
|
|
|
+ printf("[DNS] DNS server: %s\n", dns_server_str);
|
|
|
|
+ setenv_cond("status.net.dns.server", dns_server_str);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static esp_ip_addr_t ipaddr_toesp(IPAddress ip)
|
|
|
|
+{
|
|
|
|
+ esp_ip_addr_t eip;
|
|
|
|
+ memset(&eip, 0, sizeof eip);
|
|
|
|
+ eip.u_addr.ip4.addr = (uint32_t)ip;
|
|
|
|
+ return eip;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void mdns_setup(void)
|
|
|
|
+{
|
|
|
|
+ static bool mdns_started;
|
|
|
|
+ static const struct mdns_service {
|
|
|
|
+ const char *type, *proto;
|
|
|
|
+ uint16_t port;
|
|
|
|
+ } mdns_services[] = {
|
|
|
|
+ { "_http", "_tcp", 80 },
|
|
|
|
+ { NULL, NULL, 0 }
|
|
|
|
+ };
|
|
|
|
+ char unique_name[32];
|
|
|
|
+
|
|
|
|
+ if (mdns_started)
|
|
|
|
+ mdns_free();
|
|
|
|
+
|
|
|
|
+ if (!getenv_bool("mdns.enabled"))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ mdns_started = mdns_init() == ESP_OK;
|
|
|
|
+ if (!mdns_started)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ mdns_hostname_set(hostname);
|
|
|
|
+ mdns_instance_name_set(hostname);
|
|
|
|
+
|
|
|
|
+ unique_name[0] = 0;
|
|
|
|
+
|
|
|
|
+ if (connected & CON_STA) {
|
|
|
|
+ mdns_ip_addr_t iplist;
|
|
|
|
+
|
|
|
|
+ iplist.addr = ipaddr_toesp(WiFi.localIP());
|
|
|
|
+ iplist.next = NULL;
|
|
|
|
+ snprintf(unique_name, sizeof unique_name, "MAX80-%s", serial_number);
|
|
|
|
+ mdns_delegate_hostname_add(unique_name, &iplist);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (const struct mdns_service *svc = mdns_services; svc->type; svc++) {
|
|
|
|
+ mdns_service_add(NULL, svc->type, svc->proto, svc->port, NULL, 0);
|
|
|
|
+ if (unique_name[0]) {
|
|
|
|
+ mdns_service_add_for_host(NULL, svc->type, svc->proto,
|
|
|
|
+ unique_name, svc->port, NULL, 0);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void start_services(void)
|
|
|
|
+{
|
|
|
|
+ /* Always run after (re)connect */
|
|
|
|
+ dns_setup();
|
|
|
|
+ mdns_setup();
|
|
|
|
|
|
// If Arduino supported both of these at the same that would be
|
|
// If Arduino supported both of these at the same that would be
|
|
// awesome, but it requires ESP-IDF reconfiguration...
|
|
// awesome, but it requires ESP-IDF reconfiguration...
|
|
@@ -412,7 +472,6 @@ static void wifi_config_ap(void)
|
|
IPAddress AP_Gateway = IPAddress(0,0,0,0); // No gateway
|
|
IPAddress AP_Gateway = IPAddress(0,0,0,0); // No gateway
|
|
unsigned int channel = (time(NULL) % 11) + 1; // Pseudo-random
|
|
unsigned int channel = (time(NULL) % 11) + 1; // Pseudo-random
|
|
uint8_t mac[6];
|
|
uint8_t mac[6];
|
|
- char mac_str[6*3];
|
|
|
|
static char ap_ssid[64];
|
|
static char ap_ssid[64];
|
|
|
|
|
|
WiFi.softAPmacAddress(mac);
|
|
WiFi.softAPmacAddress(mac);
|
|
@@ -423,7 +482,8 @@ static void wifi_config_ap(void)
|
|
efuse_default_mac[4], efuse_default_mac[5]);
|
|
efuse_default_mac[4], efuse_default_mac[5]);
|
|
|
|
|
|
printf("[WIFI] AP SSID %s IP %s netmask %s channel %u\n",
|
|
printf("[WIFI] AP SSID %s IP %s netmask %s channel %u\n",
|
|
- ap_ssid, AP_IP.toString(), AP_Netmask.toString(), channel);
|
|
|
|
|
|
+ ap_ssid, AP_IP.toString().c_str(),
|
|
|
|
+ AP_Netmask.toString().c_str(), channel);
|
|
setenv_cond("status.net.ap.ssid", ap_ssid);
|
|
setenv_cond("status.net.ap.ssid", ap_ssid);
|
|
setenv_ip("status.net.ap.ip4", AP_IP);
|
|
setenv_ip("status.net.ap.ip4", AP_IP);
|
|
setenv_ip("status.net.ap.ip4.mask", AP_Netmask);
|
|
setenv_ip("status.net.ap.ip4.mask", AP_Netmask);
|
|
@@ -434,7 +494,7 @@ static void wifi_config_ap(void)
|
|
printf("WiFi.softAPConfig\n");
|
|
printf("WiFi.softAPConfig\n");
|
|
WiFi.softAPConfig(AP_IP, AP_Gateway, AP_Netmask);
|
|
WiFi.softAPConfig(AP_IP, AP_Gateway, AP_Netmask);
|
|
printf("WiFi.softAPsetHostname\n");
|
|
printf("WiFi.softAPsetHostname\n");
|
|
- WiFi.softAPsetHostname("max80");
|
|
|
|
|
|
+ WiFi.softAPsetHostname(hostname);
|
|
|
|
|
|
// Conservative setting: 20 MHz (single channel) only; this is for
|
|
// Conservative setting: 20 MHz (single channel) only; this is for
|
|
// reliability, not performance.
|
|
// reliability, not performance.
|
|
@@ -442,7 +502,6 @@ static void wifi_config_ap(void)
|
|
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_AP, WIFI_BW_HT20);
|
|
esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_AP, WIFI_BW_HT20);
|
|
|
|
|
|
// Enable AP immediately if no SSID configured
|
|
// Enable AP immediately if no SSID configured
|
|
- printf("WiFi.enableAP\n");
|
|
|
|
WiFi.enableAP(!ssid);
|
|
WiFi.enableAP(!ssid);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -477,19 +536,11 @@ static void wifi_config_sta(void)
|
|
WiFi.begin(ssid, password);
|
|
WiFi.begin(ssid, password);
|
|
}
|
|
}
|
|
|
|
|
|
-static const char *getenv_notempty(const char *env)
|
|
|
|
-{
|
|
|
|
- const char *str = getenv(env);
|
|
|
|
- if (str && !*str)
|
|
|
|
- str = NULL;
|
|
|
|
- return str;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
static void wifi_config(void)
|
|
static void wifi_config(void)
|
|
{
|
|
{
|
|
ssid = getenv_notempty("wifi.ssid");
|
|
ssid = getenv_notempty("wifi.ssid");
|
|
password = getenv_notempty("wifi.psk");
|
|
password = getenv_notempty("wifi.psk");
|
|
- hostname = getenv_notempty("hostname");
|
|
|
|
|
|
+ hostname = getenv_def("hostname", "max80");
|
|
dnsserver = getenv_notempty("ip4.dns");
|
|
dnsserver = getenv_notempty("ip4.dns");
|
|
|
|
|
|
force_conn_update = true;
|
|
force_conn_update = true;
|
|
@@ -501,8 +552,8 @@ static void wifi_config(void)
|
|
|
|
|
|
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
|
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
|
|
|
|
|
- if (hostname)
|
|
|
|
- WiFi.hostname(hostname);
|
|
|
|
|
|
+ setenv_config("status.hostname", hostname);
|
|
|
|
+ WiFi.hostname(hostname);
|
|
|
|
|
|
printf("wifi_config_ap\n");
|
|
printf("wifi_config_ap\n");
|
|
wifi_config_ap();
|
|
wifi_config_ap();
|