Bug in esp_wifi_scan_get_ap_num ??

lukilukeskywalker
Posts: 21
Joined: Wed Feb 22, 2023 4:25 pm

Bug in esp_wifi_scan_get_ap_num ??

Postby lukilukeskywalker » Thu Mar 30, 2023 11:37 am

Greetings!!!

I don' t know if I have found a bug or maybe there is some bug in my code. I have written a function that scans the avalaible AP's and returns them in a json format. It worked, until I noticed that my webpage sometimes wasn't loading the new AP's, then I saw the json was malformed. When I looked into it, then I saw that was returning (sometimes) more APs than where avaliable, and when I was reading them and writing them into an array with a json form, they where overflowing, making the json malformed.
Here is the code:

Code: Select all

size_t scan_APs(char **scanned_APs){
    //Scans APs and returns them in a jsonfied form for processing them in the configurator
    // Redone: It returns the size, but receives a pointer to where the data will be set
    ESP_LOGD(TAG, "Scanning Wifi Access Points, and stringifying them to json format");
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
    uint16_t ap_count = 0;
    uint16_t number = DEFAULT_SCAN_LIST_SIZE;
    wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
    esp_wifi_scan_start(NULL, true);
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));

    // Max size of ssid is 32 Caracters, 
    size_t used_space=0;
    const size_t max_size = 1024;
    *scanned_APs = malloc(max_size);
    ESP_LOGI(__func__, "Found Networks: %d", ap_count);
    used_space = snprintf(*scanned_APs, max_size, "{\"wifi_APs\":[\n");
    int i = 0;
    while((i < ap_count) & (used_space < max_size)){
        //The other end just wants to know Name, Security and RSSI
        //if(ap_info[i].ssid == "" | ap_info[i].rssi >= 0)break;      //This condition is not possible, but the 
        used_space += snprintf(*scanned_APs + used_space, max_size - used_space,
                        "{\"Name\": \"%s\",\n\"Authmode\": \"%d\",\n\"RSSI\": \"%d\"\n},", ap_info[i].ssid, ap_info[i].authmode, ap_info[i].rssi);
        i++;
    }
    used_space--;   //This way we rewrite the , of the end of the previous and last object
    used_space += snprintf(*scanned_APs + used_space, max_size - used_space, "\n]}");
    //used_space = snprintf(*scanned_APs, max_size, "{'wifi_ap':'thisisatest'}");
    ESP_LOGD(TAG, "Number of Scanned Networks: %d \nScanned Networks: \n %s", ap_count, *scanned_APs);
    ESP_LOGD(TAG, "Size Scanned Networks: %d", used_space);
    scanned_APs = realloc(*scanned_APs, used_space);
    return used_space;
}
Maybe there is something with my code and I can't see it, but it is weird that sometimes the number of AP's it says it has is bigger than what the memory holds

MicroController
Posts: 1136
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Bug in esp_wifi_scan_get_ap_num ??

Postby MicroController » Sat Apr 01, 2023 11:04 pm

I wonder what esp_err_t value esp_wifi_scan_start(NULL, true); returns.
And you should only access the scan results indicated by number, which may be different from ap_count in your case.

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Bug in esp_wifi_scan_get_ap_num ??

Postby Craige Hales » Sun Apr 02, 2023 11:21 am

Should this

Code: Select all

scanned_APs = realloc(*scanned_APs, used_space);
be

Code: Select all

*scanned_APs = realloc(*scanned_APs, used_space);
? I believe realloc is hard to use correctly: https://stackoverflow.com/questions/907 ... se-realloc and I don't think I've ever used it.
Craige

lukilukeskywalker
Posts: 21
Joined: Wed Feb 22, 2023 4:25 pm

Re: Bug in esp_wifi_scan_get_ap_num ??

Postby lukilukeskywalker » Mon Apr 03, 2023 11:41 am

You are right... Dumb mistake by my side. :roll: :oops:
I wonder how it doesn't crash. Also I don' t know why I decommented that line, I was actually not using it before posting this question, and yeah, if it works the same as it is escribed in the link you posted it is a bit dumb... I thought realloc would free the portion not being used, but in hinsight, it doesn't matter, as after it gets out of the function, the whole array is sent and freed.

Anyway, I have set a test condition that looks if the ssid or rssi are valid, and if not, it will just break the while and send the received data.

Code: Select all

if((ap_info[i].ssid[0] == '\0') | (ap_info[i].rssi >= 0))break;
Also I have set an ESP_ERROR_CHECK (which I know it is not desirable, and I will remove them, when the device goes into production) So that in case there is a esp_err_t https://docs.espressif.com/projects/esp ... _config_tb that comes out, I am able to see it

Who is online

Users browsing this forum: Kavya S and 113 guests