Page 1 of 1

[Wi-Fi scanning] esp_wifi_scan_get_ap_records() crashing with fixed array

Posted: Fri Apr 27, 2018 5:47 pm
by dvddvd
Hi, I'm scanning for AP's nearby using the esp_wifi_scan_start() function.

Code: Select all

    static esp_err_t __wifi_event_handler(void *ctx, system_event_t *event) {
        switch (event->event_id) {
			:
			:
             case SYSTEM_EVENT_SCAN_DONE: {
                {
                    ESP_LOGI(TAG, "[%s] SYSTEM_EVENT_SCAN_DONE", __func__);
                    uint16_t apCount = 0;
                    esp_wifi_scan_get_ap_num(&apCount);
                    printf("Number of access points found: %d (apCount = %d)\n", event->event_info.scan_done.number,
                           apCount);

                    wifi_ap_record_t list[40];
                    uint16_t size = sizeof(t);

                    esp_wifi_scan_get_ap_records(&size, list);             // <<<--------------- CRASHES HERE

                    for (uint16_t i = 0; i < size; i++) {
                        wifi_ap_record_t *ap = list + i;
                        char *ssid = (char *) ap->ssid;
                        char ap_str[20] = {0};
                        sprintf(ap_str, "%d,%s", ap->rssi,
                                ap->authmode == WIFI_AUTH_OPEN ? "open" :
                                ap->authmode == WIFI_AUTH_WEP ? "wep" :
                                ap->authmode == WIFI_AUTH_WPA_PSK ? "wpa-psk" :
                                ap->authmode == WIFI_AUTH_WPA2_PSK ? "wpa-psk" :
                                ap->authmode == WIFI_AUTH_WPA_WPA2_PSK ? "wpa-wpa2-psk" :
                                ap->authmode == WIFI_AUTH_WPA2_ENTERPRISE ? "wpa-eap" :
                                "unknown"
                        );
                        printf("#%d/%d | %s: %s\n", i, apCount, ssid, ap_str);
                    }
                }

            }
            default:
                break;
        }
        return ESP_OK;
    }
However the program crashes when I call esp_wifi_scan_get_ap_records(&size, list);

If I perform a malloc to get a pointer and pass it to esp_wifi_scan_get_ap_records(), it works.
But in my application, memory fragmentation is a serious issue and I'd like to avoid that at all cost.

What is a better way to do this?

Many thanks.

Re: [Wi-Fi scanning] esp_wifi_scan_get_ap_records() crashing with fixed array

Posted: Mon Apr 30, 2018 4:09 am
by kolban
Howdy,
Welcome to the community. Am reading your code but not following the line which reads:

Code: Select all

uint16_t size = sizeof(t);
What is "t" in that expression? Wouldn't you want to set size to be 40 (the number of stack held wifi_ap_record_t records pointed to by list?