Skip to content

Commit 1c23c5b

Browse files
author
Cruz Monrreal
authored
Merge pull request #9927 from VeijoPesonen/esp8266_country_code_policy
ESP8266: Country code API
2 parents 61b0d8e + 50984e5 commit 1c23c5b

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ ESP8266Interface::ESP8266Interface()
6969
memset(ap_ssid, 0, sizeof(ap_ssid));
7070
memset(ap_pass, 0, sizeof(ap_pass));
7171

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;
76+
7277
_esp.sigio(this, &ESP8266Interface::event);
7378
_esp.set_timeout();
7479
_esp.attach(this, &ESP8266Interface::refresh_conn_state_cb);
@@ -98,6 +103,11 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
98103
memset(ap_ssid, 0, sizeof(ap_ssid));
99104
memset(ap_pass, 0, sizeof(ap_pass));
100105

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;
110+
101111
_esp.sigio(this, &ESP8266Interface::event);
102112
_esp.set_timeout();
103113
_esp.attach(this, &ESP8266Interface::refresh_conn_state_cb);
@@ -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, _ch_info.country_code, _ch_info.channel_start, _ch_info.channels)) {
421+
return NSAPI_ERROR_DEVICE_ERROR;
422+
}
410423
if (!_esp.cond_enable_tcp_passive_mode()) {
411424
return NSAPI_ERROR_DEVICE_ERROR;
412425
}
@@ -855,5 +868,26 @@ nsapi_error_t ESP8266Interface::set_blocking(bool blocking)
855868
return NSAPI_ERROR_OK;
856869
}
857870

871+
nsapi_error_t ESP8266Interface::set_country_code(bool track_ap, const char *country_code, int len, int channel_start, int channels)
872+
{
873+
for (int i = 0; i < len; i++) {
874+
// Validation done by firmware
875+
if (!country_code[i]) {
876+
tr_warning("invalid country code");
877+
return NSAPI_ERROR_PARAMETER;
878+
}
879+
}
880+
881+
_ch_info.track_ap = track_ap;
882+
883+
// Firmware takes only first three characters
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;
889+
890+
return NSAPI_ERROR_OK;
891+
}
858892

859893
#endif

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 33 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
*/
@@ -325,6 +337,18 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
325337
*/
326338
virtual nsapi_error_t set_blocking(bool blocking);
327339

340+
/** Set country code
341+
*
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.
349+
*/
350+
nsapi_error_t set_country_code(bool track_ap, const char *country_code, int len, int channel_start, int channels);
351+
328352
private:
329353
// AT layer
330354
ESP8266 _esp;
@@ -350,6 +374,15 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
350374
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */
351375
nsapi_security_t _ap_sec;
352376

377+
// Country code
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;
385+
353386
bool _if_blocking; // NetworkInterface, blocking or not
354387
rtos::ConditionVariable _if_connected;
355388

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)