Skip to content

Commit 3d4a979

Browse files
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"
1 parent bcb4179 commit 3d4a979

15 files changed

+556
-90
lines changed

examples/platform/silabs/BaseApplication.cpp

+73-6
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ app::Clusters::NetworkCommissioning::Instance
115115
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::SlWiFiDriver::GetInstance()));
116116
#endif /* SL_WIFI */
117117

118-
#if !(defined(CHIP_CONFIG_ENABLE_ICD_SERVER) && CHIP_CONFIG_ENABLE_ICD_SERVER)
119118
bool sIsEnabled = false;
120119
bool sIsAttached = false;
120+
121+
#if !(defined(CHIP_CONFIG_ENABLE_ICD_SERVER) && CHIP_CONFIG_ENABLE_ICD_SERVER)
121122
bool sHaveBLEConnections = false;
122123
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
123124

@@ -156,14 +157,13 @@ Identify gIdentify = {
156157
};
157158

158159
#endif // MATTER_DM_PLUGIN_IDENTIFY_SERVER
160+
159161
} // namespace
160162

161163
bool BaseApplication::sIsProvisioned = false;
162164
bool BaseApplication::sIsFactoryResetTriggered = false;
163165
LEDWidget * BaseApplication::sAppActionLed = nullptr;
164-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
165166
BaseApplicationDelegate BaseApplication::sAppDelegate = BaseApplicationDelegate();
166-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
167167

168168
#ifdef DIC_ENABLE
169169
namespace {
@@ -181,17 +181,19 @@ void AppSpecificConnectivityEventCallback(const ChipDeviceEvent * event, intptr_
181181
} // namespace
182182
#endif // DIC_ENABLE
183183

184-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
185184
void BaseApplicationDelegate::OnCommissioningSessionStarted()
186185
{
187186
isComissioningStarted = true;
188187
}
188+
189189
void BaseApplicationDelegate::OnCommissioningSessionStopped()
190190
{
191191
isComissioningStarted = false;
192192
}
193+
193194
void BaseApplicationDelegate::OnCommissioningWindowClosed()
194195
{
196+
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
195197
if (!BaseApplication::GetProvisionStatus() && !isComissioningStarted)
196198
{
197199
int32_t status = wfx_power_save(RSI_SLEEP_MODE_8, STANDBY_POWER_SAVE_WITH_RAM_RETENTION);
@@ -200,8 +202,27 @@ void BaseApplicationDelegate::OnCommissioningWindowClosed()
200202
ChipLogError(DeviceLayer, "Failed to enable the TA Deep Sleep");
201203
}
202204
}
205+
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917qq
206+
}
207+
208+
void BaseApplicationDelegate::OnFabricCommitted(const FabricTable & fabricTable, FabricIndex fabricIndex)
209+
{
210+
// If we commissioned our first fabric, Update the commissioned status of the App
211+
if (fabricTable.FabricCount() == 1)
212+
{
213+
BaseApplication::UpdateCommissioningStatus(true);
214+
}
215+
}
216+
217+
void BaseApplicationDelegate::OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex)
218+
{
219+
if (fabricTable.FabricCount() == 0)
220+
{
221+
BaseApplication::UpdateCommissioningStatus(false);
222+
223+
BaseApplication::DoProvisioningReset();
224+
}
203225
}
204-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
205226

206227
/**********************************************************
207228
* AppTask Definitions
@@ -298,6 +319,8 @@ CHIP_ERROR BaseApplication::Init()
298319
#if CHIP_ENABLE_OPENTHREAD
299320
BaseApplication::sIsProvisioned = ConnectivityMgr().IsThreadProvisioned();
300321
#endif
322+
323+
err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppDelegate);
301324
return err;
302325
}
303326

@@ -411,6 +434,23 @@ bool BaseApplication::ActivateStatusLedPatterns()
411434
return isPatternSet;
412435
}
413436

437+
void BaseApplication::UpdateCommissioningStatus(bool newState)
438+
{
439+
#ifdef SL_WIFI
440+
BaseApplication::sIsProvisioned = ConnectivityMgr().IsWiFiStationProvisioned();
441+
sIsEnabled = ConnectivityMgr().IsWiFiStationEnabled();
442+
sIsAttached = ConnectivityMgr().IsWiFiStationConnected();
443+
#endif /* SL_WIFI */
444+
#if CHIP_ENABLE_OPENTHREAD
445+
// TODO: This is a temporary solution until we can read Thread provisioning status from RAM instead of NVM.
446+
BaseApplication::sIsProvisioned = newState;
447+
sIsEnabled = ConnectivityMgr().IsThreadEnabled();
448+
sIsAttached = ConnectivityMgr().IsThreadAttached();
449+
#endif /* CHIP_ENABLE_OPENTHREAD */
450+
451+
ActivateStatusLedPatterns();
452+
}
453+
414454
// TODO Move State Monitoring elsewhere
415455
void BaseApplication::LightEventHandler()
416456
{
@@ -750,15 +790,42 @@ void BaseApplication::ScheduleFactoryReset()
750790
{
751791
Provision::Manager::GetInstance().SetProvisionRequired(true);
752792
}
753-
PlatformMgr().HandleServerShuttingDown();
793+
PlatformMgr().HandleServerShuttingDown(); // HandleServerShuttingDown calls OnShutdown() which is only implemented for the basic information cluster it seems. And triggers and Event flush, which is not relevant when there are no fabrics left
754794
ConfigurationMgr().InitiateFactoryReset();
755795
});
756796
}
757797

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

examples/platform/silabs/BaseApplication.h

+20-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <lib/core/CHIPError.h>
3636
#include <platform/CHIPDeviceEvent.h>
3737
#include <platform/CHIPDeviceLayer.h>
38+
#include <credentials/FabricTable.h>
3839

3940
#include "LEDWidget.h"
4041

@@ -62,16 +63,20 @@
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,
67+
public chip::FabricTable::Delegate
6768
{
6869
private:
70+
// AppDelegate
6971
bool isComissioningStarted;
7072
void OnCommissioningSessionStarted() override;
7173
void OnCommissioningSessionStopped() override;
7274
void OnCommissioningWindowClosed() override;
75+
76+
// FabricTable::Delegate
77+
void OnFabricCommitted(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override;
78+
void OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex) override;
7379
};
74-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
7580

7681
/**********************************************************
7782
* BaseApplication Declaration
@@ -86,9 +91,7 @@ class BaseApplication
8691
static bool sIsProvisioned;
8792
static bool sIsFactoryResetTriggered;
8893
static LEDWidget * sAppActionLed;
89-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
9094
static BaseApplicationDelegate sAppDelegate;
91-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
9295

9396
/**
9497
* @brief Create AppTask task and Event Queue
@@ -156,6 +159,18 @@ class BaseApplication
156159
static void OnTriggerIdentifyEffect(Identify * identify);
157160
#endif
158161

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

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

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

0 commit comments

Comments
 (0)