@@ -735,30 +735,47 @@ STATIC mp_obj_t wlan_setpm(mp_obj_t self_in, mp_obj_t pm_mode) {
735
735
}
736
736
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (wlan_setpm_obj , wlan_setpm );
737
737
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
739
741
STATIC mp_obj_t wlan_connect (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
740
742
STATIC const mp_arg_t allowed_args [] = {
741
743
{ 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 } },
743
744
{ 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 } },
744
746
{ MP_QSTR_bssid , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
745
747
};
746
748
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
+
747
754
// parse args
748
755
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
749
756
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
750
757
751
- // get ssid
758
+ // get the ssid
752
759
mp_uint_t ssid_len ;
753
760
const char * ssid = mp_obj_str_get_data (args [0 ].u_obj , & ssid_len );
754
761
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
756
766
mp_uint_t key_len = 0 ;
757
767
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
+ }
762
779
}
763
780
764
781
// 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_
767
784
bssid = mp_obj_str_get_str (args [3 ].u_obj );
768
785
}
769
786
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 );
779
791
}
780
792
}
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 ));
787
803
}
788
804
789
805
return mp_const_none ;
@@ -812,12 +828,17 @@ STATIC mp_obj_t wlan_isconnected(mp_obj_t self_in) {
812
828
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (wlan_isconnected_obj , wlan_isconnected );
813
829
814
830
/// \method getip()
815
- /// Get the IP
816
- ///
817
- /// - Returns the acquired IP address
831
+ /// return the ip address or None if not connected
818
832
///
819
833
STATIC 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
+ }
821
842
}
822
843
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (wlan_getip_obj , wlan_getip );
823
844
0 commit comments