3535#include  "py/runtime.h" 
3636#include  "shared-bindings/ipaddress/IPv4Address.h" 
3737#include  "shared-bindings/wifi/ScannedNetworks.h" 
38+ #include  "shared-bindings/wifi/AuthMode.h" 
3839#include  "shared-module/ipaddress/__init__.h" 
3940
4041#include  "components/esp_wifi/include/esp_wifi.h" 
4142#include  "components/lwip/include/apps/ping/ping_sock.h" 
4243
4344#define  MAC_ADDRESS_LENGTH  6
4445
45- static  void  start_station (wifi_radio_obj_t  * self ) {
46-     if  (self -> sta_mode ) {
47-         return ;
48-     }
46+ static  void  set_mode_station (wifi_radio_obj_t  * self , bool  state ) {
4947    wifi_mode_t  next_mode ;
50-     if  (self -> ap_mode ) {
51-         next_mode  =  WIFI_MODE_APSTA ;
48+     if  (state ) {
49+         if  (self -> ap_mode ) {
50+             next_mode  =  WIFI_MODE_APSTA ;
51+         } else  {
52+             next_mode  =  WIFI_MODE_STA ;
53+         }
5254    } else  {
53-         next_mode  =  WIFI_MODE_STA ;
55+         if  (self -> ap_mode ) {
56+             next_mode  =  WIFI_MODE_AP ;
57+         } else  {
58+             next_mode  =  WIFI_MODE_NULL ;
59+         }
5460    }
5561    esp_wifi_set_mode (next_mode );
62+     self -> sta_mode  =  state ;
63+ }
5664
57-     self -> sta_mode  =  1 ;
65+ static  void  set_mode_ap (wifi_radio_obj_t  * self , bool  state ) {
66+     wifi_mode_t  next_mode ;
67+     if  (state ) {
68+         if  (self -> sta_mode ) {
69+             next_mode  =  WIFI_MODE_APSTA ;
70+         } else  {
71+             next_mode  =  WIFI_MODE_AP ;
72+         }
73+     } else  {
74+         if  (self -> sta_mode ) {
75+             next_mode  =  WIFI_MODE_STA ;
76+         } else  {
77+             next_mode  =  WIFI_MODE_NULL ;
78+         }
79+     }
80+     esp_wifi_set_mode (next_mode );
81+     self -> ap_mode  =  state ;
5882}
5983
6084bool  common_hal_wifi_radio_get_enabled (wifi_radio_obj_t  * self ) {
@@ -71,8 +95,6 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
7195        return ;
7296    }
7397    if  (!self -> started  &&  enabled ) {
74-         // esp_wifi_start() would default to soft-AP, thus setting it to station 
75-         start_station (self );
7698        ESP_ERROR_CHECK (esp_wifi_start ());
7799        self -> started  =  true;
78100        return ;
@@ -85,14 +107,20 @@ mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
85107    return  mp_obj_new_bytes (mac , MAC_ADDRESS_LENGTH );
86108}
87109
110+ mp_obj_t  common_hal_wifi_radio_get_mac_address_ap (wifi_radio_obj_t  * self ) {
111+     uint8_t  mac [MAC_ADDRESS_LENGTH ];
112+     esp_wifi_get_mac (ESP_IF_WIFI_AP , mac );
113+     return  mp_obj_new_bytes (mac , MAC_ADDRESS_LENGTH );
114+ }
115+ 
88116mp_obj_t  common_hal_wifi_radio_start_scanning_networks (wifi_radio_obj_t  * self ) {
89117    if  (self -> current_scan  !=  NULL ) {
90118        mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
91119    }
92120    if  (!common_hal_wifi_radio_get_enabled (self )) {
93121        mp_raise_RuntimeError (translate ("wifi is not enabled" ));
94122    }
95-     start_station (self );
123+     set_mode_station (self , true );
96124
97125    wifi_scannednetworks_obj_t  * scan  =  m_new_obj (wifi_scannednetworks_obj_t );
98126    scan -> base .type  =  & wifi_scannednetworks_type ;
@@ -127,6 +155,50 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host
127155    esp_netif_set_hostname (self -> netif , hostname );
128156}
129157
158+ void  common_hal_wifi_radio_start_station (wifi_radio_obj_t  * self ) {
159+     set_mode_station (self , true);
160+ }
161+ 
162+ void  common_hal_wifi_radio_stop_station (wifi_radio_obj_t  * self ) {
163+     set_mode_station (self , false);
164+ }
165+ 
166+ 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 , uint8_t  authmode ) {
167+     set_mode_ap (self , true);
168+ 
169+     switch  (authmode ) {
170+         case  (1  << AUTHMODE_OPEN ):
171+             authmode  =  WIFI_AUTH_OPEN ;
172+             break ;
173+         case  ((1  << AUTHMODE_WPA ) | (1  << AUTHMODE_PSK )):
174+             authmode  =  WIFI_AUTH_WPA_PSK ;
175+             break ;
176+         case  ((1  << AUTHMODE_WPA2 ) | (1  << AUTHMODE_PSK )):
177+             authmode  =  WIFI_AUTH_WPA2_PSK ;
178+             break ;
179+         case  ((1  << AUTHMODE_WPA ) | (1  << AUTHMODE_WPA2 ) | (1  << AUTHMODE_PSK )):
180+             authmode  =  WIFI_AUTH_WPA_WPA2_PSK ;
181+             break ;
182+         default :
183+             mp_raise_ValueError (translate ("Invalid AuthMode" ));
184+             break ;
185+     }
186+ 
187+     wifi_config_t  * config  =  & self -> ap_config ;
188+     memcpy (& config -> ap .ssid , ssid , ssid_len );
189+     config -> ap .ssid [ssid_len ] =  0 ;
190+     memcpy (& config -> ap .password , password , password_len );
191+     config -> ap .password [password_len ] =  0 ;
192+     config -> ap .channel  =  channel ;
193+     config -> ap .authmode  =  authmode ;
194+     config -> ap .max_connection  =  4 ; // kwarg? 
195+     esp_wifi_set_config (WIFI_IF_AP , config );
196+ }
197+ 
198+ void  common_hal_wifi_radio_stop_ap (wifi_radio_obj_t  * self ) {
199+     set_mode_ap (self , false);
200+ }
201+ 
130202wifi_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 ) {
131203    if  (!common_hal_wifi_radio_get_enabled (self )) {
132204        mp_raise_RuntimeError (translate ("wifi is not enabled" ));
@@ -147,7 +219,7 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
147219    // explicitly clear bits since xEventGroupWaitBits may have timed out 
148220    xEventGroupClearBits (self -> event_group_handle , WIFI_CONNECTED_BIT );
149221    xEventGroupClearBits (self -> event_group_handle , WIFI_DISCONNECTED_BIT );
150-     start_station (self );
222+     set_mode_station (self , true );
151223
152224    wifi_config_t  * config  =  & self -> sta_config ;
153225    memcpy (& config -> sta .ssid , ssid , ssid_len );
@@ -239,6 +311,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) {
239311    return  common_hal_ipaddress_new_ipv4address (self -> ip_info .gw .addr );
240312}
241313
314+ mp_obj_t  common_hal_wifi_radio_get_ipv4_gateway_ap (wifi_radio_obj_t  * self ) {
315+     if  (!esp_netif_is_netif_up (self -> ap_netif )) {
316+         return  mp_const_none ;
317+     }
318+     esp_netif_get_ip_info (self -> ap_netif , & self -> ap_ip_info );
319+     return  common_hal_ipaddress_new_ipv4address (self -> ap_ip_info .gw .addr );
320+ }
321+ 
242322mp_obj_t  common_hal_wifi_radio_get_ipv4_subnet (wifi_radio_obj_t  * self ) {
243323    if  (!esp_netif_is_netif_up (self -> netif )) {
244324        return  mp_const_none ;
@@ -247,6 +327,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
247327    return  common_hal_ipaddress_new_ipv4address (self -> ip_info .netmask .addr );
248328}
249329
330+ mp_obj_t  common_hal_wifi_radio_get_ipv4_subnet_ap (wifi_radio_obj_t  * self ) {
331+     if  (!esp_netif_is_netif_up (self -> ap_netif )) {
332+         return  mp_const_none ;
333+     }
334+     esp_netif_get_ip_info (self -> ap_netif , & self -> ap_ip_info );
335+     return  common_hal_ipaddress_new_ipv4address (self -> ap_ip_info .netmask .addr );
336+ }
337+ 
250338mp_obj_t  common_hal_wifi_radio_get_ipv4_address (wifi_radio_obj_t  * self ) {
251339    if  (!esp_netif_is_netif_up (self -> netif )) {
252340        return  mp_const_none ;
@@ -255,6 +343,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
255343    return  common_hal_ipaddress_new_ipv4address (self -> ip_info .ip .addr );
256344}
257345
346+ mp_obj_t  common_hal_wifi_radio_get_ipv4_address_ap (wifi_radio_obj_t  * self ) {
347+     if  (!esp_netif_is_netif_up (self -> ap_netif )) {
348+         return  mp_const_none ;
349+     }
350+     esp_netif_get_ip_info (self -> ap_netif , & self -> ap_ip_info );
351+     return  common_hal_ipaddress_new_ipv4address (self -> ap_ip_info .ip .addr );
352+ }
353+ 
258354mp_obj_t  common_hal_wifi_radio_get_ipv4_dns (wifi_radio_obj_t  * self ) {
259355    if  (!esp_netif_is_netif_up (self -> netif )) {
260356        return  mp_const_none ;
0 commit comments