@@ -93,29 +93,55 @@ STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args) {
9393}
9494STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_active_obj , 1 , 2 , esp_active );
9595
96- STATIC mp_obj_t esp_connect (size_t n_args , const mp_obj_t * args ) {
97- require_if (args [0 ], STATION_IF );
96+ STATIC mp_obj_t esp_connect (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
97+ enum { ARG_ssid , ARG_password , ARG_bssid };
98+ static const mp_arg_t allowed_args [] = {
99+ { MP_QSTR_ , MP_ARG_OBJ , {.u_obj = mp_const_none } },
100+ { MP_QSTR_ , MP_ARG_OBJ , {.u_obj = mp_const_none } },
101+ { MP_QSTR_bssid , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
102+ };
103+
104+ // parse args
105+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
106+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
107+
108+ require_if (pos_args [0 ], STATION_IF );
98109 struct station_config config = {{0 }};
99110 size_t len ;
100111 const char * p ;
112+ bool set_config = false;
101113
102- if (n_args > 1 ) {
103- p = mp_obj_str_get_data (args [1 ], & len );
114+ // set parameters based on given args
115+ if (args [ARG_ssid ].u_obj != mp_const_none ) {
116+ p = mp_obj_str_get_data (args [ARG_ssid ].u_obj , & len );
104117 len = MIN (len , sizeof (config .ssid ));
105118 memcpy (config .ssid , p , len );
106- if (n_args > 2 ) {
107- p = mp_obj_str_get_data (args [2 ], & len );
108- len = MIN (len , sizeof (config .password ));
109- memcpy (config .password , p , len );
119+ set_config = true;
120+ }
121+ if (args [ARG_password ].u_obj != mp_const_none ) {
122+ p = mp_obj_str_get_data (args [ARG_password ].u_obj , & len );
123+ len = MIN (len , sizeof (config .password ));
124+ memcpy (config .password , p , len );
125+ set_config = true;
126+ }
127+ if (args [ARG_bssid ].u_obj != mp_const_none ) {
128+ p = mp_obj_str_get_data (args [ARG_bssid ].u_obj , & len );
129+ if (len != sizeof (config .bssid )) {
130+ mp_raise_ValueError (NULL );
110131 }
132+ config .bssid_set = 1 ;
133+ memcpy (config .bssid , p , sizeof (config .bssid ));
134+ set_config = true;
135+ }
111136
137+ if (set_config ) {
112138 error_check (wifi_station_set_config (& config ), "Cannot set STA config" );
113139 }
114140 error_check (wifi_station_connect (), "Cannot connect to AP" );
115141
116142 return mp_const_none ;
117143}
118- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_connect_obj , 1 , 7 , esp_connect );
144+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (esp_connect_obj , 1 , esp_connect );
119145
120146STATIC mp_obj_t esp_disconnect (mp_obj_t self_in ) {
121147 require_if (self_in , STATION_IF );
0 commit comments