diff options
Diffstat (limited to 'cc3200/mods/modwlan.c')
| -rw-r--r-- | cc3200/mods/modwlan.c | 880 |
1 files changed, 501 insertions, 379 deletions
diff --git a/cc3200/mods/modwlan.c b/cc3200/mods/modwlan.c index b7ff7b594..035527593 100644 --- a/cc3200/mods/modwlan.c +++ b/cc3200/mods/modwlan.c @@ -114,6 +114,7 @@ typedef enum{ #define MODWLAN_MAX_NETWORKS 20 #define MODWLAN_SCAN_PERIOD_S 3600 // 1 hour #define MODWLAN_WAIT_FOR_SCAN_MS 1050 +#define MODWLAN_CONNECTION_WAIT_MS 2 #define ASSERT_ON_ERROR(x) ASSERT((x) >= 0) @@ -144,7 +145,7 @@ STATIC wlan_obj_t wlan_obj = { .mode = -1, .status = 0, .ip = 0, - .security = MICROPY_PORT_WLAN_AP_SECURITY, + .auth = MICROPY_PORT_WLAN_AP_SECURITY, .channel = MICROPY_PORT_WLAN_AP_CHANNEL, .ssid = MICROPY_PORT_WLAN_AP_SSID, .key = MICROPY_PORT_WLAN_AP_KEY, @@ -170,10 +171,24 @@ STATIC void wlan_clear_data (void); STATIC void wlan_reenable (SlWlanMode_t mode); STATIC void wlan_servers_start (void); STATIC void wlan_servers_stop (void); -STATIC void wlan_get_sl_mac (void); -STATIC void wlan_wep_key_unhexlify(const char *key, char *key_out); +STATIC void wlan_reset (void); +STATIC void wlan_validate_mode (uint mode); +STATIC void wlan_set_mode (uint mode); +STATIC void wlan_validate_ssid_len (uint32_t len); +STATIC void wlan_set_ssid (const char *ssid, uint8_t len, bool add_mac); +STATIC void wlan_validate_security (uint8_t auth, const char *key, uint8_t len); +STATIC void wlan_set_security (uint8_t auth, const char *key, uint8_t len); +STATIC void wlan_validate_channel (uint8_t channel); +STATIC void wlan_set_channel (uint8_t channel); +#if MICROPY_HW_ANTENNA_DIVERSITY +STATIC void wlan_validate_antenna (uint8_t antenna); +STATIC void wlan_set_antenna (uint8_t antenna); +#endif +STATIC void wlan_sl_disconnect (void); STATIC modwlan_Status_t wlan_do_connect (const char* ssid, uint32_t ssid_len, const char* bssid, uint8_t sec, - const char* key, uint32_t key_len, uint32_t timeout); + const char* key, uint32_t key_len, int32_t timeout); +STATIC void wlan_get_sl_mac (void); +STATIC void wlan_wep_key_unhexlify (const char *key, char *key_out); STATIC void wlan_lpds_irq_enable (mp_obj_t self_in); STATIC void wlan_lpds_irq_disable (mp_obj_t self_in); STATIC bool wlan_scan_result_is_unique (const mp_obj_list_t *nets, _u8 *bssid); @@ -394,8 +409,8 @@ void wlan_first_start (void) { wlan_get_sl_mac(); } -void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t sec, - const char *key, uint8_t key_len, uint8_t channel, bool append_mac) { +void wlan_sl_init (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t auth, const char *key, uint8_t key_len, + uint8_t channel, uint8_t antenna, bool add_mac) { // stop the servers wlan_servers_stop(); @@ -403,16 +418,8 @@ void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t se // do a basic start wlan_first_start(); - // Device in station-mode. Disconnect previous connection if any - // The function returns 0 if 'Disconnected done', negative number if already - // disconnected Wait for 'disconnection' event if 0 is returned, Ignore - // other return-codes - if (0 == sl_WlanDisconnect()) { - while (IS_CONNECTED (wlan_obj.status)) { - HAL_Delay (5); - wlan_update(); - } - } + // close any active connections + wlan_sl_disconnect(); // Remove all profiles ASSERT_ON_ERROR(sl_WlanProfileDel(0xFF)); @@ -436,39 +443,32 @@ void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t se memset(RxFilterIdMask.FilterIdMask, 0xFF, 8); ASSERT_ON_ERROR(sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t))); + // switch to the requested mode + wlan_set_mode(mode); + +#if MICROPY_HW_ANTENNA_DIVERSITY + // set the antenna type + wlan_set_antenna (antenna); +#endif + // Set Tx power level for station or AP mode // Number between 0-15, as dB offset from max power - 0 will set max power uint8_t ucPower = 0; if (mode == ROLE_AP) { - // switch to AP mode - ASSERT_ON_ERROR(sl_WlanSetMode(mode)); - ASSERT (ssid != NULL && key != NULL); ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_AP_TX_POWER, sizeof(ucPower), (unsigned char *)&ucPower)); - memcpy(wlan_obj.ssid, (unsigned char *)ssid, ssid_len); - // append the last 2 bytes of the MAC address, since the use of this functionality is under our controll - // we can assume that the lenght of the ssid is less than (32 - 5) - if (append_mac) { - snprintf((char *)&wlan_obj.ssid[ssid_len], sizeof(wlan_obj.ssid) - ssid_len, "-%02x%02x", wlan_obj.mac[4], wlan_obj.mac[5]); - ssid_len += 5; - } - wlan_obj.ssid[ssid_len] = '\0'; - ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, ssid_len, (unsigned char *)wlan_obj.ssid)); - ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, sizeof(uint8_t), &sec)); - if (sec == SL_SEC_TYPE_WEP) { - _u8 wep_key[32]; - wlan_wep_key_unhexlify(key, (char *)&wep_key); - key = (const char *)&wep_key; - key_len /= 2; - } - ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, key_len, (unsigned char *)key)); - _u8* country = (_u8*)"EU"; - ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, country)); - ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, (_u8 *)&channel)); - // stop and start again + // stop and start again (we need to be in AP mode in order to continue) wlan_reenable(mode); - ASSERT (wlan_obj.mode == mode); + + // configure all parameters + wlan_set_ssid (ssid, ssid_len, add_mac); + wlan_set_security (auth, key, key_len); + wlan_set_channel (channel); + + // set the country + _u8* country = (_u8*)"EU"; + ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, country)); SlNetCfgIpV4Args_t ipV4; ipV4.ipV4 = (_u32)SL_IPV4_VAL(192,168,1,1); // _u32 IP address @@ -478,9 +478,6 @@ void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t se ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4)); - // stop and start again - wlan_reenable(mode); - SlNetAppDhcpServerBasicOpt_t dhcpParams; dhcpParams.lease_time = 4096; // lease time (in seconds) of the IP Address dhcpParams.ipv4_addr_start = SL_IPV4_VAL(192,168,1,2); // first IP Address for allocation. @@ -492,19 +489,13 @@ void wlan_sl_enable (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t se // stop and start again wlan_reenable(mode); - - // save the security type - wlan_obj.security = sec; - } - // STA and P2P modes - else { + } else { // STA and P2P modes ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, sizeof(ucPower), (unsigned char *)&ucPower)); - ASSERT_ON_ERROR(sl_WlanSetMode(mode)); // stop and start again wlan_reenable(mode); // set connection policy to Auto + Fast (tries to connect to the last connected AP) - ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1, 1, 0, 0, 0), NULL, 0)); + ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 1, 0, 0, 0), NULL, 0)); } // set current time and date (needed to validate certificates) @@ -528,12 +519,6 @@ void wlan_stop (uint32_t timeout) { wlan_obj.mode = -1; } -void wlan_start (void) { - wlan_obj.mode = sl_Start(0, 0, 0); - sl_LockObjUnlock (&wlan_LockObj); - wlan_servers_start(); -} - void wlan_get_mac (uint8_t *macAddress) { if (macAddress) { memcpy (macAddress, wlan_obj.mac, SL_MAC_ADDR_LEN); @@ -604,20 +589,133 @@ STATIC void wlan_servers_stop (void) { #endif } +STATIC void wlan_reset (void) { + wlan_servers_stop(); + wlan_reenable (wlan_obj.mode); + wlan_servers_start(); +} + +STATIC void wlan_validate_mode (uint mode) { + if (mode != ROLE_STA && mode != ROLE_AP) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } +} + +STATIC void wlan_set_mode (uint mode) { + wlan_obj.mode = mode; + ASSERT_ON_ERROR(sl_WlanSetMode(mode)); +} + +STATIC void wlan_validate_ssid_len (uint32_t len) { + if (len > MODWLAN_SSID_LEN_MAX) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } +} + +STATIC void wlan_set_ssid (const char *ssid, uint8_t len, bool add_mac) { + if (ssid != NULL) { + // save the ssid + memcpy(&wlan_obj.ssid, ssid, len); + // append the last 2 bytes of the MAC address, since the use of this functionality is under our control + // we can assume that the lenght of the ssid is less than (32 - 5) + if (add_mac) { + snprintf((char *)&wlan_obj.ssid[len], sizeof(wlan_obj.ssid) - len, "-%02x%02x", wlan_obj.mac[4], wlan_obj.mac[5]); + len += 5; + } + wlan_obj.ssid[len] = '\0'; + ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, len, (unsigned char *)wlan_obj.ssid)); + } +} + +STATIC void wlan_validate_security (uint8_t auth, const char *key, uint8_t len) { + if (auth != SL_SEC_TYPE_WEP && auth != SL_SEC_TYPE_WPA_WPA2) { + goto invalid_args; + } + if (auth == SL_SEC_TYPE_WEP) { + for (mp_uint_t i = strlen(key); i > 0; i--) { + if (!unichar_isxdigit(*key++)) { + goto invalid_args; + } + } + } + return; + +invalid_args: + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); +} + +STATIC void wlan_set_security (uint8_t auth, const char *key, uint8_t len) { + wlan_obj.auth = auth; + ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, sizeof(uint8_t), &auth)); + if (key != NULL) { + memcpy(&wlan_obj.key, key, len); + wlan_obj.key[len] = '\0'; + if (auth == SL_SEC_TYPE_WEP) { + _u8 wep_key[32]; + wlan_wep_key_unhexlify(key, (char *)&wep_key); + key = (const char *)&wep_key; + len /= 2; + } + ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, len, (unsigned char *)key)); + } else { + wlan_obj.key[0] = '\0'; + } +} + +STATIC void wlan_validate_channel (uint8_t channel) { + if (channel < 1 || channel > 11) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } +} + +STATIC void wlan_set_channel (uint8_t channel) { + wlan_obj.channel = channel; + ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, &channel)); +} + +#if MICROPY_HW_ANTENNA_DIVERSITY +STATIC void wlan_validate_antenna (uint8_t antenna) { + if (antenna != ANTENNA_TYPE_INTERNAL && antenna != ANTENNA_TYPE_EXTERNAL) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } +} + +STATIC void wlan_set_antenna (uint8_t antenna) { + wlan_obj.antenna = antenna; + antenna_select(antenna); +} +#endif + +STATIC void wlan_sl_disconnect (void) { + // Device in station-mode. Disconnect previous connection if any + // The function returns 0 if 'Disconnected done', negative number if already + // disconnected Wait for 'disconnection' event if 0 is returned, Ignore + // other return-codes + if (0 == sl_WlanDisconnect()) { + while (IS_CONNECTED(wlan_obj.status)) { + HAL_Delay(MODWLAN_CONNECTION_WAIT_MS); + wlan_update(); + } + } +} + STATIC modwlan_Status_t wlan_do_connect (const char* ssid, uint32_t ssid_len, const char* bssid, uint8_t sec, - const char* key, uint32_t key_len, uint32_t timeout) { + const char* key, uint32_t key_len, int32_t timeout) { SlSecParams_t secParams; secParams.Key = (_i8*)key; secParams.KeyLen = ((key != NULL) ? key_len : 0); secParams.Type = sec; - if (0 == sl_WlanConnect((_i8*)ssid, ssid_len, (_u8*)bssid, &secParams, NULL)) { + // first close any active connections + wlan_sl_disconnect(); + + if (!sl_WlanConnect((_i8*)ssid, ssid_len, (_u8*)bssid, &secParams, NULL)) { // wait for the WLAN Event uint32_t waitForConnectionMs = 0; - while (!IS_CONNECTED(wlan_obj.status)) { - HAL_Delay (5); - waitForConnectionMs += 5; - if (waitForConnectionMs > timeout) { + while (timeout && !IS_CONNECTED(wlan_obj.status)) { + HAL_Delay(MODWLAN_CONNECTION_WAIT_MS); + waitForConnectionMs += MODWLAN_CONNECTION_WAIT_MS; + if (timeout > 0 && waitForConnectionMs > timeout) { return MODWLAN_ERROR_TIMEOUT; } wlan_update(); @@ -633,16 +731,10 @@ STATIC void wlan_get_sl_mac (void) { sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macAddrLen, wlan_obj.mac); } -STATIC void wlan_wep_key_unhexlify(const char *key, char *key_out) { - int len = strlen(key); +STATIC void wlan_wep_key_unhexlify (const char *key, char *key_out) { byte hex_byte = 0; - for (mp_uint_t i = len; i--;) { - byte hex_ch = *key++; - if (unichar_isxdigit(hex_ch)) { - hex_byte += unichar_xdigit_value(hex_ch); - } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, mpexception_value_invalid_arguments)); - } + for (mp_uint_t i = strlen(key); i > 0 ; i--) { + hex_byte += unichar_xdigit_value(*key++); if (i & 1) { hex_byte <<= 4; } else { @@ -652,122 +744,6 @@ STATIC void wlan_wep_key_unhexlify(const char *key, char *key_out) { } } -/// \method iwconfig(*, mode, ssid, security, key, channel, antenna) -/// -/// Initialise the WLAN engine with the given parameters: -/// -/// - `mode` can be ROLE_AP, ROLE_STA and ROLE_P2P. -/// - `ssid` is the network ssid in case of AP mode -/// - `security` is the security type for AP mode -/// - `key` is the key when in AP mode -/// - `channel` is the channel to use for the AP network -/// - `antenna` selects between the internal or the external one -STATIC const mp_arg_t wlan_iwconfig_args[] = { - { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_ssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, -}; - -STATIC mp_obj_t wlan_iwconfig(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - // parse args - mp_arg_val_t args[MP_ARRAY_SIZE(wlan_iwconfig_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(wlan_iwconfig_args), wlan_iwconfig_args, args); - - bool config = false; - // all params are kw only, so check 'em one by one - if (args[0].u_obj != MP_OBJ_NULL) { - int8_t mode = mp_obj_get_int(args[0].u_obj); - if (mode != ROLE_AP && mode != ROLE_STA && mode != ROLE_P2P) { - goto arg_error; - } - wlan_obj.mode = mode; - config = true; - } - if (args[1].u_obj != MP_OBJ_NULL) { - // get the ssid - mp_uint_t ssid_len; - const char *ssid = mp_obj_str_get_data(args[1].u_obj, &ssid_len); - if (ssid_len > 32) { - goto arg_error; - } - memcpy (wlan_obj.ssid, ssid, ssid_len); - wlan_obj.ssid[ssid_len] = '\0'; - config = true; - } - if (args[2].u_obj != MP_OBJ_NULL) { - int8_t security = mp_obj_get_int(args[2].u_obj); - if (security != SL_SEC_TYPE_OPEN && security != SL_SEC_TYPE_WEP && security != SL_SEC_TYPE_WPA_WPA2) { - goto arg_error; - } - wlan_obj.security = security; - config = true; - } - if (args[3].u_obj != MP_OBJ_NULL) { - // get the key - mp_uint_t key_len; - const char *key; - key = mp_obj_str_get_data(args[3].u_obj, &key_len); - if ((wlan_obj.security == SL_SEC_TYPE_WEP && (key_len < 10 || key_len > 58)) || key_len < 8 || key_len > 64) { - goto arg_error; - } - memcpy (wlan_obj.key, key, key_len); - wlan_obj.key[key_len] = '\0'; - config = true; - } - if (args[4].u_obj != MP_OBJ_NULL) { - int8_t channel = mp_obj_get_int(args[4].u_obj); - if (channel < 1 || channel > 11) { - goto arg_error; - } - wlan_obj.channel = channel; - config = true; - } - - if (config) { - wlan_sl_enable (wlan_obj.mode, (const char *)wlan_obj.ssid, strlen((const char *)wlan_obj.ssid), wlan_obj.security, - (const char *)wlan_obj.key, strlen((const char *)wlan_obj.key), wlan_obj.channel, false); - } - - if (args[5].u_obj != MP_OBJ_NULL) { - #if MICROPY_HW_ANTENNA_DIVERSITY - int8_t antenna = mp_obj_get_int(args[5].u_obj); - if (antenna != ANTENNA_TYPE_INTERNAL && antenna != ANTENNA_TYPE_EXTERNAL) { - goto arg_error; - } - wlan_obj.antenna = antenna; - antenna_select (antenna); - #endif - config = true; - } - - if (!config) { - // return the current configuration - STATIC const qstr iwconfig_fields[] = { - MP_QSTR_mode, MP_QSTR_ssid, - MP_QSTR_security, MP_QSTR_key, - MP_QSTR_channel, MP_QSTR_antenna - }; - - mp_obj_t iwconfig[6]; - iwconfig[0] = mp_obj_new_int(wlan_obj.mode); - iwconfig[1] = mp_obj_new_str((const char *)wlan_obj.ssid, strlen((const char *)wlan_obj.ssid), false); - iwconfig[2] = mp_obj_new_int(wlan_obj.security); - iwconfig[3] = mp_obj_new_str((const char *)wlan_obj.key, strlen((const char *)wlan_obj.key), false); - iwconfig[4] = mp_obj_new_int(wlan_obj.channel); - iwconfig[5] = mp_obj_new_int(wlan_obj.antenna); - return mp_obj_new_attrtuple(iwconfig_fields, MP_ARRAY_SIZE(iwconfig), iwconfig); - } - - return mp_const_none; - -arg_error: - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_iwconfig_obj, 1, wlan_iwconfig); - STATIC void wlan_lpds_irq_enable (mp_obj_t self_in) { wlan_obj_t *self = self_in; self->irq_enabled = true; @@ -799,32 +775,148 @@ STATIC bool wlan_scan_result_is_unique (const mp_obj_list_t *nets, _u8 *bssid) { /// \class WLAN - WiFi driver -/// \classmethod \constructor() -/// Create a wlan object. See iwconfig for parameters of initialization. -STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 0, 0, true); - wlan_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wlan; - if (n_kw > 0) { - mp_map_t kw_args; - mp_map_init_fixed_table(&kw_args, n_kw, args); - wlan_iwconfig(n_args + 1, (const mp_obj_t *)&wlan_obj, &kw_args); +STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) { + // get the mode + int8_t mode = args[0].u_int; + wlan_validate_mode(mode); + + // get the ssid + mp_uint_t ssid_len = 0; + const char *ssid = NULL; + if (args[1].u_obj != NULL) { + ssid = mp_obj_str_get_data(args[1].u_obj, &ssid_len); + wlan_validate_ssid_len(ssid_len); + } + + // get the auth config + uint8_t auth = SL_SEC_TYPE_OPEN; + mp_uint_t key_len = 0; + const char *key = NULL; + if (args[2].u_obj != mp_const_none) { + mp_obj_t *sec; + mp_obj_get_array_fixed_n(args[2].u_obj, 2, &sec); + auth = mp_obj_get_int(sec[0]); + key = mp_obj_str_get_data(sec[1], &key_len); + wlan_validate_security(auth, key, key_len); + } + + // get the channel + uint8_t channel = args[3].u_int; + wlan_validate_channel(channel); + + // get the antenna type + uint8_t antenna = args[4].u_int; +#if MICROPY_HW_ANTENNA_DIVERSITY + wlan_validate_antenna(antenna); +#endif + + // initialize the wlan subsystem + wlan_sl_init(mode, (const char *)ssid, ssid_len, auth, (const char *)key, key_len, channel, antenna, false); + + return mp_const_none; +} + +STATIC const mp_arg_t wlan_init_args[] = { + { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_mode, MP_ARG_INT, {.u_int = ROLE_STA} }, + { MP_QSTR_ssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_auth, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, + { MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ANTENNA_TYPE_INTERNAL} }, +}; +STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) { + // parse args + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args); + mp_arg_val_t args[MP_ARRAY_SIZE(wlan_init_args)]; + mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), wlan_init_args, args); + + // check the peripheral id + if (args[0].u_int != 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); + } + + // setup the object + wlan_obj_t *self = &wlan_obj; + self->base.type = (mp_obj_t)&mod_network_nic_type_wlan; + + // start the peripheral + wlan_init_helper(self, &args[1]); + + // pass it to the sleep module + pybsleep_set_wlan_obj(self); + + return (mp_obj_t)self; +} + +STATIC mp_obj_t wlan_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(wlan_init_args) - 1]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &wlan_init_args[1], args); + return wlan_init_helper(pos_args[0], args); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_init_obj, 1, wlan_init); + +STATIC mp_obj_t wlan_scan(mp_obj_t self_in) { + STATIC const qstr wlan_scan_info_fields[] = { + MP_QSTR_ssid, MP_QSTR_bssid, MP_QSTR_auth, MP_QSTR_channel, MP_QSTR_rssi + }; + + // check for correct wlan mode + if (wlan_obj.mode == ROLE_AP) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); } - pybsleep_set_wlan_obj(&wlan_obj); - return &wlan_obj; + + Sl_WlanNetworkEntry_t wlanEntry; + mp_obj_t nets = mp_obj_new_list(0, NULL); + uint8_t _index = 0; + + // trigger a new network scan + uint32_t scanSeconds = MODWLAN_SCAN_PERIOD_S; + ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_SCAN , MODWLAN_SL_SCAN_ENABLE, (_u8 *)&scanSeconds, sizeof(scanSeconds))); + + // wait for the scan to complete + HAL_Delay (MODWLAN_WAIT_FOR_SCAN_MS); + + do { + if (sl_WlanGetNetworkList(_index++, 1, &wlanEntry) <= 0) { + break; + } + + // we must skip any duplicated results + if (!wlan_scan_result_is_unique(nets, wlanEntry.bssid)) { + continue; + } + + mp_obj_t tuple[5]; + tuple[0] = mp_obj_new_str((const char *)wlanEntry.ssid, wlanEntry.ssid_len, false); + tuple[1] = mp_obj_new_bytes((const byte *)wlanEntry.bssid, SL_BSSID_LENGTH); + // 'normalize' the security type + if (wlanEntry.sec_type > 2) { + wlanEntry.sec_type = 2; + } + tuple[2] = mp_obj_new_int(wlanEntry.sec_type); + tuple[3] = mp_const_none; + tuple[4] = mp_obj_new_int(wlanEntry.rssi); + + // add the network to the list + mp_obj_list_append(nets, mp_obj_new_attrtuple(wlan_scan_info_fields, 5, tuple)); + + } while (_index < MODWLAN_SL_MAX_NETWORKS); + + return nets; } +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_scan_obj, wlan_scan); -/// \method connect(ssid, *, security=OPEN, key=None, bssid=None, timeout=5000) STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC const mp_arg_t allowed_args[] = { { MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ, }, - { MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SL_SEC_TYPE_OPEN} }, - { MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MODWLAN_TIMEOUT_MS} }, + { MP_QSTR_auth, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; - // check for correct wlan mode + // check for the correct wlan mode if (wlan_obj.mode == ROLE_AP) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); } @@ -836,79 +928,80 @@ STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_ // get the ssid mp_uint_t ssid_len; const char *ssid = mp_obj_str_get_data(args[0].u_obj, &ssid_len); + wlan_validate_ssid_len(ssid_len); - // get the security type - mp_uint_t sec = args[1].u_int; + // get the bssid + const char *bssid = NULL; + if (args[1].u_obj != mp_const_none) { + bssid = mp_obj_str_get_str(args[1].u_obj); + } - // get key and its len + // get the auth config + uint8_t auth = SL_SEC_TYPE_OPEN; mp_uint_t key_len = 0; const char *key = NULL; - mp_obj_t key_o = args[2].u_obj; - if (key_o != mp_const_none) { - key = mp_obj_str_get_data(key_o, &key_len); - } - - if (sec == SL_SEC_TYPE_WEP) { - _u8 wep_key[32]; - wlan_wep_key_unhexlify(key, (char *)&wep_key); - key = (const char *)&wep_key; - key_len /= 2; - } - - // get bssid - const char *bssid = NULL; - if (args[3].u_obj != mp_const_none) { - bssid = mp_obj_str_get_str(args[3].u_obj); + if (args[2].u_obj != mp_const_none) { + mp_obj_t *sec; + mp_obj_get_array_fixed_n(args[2].u_obj, 2, &sec); + auth = mp_obj_get_int(sec[0]); + key = mp_obj_str_get_data(sec[1], &key_len); + wlan_validate_security(auth, key, key_len); + + // convert the wep key if needed + if (auth == SL_SEC_TYPE_WEP) { + _u8 wep_key[32]; + wlan_wep_key_unhexlify(key, (char *)&wep_key); + key = (const char *)&wep_key; + key_len /= 2; + } } // get the timeout - uint32_t timeout = MAX(args[4].u_int, 0); - - if (GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION)) { - if (0 == sl_WlanDisconnect()) { - while (IS_CONNECTED(wlan_obj.status)) { - HAL_Delay (5); - wlan_update(); - } - } + int32_t timeout = -1; + if (args[3].u_obj != mp_const_none) { + timeout = mp_obj_get_int(args[3].u_obj); } // connect to the requested access point modwlan_Status_t status; - status = wlan_do_connect (ssid, ssid_len, bssid, sec, key, key_len, timeout); + status = wlan_do_connect (ssid, ssid_len, bssid, auth, key, key_len, timeout); if (status == MODWLAN_ERROR_TIMEOUT) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); - } - else if (status == MODWLAN_ERROR_INVALID_PARAMS) { + } else if (status == MODWLAN_ERROR_INVALID_PARAMS) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } - wlan_obj.security = sec; - return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_connect_obj, 1, wlan_connect); -/// \method wlan_disconnect() -/// Close the current WLAN connection STATIC mp_obj_t wlan_disconnect(mp_obj_t self_in) { - sl_WlanDisconnect(); + wlan_sl_disconnect(); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_disconnect_obj, wlan_disconnect); -/// \method is_connected() -/// Return true if connected to the AP and an IP address has been assigned. Also true if there's any station connected. -/// false otherwise. STATIC mp_obj_t wlan_isconnected(mp_obj_t self_in) { - if (wlan_is_connected()) { - return mp_const_true; - } - return mp_const_false; + return wlan_is_connected() ? mp_const_true : mp_const_false; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_isconnected_obj, wlan_isconnected); -STATIC mp_obj_t wlan_ifconfig (mp_uint_t n_args, const mp_obj_t *args) { - if (n_args == 1) { +STATIC mp_obj_t wlan_ifconfig (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + STATIC const mp_arg_t wlan_ifconfig_args[] = { + { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_config, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + }; + + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(wlan_ifconfig_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), wlan_ifconfig_args, args); + + // check the interface id + if (args[0].u_int != 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); + } + + // get the configuration + if (args[1].u_obj == MP_OBJ_NULL) { // get unsigned char len = sizeof(SlNetCfgIpV4Args_t); unsigned char dhcpIsOn; @@ -922,12 +1015,11 @@ STATIC mp_obj_t wlan_ifconfig (mp_uint_t n_args, const mp_obj_t *args) { netutils_format_ipv4_addr((uint8_t *)&ipV4.ipV4DnsServer, NETUTILS_LITTLE) }; return mp_obj_new_tuple(4, ifconfig); - } - else { - if (mp_obj_get_type(args[1]) == &mp_type_tuple) { + } else { // set the configuration + if (MP_OBJ_IS_TYPE(args[1].u_obj, &mp_type_tuple)) { // set a static ip mp_obj_t *items; - mp_obj_get_array_fixed_n(args[1], 4, &items); + mp_obj_get_array_fixed_n(args[1].u_obj, 4, &items); SlNetCfgIpV4Args_t ipV4; netutils_parse_ipv4_addr(items[0], (uint8_t *)&ipV4.ipV4, NETUTILS_LITTLE); @@ -935,32 +1027,22 @@ STATIC mp_obj_t wlan_ifconfig (mp_uint_t n_args, const mp_obj_t *args) { netutils_parse_ipv4_addr(items[2], (uint8_t *)&ipV4.ipV4Gateway, NETUTILS_LITTLE); netutils_parse_ipv4_addr(items[3], (uint8_t *)&ipV4.ipV4DnsServer, NETUTILS_LITTLE); - // stop the servers - wlan_servers_stop(); if (wlan_obj.mode == ROLE_AP) { ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4)); - // stop and start again - wlan_reenable(wlan_obj.mode); - SlNetAppDhcpServerBasicOpt_t dhcpParams; dhcpParams.lease_time = 4096; // lease time (in seconds) of the IP Address dhcpParams.ipv4_addr_start = ipV4.ipV4 + 1; // first IP Address for allocation. dhcpParams.ipv4_addr_last = (ipV4.ipV4 & 0xFFFFFF00) + 254; // last IP Address for allocation. ASSERT_ON_ERROR(sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID)); // stop DHCP server before settings ASSERT_ON_ERROR(sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, - sizeof(SlNetAppDhcpServerBasicOpt_t), (_u8* )&dhcpParams)); // set parameters + sizeof(SlNetAppDhcpServerBasicOpt_t), (_u8* )&dhcpParams)); // set parameters ASSERT_ON_ERROR(sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID)); // start DHCP server with new settings - } - else { + } else { ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4)); } - // re-enable WLAN and start the servers again - wlan_reenable (wlan_obj.mode); - wlan_servers_start(); - } - else { + } else { // check for the correct string - const char *mode = mp_obj_str_get_str(args[1]); + const char *mode = mp_obj_str_get_str(args[1].u_obj); if (strcmp("dhcp", mode)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } @@ -968,107 +1050,124 @@ STATIC mp_obj_t wlan_ifconfig (mp_uint_t n_args, const mp_obj_t *args) { // only if we are not in AP mode if (wlan_obj.mode != ROLE_AP) { _u8 val = 1; - wlan_servers_stop(); sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, 1, &val); - wlan_reenable (wlan_obj.mode); - wlan_servers_start(); } } + // config values have changed, so reset + wlan_reset(); // set current time and date (needed to validate certificates) wlan_set_current_time (pyb_rtc_get_seconds()); return mp_const_none; } } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_ifconfig_obj, 1, 2, wlan_ifconfig); - -#if MICROPY_PORT_WLAN_URN -STATIC mp_obj_t wlan_urn (uint n_args, const mp_obj_t *args) { - char urn[MAX_DEVICE_URN_LEN]; - uint8_t len = MAX_DEVICE_URN_LEN; +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_ifconfig_obj, 1, wlan_ifconfig); - // an URN is given, so set it - if (n_args == 2) { - const char *p = mp_obj_str_get_str(args[1]); - uint8_t len = strlen(p); +STATIC mp_obj_t wlan_mode (mp_uint_t n_args, const mp_obj_t *args) { + wlan_obj_t *self = args[0]; + if (n_args == 1) { + return mp_obj_new_int(self->mode); + } else { + uint mode = mp_obj_get_int(args[1]); + wlan_validate_mode(mode); + wlan_set_mode(mode); + wlan_reset(); + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_mode_obj, 1, 2, wlan_mode); - // the call to sl_NetAppSet corrupts the input string URN=args[1], so we copy into a local buffer - if (len > MAX_DEVICE_URN_LEN) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); - } - strcpy(urn, p); +STATIC mp_obj_t wlan_ssid (mp_uint_t n_args, const mp_obj_t *args) { + wlan_obj_t *self = args[0]; + if (n_args == 1) { + return mp_obj_new_str((const char *)self->ssid, strlen((const char *)self->ssid), false); + } else { + mp_uint_t len; + const char *ssid = mp_obj_str_get_data(args[1], &len); + wlan_validate_ssid_len(len); + wlan_set_ssid(ssid, len, false); + wlan_reset(); + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_ssid_obj, 1, 2, wlan_ssid); - if (sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, len, (unsigned char *)urn) < 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); +STATIC mp_obj_t wlan_auth (mp_uint_t n_args, const mp_obj_t *args) { + wlan_obj_t *self = args[0]; + if (n_args == 1) { + if (self->auth == SL_SEC_TYPE_OPEN) { + return mp_const_none; + } else { + mp_obj_t security[2]; + security[0] = mp_obj_new_int(self->auth); + security[1] = mp_obj_new_str((const char *)self->key, strlen((const char *)self->key), false); + return mp_obj_new_tuple(2, security); } - } - else { - // get the URN - if (sl_NetAppGet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, &len, (uint8_t *)urn) < 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); + } else { + // get the auth config + uint8_t auth = SL_SEC_TYPE_OPEN; + mp_uint_t key_len = 0; + const char *key = NULL; + if (args[1] != mp_const_none) { + mp_obj_t *sec; + mp_obj_get_array_fixed_n(args[1], 2, &sec); + auth = mp_obj_get_int(sec[0]); + key = mp_obj_str_get_data(sec[1], &key_len); + wlan_validate_security(auth, key, key_len); } - return mp_obj_new_str(urn, (len - 1), false); + wlan_set_security(auth, key, key_len); + wlan_reset(); + return mp_const_none; } - - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_urn_obj, 1, 2, wlan_urn); -#endif - -/// \method wlan_netlist() -/// Return a list of tuples with all the access points within range -STATIC mp_obj_t wlan_scan(mp_obj_t self_in) { - STATIC const qstr wlan_scan_info_fields[] = { - MP_QSTR_ssid, MP_QSTR_bssid, - MP_QSTR_security, MP_QSTR_channel, MP_QSTR_rssi - }; +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_auth_obj, 1, 2, wlan_auth); - // check for correct wlan mode - if (wlan_obj.mode == ROLE_AP) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible)); +STATIC mp_obj_t wlan_channel (mp_uint_t n_args, const mp_obj_t *args) { + wlan_obj_t *self = args[0]; + if (n_args == 1) { + return mp_obj_new_int(self->channel); + } else { + uint8_t channel = mp_obj_get_int(args[1]); + wlan_validate_channel(channel); + wlan_set_channel(channel); + wlan_reset(); + return mp_const_none; } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_channel_obj, 1, 2, wlan_channel); - Sl_WlanNetworkEntry_t wlanEntry; - mp_obj_t nets = mp_obj_new_list(0, NULL); - uint8_t _index = 0; - - // trigger a new network scan - uint32_t scanSeconds = MODWLAN_SCAN_PERIOD_S; - ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_SCAN , MODWLAN_SL_SCAN_ENABLE, (_u8 *)&scanSeconds, sizeof(scanSeconds))); - - // wait for the scan to complete - HAL_Delay (MODWLAN_WAIT_FOR_SCAN_MS); - - do { - if (sl_WlanGetNetworkList(_index++, 1, &wlanEntry) <= 0) { - break; - } - - // we must skip any duplicated results - if (!wlan_scan_result_is_unique(nets, wlanEntry.bssid)) { - continue; - } +STATIC mp_obj_t wlan_antenna (mp_uint_t n_args, const mp_obj_t *args) { + wlan_obj_t *self = args[0]; + if (n_args == 1) { + return mp_obj_new_int(self->antenna); + } else { + #if MICROPY_HW_ANTENNA_DIVERSITY + uint8_t antenna = mp_obj_get_int(args[1]); + wlan_validate_antenna(antenna); + wlan_set_antenna(antenna); + #endif + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_antenna_obj, 1, 2, wlan_antenna); - mp_obj_t tuple[5]; - tuple[0] = mp_obj_new_str((const char *)wlanEntry.ssid, wlanEntry.ssid_len, false); - tuple[1] = mp_obj_new_bytes((const byte *)wlanEntry.bssid, SL_BSSID_LENGTH); - // 'normalize' the security type - if (wlanEntry.sec_type > 2) { - wlanEntry.sec_type = 2; +STATIC mp_obj_t wlan_mac (mp_uint_t n_args, const mp_obj_t *args) { + wlan_obj_t *self = args[0]; + if (n_args == 1) { + return mp_obj_new_bytes((const byte *)self->mac, SL_BSSID_LENGTH); + } else { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); + if (bufinfo.len != 6) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } - tuple[2] = mp_obj_new_int(wlanEntry.sec_type); - tuple[3] = mp_const_none; - tuple[4] = mp_obj_new_int(wlanEntry.rssi); - - // add the network to the list - mp_obj_list_append(nets, mp_obj_new_attrtuple(wlan_scan_info_fields, 5, tuple)); - - } while (_index < MODWLAN_SL_MAX_NETWORKS); - - return nets; + memcpy(self->mac, bufinfo.buf, SL_MAC_ADDR_LEN); + sl_NetCfgSet(SL_MAC_ADDRESS_SET, 1, SL_MAC_ADDR_LEN, (_u8 *)self->mac); + wlan_reset(); + return mp_const_none; + } } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_scan_obj, wlan_scan); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_mac_obj, 1, 2, wlan_mac); -/// \method irq(trigger, priority, handler, wake) STATIC mp_obj_t wlan_irq (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { mp_arg_val_t args[mp_irq_INIT_NUM_ARGS]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mp_irq_INIT_NUM_ARGS, mp_irq_init_args, args); @@ -1092,57 +1191,80 @@ STATIC mp_obj_t wlan_irq (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t * return _irq; invalid_args: - // throw an exception since WLAN only supports LPDS mode nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_irq_obj, 1, wlan_irq); -/// \method mac() -/// returns the MAC address -STATIC mp_obj_t wlan_mac (mp_obj_t self_in) { - return mp_obj_new_bytes((const byte *)wlan_obj.mac, SL_BSSID_LENGTH); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_mac_obj, wlan_mac); - -/// \method connections() -/// returns (ssid/name, bssi), name is the P2P name if in this mode -STATIC mp_obj_t wlan_connections (mp_obj_t self_in) { - mp_obj_t device[2]; - mp_obj_t connections = mp_obj_new_list(0, NULL); - - if (wlan_is_connected()) { - device[0] = mp_obj_new_str((const char *)wlan_obj.ssid_o, strlen((const char *)wlan_obj.ssid_o), false); - device[1] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH); - // add the device to the list - mp_obj_list_append(connections, mp_obj_new_tuple(MP_ARRAY_SIZE(device), device)); - } - return connections; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_connections_obj, wlan_connections); +//STATIC mp_obj_t wlan_connections (mp_obj_t self_in) { +// mp_obj_t device[2]; +// mp_obj_t connections = mp_obj_new_list(0, NULL); +// +// if (wlan_is_connected()) { +// device[0] = mp_obj_new_str((const char *)wlan_obj.ssid_o, strlen((const char *)wlan_obj.ssid_o), false); +// device[1] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH); +// // add the device to the list +// mp_obj_list_append(connections, mp_obj_new_tuple(MP_ARRAY_SIZE(device), device)); +// } +// return connections; +//} +//STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_connections_obj, wlan_connections); + +//STATIC mp_obj_t wlan_urn (uint n_args, const mp_obj_t *args) { +// char urn[MAX_DEVICE_URN_LEN]; +// uint8_t len = MAX_DEVICE_URN_LEN; +// +// // an URN is given, so set it +// if (n_args == 2) { +// const char *p = mp_obj_str_get_str(args[1]); +// uint8_t len = strlen(p); +// +// // the call to sl_NetAppSet corrupts the input string URN=args[1], so we copy into a local buffer +// if (len > MAX_DEVICE_URN_LEN) { +// nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); +// } +// strcpy(urn, p); +// +// if (sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, len, (unsigned char *)urn) < 0) { +// nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); +// } +// } +// else { +// // get the URN +// if (sl_NetAppGet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, &len, (uint8_t *)urn) < 0) { +// nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); +// } +// return mp_obj_new_str(urn, (len - 1), false); +// } +// +// return mp_const_none; +//} +//STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_urn_obj, 1, 2, wlan_urn); STATIC const mp_map_elem_t wlan_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_iwconfig), (mp_obj_t)&wlan_iwconfig_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&wlan_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&wlan_scan_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&wlan_connect_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect), (mp_obj_t)&wlan_disconnect_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_isconnected), (mp_obj_t)&wlan_isconnected_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig), (mp_obj_t)&wlan_ifconfig_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_mode), (mp_obj_t)&wlan_mode_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ssid), (mp_obj_t)&wlan_ssid_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_auth), (mp_obj_t)&wlan_auth_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_channel), (mp_obj_t)&wlan_channel_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_antenna), (mp_obj_t)&wlan_antenna_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_mac), (mp_obj_t)&wlan_mac_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_connections), (mp_obj_t)&wlan_connections_obj }, -#if MICROPY_PORT_WLAN_URN - { MP_OBJ_NEW_QSTR(MP_QSTR_urn), (mp_obj_t)&wlan_urn_obj }, -#endif { MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&wlan_irq_obj }, + // { MP_OBJ_NEW_QSTR(MP_QSTR_connections), (mp_obj_t)&wlan_connections_obj }, + // { MP_OBJ_NEW_QSTR(MP_QSTR_urn), (mp_obj_t)&wlan_urn_obj }, // class constants { MP_OBJ_NEW_QSTR(MP_QSTR_STA), MP_OBJ_NEW_SMALL_INT(ROLE_STA) }, { MP_OBJ_NEW_QSTR(MP_QSTR_AP), MP_OBJ_NEW_SMALL_INT(ROLE_AP) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_OPEN), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_OPEN) }, { MP_OBJ_NEW_QSTR(MP_QSTR_WEP), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WEP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_WPA), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPA_WPA2) }, { MP_OBJ_NEW_QSTR(MP_QSTR_WPA2), MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPA_WPA2) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_INTERNAL), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_INTERNAL) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_EXTERNAL), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_EXTERNAL) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_INT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_INTERNAL) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_EXT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_EXTERNAL) }, { MP_OBJ_NEW_QSTR(MP_QSTR_ANY_EVENT), MP_OBJ_NEW_SMALL_INT(MODWLAN_WIFI_EVENT_ANY) }, }; STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table); |
