Mon, 29 Oct 2018 19:37:14 +0100
Swapped MLT and HLT SSR pins to match he real hardware. More doxygen changes.
0 | 1 | /** |
2 | * @file task_wifi.c | |
3 | * @brief WiFi task. Connects to a known Access Point. If we know more then | |
4 | * one AP, try to connect all of them until it succeeds (Not yet written). | |
5 | */ | |
6 | ||
7 | ||
8 | #include "config.h" | |
9 | ||
10 | ||
11 | static const char *TAG = "task_wifi"; | |
12 | ||
13 | ||
14 | SemaphoreHandle_t xSemaphoreWiFi = NULL; ///< Semaphore WiFi task. | |
15 | EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. | |
16 | uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. | |
17 | wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. | |
18 | wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. | |
19 | WIFI_State *wifi_state = NULL; ///< Public state for other tasks. | |
20 | ||
21 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
22 | uint8_t _wifi_ssid[33]; ///< Current SSID |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
23 | bool _wifi_ScanAPs = false; ///< Scanning |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
24 | bool _wifi_ScanDone = false; ///< Scan ready |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | uint16_t _wifi_Scanned = 0; ///< Total scanned APs. |
0 | 26 | |
27 | extern int Main_Screen; ///< Current Screen number. | |
28 | extern sButton Buttons[MAXBUTTONS]; ///< Buttons definitions. | |
29 | extern uint32_t TimeSpent; ///< Counter that is increased each second. | |
30 | ||
31 | const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. | |
32 | const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. | |
33 | const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. | |
34 | ||
35 | const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address | |
36 | const int TASK_WIFI_AP_STARTED = BIT4; ///< Indicate that the SoftAP is started | |
37 | const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
38 | const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
0 | 39 | const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. |
40 | ||
41 | ||
42 | /** | |
43 | * @brief Local function, save station configuration. | |
44 | * @return Esp error code. | |
45 | */ | |
46 | esp_err_t SaveStaConfig(void); | |
47 | ||
48 | /** | |
49 | * @brief Local function, fetch last connected station configuration. | |
50 | * @return True if there was a last connection, false if there was not. | |
51 | */ | |
52 | bool FetchStaConfig(void); | |
53 | ||
54 | /** | |
55 | * @brief Local function, WiFi event handler. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
56 | * @param ctx Context |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
57 | * @param event The event |
0 | 58 | * @return Esp error code. |
59 | */ | |
60 | esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event); | |
61 | ||
62 | ||
63 | /****************************************************************************/ | |
64 | ||
65 | ||
66 | ||
67 | esp_err_t SaveStaConfig(void) | |
68 | { | |
69 | int record; | |
70 | ||
71 | if (task_wifi_ConfigSTA && strlen((char *)task_wifi_ConfigSTA->sta.ssid)) { | |
72 | /* | |
73 | * Store in /spiffs/stations.conf if it's a new station. | |
74 | */ | |
75 | record = read_station(task_wifi_ConfigSTA->sta.ssid); | |
76 | if (record == -1) { | |
77 | add_station(task_wifi_ConfigSTA->sta.ssid, task_wifi_ConfigSTA->sta.password); | |
78 | } | |
79 | ||
80 | /* | |
81 | * Update main configuration if needed. | |
82 | */ | |
83 | if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { | |
84 | sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); | |
85 | write_config(); | |
86 | } | |
87 | ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); | |
88 | } | |
89 | ||
90 | return ESP_OK; | |
91 | } | |
92 | ||
93 | ||
94 | ||
95 | bool FetchStaConfig(void) | |
96 | { | |
97 | if (task_wifi_ConfigSTA == NULL) { | |
98 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
99 | } | |
100 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
101 | ||
102 | /* | |
103 | * Search last connected AP as station. | |
104 | */ | |
105 | if (strlen(config.lastSSID)) { | |
106 | read_station((uint8_t *)config.lastSSID); | |
107 | ||
108 | /* ssid */ | |
109 | size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); | |
110 | memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); | |
111 | ||
112 | /* password */ | |
113 | sz = sizeof(task_wifi_ConfigSTA->sta.password); | |
114 | memcpy(task_wifi_ConfigSTA->sta.password, wifiStation.Password, sz); | |
115 | ||
116 | ESP_LOGI(TAG, "FetchStaConfig: last connected to ssid: %s", task_wifi_ConfigSTA->sta.ssid); | |
117 | return task_wifi_ConfigSTA->sta.ssid[0] != '\0'; | |
118 | } | |
119 | ||
120 | ESP_LOGI(TAG, "FetchStaConfig: no last connection found"); | |
121 | return false; | |
122 | } | |
123 | ||
124 | ||
125 | ||
126 | esp_err_t task_wifi_EventHandler(void *ctx, system_event_t *event) | |
127 | { | |
128 | switch(event->event_id) { | |
129 | case SYSTEM_EVENT_SCAN_DONE: | |
130 | // Get the results so the memory used is freed. | |
131 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records)); | |
132 | _wifi_Scanned = ap_num; | |
133 | _wifi_ScanDone = true; | |
134 | break; | |
135 | ||
136 | case SYSTEM_EVENT_STA_START: | |
137 | ESP_LOGD(TAG, "Event STA started"); | |
138 | // Set the configured hostname for the dhcp client. | |
139 | ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname)); | |
140 | esp_wifi_connect(); | |
141 | break; | |
142 | ||
143 | // SYSTEM_EVENT_STA_STOP 3 | |
144 | case SYSTEM_EVENT_STA_CONNECTED: | |
145 | ESP_LOGI(TAG, "Event STA connected, turn off AP"); | |
146 | wifi_ap_record_t ap_info; | |
147 | esp_wifi_sta_get_ap_info(&ap_info); | |
148 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
149 | wifi_state->STA_connected = true; | |
150 | wifi_state->STA_rssi = ap_info.rssi; | |
151 | sprintf(wifi_state->STA_ssid, "%s", ap_info.ssid); | |
152 | xSemaphoreGive(xSemaphoreWiFi); | |
153 | } | |
154 | // Turn off AP | |
155 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | |
156 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
157 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
158 | break; | |
159 | ||
160 | case SYSTEM_EVENT_STA_DISCONNECTED: | |
161 | ESP_LOGI(TAG, "Event STA disconnected, turn on AP"); | |
162 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { | |
163 | wifi_state->STA_connected = false; | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
164 | wifi_state->STA_online = false; |
0 | 165 | wifi_state->STA_rssi = 0; |
166 | xSemaphoreGive(xSemaphoreWiFi); | |
167 | } | |
168 | // Turn on AP | |
169 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); | |
170 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); | |
171 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
172 | ||
173 | /* | |
174 | * Meanwhile, try to reconnect. | |
175 | */ | |
176 | if (FetchStaConfig()) { | |
177 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
178 | } | |
179 | break; | |
180 | ||
181 | // SYSTEM_EVENT_STA_AUTHMODE_CHANGE 6 | |
182 | case SYSTEM_EVENT_STA_GOT_IP: | |
183 | // printf("SYSTEM_EVENT_STA_GOT_IP\n"); | |
184 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); | |
185 | tcpip_adapter_ip_info_t ip; | |
186 | memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); | |
187 | if (tcpip_adapter_get_ip_info(ESP_IF_WIFI_STA, &ip) == 0) { | |
188 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
189 | wifi_state->STA_online = true; |
0 | 190 | snprintf(wifi_state->STA_ip, 15, IPSTR, IP2STR(&ip.ip)); |
191 | snprintf(wifi_state->STA_nm, 15, IPSTR, IP2STR(&ip.netmask)); | |
192 | snprintf(wifi_state->STA_gw, 15, IPSTR, IP2STR(&ip.gw)); | |
193 | xSemaphoreGive(xSemaphoreWiFi); | |
194 | } | |
195 | } | |
196 | break; | |
197 | ||
198 | case SYSTEM_EVENT_STA_LOST_IP: | |
199 | ESP_LOGI(TAG, "Lost IP address"); | |
200 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); | |
201 | if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { | |
202 | wifi_state->STA_ip[0] = '\0'; | |
203 | wifi_state->STA_nm[0] = '\0'; | |
204 | wifi_state->STA_gw[0] = '\0'; | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
205 | wifi_state->STA_online = false; |
0 | 206 | xSemaphoreGive(xSemaphoreWiFi); |
207 | } | |
208 | break; | |
209 | ||
210 | // SYSTEM_EVENT_STA_WPS_ER_SUCCESS 9 | |
211 | // SYSTEM_EVENT_STA_WPS_ER_FAILED 10 | |
212 | // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11 | |
213 | // SYSTEM_EVENT_STA_WPS_ER_PIN 12 | |
214 | ||
215 | case SYSTEM_EVENT_AP_START: | |
216 | ESP_LOGD(TAG, "Event AP started"); | |
217 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); | |
218 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
219 | wifi_state->AP_active = true; | |
220 | wifi_state->AP_clients = 0; | |
221 | xSemaphoreGive(xSemaphoreWiFi); | |
222 | } | |
223 | break; | |
224 | ||
225 | case SYSTEM_EVENT_AP_STOP: | |
226 | ESP_LOGD(TAG, "Event AP stopped"); | |
227 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); | |
228 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
229 | wifi_state->AP_active = false; | |
230 | wifi_state->AP_clients = 0; | |
231 | xSemaphoreGive(xSemaphoreWiFi); | |
232 | } | |
233 | break; | |
234 | ||
235 | case SYSTEM_EVENT_AP_STACONNECTED: | |
236 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
237 | wifi_state->AP_clients++; | |
238 | xSemaphoreGive(xSemaphoreWiFi); | |
239 | } | |
240 | ESP_LOGI(TAG, "Event AP new client, %d connections", wifi_state->AP_clients); | |
241 | break; | |
242 | ||
243 | case SYSTEM_EVENT_AP_STADISCONNECTED: | |
244 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
245 | if (wifi_state->AP_clients > 0) | |
246 | wifi_state->AP_clients--; | |
247 | else | |
248 | wifi_state->AP_clients = 0; | |
249 | xSemaphoreGive(xSemaphoreWiFi); | |
250 | } | |
251 | ESP_LOGI(TAG, "Event AP client disconnect, %d connections", wifi_state->AP_clients); | |
252 | break; | |
253 | ||
254 | // SYSTEM_EVENT_AP_PROBEREQRECVED 17 | |
255 | // SYSTEM_EVENT_GOT_IP6 18 | |
256 | // SYSTEM_EVENT_ETH_START 19 | |
257 | // SYSTEM_EVENT_ETH_STOP 20 | |
258 | // SYSTEM_EVENT_ETH_CONNECTED 21 | |
259 | // SYSTEM_EVENT_ETH_DISCONNECTED 22 | |
260 | // SYSTEM_EVENT_ETH_GOT_IP 23 | |
261 | ||
262 | default: | |
263 | printf("Unknown event %d\n", event->event_id); | |
264 | break; | |
265 | } | |
266 | return ESP_OK; | |
267 | } | |
268 | ||
269 | ||
270 | ||
271 | void task_wifi( void * pvParameters ) | |
272 | { | |
273 | esp_err_t ret; | |
274 | ||
275 | ESP_LOGI(TAG, "Starting WiFi driver"); | |
276 | ||
277 | /* | |
278 | * Initialize NVS | |
279 | */ | |
280 | ret = nvs_flash_init(); | |
281 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { | |
282 | ESP_ERROR_CHECK(nvs_flash_erase()); | |
283 | ret = nvs_flash_init(); | |
284 | } | |
285 | ESP_ERROR_CHECK(ret); | |
286 | ||
287 | /* event handler and event group for the wifi driver */ | |
288 | xEventGroupWifi = xEventGroupCreate(); | |
289 | /* initialize the tcp stack */ | |
290 | tcpip_adapter_init(); | |
291 | ESP_ERROR_CHECK(esp_event_loop_init(task_wifi_EventHandler, NULL)); | |
292 | ||
293 | /* | |
294 | * memory allocation of objects used by the task | |
295 | */ | |
296 | accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); | |
297 | task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); | |
298 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
299 | ||
300 | xSemaphoreWiFi = xSemaphoreCreateMutex(); | |
301 | wifi_state = malloc(sizeof(WIFI_State)); | |
302 | wifi_state->AP_clients = 0; | |
303 | wifi_state->AP_active = false; | |
304 | wifi_state->STA_connected = false; | |
22
90f22a101fc6
Boot now checks got IP status before installing the http and vnc servers.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
305 | wifi_state->STA_online = false; |
0 | 306 | wifi_state->STA_rssi = 0; |
307 | ||
308 | /* wifi scanner config */ | |
309 | wifi_scan_config_t scan_config = { | |
310 | .ssid = 0, | |
311 | .bssid = 0, | |
312 | .channel = 0, | |
313 | .show_hidden = false | |
314 | }; | |
315 | ||
316 | /* | |
317 | * start the softAP access point | |
318 | * stop DHCP server | |
319 | */ | |
320 | ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); | |
321 | ||
322 | /* | |
323 | * Assign a static IP to the AP network interface | |
324 | */ | |
325 | tcpip_adapter_ip_info_t info; | |
326 | memset(&info, 0x00, sizeof(info)); | |
327 | IP4_ADDR(&info.ip, 192, 168, 1, 1); | |
328 | IP4_ADDR(&info.gw, 192, 168, 1, 1); | |
329 | IP4_ADDR(&info.netmask, 255, 255, 255, 0); | |
330 | ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info)); | |
331 | ||
332 | /* start dhcp server */ | |
333 | ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); | |
334 | ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1"); | |
335 | ||
336 | /* start dhcp client */ | |
337 | ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); | |
338 | ESP_LOGI(TAG, "Start DHCP client for STA interface."); | |
339 | ||
340 | /* | |
341 | * init wifi as station + access point | |
342 | */ | |
343 | wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); | |
344 | ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); | |
345 | ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); | |
346 | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); | |
347 | ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap_bandwidth)); | |
348 | ||
349 | // configure the softAP and start it */ | |
350 | wifi_config_t ap_config = { | |
351 | .ap = { | |
352 | .ssid_len = 0, | |
353 | .channel = config.ap_channel, | |
354 | .authmode = WIFI_AUTH_WPA2_PSK, | |
355 | .ssid_hidden = config.ap_ssid_hidden, | |
356 | .max_connection = AP_MAX_CONNECTIONS, | |
357 | .beacon_interval = 100, | |
358 | }, | |
359 | }; | |
360 | memcpy(ap_config.ap.ssid, config.ap_ssid , sizeof(config.ap_ssid)); | |
361 | memcpy(ap_config.ap.password, config.ap_pwd, sizeof(config.ap_pwd)); | |
362 | ret = esp_wifi_set_config(WIFI_IF_AP, &ap_config); | |
363 | if (ret != ESP_OK) { | |
364 | ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret); | |
365 | } | |
366 | ESP_ERROR_CHECK(esp_wifi_start()); | |
367 | ||
368 | ESP_LOGI(TAG, "SoftAP start ssid: `%s' pwd: `%s' channel: %d, hidden: %s", | |
369 | ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no"); | |
370 | ||
371 | /* | |
372 | * try to get access to previously saved wifi | |
373 | */ | |
374 | if (FetchStaConfig()) { | |
375 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
376 | } | |
377 | ||
378 | /* | |
379 | * Wait for access point to start | |
380 | */ | |
381 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); | |
382 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
383 | EventBits_t uxBits; | |
384 | ||
385 | for(;;) { | |
386 | ||
387 | /* actions that can trigger: request a connection, a scan, or a disconnection */ | |
388 | uxBits = xEventGroupWaitBits(xEventGroupWifi, | |
389 | TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, | |
390 | pdFALSE, pdFALSE, portMAX_DELAY ); | |
391 | ||
392 | if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { | |
393 | /* | |
394 | * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory | |
395 | */ | |
396 | ESP_LOGI(TAG, "Request disconnect"); | |
397 | sntp_stop(); | |
398 | ESP_ERROR_CHECK(esp_wifi_disconnect()); | |
399 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
400 | ||
401 | /* | |
402 | * erase configuration | |
403 | */ | |
404 | if (task_wifi_ConfigSTA) { | |
405 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
406 | } | |
407 | config.lastSSID[0] = '\0'; | |
408 | write_config(); | |
409 | ||
410 | /* finally: release the scan request bit */ | |
411 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
412 | ESP_LOGI(TAG, "Request disconnect is finished."); | |
413 | ||
414 | } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { | |
415 | ||
416 | //someone requested a connection! | |
417 | ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid); | |
418 | /* set the new config and connect - reset the failed bit first as it is later tested */ | |
419 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
420 | ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); | |
421 | ||
422 | esp_err_t wifierror = esp_wifi_connect(); | |
423 | if (wifierror != ESP_OK) { | |
424 | ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); | |
425 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); | |
426 | } | |
427 | ||
428 | /* | |
429 | * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted | |
430 | * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. | |
431 | * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. | |
432 | */ | |
433 | uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, portMAX_DELAY ); | |
434 | ||
435 | if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { | |
436 | /* | |
437 | * only save the config if the connection was successful! | |
438 | */ | |
439 | if(uxBits & TASK_WIFI_HAS_IP) { | |
440 | /* save wifi config */ | |
441 | SaveStaConfig(); | |
442 | sntp_setservername(0, "nl.pool.ntp.org"); | |
443 | sntp_init(); | |
444 | ESP_LOGI(TAG, "Initialized SNTP %s", sntp_getservername(0)); | |
445 | } else { | |
446 | ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks. | |
447 | /* failed attempt to connect regardles of the reason */ | |
448 | ||
449 | /* otherwise: reset the config */ | |
450 | memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); | |
451 | } | |
452 | } else { | |
453 | /* hit portMAX_DELAY limit ? */ | |
454 | abort(); | |
455 | } | |
456 | ||
457 | /* finally: release the connection request bit */ | |
458 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
459 | ||
460 | } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { | |
461 | ||
462 | /* safe guard against overflow */ | |
463 | ap_num = MAX_AP_NUM; | |
464 | ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); | |
465 | ||
466 | /* finally: release the scan request bit */ | |
467 | xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
468 | } | |
469 | ||
470 | /* | |
471 | * Here we should check for reconnect actions. | |
472 | */ | |
473 | ||
474 | } /* for(;;) */ | |
475 | vTaskDelay( (TickType_t)10); | |
476 | } | |
477 | ||
478 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
479 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
480 | * @brief Array with AP security names |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
481 | */ |
0 | 482 | const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" }; |
483 | ||
484 | ||
485 | /** | |
486 | * @brief Show an AP station as a button. The buttons are already defined. | |
487 | * @param idx The index position on the display, 1 to 7. | |
488 | * @param ap The AP information from the WiFi scan. | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
489 | * @param show How to display. 0 is blank, 1 is unknown, 2 is known, 3 is a connected AP. |
0 | 490 | */ |
491 | void Show_AP(uint8_t idx, wifi_ap_record_t ap, int show) | |
492 | { | |
493 | char tmp[33]; | |
494 | ||
495 | if ((idx > 7) || (idx < 1)) | |
496 | return; | |
497 | ||
498 | if (show == 0) { | |
499 | _bg = TFT_BLACK; | |
500 | } else { | |
501 | _bg = (color_t){ 63, 63, 64 }; | |
502 | } | |
503 | ||
504 | TFT_fillRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, _bg); | |
505 | if (show == 0) | |
506 | return; | |
507 | ||
508 | TFT_drawRect(Buttons[idx].x, Buttons[idx].y, Buttons[idx].w, Buttons[idx].h, TFT_NAVY); | |
509 | if (show == 3) { | |
510 | _fg = TFT_WHITE; | |
511 | } else if (show == 1) { | |
512 | _fg = TFT_YELLOW; | |
513 | } else { | |
514 | _fg = TFT_CYAN; | |
515 | } | |
516 | ||
517 | TFT_setFont(DEJAVU18_FONT, NULL); | |
518 | sprintf(tmp, "%s", ap.ssid); | |
519 | TFT_print(tmp, Buttons[idx].x + 5, Buttons[idx].y + 6); | |
520 | sprintf(tmp, "%d", ap.rssi); | |
521 | TFT_setFont(DEF_SMALL_FONT, NULL); | |
522 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 4); | |
523 | sprintf(tmp, "%s", apsec[ap.authmode]); | |
524 | TFT_print(tmp, Buttons[idx].x + Buttons[idx].w - (TFT_getStringWidth(tmp) + 5), Buttons[idx].y + 15); | |
525 | } | |
526 | ||
527 | ||
528 | ||
529 | bool WiFi_Init(void) | |
530 | { | |
531 | char pwd[65], pmpt[32]; | |
532 | ||
533 | switch (Main_Screen) { | |
534 | case MAIN_TOOLS_SETUP_WIFI: | |
535 | TopMessage("WiFi"); | |
536 | TFT_setFont(DEJAVU24_FONT, NULL); | |
537 | _fg = TFT_WHITE; | |
538 | TFT_print("Momentje ..", CENTER, CENTER); | |
539 | _wifi_ScanAPs = true; | |
540 | _wifi_ScanDone = false; | |
541 | Buttons_Add(260, 200, 60, 40, "Ok", 0); | |
542 | Buttons[0].dark = true; | |
543 | Buttons_Show(); | |
544 | // Now add the buttons we draw manually. | |
545 | Buttons_Add( 0, 30, 250, 30, "", 1); | |
546 | Buttons_Add( 0, 60, 250, 30, "", 2); | |
547 | Buttons_Add( 0, 90, 250, 30, "", 3); | |
548 | Buttons_Add( 0,120, 250, 30, "", 4); | |
549 | Buttons_Add( 0,150, 250, 30, "", 5); | |
550 | Buttons_Add( 0,180, 250, 30, "", 6); | |
551 | Buttons_Add( 0,210, 250, 30, "", 7); | |
552 | break; | |
553 | ||
554 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
555 | TopMessage("WiFi verbinding"); | |
556 | // Get extra information. | |
557 | wifi_ap_record_t ap_info; | |
558 | esp_wifi_sta_get_ap_info(&ap_info); | |
559 | ||
560 | wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; | |
561 | if (wconfig) { | |
562 | ||
563 | tcpip_adapter_ip_info_t ip_info; | |
564 | ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); | |
565 | char ip[IP4ADDR_STRLEN_MAX]; | |
566 | char gw[IP4ADDR_STRLEN_MAX]; | |
567 | char netmask[IP4ADDR_STRLEN_MAX]; | |
568 | strcpy(ip, ip4addr_ntoa(&ip_info.ip)); | |
569 | strcpy(netmask, ip4addr_ntoa(&ip_info.netmask)); | |
570 | strcpy(gw, ip4addr_ntoa(&ip_info.gw)); | |
571 | TFT_setFont(DEFAULT_FONT, NULL); | |
572 | _fg = TFT_WHITE; | |
573 | TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 40); | |
574 | TFT_print("Kanaal", 155 - TFT_getStringWidth("Kanaal"), 60); | |
575 | TFT_print("Rssi", 155 - TFT_getStringWidth("Rssi"), 80); | |
576 | TFT_print("Mode", 155 - TFT_getStringWidth("Mode"), 100); | |
577 | TFT_print("IP adres", 155 - TFT_getStringWidth("IP adres"), 120); | |
578 | TFT_print("Netmask", 155 - TFT_getStringWidth("Netmask"), 140); | |
579 | TFT_print("Gateway", 155 - TFT_getStringWidth("Gateway"), 160); | |
580 | _fg = TFT_YELLOW; | |
581 | TFT_print((char*)wconfig->sta.ssid, 165, 40); | |
582 | sprintf(pmpt, "%d", ap_info.primary); | |
583 | TFT_print(pmpt, 165, 60); | |
584 | sprintf(pmpt, "%d", ap_info.rssi); | |
585 | TFT_print(pmpt, 165, 80); | |
586 | sprintf(pmpt, "%s%s%s", ap_info.phy_11b ? "b":"", ap_info.phy_11g ? "g":"", ap_info.phy_11n ? "n":""); | |
587 | TFT_print(pmpt, 165, 100); | |
588 | TFT_print((char*)ip, 165, 120); | |
589 | TFT_print((char*)netmask, 165, 140); | |
590 | TFT_print((char*)gw, 165, 160); | |
591 | } | |
592 | Buttons_Add(130, 200, 60, 40, "Ok", 0); | |
593 | Buttons[0].dark = true; | |
594 | Buttons_Show(); | |
595 | break; | |
596 | ||
597 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
598 | TopMessage("WiFi verbinden"); | |
599 | TFT_setFont(DEJAVU18_FONT, NULL); | |
600 | _fg = TFT_WHITE; | |
601 | TFT_print("SSID", 155 - TFT_getStringWidth("SSID"), 70); | |
602 | _fg = TFT_YELLOW; | |
603 | TFT_print((char*)_wifi_ssid, 165, 70); | |
604 | Buttons_Add( 0, 200, 100, 40, "Annuleer", 0); | |
605 | Buttons_Add(110, 200, 100, 40, "Vergeet", 1); | |
606 | Buttons_Add(220, 200, 100, 40, "Verbind", 2); | |
607 | Buttons_Show(); | |
608 | Buttons[0].dark = true; | |
609 | break; | |
610 | ||
611 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
612 | TopMessage("WiFi nieuw"); | |
613 | snprintf(pmpt, 32, "Password for %s", _wifi_ssid); | |
614 | pwd[0] = '\0'; | |
615 | EditTextMin(pmpt, pwd, 64, 8); | |
616 | /* | |
617 | * Disconnect first | |
618 | */ | |
619 | _bg = TFT_BLACK; | |
620 | TFT_fillScreen(_bg); | |
621 | TFT_setFont(DEJAVU24_FONT, NULL); | |
622 | _fg = TFT_WHITE; | |
623 | TFT_print("Momentje ..", CENTER, CENTER); | |
624 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
625 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
626 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
627 | ||
628 | /* | |
629 | * Setup new connection | |
630 | */ | |
631 | if (strlen(pwd)) { | |
632 | wifi_config_t* config = task_wifi_ConfigSTA; | |
633 | memset(config, 0x00, sizeof(wifi_config_t)); | |
634 | memcpy(config->sta.ssid, _wifi_ssid, strlen((char*)_wifi_ssid)); | |
635 | memcpy(config->sta.password, pwd, strlen(pwd)); | |
636 | ESP_LOGI(TAG, "new AP %s %s", _wifi_ssid, pwd); | |
637 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
638 | } else { | |
639 | // TODO: what about WPS, it works but how to insert it in this app. | |
640 | return true; | |
641 | } | |
642 | // We must wait here for the result. | |
643 | break; | |
644 | ||
645 | default: | |
646 | break; | |
647 | } | |
648 | ||
649 | return false; | |
650 | } | |
651 | ||
652 | ||
653 | bool WiFi_Loop(void) | |
654 | { | |
655 | uint8_t idx; | |
656 | int Choice; | |
657 | static int AP[8]; | |
658 | wifi_ap_record_t ap; | |
659 | ||
660 | switch (Main_Screen) { | |
661 | case MAIN_TOOLS_SETUP_WIFI: | |
662 | if (_wifi_ScanAPs) { | |
663 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); | |
664 | _wifi_ScanAPs = false; | |
665 | TimeSpent = 0; | |
666 | } | |
667 | if (_wifi_ScanDone) { | |
668 | /* | |
669 | * Show scan results. There is room for 7 entries. If we have a connection, | |
670 | * the first one is that connection followed by available Access Points. | |
671 | * If there is no connection yet, there is only a iist of available Access | |
672 | * points. | |
673 | * The list is sorted by signal strength and is filled by the eventhandler. | |
674 | */ | |
675 | idx = 1; | |
676 | _wifi_ScanDone = false; | |
677 | ||
678 | if ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED) { | |
679 | // We are connected. Search and display this one. | |
680 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
681 | for (int i = 0; i < _wifi_Scanned; i++) { | |
682 | ap = accessp_records[i]; | |
683 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
684 | AP[idx] = i; | |
685 | Show_AP(idx, ap, 3); | |
686 | idx++; | |
687 | break; | |
688 | } | |
689 | } | |
690 | xSemaphoreGive(xSemaphoreWiFi); | |
691 | } | |
692 | } | |
693 | ||
694 | // Display available Access Points. | |
695 | for (int i = 0; i < _wifi_Scanned; i++) { | |
696 | // The connected AP can be somewhere in this list, don't display it again. | |
697 | ap = accessp_records[i]; | |
698 | if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { | |
699 | if (strcmp(wifi_state->STA_ssid, (char *)ap.ssid) == 0) { | |
700 | xSemaphoreGive(xSemaphoreWiFi); | |
701 | continue; // Skip connected AP, already on top of the list. | |
702 | } | |
703 | xSemaphoreGive(xSemaphoreWiFi); | |
704 | } | |
705 | // Check if we know this AP in the database. | |
706 | if ((read_station(ap.ssid) == -1)) { | |
707 | Show_AP(idx, ap, 2); // Unknown | |
708 | } else { | |
709 | if (wifiStation.hide) { | |
710 | continue; // Blacklisted. | |
711 | } | |
712 | Show_AP(idx, ap, 1); // We know this one. | |
713 | } | |
714 | AP[idx] = i; | |
715 | idx++; | |
716 | if (idx == 8) | |
717 | break; | |
718 | } | |
719 | if (idx < 7) { | |
720 | for (int i = idx; i < 8; i++) { | |
721 | Show_AP(i, ap, 0); | |
722 | AP[i] = 0; | |
723 | } | |
724 | } | |
725 | } | |
726 | ||
727 | Choice = Buttons_Scan(); | |
728 | if ((Choice >= 1) && (Choice <= 7)) { | |
729 | ap = accessp_records[AP[Choice]]; | |
730 | sprintf((char *)_wifi_ssid, "%s", ap.ssid); // Save selected SSID. | |
731 | } | |
732 | if ((Choice == 1) && ((xEventGroupGetBits(xEventGroupWifi) & TASK_WIFI_STA_CONNECTED) == TASK_WIFI_STA_CONNECTED)) { | |
733 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CUR; | |
734 | // Cancel a possible scan. | |
735 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
736 | } else if ((Choice >= 1) && (Choice <= 7)) { | |
737 | if ((read_station(_wifi_ssid) != -1)) { | |
738 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_CON; | |
739 | } else { | |
740 | Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW; | |
741 | } | |
742 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
743 | } else if (Choice == 0) { | |
744 | Main_Screen = MAIN_TOOLS_SETUP; | |
745 | ESP_ERROR_CHECK(esp_wifi_scan_stop()); | |
746 | } | |
747 | if (TimeSpent > 10) { | |
748 | _wifi_ScanAPs = true; | |
749 | } | |
750 | break; | |
751 | ||
752 | case MAIN_TOOLS_SETUP_WIFI_CUR: | |
753 | ||
754 | if (Buttons_Scan() == 0) { | |
755 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
756 | } | |
757 | break; | |
758 | ||
759 | case MAIN_TOOLS_SETUP_WIFI_CON: | |
760 | switch (Buttons_Scan()) { | |
761 | case 0: // Cancel choice | |
762 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
763 | break; | |
764 | ||
765 | case 1: // Forget connection | |
766 | remove_station(_wifi_ssid); | |
767 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
768 | break; | |
769 | ||
770 | case 2: // Connect | |
771 | _bg = TFT_BLACK; | |
772 | TFT_fillScreen(_bg); | |
773 | TFT_setFont(DEJAVU24_FONT, NULL); | |
774 | _fg = TFT_WHITE; | |
775 | TFT_print("Momentje ..", CENTER, CENTER); | |
776 | /* | |
777 | * Disconnect old connections and wait until it's gone. | |
778 | */ | |
779 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); | |
780 | vTaskDelay(100 / portTICK_PERIOD_MS); | |
781 | xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); | |
782 | /* | |
783 | * Setup new connection. | |
784 | */ | |
785 | if ((read_station(_wifi_ssid) != -1)) { | |
786 | wifi_config_t* config = task_wifi_ConfigSTA; | |
787 | memset(config, 0x00, sizeof(wifi_config_t)); | |
788 | memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); | |
789 | memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); | |
790 | xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); | |
791 | vTaskDelay(1000 / portTICK_PERIOD_MS); | |
792 | } | |
793 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
794 | break; | |
795 | ||
796 | default: break; | |
797 | } | |
798 | break; | |
799 | ||
800 | case MAIN_TOOLS_SETUP_WIFI_NEW: | |
801 | // All work is already done, jump to the base screen. | |
802 | Main_Screen = MAIN_TOOLS_SETUP_WIFI; | |
803 | break; | |
804 | ||
805 | default: | |
806 | break; | |
807 | } | |
808 | ||
809 | return false; | |
810 | } | |
811 | ||
812 |