Skip to content

Commit 50984e5

Browse files
author
Veijo Pesonen
committed
ESP8266: makes usable channels runtime configurable
1 parent eb27a23 commit 50984e5

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ ESP8266Interface::ESP8266Interface()
6868
memset(_cbs, 0, sizeof(_cbs));
6969
memset(ap_ssid, 0, sizeof(ap_ssid));
7070
memset(ap_pass, 0, sizeof(ap_pass));
71-
memset(_country_code, 0, sizeof(_country_code));
72-
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_country_code));
71+
72+
_ch_info.track_ap = true;
73+
strncpy(_ch_info.country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_ch_info.country_code));
74+
_ch_info.channel_start = MBED_CONF_ESP8266_CHANNEL_START;
75+
_ch_info.channels = MBED_CONF_ESP8266_CHANNELS;
7376

7477
_esp.sigio(this, &ESP8266Interface::event);
7578
_esp.set_timeout();
@@ -99,8 +102,11 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
99102
memset(_cbs, 0, sizeof(_cbs));
100103
memset(ap_ssid, 0, sizeof(ap_ssid));
101104
memset(ap_pass, 0, sizeof(ap_pass));
102-
memset(_country_code, 0, sizeof(_country_code));
103-
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_country_code));
105+
106+
_ch_info.track_ap = true;
107+
strncpy(_ch_info.country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_ch_info.country_code));
108+
_ch_info.channel_start = MBED_CONF_ESP8266_CHANNEL_START;
109+
_ch_info.channels = MBED_CONF_ESP8266_CHANNELS;
104110

105111
_esp.sigio(this, &ESP8266Interface::event);
106112
_esp.set_timeout();
@@ -411,7 +417,7 @@ nsapi_error_t ESP8266Interface::_init(void)
411417
if (!_esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION)) {
412418
return NSAPI_ERROR_DEVICE_ERROR;
413419
}
414-
if (!_esp.set_country_code_policy(true, _country_code, MBED_CONF_ESP8266_CHANNEL_START, MBED_CONF_ESP8266_CHANNELS)) {
420+
if (!_esp.set_country_code_policy(true, _ch_info.country_code, _ch_info.channel_start, _ch_info.channels)) {
415421
return NSAPI_ERROR_DEVICE_ERROR;
416422
}
417423
if (!_esp.cond_enable_tcp_passive_mode()) {
@@ -862,7 +868,7 @@ nsapi_error_t ESP8266Interface::set_blocking(bool blocking)
862868
return NSAPI_ERROR_OK;
863869
}
864870

865-
nsapi_error_t ESP8266Interface::set_country_code(const char *country_code, int len)
871+
nsapi_error_t ESP8266Interface::set_country_code(bool track_ap, const char *country_code, int len, int channel_start, int channels)
866872
{
867873
for (int i = 0; i < len; i++) {
868874
// Validation done by firmware
@@ -872,9 +878,14 @@ nsapi_error_t ESP8266Interface::set_country_code(const char *country_code, int l
872878
}
873879
}
874880

881+
_ch_info.track_ap = track_ap;
882+
875883
// Firmware takes only first three characters
876-
strncpy(_country_code, country_code, sizeof(_country_code));
877-
_country_code[sizeof(_country_code)-1] = '\0';
884+
strncpy(_ch_info.country_code, country_code, sizeof(_ch_info.country_code));
885+
_ch_info.country_code[sizeof(_ch_info.country_code)-1] = '\0';
886+
887+
_ch_info.channel_start = channel_start;
888+
_ch_info.channels = channels;
878889

879890
return NSAPI_ERROR_OK;
880891
}

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,15 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
339339

340340
/** Set country code
341341
*
342-
* @param country_code 2-3 character country code
343-
* @param len Length of the country code
344-
* @return NSAPI_ERROR_OK on success, negative error code on failure.
342+
* @param track_ap if TRUE, use country code used by the AP ESP is connected to,
343+
* otherwise uses country_code always
344+
* @param country_code ISO 3166-1 coded, 2 character alphanumeric country code assumed
345+
* @param len Length of the country code
346+
* @param channel_start The channel number to start at
347+
* @param channel Number of channels
348+
* @return NSAPI_ERROR_OK on success, negative error code on failure.
345349
*/
346-
nsapi_error_t set_country_code(const char *country_code, int len)
350+
nsapi_error_t set_country_code(bool track_ap, const char *country_code, int len, int channel_start, int channels);
347351

348352
private:
349353
// AT layer
@@ -371,7 +375,13 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
371375
nsapi_security_t _ap_sec;
372376

373377
// Country code
374-
char _country_code[4]; /* ISO 3166-1 coded country code - +1 for the '\0' - assumed. Documentation doesn't tell */
378+
struct _channel_info {
379+
bool track_ap; // Set country code based on the AP ESP is connected to
380+
char country_code[4]; // ISO 3166-1 coded, 2-3 character alphanumeric country code - +1 for the '\0' - assumed. Documentation doesn't tell.
381+
int channel_start;
382+
int channels;
383+
};
384+
struct _channel_info _ch_info;
375385

376386
bool _if_blocking; // NetworkInterface, blocking or not
377387
rtos::ConditionVariable _if_connected;

components/wifi/esp8266-driver/mbed_lib.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
"socket-bufsize": {
3333
"help": "Max socket data heap usage",
3434
"value": 8192
35+
},
36+
"country-code": {
37+
"help": "ISO 3166-1 coded, 2 character alphanumeric country code, 'CN' by default",
38+
"value": null
39+
},
40+
"channel-start": {
41+
"help": "the channel number to start at, 1 by default",
42+
"value": null
43+
},
44+
"channels": {
45+
"help": "channel count, 13 by default",
46+
"value": null
3547
}
3648
},
3749
"target_overrides": {

0 commit comments

Comments
 (0)