ESP8266: makes usable channels runtime configurable

pull/9927/head
Veijo Pesonen 2019-03-04 16:17:30 +02:00
parent eb27a23641
commit 50984e5d2d
3 changed files with 46 additions and 13 deletions

View File

@ -68,8 +68,11 @@ ESP8266Interface::ESP8266Interface()
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
memset(_country_code, 0, sizeof(_country_code));
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_country_code));
_ch_info.track_ap = true;
strncpy(_ch_info.country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_ch_info.country_code));
_ch_info.channel_start = MBED_CONF_ESP8266_CHANNEL_START;
_ch_info.channels = MBED_CONF_ESP8266_CHANNELS;
_esp.sigio(this, &ESP8266Interface::event);
_esp.set_timeout();
@ -99,8 +102,11 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
memset(_country_code, 0, sizeof(_country_code));
strncpy(_country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_country_code));
_ch_info.track_ap = true;
strncpy(_ch_info.country_code, MBED_CONF_ESP8266_COUNTRY_CODE, sizeof(_ch_info.country_code));
_ch_info.channel_start = MBED_CONF_ESP8266_CHANNEL_START;
_ch_info.channels = MBED_CONF_ESP8266_CHANNELS;
_esp.sigio(this, &ESP8266Interface::event);
_esp.set_timeout();
@ -411,7 +417,7 @@ nsapi_error_t ESP8266Interface::_init(void)
if (!_esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (!_esp.set_country_code_policy(true, _country_code, MBED_CONF_ESP8266_CHANNEL_START, MBED_CONF_ESP8266_CHANNELS)) {
if (!_esp.set_country_code_policy(true, _ch_info.country_code, _ch_info.channel_start, _ch_info.channels)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (!_esp.cond_enable_tcp_passive_mode()) {
@ -862,7 +868,7 @@ nsapi_error_t ESP8266Interface::set_blocking(bool blocking)
return NSAPI_ERROR_OK;
}
nsapi_error_t ESP8266Interface::set_country_code(const char *country_code, int len)
nsapi_error_t ESP8266Interface::set_country_code(bool track_ap, const char *country_code, int len, int channel_start, int channels)
{
for (int i = 0; i < len; i++) {
// Validation done by firmware
@ -872,9 +878,14 @@ nsapi_error_t ESP8266Interface::set_country_code(const char *country_code, int l
}
}
_ch_info.track_ap = track_ap;
// Firmware takes only first three characters
strncpy(_country_code, country_code, sizeof(_country_code));
_country_code[sizeof(_country_code)-1] = '\0';
strncpy(_ch_info.country_code, country_code, sizeof(_ch_info.country_code));
_ch_info.country_code[sizeof(_ch_info.country_code)-1] = '\0';
_ch_info.channel_start = channel_start;
_ch_info.channels = channels;
return NSAPI_ERROR_OK;
}

View File

@ -339,11 +339,15 @@ protected:
/** Set country code
*
* @param country_code 2-3 character country code
* @param len Length of the country code
* @return NSAPI_ERROR_OK on success, negative error code on failure.
* @param track_ap if TRUE, use country code used by the AP ESP is connected to,
* otherwise uses country_code always
* @param country_code ISO 3166-1 coded, 2 character alphanumeric country code assumed
* @param len Length of the country code
* @param channel_start The channel number to start at
* @param channel Number of channels
* @return NSAPI_ERROR_OK on success, negative error code on failure.
*/
nsapi_error_t set_country_code(const char *country_code, int len)
nsapi_error_t set_country_code(bool track_ap, const char *country_code, int len, int channel_start, int channels);
private:
// AT layer
@ -371,7 +375,13 @@ private:
nsapi_security_t _ap_sec;
// Country code
char _country_code[4]; /* ISO 3166-1 coded country code - +1 for the '\0' - assumed. Documentation doesn't tell */
struct _channel_info {
bool track_ap; // Set country code based on the AP ESP is connected to
char country_code[4]; // ISO 3166-1 coded, 2-3 character alphanumeric country code - +1 for the '\0' - assumed. Documentation doesn't tell.
int channel_start;
int channels;
};
struct _channel_info _ch_info;
bool _if_blocking; // NetworkInterface, blocking or not
rtos::ConditionVariable _if_connected;

View File

@ -32,6 +32,18 @@
"socket-bufsize": {
"help": "Max socket data heap usage",
"value": 8192
},
"country-code": {
"help": "ISO 3166-1 coded, 2 character alphanumeric country code, 'CN' by default",
"value": null
},
"channel-start": {
"help": "the channel number to start at, 1 by default",
"value": null
},
"channels": {
"help": "channel count, 13 by default",
"value": null
}
},
"target_overrides": {