Skip to content

Commit 41534f1

Browse files
committed
esp32/modnetwork: Add initial implementation of wlan.config().
Taken mostly from esp8266 implementation.
1 parent 4e2ca30 commit 41534f1

File tree

1 file changed

+130
-3
lines changed

1 file changed

+130
-3
lines changed

esp32/modnetwork.c

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,14 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
160160
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, msg));
161161
}
162162
}
163+
*/
163164

164165
STATIC void require_if(mp_obj_t wlan_if, int if_no) {
165166
wlan_if_obj_t *self = MP_OBJ_TO_PTR(wlan_if);
166167
if (self->if_id != if_no) {
167-
error_check(false, if_no == WIFI_IF_STA ? "STA required" : "AP required");
168+
mp_raise_msg(&mp_type_OSError, if_no == WIFI_IF_STA ? "STA required" : "AP required");
168169
}
169-
}*/
170+
}
170171

171172
STATIC mp_obj_t get_wlan(mp_uint_t n_args, const mp_obj_t *args) {
172173
int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA;
@@ -349,7 +350,133 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
349350
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
350351

351352
STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
352-
return mp_const_none;
353+
if (n_args != 1 && kwargs->used != 0) {
354+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
355+
"either pos or kw args are allowed"));
356+
}
357+
358+
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
359+
360+
// get the config for the interface
361+
wifi_config_t cfg;
362+
ESP_EXCEPTIONS(esp_wifi_get_config(self->if_id, &cfg));
363+
364+
if (kwargs->used != 0) {
365+
366+
for (size_t i = 0; i < kwargs->alloc; i++) {
367+
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
368+
int req_if = -1;
369+
370+
#define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
371+
switch ((uintptr_t)kwargs->table[i].key) {
372+
case QS(MP_QSTR_mac): {
373+
mp_buffer_info_t bufinfo;
374+
mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
375+
if (bufinfo.len != 6) {
376+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
377+
"invalid buffer length"));
378+
}
379+
ESP_EXCEPTIONS(esp_wifi_set_mac(self->if_id, bufinfo.buf));
380+
break;
381+
}
382+
case QS(MP_QSTR_essid): {
383+
req_if = WIFI_IF_AP;
384+
mp_uint_t len;
385+
const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
386+
len = MIN(len, sizeof(cfg.ap.ssid));
387+
memcpy(cfg.ap.ssid, s, len);
388+
cfg.ap.ssid_len = len;
389+
break;
390+
}
391+
case QS(MP_QSTR_hidden): {
392+
req_if = WIFI_IF_AP;
393+
cfg.ap.ssid_hidden = mp_obj_is_true(kwargs->table[i].value);
394+
break;
395+
}
396+
case QS(MP_QSTR_authmode): {
397+
req_if = WIFI_IF_AP;
398+
cfg.ap.authmode = mp_obj_get_int(kwargs->table[i].value);
399+
break;
400+
}
401+
case QS(MP_QSTR_password): {
402+
req_if = WIFI_IF_AP;
403+
mp_uint_t len;
404+
const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len);
405+
len = MIN(len, sizeof(cfg.ap.password) - 1);
406+
memcpy(cfg.ap.password, s, len);
407+
cfg.ap.password[len] = 0;
408+
break;
409+
}
410+
case QS(MP_QSTR_channel): {
411+
req_if = WIFI_IF_AP;
412+
cfg.ap.channel = mp_obj_get_int(kwargs->table[i].value);
413+
break;
414+
}
415+
default:
416+
goto unknown;
417+
}
418+
#undef QS
419+
420+
// We post-check interface requirements to save on code size
421+
if (req_if >= 0) {
422+
require_if(args[0], req_if);
423+
}
424+
}
425+
}
426+
427+
ESP_EXCEPTIONS(esp_wifi_set_config(self->if_id, &cfg));
428+
429+
return mp_const_none;
430+
}
431+
432+
// Get config
433+
434+
if (n_args != 2) {
435+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
436+
"can query only one param"));
437+
}
438+
439+
int req_if = -1;
440+
mp_obj_t val;
441+
442+
#define QS(x) (uintptr_t)MP_OBJ_NEW_QSTR(x)
443+
switch ((uintptr_t)args[1]) {
444+
case QS(MP_QSTR_mac): {
445+
uint8_t mac[6];
446+
ESP_EXCEPTIONS(esp_wifi_get_mac(self->if_id, mac));
447+
return mp_obj_new_bytes(mac, sizeof(mac));
448+
}
449+
case QS(MP_QSTR_essid):
450+
req_if = WIFI_IF_AP;
451+
val = mp_obj_new_str((char*)cfg.ap.ssid, cfg.ap.ssid_len, false);
452+
break;
453+
case QS(MP_QSTR_hidden):
454+
req_if = WIFI_IF_AP;
455+
val = mp_obj_new_bool(cfg.ap.ssid_hidden);
456+
break;
457+
case QS(MP_QSTR_authmode):
458+
req_if = WIFI_IF_AP;
459+
val = MP_OBJ_NEW_SMALL_INT(cfg.ap.authmode);
460+
break;
461+
case QS(MP_QSTR_channel):
462+
req_if = WIFI_IF_AP;
463+
val = MP_OBJ_NEW_SMALL_INT(cfg.ap.channel);
464+
break;
465+
default:
466+
goto unknown;
467+
}
468+
#undef QS
469+
470+
// We post-check interface requirements to save on code size
471+
if (req_if >= 0) {
472+
require_if(args[0], req_if);
473+
}
474+
475+
return val;
476+
477+
unknown:
478+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
479+
"unknown config param"));
353480
}
354481

355482
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_config_obj, 1, esp_config);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy