@@ -203,23 +203,15 @@ static const struct mg_tcpip_driver_cyw_firmware fw = {
203203// mif user states
204204enum {AP , SCANNING , STOPPING_AP , CONNECTING , READY };
205205static unsigned int state ;
206- static uint32_t s_ip , s_mask ;
207206
208207
209208static 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 ?
211209 if (ev == MG_TCPIP_EV_ST_CHG ) {
212210 MG_INFO (("State change: %u" , * (uint8_t * ) ev_data ));
213211 }
214212 switch (state ) {
215213 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 ) {
223215 MG_INFO (("Access Point READY !" ));
224216
225217 // 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) {
233225 struct mg_wifi_scan_bss_data * bss = (struct mg_wifi_scan_bss_data * ) ev_data ;
234226 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 ));
235227 } 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;
237228 MG_INFO (("Wi-Fi scan finished" ));
238229
239230 // 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) {
245236 break ;
246237 case STOPPING_AP :
247238 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;
250241
251242 // 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 );
253244 MG_INFO (("Manually connecting: %s" , res ? "OK" :"FAIL" ));
254245 if (res ) {
255246 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;
260247 } // else manually start AP as below
261248 }
262249 break ;
@@ -276,13 +263,13 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
276263 case READY :
277264 // go back to AP mode after a disconnection (simulation 2/2), you could retry
278265 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 );
281268 MG_INFO (("Disconnected" ));
282269 MG_INFO (("Manually starting AP: %s" , res ? "OK" :"FAIL" ));
283270 if (res ) {
284271 state = AP ;
285- d -> apmode = true;
272+ wifi -> apmode = true;
286273 }
287274 }
288275 break ;
@@ -291,7 +278,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
291278
292279
293280static 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};
295282
296283
297284/* USER CODE END 0 */
@@ -359,7 +346,9 @@ Error_Handler();
359346 /* USER CODE BEGIN 2 */
360347 hwspecific_sdio_init ();
361348
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 ;
363352
364353 struct mg_mgr mgr ; // Initialise Mongoose event manager
365354 mg_mgr_init (& mgr ); // and attach it to the interface
0 commit comments