Skip to content

Commit f20a531

Browse files
committed
add authmode to start_ap()
1 parent 747d72f commit f20a531

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
164164
set_mode_station(self, false);
165165
}
166166

167-
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) {
167+
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) {
168168
set_mode_ap(self, true);
169169

170170
wifi_config_t *config = &self->ap_config;
@@ -173,7 +173,7 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
173173
memcpy(&config->ap.password, password, password_len);
174174
config->ap.password[password_len] = 0;
175175
config->ap.channel = channel;
176-
config->ap.authmode = WIFI_AUTH_WPA2_PSK;
176+
config->ap.authmode = authmode;
177177
config->ap.max_connection = 4;
178178
esp_wifi_set_config(WIFI_IF_AP, config);
179179
}

shared-bindings/wifi/Radio.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,34 @@ STATIC mp_obj_t wifi_radio_stop_station(mp_obj_t self) {
190190
}
191191
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
192192

193+
//| OPEN: int
194+
//| WPA_PSK: int
195+
//| WPA2_PSK: int
196+
//| WPA_WPA2_PSK: int
197+
//|
193198
//| def start_ap(self,
194199
//| ssid: ReadableBuffer,
195200
//| password: ReadableBuffer = b"",
196201
//| *,
197-
//| channel: Optional[int] = 1) -> None:
198-
//| """Starts an Access Point with the specified ssid and password.
202+
//| channel: Optional[int] = 1,
203+
//| authmode: Optional[int] = WPA_WPA2_PSK) -> None:
204+
//| """Starts an Access Point with the specified ssid and password
205+
//| If an empty.
199206
//|
200207
//| If ``channel`` is given, the access point will use that channel unless
201-
//| wifi is already operating on a different channel due to an active station."""
208+
//| a station is already operating on a different channel.
209+
//|
210+
//| If ``authmode`` is given, the access point will use that Authentication
211+
//| mode. If a password is given, ``authmode`` must not be ``OPEN``."""
202212
//| ...
203213
//|
204214
STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
205-
enum { ARG_ssid, ARG_password, ARG_channel };
215+
enum { ARG_ssid, ARG_password, ARG_channel, ARG_authmode };
206216
static const mp_arg_t allowed_args[] = {
207217
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ },
208218
{ MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
209219
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
220+
{ MP_QSTR_authmode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = WIFI_RADIO_AUTH_WPA_WPA2_PSK} },
210221
};
211222

212223
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
@@ -223,9 +234,12 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
223234
if (password.len > 0 && (password.len < 8 || password.len > 63)) {
224235
mp_raise_ValueError(translate("WiFi password must be between 8 and 63 characters"));
225236
}
237+
if (args[ARG_authmode].u_int == WIFI_RADIO_AUTH_OPEN) {
238+
mp_raise_ValueError(translate("WiFi password is not used with OPEN authentication"));
239+
}
226240
}
227241

228-
common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int);
242+
common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, args[ARG_authmode].u_int);
229243
return mp_const_none;
230244
}
231245
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_start_ap_obj, 1, wifi_radio_start_ap);
@@ -455,8 +469,13 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
455469

456470
{ MP_ROM_QSTR(MP_QSTR_start_station), MP_ROM_PTR(&wifi_radio_start_station_obj) },
457471
{ MP_ROM_QSTR(MP_QSTR_stop_station), MP_ROM_PTR(&wifi_radio_stop_station_obj) },
458-
{ MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) },
459472
{ MP_ROM_QSTR(MP_QSTR_stop_ap), MP_ROM_PTR(&wifi_radio_stop_ap_obj) },
473+
{ MP_ROM_QSTR(MP_QSTR_start_ap), MP_ROM_PTR(&wifi_radio_start_ap_obj) },
474+
{ MP_ROM_QSTR(MP_QSTR_OPEN), MP_ROM_INT(WIFI_RADIO_AUTH_OPEN) },
475+
{ MP_ROM_QSTR(MP_QSTR_WPA_PSK), MP_ROM_INT(WIFI_RADIO_AUTH_WPA_PSK) },
476+
{ MP_ROM_QSTR(MP_QSTR_WPA2_PSK), MP_ROM_INT(WIFI_RADIO_AUTH_WPA2_PSK) },
477+
{ MP_ROM_QSTR(MP_QSTR_WPA_WPA2_PSK), MP_ROM_INT(WIFI_RADIO_AUTH_WPA_WPA2_PSK) },
478+
460479
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
461480
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
462481

shared-bindings/wifi/Radio.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ typedef enum {
7171
WIFI_RADIO_ERROR_AP_TSF_RESET = 206,
7272
} wifi_radio_error_t;
7373

74+
typedef enum {
75+
WIFI_RADIO_AUTH_OPEN = 0, // OK
76+
WIFI_RADIO_AUTH_WEP, // not supported in SoftAP
77+
WIFI_RADIO_AUTH_WPA_PSK, // OK
78+
WIFI_RADIO_AUTH_WPA2_PSK, // OK
79+
WIFI_RADIO_AUTH_WPA_WPA2_PSK, // OK
80+
WIFI_RADIO_AUTH_WPA2_ENTERPRISE, // not currently supported
81+
WIFI_RADIO_AUTH_WPA3_PSK, // not currently supported
82+
WIFI_RADIO_AUTH_WPA2_WPA3_PSK, // not currently supported
83+
WIFI_RADIO_AUTH_MAX, // not currently supported
84+
} wifi_radio_authmode_t;
85+
7486
extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self);
7587
extern void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled);
7688

@@ -85,7 +97,7 @@ extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self)
8597

8698
extern void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self);
8799
extern void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self);
88-
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);
100+
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, uint8_t authmode);
89101
extern void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self);
90102

91103
extern 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);

0 commit comments

Comments
 (0)