@@ -203,23 +203,15 @@ static const struct mg_tcpip_driver_cyw_firmware fw = {
203
203
// mif user states
204
204
enum {AP , SCANNING , STOPPING_AP , CONNECTING , READY };
205
205
static unsigned int state ;
206
- static uint32_t s_ip , s_mask ;
207
206
208
207
209
208
static void mif_fn (struct mg_tcpip_if * ifp , int ev , void * ev_data ) {
210
- // TODO(): should we include this inside ifp ? add an fn_data ?
211
209
if (ev == MG_TCPIP_EV_ST_CHG ) {
212
210
MG_INFO (("State change: %u" , * (uint8_t * ) ev_data ));
213
211
}
214
212
switch (state ) {
215
213
case AP : // we are in AP mode, wait for a user connection to trigger a scan or a connection to a network
216
- if (ev == MG_TCPIP_EV_ST_CHG && * (uint8_t * ) ev_data == MG_TCPIP_STATE_UP ) {
217
- MG_INFO (("Access Point started" ));
218
- s_ip = ifp -> ip , ifp -> ip = MG_IPV4 (192 , 168 , 169 , 1 );
219
- s_mask = ifp -> mask , ifp -> mask = MG_IPV4 (255 , 255 , 255 , 0 );
220
- ifp -> enable_dhcp_client = false;
221
- ifp -> enable_dhcp_server = true;
222
- } else if (ev == MG_TCPIP_EV_ST_CHG && * (uint8_t * ) ev_data == MG_TCPIP_STATE_READY ) {
214
+ if (ev == MG_TCPIP_EV_ST_CHG && * (uint8_t * ) ev_data == MG_TCPIP_STATE_READY ) {
223
215
MG_INFO (("Access Point READY !" ));
224
216
225
217
// simulate user request to scan for networks
@@ -233,7 +225,6 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
233
225
struct mg_wifi_scan_bss_data * bss = (struct mg_wifi_scan_bss_data * ) ev_data ;
234
226
MG_INFO (("BSS: %.*s (%u) (%M) %d dBm %u" , bss -> SSID .len , bss -> SSID .buf , bss -> channel , mg_print_mac , bss -> BSSID , (int ) bss -> RSSI , bss -> security ));
235
227
} else if (ev == MG_TCPIP_EV_WIFI_SCAN_END ) {
236
- // struct mg_tcpip_driver_cyw_data *d = (struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
237
228
MG_INFO (("Wi-Fi scan finished" ));
238
229
239
230
// simulate user selection of a network (1/2: stop AP)
@@ -245,18 +236,14 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
245
236
break ;
246
237
case STOPPING_AP :
247
238
if (ev == MG_TCPIP_EV_ST_CHG && * (uint8_t * ) ev_data == MG_TCPIP_STATE_DOWN ) {
248
- struct mg_tcpip_driver_cyw_data * d = ( struct mg_tcpip_driver_cyw_data * ) ifp -> driver_data ;
249
- d -> apmode = false;
239
+ struct mg_wifi_data * wifi = & (( struct mg_tcpip_driver_cyw_data * ) ifp -> driver_data ) -> wifi ;
240
+ wifi -> apmode = false;
250
241
251
242
// simulate user selection of a network (2/2: actual connect)
252
- bool res = mg_wifi_connect (d -> ssid , d -> pass );
243
+ bool res = mg_wifi_connect (wifi );
253
244
MG_INFO (("Manually connecting: %s" , res ? "OK" :"FAIL" ));
254
245
if (res ) {
255
246
state = CONNECTING ;
256
- ifp -> ip = s_ip ;
257
- ifp -> mask = s_mask ;
258
- if (ifp -> ip == 0 ) ifp -> enable_dhcp_client = true;
259
- ifp -> enable_dhcp_server = false;
260
247
} // else manually start AP as below
261
248
}
262
249
break ;
@@ -276,13 +263,13 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
276
263
case READY :
277
264
// go back to AP mode after a disconnection (simulation 2/2), you could retry
278
265
if (ev == MG_TCPIP_EV_ST_CHG && * (uint8_t * ) ev_data == MG_TCPIP_STATE_DOWN ) {
279
- struct mg_tcpip_driver_cyw_data * d = ( struct mg_tcpip_driver_cyw_data * ) ifp -> driver_data ;
280
- bool res = mg_wifi_ap_start (d -> apssid , d -> appass , d -> apchannel );
266
+ struct mg_wifi_data * wifi = & (( struct mg_tcpip_driver_cyw_data * ) ifp -> driver_data ) -> wifi ;
267
+ bool res = mg_wifi_ap_start (wifi );
281
268
MG_INFO (("Disconnected" ));
282
269
MG_INFO (("Manually starting AP: %s" , res ? "OK" :"FAIL" ));
283
270
if (res ) {
284
271
state = AP ;
285
- d -> apmode = true;
272
+ wifi -> apmode = true;
286
273
}
287
274
}
288
275
break ;
@@ -291,7 +278,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
291
278
292
279
293
280
static struct mg_tcpip_driver_cyw_data d = {
294
- ( void * ) & sdio , ( struct mg_tcpip_driver_cyw_firmware * ) & fw , WIFI_SSID , WIFI_PASS , "mongoose" , "mongoose" , 0 , 0 , 10 , true, false};
281
+ { WIFI_SSID , WIFI_PASS , "mongoose" , "mongoose" , 0 , 0 , 0 , 0 , 10 , true}, ( void * ) & sdio , ( struct mg_tcpip_driver_cyw_firmware * ) & fw , false};
295
282
296
283
297
284
/* USER CODE END 0 */
@@ -359,7 +346,9 @@ Error_Handler();
359
346
/* USER CODE BEGIN 2 */
360
347
hwspecific_sdio_init ();
361
348
362
- state = d .apmode ? AP : CONNECTING ;
349
+ d .wifi .apip = MG_IPV4 (192 , 168 , 169 , 1 ),
350
+ d .wifi .apmask = MG_IPV4 (255 , 255 , 255 , 0 ),
351
+ state = d .wifi .apmode ? AP : CONNECTING ;
363
352
364
353
struct mg_mgr mgr ; // Initialise Mongoose event manager
365
354
mg_mgr_init (& mgr ); // and attach it to the interface
0 commit comments