Skip to content

Commit 478ce8f

Browse files
pocus01dpgeorge
authored andcommitted
esp32/modnetwork: Implement status('stations') to list STAs in AP mode.
The method returns a list of tuples representing the connected stations. The first element of the tuple is the MAC address of the station.
1 parent d4470af commit 478ce8f

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

ports/esp32/modnetwork.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,36 @@ STATIC mp_obj_t esp_disconnect(mp_obj_t self_in) {
275275

276276
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_disconnect_obj, esp_disconnect);
277277

278-
STATIC mp_obj_t esp_status(mp_obj_t self_in) {
278+
STATIC mp_obj_t esp_status(size_t n_args, const mp_obj_t *args) {
279+
if (n_args == 1) {
280+
// no arguments: return None until link status is implemented
281+
return mp_const_none;
282+
}
283+
284+
// one argument: return status based on query parameter
285+
switch ((uintptr_t)args[1]) {
286+
case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_stations): {
287+
// return list of connected stations, only if in soft-AP mode
288+
require_if(args[0], WIFI_IF_AP);
289+
wifi_sta_list_t station_list;
290+
ESP_EXCEPTIONS(esp_wifi_ap_get_sta_list(&station_list));
291+
wifi_sta_info_t *stations = (wifi_sta_info_t*)station_list.sta;
292+
mp_obj_t list = mp_obj_new_list(0, NULL);
293+
for (int i = 0; i < station_list.num; ++i) {
294+
mp_obj_tuple_t *t = mp_obj_new_tuple(1, NULL);
295+
t->items[0] = mp_obj_new_bytes(stations[i].mac, sizeof(stations[i].mac));
296+
mp_obj_list_append(list, t);
297+
}
298+
return list;
299+
}
300+
301+
default:
302+
mp_raise_ValueError("unknown status param");
303+
}
304+
279305
return mp_const_none;
280306
}
281-
282-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_status_obj, esp_status);
307+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_status_obj, 1, 2, esp_status);
283308

284309
STATIC mp_obj_t esp_scan(mp_obj_t self_in) {
285310
// check that STA mode is active

0 commit comments

Comments
 (0)