Skip to content

Commit db8bd56

Browse files
authored
Merge pull request #7946 from anecdata/ap_ipv4
Allow Access Point static IPv4 on the espressif port
2 parents 02d4c81 + 29bf64d commit db8bd56

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,16 @@ void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self) {
475475
esp_netif_dhcpc_stop(self->netif);
476476
}
477477

478+
void common_hal_wifi_radio_start_dhcp_server(wifi_radio_obj_t *self) {
479+
esp_netif_dhcps_start(self->ap_netif);
480+
}
481+
482+
void common_hal_wifi_radio_stop_dhcp_server(wifi_radio_obj_t *self) {
483+
esp_netif_dhcps_stop(self->ap_netif);
484+
}
485+
478486
void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway, mp_obj_t ipv4_dns) {
479-
common_hal_wifi_radio_stop_dhcp_client(self); // Must stop DHCP to set a manual address
487+
common_hal_wifi_radio_stop_dhcp_client(self); // Must stop station DHCP to set a manual address
480488

481489
esp_netif_ip_info_t ip_info;
482490
ipaddress_ipaddress_to_esp_idf_ip4(ipv4, &ip_info.ip);
@@ -490,6 +498,19 @@ void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv
490498
}
491499
}
492500

501+
void common_hal_wifi_radio_set_ipv4_address_ap(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway) {
502+
common_hal_wifi_radio_stop_dhcp_server(self); // Must stop access point DHCP to set a manual address
503+
504+
esp_netif_ip_info_t ip_info;
505+
ipaddress_ipaddress_to_esp_idf_ip4(ipv4, &ip_info.ip);
506+
ipaddress_ipaddress_to_esp_idf_ip4(netmask, &ip_info.netmask);
507+
ipaddress_ipaddress_to_esp_idf_ip4(gateway, &ip_info.gw);
508+
509+
esp_netif_set_ip_info(self->ap_netif, &ip_info);
510+
511+
common_hal_wifi_radio_start_dhcp_server(self); // restart access point DHCP
512+
}
513+
493514
mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) {
494515
esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
495516
ipaddress_ipaddress_to_esp_idf(ip_address, &ping_config.target_addr);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self) {
358358
dhcp_stop(NETIF_STA);
359359
}
360360

361+
void common_hal_wifi_radio_start_dhcp_server(wifi_radio_obj_t *self) {
362+
mp_raise_NotImplementedError(NULL);
363+
}
364+
365+
void common_hal_wifi_radio_stop_dhcp_server(wifi_radio_obj_t *self) {
366+
mp_raise_NotImplementedError(NULL);
367+
}
368+
361369
void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway, mp_obj_t ipv4_dns) {
362370
common_hal_wifi_radio_stop_dhcp_client(self);
363371

@@ -371,6 +379,10 @@ void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv
371379
}
372380
}
373381

382+
void common_hal_wifi_radio_set_ipv4_address_ap(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway) {
383+
mp_raise_NotImplementedError(NULL);
384+
}
385+
374386
volatile bool ping_received;
375387
uint32_t ping_time;
376388

shared-bindings/wifi/Radio.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
317317
//| *,
318318
//| channel: int = 1,
319319
//| authmode: Optional[AuthMode] = None,
320-
//| max_connections: Optional[int] = 4
320+
//| max_connections: Optional[int] = 4,
321321
//| ) -> None:
322322
//| """Starts running an access point with the specified ssid and password.
323323
//|
@@ -416,7 +416,7 @@ MP_PROPERTY_GETTER(wifi_radio_ap_active_obj,
416416
//| *,
417417
//| channel: int = 0,
418418
//| bssid: Optional[Union[str | ReadableBuffer]] = None,
419-
//| timeout: Optional[float] = None
419+
//| timeout: Optional[float] = None,
420420
//| ) -> None:
421421
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
422422
//| automatically once one connection succeeds.
@@ -551,7 +551,7 @@ MP_PROPERTY_GETTER(wifi_radio_ipv4_subnet_ap_obj,
551551
//| ipv4: ipaddress.IPv4Address,
552552
//| netmask: ipaddress.IPv4Address,
553553
//| gateway: ipaddress.IPv4Address,
554-
//| ipv4_dns: Optional[ipaddress.IPv4Address]
554+
//| ipv4_dns: Optional[ipaddress.IPv4Address],
555555
//| ) -> None:
556556
//| """Sets the IP v4 address of the station. Must include the netmask and gateway. DNS address is optional.
557557
//| Setting the address manually will stop the DHCP client."""
@@ -574,6 +574,32 @@ STATIC mp_obj_t wifi_radio_set_ipv4_address(size_t n_args, const mp_obj_t *pos_a
574574
}
575575
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_set_ipv4_address_obj, 1, wifi_radio_set_ipv4_address);
576576

577+
//| def set_ipv4_address_ap(
578+
//| self,
579+
//| *,
580+
//| ipv4: ipaddress.IPv4Address,
581+
//| netmask: ipaddress.IPv4Address,
582+
//| gateway: ipaddress.IPv4Address,
583+
//| ) -> None:
584+
//| """Sets the IP v4 address of the access point. Must include the netmask and gateway."""
585+
//| ...
586+
STATIC mp_obj_t wifi_radio_set_ipv4_address_ap(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
587+
enum { ARG_ipv4, ARG_netmask, ARG_gateway };
588+
static const mp_arg_t allowed_args[] = {
589+
{ MP_QSTR_ipv4, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, },
590+
{ MP_QSTR_netmask, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, },
591+
{ MP_QSTR_gateway, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, },
592+
};
593+
594+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
595+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
596+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
597+
598+
common_hal_wifi_radio_set_ipv4_address_ap(self, args[ARG_ipv4].u_obj, args[ARG_netmask].u_obj, args[ARG_gateway].u_obj);
599+
return mp_const_none;
600+
}
601+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_set_ipv4_address_ap_obj, 1, wifi_radio_set_ipv4_address_ap);
602+
577603
//| ipv4_address: Optional[ipaddress.IPv4Address]
578604
//| """IP v4 Address of the station when connected to an access point. None otherwise. (read-only)"""
579605
STATIC mp_obj_t _wifi_radio_get_ipv4_address(mp_obj_t self) {
@@ -620,7 +646,7 @@ STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
620646
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info);
621647

622648
//| def start_dhcp(self) -> None:
623-
//| """Starts the DHCP client."""
649+
//| """Starts the station DHCP client."""
624650
//| ...
625651
STATIC mp_obj_t wifi_radio_start_dhcp_client(mp_obj_t self) {
626652
common_hal_wifi_radio_start_dhcp_client(self);
@@ -629,14 +655,32 @@ STATIC mp_obj_t wifi_radio_start_dhcp_client(mp_obj_t self) {
629655
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_start_dhcp_client_obj, wifi_radio_start_dhcp_client);
630656

631657
//| def stop_dhcp(self) -> None:
632-
//| """Stops the DHCP client. Needed to assign a static IP address."""
658+
//| """Stops the station DHCP client. Needed to assign a static IP address."""
633659
//| ...
634660
STATIC mp_obj_t wifi_radio_stop_dhcp_client(mp_obj_t self) {
635661
common_hal_wifi_radio_stop_dhcp_client(self);
636662
return mp_const_none;
637663
}
638664
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_dhcp_client_obj, wifi_radio_stop_dhcp_client);
639665

666+
//| def start_dhcp_ap(self) -> None:
667+
//| """Starts the access point DHCP server."""
668+
//| ...
669+
STATIC mp_obj_t wifi_radio_start_dhcp_server(mp_obj_t self) {
670+
common_hal_wifi_radio_start_dhcp_server(self);
671+
return mp_const_none;
672+
}
673+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_start_dhcp_server_obj, wifi_radio_start_dhcp_server);
674+
675+
//| def stop_dhcp_ap(self) -> None:
676+
//| """Stops the access point DHCP server. Needed to assign a static IP address."""
677+
//| ...
678+
STATIC mp_obj_t wifi_radio_stop_dhcp_server(mp_obj_t self) {
679+
common_hal_wifi_radio_stop_dhcp_server(self);
680+
return mp_const_none;
681+
}
682+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_dhcp_server_obj, wifi_radio_stop_dhcp_server);
683+
640684
MP_PROPERTY_GETTER(wifi_radio_ap_info_obj,
641685
(mp_obj_t)&wifi_radio_get_ap_info_obj);
642686

@@ -693,6 +737,8 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
693737

694738
{ MP_ROM_QSTR(MP_QSTR_start_dhcp), MP_ROM_PTR(&wifi_radio_start_dhcp_client_obj) },
695739
{ MP_ROM_QSTR(MP_QSTR_stop_dhcp), MP_ROM_PTR(&wifi_radio_stop_dhcp_client_obj) },
740+
{ MP_ROM_QSTR(MP_QSTR_start_dhcp_ap), MP_ROM_PTR(&wifi_radio_start_dhcp_server_obj) },
741+
{ MP_ROM_QSTR(MP_QSTR_stop_dhcp_ap), MP_ROM_PTR(&wifi_radio_stop_dhcp_server_obj) },
696742

697743
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
698744
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
@@ -708,6 +754,7 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
708754
{ MP_ROM_QSTR(MP_QSTR_ipv4_address_ap), MP_ROM_PTR(&wifi_radio_ipv4_address_ap_obj) },
709755

710756
{ MP_ROM_QSTR(MP_QSTR_set_ipv4_address), MP_ROM_PTR(&wifi_radio_set_ipv4_address_obj) },
757+
{ MP_ROM_QSTR(MP_QSTR_set_ipv4_address_ap), MP_ROM_PTR(&wifi_radio_set_ipv4_address_ap_obj) },
711758

712759
{ MP_ROM_QSTR(MP_QSTR_ping), MP_ROM_PTR(&wifi_radio_ping_obj) },
713760
};

shared-bindings/wifi/Radio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ extern bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self);
9999

100100
extern void common_hal_wifi_radio_start_dhcp_client(wifi_radio_obj_t *self);
101101
extern void common_hal_wifi_radio_stop_dhcp_client(wifi_radio_obj_t *self);
102+
extern void common_hal_wifi_radio_start_dhcp_server(wifi_radio_obj_t *self);
103+
extern void common_hal_wifi_radio_stop_dhcp_server(wifi_radio_obj_t *self);
102104

103105
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);
104106
extern bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self);
@@ -115,6 +117,7 @@ extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self);
115117
extern mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self);
116118

117119
extern void common_hal_wifi_radio_set_ipv4_address(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway, mp_obj_t ipv4_dns_addr);
120+
extern void common_hal_wifi_radio_set_ipv4_address_ap(wifi_radio_obj_t *self, mp_obj_t ipv4, mp_obj_t netmask, mp_obj_t gateway);
118121

119122
extern mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout);
120123

0 commit comments

Comments
 (0)