Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 097e6a2

Browse files
authored
Merge pull request #597 from erjiaqing/esp32-ot
Add OpenThread Support to ESP32 DeviceLayer using ot-esp32
2 parents 262dcab + 10ab9d3 commit 097e6a2

15 files changed

+353
-16
lines changed

build/esp32/components/openweave/Kconfig

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,14 @@ menu "OpenWeave Device Layer"
393393
The priority of the Weave task.
394394

395395
On the ESP32 platform this value is added to ESP_TASK_PRIO_MIN to determine the FreeRTOS priority value.
396-
396+
397+
config THREAD_TASK_STACK_SIZE
398+
int "Thread Task Stack Size"
399+
range 0 65535
400+
default 8192
401+
help
402+
The size (in bytes) of the Thread task stack.
403+
397404
config MAX_EVENT_QUEUE_SIZE
398405
int "Max Event Queue Size"
399406
range 0 65535
@@ -657,6 +664,14 @@ menu "OpenWeave Device Layer"
657664

658665
endmenu
659666

667+
menu "Thread Options"
668+
669+
config WEAVE_DEVICE_CONFIG_ENABLE_THREAD
670+
bool "Enable Thread support (with external RCP)"
671+
default n
672+
673+
endmenu
674+
660675
menu "Service Provisioning Options"
661676

662677
config SERVICE_PROVISIONING_ENDPOINT_ID

src/adaptations/device-layer/ESP32/ConfigurationManagerImpl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
239239
WeaveLogError(DeviceLayer, "esp_wifi_restore() failed: %s", nl::ErrorStr(err));
240240
}
241241

242+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
243+
ThreadStackMgrImpl().LockThreadStack();
244+
otInstanceFactoryReset(ThreadStackMgrImpl().OTInstance());
245+
ThreadStackMgrImpl().UnlockThreadStack();
246+
#endif
247+
242248
// Restart the system.
243249
WeaveLogProgress(DeviceLayer, "System restarting");
244250
esp_restart();

src/adaptations/device-layer/ESP32/ConnectivityManagerImpl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#include <nest/trait/network/TelemetryNetworkWifiTrait.h>
5858
#endif
5959

60+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
61+
#include <Weave/DeviceLayer/internal/GenericConnectivityManagerImpl_Thread.ipp>
62+
#endif
63+
6064
using namespace ::nl;
6165
using namespace ::nl::Weave;
6266
using namespace ::nl::Weave::TLV;
@@ -398,6 +402,10 @@ WEAVE_ERROR ConnectivityManagerImpl::_Init()
398402
mWiFiAPIdleTimeoutMS = WEAVE_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT;
399403
mFlags = 0;
400404

405+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
406+
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_Init();
407+
#endif
408+
401409
// Initialize the Weave Addressing and Routing Module.
402410
err = Warm::Init(FabricState);
403411
SuccessOrExit(err);
@@ -555,6 +563,10 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const WeaveDeviceEvent * event)
555563
}
556564

557565
#endif // !WEAVE_DEVICE_CONFIG_DISABLE_ACCOUNT_PAIRING
566+
567+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
568+
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_OnPlatformEvent(event);
569+
#endif // WEAVE_DEVICE_CONFIG_ENABLE_THREAD
558570
}
559571

560572
void ConnectivityManagerImpl::_OnWiFiScanDone()
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
*
3+
* Copyright (c) 2019 Nest Labs, Inc.
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @file
21+
* Provides an implementation of the ThreadStackManager object for
22+
* ESP32 platforms using the ESP-IDF SDK and the OpenThread
23+
* stack.
24+
*
25+
*/
26+
27+
#include <Weave/DeviceLayer/internal/WeaveDeviceLayerInternal.h>
28+
29+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
30+
31+
#include <Weave/DeviceLayer/ThreadStackManager.h>
32+
#include <Weave/DeviceLayer/OpenThread/OpenThreadUtils.h>
33+
34+
#include <Weave/DeviceLayer/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.ipp>
35+
#include <Weave/DeviceLayer/OpenThread/GenericThreadStackManagerImpl_OpenThread_LwIP.ipp>
36+
37+
#include <openthread/cli.h>
38+
39+
#include <openthread/openthread-esp32.h>
40+
41+
namespace nl {
42+
namespace Weave {
43+
namespace DeviceLayer {
44+
45+
using namespace ::nl::Weave::DeviceLayer::Internal;
46+
47+
namespace Internal {
48+
49+
// Specialize ESP32 ThreadStackManagerImpl's ThreadTaskMain since OpenThread on ESP32 is using a RCP for thread support.
50+
template<>
51+
void GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>::ThreadTaskMain(void * arg)
52+
{
53+
GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl> * self =
54+
static_cast<GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>*>(arg);
55+
56+
VerifyOrDie(self->mThreadTask == NULL);
57+
58+
WeaveLogDetail(DeviceLayer, "Thread task running");
59+
60+
// Capture the Thread task handle.
61+
self->mThreadTask = xTaskGetCurrentTaskHandle();
62+
63+
ThreadStackMgr().LockThreadStack();
64+
otInstance *instance = ThreadStackMgrImpl().OTInstance();
65+
ThreadStackMgr().UnlockThreadStack();
66+
67+
68+
while (true)
69+
{
70+
otSysMainloopContext mainloop;
71+
72+
otSysMainloopInit(&mainloop);
73+
74+
ThreadStackMgr().LockThreadStack();
75+
otTaskletsProcess(instance);
76+
otSysMainloopUpdate(instance, &mainloop);
77+
ThreadStackMgr().UnlockThreadStack();
78+
79+
if (otSysMainloopPoll(&mainloop) >= 0)
80+
{
81+
ThreadStackMgr().LockThreadStack();
82+
otSysMainloopProcess(instance, &mainloop);
83+
ThreadStackMgr().UnlockThreadStack();
84+
}
85+
else
86+
{
87+
WeaveLogError(DeviceLayer, "OpenThread system polling failed");
88+
abort();
89+
}
90+
}
91+
}
92+
93+
}
94+
95+
ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;
96+
97+
WEAVE_ERROR ThreadStackManagerImpl::_InitThreadStack(void)
98+
{
99+
return InitThreadStack(NULL);
100+
}
101+
102+
WEAVE_ERROR ThreadStackManagerImpl::InitThreadStack(otInstance *otInst)
103+
{
104+
WEAVE_ERROR err = WEAVE_NO_ERROR;
105+
106+
// Initialize the generic implementation base classes.
107+
err = GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>::DoInit();
108+
SuccessOrExit(err);
109+
err = GenericThreadStackManagerImpl_OpenThread_LwIP<ThreadStackManagerImpl>::DoInit(otInst);
110+
SuccessOrExit(err);
111+
112+
exit:
113+
return err;
114+
}
115+
116+
bool ThreadStackManagerImpl::IsInitialized()
117+
{
118+
return sInstance.mThreadStackLock != NULL;
119+
}
120+
121+
} // namespace DeviceLayer
122+
} // namespace Weave
123+
} // namespace nl
124+
125+
using namespace ::nl::Weave::DeviceLayer;
126+
127+
/**
128+
* Glue function called directly by the OpenThread stack when tasklet processing work
129+
* is pending.
130+
*/
131+
extern "C" void otTaskletsSignalPending(otInstance *p_instance)
132+
{
133+
ThreadStackMgrImpl().SignalThreadActivityPending();
134+
}
135+
136+
/**
137+
* Glue function called directly by the OpenThread stack when system event processing work
138+
* is pending.
139+
*/
140+
extern "C" void otSysEventSignalPending(void)
141+
{
142+
BaseType_t yieldRequired = ThreadStackMgrImpl().SignalThreadActivityPendingFromISR();
143+
if (yieldRequired) portYIELD_FROM_ISR();
144+
}
145+
146+
#endif // WEAVE_DEVICE_CONFIG_ENABLE_THREAD

src/adaptations/device-layer/LwIP/WarmSupport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,6 @@ PlatformResult AddRemoveThreadRoute(InterfaceType inInterfaceType, const Inet::I
448448

449449
ThreadStackMgrImpl().LockThreadStack();
450450

451-
otBorderRouterRegister(ThreadStackMgrImpl().OTInstance());
452-
453451
memcpy(routeConfig.mPrefix.mPrefix.mFields.m8, inPrefix.IPAddr.Addr, sizeof(routeConfig.mPrefix.mPrefix.mFields));
454452
routeConfig.mPrefix.mLength = inPrefix.Length;
455453
routeConfig.mStable = true;
@@ -464,6 +462,8 @@ PlatformResult AddRemoveThreadRoute(InterfaceType inInterfaceType, const Inet::I
464462
otErr = otBorderRouterRemoveRoute(ThreadStackMgrImpl().OTInstance(), &routeConfig.mPrefix);
465463
}
466464

465+
otBorderRouterRegister(ThreadStackMgrImpl().OTInstance());
466+
467467
ThreadStackMgrImpl().UnlockThreadStack();
468468

469469
if (otErr == OT_ERROR_NONE)

src/adaptations/device-layer/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ noinst_HEADERS = \
4747
include/Weave/DeviceLayer/ESP32/SoftwareUpdateManagerImpl.h \
4848
include/Weave/DeviceLayer/ESP32/WeaveDevicePlatformEvent.h \
4949
include/Weave/DeviceLayer/ESP32/NetworkProvisioningServerImpl.h \
50+
include/Weave/DeviceLayer/ESP32/ThreadStackManagerImpl.h \
5051
include/Weave/DeviceLayer/nRF5/ConfigurationManagerImpl.h \
5152
include/Weave/DeviceLayer/nRF5/PlatformManagerImpl.h \
5253
include/Weave/DeviceLayer/nRF5/nRF5Config.h \
@@ -169,6 +170,8 @@ libDeviceLayer_a_SOURCES += \
169170
ESP32/PlatformManagerImpl.cpp \
170171
ESP32/SoftwareUpdateManagerImpl.cpp \
171172
ESP32/SystemTimeSupport.cpp \
173+
ESP32/ThreadStackManagerImpl.cpp \
174+
OpenThread/OpenThreadUtils.cpp \
172175
LwIP/WarmSupport.cpp \
173176
$(NULL)
174177

src/adaptations/device-layer/OpenThread/OpenThreadUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424

2525
#include <Weave/DeviceLayer/internal/WeaveDeviceLayerInternal.h>
26+
27+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
28+
2629
#include <Weave/DeviceLayer/OpenThread/OpenThreadUtils.h>
2730

2831
#include <openthread/error.h>
@@ -276,3 +279,5 @@ const char * OpenThreadRoleToStr(otDeviceRole role)
276279
} // namespace DeviceLayer
277280
} // namespace Weave
278281
} // namespace nl
282+
283+
#endif // WEAVE_DEVICE_CONFIG_ENABLE_THREAD

src/adaptations/device-layer/include/Weave/DeviceLayer/ESP32/ConnectivityManagerImpl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#else
2727
#include <Weave/DeviceLayer/internal/GenericConnectivityManagerImpl_NoBLE.h>
2828
#endif
29+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
30+
#include <Weave/DeviceLayer/internal/GenericConnectivityManagerImpl_Thread.h>
31+
#else
32+
#include <Weave/DeviceLayer/internal/GenericConnectivityManagerImpl_NoThread.h>
33+
#endif
2934
#include <Weave/DeviceLayer/internal/GenericConnectivityManagerImpl_NoThread.h>
3035
#include <Weave/Profiles/network-provisioning/NetworkProvisioning.h>
3136
#include <Weave/Profiles/weave-tunneling/WeaveTunnelCommon.h>
@@ -64,7 +69,11 @@ class ConnectivityManagerImpl final
6469
#else
6570
public Internal::GenericConnectivityManagerImpl_NoBLE<ConnectivityManagerImpl>,
6671
#endif
72+
#if WEAVE_DEVICE_CONFIG_ENABLE_THREAD
73+
public Internal::GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>
74+
#else
6775
public Internal::GenericConnectivityManagerImpl_NoThread<ConnectivityManagerImpl>
76+
#endif
6877
{
6978
using TunnelConnNotifyReasons = ::nl::Weave::Profiles::WeaveTunnel::WeaveTunnelConnectionMgr::TunnelConnNotifyReasons;
7079

0 commit comments

Comments
 (0)