Skip to content

Commit

Permalink
Handle Thread Association Request in rendezvous session (#3303)
Browse files Browse the repository at this point in the history
* [thread] Add method to retrieve SLAAC IPv6 address

* [transport] Handle Thread Association request

Currently, NetworkProvisioning class, which takes part in
rendezvous over BLE session, only supports WiFi provisioning
and Thread provisioning is partially implemented in
RendezvousServer (Application layer). Move the Thread
provisioning code to the BLE transport layer to process
both network types in the same way.

Also reply to Thread Association request with SLAAC/On-Mesh
IPv6 Address once that is assigned so that the controller
knows that provisioning completed successfully and how to
reach the device.

* [nrfconnect] Initialize memory allocator

More and more CHIP components rely on CHIP memory allocator.
Also PlatformManager::AddEventHandler() used in this PR
needs dynamic memory allocation. Run MemoryInit() on
the app startup.

* [esp32] Fix build

* Fix typo and use more accurate error code

* Let RendezvousServer implement DeviceNetworkProvisioningDelegate

* Fix after rebase
  • Loading branch information
Damian-Nordic authored Oct 26, 2020
1 parent 5ba0597 commit 960cfe0
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 79 deletions.
72 changes: 10 additions & 62 deletions examples/common/chip-app-server/RendezvousServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#if CHIP_ENABLE_OPENTHREAD
#include <platform/ThreadStackManager.h>
#include <platform/internal/DeviceNetworkInfo.h>
#endif

using namespace ::chip::Inet;
Expand All @@ -34,7 +33,7 @@ using namespace ::chip::DeviceLayer;

namespace chip {

RendezvousServer::RendezvousServer() : mRendezvousSession(this) {}
RendezvousServer::RendezvousServer() : mRendezvousSession(this, this) {}

CHIP_ERROR RendezvousServer::Init(const RendezvousParameters & params)
{
Expand All @@ -58,66 +57,6 @@ void RendezvousServer::OnRendezvousConnectionClosed()

void RendezvousServer::OnRendezvousMessageReceived(PacketBuffer * buffer)
{
#if CHIP_ENABLE_OPENTHREAD
uint16_t bufferLen = buffer->DataLength();
uint8_t * data = buffer->Start();
chip::DeviceLayer::Internal::DeviceNetworkInfo networkInfo;
ChipLogProgress(AppServer, "Receive BLE message size=%u", bufferLen);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadNetworkName),
ChipLogProgress(AppServer, "Invalid network provision message"));
memcpy(networkInfo.ThreadNetworkName, data, sizeof(networkInfo.ThreadNetworkName));
data += sizeof(networkInfo.ThreadNetworkName);
bufferLen -= sizeof(networkInfo.ThreadNetworkName);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadExtendedPANId),
ChipLogProgress(AppServer, "Invalid network provision message"));
memcpy(networkInfo.ThreadExtendedPANId, data, sizeof(networkInfo.ThreadExtendedPANId));
data += sizeof(networkInfo.ThreadExtendedPANId);
bufferLen -= sizeof(networkInfo.ThreadExtendedPANId);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadMeshPrefix),
ChipLogProgress(AppServer, "Invalid network provision message"));
memcpy(networkInfo.ThreadMeshPrefix, data, sizeof(networkInfo.ThreadMeshPrefix));
data += sizeof(networkInfo.ThreadMeshPrefix);
bufferLen -= sizeof(networkInfo.ThreadMeshPrefix);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadMasterKey), ChipLogProgress(AppServer, "Invalid network provision message"));
memcpy(networkInfo.ThreadMasterKey, data, sizeof(networkInfo.ThreadMasterKey));
data += sizeof(networkInfo.ThreadMasterKey);
bufferLen -= sizeof(networkInfo.ThreadMasterKey);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadPSKc), ChipLogProgress(AppServer, "Invalid network provision message"));
memcpy(networkInfo.ThreadPSKc, data, sizeof(networkInfo.ThreadPSKc));
data += sizeof(networkInfo.ThreadPSKc);
bufferLen -= sizeof(networkInfo.ThreadPSKc);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadPANId), ChipLogProgress(AppServer, "Invalid network provision message"));
networkInfo.ThreadPANId = data[0] | (data[1] << 8);
data += sizeof(networkInfo.ThreadPANId);
bufferLen -= sizeof(networkInfo.ThreadPANId);

VerifyOrExit(bufferLen >= sizeof(networkInfo.ThreadChannel), ChipLogProgress(AppServer, "Invalid network provision message"));
networkInfo.ThreadChannel = data[0];
data += sizeof(networkInfo.ThreadChannel);
bufferLen -= sizeof(networkInfo.ThreadChannel);

VerifyOrExit(bufferLen >= 3, ChipLogProgress(AppServer, "Invalid network provision message"));
networkInfo.FieldPresent.ThreadExtendedPANId = *data;
data++;
networkInfo.FieldPresent.ThreadMeshPrefix = *data;
data++;
networkInfo.FieldPresent.ThreadPSKc = *data;
data++;
networkInfo.NetworkId = 0;
networkInfo.FieldPresent.NetworkId = true;

ThreadStackMgr().SetThreadEnabled(false);
ThreadStackMgr().SetThreadProvision(networkInfo);
ThreadStackMgr().SetThreadEnabled(true);

#endif
exit:
chip::System::PacketBuffer::Free(buffer);
}

Expand All @@ -142,4 +81,13 @@ void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)
return;
}

void RendezvousServer::ProvisionThread(const DeviceLayer::Internal::DeviceNetworkInfo & threadData)
{
#if CHIP_ENABLE_OPENTHREAD
ThreadStackMgr().SetThreadEnabled(false);
ThreadStackMgr().SetThreadProvision(threadData);
ThreadStackMgr().SetThreadEnabled(true);
#endif // CHIP_ENABLE_OPENTHREAD
}

} // namespace chip
8 changes: 7 additions & 1 deletion examples/common/chip-app-server/include/RendezvousServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@

namespace chip {

class RendezvousServer : public RendezvousSessionDelegate
class RendezvousServer : public RendezvousSessionDelegate, public DeviceNetworkProvisioningDelegate
{
public:
RendezvousServer();

CHIP_ERROR Init(const RendezvousParameters & params);

//////////////// RendezvousSessionDelegate Implementation ///////////////////

void OnRendezvousConnectionOpened() override;
void OnRendezvousConnectionClosed() override;
void OnRendezvousError(CHIP_ERROR err) override;
void OnRendezvousMessageReceived(System::PacketBuffer * buffer) override;
void OnRendezvousStatusUpdate(Status status, CHIP_ERROR err) override;

//////////// DeviceNetworkProvisioningDelegate Implementation ///////////////

void ProvisionThread(const DeviceLayer::Internal::DeviceNetworkInfo & threadData) override;

private:
RendezvousSession mRendezvousSession;
};
Expand Down
8 changes: 8 additions & 0 deletions examples/lighting-app/nrfconnect/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "AppTask.h"

#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPMem.h>

#include <logging/log.h>

Expand All @@ -34,6 +35,13 @@ int main(void)

k_thread_priority_set(k_current_get(), K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1));

ret = chip::Platform::MemoryInit();
if (ret != CHIP_NO_ERROR)
{
LOG_ERR("Platform::MemoryInit() failed");
goto exit;
}

LOG_INF("Init CHIP stack");
ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
Expand Down
8 changes: 8 additions & 0 deletions examples/lock-app/nrfconnect/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <logging/log.h>

#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPMem.h>

LOG_MODULE_REGISTER(app);

Expand All @@ -35,6 +36,13 @@ int main()

k_thread_priority_set(k_current_get(), K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1));

ret = chip::Platform::MemoryInit();
if (ret != CHIP_NO_ERROR)
{
LOG_ERR("Platform::MemoryInit() failed");
goto exit;
}

LOG_INF("Init CHIP stack");
ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

using namespace ::chip;

void ESP32NetworkProvisioningDelegate::ProvisionNetwork(const char * ssid, const char * key)
void ESP32NetworkProvisioningDelegate::ProvisionWiFi(const char * ssid, const char * key)
{
ChipLogProgress(NetworkProvisioning, "ESP32NetworkProvisioningDelegate: SSID: %s, key: %s", ssid, key);
SetWiFiStationProvisioning(ssid, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ class ESP32NetworkProvisioningDelegate : public DeviceNetworkProvisioningDelegat
* @param ssid WiFi SSID
* @param passwd WiFi password
*/
void ProvisionNetwork(const char * ssid, const char * passwd) override;
void ProvisionWiFi(const char * ssid, const char * passwd) override;
};
6 changes: 6 additions & 0 deletions src/include/platform/ThreadStackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ThreadStackManager
CHIP_ERROR GetAndLogThreadTopologyMinimal();
CHIP_ERROR GetAndLogThreadTopologyFull();
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf);
CHIP_ERROR GetSlaacIPv6Address(chip::Inet::IPAddress & addr);

CHIP_ERROR JoinerStart();
CHIP_ERROR SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo);
Expand Down Expand Up @@ -305,6 +306,11 @@ inline CHIP_ERROR ThreadStackManager::GetPrimary802154MACAddress(uint8_t * buf)
return static_cast<ImplClass *>(this)->_GetPrimary802154MACAddress(buf);
}

inline CHIP_ERROR ThreadStackManager::GetSlaacIPv6Address(chip::Inet::IPAddress & addr)
{
return static_cast<ImplClass *>(this)->_GetSlaacIPv6Address(addr);
}

inline CHIP_ERROR ThreadStackManager::JoinerStart()
{
return static_cast<ImplClass *>(this)->_JoinerStart();
Expand Down
5 changes: 5 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ CHIP_ERROR ThreadStackManagerImpl::_GetPrimary802154MACAddress(uint8_t * buf)
return OTBR_TO_CHIP_ERROR(error);
}

CHIP_ERROR ThreadStackManagerImpl::_GetSlaacIPv6Address(chip::Inet::IPAddress & addr)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR ThreadStackManagerImpl::_JoinerStart()
{
return CHIP_ERROR_NOT_IMPLEMENTED;
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class ThreadStackManagerImpl : public ThreadStackManager

CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);

CHIP_ERROR _GetSlaacIPv6Address(chip::Inet::IPAddress & addr);

CHIP_ERROR _JoinerStart();

~ThreadStackManagerImpl() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,21 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_GetPrimary80215
return CHIP_NO_ERROR;
};

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_GetSlaacIPv6Address(chip::Inet::IPAddress & addr)
{
for (const otNetifAddress * otAddr = otIp6GetUnicastAddresses(mOTInst); otAddr != nullptr; otAddr = otAddr->mNext)
{
if (otAddr->mValid && otAddr->mAddressOrigin == OT_ADDRESS_ORIGIN_SLAAC)
{
addr = ToIPAddress(otAddr->mAddress);
return CHIP_NO_ERROR;
}
}

return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstance * otInst)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class GenericThreadStackManagerImpl_OpenThread
CHIP_ERROR _GetAndLogThreadTopologyMinimal(void);
CHIP_ERROR _GetAndLogThreadTopologyFull(void);
CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);
CHIP_ERROR _GetSlaacIPv6Address(chip::Inet::IPAddress & addr);
void _OnWoBLEAdvertisingStart(void);
void _OnWoBLEAdvertisingStop(void);

Expand Down
Loading

0 comments on commit 960cfe0

Please sign in to comment.