Files
libretiny/cores/lightning-ln882h/arduino/libraries/WiFi/WiFiScan.cpp
lamauny 69e7e2debe [lightning-ln882h] Add support for Lightning LN882H family (#312)
* fix mbedtls bad pointer in function call (prototype mismatch)

* fix issue with weak families functions implemented in static library, it will never be linked. fixed by redefining prototypes inside families

* [ln882x] add support for lightning ln882x & ln882h families

* add i2c (wire) support

* add analog (adc) support

* add watchdog support

* [ln882x] changed default uart 0/1 pins; added board wl2s

* [ln882x] fix IRQ & ADC pins

* [ln882x] boards cosmetic

* [ln882x] wifi sta use otp mac addr by default; re-enabled wifi powersave mode

* [ln882x] clang-format clean code

* [ln882x] clang-format clean code

* Update families.json

* Apply suggestions from code review

* [ln882x] reformat json board files

* [ln882x] os_queue cleanup

* [ln882x] removed Beken auto-download command

* [ln882x] removed personal script file

* [ln882x] removed unusefull pi section in debugging.md

* [ln882x] removed Arduino.h and changed private I2C definition

* [ln882x] updated README.md

* [ln882x] changed pin naming scheme to PA/PB

* [ln882x] clean code

* [ln882x] clean code

* [ln882x] add ota image verification

* Update push-dev.yml

* [ln882x] fix boards ADC missing inputs]

* [ln882x] removed reg_xxx fixup files and use include guards instead

* [ln882x] cleanup code

* [ln882x] cleanup code

* [ln882x] fix lt_init weak functions linking

* [ln882x] revert lt_api.h modification, fixed with previous commit

* [ln882x] setup UF2 firmware for flasher with partitions

* [ln882x] update README.md

* [ln882x] include ln_wifi.h and ln_serial.h to avoid including bad headers on case insensitive systems

* [ln882x] Replace RingBuffer by SerialRingBuffer

* [ln882x] clang-format

* [ln882x] update README.md

* Apply suggestions from code review

* Reformat board JSON files

* Add mkdocs link redirect

* Update ltchiptool to v4.12.0

---------

Co-authored-by: Kuba Szczodrzyński <kuba@szczodrzynski.pl>
2025-03-25 17:26:53 +01:00

115 lines
2.8 KiB
C++

/* Copyright (c) Etienne Le Cousin 2024-03-13. */
#include "WiFiPrivate.h"
static void scanHandler(void *arg) {
WiFiClass *cls = (WiFiClass *)pWiFi;
WiFiScanData *scan = cls->scan;
LT_HEAP_I();
ln_list_t *list;
uint8_t n = 0, node_count = 0;
ap_info_node_t *pnode;
wifi_manager_ap_list_update_enable(LN_FALSE);
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_SCAN_COMPLETE, NULL);
// 1.get ap info list.
if (wifi_manager_get_ap_list(&list, &node_count)) {
LT_EM(WIFI, "Failed to get scan result");
goto end;
}
LT_IM(WIFI, "Found %d APs", node_count);
cls->scanAlloc(node_count);
if (!scan->ap) {
LT_WM(WIFI, "scan->ap alloc failed");
goto end;
}
// 2.get all ap info in the list.
LN_LIST_FOR_EACH_ENTRY(pnode, ap_info_node_t, list, list) {
uint8_t *mac = (uint8_t *)pnode->info.bssid;
ap_info_t *ap_info = &pnode->info;
scan->ap[n].ssid = strdup(ap_info->ssid);
scan->ap[n].auth = securityTypeToAuthMode(ap_info->authmode);
scan->ap[n].rssi = ap_info->rssi;
scan->ap[n].channel = ap_info->channel;
memcpy(scan->ap[n].bssid.addr, mac, 6);
n++;
}
end:
scan->timeout = 0;
if (scan->running) {
// running == false means it was discarded (timeout)
scan->running = false;
xSemaphoreGive(cDATA->scanSem);
// Send event scan finished
EventInfo eventInfo;
memset(&eventInfo, 0, sizeof(EventInfo));
eventInfo.wifi_scan_done.status = 0;
eventInfo.wifi_scan_done.number = scan->count;
pWiFi->postEvent(ARDUINO_EVENT_WIFI_SCAN_DONE, eventInfo);
}
// wifi_manager_ap_list_update_enable(LN_TRUE);
// wifi_sta_disconnect();
LT_HEAP_I();
return;
}
int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint32_t maxMsPerChannel, uint8_t channel) {
if (scan && scan->running) {
if (scan->timeout && millis() > scan->timeout) {
LT_WM(WIFI, "Scan timeout, discarding");
scan->running = false;
} else {
return WIFI_SCAN_RUNNING;
}
}
enableSTA(true);
scanDelete();
scanInit();
LT_IM(WIFI, "Starting WiFi scan");
__wrap_ln_printf_disable();
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_SCAN_COMPLETE, &scanHandler);
wifi_manager_ap_list_update_enable(LN_TRUE);
wifi_manager_cleanup_scan_results();
wifi_scan_cfg_t scan_cfg = {
.channel = 0,
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
.scan_time = 5,
};
wifi_sta_scan(&scan_cfg);
LT_HEAP_I();
scan->running = true;
scan->timeout = millis() + maxMsPerChannel * 20 + 1000;
int16_t ret = WIFI_SCAN_RUNNING;
if (!async) {
LT_IM(WIFI, "Waiting for results");
xSemaphoreTake(DATA->scanSem, 1); // reset the semaphore quickly
xSemaphoreTake(DATA->scanSem, pdMS_TO_TICKS(maxMsPerChannel * 20));
if (scan->running) {
scanDelete();
ret = WIFI_SCAN_FAILED;
goto exit;
}
ret = scan->count;
goto exit;
}
exit:
__wrap_ln_printf_enable();
return ret;
}