|
| 1 | +// Copyright (c) 2022 Cesanta Software Limited |
| 2 | +// All rights reserved |
| 3 | + |
| 4 | +#include "mongoose.h" |
| 5 | + |
| 6 | + |
| 7 | +#define WIFI_SSID "YOUR_WIFI_NETWORK_NAME" // SET THIS! |
| 8 | +#define WIFI_PASS "YOUR_WIFI_PASSWORD" // SET THIS! |
| 9 | + |
| 10 | +static uint32_t s_ip, s_mask; |
| 11 | + |
| 12 | + |
| 13 | +// mif user states |
| 14 | +enum {AP, SCANNING, STOPPING_AP, CONNECTING, READY}; |
| 15 | +static unsigned int state; |
| 16 | + |
| 17 | +static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) { |
| 18 | + // TODO(): should we include this inside ifp ? add an fn_data ? |
| 19 | + if (ev == MG_TCPIP_EV_ST_CHG) { |
| 20 | + MG_INFO(("State change: %u", *(uint8_t *) ev_data)); |
| 21 | + } |
| 22 | + switch(state) { |
| 23 | + case AP: // we are in AP mode, wait for a user connection to trigger a scan or a connection to a network |
| 24 | + if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_UP) { |
| 25 | + MG_INFO(("Access Point started")); |
| 26 | + s_ip = ifp->ip, ifp->ip = MG_IPV4(192, 168, 169, 1); |
| 27 | + s_mask = ifp->mask, ifp->mask = MG_IPV4(255, 255, 255, 0); |
| 28 | + ifp->enable_dhcp_client = false; |
| 29 | + ifp->enable_dhcp_server = true; |
| 30 | + } else if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_READY) { |
| 31 | + MG_INFO(("Access Point READY !")); |
| 32 | + |
| 33 | + // simulate user request to scan for networks |
| 34 | + bool res = mg_wifi_scan(); |
| 35 | + MG_INFO(("Starting scan: %s", res ? "OK":"FAIL")); |
| 36 | + if (res) state = SCANNING; |
| 37 | + } |
| 38 | + break; |
| 39 | + case SCANNING: |
| 40 | + if (ev == MG_TCPIP_EV_WIFI_SCAN_RESULT) { |
| 41 | + struct mg_wifi_scan_bss_data *bss = (struct mg_wifi_scan_bss_data *) ev_data; |
| 42 | + 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)); |
| 43 | + } else if (ev == MG_TCPIP_EV_WIFI_SCAN_END) { |
| 44 | + struct mg_tcpip_driver_pico_w_data *d = (struct mg_tcpip_driver_pico_w_data *) ifp->driver_data; |
| 45 | + MG_INFO(("Wi-Fi scan finished")); |
| 46 | + |
| 47 | + // simulate user selection of a network (1/2: stop AP) |
| 48 | + bool res = mg_wifi_ap_stop(); |
| 49 | + MG_INFO(("Manually stopping AP: %s", res ? "OK":"FAIL")); |
| 50 | + if (res) state = STOPPING_AP; |
| 51 | + // else we have a hw/fw problem |
| 52 | + } |
| 53 | + break; |
| 54 | + case STOPPING_AP: |
| 55 | + if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_DOWN) { |
| 56 | + struct mg_tcpip_driver_pico_w_data *d = (struct mg_tcpip_driver_pico_w_data *) ifp->driver_data; |
| 57 | + d->apmode = false; |
| 58 | + |
| 59 | + // simulate user selection of a network (2/2: actual connect) |
| 60 | + bool res = mg_wifi_connect(d->ssid, d->pass); |
| 61 | + MG_INFO(("Manually connecting: %s", res ? "OK":"FAIL")); |
| 62 | + if (res) { |
| 63 | + state = CONNECTING; |
| 64 | + ifp->ip = s_ip; |
| 65 | + ifp->mask = s_mask; |
| 66 | + if (ifp->ip == 0) ifp->enable_dhcp_client = true; |
| 67 | + ifp->enable_dhcp_server = false; |
| 68 | + } // else manually start AP as below |
| 69 | + } |
| 70 | + break; |
| 71 | + case CONNECTING: |
| 72 | + if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_READY) { |
| 73 | + MG_INFO(("READY!")); |
| 74 | + state = READY; |
| 75 | + |
| 76 | + // simulate user code disconnection and go back to AP mode (1/2: disconnect) |
| 77 | + bool res = mg_wifi_disconnect(); |
| 78 | + MG_INFO(("Manually disconnecting: %s", res ? "OK":"FAIL")); |
| 79 | + } else if (ev == MG_TCPIP_EV_WIFI_CONNECT_ERR) { |
| 80 | + MG_ERROR(("Wi-Fi connect failed")); |
| 81 | + // manually start AP as below |
| 82 | + } |
| 83 | + break; |
| 84 | + case READY: |
| 85 | + // go back to AP mode after a disconnection (simulation 2/2), you could retry |
| 86 | + if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_DOWN) { |
| 87 | + struct mg_tcpip_driver_pico_w_data *d = (struct mg_tcpip_driver_pico_w_data *) ifp->driver_data; |
| 88 | + bool res = mg_wifi_ap_start(d->apssid, d->appass, d->apchannel); |
| 89 | + MG_INFO(("Disconnected")); |
| 90 | + MG_INFO(("Manually starting AP: %s", res ? "OK":"FAIL")); |
| 91 | + if (res) { |
| 92 | + state = AP; |
| 93 | + d->apmode = true; |
| 94 | + } |
| 95 | + } |
| 96 | + break; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +int main(void) { |
| 102 | + // initialize stdio |
| 103 | + stdio_init_all(); |
| 104 | + sleep_ms(4000); |
| 105 | + |
| 106 | + struct mg_mgr mgr; // Initialise Mongoose event manager |
| 107 | + mg_mgr_init(&mgr); // and attach it to the interface |
| 108 | + |
| 109 | + // Initialise WiFi creds |
| 110 | + struct mg_tcpip_driver_pico_w_data driver_data = { |
| 111 | + .ssid = WIFI_SSID, |
| 112 | + .pass = WIFI_PASS, |
| 113 | + .apssid = "mongoose", |
| 114 | + .appass = "eightchars", |
| 115 | + .security = 0, |
| 116 | + .apsecurity = 0, |
| 117 | + .apchannel = 10, |
| 118 | + .apmode = true |
| 119 | + }; |
| 120 | + |
| 121 | + state = driver_data.apmode ? AP : CONNECTING; |
| 122 | + |
| 123 | + // Initialise Mongoose network stack |
| 124 | + // Either set use_dhcp or enter a static config. |
| 125 | + // For static configuration, specify IP/mask/GW in network byte order |
| 126 | + struct mg_tcpip_if mif = { |
| 127 | + .ip = 0, |
| 128 | + .driver = &mg_tcpip_driver_pico_w, |
| 129 | + .driver_data = &driver_data, |
| 130 | + .recv_queue.size = 8192, |
| 131 | + .fn = mif_fn |
| 132 | + }; |
| 133 | + |
| 134 | + mg_tcpip_init(&mgr, &mif); |
| 135 | + MG_INFO(("Init done, starting main loop")); |
| 136 | + |
| 137 | + MG_INFO(("Initialising application...")); |
| 138 | + |
| 139 | + MG_INFO(("Starting event loop")); |
| 140 | + for (;;) { |
| 141 | + mg_mgr_poll(&mgr, 0); |
| 142 | + } |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
0 commit comments