Skip to content

Commit 17a6004

Browse files
authored
[Realtek] Add error checking to app initialization and fix minor (#71395)
* [Realtek] Add error checking to app initialization and fix minor issues - Add error checking for Init(), Server::Init(), AddFabricDelegate() in AppTask.cpp - Add error handling for Binding::Manager operations in BindingHandler.cpp - Add error handling for RTKConfig read/write in BoltLockManager.cpp - Fix redundant chunkDuration >= 0 check in PushAVStreamTransportLogic.cpp - Add Python3 executable path to realtek.py build script * [Realtek] Add error handling return in initialization failure * [Realtek]: Replace (void) with RETURN_SAFELY_IGNORED for ScheduleWork * [Realtek] Restyle files
1 parent fdb31db commit 17a6004

File tree

23 files changed

+353
-89
lines changed

23 files changed

+353
-89
lines changed

examples/all-clusters-app/realtek/common/main/AppTask.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ CHIP_ERROR AppTask::StartAppTask()
211211
void AppTask::AppTaskMain(void * pvParameter)
212212
{
213213
uint8_t event;
214+
CHIP_ERROR err = CHIP_NO_ERROR;
214215

215216
#if defined(FEATURE_TRUSTZONE_ENABLE) && (FEATURE_TRUSTZONE_ENABLE == 1)
216217
os_alloc_secure_ctx(1024);
@@ -222,7 +223,12 @@ void AppTask::AppTaskMain(void * pvParameter)
222223
gap_start_bt_stack(app_evt_queue_handle, app_io_queue_handle, MAX_NUMBER_OF_GAP_MESSAGE);
223224
matter_ble_queue_init(app_evt_queue_handle, app_io_queue_handle);
224225

225-
sAppTask.Init();
226+
err = sAppTask.Init();
227+
if (err != CHIP_NO_ERROR)
228+
{
229+
ChipLogError(NotSpecified, "sAppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
230+
return;
231+
}
226232

227233
while (true)
228234
{
@@ -263,6 +269,8 @@ void AppTask::AppTaskMain(void * pvParameter)
263269

264270
void AppTask::InitServer(intptr_t arg)
265271
{
272+
CHIP_ERROR err = CHIP_NO_ERROR;
273+
266274
// Init ZCL Data Model and start server
267275
static chip::CommonCaseDeviceServerInitParams initParams;
268276
(void) initParams.InitializeStaticResourcesBeforeServerInit();
@@ -289,10 +297,19 @@ void AppTask::InitServer(intptr_t arg)
289297
(void) initParams.InitializeStaticResourcesBeforeServerInit();
290298
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
291299

292-
chip::Server::GetInstance().Init(initParams);
300+
err = chip::Server::GetInstance().Init(initParams);
301+
if (err != CHIP_NO_ERROR)
302+
{
303+
ChipLogError(NotSpecified, "Server::GetInstance().Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
304+
return;
305+
}
293306

294307
static RealtekObserver sRealtekObserver;
295-
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sRealtekObserver);
308+
err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sRealtekObserver);
309+
if (err != CHIP_NO_ERROR)
310+
{
311+
ChipLogError(NotSpecified, "AddFabricDelegate failed: %" CHIP_ERROR_FORMAT, err.Format());
312+
}
296313

297314
ConfigurationMgr().LogDeviceConfig();
298315
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
@@ -331,7 +348,10 @@ CHIP_ERROR AppTask::Init()
331348
}
332349

333350
// Init ZCL Data Model and start server
334-
PlatformMgr().ScheduleWork(InitServer, 0);
351+
// ScheduleWork is asynchronous, failure means work couldn't be queued.
352+
// Since this is init time, if it fails the system won't work anyway,
353+
// so the return value can be safely ignored.
354+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(InitServer, 0));
335355

336356
#if CONFIG_ENABLE_CHIP_SHELL
337357
chip::Shell::Engine::Root().Init();
@@ -355,13 +375,18 @@ CHIP_ERROR AppTask::Init()
355375

356376
void AppTask::BLEStartAdvertising(intptr_t arg)
357377
{
378+
CHIP_ERROR setErr = CHIP_NO_ERROR;
358379
if (ConnectivityMgr().IsBLEAdvertisingEnabled())
359380
{
360-
ConnectivityMgr().SetBLEAdvertisingEnabled(false);
381+
setErr = ConnectivityMgr().SetBLEAdvertisingEnabled(false);
361382
}
362383
else
363384
{
364-
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
385+
setErr = ConnectivityMgr().SetBLEAdvertisingEnabled(true);
386+
}
387+
if (setErr != CHIP_NO_ERROR)
388+
{
389+
ChipLogError(NotSpecified, "SetBLEAdvertisingEnabled failed: %" CHIP_ERROR_FORMAT, setErr.Format());
365390
}
366391
}
367392

@@ -430,7 +455,10 @@ void AppTask::ButtonHandler(T_IO_MSG * p_msg)
430455
case APP_BLE_ADV_BUTTON:
431456
if (btnPressed)
432457
{
433-
PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0);
458+
// ScheduleWork is asynchronous and returns immediately.
459+
// Failure means the work couldn't be queued, but there's nothing
460+
// we can do about it here, so the return value can be safely ignored.
461+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0));
434462
}
435463
break;
436464

@@ -464,7 +492,10 @@ void AppTask::ButtonHandler(T_IO_MSG * p_msg)
464492
sAppTask.CancelTimer();
465493
sAppTask.mFunction = kFunction_NoneSelected;
466494

467-
PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0);
495+
// ScheduleWork is asynchronous and returns immediately.
496+
// Failure means the work couldn't be queued, but there's nothing
497+
// we can do about it here, so the return value can be safely ignored.
498+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0));
468499
}
469500
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
470501
{

examples/all-clusters-app/realtek/common/main/CHIPDeviceManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
8383
SetDeviceInstanceInfoProvider(&mFactoryDataProvider);
8484

8585
#if CONFIG_NETWORK_LAYER_BLE
86-
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
86+
err = ConnectivityMgr().SetBLEAdvertisingEnabled(true);
87+
SuccessOrExit(err);
8788
#endif
8889

89-
PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));
90+
err = PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));
91+
SuccessOrExit(err);
9092

9193
// Start a task to run the CHIP Device event loop.
9294
err = PlatformMgr().StartEventLoopTask();
@@ -114,7 +116,7 @@ CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
114116
#endif // CHIP_DEVICE_CONFIG_THREAD_FTD
115117
SuccessOrExit(err);
116118

117-
sThreadNetworkDriver.Init();
119+
(void) sThreadNetworkDriver.Init();
118120

119121
ChipLogProgress(DeviceLayer, "Start OpenThread task");
120122
err = ThreadStackMgrImpl().StartThreadTask();

examples/light-switch-app/realtek/common/main/AppTask.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ CHIP_ERROR AppTask::StartAppTask()
263263
void AppTask::AppTaskMain(void * pvParameter)
264264
{
265265
uint8_t event;
266+
CHIP_ERROR err = CHIP_NO_ERROR;
266267

267268
#if defined(FEATURE_TRUSTZONE_ENABLE) && (FEATURE_TRUSTZONE_ENABLE == 1)
268269
os_alloc_secure_ctx(1024);
@@ -274,7 +275,12 @@ void AppTask::AppTaskMain(void * pvParameter)
274275
gap_start_bt_stack(app_evt_queue_handle, app_io_queue_handle, MAX_NUMBER_OF_GAP_MESSAGE);
275276
matter_ble_queue_init(app_evt_queue_handle, app_io_queue_handle);
276277

277-
sAppTask.Init();
278+
err = sAppTask.Init();
279+
if (err != CHIP_NO_ERROR)
280+
{
281+
ChipLogError(NotSpecified, "sAppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
282+
return;
283+
}
278284

279285
while (true)
280286
{
@@ -315,6 +321,8 @@ void AppTask::AppTaskMain(void * pvParameter)
315321

316322
void AppTask::InitServer(intptr_t arg)
317323
{
324+
CHIP_ERROR err = CHIP_NO_ERROR;
325+
318326
// Init ZCL Data Model and start server
319327
static chip::CommonCaseDeviceServerInitParams initParams;
320328
(void) initParams.InitializeStaticResourcesBeforeServerInit();
@@ -336,12 +344,21 @@ void AppTask::InitServer(intptr_t arg)
336344
(void) initParams.InitializeStaticResourcesBeforeServerInit();
337345
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
338346

339-
chip::Server::GetInstance().Init(initParams);
347+
err = chip::Server::GetInstance().Init(initParams);
348+
if (err != CHIP_NO_ERROR)
349+
{
350+
ChipLogError(NotSpecified, "Server::GetInstance().Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
351+
return;
352+
}
340353

341354
InitTag();
342355

343356
static RealtekObserver sRealtekObserver;
344-
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sRealtekObserver);
357+
err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sRealtekObserver);
358+
if (err != CHIP_NO_ERROR)
359+
{
360+
ChipLogError(NotSpecified, "AddFabricDelegate failed: %" CHIP_ERROR_FORMAT, err.Format());
361+
}
345362

346363
ConfigurationMgr().LogDeviceConfig();
347364
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
@@ -378,7 +395,10 @@ CHIP_ERROR AppTask::Init()
378395
}
379396

380397
// Init ZCL Data Model and start server
381-
PlatformMgr().ScheduleWork(InitServer, 0);
398+
// ScheduleWork is asynchronous, failure means work couldn't be queued.
399+
// Since this is init time, if it fails the system won't work anyway,
400+
// so the return value can be safely ignored.
401+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(InitServer, 0));
382402

383403
#if CONFIG_ENABLE_CHIP_SHELL
384404
chip::Shell::Engine::Root().Init();

examples/light-switch-app/realtek/common/main/BindingHandler.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ void BindingHandler::Init()
5858
// The initialization of binding manager will try establishing connection with unicast peers
5959
// so it requires the Server instance to be correctly initialized. Post the init function to
6060
// the event queue so that everything is ready when initialization is conducted.
61-
chip::DeviceLayer::PlatformMgr().ScheduleWork(InitInternal);
61+
// ScheduleWork is asynchronous, failure means work couldn't be queued.
62+
// Since this is init time, if it fails the system won't work anyway,
63+
// so the return value can be safely ignored.
64+
RETURN_SAFELY_IGNORED(chip::DeviceLayer::PlatformMgr().ScheduleWork(InitInternal));
6265
#if CONFIG_ENABLE_CHIP_SHELL
6366
RegisterSwitchCommands();
6467
#endif
@@ -387,9 +390,15 @@ void BindingHandler::LightSwitchContextReleaseHandler(void * context)
387390
void BindingHandler::InitInternal(intptr_t arg)
388391
{
389392
ChipLogProgress(NotSpecified, "Initialize binding Handler");
390-
auto & server = chip::Server::GetInstance();
391-
Binding::Manager::GetInstance().Init(
393+
CHIP_ERROR bindErr = CHIP_NO_ERROR;
394+
auto & server = chip::Server::GetInstance();
395+
bindErr = Binding::Manager::GetInstance().Init(
392396
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
397+
if (bindErr != CHIP_NO_ERROR)
398+
{
399+
ChipLogError(NotSpecified, "Binding::Manager::Init() failed: %" CHIP_ERROR_FORMAT, bindErr.Format());
400+
return;
401+
}
393402
Binding::Manager::GetInstance().RegisterBoundDeviceChangedHandler(LightSwitchChangedHandler);
394403
Binding::Manager::GetInstance().RegisterBoundDeviceContextReleaseHandler(LightSwitchContextReleaseHandler);
395404
}
@@ -701,12 +710,21 @@ void BindingHandler::SwitchWorkerFunction(intptr_t context)
701710
#if CONFIG_ENABLE_CHIP_SHELL
702711
streamer_printf(streamer_get(), "Notify Bounded Cluster | endpoint: %d CLuster: %d\r\n", data->EndpointId, data->ClusterId);
703712
#endif
704-
Binding::Manager::GetInstance().NotifyBoundClusterChanged(data->EndpointId, data->ClusterId, static_cast<void *>(data));
713+
CHIP_ERROR notifyErr =
714+
Binding::Manager::GetInstance().NotifyBoundClusterChanged(data->EndpointId, data->ClusterId, static_cast<void *>(data));
715+
if (notifyErr != CHIP_NO_ERROR)
716+
{
717+
ChipLogError(NotSpecified, "NotifyBoundClusterChanged failed: %" CHIP_ERROR_FORMAT, notifyErr.Format());
718+
}
705719
}
706720

707721
void BindingHandler::SwitchWorkerFunction2(int localEndpointId)
708722
{
709-
Binding::Manager::GetInstance().NotifyBoundClusterChanged(localEndpointId, Clusters::OnOff::Id, nullptr);
723+
CHIP_ERROR notifyErr = Binding::Manager::GetInstance().NotifyBoundClusterChanged(localEndpointId, Clusters::OnOff::Id, nullptr);
724+
if (notifyErr != CHIP_NO_ERROR)
725+
{
726+
ChipLogError(NotSpecified, "NotifyBoundClusterChanged failed: %" CHIP_ERROR_FORMAT, notifyErr.Format());
727+
}
710728
}
711729

712730
void BindingHandler::SwitchWorkerFunction3(intptr_t context)

examples/light-switch-app/realtek/common/main/CHIPDeviceManager.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
8383
SetDeviceInstanceInfoProvider(&mFactoryDataProvider);
8484

8585
#if CONFIG_NETWORK_LAYER_BLE
86-
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
86+
err = ConnectivityMgr().SetBLEAdvertisingEnabled(true);
87+
SuccessOrExit(err);
8788
#endif
8889

89-
PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));
90+
err = PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));
91+
SuccessOrExit(err);
9092

9193
// Start a task to run the CHIP Device event loop.
9294
err = PlatformMgr().StartEventLoopTask();

examples/light-switch-app/realtek/common/main/LightSwitch.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ void LightSwitch::InitiateActionSwitch(chip::EndpointId endpointId, uint8_t acti
7474
Platform::Delete(data);
7575
return;
7676
}
77-
DeviceLayer::PlatformMgr().ScheduleWork(BindingHandler::SwitchWorkerFunction, reinterpret_cast<intptr_t>(data));
77+
// ScheduleWork is asynchronous and returns immediately.
78+
// Failure means the work couldn't be queued, but there's nothing
79+
// we can do about it here, so the return value can be safely ignored.
80+
RETURN_SAFELY_IGNORED(
81+
DeviceLayer::PlatformMgr().ScheduleWork(BindingHandler::SwitchWorkerFunction, reinterpret_cast<intptr_t>(data)));
7882
}
7983
}
8084

examples/lighting-app/realtek/common/main/AppTask.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ CHIP_ERROR AppTask::StartAppTask()
206206
void AppTask::AppTaskMain(void * pvParameter)
207207
{
208208
uint8_t event;
209+
CHIP_ERROR err = CHIP_NO_ERROR;
209210

210211
#if defined(FEATURE_TRUSTZONE_ENABLE) && (FEATURE_TRUSTZONE_ENABLE == 1)
211212
os_alloc_secure_ctx(1024);
@@ -217,7 +218,12 @@ void AppTask::AppTaskMain(void * pvParameter)
217218
gap_start_bt_stack(app_evt_queue_handle, app_io_queue_handle, MAX_NUMBER_OF_GAP_MESSAGE);
218219
matter_ble_queue_init(app_evt_queue_handle, app_io_queue_handle);
219220

220-
sAppTask.Init();
221+
err = sAppTask.Init();
222+
if (err != CHIP_NO_ERROR)
223+
{
224+
ChipLogError(NotSpecified, "sAppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
225+
return;
226+
}
221227

222228
while (true)
223229
{
@@ -258,6 +264,8 @@ void AppTask::AppTaskMain(void * pvParameter)
258264

259265
void AppTask::InitServer(intptr_t arg)
260266
{
267+
CHIP_ERROR err = CHIP_NO_ERROR;
268+
261269
// Init ZCL Data Model and start server
262270
static chip::CommonCaseDeviceServerInitParams initParams;
263271
(void) initParams.InitializeStaticResourcesBeforeServerInit();
@@ -279,10 +287,19 @@ void AppTask::InitServer(intptr_t arg)
279287
(void) initParams.InitializeStaticResourcesBeforeServerInit();
280288
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
281289

282-
chip::Server::GetInstance().Init(initParams);
290+
err = chip::Server::GetInstance().Init(initParams);
291+
if (err != CHIP_NO_ERROR)
292+
{
293+
ChipLogError(NotSpecified, "Server::GetInstance().Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
294+
return;
295+
}
283296

284297
static RealtekObserver sRealtekObserver;
285-
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sRealtekObserver);
298+
err = chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sRealtekObserver);
299+
if (err != CHIP_NO_ERROR)
300+
{
301+
ChipLogError(NotSpecified, "AddFabricDelegate failed: %" CHIP_ERROR_FORMAT, err.Format());
302+
}
286303

287304
ConfigurationMgr().LogDeviceConfig();
288305
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
@@ -321,7 +338,10 @@ CHIP_ERROR AppTask::Init()
321338
}
322339

323340
// Init ZCL Data Model and start server
324-
PlatformMgr().ScheduleWork(InitServer, 0);
341+
// ScheduleWork is asynchronous, failure means work couldn't be queued.
342+
// Since this is init time, if it fails the system won't work anyway,
343+
// so the return value can be safely ignored.
344+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(InitServer, 0));
325345

326346
#if CONFIG_ENABLE_CHIP_SHELL
327347
chip::Shell::Engine::Root().Init();
@@ -345,13 +365,18 @@ CHIP_ERROR AppTask::Init()
345365

346366
void AppTask::BLEStartAdvertising(intptr_t arg)
347367
{
368+
CHIP_ERROR setErr = CHIP_NO_ERROR;
348369
if (ConnectivityMgr().IsBLEAdvertisingEnabled())
349370
{
350-
ConnectivityMgr().SetBLEAdvertisingEnabled(false);
371+
setErr = ConnectivityMgr().SetBLEAdvertisingEnabled(false);
351372
}
352373
else
353374
{
354-
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
375+
setErr = ConnectivityMgr().SetBLEAdvertisingEnabled(true);
376+
}
377+
if (setErr != CHIP_NO_ERROR)
378+
{
379+
ChipLogError(NotSpecified, "SetBLEAdvertisingEnabled failed: %" CHIP_ERROR_FORMAT, setErr.Format());
355380
}
356381
}
357382

@@ -420,7 +445,10 @@ void AppTask::ButtonHandler(T_IO_MSG * p_msg)
420445
case APP_BLE_ADV_BUTTON:
421446
if (btnPressed)
422447
{
423-
PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0);
448+
// ScheduleWork is asynchronous and returns immediately.
449+
// Failure means the work couldn't be queued, but there's nothing
450+
// we can do about it here, so the return value can be safely ignored.
451+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0));
424452
}
425453
break;
426454

@@ -454,7 +482,10 @@ void AppTask::ButtonHandler(T_IO_MSG * p_msg)
454482
sAppTask.CancelTimer();
455483
sAppTask.mFunction = kFunction_NoneSelected;
456484

457-
PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0);
485+
// ScheduleWork is asynchronous and returns immediately.
486+
// Failure means the work couldn't be queued, but there's nothing
487+
// we can do about it here, so the return value can be safely ignored.
488+
RETURN_SAFELY_IGNORED(PlatformMgr().ScheduleWork(AppTask::BLEStartAdvertising, 0));
458489
}
459490
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
460491
{

0 commit comments

Comments
 (0)