Skip to content

Commit 5dc1934

Browse files
lpbeliveau-silabsj-ororke
authored andcommitted
[Silabs] Update ThreadStackManagerImpl (project-chip#34389)
* Updated Silabs ThreadStackManagerImpl for the Simplicity_sdk changes, added a helper in GenericThreadStackManagerImpl_OpenThread.hpp to avoid code repetition Fixes for memory allocation initialization added since, unlike in SLC, here we use MBEDTLS_CONFIG_FILE="efr32-chip-mbedtls-config.h" * Updated matter support * Added ClearThreadStack Helper * Grouped extern C statements
1 parent 20a5ef3 commit 5dc1934

17 files changed

+340
-105
lines changed

examples/platform/silabs/BaseApplication.cpp

+76-10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#if CHIP_ENABLE_OPENTHREAD
5252
#include <platform/OpenThread/OpenThreadUtils.h>
5353
#include <platform/ThreadStackManager.h>
54+
#include <platform/silabs/ConfigurationManagerImpl.h>
5455
#include <platform/silabs/ThreadStackManagerImpl.h>
5556
#endif // CHIP_ENABLE_OPENTHREAD
5657

@@ -115,9 +116,10 @@ app::Clusters::NetworkCommissioning::Instance
115116
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::SlWiFiDriver::GetInstance()));
116117
#endif /* SL_WIFI */
117118

119+
bool sIsEnabled = false;
120+
bool sIsAttached = false;
121+
118122
#if !(defined(CHIP_CONFIG_ENABLE_ICD_SERVER) && CHIP_CONFIG_ENABLE_ICD_SERVER)
119-
bool sIsEnabled = false;
120-
bool sIsAttached = false;
121123
bool sHaveBLEConnections = false;
122124
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
123125

@@ -156,14 +158,13 @@ Identify gIdentify = {
156158
};
157159

158160
#endif // MATTER_DM_PLUGIN_IDENTIFY_SERVER
161+
159162
} // namespace
160163

161-
bool BaseApplication::sIsProvisioned = false;
162-
bool BaseApplication::sIsFactoryResetTriggered = false;
163-
LEDWidget * BaseApplication::sAppActionLed = nullptr;
164-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
164+
bool BaseApplication::sIsProvisioned = false;
165+
bool BaseApplication::sIsFactoryResetTriggered = false;
166+
LEDWidget * BaseApplication::sAppActionLed = nullptr;
165167
BaseApplicationDelegate BaseApplication::sAppDelegate = BaseApplicationDelegate();
166-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
167168

168169
#ifdef DIC_ENABLE
169170
namespace {
@@ -181,17 +182,19 @@ void AppSpecificConnectivityEventCallback(const ChipDeviceEvent * event, intptr_
181182
} // namespace
182183
#endif // DIC_ENABLE
183184

184-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
185185
void BaseApplicationDelegate::OnCommissioningSessionStarted()
186186
{
187187
isComissioningStarted = true;
188188
}
189+
189190
void BaseApplicationDelegate::OnCommissioningSessionStopped()
190191
{
191192
isComissioningStarted = false;
192193
}
194+
193195
void BaseApplicationDelegate::OnCommissioningWindowClosed()
194196
{
197+
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
195198
if (!BaseApplication::GetProvisionStatus() && !isComissioningStarted)
196199
{
197200
int32_t status = wfx_power_save(RSI_SLEEP_MODE_8, STANDBY_POWER_SAVE_WITH_RAM_RETENTION);
@@ -200,8 +203,27 @@ void BaseApplicationDelegate::OnCommissioningWindowClosed()
200203
ChipLogError(DeviceLayer, "Failed to enable the TA Deep Sleep");
201204
}
202205
}
206+
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917qq
207+
}
208+
209+
void BaseApplicationDelegate::OnFabricCommitted(const FabricTable & fabricTable, FabricIndex fabricIndex)
210+
{
211+
// If we commissioned our first fabric, Update the commissioned status of the App
212+
if (fabricTable.FabricCount() == 1)
213+
{
214+
BaseApplication::UpdateCommissioningStatus(true);
215+
}
216+
}
217+
218+
void BaseApplicationDelegate::OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex)
219+
{
220+
if (fabricTable.FabricCount() == 0)
221+
{
222+
BaseApplication::UpdateCommissioningStatus(false);
223+
224+
BaseApplication::DoProvisioningReset();
225+
}
203226
}
204-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
205227

206228
/**********************************************************
207229
* AppTask Definitions
@@ -298,6 +320,8 @@ CHIP_ERROR BaseApplication::Init()
298320
#if CHIP_ENABLE_OPENTHREAD
299321
BaseApplication::sIsProvisioned = ConnectivityMgr().IsThreadProvisioned();
300322
#endif
323+
324+
err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppDelegate);
301325
return err;
302326
}
303327

@@ -411,6 +435,23 @@ bool BaseApplication::ActivateStatusLedPatterns()
411435
return isPatternSet;
412436
}
413437

438+
void BaseApplication::UpdateCommissioningStatus(bool newState)
439+
{
440+
#ifdef SL_WIFI
441+
BaseApplication::sIsProvisioned = ConnectivityMgr().IsWiFiStationProvisioned();
442+
sIsEnabled = ConnectivityMgr().IsWiFiStationEnabled();
443+
sIsAttached = ConnectivityMgr().IsWiFiStationConnected();
444+
#endif /* SL_WIFI */
445+
#if CHIP_ENABLE_OPENTHREAD
446+
// TODO: This is a temporary solution until we can read Thread provisioning status from RAM instead of NVM.
447+
BaseApplication::sIsProvisioned = newState;
448+
sIsEnabled = ConnectivityMgr().IsThreadEnabled();
449+
sIsAttached = ConnectivityMgr().IsThreadAttached();
450+
#endif /* CHIP_ENABLE_OPENTHREAD */
451+
452+
ActivateStatusLedPatterns();
453+
}
454+
414455
// TODO Move State Monitoring elsewhere
415456
void BaseApplication::LightEventHandler()
416457
{
@@ -750,15 +791,40 @@ void BaseApplication::ScheduleFactoryReset()
750791
{
751792
Provision::Manager::GetInstance().SetProvisionRequired(true);
752793
}
753-
PlatformMgr().HandleServerShuttingDown();
794+
PlatformMgr().HandleServerShuttingDown(); // HandleServerShuttingDown calls OnShutdown() which is only implemented for the
795+
// basic information cluster it seems. And triggers and Event flush, which is not
796+
// relevant when there are no fabrics left
754797
ConfigurationMgr().InitiateFactoryReset();
755798
});
756799
}
757800

801+
void BaseApplication::DoProvisioningReset()
802+
{
803+
PlatformMgr().ScheduleWork([](intptr_t) {
804+
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
805+
ConfigurationManagerImpl::GetDefaultInstance().ClearThreadStack();
806+
ThreadStackMgrImpl().FactoryResetThreadStack();
807+
ThreadStackMgr().InitThreadStack();
808+
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
809+
810+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
811+
ChipLogProgress(DeviceLayer, "Clearing WiFi provision");
812+
chip::DeviceLayer::ConnectivityMgr().ClearWiFiStationProvision();
813+
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
814+
815+
CHIP_ERROR err = Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
816+
if (err != CHIP_NO_ERROR)
817+
{
818+
SILABS_LOG("Failed to open the Basic Commissioning Window");
819+
}
820+
});
821+
}
822+
758823
void BaseApplication::OnPlatformEvent(const ChipDeviceEvent * event, intptr_t)
759824
{
760825
if (event->Type == DeviceEventType::kServiceProvisioningChange)
761826
{
827+
// Note: This is only called on Attach, we need to add a method to detect Thread Network Detach
762828
BaseApplication::sIsProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned;
763829
}
764830
}

examples/platform/silabs/BaseApplication.h

+19-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <app/util/config.h>
3333
#include <ble/Ble.h>
3434
#include <cmsis_os2.h>
35+
#include <credentials/FabricTable.h>
3536
#include <lib/core/CHIPError.h>
3637
#include <platform/CHIPDeviceEvent.h>
3738
#include <platform/CHIPDeviceLayer.h>
@@ -62,16 +63,19 @@
6263
#define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05)
6364
#define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06)
6465

65-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
66-
class BaseApplicationDelegate : public AppDelegate
66+
class BaseApplicationDelegate : public AppDelegate, public chip::FabricTable::Delegate
6767
{
6868
private:
69+
// AppDelegate
6970
bool isComissioningStarted;
7071
void OnCommissioningSessionStarted() override;
7172
void OnCommissioningSessionStopped() override;
7273
void OnCommissioningWindowClosed() override;
74+
75+
// FabricTable::Delegate
76+
void OnFabricCommitted(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override;
77+
void OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override;
7378
};
74-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
7579

7680
/**********************************************************
7781
* BaseApplication Declaration
@@ -86,9 +90,7 @@ class BaseApplication
8690
static bool sIsProvisioned;
8791
static bool sIsFactoryResetTriggered;
8892
static LEDWidget * sAppActionLed;
89-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
9093
static BaseApplicationDelegate sAppDelegate;
91-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
9294

9395
/**
9496
* @brief Create AppTask task and Event Queue
@@ -156,6 +158,18 @@ class BaseApplication
156158
static void OnTriggerIdentifyEffect(Identify * identify);
157159
#endif
158160

161+
/**
162+
* @brief Updates the static boolean isCommissioned to the desired state
163+
*
164+
*/
165+
static void UpdateCommissioningStatus(bool newState);
166+
167+
/**
168+
* @brief Called when the last Fabric is removed, clears all Fabric related data, including Thread and Wifi provision.
169+
* @note This function preserves some NVM3 data that is not Fabric scoped, like Attribute Value or Boot Count.
170+
*/
171+
static void DoProvisioningReset();
172+
159173
protected:
160174
CHIP_ERROR Init();
161175

examples/platform/silabs/MatterConfig.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,12 @@ void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event
222222
CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
223223
{
224224
CHIP_ERROR err;
225-
225+
#ifdef SL_WIFI
226+
// Because OpenThread needs to use memory allocation during its Key operations, we initialize the memory management for thread
227+
// and set the allocation functions inside sl_ot_create_instance, which is called by sl_system_init in the OpenThread stack
228+
// initialization.
226229
mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
227-
230+
#endif
228231
SILABS_LOG("==================================================");
229232
SILABS_LOG("%s starting", appName);
230233
SILABS_LOG("==================================================");
@@ -241,11 +244,11 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
241244
// Init Matter Stack
242245
//==============================================
243246
SILABS_LOG("Init CHIP Stack");
244-
// Init Chip memory management before the stack
245-
ReturnErrorOnFailure(chip::Platform::MemoryInit());
246247

247-
// WiFi needs to be initialized after Memory Init for some reason
248248
#ifdef SL_WIFI
249+
// Init Chip memory management before the stack
250+
// See comment above about OpenThread memory allocation as to why this is WIFI only here.
251+
ReturnErrorOnFailure(chip::Platform::MemoryInit());
249252
ReturnErrorOnFailure(InitWiFi());
250253
#endif
251254

@@ -304,9 +307,7 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
304307
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
305308
#endif
306309

307-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
308310
initParams.appDelegate = &BaseApplication::sAppDelegate;
309-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
310311
// Init Matter Server and Start Event Loop
311312
err = chip::Server::GetInstance().Init(initParams);
312313

examples/platform/silabs/efr32/uart.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ int16_t uartConsoleWrite(const char * Buf, uint16_t BufLength)
361361
return UART_CONSOLE_ERR;
362362
}
363363

364+
if (NULL == sUartTxQueue)
365+
{
366+
// This is to prevent the first prompt from OTCLI to be rejected and to break the OTCli output
367+
uartConsoleInit();
368+
}
369+
364370
#ifdef PW_RPC_ENABLED
365371
// Pigweed Logger is already thread safe.
366372
UARTDRV_ForceTransmit(vcom_handle, (uint8_t *) Buf, BufLength);

examples/platform/silabs/matter-platform.slcp

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ component:
7272
- {id: mbedtls_platform_dynamic_memory_allocation_config_init_runtime }
7373
- {id: mbedtls_base64}
7474
- {id: ot_psa_crypto}
75+
- {id: ot_platform_abstraction}
76+
- {id: ot_rtos_wrappers_real}
77+
- {id: sl_ot_custom_cli}
7578
# Necessary componenets for ot coap cert lib
7679
# - {id: mbedtls_dtls} # Requried by COAP lib
7780
# - {id: mbedtls_tls_server} # Requried by COAP lib
@@ -86,6 +89,10 @@ config_file:
8689
directory: btconf
8790

8891
configuration:
92+
- name: SL_OPENTHREAD_ENABLE_APP_TASK
93+
value: 0
94+
- name: SL_OPENTHREAD_ENABLE_CLI_TASK
95+
value: 0
8996
- {name: SL_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED, value: '0'}
9097
- {name: SL_MBEDTLS_KEY_EXCHANGE_PSK_ENABLED, value: '1'}
9198
- condition: [uartdrv_usart]

src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class GenericThreadStackManagerImpl_OpenThread
142142

143143
// ===== Members available to the implementation subclass.
144144

145+
CHIP_ERROR ConfigureThreadStack(otInstance * otInst);
145146
CHIP_ERROR DoInit(otInstance * otInst);
146147
bool IsThreadAttachedNoLock(void);
147148
bool IsThreadInterfaceUpNoLock(void);

src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp

+40-20
Original file line numberDiff line numberDiff line change
@@ -1088,29 +1088,21 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_GetPollPeriod(u
10881088
return CHIP_NO_ERROR;
10891089
}
10901090

1091+
/**
1092+
* @brief Helper that sets callbacks for OpenThread state changes and configures the Thread stack.
1093+
* Assigns mOTInst to an instance, and configures the OT stack on a device by setting state change callbacks enabling features
1094+
* for IPv6 address configuration, enabling the Thread network if necessary, and handling SRP if enabled.
1095+
* Allows for the configuration of the Thread stack on a device where the instance and the otCLI are already initialised.
1096+
*
1097+
* @param otInst Pointer to the OT instance
1098+
* @return CHIP_ERROR OpenThread error mapped to CHIP_ERROR
1099+
*/
10911100
template <class ImplClass>
1092-
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstance * otInst)
1101+
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::ConfigureThreadStack(otInstance * otInst)
10931102
{
10941103
CHIP_ERROR err = CHIP_NO_ERROR;
10951104
otError otErr = OT_ERROR_NONE;
10961105

1097-
// Arrange for OpenThread errors to be translated to text.
1098-
RegisterOpenThreadErrorFormatter();
1099-
1100-
mOTInst = NULL;
1101-
1102-
// If an OpenThread instance hasn't been supplied, call otInstanceInitSingle() to
1103-
// create or acquire a singleton instance of OpenThread.
1104-
if (otInst == NULL)
1105-
{
1106-
otInst = otInstanceInitSingle();
1107-
VerifyOrExit(otInst != NULL, err = MapOpenThreadError(OT_ERROR_FAILED));
1108-
}
1109-
1110-
#if !defined(PW_RPC_ENABLED) && CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
1111-
otAppCliInit(otInst);
1112-
#endif
1113-
11141106
mOTInst = otInst;
11151107

11161108
// Arrange for OpenThread to call the OnOpenThreadStateChange method whenever a
@@ -1146,14 +1138,42 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc
11461138
}
11471139
#endif
11481140

1149-
initNetworkCommissioningThreadDriver();
1150-
11511141
exit:
11521142

11531143
ChipLogProgress(DeviceLayer, "OpenThread started: %s", otThreadErrorToString(otErr));
11541144
return err;
11551145
}
11561146

1147+
template <class ImplClass>
1148+
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstance * otInst)
1149+
{
1150+
CHIP_ERROR err = CHIP_NO_ERROR;
1151+
1152+
// Arrange for OpenThread errors to be translated to text.
1153+
RegisterOpenThreadErrorFormatter();
1154+
1155+
mOTInst = NULL;
1156+
1157+
// If an OpenThread instance hasn't been supplied, call otInstanceInitSingle() to
1158+
// create or acquire a singleton instance of OpenThread.
1159+
if (otInst == NULL)
1160+
{
1161+
otInst = otInstanceInitSingle();
1162+
VerifyOrExit(otInst != NULL, err = MapOpenThreadError(OT_ERROR_FAILED));
1163+
}
1164+
1165+
#if !defined(PW_RPC_ENABLED) && CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
1166+
otAppCliInit(otInst);
1167+
#endif
1168+
1169+
err = ConfigureThreadStack(otInst);
1170+
1171+
initNetworkCommissioningThreadDriver();
1172+
1173+
exit:
1174+
return err;
1175+
}
1176+
11571177
template <class ImplClass>
11581178
bool GenericThreadStackManagerImpl_OpenThread<ImplClass>::IsThreadAttachedNoLock(void)
11591179
{

0 commit comments

Comments
 (0)