Skip to content

Commit

Permalink
[ESP32]: Update WiFi Scan Network Implementation for New API (project…
Browse files Browse the repository at this point in the history
…-chip#36714)

* esp32: Optimize WiFi Scan Handling with Single AP Record API

* esp32: add separate ScanResponseIterator for new API changes

* esp32: enable ESPScanResponseIterator for compatible IDF versions

* esp32: add call for clear ap scan list, used single name for iterator

* esp32: move iterator release call outside, code refactoring

* esp32: relocate iternum variable for iterator compatibility with older IDF versions

* Make SetApData() method private
  • Loading branch information
pimpalemahesh authored Dec 12, 2024
1 parent 75ab4c9 commit 506f493
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/platform/ESP32/NetworkCommissioningDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,29 @@ void ESPWiFiDriver::OnScanWiFiNetworkDone()
return;
}

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
if (CHIP_NO_ERROR == DeviceLayer::SystemLayer().ScheduleLambda([ap_number]() {
ESPScanResponseIterator iter(ap_number);
if (GetInstance().mpScanCallback)
{
GetInstance().mpScanCallback->OnFinished(Status::kSuccess, CharSpan(), &iter);
GetInstance().mpScanCallback = nullptr;
}
else
{
ChipLogError(DeviceLayer, "can't find the ScanCallback function");
}
iter.Release();
}))
{
}
else
{
ChipLogError(DeviceLayer, "can't schedule the scan result processing");
mpScanCallback->OnFinished(Status::kUnknownError, CharSpan(), nullptr);
mpScanCallback = nullptr;
}
#else
// Since this is the dynamic memory allocation, restrict it to a configured limit
ap_number = std::min(static_cast<uint16_t>(CHIP_DEVICE_CONFIG_MAX_SCAN_NETWORKS_RESULTS), ap_number);

Expand Down Expand Up @@ -430,6 +453,7 @@ void ESPWiFiDriver::OnScanWiFiNetworkDone()
mpScanCallback->OnFinished(Status::kUnknownError, CharSpan(), nullptr);
mpScanCallback = nullptr;
}
#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
}

void ESPWiFiDriver::OnNetworkStatusChange()
Expand Down
46 changes: 34 additions & 12 deletions src/platform/ESP32/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,56 @@ BitFlags<WiFiSecurityBitmap> ConvertSecurityType(wifi_auth_mode_t authMode);
class ESPScanResponseIterator : public Iterator<WiFiScanResponse>
{
public:
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
ESPScanResponseIterator(const size_t size) : mSize(size) {}
#else
ESPScanResponseIterator(const size_t size, const wifi_ap_record_t * scanResults) : mSize(size), mpScanResults(scanResults) {}
#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
size_t Count() override { return mSize; }

bool Next(WiFiScanResponse & item) override
{
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
wifi_ap_record_t ap_record;
VerifyOrReturnValue(esp_wifi_scan_get_ap_record(&ap_record) == ESP_OK, false);
SetApData(item, ap_record);
#else
if (mIternum >= mSize)
{
return false;
}

item.security = ConvertSecurityType(mpScanResults[mIternum].authmode);
static_assert(chip::DeviceLayer::Internal::kMaxWiFiSSIDLength <= UINT8_MAX, "SSID length might not fit in item.ssidLen");
item.ssidLen = static_cast<uint8_t>(
strnlen(reinterpret_cast<const char *>(mpScanResults[mIternum].ssid), chip::DeviceLayer::Internal::kMaxWiFiSSIDLength));
item.channel = mpScanResults[mIternum].primary;
item.wiFiBand = chip::DeviceLayer::NetworkCommissioning::WiFiBand::k2g4;
item.rssi = mpScanResults[mIternum].rssi;
memcpy(item.ssid, mpScanResults[mIternum].ssid, item.ssidLen);
memcpy(item.bssid, mpScanResults[mIternum].bssid, 6);

SetApData(item, mpScanResults[mIternum]);
mIternum++;
#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
return true;
}
void Release() override {}

void Release() override
{
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
esp_wifi_clear_ap_list();
#endif // ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
}

private:
const size_t mSize;
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 3)
const wifi_ap_record_t * mpScanResults;
size_t mIternum = 0;
#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 3)

void SetApData(WiFiScanResponse & item, wifi_ap_record_t ap_record)
{
item.security = ConvertSecurityType(ap_record.authmode);
static_assert(chip::DeviceLayer::Internal::kMaxWiFiSSIDLength <= UINT8_MAX, "SSID length might not fit in item.ssidLen");
item.ssidLen = static_cast<uint8_t>(
strnlen(reinterpret_cast<const char *>(ap_record.ssid), chip::DeviceLayer::Internal::kMaxWiFiSSIDLength));
item.channel = ap_record.primary;
item.wiFiBand = chip::DeviceLayer::NetworkCommissioning::WiFiBand::k2g4;
item.rssi = ap_record.rssi;
memcpy(item.ssid, ap_record.ssid, item.ssidLen);
memcpy(item.bssid, ap_record.bssid, sizeof(item.bssid));
}
};

class ESPWiFiDriver final : public WiFiDriver
Expand Down

0 comments on commit 506f493

Please sign in to comment.