Skip to content

Use WiFi.mode to enable/disable the Network Interfaces #9436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libraries/Network/src/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,10 @@ IPAddress NetworkInterface::globalIPv6() const

size_t NetworkInterface::printTo(Print & out) const {
size_t bytes = 0;
bytes += out.print(esp_netif_get_desc(_esp_netif));
const char * dscr = esp_netif_get_desc(_esp_netif);
if(dscr != NULL){
bytes += out.print(dscr);
}
bytes += out.print(":");
if(esp_netif_is_netif_up(_esp_netif)){
bytes += out.print(" <UP");
Expand Down
46 changes: 22 additions & 24 deletions libraries/WiFi/src/AP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ static void _ap_event_cb(void* arg, esp_event_base_t event_base, int32_t event_i
}
}

static void _onApArduinoEvent(arduino_event_id_t event, arduino_event_info_t info)
static void _onApArduinoEvent(arduino_event_t *ev)
{
if(_ap_network_if == NULL || event < ARDUINO_EVENT_WIFI_AP_START || event > ARDUINO_EVENT_WIFI_AP_GOT_IP6){
if(_ap_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_AP_START || ev->event_id > ARDUINO_EVENT_WIFI_AP_GOT_IP6){
return;
}
log_d("Arduino AP Event: %d - %s", event, Network.eventName(event));
if(event == ARDUINO_EVENT_WIFI_AP_START) {
log_d("Arduino AP Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
if(ev->event_id == ARDUINO_EVENT_WIFI_AP_START) {
if (_ap_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT){
esp_err_t err = esp_netif_create_ip6_linklocal(_ap_network_if->netif());
if(err != ESP_OK){
Expand Down Expand Up @@ -157,37 +157,21 @@ APClass::~APClass(){
_ap_network_if = NULL;
}

bool APClass::begin(){

Network.begin();
bool APClass::onEnable(){
if(_ap_ev_instance == NULL && esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_ap_event_cb, this, &_ap_ev_instance)){
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
return false;
}
if(_esp_netif == NULL){
Network.onSysEvent(_onApArduinoEvent);
}

if(!WiFi.enableAP(true)) {
log_e("AP enable failed!");
return false;
}

// attach events and esp_netif here
if(_esp_netif == NULL){
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_AP);
/* attach to receive events */
initNetif(ESP_NETIF_ID_AP);
}

return true;
}

bool APClass::end(){
if(!WiFi.enableAP(false)) {
log_e("AP disable failed!");
return false;
}
bool APClass::onDisable(){
Network.removeEvent(_onApArduinoEvent);
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
// That would be done by WiFi.enableAP(false) if STA is not enabled, or when it gets disabled
Expand All @@ -200,15 +184,29 @@ bool APClass::end(){
return true;
}

bool APClass::begin(){
if(!WiFi.enableAP(true)) {
log_e("AP enable failed!");
return false;
}
return true;
}

bool APClass::end(){
if(!WiFi.enableAP(false)) {
log_e("AP disable failed!");
return false;
}
return true;
}

bool APClass::create(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection, bool ftm_responder){
if(!ssid || *ssid == 0) {
// fail SSID missing
log_e("SSID missing!");
return false;
}

if(passphrase && (strlen(passphrase) > 0 && strlen(passphrase) < 8)) {
// fail passphrase too short
log_e("passphrase too short!");
return false;
}
Expand Down
70 changes: 37 additions & 33 deletions libraries/WiFi/src/STA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,22 @@ static const char * auth_mode_str(int authmode)
}
#endif

static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t info)
static void _onStaArduinoEvent(arduino_event_t *ev)
{
if(_sta_network_if == NULL || event < ARDUINO_EVENT_WIFI_STA_START || event > ARDUINO_EVENT_WIFI_STA_LOST_IP){
if(_sta_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_STA_START || ev->event_id > ARDUINO_EVENT_WIFI_STA_LOST_IP){
return;
}
static bool first_connect = true;
log_d("Arduino STA Event: %d - %s", event, Network.eventName(event));
log_d("Arduino STA Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));

if(event == ARDUINO_EVENT_WIFI_STA_START) {
if(ev->event_id == ARDUINO_EVENT_WIFI_STA_START) {
_sta_network_if->_setStatus(WL_DISCONNECTED);
if(esp_wifi_set_ps(WiFi.getSleep()) != ESP_OK){
log_e("esp_wifi_set_ps failed");
}
} else if(event == ARDUINO_EVENT_WIFI_STA_STOP) {
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_STOP) {
_sta_network_if->_setStatus(WL_STOPPED);
} else if(event == ARDUINO_EVENT_WIFI_STA_CONNECTED) {
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_CONNECTED) {
_sta_network_if->_setStatus(WL_IDLE_STATUS);
if (_sta_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT){
esp_err_t err = esp_netif_create_ip6_linklocal(_sta_network_if->netif());
Expand All @@ -149,8 +149,8 @@ static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t in
log_v("Enabled IPv6 Link Local on %s", _sta_network_if->desc());
}
}
} else if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
uint8_t reason = info.wifi_sta_disconnected.reason;
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
uint8_t reason = ev->event_info.wifi_sta_disconnected.reason;
// Reason 0 causes crash, use reason 1 (UNSPECIFIED) instead
if(!reason)
reason = WIFI_REASON_UNSPECIFIED;
Expand Down Expand Up @@ -186,18 +186,18 @@ static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t in
_sta_network_if->disconnect();
_sta_network_if->connect();
}
} else if(event == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
uint8_t * ip = (uint8_t *)&(info.got_ip.ip_info.ip.addr);
uint8_t * mask = (uint8_t *)&(info.got_ip.ip_info.netmask.addr);
uint8_t * gw = (uint8_t *)&(info.got_ip.ip_info.gw.addr);
uint8_t * ip = (uint8_t *)&(ev->event_info.got_ip.ip_info.ip.addr);
uint8_t * mask = (uint8_t *)&(ev->event_info.got_ip.ip_info.netmask.addr);
uint8_t * gw = (uint8_t *)&(ev->event_info.got_ip.ip_info.gw.addr);
log_d("STA IP: %u.%u.%u.%u, MASK: %u.%u.%u.%u, GW: %u.%u.%u.%u",
ip[0], ip[1], ip[2], ip[3],
mask[0], mask[1], mask[2], mask[3],
gw[0], gw[1], gw[2], gw[3]);
#endif
_sta_network_if->_setStatus(WL_CONNECTED);
} else if(event == ARDUINO_EVENT_WIFI_STA_LOST_IP) {
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_LOST_IP) {
_sta_network_if->_setStatus(WL_IDLE_STATUS);
}
}
Expand Down Expand Up @@ -288,29 +288,42 @@ bool STAClass::bandwidth(wifi_bandwidth_t bandwidth) {
return true;
}

bool STAClass::begin(bool tryConnect){

Network.begin();
bool STAClass::onEnable(){
if(_sta_ev_instance == NULL && esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb, this, &_sta_ev_instance)){
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
return false;
}
if(_esp_netif == NULL){
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_STA);
if(_esp_netif == NULL){
log_e("STA was enabled, but netif is NULL???");
return false;
}
/* attach to receive events */
Network.onSysEvent(_onStaArduinoEvent);
initNetif(ESP_NETIF_ID_STA);
}
return true;
}

bool STAClass::onDisable(){
Network.removeEvent(_onStaArduinoEvent);
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
// That would be done by WiFi.enableSTA(false) if AP is not enabled, or when it gets disabled
_esp_netif = NULL;
destroyNetif();
if(_sta_ev_instance != NULL){
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb);
_sta_ev_instance = NULL;
}
return true;
}

bool STAClass::begin(bool tryConnect){
if(!WiFi.enableSTA(true)) {
log_e("STA enable failed!");
return false;
}

// attach events and esp_netif here
if(_esp_netif == NULL){
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_STA);
/* attach to receive events */
initNetif(ESP_NETIF_ID_STA);
}

if(tryConnect){
return connect();
}
Expand All @@ -322,15 +335,6 @@ bool STAClass::end(){
log_e("STA disable failed!");
return false;
}
Network.removeEvent(_onStaArduinoEvent);
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
// That would be done by WiFi.enableSTA(false) if AP is not enabled, or when it gets disabled
_esp_netif = NULL;
destroyNetif();
if(_sta_ev_instance != NULL){
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb);
_sta_ev_instance = NULL;
}
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions libraries/WiFi/src/WiFiAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class APClass: public NetworkInterface {

protected:
size_t printDriverInfo(Print & out) const;

friend class WiFiGenericClass;
bool onEnable();
bool onDisable();
};

// ----------------------------------------------------------------------------------------------
Expand Down
33 changes: 31 additions & 2 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,36 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
return false;
}
Network.onSysEvent(_eventCallback);
} else if(cm && !m){
}

if(((m & WIFI_MODE_STA) != 0) && ((cm & WIFI_MODE_STA) == 0)){
// we are enabling STA interface
WiFi.STA.onEnable();
}
if(((m & WIFI_MODE_AP) != 0) && ((cm & WIFI_MODE_AP) == 0)){
// we are enabling AP interface
WiFi.AP.onEnable();
}

if(cm && !m){
// Turn OFF WiFi
if(!espWiFiStop()){
return false;
}
if((cm & WIFI_MODE_STA) != 0){
// we are disabling STA interface
WiFi.STA.onDisable();
}
if((cm & WIFI_MODE_AP) != 0){
// we are disabling AP interface
WiFi.AP.onDisable();
}
Network.removeEvent(_eventCallback);
return true;
}

esp_err_t err;
if(m & WIFI_MODE_STA){
if(((m & WIFI_MODE_STA) != 0) && ((cm & WIFI_MODE_STA) == 0)){
err = esp_netif_set_hostname(esp_netifs[ESP_IF_WIFI_STA], NetworkManager::getHostname());
if(err){
log_e("Could not set hostname! %d", err);
Expand All @@ -493,6 +512,16 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
log_e("Could not set mode! %d", err);
return false;
}

if(((m & WIFI_MODE_STA) == 0) && ((cm & WIFI_MODE_STA) != 0)){
// we are disabling STA interface (but AP is ON)
WiFi.STA.onDisable();
}
if(((m & WIFI_MODE_AP) == 0) && ((cm & WIFI_MODE_AP) != 0)){
// we are disabling AP interface (but STA is ON)
WiFi.AP.onDisable();
}

if(_long_range){
if(m & WIFI_MODE_STA){
err = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR);
Expand Down
4 changes: 4 additions & 0 deletions libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class STAClass: public NetworkInterface {
wl_status_t _status;

size_t printDriverInfo(Print & out) const;

friend class WiFiGenericClass;
bool onEnable();
bool onDisable();
};

// ----------------------------------------------------------------------------------------------
Expand Down