Files
libretiny/cores/realtek-amb/arduino/libraries/WiFi/WiFiScan.cpp
Piotr Szulc 1d80b5fff7 [beken-72xx] Free list returned by wlan_sta_scan_result() (#226)
* Free list returned by wlan_sta_scan_result()

* scanAlloc improvements

There were a few things I didn't like about this function:
1) realloc() was called a bit too often.
2) if realloc() failed, the previous memory was not freed.
3) scanAlloc returned previous count or 255 on error. But there was no real check for error and 255 could've been used as index to null. I think it's better to simple return boolean.
4) scanAlloc was clearing memory only up to (and excluding) the new entries.

* Corrected clearing new entries in scanAlloc

* scanAlloc() now returns number of allocated items

* Fixed compilation issues related to goto.
2024-01-06 19:41:01 +01:00

60 lines
1.5 KiB
C++

/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
#include "WiFiPrivate.h"
static rtw_result_t scanHandler(rtw_scan_handler_result_t *result) {
WiFiClass *cls = (WiFiClass *)result->user_data;
WiFiScanData *scan = cls->scan;
if (!scan)
return RTW_SUCCESS;
if (result->scan_complete == RTW_TRUE) {
scan->running = false;
xSemaphoreGive(cDATA->scanSem);
return RTW_SUCCESS;
}
rtw_scan_result_t *net = &result->ap_details;
net->SSID.val[net->SSID.len] = '\0';
if (!net->SSID.len)
return RTW_SUCCESS;
uint8_t last = scan->count + 1;
if (cls->scanAlloc(last) < last) {
return RTW_SUCCESS;
}
scan->ap[last].ssid = strdup((char *)net->SSID.val);
scan->ap[last].auth = securityTypeToAuthMode(net->security);
scan->ap[last].rssi = net->signal_strength;
scan->ap[last].channel = net->channel;
memcpy(scan->ap[last].bssid.addr, net->BSSID.octet, ETH_ALEN);
return RTW_SUCCESS;
}
int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint32_t maxMsPerChannel, uint8_t channel) {
if (scan && scan->running)
return WIFI_SCAN_RUNNING;
if (getMode() == WIFI_MODE_NULL)
enableSTA(true);
scanDelete();
scanInit();
LT_IM(WIFI, "Starting WiFi scan");
if (wifi_scan_networks(scanHandler, this) != RTW_SUCCESS)
return WIFI_SCAN_FAILED;
scan->running = true;
if (!async) {
LT_IM(WIFI, "Waiting for results");
xSemaphoreTake(DATA->scanSem, 1); // reset the semaphore quickly
xSemaphoreTake(DATA->scanSem, pdMS_TO_TICKS(maxMsPerChannel * 20));
return scan->count;
}
return WIFI_SCAN_RUNNING;
}