1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/wifi/__init__.h"
#include "shared-bindings/wifi/AuthMode.h"
#include <regex.h>
#include <string.h>
#include "py/runtime.h"
#include "py/objproperty.h"
#define MAC_ADDRESS_LENGTH 6
//| class Radio:
//| """Native wifi radio.
//|
//| This class manages the station and access point functionality of the native
//| Wifi radio.
//|
//| """
//|
//| def __init__(self) -> None:
//| """You cannot create an instance of `wifi.Radio`.
//| Use `wifi.radio` to access the sole instance available."""
//| ...
//|
//| enabled: bool
//| """``True`` when the wifi radio is enabled.
//| If you set the value to ``False``, any open sockets will be closed.
//| """
//|
STATIC mp_obj_t wifi_radio_get_enabled(mp_obj_t self) {
return mp_obj_new_bool(common_hal_wifi_radio_get_enabled(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_enabled_obj, wifi_radio_get_enabled);
static mp_obj_t wifi_radio_set_enabled(mp_obj_t self, mp_obj_t value) {
const bool enabled = mp_obj_is_true(value);
common_hal_wifi_radio_set_enabled(self, enabled);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_enabled_obj, wifi_radio_set_enabled);
MP_PROPERTY_GETSET(wifi_radio_enabled_obj,
(mp_obj_t)&wifi_radio_get_enabled_obj,
(mp_obj_t)&wifi_radio_set_enabled_obj);
//| hostname: Union[str | ReadableBuffer]
//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
//| the changes would only be reflected once the interface restarts/reconnects."""
//|
STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_wifi_radio_get_hostname(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
mp_buffer_info_t hostname;
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
if (hostname.len < 1 || hostname.len > 253) {
mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
}
#ifndef CONFIG_IDF_TARGET_ESP32C3
regex_t regex; // validate hostname according to RFC 1123
regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB);
if (regexec(®ex, hostname.buf, 0, NULL, 0)) {
mp_raise_ValueError(translate("invalid hostname"));
}
regfree(®ex);
#endif
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_hostname(self, hostname.buf);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
MP_PROPERTY_GETSET(wifi_radio_hostname_obj,
(mp_obj_t)&wifi_radio_get_hostname_obj,
(mp_obj_t)&wifi_radio_set_hostname_obj);
//| mac_address: ReadableBuffer
//| """MAC address for the station. When the address is altered after interface is connected
//| the changes would only be reflected once the interface reconnects."""
//|
STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_FROM_PTR(common_hal_wifi_radio_get_mac_address(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_mac_address_obj, wifi_radio_get_mac_address);
STATIC mp_obj_t wifi_radio_set_mac_address(mp_obj_t self_in, mp_obj_t mac_address_in) {
mp_buffer_info_t mac_address;
mp_get_buffer_raise(mac_address_in, &mac_address, MP_BUFFER_READ);
if (mac_address.len != MAC_ADDRESS_LENGTH) {
mp_raise_ValueError(translate("Invalid MAC address"));
}
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_mac_address(self, mac_address.buf);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_mac_address_obj, wifi_radio_set_mac_address);
MP_PROPERTY_GETSET(wifi_radio_mac_address_obj,
(mp_obj_t)&wifi_radio_get_mac_address_obj,
(mp_obj_t)&wifi_radio_set_mac_address_obj);
//| mac_address_ap: ReadableBuffer
//| """MAC address for the AP. When the address is altered after interface is started
//| the changes would only be reflected once the interface restarts."""
//|
STATIC mp_obj_t wifi_radio_get_mac_address_ap(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_FROM_PTR(common_hal_wifi_radio_get_mac_address_ap(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_mac_address_ap_obj, wifi_radio_get_mac_address_ap);
STATIC mp_obj_t wifi_radio_set_mac_address_ap(mp_obj_t self_in, mp_obj_t mac_address_in) {
mp_buffer_info_t mac_address;
mp_get_buffer_raise(mac_address_in, &mac_address, MP_BUFFER_READ);
if (mac_address.len != MAC_ADDRESS_LENGTH) {
mp_raise_ValueError(translate("Invalid MAC address"));
}
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_mac_address_ap(self, mac_address.buf);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_mac_address_ap_obj, wifi_radio_set_mac_address_ap);
MP_PROPERTY_GETSET(wifi_radio_mac_address_ap_obj,
(mp_obj_t)&wifi_radio_get_mac_address_ap_obj,
(mp_obj_t)&wifi_radio_set_mac_address_ap_obj);
//| def start_scanning_networks(self, *, start_channel: int = 1, stop_channel: int = 11) -> Iterable[Network]:
//| """Scans for available wifi networks over the given channel range. Make sure the channels are allowed in your country."""
//| ...
//|
STATIC mp_obj_t wifi_radio_start_scanning_networks(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_wifi_radio_start_scanning_networks(self);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_start_scanning_networks_obj, wifi_radio_start_scanning_networks);
//| def stop_scanning_networks(self) -> None:
//| """Stop scanning for Wifi networks and free any resources used to do it."""
//| ...
//|
STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_stop_scanning_networks(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
//| def start_station(self) -> None:
//| """Starts a Station."""
//| ...
//|
STATIC mp_obj_t wifi_radio_start_station(mp_obj_t self) {
common_hal_wifi_radio_start_station(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_start_station_obj, wifi_radio_start_station);
//| def stop_station(self) -> None:
//| """Stops the Station."""
//| ...
//|
STATIC mp_obj_t wifi_radio_stop_station(mp_obj_t self) {
common_hal_wifi_radio_stop_station(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
//| def start_ap(self,
//| ssid: Union[str | ReadableBuffer],
//| password: Union[str | ReadableBuffer] = "",
//| *,
//| channel: Optional[int] = 1,
//| authmode: Optional[AuthMode],
//| max_connections: Optional[int] = 4) -> None:
//| """Starts an Access Point with the specified ssid and password.
//|
//| If ``channel`` is given, the access point will use that channel unless
//| a station is already operating on a different channel.
//|
//| If ``authmode`` is given, the access point will use that Authentication
//| mode. If a password is given, ``authmode`` must not be ``OPEN``.
//| If ``authmode`` isn't given, ``OPEN`` will be used when password isn't provided,
//| otherwise ``WPA_WPA2_PSK``.
//|
//| If ``max_connections`` is given, the access point will allow up to
//| that number of stations to connect."""
//| ...
//|
STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_ssid, ARG_password, ARG_channel, ARG_authmode, ARG_max_connections };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_authmode, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_max_connections, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 4} },
};
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
uint8_t authmode = 0;
if (args[ARG_authmode].u_obj != MP_OBJ_NULL) {
mp_obj_iter_buf_t iter_buf;
mp_obj_t item, iterable = mp_getiter(args[ARG_authmode].u_obj, &iter_buf);
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
authmode |= (1 << (wifi_authmode_t)cp_enum_value(&wifi_authmode_type, item));
}
}
mp_buffer_info_t ssid;
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);
mp_buffer_info_t password;
password.len = 0;
if (args[ARG_password].u_obj != MP_OBJ_NULL) {
if (authmode == 1) {
mp_raise_ValueError(translate("AuthMode.OPEN is not used with password"));
} else if (authmode == 0) {
authmode = (1 << AUTHMODE_WPA) | (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_PSK);
}
mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
if (password.len > 0 && (password.len < 8 || password.len > 63)) {
mp_raise_ValueError(translate("WiFi password must be between 8 and 63 characters"));
}
} else {
authmode = 1;
}
common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, authmode, args[ARG_max_connections].u_int);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_start_ap_obj, 1, wifi_radio_start_ap);
//| def stop_ap(self) -> None:
//| """Stops the Access Point."""
//| ...
//|
STATIC mp_obj_t wifi_radio_stop_ap(mp_obj_t self) {
common_hal_wifi_radio_stop_ap(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_ap_obj, wifi_radio_stop_ap);
//| def connect(self,
//| ssid: Union[str | ReadableBuffer],
//| password: Union[str | ReadableBuffer] = "",
//| *,
//| channel: Optional[int] = 0,
//| bssid: Optional[Union[str | ReadableBuffer]] = "",
//| timeout: Optional[float] = None) -> None:
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
//| automatically once one connection succeeds.
//|
//| By default, this will scan all channels and connect to the access point (AP) with the
//| given ``ssid`` and greatest signal strength (rssi).
//|
//| If ``channel`` is given, the scan will begin with the given channel and connect to
//| the first AP with the given ``ssid``. This can speed up the connection time
//| significantly because a full scan doesn't occur.
//|
//| If ``bssid`` is given, the scan will start at the first channel or the one given and
//| connect to the AP with the given ``bssid`` and ``ssid``."""
//| ...
//|
STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_ssid, ARG_password, ARG_channel, ARG_bssid, ARG_timeout };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_float_t timeout = 0;
if (args[ARG_timeout].u_obj != mp_const_none) {
timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
}
mp_buffer_info_t ssid;
ssid.len = 0;
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);
if (ssid.len > 32) {
mp_raise_ValueError(translate("ssid can't be more than 32 bytes"));
}
mp_buffer_info_t password;
password.len = 0;
if (args[ARG_password].u_obj != MP_OBJ_NULL) {
mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
if (password.len > 0 && (password.len < 8 || password.len > 63)) {
mp_raise_ValueError(translate("WiFi password must be between 8 and 63 characters"));
}
}
#define MAC_ADDRESS_LENGTH 6
mp_buffer_info_t bssid;
bssid.len = 0;
// Should probably make sure bssid is just bytes and not something else too
if (args[ARG_bssid].u_obj != MP_OBJ_NULL) {
mp_get_buffer_raise(args[ARG_bssid].u_obj, &bssid, MP_BUFFER_READ);
if (bssid.len != MAC_ADDRESS_LENGTH) {
mp_raise_ValueError(translate("Invalid BSSID"));
}
}
wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout, bssid.buf, bssid.len);
if (error == WIFI_RADIO_ERROR_AUTH_FAIL) {
mp_raise_ConnectionError(translate("Authentication failure"));
} else if (error == WIFI_RADIO_ERROR_NO_AP_FOUND) {
mp_raise_ConnectionError(translate("No network with that ssid"));
} else if (error != WIFI_RADIO_ERROR_NONE) {
mp_raise_msg_varg(&mp_type_ConnectionError, translate("Unknown failure %d"), error);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect);
//| ipv4_gateway: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the station gateway when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_gateway(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_gateway(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_obj, wifi_radio_get_ipv4_gateway);
MP_PROPERTY_GETTER(wifi_radio_ipv4_gateway_obj,
(mp_obj_t)&wifi_radio_get_ipv4_gateway_obj);
//| ipv4_gateway_ap: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the access point gateway, when enabled. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_gateway_ap(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_gateway_ap(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_ap_obj, wifi_radio_get_ipv4_gateway_ap);
MP_PROPERTY_GETTER(wifi_radio_ipv4_gateway_ap_obj,
(mp_obj_t)&wifi_radio_get_ipv4_gateway_ap_obj);
//| ipv4_subnet: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the station subnet when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_subnet(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_subnet(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_obj, wifi_radio_get_ipv4_subnet);
MP_PROPERTY_GETTER(wifi_radio_ipv4_subnet_obj,
(mp_obj_t)&wifi_radio_get_ipv4_subnet_obj);
//| ipv4_subnet_ap: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the access point subnet, when enabled. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_subnet_ap(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_subnet_ap(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_ap_obj, wifi_radio_get_ipv4_subnet_ap);
MP_PROPERTY_GETTER(wifi_radio_ipv4_subnet_ap_obj,
(mp_obj_t)&wifi_radio_get_ipv4_subnet_ap_obj);
//| ipv4_address: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the station when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_address(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_address(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_address_obj, wifi_radio_get_ipv4_address);
MP_PROPERTY_GETTER(wifi_radio_ipv4_address_obj,
(mp_obj_t)&wifi_radio_get_ipv4_address_obj);
//| ipv4_address_ap: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the access point, when enabled. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_address_ap(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_address_ap(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_address_ap_obj, wifi_radio_get_ipv4_address_ap);
MP_PROPERTY_GETTER(wifi_radio_ipv4_address_ap_obj,
(mp_obj_t)&wifi_radio_get_ipv4_address_ap_obj);
//| ipv4_dns: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the DNS server in use when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_dns(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_dns(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_dns_obj, wifi_radio_get_ipv4_dns);
MP_PROPERTY_GETTER(wifi_radio_ipv4_dns_obj,
(mp_obj_t)&wifi_radio_get_ipv4_dns_obj);
//| ap_info: Optional[Network]
//| """Network object containing BSSID, SSID, authmode, channel, country and RSSI when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
return common_hal_wifi_radio_get_ap_info(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info);
MP_PROPERTY_GETTER(wifi_radio_ap_info_obj,
(mp_obj_t)&wifi_radio_get_ap_info_obj);
//| def ping(self, ip: ipaddress.IPv4Address, *, timeout: Optional[float] = 0.5) -> Optional[float]:
//| """Ping an IP to test connectivity. Returns echo time in seconds.
//| Returns None when it times out."""
//| ...
//|
STATIC mp_obj_t wifi_radio_ping(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_ip, ARG_timeout };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ip, MP_ARG_REQUIRED | MP_ARG_OBJ, },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_float_t timeout = 0.5;
if (args[ARG_timeout].u_obj != mp_const_none) {
timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
}
mp_int_t time_ms = common_hal_wifi_radio_ping(self, args[ARG_ip].u_obj, timeout);
if (time_ms == -1) {
return mp_const_none;
}
return mp_obj_new_float(time_ms / 1000.0);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_ping_obj, 1, wifi_radio_ping);
STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&wifi_radio_enabled_obj) },
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
{ MP_ROM_QSTR(MP_QSTR_mac_address), MP_ROM_PTR(&wifi_radio_mac_address_obj) },
{ MP_ROM_QSTR(MP_QSTR_mac_address_ap), MP_ROM_PTR(&wifi_radio_mac_address_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_start_station), MP_ROM_PTR(&wifi_radio_start_station_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_station), MP_ROM_PTR(&wifi_radio_stop_station_obj) },
{ MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
{ MP_ROM_QSTR(MP_QSTR_ap_info), MP_ROM_PTR(&wifi_radio_ap_info_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_gateway), MP_ROM_PTR(&wifi_radio_ipv4_gateway_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_gateway_ap), MP_ROM_PTR(&wifi_radio_ipv4_gateway_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_subnet), MP_ROM_PTR(&wifi_radio_ipv4_subnet_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_subnet_ap), MP_ROM_PTR(&wifi_radio_ipv4_subnet_ap_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_address), MP_ROM_PTR(&wifi_radio_ipv4_address_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_address_ap), MP_ROM_PTR(&wifi_radio_ipv4_address_ap_obj) },
// { MP_ROM_QSTR(MP_QSTR_access_point_active), MP_ROM_PTR(&wifi_radio_access_point_active_obj) },
// { MP_ROM_QSTR(MP_QSTR_start_access_point), MP_ROM_PTR(&wifi_radio_start_access_point_obj) },
{ MP_ROM_QSTR(MP_QSTR_ping), MP_ROM_PTR(&wifi_radio_ping_obj) },
};
STATIC MP_DEFINE_CONST_DICT(wifi_radio_locals_dict, wifi_radio_locals_dict_table);
const mp_obj_type_t wifi_radio_type = {
.base = { &mp_type_type },
.name = MP_QSTR_Radio,
.locals_dict = (mp_obj_t)&wifi_radio_locals_dict,
};
|