@@ -282,21 +282,22 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
282
282
//| def start_ap(
283
283
//| self,
284
284
//| ssid: Union[str | ReadableBuffer],
285
- //| password: Union[str | ReadableBuffer] = "",
285
+ //| password: Union[str | ReadableBuffer] = b "",
286
286
//| *,
287
- //| channel: Optional[ int] = 1,
288
- //| authmode: Optional[AuthMode],
287
+ //| channel: int = 1,
288
+ //| authmode: Optional[AuthMode] = None ,
289
289
//| max_connections: Optional[int] = 4
290
290
//| ) -> None:
291
291
//| """Starts an Access Point with the specified ssid and password.
292
292
//|
293
293
//| If ``channel`` is given, the access point will use that channel unless
294
294
//| a station is already operating on a different channel.
295
295
//|
296
- //| If ``authmode`` is given, the access point will use that Authentication
297
- //| mode. If a password is given, ``authmode`` must not be ``OPEN``.
298
- //| If ``authmode`` isn't given, ``OPEN`` will be used when password isn't provided,
299
- //| otherwise ``WPA_WPA2_PSK``.
296
+ //| If ``authmode`` is not None, the access point will use that Authentication
297
+ //| mode. If a non-empty password is given, ``authmode`` must not be ``OPEN``.
298
+ //| If ``authmode`` is not given or is None,
299
+ //| ``OPEN`` will be used when the password is the empty string,
300
+ //| otherwise ``authmode`` will be ``WPA_WPA2_PSK``.
300
301
//|
301
302
//| If ``max_connections`` is given, the access point will allow up to
302
303
//| that number of stations to connect."""
@@ -305,22 +306,23 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
305
306
enum { ARG_ssid , ARG_password , ARG_channel , ARG_authmode , ARG_max_connections };
306
307
static const mp_arg_t allowed_args [] = {
307
308
{ MP_QSTR_ssid , MP_ARG_REQUIRED | MP_ARG_OBJ },
308
- { MP_QSTR_password , MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
309
+ { MP_QSTR_password , MP_ARG_OBJ , {.u_obj = mp_const_empty_bytes } },
309
310
{ MP_QSTR_channel , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 1 } },
310
- { MP_QSTR_authmode , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
311
+ { MP_QSTR_authmode , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
311
312
{ MP_QSTR_max_connections , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 4 } },
312
313
};
313
314
314
315
wifi_radio_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
315
316
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
316
317
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
317
318
318
- uint8_t authmode = 0 ;
319
- if (args [ARG_authmode ].u_obj != MP_OBJ_NULL ) {
319
+ // 0 indicates mode wasn't given.
320
+ uint32_t authmodes = 0 ;
321
+ if (args [ARG_authmode ].u_obj != mp_const_none ) {
320
322
mp_obj_iter_buf_t iter_buf ;
321
323
mp_obj_t item , iterable = mp_getiter (args [ARG_authmode ].u_obj , & iter_buf );
322
324
while ((item = mp_iternext (iterable )) != MP_OBJ_STOP_ITERATION ) {
323
- authmode |= ( 1 << ( wifi_authmode_t ) cp_enum_value (& wifi_authmode_type , item ) );
325
+ authmodes |= cp_enum_value (& wifi_authmode_type , item );
324
326
}
325
327
}
326
328
@@ -329,20 +331,24 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
329
331
mp_arg_validate_length_range (ssid .len , 1 , 32 , MP_QSTR_ssid );
330
332
331
333
mp_buffer_info_t password ;
332
- password . len = 0 ;
333
- if (args [ ARG_password ]. u_obj != MP_OBJ_NULL ) {
334
- if (authmode == 1 ) {
335
- mp_raise_ValueError ( translate ( "AuthMode.OPEN is not used with password" )) ;
336
- } else if ( authmode == 0 ) {
337
- authmode = ( 1 << AUTHMODE_WPA ) | ( 1 << AUTHMODE_WPA2 ) | ( 1 << AUTHMODE_PSK ) ;
334
+ mp_get_buffer_raise ( args [ ARG_password ]. u_obj , & password , MP_BUFFER_READ ) ;
335
+ if (authmodes == 0 ) {
336
+ if (password . len == 0 ) {
337
+ authmodes = AUTHMODE_OPEN ;
338
+ } else {
339
+ authmodes = AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_PSK ;
338
340
}
339
- mp_get_buffer_raise (args [ARG_password ].u_obj , & password , MP_BUFFER_READ );
341
+ }
342
+
343
+ if (authmodes == AUTHMODE_OPEN && password .len > 0 ) {
344
+ mp_raise_ValueError (translate ("AuthMode.OPEN is not used with password" ));
345
+ }
346
+
347
+ if (authmodes != AUTHMODE_OPEN ) {
340
348
mp_arg_validate_length_range (password .len , 8 , 63 , MP_QSTR_password );
341
- } else {
342
- authmode = 1 ;
343
349
}
344
350
345
- 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 );
351
+ common_hal_wifi_radio_start_ap (self , ssid .buf , ssid .len , password .buf , password .len , args [ARG_channel ].u_int , authmodes , args [ARG_max_connections ].u_int );
346
352
return mp_const_none ;
347
353
}
348
354
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (wifi_radio_start_ap_obj , 1 , wifi_radio_start_ap );
@@ -359,10 +365,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_ap_obj, wifi_radio_stop_ap);
359
365
//| def connect(
360
366
//| self,
361
367
//| ssid: Union[str | ReadableBuffer],
362
- //| password: Union[str | ReadableBuffer] = "",
368
+ //| password: Union[str | ReadableBuffer] = b "",
363
369
//| *,
364
- //| channel: Optional[ int] = 0,
365
- //| bssid: Optional[Union[str | ReadableBuffer]] = "" ,
370
+ //| channel: int = 0,
371
+ //| bssid: Optional[Union[str | ReadableBuffer]] = None ,
366
372
//| timeout: Optional[float] = None
367
373
//| ) -> None:
368
374
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
@@ -371,20 +377,20 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_ap_obj, wifi_radio_stop_ap);
371
377
//| By default, this will scan all channels and connect to the access point (AP) with the
372
378
//| given ``ssid`` and greatest signal strength (rssi).
373
379
//|
374
- //| If ``channel`` is given , the scan will begin with the given channel and connect to
380
+ //| If ``channel`` is non-zero , the scan will begin with the given channel and connect to
375
381
//| the first AP with the given ``ssid``. This can speed up the connection time
376
382
//| significantly because a full scan doesn't occur.
377
383
//|
378
- //| If ``bssid`` is given, the scan will start at the first channel or the one given and
384
+ //| If ``bssid`` is given and not None , the scan will start at the first channel or the one given and
379
385
//| connect to the AP with the given ``bssid`` and ``ssid``."""
380
386
//| ...
381
387
STATIC mp_obj_t wifi_radio_connect (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
382
388
enum { ARG_ssid , ARG_password , ARG_channel , ARG_bssid , ARG_timeout };
383
389
static const mp_arg_t allowed_args [] = {
384
390
{ MP_QSTR_ssid , MP_ARG_REQUIRED | MP_ARG_OBJ },
385
- { MP_QSTR_password , MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
391
+ { MP_QSTR_password , MP_ARG_OBJ , {.u_obj = mp_const_empty_bytes } },
386
392
{ MP_QSTR_channel , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
387
- { MP_QSTR_bssid , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
393
+ { MP_QSTR_bssid , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
388
394
{ MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
389
395
};
390
396
@@ -404,17 +410,19 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
404
410
405
411
mp_buffer_info_t password ;
406
412
password .len = 0 ;
407
- if (args [ARG_password ].u_obj != MP_OBJ_NULL ) {
413
+ if (args [ARG_password ].u_obj != mp_const_none ) {
408
414
mp_get_buffer_raise (args [ARG_password ].u_obj , & password , MP_BUFFER_READ );
409
- mp_arg_validate_length_range (password .len , 8 , 63 , MP_QSTR_password );
415
+ if (password .len != 0 ) {
416
+ mp_arg_validate_length_range (password .len , 8 , 63 , MP_QSTR_password );
417
+ }
410
418
}
411
419
412
420
#define MAC_ADDRESS_LENGTH 6
413
421
414
422
mp_buffer_info_t bssid ;
415
423
bssid .len = 0 ;
416
424
// Should probably make sure bssid is just bytes and not something else too
417
- if (args [ARG_bssid ].u_obj != MP_OBJ_NULL ) {
425
+ if (args [ARG_bssid ].u_obj != mp_const_none ) {
418
426
mp_get_buffer_raise (args [ARG_bssid ].u_obj , & bssid , MP_BUFFER_READ );
419
427
if (bssid .len != MAC_ADDRESS_LENGTH ) {
420
428
mp_raise_ValueError (translate ("Invalid BSSID" ));
0 commit comments