Skip to content

Commit c60c524

Browse files
author
Veijo Pesonen
committed
ESP8266: makes connect() connection status aware
1 parent 4019efb commit c60c524

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ void ESP8266Interface::_oob2global_event_queue()
169169

170170
int ESP8266Interface::connect()
171171
{
172-
nsapi_error_t status;
172+
nsapi_error_t status = _conn_status_to_error();
173+
if (status != NSAPI_ERROR_NO_CONNECTION) {
174+
return status;
175+
}
173176

174177
if (strlen(ap_ssid) == 0) {
175178
return NSAPI_ERROR_NO_SSID;
@@ -199,6 +202,11 @@ int ESP8266Interface::connect()
199202

200203
int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
201204
{
205+
nsapi_error_t status = _conn_status_to_error();
206+
if (status != NSAPI_ERROR_NO_CONNECTION) {
207+
return status;
208+
}
209+
202210
_ap_sec = security;
203211

204212
if (!ssid) {
@@ -246,8 +254,8 @@ int ESP8266Interface::disconnect()
246254
{
247255
_initialized = false;
248256

249-
if (_conn_stat == NSAPI_STATUS_DISCONNECTED || !get_ip_address())
250-
{
257+
nsapi_error_t status = _conn_status_to_error();
258+
if (status == NSAPI_ERROR_NO_CONNECTION || !get_ip_address()) {
251259
return NSAPI_ERROR_NO_CONNECTION;
252260
}
253261

@@ -716,4 +724,27 @@ void ESP8266Interface::proc_oob_evnt()
716724
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
717725
}
718726

727+
nsapi_error_t ESP8266Interface::_conn_status_to_error()
728+
{
729+
nsapi_error_t ret;
730+
731+
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
732+
733+
switch (_conn_stat) {
734+
case NSAPI_STATUS_DISCONNECTED:
735+
ret = NSAPI_ERROR_NO_CONNECTION;
736+
break;
737+
case NSAPI_STATUS_CONNECTING:
738+
ret = NSAPI_ERROR_ALREADY;
739+
break;
740+
case NSAPI_STATUS_GLOBAL_UP:
741+
ret = NSAPI_ERROR_IS_CONNECTED;
742+
break;
743+
default:
744+
ret = NSAPI_ERROR_DEVICE_ERROR;
745+
}
746+
747+
return ret;
748+
}
749+
719750
#endif

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
341341
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */
342342
nsapi_security_t _ap_sec;
343343

344+
// connect status reporting
345+
nsapi_error_t _conn_status_to_error();
346+
344347
// Drivers's socket info
345348
struct _sock_info {
346349
bool open;

0 commit comments

Comments
 (0)