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