Browse Source

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

Thomas Buck 6 months ago
parent
commit
b1c5e3b983
3 changed files with 63 additions and 7 deletions
  1. 2
    0
      include/config.h
  2. 1
    1
      src/main.c
  3. 60
    6
      src/wifi.c

+ 2
- 0
include/config.h View File

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

+ 1
- 1
src/main.c View File

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

+ 60
- 6
src/wifi.c View File

25
 #include "mem.h"
25
 #include "mem.h"
26
 #include "wifi.h"
26
 #include "wifi.h"
27
 
27
 
28
+#define CONNECT_TIMEOUT (5 * 1000)
29
+
28
 enum wifi_state {
30
 enum wifi_state {
29
     WS_IDLE = 0,
31
     WS_IDLE = 0,
30
     WS_SCAN,
32
     WS_SCAN,
31
     WS_CONNECT,
33
     WS_CONNECT,
34
+    WS_WAIT_FOR_IP,
32
     WS_READY,
35
     WS_READY,
33
 };
36
 };
34
 
37
 
35
 static enum wifi_state state = WS_IDLE;
38
 static enum wifi_state state = WS_IDLE;
39
+static uint32_t start_time = 0;
36
 
40
 
37
 static void wifi_connect(const char *ssid, const char *pw, uint32_t auth) {
41
 static void wifi_connect(const char *ssid, const char *pw, uint32_t auth) {
38
     debug("connecting to '%s'", ssid);
42
     debug("connecting to '%s'", ssid);
43
+    start_time = to_ms_since_boot(get_absolute_time());
44
+    state = WS_CONNECT;
39
 
45
 
40
     // https://github.com/raspberrypi/pico-sdk/issues/1413
46
     // https://github.com/raspberrypi/pico-sdk/issues/1413
41
     uint32_t a = 0;
47
     uint32_t a = 0;
48
     int r = cyw43_arch_wifi_connect_async(ssid, pw, a);
54
     int r = cyw43_arch_wifi_connect_async(ssid, pw, a);
49
     if (r != 0) {
55
     if (r != 0) {
50
         debug("failed to connect %d", r);
56
         debug("failed to connect %d", r);
51
-    } else {
52
-        state = WS_CONNECT;
57
+        state = WS_SCAN;
53
     }
58
     }
54
 }
59
 }
55
 
60
 
75
 
80
 
76
 static void wifi_scan(void) {
81
 static void wifi_scan(void) {
77
     debug("starting scan");
82
     debug("starting scan");
83
+    state = WS_SCAN;
84
+
78
     cyw43_wifi_scan_options_t scan_options = {0};
85
     cyw43_wifi_scan_options_t scan_options = {0};
79
     int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
86
     int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
80
     if (err != 0) {
87
     if (err != 0) {
81
         debug("error %d", err);
88
         debug("error %d", err);
82
     }
89
     }
83
-    state = WS_SCAN;
84
 }
90
 }
85
 
91
 
86
 void wifi_init(void) {
92
 void wifi_init(void) {
117
     case WS_CONNECT:
123
     case WS_CONNECT:
118
         return "Connecting";
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
     return NULL;
138
     return NULL;
133
             wifi_scan();
147
             wifi_scan();
134
         }
148
         }
135
     } else if (state == WS_CONNECT) {
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
             debug("joined network");
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
             state = WS_READY;
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
 

Loading…
Cancel
Save