@@ -735,30 +735,47 @@ STATIC mp_obj_t wlan_setpm(mp_obj_t self_in, mp_obj_t pm_mode) {
735735}
736736STATIC MP_DEFINE_CONST_FUN_OBJ_2 (wlan_setpm_obj , wlan_setpm );
737737
738- /// \method connect(ssid, key=None, *, security=OPEN, bssid=None)
738+ /// \method connect(ssid, security=OPEN, key=None, bssid=None)
739+ // if security is WPA/WPA2, the key must be a string
740+ /// if security is WEP, the key must be binary
739741STATIC mp_obj_t wlan_connect (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
740742 STATIC const mp_arg_t allowed_args [] = {
741743 { MP_QSTR_ssid , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
742- { MP_QSTR_key , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
743744 { MP_QSTR_security , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = SL_SEC_TYPE_OPEN } },
745+ { MP_QSTR_key , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
744746 { MP_QSTR_bssid , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
745747 };
746748
749+ // check for correct wlan mode
750+ if (wlan_obj .mode != ROLE_STA && wlan_obj .mode != ROLE_P2P ) {
751+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_request_not_possible ));
752+ }
753+
747754 // parse args
748755 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
749756 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
750757
751- // get ssid
758+ // get the ssid
752759 mp_uint_t ssid_len ;
753760 const char * ssid = mp_obj_str_get_data (args [0 ].u_obj , & ssid_len );
754761
755- // get key and sec
762+ // get the security type
763+ mp_uint_t sec = args [1 ].u_int ;
764+
765+ // get key and its len
756766 mp_uint_t key_len = 0 ;
757767 const char * key = NULL ;
758- mp_uint_t sec = SL_SEC_TYPE_OPEN ;
759- if (args [1 ].u_obj != mp_const_none ) {
760- key = mp_obj_str_get_data (args [1 ].u_obj , & key_len );
761- sec = args [2 ].u_int ;
768+ mp_buffer_info_t wepkey ;
769+ if (args [2 ].u_obj != mp_const_none ) {
770+ // wep key must be given as raw bytes
771+ if (sec == SL_SEC_TYPE_WEP ) {
772+ mp_get_buffer_raise (args [2 ].u_obj , & wepkey , MP_BUFFER_READ );
773+ key = wepkey .buf ;
774+ key_len = wepkey .len ;
775+ }
776+ else {
777+ key = mp_obj_str_get_data (args [2 ].u_obj , & key_len );
778+ }
762779 }
763780
764781 // get bssid
@@ -767,23 +784,22 @@ STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
767784 bssid = mp_obj_str_get_str (args [3 ].u_obj );
768785 }
769786
770- if (wlan_obj .mode != ROLE_STA ) {
771- nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_request_not_possible ));
772- }
773- else {
774- if (GET_STATUS_BIT (wlan_obj .status , STATUS_BIT_CONNECTION )) {
775- if (0 == sl_WlanDisconnect ()) {
776- while (IS_CONNECTED (wlan_obj .status )) {
777- HAL_Delay (5 );
778- }
787+ if (GET_STATUS_BIT (wlan_obj .status , STATUS_BIT_CONNECTION )) {
788+ if (0 == sl_WlanDisconnect ()) {
789+ while (IS_CONNECTED (wlan_obj .status )) {
790+ HAL_Delay (5 );
779791 }
780792 }
781- // connect to the requested access point
782- modwlan_Status_t status ;
783- status = wlan_do_connect (ssid , ssid_len , bssid , sec , key , key_len );
784- if (status != MODWLAN_OK ) {
785- nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_operation_failed ));
786- }
793+ }
794+
795+ // connect to the requested access point
796+ modwlan_Status_t status ;
797+ status = wlan_do_connect (ssid , ssid_len , bssid , sec , key , key_len );
798+ if (status == MODWLAN_ERROR_TIMEOUT ) {
799+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_operation_failed ));
800+ }
801+ else if (status == MODWLAN_ERROR_INVALID_PARAMS ) {
802+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
787803 }
788804
789805 return mp_const_none ;
@@ -812,12 +828,17 @@ STATIC mp_obj_t wlan_isconnected(mp_obj_t self_in) {
812828STATIC MP_DEFINE_CONST_FUN_OBJ_1 (wlan_isconnected_obj , wlan_isconnected );
813829
814830/// \method getip()
815- /// Get the IP
816- ///
817- /// - Returns the acquired IP address
831+ /// return the ip address or None if not connected
818832///
819833STATIC mp_obj_t wlan_getip (mp_obj_t self_in ) {
820- return mod_network_format_ipv4_addr ((uint8_t * )& wlan_obj .ip );
834+ uint32_t ip ;
835+ wlan_get_ip (& ip );
836+ if (ip ) {
837+ return mod_network_format_ipv4_addr ((uint8_t * )& ip );
838+ }
839+ else {
840+ return mp_const_none ;
841+ }
821842}
822843STATIC MP_DEFINE_CONST_FUN_OBJ_1 (wlan_getip_obj , wlan_getip );
823844
0 commit comments