diff options
| author | Your Name <you@example.com> | 2026-05-18 23:36:29 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 23:36:29 +0530 |
| commit | 9ff7cffcff11237288cfddecee2684f499d23ddf (patch) | |
| tree | 18d8453ef76fd8e47b44dcef9248417e778708a5 | |
| parent | 46e7dabaf80333ca045061ff480377a4186e5deb (diff) | |
fix: prevent IntegerDivideByZero in config_get_next_wifi when no networks
- Check network_count == 0 before modulo operation
- Disconnect STA before WiFi scan to avoid 'STA is connecting, scan not allowed'
- Add error handling for failed WiFi scans
- This fixes the crash/restart loop on Board C with no configured WiFi
| -rw-r--r-- | main/config.c | 1 | ||||
| -rw-r--r-- | main/display.c | 54 |
2 files changed, 33 insertions, 22 deletions
diff --git a/main/config.c b/main/config.c index 32ae30e..b5de68e 100644 --- a/main/config.c +++ b/main/config.c | |||
| @@ -294,6 +294,7 @@ esp_err_t tollgate_config_get_wifi(wifi_config_t *wifi_config) | |||
| 294 | 294 | ||
| 295 | esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config) | 295 | esp_err_t tollgate_config_get_next_wifi(wifi_config_t *wifi_config) |
| 296 | { | 296 | { |
| 297 | if (g_config.network_count == 0) return ESP_ERR_NOT_FOUND; | ||
| 297 | g_config.current_network = (g_config.current_network + 1) % g_config.network_count; | 298 | g_config.current_network = (g_config.current_network + 1) % g_config.network_count; |
| 298 | return tollgate_config_get_wifi(wifi_config); | 299 | return tollgate_config_get_wifi(wifi_config); |
| 299 | } | 300 | } |
diff --git a/main/display.c b/main/display.c index bec795d..44af981 100644 --- a/main/display.c +++ b/main/display.c | |||
| @@ -631,33 +631,43 @@ static void display_task(void *pvParameters) { | |||
| 631 | 631 | ||
| 632 | if (s_wifi_scan_pending && s_wifi_setup.state == SETUP_SCAN) { | 632 | if (s_wifi_scan_pending && s_wifi_setup.state == SETUP_SCAN) { |
| 633 | s_wifi_scan_pending = false; | 633 | s_wifi_scan_pending = false; |
| 634 | esp_wifi_disconnect(); | ||
| 635 | vTaskDelay(pdMS_TO_TICKS(500)); | ||
| 634 | wifi_scan_config_t scan_cfg = {0}; | 636 | wifi_scan_config_t scan_cfg = {0}; |
| 635 | esp_wifi_scan_start(&scan_cfg, true); | 637 | scan_cfg.scan_type = WIFI_SCAN_TYPE_ACTIVE; |
| 636 | uint16_t ap_count = 0; | 638 | scan_cfg.scan_time.active.min = 100; |
| 637 | esp_wifi_scan_get_ap_num(&ap_count); | 639 | scan_cfg.scan_time.active.max = 300; |
| 638 | if (ap_count > WIFI_SETUP_MAX_APS) ap_count = WIFI_SETUP_MAX_APS; | 640 | esp_err_t scan_ret = esp_wifi_scan_start(&scan_cfg, true); |
| 639 | wifi_ap_record_t ap_records[WIFI_SETUP_MAX_APS]; | 641 | if (scan_ret != ESP_OK) { |
| 640 | esp_wifi_scan_get_ap_records(&ap_count, ap_records); | 642 | ESP_LOGE(TAG, "WiFi scan failed: %s", esp_err_to_name(scan_ret)); |
| 641 | 643 | wifi_setup_set_aps(&s_wifi_setup, NULL, 0); | |
| 642 | wifi_ap_info_t aps[WIFI_SETUP_MAX_APS]; | 644 | } else { |
| 643 | for (int i = 0; i < (int)ap_count; i++) { | 645 | uint16_t ap_count = 0; |
| 644 | strncpy(aps[i].ssid, (const char *)ap_records[i].ssid, WIFI_SETUP_SSID_LEN - 1); | 646 | esp_wifi_scan_get_ap_num(&ap_count); |
| 645 | aps[i].ssid[WIFI_SETUP_SSID_LEN - 1] = '\0'; | 647 | if (ap_count > WIFI_SETUP_MAX_APS) ap_count = WIFI_SETUP_MAX_APS; |
| 646 | aps[i].rssi = ap_records[i].rssi; | 648 | wifi_ap_record_t ap_records[WIFI_SETUP_MAX_APS]; |
| 647 | aps[i].secured = (ap_records[i].authmode != WIFI_AUTH_OPEN); | 649 | esp_wifi_scan_get_ap_records(&ap_count, ap_records); |
| 648 | } | 650 | |
| 651 | wifi_ap_info_t aps[WIFI_SETUP_MAX_APS]; | ||
| 652 | for (int i = 0; i < (int)ap_count; i++) { | ||
| 653 | strncpy(aps[i].ssid, (const char *)ap_records[i].ssid, WIFI_SETUP_SSID_LEN - 1); | ||
| 654 | aps[i].ssid[WIFI_SETUP_SSID_LEN - 1] = '\0'; | ||
| 655 | aps[i].rssi = ap_records[i].rssi; | ||
| 656 | aps[i].secured = (ap_records[i].authmode != WIFI_AUTH_OPEN); | ||
| 657 | } | ||
| 649 | 658 | ||
| 650 | for (int i = 0; i < (int)ap_count - 1; i++) { | 659 | for (int i = 0; i < (int)ap_count - 1; i++) { |
| 651 | for (int j = i + 1; j < (int)ap_count; j++) { | 660 | for (int j = i + 1; j < (int)ap_count; j++) { |
| 652 | if (aps[j].rssi > aps[i].rssi) { | 661 | if (aps[j].rssi > aps[i].rssi) { |
| 653 | wifi_ap_info_t tmp = aps[i]; | 662 | wifi_ap_info_t tmp = aps[i]; |
| 654 | aps[i] = aps[j]; | 663 | aps[i] = aps[j]; |
| 655 | aps[j] = tmp; | 664 | aps[j] = tmp; |
| 665 | } | ||
| 656 | } | 666 | } |
| 657 | } | 667 | } |
| 658 | } | ||
| 659 | 668 | ||
| 660 | wifi_setup_set_aps(&s_wifi_setup, aps, (int)ap_count); | 669 | wifi_setup_set_aps(&s_wifi_setup, aps, (int)ap_count); |
| 670 | } | ||
| 661 | } | 671 | } |
| 662 | 672 | ||
| 663 | if (s_wifi_setup_active && s_wifi_setup.state == SETUP_CONNECTING) { | 673 | if (s_wifi_setup_active && s_wifi_setup.state == SETUP_CONNECTING) { |