Browse Source

esp32: create yet another wrapper class for IP4 addresses

There are three different APIs for keeping IPv4 addresses. Create a
wrapper class with defined conversions between all of them.
H. Peter Anvin 1 year ago
parent
commit
d039621dea
7 changed files with 111 additions and 57 deletions
  1. 15 0
      esp32/max80/IP4.cpp
  2. 55 0
      esp32/max80/IP4.h
  3. 41 57
      esp32/max80/wifi.cpp
  4. BIN
      esp32/output/max80.ino.bin
  5. BIN
      fpga/output/max80.fw
  6. BIN
      fpga/output/v1.fw
  7. BIN
      fpga/output/v2.fw

+ 15 - 0
esp32/max80/IP4.cpp

@@ -0,0 +1,15 @@
+#include "IP4.h"
+
+#include <lwip/inet.h>
+
+IP4::IP4(const char *str) {
+    in_addr_t addr;
+    if (str && inet_aton(str, &addr))
+	*this = addr;
+    else
+	*this = null_ip;
+}
+const char * IP4::cstr() {
+    in_addr_t addr = *this;
+    return inet_ntoa(addr);
+}

+ 55 - 0
esp32/max80/IP4.h

@@ -0,0 +1,55 @@
+#pragma once
+
+#include "common.h"
+#include <WiFi.h>
+#include <lwip/inet.h>
+#include <esp_wifi.h>
+
+//
+// There are no less than 3 different types for IP addresses used
+// by different APIs. This class attempts to unify them to mask the
+// differences.
+//
+class IP4 {
+private:
+    union {
+	uint8_t b[4];
+	uint32_t l;
+    };
+public:
+    constexpr IP4() : l{0} { }
+    constexpr IP4(nullptr_t) : l{0} { }
+    constexpr IP4(int ll) : l{(uint32_t)ll} { }
+    constexpr IP4(uint32_t ll) : l{ll} { }
+    constexpr IP4(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) :
+	b{b0,b1,b2,b3} { }
+    IP4(const IPAddress & ip) : l{ip} { }
+    constexpr IP4(const ip_addr_t & ip) : l{ip.u_addr.ip4.addr} { }
+    constexpr IP4(const esp_ip_addr_t & ip) : l{ip.u_addr.ip4.addr} { }
+    IP4(const char *str);
+
+    operator uint32_t () { return l; }
+    operator IPAddress () {
+	IPAddress ip(l);
+	return ip;
+    }
+    operator ip_addr_t () {
+	ip_addr_t ip = { };
+	ip.u_addr.ip4.addr = l;
+	return ip;
+    }
+    operator esp_ip_addr_t () {
+	esp_ip_addr_t ip = { };
+	ip.u_addr.ip4.addr = l;
+	return ip;
+    }
+    constexpr operator bool () { return l != 0; }
+    constexpr bool operator ! () { return l == 0; }
+    constexpr bool operator == (const IP4 &b) { return l == b.l; }
+    constexpr uint8_t operator [] (size_t n) { return b[n]; }
+    uint8_t & operator [] (size_t n) { return b[n]; }
+
+    const char *cstr();
+};
+
+constexpr IP4 null_ip(0);

+ 41 - 57
esp32/max80/wifi.cpp

@@ -14,10 +14,12 @@
 #include <esp_sntp.h>
 #include <esp_wifi.h>
 
+#include "IP4.h"
+
 WiFiUDP UDP;
 
 static const char *ssid, *password, *hostname;
-static IPAddress dnsserver;
+static IP4 dnsserver;
 static TimerHandle_t sta_failure_timer;
 
 enum connected {
@@ -28,27 +30,14 @@ enum connected {
 static volatile bool sta_timeout_enabled;
 static volatile unsigned int sta_timeout_count;
 static unsigned int connected;
-static const IPAddress null_ip(0,0,0,0);
 
-static inline bool setvar_ip(enum sysvars_enum var, const esp_ip_addr_t &ip)
-{
-    return setvar_ip(var, ip.u_addr.ip4.addr);
-}
-static inline bool setvar_ip(enum sysvars_enum var, const ip_addr_t &ip)
-{
-    return setvar_ip(var, ip.u_addr.ip4.addr);
-}
-static inline esp_ip_addr_t *ip_toesp(esp_ip_addr_t *eip, IPAddress ip)
+static inline bool setvar_ip4(enum sysvars_enum var, const IP4 &ip)
 {
-    memset(eip, 0, sizeof *eip);
-    eip->u_addr.ip4.addr = (uint32_t)ip;
-    return eip;
+    return setvar_ip(var, static_cast<uint32_t>(ip));
 }
-static inline ip_addr_t *ip_toaddr(ip_addr_t *eip, IPAddress ip)
+static inline IP4 getvar_ip4(enum sysvars_enum var)
 {
-    memset(eip, 0, sizeof *eip);
-    eip->u_addr.ip4.addr = (uint32_t)ip;
-    return eip;
+    return IP4(getvar_ip(var));
 }
 
 static void sta_bounce(void)
@@ -142,20 +131,15 @@ static void stop_services(void)
     }
 }
 
-static inline bool invalid_ip(const ip_addr_t *ip)
-{
-    return !memcmp(ip, &ip_addr_any, sizeof *ip);
-}
-
 static void sntp_server_show(void)
 {
-    const ip_addr_t *sntp_ip = sntp_getserver(0);
+    IP4 sntp_ip = *sntp_getserver(0);
 
-    if (!invalid_ip(sntp_ip)) {
-	printf("[SNTP] Time server: %s\n", inet_ntoa(*sntp_ip));
-	setvar_ip(status_net_sntp_server, *sntp_ip);
+    if (!sntp_ip) {
+	printf("[SNTP] Time server: %s\n", sntp_ip.cstr());
+	setvar_ip4(status_net_sntp_server, sntp_ip);
     } else {
-	setvar_ip(status_net_sntp_server, null_ip);
+	setvar_ip4(status_net_sntp_server, null_ip);
     }
 }
 
@@ -165,7 +149,7 @@ static void sntp_server_found(const char *name, const ip_addr_t *addr,
     (void)name;
     (void)arg;
 
-    if (invalid_ip(addr))
+    if (!IP4(*addr))
 	return;
 
     sntp_setserver(0, addr);
@@ -185,19 +169,19 @@ static void sntp_set_server(const char *name)
 
 static void dns_setup(void)
 {
-    const ip_addr_t *dns_ip = dns_getserver(0);
+    IP4 dns_ip = *dns_getserver(0);
 
-    if (invalid_ip(dns_ip) || getvar_bool(config_ip4_dhcp_nodns)) {
+    if (!dns_ip || getvar_bool(config_ip4_dhcp_nodns)) {
 	/* Static DNS server configuration */
-	ip_addr_t addr;
-	ip_toaddr(&addr, dnsserver);
-	if (memcmp(&addr, dns_ip, sizeof addr))
+	if (dns_ip != dnsserver) {
+	    ip_addr_t addr = dnsserver;
 	    dns_setserver(0, &addr);
+	}
     }
 
-    dns_ip = dns_getserver(0);
-    printf("[DNS]  DNS server: %s\n", inet_ntoa(*dns_ip));
-    setvar_ip(status_net_dns_server, *dns_ip);
+    dns_ip = *dns_getserver(0);
+    printf("[DNS]  DNS server: %s\n", dns_ip.cstr());
+    setvar_ip4(status_net_dns_server, dns_ip);
 }
 
 static void mdns_setup(void)
@@ -231,7 +215,7 @@ static void mdns_setup(void)
     snprintf(unique_name, sizeof unique_name, "MAX80-%s", serial_number);
     if (connected & CON_STA) {
 	mdns_ip_addr_t iplist;
-	ip_toesp(&iplist.addr, WiFi.localIP());
+	iplist.addr = IP4(WiFi.localIP());
 	iplist.next = NULL;
 	unique_mdns = mdns_delegate_hostname_add(unique_name, &iplist);
 	printf("[MDNS] mDNS unique hostname: %s\n", unique_name);
@@ -255,10 +239,11 @@ static void start_services(void)
     // If Arduino supported both of these at the same that would be
     // awesome, but it requires ESP-IDF reconfiguration...
     if (getvar_bool(config_sntp_enabled)) {
-	if (!invalid_ip(sntp_getserver(0))) {
+	IP4 sntp_ip = *sntp_getserver(0);
+	if (sntp_ip) {
 	    sntp_server_show();
 	} else {
-	    sntp_set_server(getenv("sntp.server"));
+	    sntp_set_server(getvar_str(config_sntp_server));
 	}
     }
 
@@ -280,8 +265,8 @@ static void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
     unsigned int prev_connected = connected;
     static int ap_clients;
     int prev_ap_clients = ap_clients;
-    IPAddress wifi_local_ip = WiFi.localIP();
-    const char *local_ip = wifi_local_ip.toString().c_str();
+    IP4 wifi_local_ip = WiFi.localIP();
+    const char *local_ip = wifi_local_ip.cstr();
 
     switch (event) {
     case ARDUINO_EVENT_WIFI_READY:
@@ -417,19 +402,19 @@ static void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
 
 	if (conn_change & CON_STA) {
 	    setvar_bool(status_net_sta_conn, connected & CON_STA);
-	    setvar_ip(status_net_sta_ip4,
+	    setvar_ip4(status_net_sta_ip4,
 		      connected & CON_STA ? wifi_local_ip : null_ip);
-	    setvar_ip(status_net_sta_ip4_mask, WiFi.subnetMask());
-	    setvar_ip(status_net_sta_ip4_gw, WiFi.gatewayIP());
+	    setvar_ip4(status_net_sta_ip4_mask, WiFi.subnetMask());
+	    setvar_ip4(status_net_sta_ip4_gw, WiFi.gatewayIP());
 	}
 	if (conn_change & CON_AP)
 	    setvar_bool(status_net_ap_conn, connected & CON_AP);
 	if (conn_change & CON_ETH) {
 	    setvar_bool(status_net_eth_conn, connected & CON_ETH);
-	    setvar_ip(status_net_eth_ip4,
+	    setvar_ip4(status_net_eth_ip4,
 		      connected & CON_STA ? wifi_local_ip : null_ip);
-	    setvar_ip(status_net_eth_ip4_mask, WiFi.subnetMask());
-	    setvar_ip(status_net_eth_ip4_gw, WiFi.gatewayIP());
+	    setvar_ip4(status_net_eth_ip4_mask, WiFi.subnetMask());
+	    setvar_ip4(status_net_eth_ip4_gw, WiFi.gatewayIP());
 	}
 
 	if (!ssid) {
@@ -459,9 +444,9 @@ static void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
 static void wifi_config_ap(void)
 {
     /* No network configuration set */
-    IPAddress AP_IP      = IPAddress(192,168,0,1);
-    IPAddress AP_Netmask = IPAddress(255,255,255,0);
-    IPAddress AP_Gateway = IPAddress(0,0,0,0); // No gateway
+    IP4 AP_IP      = IP4(192,168,0,1);
+    IP4 AP_Netmask = IP4(255,255,255,0);
+    IP4 AP_Gateway = IP4(0,0,0,0); // No gateway
     unsigned int channel = (time(NULL) % 11) + 1;	   // Pseudo-random
     uint8_t mac[6];
     static char ap_ssid[64];
@@ -473,12 +458,11 @@ static void wifi_config_ap(void)
     snprintf(ap_ssid, sizeof ap_ssid, "MAX80_%02X%02X",
 	     efuse_default_mac[4], efuse_default_mac[5]);
 
-    printf("[WIFI] AP SSID %s IP %s netmask %s channel %u\n",
-	   ap_ssid, AP_IP.toString().c_str(),
-	   AP_Netmask.toString().c_str(), channel);
+    printf("[WIFI] AP SSID %s IP %s ", ap_ssid, AP_IP.cstr());
+    printf("netmask %s channel %u\n", AP_Netmask.cstr(), channel);
     setvar_str(status_net_ap_ssid, ap_ssid);
-    setvar_ip(status_net_ap_ip4, AP_IP);
-    setvar_ip(status_net_ap_ip4_mask, AP_Netmask);
+    setvar_ip4(status_net_ap_ip4, AP_IP);
+    setvar_ip4(status_net_ap_ip4_mask, AP_Netmask);
     setvar_uint(status_net_ap_clients, 0);
 
     printf("WiFi.softAP\n");
@@ -533,7 +517,7 @@ static void wifi_config(void)
     ssid         = dupstr(getvar_str(config_wifi_ssid));
     password     = dupstr(getvar_str(config_wifi_psk));
     hostname     = dupstr(getvar_str(config_hostname));
-    dnsserver    = IPAddress(getvar_ip(config_ip4_dns));
+    dnsserver    = getvar_ip4(config_ip4_dns);
 
     force_conn_update = true;
 

BIN
esp32/output/max80.ino.bin


BIN
fpga/output/max80.fw


BIN
fpga/output/v1.fw


BIN
fpga/output/v2.fw