From 1c43a0fbf872ab44e3fc8c427f6ce180b7d6cb8c Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Fri, 19 Feb 2016 18:44:13 +0200
Subject: [PATCH] esp8266/modnetwork: Add per-interface .active() method.

Allows to up/down interface when called with a boolean, or query current
state if called without args. This per-interface method is intended to
supersede adhoc network.wifi_mode() function.
---
 esp8266/modnetwork.c   | 24 ++++++++++++++++++++++++
 esp8266/qstrdefsport.h |  1 +
 2 files changed, 25 insertions(+)

diff --git a/esp8266/modnetwork.c b/esp8266/modnetwork.c
index ca842ab60..09407da85 100644
--- a/esp8266/modnetwork.c
+++ b/esp8266/modnetwork.c
@@ -68,6 +68,29 @@ STATIC mp_obj_t get_wlan(mp_uint_t n_args, const mp_obj_t *args) {
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(get_wlan_obj, 0, 1, get_wlan);
 
+STATIC mp_obj_t esp_active(mp_uint_t n_args, const mp_obj_t *args) {
+    wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+    uint32_t mode = wifi_get_opmode();
+    if (n_args > 1) {
+        int mask = self->if_id == STATION_IF ? STATION_MODE : SOFTAP_MODE;
+        if (mp_obj_get_int(args[1]) != 0) {
+            mode |= mask;
+        } else {
+            mode &= ~mask;
+        }
+        error_check(wifi_set_opmode(mode), "Cannot update i/f status");
+        return mp_const_none;
+    }
+
+    // Get active status
+    if (self->if_id == STATION_IF) {
+        return mp_obj_new_bool(mode & STATION_MODE);
+    } else {
+        return mp_obj_new_bool(mode & SOFTAP_MODE);
+    }
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_active_obj, 1, 2, esp_active);
+
 STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) {
     require_if(args[0], STATION_IF);
     struct station_config config = {{0}};
@@ -186,6 +209,7 @@ STATIC mp_obj_t esp_ifconfig(mp_obj_t self_in) {
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_ifconfig_obj, esp_ifconfig);
 
 STATIC const mp_map_elem_t wlan_if_locals_dict_table[] = {
+    { MP_OBJ_NEW_QSTR(MP_QSTR_active), (mp_obj_t)&esp_active_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&esp_disconnect_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&esp_status_obj },
diff --git a/esp8266/qstrdefsport.h b/esp8266/qstrdefsport.h
index 6895ed25e..3e134aabb 100644
--- a/esp8266/qstrdefsport.h
+++ b/esp8266/qstrdefsport.h
@@ -99,6 +99,7 @@ Q(STA_AP_MODE)
 // network module
 Q(network)
 Q(WLAN)
+Q(active)
 Q(scan)
 Q(status)
 Q(isconnected)
-- 
GitLab