summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitchell Currie <mxcurri@caltex.com.au>2018-07-13 13:47:51 +1000
committerDamien George <damien.p.george@gmail.com>2018-07-14 16:26:43 +1000
commit385fa5180663221bbec033c54c37fb38f589579b (patch)
tree17b2e49114507b52113f2cabc02cf3237fe710c1
parent8c9c167dc6986a44acf9f255ff96e6822165e3e0 (diff)
esp32: Implement WLAN.status() return codes.
Resolves #3913: missing esp32 status() implementation.
-rw-r--r--ports/esp32/modnetwork.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/ports/esp32/modnetwork.c b/ports/esp32/modnetwork.c
index 1efb7ccba..5543df0e2 100644
--- a/ports/esp32/modnetwork.c
+++ b/ports/esp32/modnetwork.c
@@ -125,6 +125,9 @@ static bool wifi_sta_connect_requested = false;
// Set to "true" if the STA interface is connected to wifi and has IP address.
static bool wifi_sta_connected = false;
+// Store the current status. 0 means None here, safe to do so as first enum value is WIFI_REASON_UNSPECIFIED=1.
+static uint8_t wifi_sta_disconn_reason = 0;
+
// This function is called by the system-event task and so runs in a different
// thread to the main MicroPython task. It must not raise any Python exceptions.
static esp_err_t event_handler(void *ctx, system_event_t *event) {
@@ -138,12 +141,14 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI("network", "GOT_IP");
wifi_sta_connected = true;
+ wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway)
break;
case SYSTEM_EVENT_STA_DISCONNECTED: {
// This is a workaround as ESP32 WiFi libs don't currently
// auto-reassociate.
system_event_sta_disconnected_t *disconn = &event->event_info.disconnected;
char *message = "";
+ wifi_sta_disconn_reason = disconn->reason;
switch (disconn->reason) {
case WIFI_REASON_BEACON_TIMEOUT:
// AP has dropped out; try to reconnect.
@@ -334,9 +339,33 @@ STATIC mp_obj_t esp_disconnect(mp_obj_t self_in) {
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_disconnect_obj, esp_disconnect);
+// Cases similar to ESP8266 user_interface.h
+// Error cases are referenced from wifi_err_reason_t in ESP-IDF
+enum {
+ STAT_IDLE = 1000,
+ STAT_CONNECTING = 1001,
+ STAT_GOT_IP = 1010,
+};
+
STATIC mp_obj_t esp_status(size_t n_args, const mp_obj_t *args) {
+ wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
if (n_args == 1) {
- // no arguments: return None until link status is implemented
+ if (self->if_id == WIFI_IF_STA) {
+ // Case of no arg is only for the STA interface
+ if (wifi_sta_connected) {
+ // Happy path, connected with IP
+ return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP);
+ } else if (wifi_sta_connect_requested) {
+ // No connection or error, but is requested = Still connecting
+ return MP_OBJ_NEW_SMALL_INT(STAT_CONNECTING);
+ } else if (wifi_sta_disconn_reason == 0) {
+ // No activity, No error = Idle
+ return MP_OBJ_NEW_SMALL_INT(STAT_IDLE);
+ } else {
+ // Simply pass the error through from ESP-identifier
+ return MP_OBJ_NEW_SMALL_INT(wifi_sta_disconn_reason);
+ }
+ }
return mp_const_none;
}
@@ -657,6 +686,16 @@ STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_PHY_LAN8720), MP_ROM_INT(PHY_LAN8720) },
{ MP_ROM_QSTR(MP_QSTR_PHY_TLK110), MP_ROM_INT(PHY_TLK110) },
+
+ { MP_ROM_QSTR(MP_QSTR_STAT_IDLE), MP_ROM_INT(STAT_IDLE)},
+ { MP_ROM_QSTR(MP_QSTR_STAT_CONNECTING), MP_ROM_INT(STAT_CONNECTING)},
+ { MP_ROM_QSTR(MP_QSTR_STAT_GOT_IP), MP_ROM_INT(STAT_GOT_IP)},
+ // Errors from the ESP-IDF
+ { MP_ROM_QSTR(MP_QSTR_STAT_NO_AP_FOUND), MP_ROM_INT(WIFI_REASON_NO_AP_FOUND)},
+ { MP_ROM_QSTR(MP_QSTR_STAT_WRONG_PASSWORD), MP_ROM_INT(WIFI_REASON_AUTH_FAIL)},
+ { MP_ROM_QSTR(MP_QSTR_STAT_BEACON_TIMEOUT), MP_ROM_INT(WIFI_REASON_BEACON_TIMEOUT)},
+ { MP_ROM_QSTR(MP_QSTR_STAT_ASSOC_FAIL), MP_ROM_INT(WIFI_REASON_ASSOC_FAIL)},
+ { MP_ROM_QSTR(MP_QSTR_STAT_HANDSHAKE_TIMEOUT), MP_ROM_INT(WIFI_REASON_HANDSHAKE_TIMEOUT)},
#endif
};