Skip to content

Commit d8d2f09

Browse files
committed
Cleanup
1 parent 1e6725a commit d8d2f09

File tree

6 files changed

+37
-250
lines changed

6 files changed

+37
-250
lines changed

libraries/Networking/src/ESP_Network_Events.h

+4-14
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,12 @@
2424
#endif
2525

2626
#if SOC_WIFI_SUPPORTED
27-
static const int AP_STARTED_BIT = BIT0;
28-
static const int AP_HAS_IP6_BIT = BIT1;
29-
static const int AP_HAS_CLIENT_BIT = BIT2;
30-
static const int AP_WANT_IP6_BIT = BIT3;
31-
static const int STA_STARTED_BIT = BIT4;
32-
static const int STA_CONNECTED_BIT = BIT5;
33-
static const int STA_HAS_IP_BIT = BIT6;
34-
static const int STA_HAS_IP6_BIT = BIT7;
35-
static const int STA_HAS_IP6_GLOBAL_BIT = BIT8;
36-
static const int STA_WANT_IP6_BIT = BIT9;
37-
static const int WIFI_SCANNING_BIT = BIT10;
38-
static const int WIFI_SCAN_DONE_BIT= BIT11;
27+
static const int WIFI_SCANNING_BIT = BIT0;
28+
static const int WIFI_SCAN_DONE_BIT= BIT1;
3929
#endif
4030

41-
static const int NET_DNS_IDLE_BIT = BIT12;
42-
static const int NET_DNS_DONE_BIT = BIT13;
31+
static const int NET_DNS_IDLE_BIT = BIT2;
32+
static const int NET_DNS_DONE_BIT = BIT3;
4333

4434
#define NET_HAS_IP6_GLOBAL_BIT 0
4535

libraries/Networking/src/ESP_Network_Manager.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,24 @@ String ESP_Network_Manager::macAddress(void){
138138
return String(macStr);
139139
}
140140

141+
static char default_hostname[32] = {0,};
142+
143+
const char * ESP_Network_Manager::getHostname()
144+
{
145+
if(default_hostname[0] == 0){
146+
uint8_t eth_mac[6];
147+
esp_base_mac_addr_get(eth_mac);
148+
snprintf(default_hostname, 32, "%s%02X%02X%02X", CONFIG_IDF_TARGET "-", eth_mac[3], eth_mac[4], eth_mac[5]);
149+
}
150+
return (const char *)default_hostname;
151+
}
152+
153+
bool ESP_Network_Manager::setHostname(const char * name)
154+
{
155+
if(name){
156+
snprintf(default_hostname, 32, "%s", name);
157+
}
158+
return true;
159+
}
160+
141161
ESP_Network_Manager Network;

libraries/Networking/src/ESP_Network_Manager.h

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class ESP_Network_Manager : public ESP_Network_Events {
1717
int hostByName(const char *aHostname, IPAddress &aResult, bool preferV6=false);
1818
uint8_t * macAddress(uint8_t * mac);
1919
String macAddress();
20+
21+
static const char * getHostname();
22+
static bool setHostname(const char * hostname);
23+
static bool hostname(const String& aHostname) { return setHostname(aHostname.c_str()); }
2024
};
2125

2226
extern ESP_Network_Manager Network;

libraries/WiFi/src/STA.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t in
135135

136136
if(event == ARDUINO_EVENT_WIFI_STA_START) {
137137
_sta_network_if->_setStatus(WL_DISCONNECTED);
138-
// if(esp_wifi_set_ps(_sleepEnabled) != ESP_OK){
139-
// log_e("esp_wifi_set_ps failed");
140-
// }
138+
if(esp_wifi_set_ps(WiFi.getSleep()) != ESP_OK){
139+
log_e("esp_wifi_set_ps failed");
140+
}
141141
} else if(event == ARDUINO_EVENT_WIFI_STA_STOP) {
142142
_sta_network_if->_setStatus(WL_STOPPED);
143143
} else if(event == ARDUINO_EVENT_WIFI_STA_CONNECTED) {

libraries/WiFi/src/WiFiGeneric.cpp

+6-229
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,8 @@ extern "C" {
5454
#include "sdkconfig.h"
5555

5656
ESP_EVENT_DEFINE_BASE(ARDUINO_EVENTS);
57-
/*
58-
* Private (exposable) methods
59-
* */
60-
static char default_hostname[32] = {0,};
61-
static const char * get_esp_netif_hostname(){
62-
if(default_hostname[0] == 0){
63-
uint8_t eth_mac[6];
64-
esp_wifi_get_mac((wifi_interface_t)WIFI_IF_STA, eth_mac);
65-
snprintf(default_hostname, 32, "%s%02X%02X%02X", CONFIG_IDF_TARGET "-", eth_mac[3], eth_mac[4], eth_mac[5]);
66-
}
67-
return (const char *)default_hostname;
68-
}
69-
static void set_esp_netif_hostname(const char * name){
70-
if(name){
71-
snprintf(default_hostname, 32, "%s", name);
72-
}
73-
}
74-
75-
76-
77-
78-
79-
80-
8157

8258
static esp_netif_t* esp_netifs[ESP_IF_MAX] = {NULL, NULL, NULL};
83-
esp_interface_t get_esp_netif_interface(esp_netif_t* esp_netif){
84-
for(int i=0; i<ESP_IF_MAX; i++){
85-
if(esp_netifs[i] != NULL && esp_netifs[i] == esp_netif){
86-
return (esp_interface_t)i;
87-
}
88-
}
89-
return ESP_IF_MAX;
90-
}
91-
92-
void add_esp_interface_netif(esp_interface_t interface, esp_netif_t* esp_netif){
93-
if(interface < ESP_IF_MAX){
94-
esp_netifs[interface] = esp_netif;
95-
}
96-
}
9759

9860
esp_netif_t* get_esp_interface_netif(esp_interface_t interface){
9961
if(interface < ESP_IF_MAX){
@@ -102,149 +64,6 @@ esp_netif_t* get_esp_interface_netif(esp_interface_t interface){
10264
return NULL;
10365
}
10466

105-
esp_err_t set_esp_interface_hostname(esp_interface_t interface, const char * hostname){
106-
if(interface < ESP_IF_MAX){
107-
return esp_netif_set_hostname(esp_netifs[interface], hostname);
108-
}
109-
return ESP_FAIL;
110-
}
111-
112-
esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress(), IPAddress dhcp_lease_start=INADDR_NONE){
113-
esp_netif_t *esp_netif = esp_netifs[interface];
114-
esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
115-
esp_netif_ip_info_t info;
116-
info.ip.addr = static_cast<uint32_t>(local_ip);
117-
info.gw.addr = static_cast<uint32_t>(gateway);
118-
info.netmask.addr = static_cast<uint32_t>(subnet);
119-
120-
log_v("Configuring %s static IP: " IPSTR ", MASK: " IPSTR ", GW: " IPSTR,
121-
interface == ESP_IF_WIFI_STA ? "Station" :
122-
interface == ESP_IF_WIFI_AP ? "SoftAP" : "Ethernet",
123-
IP2STR(&info.ip), IP2STR(&info.netmask), IP2STR(&info.gw));
124-
125-
esp_err_t err = ESP_OK;
126-
if(interface != ESP_IF_WIFI_AP){
127-
err = esp_netif_dhcpc_get_status(esp_netif, &status);
128-
if(err){
129-
log_e("DHCPC Get Status Failed! 0x%04x", err);
130-
return err;
131-
}
132-
err = esp_netif_dhcpc_stop(esp_netif);
133-
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
134-
log_e("DHCPC Stop Failed! 0x%04x", err);
135-
return err;
136-
}
137-
err = esp_netif_set_ip_info(esp_netif, &info);
138-
if(err){
139-
log_e("Netif Set IP Failed! 0x%04x", err);
140-
return err;
141-
}
142-
if(info.ip.addr == 0){
143-
err = esp_netif_dhcpc_start(esp_netif);
144-
if(err){
145-
log_e("DHCPC Start Failed! 0x%04x", err);
146-
return err;
147-
}
148-
}
149-
} else {
150-
err = esp_netif_dhcps_get_status(esp_netif, &status);
151-
if(err){
152-
log_e("DHCPS Get Status Failed! 0x%04x", err);
153-
return err;
154-
}
155-
err = esp_netif_dhcps_stop(esp_netif);
156-
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
157-
log_e("DHCPS Stop Failed! 0x%04x", err);
158-
return err;
159-
}
160-
err = esp_netif_set_ip_info(esp_netif, &info);
161-
if(err){
162-
log_e("Netif Set IP Failed! 0x%04x", err);
163-
return err;
164-
}
165-
166-
dhcps_lease_t lease;
167-
lease.enable = true;
168-
uint8_t CIDR = WiFiGenericClass::calculateSubnetCIDR(subnet);
169-
log_v("SoftAP: %s | Gateway: %s | DHCP Start: %s | Netmask: %s", local_ip.toString().c_str(), gateway.toString().c_str(), dhcp_lease_start.toString().c_str(), subnet.toString().c_str());
170-
// netmask must have room for at least 12 IP addresses (AP + GW + 10 DHCP Leasing addresses)
171-
// netmask also must be limited to the last 8 bits of IPv4, otherwise this function won't work
172-
// IDF NETIF checks netmask for the 3rd byte: https://github.com/espressif/esp-idf/blob/master/components/esp_netif/lwip/esp_netif_lwip.c#L1857-L1862
173-
if (CIDR > 28 || CIDR < 24) {
174-
log_e("Bad netmask. It must be from /24 to /28 (255.255.255. 0<->240)");
175-
return ESP_FAIL; // ESP_FAIL if initializing failed
176-
}
177-
#define _byte_swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
178-
// The code below is ready for any netmask, not limited to 255.255.255.0
179-
uint32_t netmask = _byte_swap32(info.netmask.addr);
180-
uint32_t ap_ipaddr = _byte_swap32(info.ip.addr);
181-
uint32_t dhcp_ipaddr = _byte_swap32(static_cast<uint32_t>(dhcp_lease_start));
182-
dhcp_ipaddr = dhcp_ipaddr == 0 ? ap_ipaddr + 1 : dhcp_ipaddr;
183-
uint32_t leaseStartMax = ~netmask - 10;
184-
// there will be 10 addresses for DHCP to lease
185-
lease.start_ip.addr = dhcp_ipaddr;
186-
lease.end_ip.addr = lease.start_ip.addr + 10;
187-
// Check if local_ip is in the same subnet as the dhcp leasing range initial address
188-
if ((ap_ipaddr & netmask) != (dhcp_ipaddr & netmask)) {
189-
log_e("The AP IP address (%s) and the DHCP start address (%s) must be in the same subnet",
190-
local_ip.toString().c_str(), IPAddress(_byte_swap32(dhcp_ipaddr)).toString().c_str());
191-
return ESP_FAIL; // ESP_FAIL if initializing failed
192-
}
193-
// prevents DHCP lease range to overflow subnet range
194-
if ((dhcp_ipaddr & ~netmask) >= leaseStartMax) {
195-
// make first DHCP lease addr stay in the begining of the netmask range
196-
lease.start_ip.addr = (dhcp_ipaddr & netmask) + 1;
197-
lease.end_ip.addr = lease.start_ip.addr + 10;
198-
log_w("DHCP Lease out of range - Changing DHCP leasing start to %s", IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str());
199-
}
200-
// Check if local_ip is within DHCP range
201-
if (ap_ipaddr >= lease.start_ip.addr && ap_ipaddr <= lease.end_ip.addr) {
202-
log_e("The AP IP address (%s) can't be within the DHCP range (%s -- %s)",
203-
local_ip.toString().c_str(), IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str(), IPAddress(_byte_swap32(lease.end_ip.addr)).toString().c_str());
204-
return ESP_FAIL; // ESP_FAIL if initializing failed
205-
}
206-
// Check if gateway is within DHCP range
207-
uint32_t gw_ipaddr = _byte_swap32(info.gw.addr);
208-
bool gw_in_same_subnet = (gw_ipaddr & netmask) == (ap_ipaddr & netmask);
209-
if (gw_in_same_subnet && gw_ipaddr >= lease.start_ip.addr && gw_ipaddr <= lease.end_ip.addr) {
210-
log_e("The GatewayP address (%s) can't be within the DHCP range (%s -- %s)",
211-
gateway.toString().c_str(), IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str(), IPAddress(_byte_swap32(lease.end_ip.addr)).toString().c_str());
212-
return ESP_FAIL; // ESP_FAIL if initializing failed
213-
}
214-
// all done, just revert back byte order of DHCP lease range
215-
lease.start_ip.addr = _byte_swap32(lease.start_ip.addr);
216-
lease.end_ip.addr = _byte_swap32(lease.end_ip.addr);
217-
log_v("DHCP Server Range: %s to %s", IPAddress(lease.start_ip.addr).toString().c_str(), IPAddress(lease.end_ip.addr).toString().c_str());
218-
// Following block is commented because it breaks AP DHCPS on recent ESP-IDF
219-
// err = esp_netif_dhcps_option(
220-
// esp_netif,
221-
// ESP_NETIF_OP_SET,
222-
// ESP_NETIF_SUBNET_MASK,
223-
// (void*)&info.netmask.addr, sizeof(info.netmask.addr)
224-
// );
225-
// if(err){
226-
// log_e("DHCPS Set Netmask Failed! 0x%04x", err);
227-
// return err;
228-
// }
229-
err = esp_netif_dhcps_option(
230-
esp_netif,
231-
ESP_NETIF_OP_SET,
232-
ESP_NETIF_REQUESTED_IP_ADDRESS,
233-
(void*)&lease, sizeof(dhcps_lease_t)
234-
);
235-
if(err){
236-
log_e("DHCPS Set Lease Failed! 0x%04x", err);
237-
return err;
238-
}
239-
err = esp_netif_dhcps_start(esp_netif);
240-
if(err){
241-
log_e("DHCPS Start Failed! 0x%04x", err);
242-
return err;
243-
}
244-
}
245-
return err;
246-
}
247-
24867
static void _arduino_event_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
24968
arduino_event_t arduino_event;
25069
arduino_event.event_id = ARDUINO_EVENT_MAX;
@@ -351,11 +170,6 @@ static bool initWiFiEvents(){
351170
return false;
352171
}
353172

354-
// if(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
355-
// log_e("event_handler_instance_register for IP_EVENT Failed!");
356-
// return false;
357-
// }
358-
359173
if(esp_event_handler_instance_register(SC_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
360174
log_e("event_handler_instance_register for SC_EVENT Failed!");
361175
return false;
@@ -375,11 +189,6 @@ static bool deinitWiFiEvents(){
375189
return false;
376190
}
377191

378-
// if(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb)){
379-
// log_e("esp_event_handler_unregister for IP_EVENT Failed!");
380-
// return false;
381-
// }
382-
383192
if(esp_event_handler_unregister(SC_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb)){
384193
log_e("esp_event_handler_unregister for SC_EVENT Failed!");
385194
return false;
@@ -545,13 +354,12 @@ const char * WiFiGenericClass::eventName(arduino_event_id_t id) {
545354

546355
const char * WiFiGenericClass::getHostname()
547356
{
548-
return get_esp_netif_hostname();
357+
return ESP_Network_Manager::getHostname();
549358
}
550359

551360
bool WiFiGenericClass::setHostname(const char * hostname)
552361
{
553-
set_esp_netif_hostname(hostname);
554-
return true;
362+
return ESP_Network_Manager::setHostname(hostname);
555363
}
556364

557365
/**
@@ -578,37 +386,6 @@ void WiFiGenericClass::_eventCallback(arduino_event_t *event)
578386
}
579387
}
580388

581-
bool WiFiGenericClass::_isReconnectableReason(uint8_t reason) {
582-
switch(reason) {
583-
case WIFI_REASON_UNSPECIFIED:
584-
//Timeouts (retry)
585-
case WIFI_REASON_AUTH_EXPIRE:
586-
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
587-
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
588-
case WIFI_REASON_802_1X_AUTH_FAILED:
589-
case WIFI_REASON_HANDSHAKE_TIMEOUT:
590-
//Transient error (reconnect)
591-
case WIFI_REASON_AUTH_LEAVE:
592-
case WIFI_REASON_ASSOC_EXPIRE:
593-
case WIFI_REASON_ASSOC_TOOMANY:
594-
case WIFI_REASON_NOT_AUTHED:
595-
case WIFI_REASON_NOT_ASSOCED:
596-
case WIFI_REASON_ASSOC_NOT_AUTHED:
597-
case WIFI_REASON_MIC_FAILURE:
598-
case WIFI_REASON_IE_IN_4WAY_DIFFERS:
599-
case WIFI_REASON_INVALID_PMKID:
600-
case WIFI_REASON_BEACON_TIMEOUT:
601-
case WIFI_REASON_NO_AP_FOUND:
602-
case WIFI_REASON_ASSOC_FAIL:
603-
case WIFI_REASON_CONNECTION_FAIL:
604-
case WIFI_REASON_AP_TSF_RESET:
605-
case WIFI_REASON_ROAMING:
606-
return true;
607-
default:
608-
return false;
609-
}
610-
}
611-
612389
/**
613390
* Return the current channel associated with the network
614391
* @return channel (1-13)
@@ -671,7 +448,7 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
671448

672449
esp_err_t err;
673450
if(m & WIFI_MODE_STA){
674-
err = set_esp_interface_hostname(ESP_IF_WIFI_STA, get_esp_netif_hostname());
451+
err = esp_netif_set_hostname(esp_netifs[ESP_IF_WIFI_STA], ESP_Network_Manager::getHostname());
675452
if(err){
676453
log_e("Could not set hostname! %d", err);
677454
return false;
@@ -787,7 +564,7 @@ bool WiFiGenericClass::setSleep(wifi_ps_type_t sleepType)
787564
{
788565
if(sleepType != _sleepEnabled){
789566
_sleepEnabled = sleepType;
790-
if((getMode() & WIFI_MODE_STA) != 0){
567+
if(WiFi.STA.started()){
791568
if(esp_wifi_set_ps(_sleepEnabled) != ESP_OK){
792569
log_e("esp_wifi_set_ps failed!");
793570
return false;
@@ -813,7 +590,7 @@ wifi_ps_type_t WiFiGenericClass::getSleep()
813590
* @return ok
814591
*/
815592
bool WiFiGenericClass::setTxPower(wifi_power_t power){
816-
if((getStatusBits() & (STA_STARTED_BIT | AP_STARTED_BIT)) == 0){
593+
if(!WiFi.STA.started() && !WiFi.AP.started()){
817594
log_w("Neither AP or STA has been started");
818595
return false;
819596
}
@@ -822,7 +599,7 @@ bool WiFiGenericClass::setTxPower(wifi_power_t power){
822599

823600
wifi_power_t WiFiGenericClass::getTxPower(){
824601
int8_t power;
825-
if((getStatusBits() & (STA_STARTED_BIT | AP_STARTED_BIT)) == 0){
602+
if(!WiFi.STA.started() && !WiFi.AP.started()){
826603
log_w("Neither AP or STA has been started");
827604
return WIFI_POWER_19_5dBm;
828605
}

libraries/WiFi/src/WiFiGeneric.h

-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ class WiFiGenericClass
135135
static int setStatusBits(int bits);
136136
static int clearStatusBits(int bits);
137137

138-
private:
139-
static bool _isReconnectableReason(uint8_t reason);
140-
141-
protected:
142138
friend class WiFiSTAClass;
143139
friend class WiFiScanClass;
144140
friend class WiFiAPClass;

0 commit comments

Comments
 (0)