3939#include "spi_flash.h"
4040#include "utils.h"
4141
42+ typedef struct _wlan_if_obj_t {
43+ mp_obj_base_t base ;
44+ int if_id ;
45+ } wlan_if_obj_t ;
46+
4247void error_check (bool status , const char * msg );
43- extern const mp_obj_module_t network_module ;
48+ const mp_obj_type_t wlan_if_type ;
49+
50+ STATIC const wlan_if_obj_t wlan_objs [] = {
51+ {{& wlan_if_type }, STATION_IF },
52+ {{& wlan_if_type }, SOFTAP_IF },
53+ };
4454
45- STATIC mp_obj_t get_module () {
46- return (mp_obj_t )& network_module ;
55+ STATIC void require_if (mp_obj_t wlan_if , int if_no ) {
56+ wlan_if_obj_t * self = MP_OBJ_TO_PTR (wlan_if );
57+ if (self -> if_id != if_no ) {
58+ error_check (false, "STA required" );
59+ }
4760}
48- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (get_module_obj , get_module );
61+
62+ STATIC mp_obj_t get_wlan (mp_uint_t n_args , const mp_obj_t * args ) {
63+ int idx = 0 ;
64+ if (n_args > 0 ) {
65+ idx = mp_obj_get_int (args [0 ]);
66+ }
67+ return MP_OBJ_FROM_PTR (& wlan_objs [idx ]);
68+ }
69+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (get_wlan_obj , 0 , 1 , get_wlan );
4970
5071STATIC mp_obj_t esp_connect (mp_uint_t n_args , const mp_obj_t * args ) {
72+ require_if (args [0 ], STATION_IF );
5173 struct station_config config = {{0 }};
5274 mp_uint_t len ;
5375 const char * p ;
5476
55- p = mp_obj_str_get_data (args [0 ], & len );
56- memcpy (config .ssid , p , len );
5777 p = mp_obj_str_get_data (args [1 ], & len );
78+ memcpy (config .ssid , p , len );
79+ p = mp_obj_str_get_data (args [2 ], & len );
5880 memcpy (config .password , p , len );
5981
6082 error_check (wifi_station_set_config (& config ), "Cannot set STA config" );
6183 error_check (wifi_station_connect (), "Cannot connect to AP" );
6284
6385 return mp_const_none ;
6486}
65- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_connect_obj , 2 , 6 , esp_connect );
87+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_connect_obj , 3 , 7 , esp_connect );
6688
67- STATIC mp_obj_t esp_disconnect () {
89+ STATIC mp_obj_t esp_disconnect (mp_obj_t self_in ) {
90+ require_if (self_in , STATION_IF );
6891 error_check (wifi_station_disconnect (), "Cannot disconnect from AP" );
6992 return mp_const_none ;
7093}
71- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (esp_disconnect_obj , esp_disconnect );
94+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_disconnect_obj , esp_disconnect );
7295
7396#define MODNETWORK_INCLUDE_CONSTANTS (1)
7497
75- STATIC mp_obj_t esp_status () {
76- return MP_OBJ_NEW_SMALL_INT (wifi_station_get_connect_status ());
98+ STATIC mp_obj_t esp_status (mp_obj_t self_in ) {
99+ wlan_if_obj_t * self = MP_OBJ_TO_PTR (self_in );
100+ if (self -> if_id == STATION_IF ) {
101+ return MP_OBJ_NEW_SMALL_INT (wifi_station_get_connect_status ());
102+ }
103+ return MP_OBJ_NEW_SMALL_INT (-1 );
77104}
78- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (esp_status_obj , esp_status );
105+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_status_obj , esp_status );
79106
80107STATIC void esp_scan_cb (scaninfo * si , STATUS status ) {
81108 struct bss_info * bs ;
@@ -93,7 +120,7 @@ STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
93120 }
94121}
95122
96- STATIC mp_obj_t esp_scan (mp_obj_t cb_in ) {
123+ STATIC mp_obj_t esp_scan (mp_obj_t self_in , mp_obj_t cb_in ) {
97124 MP_STATE_PORT (scan_cb_obj ) = cb_in ;
98125 if (wifi_get_opmode () == SOFTAP_MODE ) {
99126 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError ,
@@ -102,43 +129,52 @@ STATIC mp_obj_t esp_scan(mp_obj_t cb_in) {
102129 wifi_station_scan (NULL , (scan_done_cb_t )esp_scan_cb );
103130 return mp_const_none ;
104131}
105- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_scan_obj , esp_scan );
132+ STATIC MP_DEFINE_CONST_FUN_OBJ_2 (esp_scan_obj , esp_scan );
106133
107134/// \method isconnected()
108135/// Return True if connected to an AP and an IP address has been assigned,
109136/// false otherwise.
110- STATIC mp_obj_t esp_isconnected () {
111- if (wifi_station_get_connect_status () == STATION_GOT_IP ) {
112- return mp_const_true ;
137+ STATIC mp_obj_t esp_isconnected (mp_obj_t self_in ) {
138+ wlan_if_obj_t * self = MP_OBJ_TO_PTR (self_in );
139+ if (self -> if_id == STATION_IF ) {
140+ if (wifi_station_get_connect_status () == STATION_GOT_IP ) {
141+ return mp_const_true ;
142+ }
143+ } else {
144+ if (wifi_softap_get_station_num () > 0 ) {
145+ return mp_const_true ;
146+ }
113147 }
114148 return mp_const_false ;
115149}
116150
117- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (esp_isconnected_obj , esp_isconnected );
151+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_isconnected_obj , esp_isconnected );
118152
119153STATIC mp_obj_t esp_mac (mp_uint_t n_args , const mp_obj_t * args ) {
154+ wlan_if_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
120155 uint8_t mac [6 ];
121- if (n_args == 0 ) {
122- wifi_get_macaddr (STATION_IF , mac );
156+ if (n_args == 1 ) {
157+ wifi_get_macaddr (self -> if_id , mac );
123158 return mp_obj_new_bytes (mac , sizeof (mac ));
124159 } else {
125160 mp_buffer_info_t bufinfo ;
126- mp_get_buffer_raise (args [0 ], & bufinfo , MP_BUFFER_READ );
161+ mp_get_buffer_raise (args [1 ], & bufinfo , MP_BUFFER_READ );
127162
128163 if (bufinfo .len != 6 ) {
129164 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError ,
130165 "invalid buffer length" ));
131166 }
132167
133- wifi_set_macaddr (STATION_IF , bufinfo .buf );
168+ wifi_set_macaddr (self -> if_id , bufinfo .buf );
134169 return mp_const_none ;
135170 }
136171}
137- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_mac_obj , 0 , 1 , esp_mac );
172+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_mac_obj , 1 , 2 , esp_mac );
138173
139- STATIC mp_obj_t esp_ifconfig (void ) {
174+ STATIC mp_obj_t esp_ifconfig (mp_obj_t self_in ) {
175+ wlan_if_obj_t * self = MP_OBJ_TO_PTR (self_in );
140176 struct ip_info info ;
141- wifi_get_ip_info (STATION_IF , & info );
177+ wifi_get_ip_info (self -> if_id , & info );
142178 mp_obj_t ifconfig [4 ] = {
143179 netutils_format_ipv4_addr ((uint8_t * )& info .ip , NETUTILS_BIG ),
144180 netutils_format_ipv4_addr ((uint8_t * )& info .netmask , NETUTILS_BIG ),
@@ -147,22 +183,29 @@ STATIC mp_obj_t esp_ifconfig(void) {
147183 };
148184 return mp_obj_new_tuple (4 , ifconfig );
149185}
150- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (esp_ifconfig_obj , esp_ifconfig );
186+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_ifconfig_obj , esp_ifconfig );
151187
152- STATIC const mp_map_elem_t mp_module_network_globals_table [] = {
153- { MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_network ) },
154- // MicroPython "network" module interface requires it to contains classes
155- // to instantiate. But as we have just a static network interace,
156- // use module as a "class", and just make all methods module-global
157- // functions.
158- { MP_OBJ_NEW_QSTR (MP_QSTR_WLAN ), (mp_obj_t )& get_module_obj },
188+ STATIC const mp_map_elem_t wlan_if_locals_dict_table [] = {
159189 { MP_OBJ_NEW_QSTR (MP_QSTR_connect ), (mp_obj_t )& esp_connect_obj },
160190 { MP_OBJ_NEW_QSTR (MP_QSTR_disconnect ), (mp_obj_t )& esp_disconnect_obj },
161191 { MP_OBJ_NEW_QSTR (MP_QSTR_status ), (mp_obj_t )& esp_status_obj },
162192 { MP_OBJ_NEW_QSTR (MP_QSTR_scan ), (mp_obj_t )& esp_scan_obj },
163193 { MP_OBJ_NEW_QSTR (MP_QSTR_isconnected ), (mp_obj_t )& esp_isconnected_obj },
164194 { MP_OBJ_NEW_QSTR (MP_QSTR_mac ), (mp_obj_t )& esp_mac_obj },
165195 { MP_OBJ_NEW_QSTR (MP_QSTR_ifconfig ), (mp_obj_t )& esp_ifconfig_obj },
196+ };
197+
198+ STATIC MP_DEFINE_CONST_DICT (wlan_if_locals_dict , wlan_if_locals_dict_table );
199+
200+ const mp_obj_type_t wlan_if_type = {
201+ { & mp_type_type },
202+ .name = MP_QSTR_WLAN ,
203+ .locals_dict = (mp_obj_t )& wlan_if_locals_dict ,
204+ };
205+
206+ STATIC const mp_map_elem_t mp_module_network_globals_table [] = {
207+ { MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_network ) },
208+ { MP_OBJ_NEW_QSTR (MP_QSTR_WLAN ), (mp_obj_t )& get_wlan_obj },
166209
167210#if MODNETWORK_INCLUDE_CONSTANTS
168211 { MP_OBJ_NEW_QSTR (MP_QSTR_STAT_IDLE ),
0 commit comments