8000 Merge pull request #7823 from dhalbert/wifi-connected · megacoder/circuitpython@cc0eeb4 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc0eeb4

Browse files
authored
Merge pull request adafruit#7823 from dhalbert/wifi-connected
Add wifi.radio.connected, wifi.radio.ap_active
2 parents 4a14e4b + 0aacb14 commit cc0eeb4

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

ports/espressif/common-hal/wifi/Radio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
242242
esp_wifi_set_config(WIFI_IF_AP, config);
243243
}
244244

245+
bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self) {
246+
return self->ap_mode && esp_netif_is_netif_up(self->ap_netif);
247+
}
248+
245249
void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
246250
set_mode_ap(self, false);
247251
}
@@ -347,6 +351,10 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
347351
return WIFI_RADIO_ERROR_NONE;
348352
}
349353

354+
bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self) {
355+
return self->sta_mode && esp_netif_is_netif_up(self->netif);
356+
}
357+
350358
mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
351359
if (!esp_netif_is_netif_up(self->netif)) {
352360
return mp_const_none;

ports/raspberrypi/common-hal/wifi/Radio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
186186
bindings_cyw43_wifi_enforce_pm();
187187
}
188188

189+
bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self) {
190+
return cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) == CYW43_LINK_UP;
191+
}
192+
189193
void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
190194
if (!common_hal_wifi_radio_get_enabled(self)) {
191195
mp_raise_RuntimeError(translate("wifi is not enabled"));
@@ -275,6 +279,10 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
275279
return WIFI_RADIO_ERROR_UNSPECIFIED;
276280
}
277281

282+
bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self) {
283+
return cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) == CYW43_LINK_UP;
284+
}
285+
278286
mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
279287
mp_raise_NotImplementedError(NULL);
280288
}

shared-bindings/wifi/Radio.c

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
310310
//| authmode: Optional[AuthMode] = None,
311311
//| max_connections: Optional[int] = 4
312312
8000 //| ) -> None:
313-
//| """Starts an Access Point with the specified ssid and password.
313+
//| """Starts running an access point with the specified ssid and password.
314314
//|
315315
//| If ``channel`` is given, the access point will use that channel unless
316316
//| a station is already operating on a different channel.
@@ -376,14 +376,24 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
376376
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_start_ap_obj, 1, wifi_radio_start_ap);
377377

378378
//| def stop_ap(self) -> None:
379-
//| """Stops the Access Point."""
379+
//| """Stops the access point."""
380380
//| ...
381381
STATIC mp_obj_t wifi_radio_stop_ap(mp_obj_t self) {
382382
common_hal_wifi_radio_stop_ap(self);
383383
return mp_const_none;
384384
}
385385
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_ap_obj, wifi_radio_stop_ap);
386386

387+
//| ap_active: bool
388+
//| """True if running as an access point. (read-only)"""
389+
STATIC mp_obj_t wifi_radio_get_ap_active(mp_obj_t self) {
390+
return mp_obj_new_bool(common_hal_wifi_radio_get_ap_active(self));
391+
}
392+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_active_obj, wifi_radio_get_ap_active);
393+
394+
MP_PROPERTY_GETTER(wifi_radio_ap_active_obj,
395+
(mp_obj_t)&wifi_radio_get_ap_active_obj);
396+
387397
//| def connect(
388398
//| self,
389399
//| ssid: Union[str | ReadableBuffer],
@@ -464,44 +474,50 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
464474
}
465475
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect);
466476

477+
//| connected: bool
478+
//| """True if connected to an access point (read-only)."""
479+
STATIC mp_obj_t wifi_radio_get_connected(mp_obj_t self) {
480+
return mp_obj_new_bool(common_hal_wifi_radio_get_connected(self));
481+
}
482+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_connected_obj, wifi_radio_get_connected);
483+
484+
MP_PROPERTY_GETTER(wifi_radio_connected_obj,
485+
(mp_obj_t)&wifi_radio_get_connected_obj);
486+
467487
//| ipv4_gateway: Optional[ipaddress.IPv4Address]
468-
//| """IP v4 Address of the station gateway when connected to an access point. None otherwise."""
488+
//| """IP v4 Address of the station gateway when connected to an access point. None otherwise. (read-only)"""
469489
STATIC mp_obj_t wifi_radio_get_ipv4_gateway(mp_obj_t self) {
470490
return common_hal_wifi_radio_get_ipv4_gateway(self);
471-
472491
}
473492
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_obj, wifi_radio_get_ipv4_gateway);
474493

475494
MP_PROPERTY_GETTER(wifi_radio_ipv4_gateway_obj,
476495
(mp_obj_t)&wifi_radio_get_ipv4_gateway_obj);
477496

478497
//| ipv4_gateway_ap: Optional[ipaddress.IPv4Address]
479-
//| """IP v4 Address of the access point gateway, when enabled. None otherwise."""
498+
//| """IP v4 Address of the access point gateway, when enabled. None otherwise. (read-only)"""
480499
STATIC mp_obj_t wifi_radio_get_ipv4_gateway_ap(mp_obj_t self) {
481500
return common_hal_wifi_radio_get_ipv4_gateway_ap(self);
482-
483501
}
484502
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_ap_obj, wifi_radio_get_ipv4_gateway_ap);
485503

486504
MP_PROPERTY_GETTER(wifi_radio_ipv4_gateway_ap_obj,
487505
(mp_obj_t)&wifi_radio_get_ipv4_gateway_ap_obj);
488506

489507
//| ipv4_subnet: Optional[ipaddress.IPv4Address]
490-
//| """IP v4 Address of the station subnet when connected to an access point. None otherwise."""
508+
//| """IP v4 Address of the station subnet when connected to an access point. None otherwise. (read-only)"""
491509
STATIC mp_obj_t wifi_radio_get_ipv4_subnet(mp_obj_t self) {
492510
return common_hal_wifi_radio_get_ipv4_subnet(self);
493-
494511
}
495512
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_obj, wifi_radio_get_ipv4_subnet);
496513

497514
MP_PROPERTY_GETTER(wifi_radio_ipv4_subnet_obj,
498515
(mp_obj_t)&wifi_radio_get_ipv4_subnet_obj);
499516

500517
//| ipv4_subnet_ap: Optional[ipaddress.IPv4Address]
501-
//| """IP v4 Address of the access point subnet, when enabled. None otherwise."""
518+
//| """IP v4 Address of the access point subnet, when enabled. None otherwise. (read-only)"""
502519
STATIC mp_obj_t wifi_radio_get_ipv4_subnet_ap(mp_obj_t self) {
503520
return common_hal_wifi_radio_get_ipv4_subnet_ap(self);
504-
505521
}
506522
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_ap_obj, wifi_radio_get_ipv4_subnet_ap);
507523

@@ -538,10 +554,9 @@ STATIC mp_obj_t wifi_radio_set_ipv4_address(size_t n_args, const mp_obj_t *pos_a
538554
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_set_ipv4_address_obj, 1, wifi_radio_set_ipv4_address);
539555

540556
//| ipv4_address: Optional[ipaddress.IPv4Address]
541-
//| """IP v4 Address of the station when connected to an access point. None otherwise."""
557+
//| """IP v4 Address of the station when connected to an access point. None otherwise. (read-only)"""
542558
STATIC mp_obj_t _wifi_radio_get_ipv4_address(mp_obj_t self) {
543559
return common_hal_wifi_radio_get_ipv4_address(self);
544-
545560
}
546561
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_address_obj, _wifi_radio_get_ipv4_address);
547562

@@ -552,7 +567,6 @@ MP_PROPERTY_GETTER(wifi_radio_ipv4_address_obj,
552567
//| """IP v4 Address of the access point, when enabled. None otherwise."""
553568
STATIC mp_obj_t wifi_radio_get_ipv4_address_ap(mp_obj_t self) {
554569
return common_hal_wifi_radio_get_ipv4_address_ap(self);
555-
556570
}
557571
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_address_ap_obj, wifi_radio_get_ipv4_address_ap);
558572

@@ -563,7 +577,6 @@ MP_PROPERTY_GETTER(wifi_radio_ipv4_address_ap_obj,
563577
//| """IP v4 Address of the DNS server to be used."""
564578
STATIC mp_obj_t wifi_radio_get_ipv4_dns(mp_obj_t self) {
565579
return common_hal_wifi_radio_get_ipv4_dns(self);
566-
567580
}
568581
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_dns_obj, wifi_radio_get_ipv4_dns);
569582

@@ -582,7 +595,6 @@ MP_PROPERTY_GETSET(wifi_radio_ipv4_dns_obj,
582595
//| """Network object containing BSSID, SSID, authmode, channel, country and RSSI when connected to an access point. None otherwise."""
583596
STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
584597
return common_hal_wifi_radio_get_ap_info(self);
585-
586598
}
587599
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info);
588600

@@ -656,12 +668,14 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
656668

657669
{ MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) },
658670
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
671+
{ MP_ROM_QSTR(MP_QSTR_ap_active), MP_ROM_PTR(&wifi_radio_ap_active_obj) },
659672

660673
{ MP_ROM_QSTR(MP_QSTR_start_dhcp), MP_ROM_PTR(&wifi_radio_start_dhcp_client_obj) },
661674
{ MP_ROM_QSTR(MP_QSTR_stop_dhcp), MP_ROM_PTR(&wifi_radio_stop_dhcp_client_obj) },
662675

663676
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
664677
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
678+
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&wifi_radio_connected_obj) },
665679

666680
{ MP_ROM_QSTR(MP_QSTR_ap_info), MP_ROM_PTR(&wifi_radio_ap_info_obj) },
667681
{ MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_obj) },
@@ -674,9 +688,6 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
674688

675689
{ MP_ROM_QSTR(MP_QSTR_set_ipv4_address), MP_ROM_PTR(&wifi_radio_set_ipv4_address_obj) },
676690

677-
// { MP_ROM_QSTR(MP_QSTR_access_point_active), MP_ROM_PTR(&wifi_radio_access_point_active_obj) },
678-
// { MP_ROM_QSTR(MP_QSTR_start_access_point), MP_ROM_PTR(&wifi_radio_start_access_point_obj) },
679-
680691
{ MP_ROM_QSTR(MP_QSTR_ping), MP_ROM_PTR(&wifi_radio_ping_obj) },
681692
};
682693

shared-bindings/wifi/Radio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ extern void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self);
9595

9696
extern void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint32_t authmodes, uint8_t max_connections);
9797
extern void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self);
98+
extern bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self);
9899

99100
extern void common_hal_wifi_radio_start_dhcp_client(wifi_radio_obj_t *self);
100101
extern void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self);
101102

102103
extern wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len);
104+
extern bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self);
103105

104106
extern mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self);
105107
extern mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self);

0 commit comments

Comments
 (0)
0