Skip to content

Commit

Permalink
[Telink] Alignment with NRF/Zephyr platform
Browse files Browse the repository at this point in the history
  • Loading branch information
s07641069 committed Jun 6, 2024
1 parent 0dcdb40 commit e2ac874
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
7 changes: 3 additions & 4 deletions src/platform/telink/wifi/TelinkWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,10 @@ void TelinkWiFiDriver::LoadFromStorage()
mStagingNetwork = network;
}

void TelinkWiFiDriver::OnScanWiFiNetworkDone(WiFiManager::WiFiRequestStatus status)
void TelinkWiFiDriver::OnScanWiFiNetworkDone(const WiFiManager::ScanDoneStatus & status)
{
VerifyOrReturn(mScanCallback != nullptr);
mScanCallback->OnFinished(status == WiFiManager::WiFiRequestStatus::SUCCESS ? Status::kSuccess : Status::kUnknownError,
CharSpan(), &mScanResponseIterator);
mScanCallback->OnFinished(status ? Status::kUnknownError : Status::kSuccess, CharSpan(), &mScanResponseIterator);
mScanCallback = nullptr;
}

Expand All @@ -282,7 +281,7 @@ void TelinkWiFiDriver::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * ca
mScanCallback = callback;
CHIP_ERROR error = WiFiManager::Instance().Scan(
ssid, [](const WiFiScanResponse & response) { Instance().OnScanWiFiNetworkResult(response); },
[](WiFiManager::WiFiRequestStatus status) { Instance().OnScanWiFiNetworkDone(status); });
[](const WiFiManager::ScanDoneStatus & status) { Instance().OnScanWiFiNetworkDone(status); });

if (error != CHIP_NO_ERROR)
{
Expand Down
2 changes: 1 addition & 1 deletion src/platform/telink/wifi/TelinkWiFiDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TelinkWiFiDriver final : public WiFiDriver

void OnNetworkStatusChanged(Status status);
void OnScanWiFiNetworkResult(const WiFiScanResponse & result);
void OnScanWiFiNetworkDone(WiFiManager::WiFiRequestStatus status);
void OnScanWiFiNetworkDone(const WiFiManager::ScanDoneStatus & status);

void StartDefaultWiFiNetwork(void);

Expand Down
58 changes: 39 additions & 19 deletions src/platform/telink/wifi/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ const Map<wifi_iface_state, WiFiManager::StationStatus, 10>
{ WIFI_STATE_GROUP_HANDSHAKE, WiFiManager::StationStatus::PROVISIONING },
{ WIFI_STATE_COMPLETED, WiFiManager::StationStatus::FULLY_PROVISIONED } });

const Map<uint32_t, WiFiManager::NetEventHandler, 5>
WiFiManager::sEventHandlerMap({ { NET_EVENT_WIFI_SCAN_RESULT, WiFiManager::ScanResultHandler },
{ NET_EVENT_WIFI_SCAN_DONE, WiFiManager::ScanDoneHandler },
{ NET_EVENT_WIFI_CONNECT_RESULT, WiFiManager::ConnectHandler },
{ NET_EVENT_WIFI_DISCONNECT_RESULT, WiFiManager::DisconnectHandler } });
const Map<uint32_t, WiFiManager::NetEventHandler, 5> WiFiManager::sEventHandlerMap({
{ NET_EVENT_WIFI_SCAN_RESULT, WiFiManager::ScanResultHandler },
{ NET_EVENT_WIFI_SCAN_DONE, WiFiManager::ScanDoneHandler },
{ NET_EVENT_WIFI_CONNECT_RESULT, WiFiManager::ConnectHandler },
{ NET_EVENT_WIFI_DISCONNECT_RESULT, WiFiManager::DisconnectHandler },
});

void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface)
{
Expand All @@ -138,7 +139,7 @@ void WiFiManager::WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mg
Platform::UniquePtr<uint8_t> eventData(new uint8_t[cb->info_length]);
VerifyOrReturn(eventData);
memcpy(eventData.get(), cb->info, cb->info_length);
sEventHandlerMap[mgmtEvent](std::move(eventData));
sEventHandlerMap[mgmtEvent](std::move(eventData), cb->info_length);
}
}

Expand Down Expand Up @@ -270,10 +271,13 @@ CHIP_ERROR WiFiManager::GetNetworkStatistics(NetworkStatistics & stats) const
return CHIP_NO_ERROR;
}

void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> data)
void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> data, size_t length)
{
// Validate that input data size matches the expected one.
VerifyOrReturn(length == sizeof(wifi_scan_result));

// Contrary to other handlers, offload accumulating of the scan results from the CHIP thread to the caller's thread
const struct wifi_scan_result * scanResult = reinterpret_cast<const struct wifi_scan_result *>(data.get());
const wifi_scan_result * scanResult = reinterpret_cast<const wifi_scan_result *>(data.get());

if (Instance().mInternalScan &&
Instance().mWantedNetwork.GetSsidSpan().data_equal(ByteSpan(scanResult->ssid, scanResult->ssid_length)))
Expand Down Expand Up @@ -304,6 +308,7 @@ void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> data)
Instance().mWiFiParams.mParams.timeout = Instance().mHandling.mConnectionTimeout.count();
Instance().mWiFiParams.mParams.channel = WIFI_CHANNEL_ANY;
Instance().mWiFiParams.mRssi = scanResult->rssi;
Instance().mWiFiParams.mParams.band = WIFI_FREQ_BAND_UNKNOWN;
Instance().mSsidFound = true;
}
}
Expand All @@ -314,26 +319,29 @@ void WiFiManager::ScanResultHandler(Platform::UniquePtr<uint8_t> data)
}
}

void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> data)
void WiFiManager::ScanDoneHandler(Platform::UniquePtr<uint8_t> data, size_t length)
{
// Validate that input data size matches the expected one.
VerifyOrReturn(length == sizeof(wifi_status));

CHIP_ERROR err = SystemLayer().ScheduleLambda([capturedData = data.get()] {
Platform::UniquePtr<uint8_t> safePtr(capturedData);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);
WiFiRequestStatus requestStatus = static_cast<WiFiRequestStatus>(status->status);
uint8_t * rawData = safePtr.get();
const wifi_status * status = reinterpret_cast<const wifi_status *>(rawData);
ScanDoneStatus scanDoneStatus = status->status;

if (requestStatus == WiFiRequestStatus::FAILURE)
if (scanDoneStatus)
{
ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", status->status);
ChipLogError(DeviceLayer, "Wi-Fi scan finalization failure (%d)", scanDoneStatus);
}
else
{
ChipLogProgress(DeviceLayer, "Wi-Fi scan done (%d)", status->status);
ChipLogProgress(DeviceLayer, "Wi-Fi scan done");
}

if (Instance().mScanDoneCallback && !Instance().mInternalScan)
{
Instance().mScanDoneCallback(requestStatus);
Instance().mScanDoneCallback(scanDoneStatus);
// restore the connection state from before the scan request was issued
Instance().mWiFiState = Instance().mCachedWiFiState;
return;
Expand Down Expand Up @@ -393,8 +401,11 @@ void WiFiManager::SendRouterSolicitation(System::Layer * layer, void * param)
}
}

void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data, size_t length)
{
// Validate that input data size matches the expected one.
VerifyOrReturn(length == sizeof(wifi_status));

CHIP_ERROR err = SystemLayer().ScheduleLambda([capturedData = data.get()] {
Platform::UniquePtr<uint8_t> safePtr(capturedData);
uint8_t * rawData = safePtr.get();
Expand Down Expand Up @@ -446,13 +457,22 @@ void WiFiManager::ConnectHandler(Platform::UniquePtr<uint8_t> data)
}
}

void WiFiManager::DisconnectHandler(Platform::UniquePtr<uint8_t>)
void WiFiManager::DisconnectHandler(Platform::UniquePtr<uint8_t> data, size_t length)
{
SystemLayer().ScheduleLambda([] {
// Validate that input data size matches the expected one.
VerifyOrReturn(length == sizeof(wifi_status));

CHIP_ERROR err = SystemLayer().ScheduleLambda([] {
ChipLogProgress(DeviceLayer, "WiFi station disconnected");
Instance().mWiFiState = WIFI_STATE_DISCONNECTED;
Instance().PostConnectivityStatusChange(kConnectivity_Lost);
});

if (CHIP_NO_ERROR == err)
{
// the ownership has been transferred to the worker thread - release the buffer
data.release();
}
}

WiFiManager::StationStatus WiFiManager::GetStationStatus() const
Expand Down
15 changes: 8 additions & 7 deletions src/platform/telink/wifi/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ class WiFiManager
TERMINATED = 2
};

using ScanDoneStatus = decltype(wifi_status::status);
using ScanResultCallback = void (*)(const NetworkCommissioning::WiFiScanResponse &);
using ScanDoneCallback = void (*)(WiFiRequestStatus);
using ScanDoneCallback = void (*)(const ScanDoneStatus &);
using ConnectionCallback = void (*)();

enum class StationStatus : uint8_t
Expand Down Expand Up @@ -183,7 +184,7 @@ class WiFiManager
CHIP_ERROR SetLowPowerMode(bool onoff);

private:
using NetEventHandler = void (*)(Platform::UniquePtr<uint8_t>);
using NetEventHandler = void (*)(Platform::UniquePtr<uint8_t>, size_t);

struct ConnectionParams
{
Expand All @@ -196,10 +197,10 @@ class WiFiManager

// Event handling
static void WifiMgmtEventHandler(net_mgmt_event_callback * cb, uint32_t mgmtEvent, net_if * iface);
static void ScanResultHandler(Platform::UniquePtr<uint8_t> data);
static void ScanDoneHandler(Platform::UniquePtr<uint8_t> data);
static void ConnectHandler(Platform::UniquePtr<uint8_t> data);
static void DisconnectHandler(Platform::UniquePtr<uint8_t> data);
static void ScanResultHandler(Platform::UniquePtr<uint8_t> data, size_t length);
static void ScanDoneHandler(Platform::UniquePtr<uint8_t> data, size_t length);
static void ConnectHandler(Platform::UniquePtr<uint8_t> data, size_t length);
static void DisconnectHandler(Platform::UniquePtr<uint8_t> data, size_t length);
static void PostConnectivityStatusChange(ConnectivityChange changeType);
static void SendRouterSolicitation(System::Layer * layer, void * param);

Expand All @@ -218,7 +219,7 @@ class WiFiManager

net_if * mNetIf{ nullptr };
ConnectionParams mWiFiParams{};
ConnectionHandling mHandling;
ConnectionHandling mHandling{};
wifi_iface_state mWiFiState;
wifi_iface_state mCachedWiFiState;
net_mgmt_event_callback mWiFiMgmtClbk{};
Expand Down

0 comments on commit e2ac874

Please sign in to comment.