Przeglądaj źródła

wifi retry on connect or dhcp fail, init with country code.

Thomas Buck 11 miesięcy temu
rodzic
commit
b1c5e3b983
3 zmienionych plików z 63 dodań i 7 usunięć
  1. 2
    0
      include/config.h
  2. 1
    1
      src/main.c
  3. 60
    6
      src/wifi.c

+ 2
- 0
include/config.h Wyświetl plik

@@ -28,6 +28,8 @@
28 28
 #define VOLCANO_AUTO_CONNECT_TIMEOUT_MS 2000
29 29
 #define VOLCANO_AUTO_CONNECT_WITHIN_MS 10000
30 30
 
31
+#define COUNTRY_CODE CYW43_COUNTRY_GERMANY
32
+
31 33
 #ifdef NDEBUG
32 34
 // Release build
33 35
 #define AUTO_MOUNT_MASS_STORAGE

+ 1
- 1
src/main.c Wyświetl plik

@@ -111,7 +111,7 @@ int main(void) {
111 111
 
112 112
     // required for BLE and LiPo voltage reading
113 113
     debug("cyw43_arch_init");
114
-    if (cyw43_arch_init()) {
114
+    if (cyw43_arch_init_with_country(COUNTRY_CODE)) {
115 115
         debug("cyw43_arch_init failed");
116 116
         lcd_set_backlight(0x00FF);
117 117
         while (1) {}

+ 60
- 6
src/wifi.c Wyświetl plik

@@ -25,17 +25,23 @@
25 25
 #include "mem.h"
26 26
 #include "wifi.h"
27 27
 
28
+#define CONNECT_TIMEOUT (5 * 1000)
29
+
28 30
 enum wifi_state {
29 31
     WS_IDLE = 0,
30 32
     WS_SCAN,
31 33
     WS_CONNECT,
34
+    WS_WAIT_FOR_IP,
32 35
     WS_READY,
33 36
 };
34 37
 
35 38
 static enum wifi_state state = WS_IDLE;
39
+static uint32_t start_time = 0;
36 40
 
37 41
 static void wifi_connect(const char *ssid, const char *pw, uint32_t auth) {
38 42
     debug("connecting to '%s'", ssid);
43
+    start_time = to_ms_since_boot(get_absolute_time());
44
+    state = WS_CONNECT;
39 45
 
40 46
     // https://github.com/raspberrypi/pico-sdk/issues/1413
41 47
     uint32_t a = 0;
@@ -48,8 +54,7 @@ static void wifi_connect(const char *ssid, const char *pw, uint32_t auth) {
48 54
     int r = cyw43_arch_wifi_connect_async(ssid, pw, a);
49 55
     if (r != 0) {
50 56
         debug("failed to connect %d", r);
51
-    } else {
52
-        state = WS_CONNECT;
57
+        state = WS_SCAN;
53 58
     }
54 59
 }
55 60
 
@@ -75,12 +80,13 @@ static int scan_result(void *env, const cyw43_ev_scan_result_t *result) {
75 80
 
76 81
 static void wifi_scan(void) {
77 82
     debug("starting scan");
83
+    state = WS_SCAN;
84
+
78 85
     cyw43_wifi_scan_options_t scan_options = {0};
79 86
     int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
80 87
     if (err != 0) {
81 88
         debug("error %d", err);
82 89
     }
83
-    state = WS_SCAN;
84 90
 }
85 91
 
86 92
 void wifi_init(void) {
@@ -117,8 +123,16 @@ const char *wifi_state(void) {
117 123
     case WS_CONNECT:
118 124
         return "Connecting";
119 125
 
120
-    case WS_READY:
121
-        return ip4addr_ntoa(netif_ip4_addr(netif_default));
126
+    case WS_WAIT_FOR_IP:
127
+        return "Waiting for IP";
128
+
129
+    case WS_READY: {
130
+        cyw43_arch_lwip_begin();
131
+        const ip4_addr_t *ip = netif_ip4_addr(netif_default);
132
+        cyw43_arch_lwip_end();
133
+
134
+        return ip4addr_ntoa(ip);
135
+    }
122 136
     }
123 137
 
124 138
     return NULL;
@@ -133,9 +147,49 @@ void wifi_run(void) {
133 147
             wifi_scan();
134 148
         }
135 149
     } else if (state == WS_CONNECT) {
136
-        if (cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA) == CYW43_LINK_JOIN) {
150
+        int link = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
151
+
152
+        static int prev_link = 0xFF;
153
+        if (prev_link != link) {
154
+            prev_link = link;
155
+            debug("net link status: %d", link);
156
+        }
157
+
158
+        if (link == CYW43_LINK_JOIN) {
137 159
             debug("joined network");
160
+            start_time = to_ms_since_boot(get_absolute_time());
161
+            state = WS_WAIT_FOR_IP;
162
+        } else if (link < CYW43_LINK_DOWN) {
163
+            debug("net connection failed. retry.");
164
+            wifi_scan();
165
+        }
166
+
167
+        uint32_t now = to_ms_since_boot(get_absolute_time());
168
+        if ((now - start_time) >= CONNECT_TIMEOUT) {
169
+            debug("net connection timeout. retry.");
170
+            wifi_scan();
171
+        }
172
+    } else if (state == WS_WAIT_FOR_IP) {
173
+        cyw43_arch_lwip_begin();
174
+        const ip4_addr_t *ip = netif_ip4_addr(netif_default);
175
+        cyw43_arch_lwip_end();
176
+
177
+        if (ip4_addr_get_u32(ip) != 0) {
138 178
             state = WS_READY;
179
+            debug("got IP '%s'", ip4addr_ntoa(ip));
180
+        }
181
+
182
+        uint32_t now = to_ms_since_boot(get_absolute_time());
183
+        if ((now - start_time) >= CONNECT_TIMEOUT) {
184
+            debug("net dhcp timeout. retry.");
185
+            start_time = now;
186
+
187
+            //cyw43_arch_lwip_begin();
188
+            //dhcp_renew(netif_default);
189
+            //cyw43_arch_lwip_end();
190
+
191
+            // DHCP renew does not seem to help, only a full reconnect
192
+            wifi_scan();
139 193
         }
140 194
     }
141 195
 

Ładowanie…
Anuluj
Zapisz