Skip to content

Commit

Permalink
[Inet] Add locking mechanism in UDPEndpointImplOpenThread (#18003)
Browse files Browse the repository at this point in the history
* Add locking mechanism in UDPEndpointImplOpenThread
  • Loading branch information
jepenven-silabs authored and pull[bot] committed Jan 30, 2024
1 parent 6729ffd commit 72558ef
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 16 deletions.
21 changes: 21 additions & 0 deletions examples/light-switch-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#include <mbedtls/platform.h>
#if CHIP_ENABLE_OPENTHREAD
#include <inet/EndPointStateOpenThread.h>
#include <openthread/cli.h>
#include <openthread/dataset.h>
#include <openthread/error.h>
Expand Down Expand Up @@ -108,7 +109,20 @@ extern "C" void vApplicationIdleHook(void)
// Check CHIP Config nvm3 and repack flash if necessary.
Internal::EFR32Config::RepackNvm3Flash();
}
#if CHIP_ENABLE_OPENTHREAD
// ================================================================================
// Matter Networking Callbacks
// ================================================================================
void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}

void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}
#endif
// ================================================================================
// Main Code
// ================================================================================
Expand Down Expand Up @@ -169,6 +183,13 @@ int main(void)
// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
#if CHIP_ENABLE_OPENTHREAD
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
#endif
chip::Server::GetInstance().Init(initParams);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

Expand Down
22 changes: 22 additions & 0 deletions examples/lighting-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#include <mbedtls/platform.h>
#if CHIP_ENABLE_OPENTHREAD
#include <inet/EndPointStateOpenThread.h>
#include <openthread/cli.h>
#include <openthread/dataset.h>
#include <openthread/error.h>
Expand Down Expand Up @@ -113,6 +114,20 @@ extern "C" void vApplicationIdleHook(void)
Internal::EFR32Config::RepackNvm3Flash();
}

#if CHIP_ENABLE_OPENTHREAD
// ================================================================================
// Matter Networking Callbacks
// ================================================================================
void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}

void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}
#endif
// ================================================================================
// Main Code
// ================================================================================
Expand Down Expand Up @@ -173,6 +188,13 @@ int main(void)
// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
#if CHIP_ENABLE_OPENTHREAD
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
#endif
chip::Server::GetInstance().Init(initParams);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

Expand Down
17 changes: 17 additions & 0 deletions examples/lighting-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>

#include <inet/EndPointStateOpenThread.h>

#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>

Expand Down Expand Up @@ -72,6 +74,16 @@ namespace {
constexpr int extDiscTimeoutSecs = 20;
}

void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}

void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}

CHIP_ERROR AppTask::StartAppTask()
{
sAppEventQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
Expand Down Expand Up @@ -115,6 +127,11 @@ CHIP_ERROR AppTask::Init()
// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
chip::Server::GetInstance().Init(initParams);

// Init OTA engine
Expand Down
21 changes: 21 additions & 0 deletions examples/lock-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#include <mbedtls/platform.h>
#if CHIP_ENABLE_OPENTHREAD
#include <inet/EndPointStateOpenThread.h>
#include <openthread/cli.h>
#include <openthread/dataset.h>
#include <openthread/error.h>
Expand Down Expand Up @@ -112,7 +113,20 @@ extern "C" void vApplicationIdleHook(void)
// Check CHIP Config nvm3 and repack flash if necessary.
Internal::EFR32Config::RepackNvm3Flash();
}
#if CHIP_ENABLE_OPENTHREAD
// ================================================================================
// Matter Networking Callbacks
// ================================================================================
void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}

void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}
#endif
// ================================================================================
// Main Code
// ================================================================================
Expand Down Expand Up @@ -173,6 +187,13 @@ int main(void)
// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
#if CHIP_ENABLE_OPENTHREAD
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
#endif
chip::Server::GetInstance().Init(initParams);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

Expand Down
17 changes: 17 additions & 0 deletions examples/lock-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>

#include <inet/EndPointStateOpenThread.h>

#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>

Expand Down Expand Up @@ -74,6 +76,16 @@ namespace {
constexpr int extDiscTimeoutSecs = 20;
}

void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}

void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}

CHIP_ERROR AppTask::StartAppTask()
{
sAppEventQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
Expand Down Expand Up @@ -119,6 +131,11 @@ CHIP_ERROR AppTask::Init()
// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
chip::Server::GetInstance().Init(initParams);

// Init OTA engine
Expand Down
21 changes: 21 additions & 0 deletions examples/window-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <mbedtls/platform.h>
#if CHIP_ENABLE_OPENTHREAD
#include <inet/EndPointStateOpenThread.h>
#include <openthread/cli.h>
#include <openthread/dataset.h>
#include <openthread/error.h>
Expand Down Expand Up @@ -84,7 +85,20 @@ void appError(CHIP_ERROR err)
while (1)
;
}
#if CHIP_ENABLE_OPENTHREAD
// ================================================================================
// Matter Networking Callbacks
// ================================================================================
void LockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}

void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
}
#endif
// ================================================================================
// Main Code
// ================================================================================
Expand Down Expand Up @@ -145,6 +159,13 @@ int main(void)
// Init ZCL Data Model
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
#if CHIP_ENABLE_OPENTHREAD
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
#endif
chip::Server::GetInstance().Init(initParams);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();

Expand Down
4 changes: 1 addition & 3 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
err = mTransports.Init(UdpListenParameters(DeviceLayer::UDPEndPointManager())
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(mOperationalServicePort)
#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
.SetNativeParams(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance())
#endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
.SetNativeParams(initParams.endpointNativeParams)

#if INET_CONFIG_ENABLE_IPV4
,
Expand Down
3 changes: 3 additions & 0 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ struct ServerInitParams
// Access control delegate: MUST be injected. Used to look up access control rules. Must be
// initialized before being provided
Access::AccessControl::Delegate * accessDelegate = nullptr;
// Network native params can be injected depending on the
// selected Endpoint implementation
void * endpointNativeParams = nullptr;
};

/**
Expand Down
24 changes: 24 additions & 0 deletions src/inet/EndPointStateOpenThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ namespace Inet {
*/
class DLL_EXPORT EndPointStateOpenThread
{
public:
typedef void (*openThreadTaskSyncCb)(void);
struct OpenThreadEndpointInitParam
{
openThreadTaskSyncCb lockCb = nullptr;
openThreadTaskSyncCb unlockCb = nullptr;
otInstance * openThreadInstancePtr = nullptr;
};

protected:
EndPointStateOpenThread() : mOpenThreadEndPointType(OpenThreadEndPointType::Unknown) {}

Expand All @@ -43,6 +52,21 @@ class DLL_EXPORT EndPointStateOpenThread
UDP = 1,
TCP = 2
} mOpenThreadEndPointType;

void LockOpenThread(void)
{
if (lockOpenThread != nullptr)
lockOpenThread();
}
void UnlockOpenThread(void)
{
if (unlockOpenThread != nullptr)
unlockOpenThread();
}

otInstance * mOTInstance = nullptr;
openThreadTaskSyncCb lockOpenThread = nullptr;
openThreadTaskSyncCb unlockOpenThread = nullptr;
};

} // namespace Inet
Expand Down
2 changes: 1 addition & 1 deletion src/inet/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ otIp6Address IPAddress::ToIPv6() const
return otAddr;
}

IPAddress IPAddress::FromOtAddr(otIp6Address & address)
IPAddress IPAddress::FromOtAddr(const otIp6Address & address)
{
IPAddress addr;
static_assert(sizeof(address.mFields.m32) == sizeof(addr), "otIp6Address size mismatch");
Expand Down
2 changes: 1 addition & 1 deletion src/inet/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class DLL_EXPORT IPAddress

#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
otIp6Address ToIPv6() const;
static IPAddress FromOtAddr(otIp6Address & address);
static IPAddress FromOtAddr(const otIp6Address & address);
#endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT

/**
Expand Down
Loading

0 comments on commit 72558ef

Please sign in to comment.