@@ -51,6 +51,9 @@ typedef struct _nina_obj_t {
51
51
mp_obj_base_t base ;
52
52
bool active ;
53
53
uint32_t itf ;
54
+ mp_uint_t security ;
55
+ char ssid [NINA_MAX_SSID_LEN + 1 ];
56
+ char key [NINA_MAX_WPA_LEN + 1 ];
54
57
} nina_obj_t ;
55
58
56
59
// For auto-binding UDP sockets
@@ -78,6 +81,7 @@ const mod_network_nic_type_t mod_network_nic_type_nina;
78
81
static nina_obj_t network_nina_wl_sta = {{(mp_obj_type_t * )& mod_network_nic_type_nina }, false, MOD_NETWORK_STA_IF };
79
82
static nina_obj_t network_nina_wl_ap = {{(mp_obj_type_t * )& mod_network_nic_type_nina }, false, MOD_NETWORK_AP_IF };
80
83
static mp_sched_node_t mp_wifi_sockpoll_node ;
84
+ static mp_sched_node_t mp_wifi_connpoll_node ;
81
85
82
86
STATIC void network_ninaw10_poll_sockets (mp_sched_node_t * node ) {
83
87
(void )node ;
@@ -99,6 +103,40 @@ STATIC void network_ninaw10_poll_sockets(mp_sched_node_t *node) {
99
103
}
100
104
}
101
105
106
+ STATIC void network_ninaw10_poll_connect (mp_sched_node_t * node ) {
107
+ nina_obj_t * self = & network_nina_wl_sta ;
108
+
109
+ int status = nina_connection_status ();
110
+ if (status == NINA_STATUS_CONNECTED ) {
111
+ // Connected to AP, nothing else to do.
112
+ return ;
113
+ }
114
+
115
+ if (status != NINA_STATUS_NO_SSID_AVAIL ) {
116
+ // If not connected, and no connection in progress, the connection attempt has failed.
117
+ // Read the ESP failure reason, reconnect and reschedule the connection polling code.
118
+ int reason = nina_connection_reason ();
119
+ if (reason == NINA_ESP_REASON_AUTH_EXPIRE ||
120
+ reason == NINA_ESP_REASON_ASSOC_EXPIRE ||
121
+ reason == NINA_ESP_REASON_NOT_AUTHED ||
122
+ reason == NINA_ESP_REASON_4WAY_HANDSHAKE_TIMEOUT ||
123
+ reason >= NINA_ESP_REASON_BEACON_TIMEOUT ) {
124
+ debug_printf (& mp_plat_print , "poll_connect() status: %d reason %d\n" , status , reason );
125
+ if (nina_connect (self -> ssid , self -> security , self -> key , 0 ) != 0 ) {
126
+ mp_raise_msg_varg (& mp_type_OSError ,
127
+ MP_ERROR_TEXT ("could not connect to ssid=%s, sec=%d, key=%s\n" ),
128
+ self -> ssid , self -> security , self -> key );
129
+ }
130
+ } else {
131
+ // Will not attempt to reconnect if there's another error code set.
132
+ return ;
133
+ }
134
+ }
135
+
136
+ // Reschedule the connection polling code.
137
+ mp_sched_schedule_node (& mp_wifi_connpoll_node , network_ninaw10_poll_connect );
138
+ }
139
+
102
140
STATIC mp_obj_t network_ninaw10_timer_callback (mp_obj_t none_in ) {
103
141
if (MP_STATE_PORT (mp_wifi_sockpoll_list ) != MP_OBJ_NULL && MP_STATE_PORT (mp_wifi_sockpoll_list )-> len ) {
104
142
mp_sched_schedule_node (& mp_wifi_sockpoll_node , network_ninaw10_poll_sockets );
@@ -240,6 +278,12 @@ STATIC mp_obj_t network_ninaw10_connect(mp_uint_t n_args, const mp_obj_t *pos_ar
240
278
mp_raise_msg_varg (& mp_type_OSError ,
241
279
MP_ERROR_TEXT ("could not connect to ssid=%s, sec=%d, key=%s\n" ), ssid , security , key );
242
280
}
281
+
282
+ // Save connection info to re-connect if needed.
283
+ self -> security = security ;
284
+ strncpy (self -> key , key , NINA_MAX_WPA_LEN );
285
+ strncpy (self -> ssid , ssid , NINA_MAX_SSID_LEN );
286
+ mp_sched_schedule_node (& mp_wifi_connpoll_node , network_ninaw10_poll_connect );
243
287
} else {
244
288
mp_uint_t channel = args [ARG_channel ].u_int ;
245
289
@@ -252,6 +296,7 @@ STATIC mp_obj_t network_ninaw10_connect(mp_uint_t n_args, const mp_obj_t *pos_ar
252
296
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("failed to start in AP mode" ));
253
297
}
254
298
}
299
+
255
300
return mp_const_none ;
256
301
}
257
302
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (network_ninaw10_connect_obj , 1 , network_ninaw10_connect );
0 commit comments