diff --git a/cc3200/mods/modussl.c b/cc3200/mods/modussl.c index e6271e3916..31005631ba 100644 --- a/cc3200/mods/modussl.c +++ b/cc3200/mods/modussl.c @@ -130,7 +130,7 @@ socket_error: arg_error: nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 1, mod_ssl_wrap_socket); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 0, mod_ssl_wrap_socket); STATIC const mp_map_elem_t mp_module_ussl_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ussl) }, diff --git a/cc3200/mods/modwlan.c b/cc3200/mods/modwlan.c index b7ff7b5941..0355275938 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); diff --git a/cc3200/mods/modwlan.h b/cc3200/mods/modwlan.h index 1386d2d191..60582068ff 100644 --- a/cc3200/mods/modwlan.h +++ b/cc3200/mods/modwlan.h @@ -37,6 +37,8 @@ #define MODWLAN_WIFI_EVENT_ANY 0x01 +#define MODWLAN_SSID_LEN_MAX 32 + /****************************************************************************** DEFINE TYPES ******************************************************************************/ @@ -55,12 +57,12 @@ typedef struct _wlan_obj_t { uint32_t ip; int8_t mode; - uint8_t security; + uint8_t auth; uint8_t channel; uint8_t antenna; // my own ssid, key and mac - uint8_t ssid[33]; + uint8_t ssid[(MODWLAN_SSID_LEN_MAX + 1)]; uint8_t key[65]; uint8_t mac[SL_MAC_ADDR_LEN]; @@ -84,12 +86,11 @@ extern _SlLockObj_t wlan_LockObj; DECLARE PUBLIC FUNCTIONS ******************************************************************************/ extern void wlan_pre_init (void); -extern 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); +extern 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); extern void wlan_first_start (void); extern void wlan_update(void); extern void wlan_stop (uint32_t timeout); -extern void wlan_start (void); extern void wlan_get_mac (uint8_t *macAddress); extern void wlan_get_ip (uint32_t *ip); extern bool wlan_is_connected (void); diff --git a/cc3200/mods/pybi2c.c b/cc3200/mods/pybi2c.c index 5369aa97ef..0bf054f559 100644 --- a/cc3200/mods/pybi2c.c +++ b/cc3200/mods/pybi2c.c @@ -320,8 +320,8 @@ invalid_args: STATIC const mp_arg_t pyb_i2c_init_args[] = { { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} }, - { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, - { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, + { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, }; STATIC mp_obj_t pyb_i2c_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 @@ -414,7 +414,7 @@ STATIC mp_obj_t pyb_i2c_readfrom_into(mp_uint_t n_args, const mp_obj_t *pos_args // return the number of bytes received return mp_obj_new_int(vstr.len); } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_into_obj, 3, pyb_i2c_readfrom_into); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_into_obj, 1, pyb_i2c_readfrom_into); STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC const mp_arg_t pyb_i2c_writeto_args[] = { @@ -442,7 +442,7 @@ STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m // return the number of bytes written return mp_obj_new_int(bufinfo.len); } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_obj, 3, pyb_i2c_writeto); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_obj, 1, pyb_i2c_writeto); STATIC mp_obj_t pyb_i2c_readfrom_mem(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC const mp_arg_t pyb_i2c_readfrom_mem_args[] = { @@ -460,7 +460,7 @@ STATIC mp_obj_t pyb_i2c_readfrom_mem(mp_uint_t n_args, const mp_obj_t *pos_args, pyb_i2c_readmem_into (args, &vstr); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_obj, 4, pyb_i2c_readfrom_mem); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_obj, 1, pyb_i2c_readfrom_mem); STATIC const mp_arg_t pyb_i2c_readfrom_mem_into_args[] = { { MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, }, @@ -479,7 +479,7 @@ STATIC mp_obj_t pyb_i2c_readfrom_mem_into(mp_uint_t n_args, const mp_obj_t *pos_ pyb_i2c_readmem_into (args, &vstr); return mp_obj_new_int(vstr.len); } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_into_obj, 4, pyb_i2c_readfrom_mem_into); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_into_obj, 1, pyb_i2c_readfrom_mem_into); STATIC mp_obj_t pyb_i2c_writeto_mem(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { // parse args @@ -507,7 +507,7 @@ STATIC mp_obj_t pyb_i2c_writeto_mem(mp_uint_t n_args, const mp_obj_t *pos_args, nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_mem_obj, 4, pyb_i2c_writeto_mem); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_mem_obj, 1, pyb_i2c_writeto_mem); STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = { // instance methods diff --git a/cc3200/mods/pybsleep.c b/cc3200/mods/pybsleep.c index d7a8e439df..2cdb10fd36 100644 --- a/cc3200/mods/pybsleep.c +++ b/cc3200/mods/pybsleep.c @@ -164,7 +164,7 @@ void pybsleep_init0 (void) { MAP_PRCMHibernateWakeupSourceDisable(PRCM_HIB_SLOW_CLK_CTR | PRCM_HIB_GPIO2 | PRCM_HIB_GPIO4 | PRCM_HIB_GPIO13 | PRCM_HIB_GPIO17 | PRCM_HIB_GPIO11 | PRCM_HIB_GPIO24 | PRCM_HIB_GPIO26); - // store the reset casue (if it's soft reset, leave it as it is) + // check the reset casue (if it's soft reset, leave it as it is) if (pybsleep_reset_cause != PYB_SLP_SOFT_RESET) { switch (MAP_PRCMSysResetCauseGet()) { case PRCM_POWER_ON: @@ -188,6 +188,7 @@ void pybsleep_init0 (void) { switch (MAP_PRCMHibernateWakeupCauseGet()) { case PRCM_HIB_WAKEUP_CAUSE_SLOW_CLOCK: pybsleep_wake_reason = PYB_SLP_WAKED_BY_RTC; + // TODO repeat the alarm break; case PRCM_HIB_WAKEUP_CAUSE_GPIO: pybsleep_wake_reason = PYB_SLP_WAKED_BY_GPIO; diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index fca534458a..2130009757 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -446,7 +446,7 @@ error: } STATIC const mp_arg_t pyb_uart_init_args[] = { - { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 9600} }, { MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} }, { MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} }, @@ -462,7 +462,7 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t // work out the uart id uint uart_id; - if (args[0].u_obj == mp_const_none) { + if (args[0].u_obj == MP_OBJ_NULL) { if (args[5].u_obj != MP_OBJ_NULL) { mp_obj_t *pins; mp_uint_t n_pins = 2; diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h index 7c0a693279..92d96d63b1 100644 --- a/cc3200/mpconfigport.h +++ b/cc3200/mpconfigport.h @@ -207,7 +207,6 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len); #define MICROPY_HAL_H "cc3200_hal.h" #define MICROPY_PORT_HAS_TELNET (1) #define MICROPY_PORT_HAS_FTP (1) -#define MICROPY_PORT_WLAN_URN (0) #define MICROPY_PY_SYS_PLATFORM "WiPy" #define MICROPY_PORT_WLAN_AP_SSID "wipy-wlan" diff --git a/cc3200/mptask.c b/cc3200/mptask.c index 4f01a66eb6..f3d662af0d 100644 --- a/cc3200/mptask.c +++ b/cc3200/mptask.c @@ -68,6 +68,7 @@ #include "mpirq.h" #include "updater.h" #include "moduos.h" +#include "antenna.h" /****************************************************************************** DECLARE PRIVATE CONSTANTS @@ -360,11 +361,11 @@ STATIC void mptask_init_sflash_filesystem (void) { STATIC void mptask_enter_ap_mode (void) { // append the mac only if it's not the first boot - bool append_mac = !PRCMGetSpecialBit(PRCM_FIRST_BOOT_BIT); - + bool add_mac = !PRCMGetSpecialBit(PRCM_FIRST_BOOT_BIT); // enable simplelink in ap mode (use the MAC address to make the ssid unique) - wlan_sl_enable (ROLE_AP, MICROPY_PORT_WLAN_AP_SSID, strlen(MICROPY_PORT_WLAN_AP_SSID), MICROPY_PORT_WLAN_AP_SECURITY, - MICROPY_PORT_WLAN_AP_KEY, strlen(MICROPY_PORT_WLAN_AP_KEY), MICROPY_PORT_WLAN_AP_CHANNEL, append_mac); + wlan_sl_init (ROLE_AP, MICROPY_PORT_WLAN_AP_SSID, strlen(MICROPY_PORT_WLAN_AP_SSID), + MICROPY_PORT_WLAN_AP_SECURITY, MICROPY_PORT_WLAN_AP_KEY, strlen(MICROPY_PORT_WLAN_AP_KEY), + MICROPY_PORT_WLAN_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, add_mac); } STATIC void mptask_create_main_py (void) { diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index 5c619e1cdc..65a391046c 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -176,6 +176,7 @@ Q(pins) // for RTC class Q(RTC) +Q(id) Q(init) Q(alarm) Q(alarm_left) @@ -262,11 +263,15 @@ Q(server_timeout) // for WLAN class Q(WLAN) -Q(iwconfig) +Q(id) +Q(init) +Q(mode) Q(key) -Q(security) +Q(auth) Q(ssid) Q(bssid) +Q(mac) +Q(antenna) Q(scan) Q(connect) Q(isconnected) @@ -274,26 +279,17 @@ Q(disconnect) Q(channel) Q(rssi) Q(ifconfig) -Q(info) -Q(connections) -#if MICROPY_PORT_WLAN_URN -Q(urn) -#endif -Q(mode) -Q(ip) -Q(subnet) -Q(gateway) -Q(dns) -Q(mac) -Q(antenna) +Q(config) +//Q(connections) +//Q(urn) Q(STA) Q(AP) Q(OPEN) Q(WEP) Q(WPA) Q(WPA2) -Q(INTERNAL) -Q(EXTERNAL) +Q(INT_ANT) +Q(EXT_ANT) Q(ANY_EVENT) // for WDT class diff --git a/cc3200/serverstask.c b/cc3200/serverstask.c index 0b6ad18b47..f39ccdbdfc 100644 --- a/cc3200/serverstask.c +++ b/cc3200/serverstask.c @@ -144,7 +144,7 @@ void TASK_Servers (void *pvParameters) { void servers_start (void) { servers_data.do_enable = true; - HAL_Delay (SERVERS_CYCLE_TIME_MS * 5); + HAL_Delay (SERVERS_CYCLE_TIME_MS * 3); } void servers_stop (void) { @@ -152,7 +152,7 @@ void servers_stop (void) { do { HAL_Delay (SERVERS_CYCLE_TIME_MS); } while (servers_are_enabled()); - HAL_Delay (SERVERS_CYCLE_TIME_MS * 5); + HAL_Delay (SERVERS_CYCLE_TIME_MS * 3); } void servers_reset (void) { diff --git a/tests/wipy/reset/reset.py b/tests/wipy/reset/reset.py new file mode 100644 index 0000000000..c1990a4af3 --- /dev/null +++ b/tests/wipy/reset/reset.py @@ -0,0 +1,12 @@ +''' +Reset script for the cc3200 boards +This is needed to force the board to reboot +with the default WLAN AP settings +''' + +from pyb import WDT +import time + +wdt = WDT(timeout=1000) +print(wdt) +time.sleep_ms(900) diff --git a/tests/wipy/reset/reset.py.exp b/tests/wipy/reset/reset.py.exp new file mode 100644 index 0000000000..4b6cc11e0d --- /dev/null +++ b/tests/wipy/reset/reset.py.exp @@ -0,0 +1 @@ + diff --git a/tests/wipy/wlan/wlan.py b/tests/wipy/wlan/wlan.py new file mode 100644 index 0000000000..d4f9b9fa94 --- /dev/null +++ b/tests/wipy/wlan/wlan.py @@ -0,0 +1,166 @@ +''' +WLAN test for the CC3200 based boards. +''' + +from network import WLAN +import os +import time +import testconfig +import pyb + +machine = os.uname().machine +if not 'LaunchPad' in machine and not 'WiPy' in machine: + raise Exception('Board not supported!') + + +def wait_for_connection(wifi, timeout=10): + while not wifi.isconnected() and timeout > 0: + time.sleep(1) + timeout -= 1 + if wifi.isconnected(): + print('Connected') + else: + print('Connection failed!') + + +wifi = WLAN() +print(wifi.mode() == WLAN.STA) +print(wifi.antenna() == WLAN.INT_ANT) + +wifi = WLAN(mode=WLAN.AP) +print(wifi.mode() == WLAN.AP) +print(wifi.channel() == 1) +print(wifi.auth() == None) +print(wifi.antenna() == WLAN.INT_ANT) +wifi = WLAN(0, mode=WLAN.AP, ssid='test-wlan', auth=(WLAN.WPA, '123456abc'), channel=7) +print(wifi.mode() == WLAN.AP) +print(wifi.channel() == 7) +print(wifi.ssid() == 'test-wlan') +print(wifi.auth() == (WLAN.WPA, '123456abc')) +print(wifi.antenna() == WLAN.INT_ANT) + +wifi = WLAN(mode=WLAN.STA) +print(wifi.mode() == WLAN.STA) +scan_r = wifi.scan() +print(len(scan_r) > 3) +for net in scan_r: + if net.ssid == testconfig.wlan_ssid: + print('Network found') + break + +wifi.mode(WLAN.STA) +print(wifi.mode() == WLAN.STA) +wifi.channel(7) +print(wifi.channel() == 7) +wifi.ssid('t-wlan') +print(wifi.ssid() == 't-wlan') +wifi.auth(None) +print(wifi.auth() == None) +wifi.auth((WLAN.WEP, '11223344556677889900')) +print(wifi.auth() == (WLAN.WEP, '11223344556677889900')) +wifi.antenna(WLAN.INT_ANT) +print(wifi.antenna() == WLAN.INT_ANT) + +wifi.antenna(WLAN.EXT_ANT) +print(wifi.antenna() == WLAN.EXT_ANT) +scan_r = wifi.scan() +print(len(scan_r) > 3) +for net in scan_r: + if net.ssid == testconfig.wlan_ssid: + print('Network found') + break + +wifi.antenna(WLAN.INT_ANT) +wifi.mode(WLAN.STA) +print(wifi.mode() == WLAN.STA) +wifi.connect(testconfig.wlan_ssid, auth=testconfig.wlan_auth, timeout=10000) +wait_for_connection(wifi) + +wifi.ifconfig(config='dhcp') +wait_for_connection(wifi) +print('0.0.0.0' not in wifi.ifconfig()) +wifi.ifconfig(0, ('192.168.178.109', '255.255.255.0', '192.168.178.1', '8.8.8.8')) +wait_for_connection(wifi) +print(wifi.ifconfig(0) == ('192.168.178.109', '255.255.255.0', '192.168.178.1', '8.8.8.8')) +wait_for_connection(wifi) + +print(wifi.isconnected() == True) +wifi.disconnect() +print(wifi.isconnected() == False) + +t0 = time.ticks_ms() +wifi.connect(testconfig.wlan_ssid, auth=testconfig.wlan_auth, timeout=0) +print(time.ticks_ms() - t0 < 500) + +wifi.disconnect() +print(wifi.isconnected() == False) + +# test init again +wifi.init(WLAN.AP, ssid='www.wipy.io', auth=None, channel=5, antenna=WLAN.INT_ANT) + +# next ones MUST raise +try: + wifi.init(mode=12345) +except: + print('Exception') + +try: + wifi.init(1, mode=WLAN.AP) +except: + print('Exception') + +try: + wifi.init(mode=WLAN.AP, ssid=None) +except: + print('Exception') + +try: + wifi = WLAN(mode=WLAN.AP, channel=12) +except: + print('Exception') + +try: + wifi.antenna(2) +except: + print('Exception') + +try: + wifi.mode(10) +except: + print('Exception') + +try: + wifi.ssid('11111sdfasdfasdfasdf564sdf654asdfasdf123451245ssdgfsdf1111111111111111111111111234123412341234asdfasdf') +except: + print('Exception') + +try: + wifi.auth((0)) +except: + print('Exception') + +try: + wifi.auth((0, None)) +except: + print('Exception') + +try: + wifi.auth((10, 10)) +except: + print('Exception') + +try: + wifi.channel(0) +except: + print('Exception') + +try: + wifi.ifconfig(1, 'dhcp') +except: + print('Exception') + +try: + wifi.ifconfig(config=()) +except: + print('Exception') + diff --git a/tests/wipy/wlan/wlan.py.exp b/tests/wipy/wlan/wlan.py.exp new file mode 100644 index 0000000000..407c31db98 --- /dev/null +++ b/tests/wipy/wlan/wlan.py.exp @@ -0,0 +1,47 @@ +True +True +True +True +True +True +True +True +True +True +True +True +True +Network found +True +True +True +True +True +True +True +True +Network found +True +Connected +Connected +True +Connected +True +Connected +True +True +True +True +Exception +Exception +Exception +Exception +Exception +Exception +Exception +Exception +Exception +Exception +Exception +Exception +Exception