Skip to content

Commit

Permalink
support Thread Scan Network (project-chip#15105)
Browse files Browse the repository at this point in the history
fix malloc and release issue

Cleanup + restyle
  • Loading branch information
jmartinez-silabs authored and fuxingguo16 committed Apr 26, 2022
1 parent a3aa65b commit 5c320b5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
48 changes: 45 additions & 3 deletions src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,48 @@ namespace chip {
namespace DeviceLayer {
namespace NetworkCommissioning {

template <typename T>
class otScanResponseIterator : public Iterator<T>
{
public:
otScanResponseIterator(T * apScanResponse) : mpScanResponse(apScanResponse) {}
size_t Count() override { return itemCount; }
bool Next(T & item) override
{
if (mpScanResponse == nullptr || currentIterating >= itemCount)
{
return false;
}
item = mpScanResponse[currentIterating];
currentIterating++;
return true;
}
void Release() override
{
itemCount = currentIterating = 0;
Platform::MemoryFree(mpScanResponse);
mpScanResponse = nullptr;
}

void Add(T * pResponse)
{
size_t tempCount = itemCount + 1;
mpScanResponse = static_cast<T *>(Platform::MemoryRealloc(mpScanResponse, kItemSize * tempCount));
if (mpScanResponse)
{
// first item at index. update after the copy.
memcpy(&(mpScanResponse[itemCount]), pResponse, kItemSize);
itemCount = tempCount;
}
}

private:
size_t currentIterating = 0;
size_t itemCount = 0;
size_t kItemSize = sizeof(T);
T * mpScanResponse;
};

class GenericThreadDriver final : public ThreadDriver
{
public:
Expand Down Expand Up @@ -62,9 +104,9 @@ class GenericThreadDriver final : public ThreadDriver
void ScanNetworks(ScanCallback * callback) override;

private:
ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this);
Thread::OperationalDataset mSavedNetwork;
Thread::OperationalDataset mStagingNetwork;
ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this);
Thread::OperationalDataset mSavedNetwork = {};
Thread::OperationalDataset mStagingNetwork = {};
};

} // namespace NetworkCommissioning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ void initNetworkCommissioningThreadDriver(void)
#endif
}

NetworkCommissioning::ThreadScanResponse * scanResult;
otScanResponseIterator<NetworkCommissioning::ThreadScanResponse> mScanResponseIter(scanResult);
} // namespace
// Fully instantiate the generic implementation class in whatever compilation unit includes this file.
template class GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>;
Expand Down Expand Up @@ -359,11 +361,55 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnThreadAttachFinishe
template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_StartThreadScan(ThreadDriver::ScanCallback * callback)
{
// TODO END scan feature + _OnNetworkScanFinished callback for response
// There is another ongoing scan request, reject the new one.
// If there is another ongoing scan request, reject the new one.
VerifyOrReturnError(mpScanCallback == nullptr, CHIP_ERROR_INCORRECT_STATE);
mpScanCallback = callback;
return CHIP_NO_ERROR;
CHIP_ERROR err = MapOpenThreadError(otLinkActiveScan(mOTInst, 0, /* all channels */
0, /* default value `kScanDurationDefault` = 300 ms. */
_OnNetworkScanFinished, this));
return err;
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnNetworkScanFinished(otActiveScanResult * aResult, void * aContext)
{
reinterpret_cast<GenericThreadStackManagerImpl_OpenThread *>(aContext)->_OnNetworkScanFinished(aResult);
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnNetworkScanFinished(otActiveScanResult * aResult)
{
if (aResult == nullptr) // scan completed
{
if (mpScanCallback != nullptr)
{
DeviceLayer::SystemLayer().ScheduleLambda([this]() {
mpScanCallback->OnFinished(NetworkCommissioning::Status::kSuccess, CharSpan(), &mScanResponseIter);
mpScanCallback = nullptr;
});
}
}
else
{
ChipLogProgress(
DeviceLayer,
"Thread Network: %s Panid 0x%" PRIx16 " Channel %" PRIu16 " RSSI %" PRId16 " LQI %" PRIu16 " Version %" PRIu16,
aResult->mNetworkName.m8, aResult->mPanId, aResult->mChannel, aResult->mRssi, aResult->mLqi, aResult->mVersion);

NetworkCommissioning::ThreadScanResponse scanResponse = { 0 };

scanResponse.panId = aResult->mPanId; // why is scanResponse.panID 64b
scanResponse.channel = aResult->mChannel; // why is scanResponse.channel 16b
scanResponse.version = aResult->mVersion;
scanResponse.rssi = aResult->mRssi;
scanResponse.lqi = aResult->mLqi;
scanResponse.extendedAddress = Encoding::BigEndian::Get64(aResult->mExtAddress.m8);
scanResponse.extendedPanId = Encoding::BigEndian::Get64(aResult->mExtendedPanId.m8);
scanResponse.networkNameLen = strnlen(aResult->mNetworkName.m8, OT_NETWORK_NAME_MAX_SIZE);
memcpy(scanResponse.networkName, aResult->mNetworkName.m8, scanResponse.networkNameLen);

mScanResponseIter.Add(&scanResponse);
}
}

template <class ImplClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#pragma once

#include <openthread/instance.h>
#include <openthread/link.h>
#include <openthread/netdata.h>

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
Expand Down Expand Up @@ -89,6 +90,8 @@ class GenericThreadStackManagerImpl_OpenThread
ConnectivityManager::ThreadDeviceType _GetThreadDeviceType(void);
CHIP_ERROR _SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType);
CHIP_ERROR _StartThreadScan(NetworkCommissioning::ThreadDriver::ScanCallback * callback);
static void _OnNetworkScanFinished(otActiveScanResult * aResult, void * aContext);
void _OnNetworkScanFinished(otActiveScanResult * aResult);

#if CHIP_DEVICE_CONFIG_ENABLE_SED
CHIP_ERROR _GetSEDPollingConfig(ConnectivityManager::SEDPollingConfig & pollingConfig);
Expand Down

0 comments on commit 5c320b5

Please sign in to comment.