Skip to content

Commit e54a4f1

Browse files
author
Daniel Campora
committed
cc3200: Improve support for WEP security.
Key is always entered as a string, but if security is WEP, the key is converted automatically to hex before connecting or configuring the device as an AP.
1 parent d430191 commit e54a4f1

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

cc3200/mods/modwlan.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ STATIC void wlan_reenable (SlWlanMode_t mode);
196196
STATIC void wlan_servers_start (void);
197197
STATIC void wlan_servers_stop (void);
198198
STATIC void wlan_get_sl_mac (void);
199+
STATIC void wlan_wep_key_unhexlify(const char *key, char *key_out);
199200
STATIC modwlan_Status_t wlan_do_connect (const char* ssid, uint32_t ssid_len, const char* bssid, uint8_t sec,
200201
const char* key, uint32_t key_len, uint32_t timeout);
201202
STATIC void wlan_lpds_callback_enable (mp_obj_t self_in);
@@ -479,6 +480,12 @@ void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t se
479480
wlan_obj.ssid[ssid_len] = '\0';
480481
ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, ssid_len, (unsigned char *)wlan_obj.ssid));
481482
ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, sizeof(uint8_t), &sec));
483+
if (sec == SL_SEC_TYPE_WEP) {
484+
_u8 wep_key[32];
485+
wlan_wep_key_unhexlify(key, (char *)&wep_key);
486+
key = (const char *)&wep_key;
487+
key_len /= 2;
488+
}
482489
ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, key_len, (unsigned char *)key));
483490
_u8* country = (_u8*)"EU";
484491
ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, country));
@@ -651,6 +658,25 @@ STATIC void wlan_get_sl_mac (void) {
651658
sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macAddrLen, wlan_obj.mac);
652659
}
653660

661+
STATIC void wlan_wep_key_unhexlify(const char *key, char *key_out) {
662+
int len = strlen(key);
663+
byte hex_byte = 0;
664+
for (mp_uint_t i = len; i--;) {
665+
byte hex_ch = *key++;
666+
if (unichar_isxdigit(hex_ch)) {
667+
hex_byte += unichar_xdigit_value(hex_ch);
668+
} else {
669+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, mpexception_value_invalid_arguments));
670+
}
671+
if (i & 1) {
672+
hex_byte <<= 4;
673+
} else {
674+
*key_out++ = hex_byte;
675+
hex_byte = 0;
676+
}
677+
}
678+
}
679+
654680
/// \method iwconfig(*, mode, ssid, security, key, channel, antenna)
655681
///
656682
/// Initialise the WLAN engine with the given parameters:
@@ -707,8 +733,9 @@ STATIC mp_obj_t wlan_iwconfig(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
707733
if (args[3].u_obj != MP_OBJ_NULL) {
708734
// get the key
709735
mp_uint_t key_len;
710-
const char *key = mp_obj_str_get_data(args[3].u_obj, &key_len);
711-
if (key_len < 5 || key_len > 64) {
736+
const char *key;
737+
key = mp_obj_str_get_data(args[3].u_obj, &key_len);
738+
if ((wlan_obj.security == SL_SEC_TYPE_WEP && (key_len < 10 || key_len > 58)) || key_len < 8 || key_len > 64) {
712739
goto arg_error;
713740
}
714741
memcpy (wlan_obj.key, key, key_len);
@@ -806,8 +833,6 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
806833
}
807834

808835
/// \method connect(ssid, *, security=OPEN, key=None, bssid=None, timeout=5000)
809-
// if security is WPA/WPA2, the key must be a string
810-
/// if security is WEP, the key must be binary
811836
STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
812837
STATIC const mp_arg_t allowed_args[] = {
813838
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ, },
@@ -836,17 +861,16 @@ STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
836861
// get key and its len
837862
mp_uint_t key_len = 0;
838863
const char *key = NULL;
839-
mp_buffer_info_t wepkey;
840864
mp_obj_t key_o = args[2].u_obj;
841865
if (key_o != mp_const_none) {
842-
// wep key must be given as raw bytes
843-
if (sec == SL_SEC_TYPE_WEP) {
844-
mp_get_buffer_raise(key_o, &wepkey, MP_BUFFER_READ);
845-
key = wepkey.buf;
846-
key_len = wepkey.len;
847-
} else {
848-
key = mp_obj_str_get_data(key_o, &key_len);
849-
}
866+
key = mp_obj_str_get_data(key_o, &key_len);
867+
}
868+
869+
if (sec == SL_SEC_TYPE_WEP) {
870+
_u8 wep_key[32];
871+
wlan_wep_key_unhexlify(key, (char *)&wep_key);
872+
key = (const char *)&wep_key;
873+
key_len /= 2;
850874
}
851875

852876
// get bssid

docs/library/network.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ class WLAN
272272
- ``security`` can be ``WLAN.OPEN``, ``WLAN.WEP``, ``WLAN.WPA`` or ``WLAN.WPA2``.
273273
Only needed when mode is ``WLAN.AP``.
274274
- ``key`` is a string with the network password. Not needed when mode is ``WLAN.STA``
275-
or security is ``WLAN.OPEN``.
275+
or security is ``WLAN.OPEN``. If ``security`` is ``WLAN.WEP`` the key must be a
276+
string representing hexadecimal values (e.g. 'ABC1DE45BF').
276277
- ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``.
277278
- ``antenna`` selects between the internal and the external antenna. Can be either
278279
``WLAN.INTERNAL`` or ``WLAN.EXTERNAL``.
@@ -295,6 +296,8 @@ class WLAN
295296
Connect to a wifi access point using the given SSID, and other security
296297
parameters.
297298

299+
- ``key`` is always a string, but if ``security`` is ``WLAN.WEP`` the key must be a string
300+
representing hexadecimal values (e.g. 'ABC1DE45BF').
298301
- ``bssid`` is the MAC address of the AP to connect to. Useful when there are several APs
299302
with the same ssid.
300303
- ``timeout`` is the maximum time in milliseconds to wait for the connection to succeed.

0 commit comments

Comments
 (0)