Skip to content

Commit a49c160

Browse files
committed
esp8266/modnetwork: Introduce interface .config() method.
Allows to set (in case keyword args are given) or query (in case a single "symbolic keyword" (a string, value is the same as keyword)) arbitrary interface paramters (i.e. extensible and adaptable to various hardware). Example usage: ap_if = network.WLAN(1) ap_if.config(essid="MicroPython on Air") print(ap_if.config("essid"))
1 parent d5a12a6 commit a49c160

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

esp8266/modnetwork.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,74 @@ STATIC mp_obj_t esp_ifconfig(mp_obj_t self_in) {
208208
}
209209
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_ifconfig_obj, esp_ifconfig);
210210

211+
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
212+
if (n_args != 1 && kwargs->used != 0) {
213+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
214+
"either pos or kw args are allowed"));
215+
}
216+
217+
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
218+
union {
219+
struct station_config sta;
220+
struct softap_config ap;
221+
} cfg;
222+
223+
if (self->if_id == STATION_IF) {
224+
error_check(wifi_station_get_config(&cfg.sta), "can't get STA config");
225+
} else {
226+
error_check(wifi_softap_get_config(&cfg.ap), "can't get AP config");
227+
}
228+
229+
if (kwargs->used != 0) {
230+
231+
for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
232+
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
233+
#define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
234+
switch ((uintptr_t)kwargs->table[i].key) {
235+
case QS(MP_QSTR_essid): {
236+
mp_uint_t len;
237+
const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
238+
len = MIN(len, sizeof(cfg.ap.ssid));
239+
memcpy(cfg.ap.ssid, s, len);
240+
cfg.ap.ssid_len = len;
241+
break;
242+
}
243+
default:
244+
goto unknown;
245+
}
246+
#undef QS
247+
}
248+
}
249+
250+
if (self->if_id == STATION_IF) {
251+
error_check(wifi_station_set_config(&cfg.sta), "can't set STA config");
252+
} else {
253+
error_check(wifi_softap_set_config(&cfg.ap), "can't set AP config");
254+
}
255+
256+
return mp_const_none;
257+
}
258+
259+
// Get config
260+
261+
if (n_args != 2) {
262+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
263+
"can query only one param"));
264+
}
265+
266+
#define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
267+
switch ((uintptr_t)args[1]) {
268+
case QS(MP_QSTR_essid):
269+
return mp_obj_new_str((char*)cfg.ap.ssid, cfg.ap.ssid_len, false);
270+
}
271+
#undef QS
272+
273+
unknown:
274+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
275+
"unknown config param"));
276+
}
277+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_config_obj, 1, esp_config);
278+
211279
STATIC const mp_map_elem_t wlan_if_locals_dict_table[] = {
212280
{ MP_OBJ_NEW_QSTR(MP_QSTR_active), (mp_obj_t)&esp_active_obj },
213281
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&esp_connect_obj },
@@ -216,6 +284,7 @@ STATIC const mp_map_elem_t wlan_if_locals_dict_table[] = {
216284
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
217285
{ MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&esp_isconnected_obj },
218286
{ MP_OBJ_NEW_QSTR(MP_QSTR_mac), (mp_obj_t)&esp_mac_obj },
287+
{ MP_OBJ_NEW_QSTR(MP_QSTR_config), (mp_obj_t)&esp_config_obj },
219288
{ MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&esp_ifconfig_obj },
220289
};
221290

esp8266/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,16 @@ Q(scan)
104104
Q(status)
105105
Q(isconnected)
106106
Q(mac)
107+
Q(config)
107108
Q(ifconfig)
108109
Q(STAT_IDLE)
109110
Q(STAT_CONNECTING)
110111
Q(STAT_WRONG_PASSWORD)
111112
Q(STAT_NO_AP_FOUND)
112113
Q(STAT_CONNECT_FAIL)
113114
Q(STAT_GOT_IP)
115+
// config keys
116+
Q(essid)
114117

115118
// Pin class
116119
Q(Pin)

0 commit comments

Comments
 (0)