35
35
#include "py/runtime.h"
36
36
#include "shared-bindings/ipaddress/IPv4Address.h"
37
37
#include "shared-bindings/wifi/ScannedNetworks.h"
38
+ #include "shared-bindings/wifi/AuthMode.h"
38
39
#include "shared-module/ipaddress/__init__.h"
39
40
40
41
#include "components/esp_wifi/include/esp_wifi.h"
41
42
#include "components/lwip/include/apps/ping/ping_sock.h"
42
43
43
44
#define MAC_ADDRESS_LENGTH 6
44
45
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 ) {
49
47
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
+ }
52
54
} 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
+ }
54
60
}
55
61
esp_wifi_set_mode (next_mode );
62
+ self -> sta_mode = state ;
63
+ }
56
64
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 ;
58
82
}
59
83
60
84
bool 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) {
71
95
return ;
72
96
}
73
97
if (!self -> started && enabled ) {
74
- // esp_wifi_start() would default to soft-AP, thus setting it to station
75
- start_station (self );
76
98
ESP_ERROR_CHECK (esp_wifi_start ());
77
99
self -> started = true;
78
100
return ;
@@ -85,14 +107,20 @@ mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
85
107
return mp_obj_new_bytes (mac , MAC_ADDRESS_LENGTH );
86
108
}
87
109
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
+
88
116
mp_obj_t common_hal_wifi_radio_start_scanning_networks (wifi_radio_obj_t * self ) {
89
117
if (self -> current_scan != NULL ) {
90
118
mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
91
119
}
92
120
if (!common_hal_wifi_radio_get_enabled (self )) {
93
121
mp_raise_RuntimeError (translate ("wifi is not enabled" ));
94
122
}
95
- start_station (self );
123
+ set_mode_station (self , true );
96
124
97
125
wifi_scannednetworks_obj_t * scan = m_new_obj (wifi_scannednetworks_obj_t );
98
126
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
127
155
esp_netif_set_hostname (self -> netif , hostname );
128
156
}
129
157
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
+
130
202
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 ) {
131
203
if (!common_hal_wifi_radio_get_enabled (self )) {
132
204
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
147
219
// explicitly clear bits since xEventGroupWaitBits may have timed out
148
220
xEventGroupClearBits (self -> event_group_handle , WIFI_CONNECTED_BIT );
149
221
xEventGroupClearBits (self -> event_group_handle , WIFI_DISCONNECTED_BIT );
150
- start_station (self );
222
+ set_mode_station (self , true );
151
223
152
224
wifi_config_t * config = & self -> sta_config ;
153
225
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) {
239
311
return common_hal_ipaddress_new_ipv4address (self -> ip_info .gw .addr );
240
312
}
241
313
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
+
242
322
mp_obj_t common_hal_wifi_radio_get_ipv4_subnet (wifi_radio_obj_t * self ) {
243
323
if (!esp_netif_is_netif_up (self -> netif )) {
244
324
return mp_const_none ;
@@ -247,6 +327,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
247
327
return common_hal_ipaddress_new_ipv4address (self -> ip_info .netmask .addr );
248
328
}
249
329
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
+
250
338
mp_obj_t common_hal_wifi_radio_get_ipv4_address (wifi_radio_obj_t * self ) {
251
339
if (!esp_netif_is_netif_up (self -> netif )) {
252
340
return mp_const_none ;
@@ -255,6 +343,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
255
343
return common_hal_ipaddress_new_ipv4address (self -> ip_info .ip .addr );
256
344
}
257
345
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
+
258
354
mp_obj_t common_hal_wifi_radio_get_ipv4_dns (wifi_radio_obj_t * self ) {
259
355
if (!esp_netif_is_netif_up (self -> netif )) {
260
356
return mp_const_none ;
0 commit comments