Skip to content

Commit 96247dd

Browse files
author
Veijo Pesonen
committed
ESP8266: sets hardcoded country code(CC) policy to track AP's CC
1 parent fa65546 commit 96247dd

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,4 +1201,29 @@ nsapi_connection_status_t ESP8266::connection_status() const
12011201
{
12021202
return _conn_status;
12031203
}
1204+
1205+
bool ESP8266::set_country_code_policy(bool track_ap, const char *country_code, int channel_start, int channels)
1206+
{
1207+
int t_ap = track_ap ? 0 : 1;
1208+
1209+
_smutex.lock();
1210+
bool done = _parser.send("AT+CWCOUNTRY_DEF=%d,\"%s\",%d,%d", t_ap, country_code, channel_start, channels)
1211+
&& _parser.recv("OK\n");
1212+
1213+
if (!done) {
1214+
tr_error("\"AT+CWCOUNTRY_DEF=%d,\"%s\",%d,%d\" - FAIL", t_ap, country_code, channel_start, channels);
1215+
}
1216+
1217+
done &= _parser.send("AT+CWCOUNTRY_CUR=%d,\"%s\",%d,%d", t_ap, country_code, channel_start, channels)
1218+
&& _parser.recv("OK\n");
1219+
1220+
if (!done) {
1221+
tr_error("\"AT+CWCOUNTRY_CUR=%d,\"%s\",%d,%d\" - FAIL", t_ap, country_code, channel_start, channels);
1222+
}
1223+
1224+
_smutex.unlock();
1225+
1226+
return done;
1227+
}
1228+
12041229
#endif

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ class ESP8266 {
335335
*/
336336
bool set_default_wifi_mode(const int8_t mode);
337337

338+
/**
339+
* @param track_ap if TRUE, sets the county code to be the same as the AP's that ESP is connected to,
340+
* if FALSE the code will not change
341+
* @param country_code ISO 3166-1 Alpha-2 coded country code
342+
* @param channel_start the channel number to start at
343+
* @param channels number of channels
344+
*/
345+
bool set_country_code_policy(bool track_ap, const char *country_code, int channel_start, int channels);
346+
338347
/** Get the connection status
339348
*
340349
* @return The connection status according to ConnectionStatusType

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ 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));
7172

7273
_esp.sigio(this, &ESP8266Interface::event);
7374
_esp.set_timeout();
@@ -97,6 +98,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
9798
memset(_cbs, 0, sizeof(_cbs));
9899
memset(ap_ssid, 0, sizeof(ap_ssid));
99100
memset(ap_pass, 0, sizeof(ap_pass));
101+
memset(_country_code, 0, sizeof(_country_code));
100102

101103
_esp.sigio(this, &ESP8266Interface::event);
102104
_esp.set_timeout();
@@ -391,7 +393,15 @@ bool ESP8266Interface::_get_firmware_ok()
391393

392394
nsapi_error_t ESP8266Interface::_init(void)
393395
{
396+
397+
394398
if (!_initialized) {
399+
400+
if (!_country_code[0] || !_country_code[1] ) {
401+
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, 2);
402+
_country_code[2] = '\0';
403+
}
404+
395405
if (_reset() != NSAPI_ERROR_OK) {
396406
return NSAPI_ERROR_DEVICE_ERROR;
397407
}
@@ -407,6 +417,9 @@ nsapi_error_t ESP8266Interface::_init(void)
407417
if (!_esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION)) {
408418
return NSAPI_ERROR_DEVICE_ERROR;
409419
}
420+
if (!_esp.set_country_code_policy(true, _country_code, MBED_CONF_ESP8266_CHANNEL_START, MBED_CONF_ESP8266_CHANNELS)) {
421+
return NSAPI_ERROR_DEVICE_ERROR;
422+
}
410423
if (!_esp.cond_enable_tcp_passive_mode()) {
411424
return NSAPI_ERROR_DEVICE_ERROR;
412425
}

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
#endif
4545
#endif /* TARGET_FF_ARDUINO */
4646

47+
#ifndef MBED_CONF_ESP8266_COUNTRY_CODE
48+
#define MBED_CONF_ESP8266_COUNTRY_CODE "CN"
49+
#endif
50+
51+
#ifndef MBED_CONF_ESP8266_CHANNEL_START
52+
#define MBED_CONF_ESP8266_CHANNEL_START 1
53+
#endif
54+
55+
#ifndef MBED_CONF_ESP8266_CHANNELS
56+
#define MBED_CONF_ESP8266_CHANNELS 13
57+
#endif
58+
4759
/** ESP8266Interface class
4860
* Implementation of the NetworkStack for the ESP8266
4961
*/
@@ -350,6 +362,9 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
350362
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */
351363
nsapi_security_t _ap_sec;
352364

365+
// Country code
366+
char _country_code[3]; /* ISO 3166-1 Alpha-2 coded country code plus, +1 for the \0 */
367+
353368
bool _if_blocking; // NetworkInterface, blocking or not
354369
rtos::ConditionVariable _if_connected;
355370

0 commit comments

Comments
 (0)