Skip to content

Commit

Permalink
extmod/network_ninaw10: Set the proper security mode if none provided.
Browse files Browse the repository at this point in the history
If no security mode is provided, use WPA for station and WEP for AP.  Note
only WEP is supported in AP mode.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
  • Loading branch information
iabdalkader authored and dpgeorge committed Mar 14, 2024
1 parent 8b4a21c commit 2b6f81f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions extmod/network_ninaw10.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static mp_obj_t network_ninaw10_connect(mp_uint_t n_args, const mp_obj_t *pos_ar
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_key, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = NINA_SEC_WPA_PSK} },
{ MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
};

Expand All @@ -276,15 +276,21 @@ static mp_obj_t network_ninaw10_connect(mp_uint_t n_args, const mp_obj_t *pos_ar
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SSID can't be empty!"));
}

// get key and sec
// get encryption key
const char *key = NULL;
mp_uint_t security = NINA_SEC_OPEN;

if (args[ARG_key].u_obj != mp_const_none) {
key = mp_obj_str_get_str(args[ARG_key].u_obj);
security = args[ARG_security].u_int;
}

// get security mode
mp_uint_t security = args[ARG_security].u_int;
if (security == -1 && self->itf == MOD_NETWORK_STA_IF) {
security = NINA_SEC_WPA_PSK;
} else if (security == -1 && self->itf == MOD_NETWORK_AP_IF) {
security = NINA_SEC_WEP;
}

// Ensure that the key is not empty if a security mode is used.
if (security != NINA_SEC_OPEN && strlen(key) == 0) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Key can't be empty!"));
}
Expand Down Expand Up @@ -316,7 +322,7 @@ static mp_obj_t network_ninaw10_connect(mp_uint_t n_args, const mp_obj_t *pos_ar
mp_uint_t channel = args[ARG_channel].u_int;

if (security != NINA_SEC_OPEN && security != NINA_SEC_WEP) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("AP mode supports WEP security only."));
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("AP mode only supports WEP or OPEN security modes"));
}

// Initialize WiFi in AP mode.
Expand Down

0 comments on commit 2b6f81f

Please sign in to comment.