Skip to content

Commit eb27a23

Browse files
author
Veijo Pesonen
committed
ESP8266: Exposes country code config API
1 parent 96247dd commit eb27a23

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ ESP8266Interface::ESP8266Interface()
6969
memset(ap_ssid, 0, sizeof(ap_ssid));
7070
memset(ap_pass, 0, sizeof(ap_pass));
7171
memset(_country_code, 0, sizeof(_country_code));
72+
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_country_code));
7273

7374
_esp.sigio(this, &ESP8266Interface::event);
7475
_esp.set_timeout();
@@ -99,6 +100,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
99100
memset(ap_ssid, 0, sizeof(ap_ssid));
100101
memset(ap_pass, 0, sizeof(ap_pass));
101102
memset(_country_code, 0, sizeof(_country_code));
103+
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_country_code));
102104

103105
_esp.sigio(this, &ESP8266Interface::event);
104106
_esp.set_timeout();
@@ -393,15 +395,7 @@ bool ESP8266Interface::_get_firmware_ok()
393395

394396
nsapi_error_t ESP8266Interface::_init(void)
395397
{
396-
397-
398398
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-
405399
if (_reset() != NSAPI_ERROR_OK) {
406400
return NSAPI_ERROR_DEVICE_ERROR;
407401
}
@@ -868,5 +862,21 @@ nsapi_error_t ESP8266Interface::set_blocking(bool blocking)
868862
return NSAPI_ERROR_OK;
869863
}
870864

865+
nsapi_error_t ESP8266Interface::set_country_code(const char *country_code, int len)
866+
{
867+
for (int i = 0; i < len; i++) {
868+
// Validation done by firmware
869+
if (!country_code[i]) {
870+
tr_warning("invalid country code");
871+
return NSAPI_ERROR_PARAMETER;
872+
}
873+
}
874+
875+
// Firmware takes only first three characters
876+
strncpy(_country_code, country_code, sizeof(_country_code));
877+
_country_code[sizeof(_country_code)-1] = '\0';
878+
879+
return NSAPI_ERROR_OK;
880+
}
871881

872882
#endif

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,14 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
337337
*/
338338
virtual nsapi_error_t set_blocking(bool blocking);
339339

340+
/** Set country code
341+
*
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.
345+
*/
346+
nsapi_error_t set_country_code(const char *country_code, int len)
347+
340348
private:
341349
// AT layer
342350
ESP8266 _esp;
@@ -363,7 +371,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
363371
nsapi_security_t _ap_sec;
364372

365373
// Country code
366-
char _country_code[3]; /* ISO 3166-1 Alpha-2 coded country code plus, +1 for the \0 */
374+
char _country_code[4]; /* ISO 3166-1 coded country code - +1 for the '\0' - assumed. Documentation doesn't tell */
367375

368376
bool _if_blocking; // NetworkInterface, blocking or not
369377
rtos::ConditionVariable _if_connected;

0 commit comments

Comments
 (0)