Skip to content

Add support for WiFi long range mode #3190

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 6 commits into from
Oct 6, 2019
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
42 changes: 33 additions & 9 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ extern "C" {

#include "esp32-hal-log.h"
#include <vector>

#include "sdkconfig.h"

static xQueueHandle _network_event_queue;
Expand Down Expand Up @@ -133,24 +132,19 @@ static bool wifiLowLevelDeinit(){

static bool _esp_wifi_started = false;

static bool espWiFiStart(bool persistent){
static bool espWiFiStart(){
if(_esp_wifi_started){
return true;
}
if(!wifiLowLevelInit(persistent)){
return false;
}
esp_err_t err = esp_wifi_start();
if (err != ESP_OK) {
log_e("esp_wifi_start %d", err);
wifiLowLevelDeinit();
return false;
}
_esp_wifi_started = true;
system_event_t event;
event.event_id = SYSTEM_EVENT_WIFI_READY;
WiFiGenericClass::_eventCallback(nullptr, &event);

return true;
}

Expand Down Expand Up @@ -190,6 +184,7 @@ wifi_event_id_t WiFiEventCbList::current_id = 1;
static std::vector<WiFiEventCbList_t> cbEventList;

bool WiFiGenericClass::_persistent = true;
bool WiFiGenericClass::_long_range = false;
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;

WiFiGenericClass::WiFiGenericClass()
Expand Down Expand Up @@ -471,6 +466,16 @@ void WiFiGenericClass::persistent(bool persistent)
}


/**
* enable WiFi long range mode
* @param enable
*/
void WiFiGenericClass::enableLongRange(bool enable)
{
_long_range = enable;
}


/**
* set new mode
* @param m WiFiMode_t
Expand All @@ -482,7 +487,7 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
return true;
}
if(!cm && m){
if(!espWiFiStart(_persistent)){
if(!wifiLowLevelInit(_persistent)){
return false;
}
} else if(cm && !m){
Expand All @@ -495,6 +500,25 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
log_e("Could not set mode! %d", err);
return false;
}
if(_long_range){
if(m & WIFI_MODE_STA){
err = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR);
if(err != ESP_OK){
log_e("Could not enable long range on STA! %d", err);
return false;
}
}
if(m & WIFI_MODE_AP){
err = esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_LR);
if(err != ESP_OK){
log_e("Could not enable long range on AP! %d", err);
return false;
}
}
}
if(!espWiFiStart()){
return false;
}
return true;
}

Expand All @@ -504,7 +528,7 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
*/
wifi_mode_t WiFiGenericClass::getMode()
{
if(!_esp_wifi_started){
if(!lowLevelInitDone){
return WIFI_MODE_NULL;
}
wifi_mode_t mode;
Expand Down
2 changes: 2 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class WiFiGenericClass
int32_t channel(void);

void persistent(bool persistent);
void enableLongRange(bool enable);

static bool mode(wifi_mode_t);
static wifi_mode_t getMode();
Expand All @@ -100,6 +101,7 @@ class WiFiGenericClass

protected:
static bool _persistent;
static bool _long_range;
static wifi_mode_t _forceSleepLastMode;

static int setStatusBits(int bits);
Expand Down