Skip to content

Commit a84551a

Browse files
authored
Merge pull request #8105 from dhalbert/authmode-doc
improve start_ap() doc; make "authmode" use consistent internally
2 parents dc13a80 + 540bf58 commit a84551a

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,22 @@ void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
206206
set_mode_station(self, false);
207207
}
208208

209-
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, uint32_t authmodes, uint8_t max_connections) {
209+
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, uint32_t authmode, uint8_t max_connections) {
210210
set_mode_ap(self, true);
211211

212-
uint8_t authmode = 0;
213-
switch (authmodes) {
212+
uint8_t esp_authmode = 0;
213+
switch (authmode) {
214214
case AUTHMODE_OPEN:
215-
authmode = WIFI_AUTH_OPEN;
215+
esp_authmode = WIFI_AUTH_OPEN;
216216
break;
217217
case AUTHMODE_WPA | AUTHMODE_PSK:
218-
authmode = WIFI_AUTH_WPA_PSK;
218+
esp_authmode = WIFI_AUTH_WPA_PSK;
219219
break;
220220
case AUTHMODE_WPA2 | AUTHMODE_PSK:
221-
authmode = WIFI_AUTH_WPA2_PSK;
221+
esp_authmode = WIFI_AUTH_WPA2_PSK;
222222
break;
223223
case AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_PSK:
224-
authmode = WIFI_AUTH_WPA_WPA2_PSK;
224+
esp_authmode = WIFI_AUTH_WPA_WPA2_PSK;
225225
break;
226226
default:
227227
mp_arg_error_invalid(MP_QSTR_authmode);
@@ -234,7 +234,7 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
234234
memcpy(&config->ap.password, password, password_len);
235235
config->ap.password[password_len] = 0;
236236
config->ap.channel = channel;
237-
config->ap.authmode = authmode;
237+
config->ap.authmode = esp_authmode;
238238

239239
mp_arg_validate_int_range(max_connections, 0, 10, MP_QSTR_max_connections);
240240

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
175175
bindings_cyw43_wifi_enforce_pm();
176176
}
177177

178-
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, uint32_t authmodes, uint8_t max_connections) {
178+
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, uint32_t authmode, uint8_t max_connections) {
179179
if (!common_hal_wifi_radio_get_enabled(self)) {
180180
mp_raise_RuntimeError(translate("Wifi is not enabled"));
181181
}

shared-bindings/wifi/Radio.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,24 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
316316
//| password: Union[str | ReadableBuffer] = b"",
317317
//| *,
318318
//| channel: int = 1,
319-
//| authmode: Optional[AuthMode] = None,
319+
//| authmode: Iterable[AuthMode] = (),
320320
//| max_connections: Optional[int] = 4,
321321
//| ) -> None:
322322
//| """Starts running an access point with the specified ssid and password.
323323
//|
324324
//| If ``channel`` is given, the access point will use that channel unless
325325
//| a station is already operating on a different channel.
326326
//|
327-
//| If ``authmode`` is not None, the access point will use that Authentication
328-
//| mode. If a non-empty password is given, ``authmode`` must not be ``OPEN``.
329-
//| If ``authmode`` is not given or is None,
330-
//| ``OPEN`` will be used when the password is the empty string,
331-
//| otherwise ``authmode`` will be ``WPA_WPA2_PSK``.
327+
//| If ``authmode`` is not None, the access point will use the given authentication modes.
328+
//| If a non-empty password is given, ``authmode`` must not include ``OPEN``.
329+
//| If ``authmode`` is not given or is an empty iterable,
330+
//| ``(wifi.AuthMode.OPEN,)`` will be used when the password is the empty string,
331+
//| otherwise ``authmode`` will be ``(wifi.AuthMode.WPA, wifi.AuthMode.WPA2, wifi.AuthMode.PSK)``.
332+
//|
333+
//| **Limitations:** On Espressif, ``authmode`` with a non-empty password must include
334+
//| `wifi.AuthMode.PSK`, and one or both of `wifi.AuthMode.WPA` and `wifi.AuthMode.WPA2`.
335+
//| On Pi Pico W, ``authmode`` is ignored; it is always ``(wifi.AuthMode.WPA2, wifi.AuthMode.PSK)`
336+
//| with a non-empty password, or ``(wifi.AuthMode.OPEN,)`` when no password is given.
332337
//|
333338
//| The length of ``password`` must be 8-63 characters if it is ASCII,
334339
//| or exactly 64 hexadecimal characters if it is the hex form of the 256-bit key.
@@ -342,22 +347,20 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
342347
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ },
343348
{ MP_QSTR_password, MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
344349
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
345-
{ MP_QSTR_authmode, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none } },
350+
{ MP_QSTR_authmode, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_tuple } },
346351
{ MP_QSTR_max_connections, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 4} },
347352
};
348353

349354
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
350355
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
351356
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
352357

353-
// 0 indicates mode wasn't given.
354-
uint32_t authmodes = 0;
355-
if (args[ARG_authmode].u_obj != mp_const_none) {
356-
mp_obj_iter_buf_t iter_buf;
357-
mp_obj_t item, iterable = mp_getiter(args[ARG_authmode].u_obj, &iter_buf);
358-
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
359-
authmodes |= cp_enum_value(&wifi_authmode_type, item, MP_QSTR_authmode);
360-
}
358+
// 0 indicates no modes were given. Otherwise authmode is the OR of bits signifying the modes.
359+
uint32_t authmode = 0;
360+
mp_obj_iter_buf_t iter_buf;
361+
mp_obj_t item, iterable = mp_getiter(args[ARG_authmode].u_obj, &iter_buf);
362+
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
363+
authmode |= cp_enum_value(&wifi_authmode_type, item, MP_QSTR_authmode);
361364
}
362365

363366
mp_buffer_info_t ssid;
@@ -366,28 +369,28 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
366369

367370
mp_buffer_info_t password;
368371
mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
369-
if (authmodes == 0) {
372+
if (authmode == 0) {
370373
if (password.len == 0) {
371-
authmodes = AUTHMODE_OPEN;
374+
authmode = AUTHMODE_OPEN;
372375
} else {
373-
authmodes = AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_PSK;
376+
authmode = AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_PSK;
374377
}
375378
}
376379

377380
mp_int_t channel = mp_arg_validate_int_range(args[ARG_channel].u_int, 1, 13, MP_QSTR_channel);
378381

379-
if (authmodes == AUTHMODE_OPEN && password.len > 0) {
382+
if (authmode == AUTHMODE_OPEN && password.len > 0) {
380383
mp_raise_ValueError(translate("AuthMode.OPEN is not used with password"));
381384
}
382385

383-
if (authmodes != AUTHMODE_OPEN) {
386+
if (authmode != AUTHMODE_OPEN) {
384387
mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
385388
if (password.len == 64) {
386389
validate_hex_password(password.buf, password.len);
387390
}
388391
}
389392

390-
common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, channel, authmodes, args[ARG_max_connections].u_int);
393+
common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, channel, authmode, args[ARG_max_connections].u_int);
391394
return mp_const_none;
392395
}
393396
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_start_ap_obj, 1, wifi_radio_start_ap);

shared-bindings/wifi/Radio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self)
9393
extern void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self);
9494
extern void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self);
9595

96-
extern 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, uint32_t authmodes, uint8_t max_connections);
96+
extern 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, uint32_t authmode, uint8_t max_connections);
9797
extern void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self);
9898
extern bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self);
9999

0 commit comments

Comments
 (0)