diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index d43b50f5780053..c2418d6b089d75 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -34,8 +34,9 @@ #include #include #include +#include +#include #include -#include #include #if CONFIG_CHIP_OTA_REQUESTOR @@ -90,8 +91,39 @@ chip::OTARequestor sOTARequestor; AppTask AppTask::sAppTask; -int AppTask::Init() +CHIP_ERROR AppTask::Init() { + // Initialize CHIP stack + LOG_INF("Init CHIP stack"); + + CHIP_ERROR err = chip::Platform::MemoryInit(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("Platform::MemoryInit() failed"); + return err; + } + + err = PlatformMgr().InitChipStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("PlatformMgr().InitChipStack() failed"); + return err; + } + + err = ThreadStackMgr().InitThreadStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); + return err; + } + + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); + return err; + } + // Initialize LEDs LEDWidget::InitGpio(); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); @@ -102,48 +134,52 @@ int AppTask::Init() UpdateStatusLED(); + int ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL); + if (ret != 0) + { + return chip::System::MapErrorZephyr(ret); + } + + LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted); + // Initialize buttons - int ret = dk_buttons_init(ButtonEventHandler); + ret = dk_buttons_init(ButtonEventHandler); if (ret) { LOG_ERR("dk_buttons_init() failed"); - return ret; + return chip::System::MapErrorZephyr(ret); } - // Initialize timer user data + // Initialize function button timer k_timer_init(&sFunctionTimer, &AppTask::TimerEventHandler, nullptr); k_timer_user_data_set(&sFunctionTimer, this); #ifdef CONFIG_MCUMGR_SMP_BT + // Initialize DFU over SMP GetDFUOverSMP().Init(RequestSMPAdvertisingStart); GetDFUOverSMP().ConfirmNewImage(); #endif - ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL); - if (ret != 0) - return ret; - - LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted); - - // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); - - // Initialize device attestation config + // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); + InitOTARequestor(); + chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(kExtDiscoveryTimeoutSecs); + ReturnErrorOnFailure(chip::Server::GetInstance().Init()); ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); -#if defined(CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY) - chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(kExtDiscoveryTimeoutSecs); -#endif - -#if defined(CONFIG_CHIP_NFC_COMMISSIONING) + // Add CHIP event handler and start CHIP thread. + // Note that all the initialization code should happen prior to this point to avoid data races + // between the main and the CHIP threads. PlatformMgr().AddEventHandler(ChipEventHandler, 0); -#endif - InitOTARequestor(); + err = PlatformMgr().StartEventLoopTask(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); + } - return 0; + return err; } void AppTask::InitOTARequestor() @@ -157,15 +193,9 @@ void AppTask::InitOTARequestor() #endif } -int AppTask::StartApp() +CHIP_ERROR AppTask::StartApp() { - int ret = Init(); - - if (ret) - { - LOG_ERR("AppTask.Init() failed"); - return ret; - } + ReturnErrorOnFailure(Init()); AppEvent event = {}; @@ -174,6 +204,8 @@ int AppTask::StartApp() k_msgq_get(&sAppEventQueue, &event, K_FOREVER); DispatchEvent(&event); } + + return CHIP_NO_ERROR; } void AppTask::LightingActionEventHandler(AppEvent * aEvent) diff --git a/examples/lighting-app/nrfconnect/main/include/AppTask.h b/examples/lighting-app/nrfconnect/main/include/AppTask.h index 45336ba6c32a7d..2f635e8d44afbb 100644 --- a/examples/lighting-app/nrfconnect/main/include/AppTask.h +++ b/examples/lighting-app/nrfconnect/main/include/AppTask.h @@ -40,7 +40,7 @@ struct Identify; class AppTask { public: - int StartApp(); + CHIP_ERROR StartApp(); void PostLightingActionRequest(LightingManager::Action_t aAction); void PostEvent(AppEvent * event); @@ -55,7 +55,7 @@ class AppTask #endif friend AppTask & GetAppTask(void); - int Init(); + CHIP_ERROR Init(); void InitOTARequestor(); static void ActionInitiated(LightingManager::Action_t aAction, int32_t aActor); diff --git a/examples/lighting-app/nrfconnect/main/main.cpp b/examples/lighting-app/nrfconnect/main/main.cpp index 2ec7a58ff062a0..04afe27bd8dd53 100644 --- a/examples/lighting-app/nrfconnect/main/main.cpp +++ b/examples/lighting-app/nrfconnect/main/main.cpp @@ -18,11 +18,9 @@ #include "AppTask.h" -#include -#include #include -#include +#include #ifdef CONFIG_CHIP_PW_RPC #include "Rpc.h" @@ -35,73 +33,28 @@ LOG_MODULE_REGISTER(app); using namespace ::chip; -using namespace ::chip::Inet; -using namespace ::chip::DeviceLayer; -int main(void) +int main() { + CHIP_ERROR err = CHIP_NO_ERROR; + #ifdef CONFIG_CHIP_PW_RPC - chip::rpc::Init(); + rpc::Init(); #endif - int ret = 0; - CHIP_ERROR err = CHIP_NO_ERROR; - #ifdef CONFIG_USB_DEVICE_STACK - ret = usb_enable(nullptr); - if (ret) + err = System::MapErrorZephyr(usb_enable(nullptr)); + if (err != CHIP_NO_ERROR) { LOG_ERR("Failed to initialize USB device"); - err = chip::System::MapErrorZephyr(ret); - goto exit; } #endif - err = chip::Platform::MemoryInit(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("Platform::MemoryInit() failed"); - goto exit; - } - - LOG_INF("Init CHIP stack"); - err = PlatformMgr().InitChipStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().InitChipStack() failed"); - goto exit; - } - - LOG_INF("Starting CHIP task"); - err = PlatformMgr().StartEventLoopTask(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); - goto exit; - } - - LOG_INF("Init Thread stack"); - err = ThreadStackMgr().InitThreadStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); - goto exit; - } - - err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); - goto exit; - } - - ret = GetAppTask().StartApp(); - if (ret != 0) + if (err == CHIP_NO_ERROR) { - err = chip::System::MapErrorZephyr(ret); + err = GetAppTask().StartApp(); } -exit: LOG_ERR("Exited with code %" CHIP_ERROR_FORMAT, err.Format()); return err == CHIP_NO_ERROR ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index 670b27f0665c12..c975c78c2f8f14 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -21,18 +21,19 @@ #include "BoltLockManager.h" #include "LEDWidget.h" #include "ThreadUtil.h" -#include -#include #include #include #include +#include +#include #include - #include #include - -#include +#include +#include +#include +#include #include #include @@ -63,8 +64,43 @@ using namespace ::chip::DeviceLayer; AppTask AppTask::sAppTask; -int AppTask::Init() +CHIP_ERROR AppTask::Init() { + // Initialize CHIP stack + LOG_INF("Init CHIP stack"); + + CHIP_ERROR err = chip::Platform::MemoryInit(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("Platform::MemoryInit() failed"); + return err; + } + + err = PlatformMgr().InitChipStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("PlatformMgr().InitChipStack() failed"); + return err; + } + + err = ThreadStackMgr().InitThreadStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); + return err; + } + +#ifdef CONFIG_OPENTHREAD_MTD_SED + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice); +#else + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); +#endif + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); + return err; + } + // Initialize LEDs LEDWidget::InitGpio(); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); @@ -78,50 +114,51 @@ int AppTask::Init() UpdateStatusLED(); + BoltLockMgr().Init(); + BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted); + // Initialize buttons int ret = dk_buttons_init(ButtonEventHandler); if (ret) { LOG_ERR("dk_buttons_init() failed"); - return ret; + return chip::System::MapErrorZephyr(ret); } - // Initialize timer user data + // Initialize function button timer k_timer_init(&sFunctionTimer, &AppTask::TimerEventHandler, nullptr); k_timer_user_data_set(&sFunctionTimer, this); #ifdef CONFIG_MCUMGR_SMP_BT + // Initialize DFU over SMP GetDFUOverSMP().Init(RequestSMPAdvertisingStart); GetDFUOverSMP().ConfirmNewImage(); #endif - BoltLockMgr().Init(); - BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted); - - // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); - - // Initialize device attestation config + // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); + ReturnErrorOnFailure(chip::Server::GetInstance().Init()); ConfigurationMgr().LogDeviceConfig(); - PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); + PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); -#ifdef CONFIG_CHIP_NFC_COMMISSIONING + // Add CHIP event handler and start CHIP thread. + // Note that all the initialization code should happen prior to this point to avoid data races + // between the main and the CHIP threads. PlatformMgr().AddEventHandler(ChipEventHandler, 0); -#endif - return 0; -} - -int AppTask::StartApp() -{ - int ret = Init(); - if (ret) + err = PlatformMgr().StartEventLoopTask(); + if (err != CHIP_NO_ERROR) { - LOG_ERR("AppTask.Init() failed"); - return ret; + LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); } + return err; +} + +CHIP_ERROR AppTask::StartApp() +{ + ReturnErrorOnFailure(Init()); + AppEvent event = {}; while (true) @@ -129,6 +166,8 @@ int AppTask::StartApp() k_msgq_get(&sAppEventQueue, &event, K_FOREVER); DispatchEvent(&event); } + + return CHIP_NO_ERROR; } void AppTask::LockActionEventHandler(AppEvent * aEvent) diff --git a/examples/lock-app/nrfconnect/main/include/AppTask.h b/examples/lock-app/nrfconnect/main/include/AppTask.h index 524cfc85c24db2..2789d15a41e229 100644 --- a/examples/lock-app/nrfconnect/main/include/AppTask.h +++ b/examples/lock-app/nrfconnect/main/include/AppTask.h @@ -34,7 +34,7 @@ struct k_timer; class AppTask { public: - int StartApp(); + CHIP_ERROR StartApp(); void PostLockActionRequest(int32_t aActor, BoltLockManager::Action_t aAction); void PostEvent(AppEvent * event); @@ -43,7 +43,7 @@ class AppTask private: friend AppTask & GetAppTask(void); - int Init(); + CHIP_ERROR Init(); static void ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor); static void ActionCompleted(BoltLockManager::Action_t aAction, int32_t aActor); diff --git a/examples/lock-app/nrfconnect/main/main.cpp b/examples/lock-app/nrfconnect/main/main.cpp index 6b1d7e2496378f..e1c0c6467e027e 100644 --- a/examples/lock-app/nrfconnect/main/main.cpp +++ b/examples/lock-app/nrfconnect/main/main.cpp @@ -21,69 +21,14 @@ #include -#include -#include - LOG_MODULE_REGISTER(app); using namespace ::chip; -using namespace ::chip::Inet; -using namespace ::chip::DeviceLayer; int main() { - int ret = 0; - CHIP_ERROR err = CHIP_NO_ERROR; - - err = chip::Platform::MemoryInit(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("Platform::MemoryInit() failed"); - goto exit; - } - - LOG_INF("Init CHIP stack"); - err = PlatformMgr().InitChipStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().InitChipStack() failed"); - goto exit; - } - - LOG_INF("Starting CHIP task"); - err = PlatformMgr().StartEventLoopTask(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); - goto exit; - } - - LOG_INF("Init Thread stack"); - err = ThreadStackMgr().InitThreadStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); - goto exit; - } - -#ifdef CONFIG_OPENTHREAD_MTD_SED - err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice); -#else - err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); -#endif - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); - goto exit; - } - - ret = GetAppTask().StartApp(); - if (ret != 0) - { - err = chip::System::MapErrorZephyr(ret); - } + CHIP_ERROR err = GetAppTask().StartApp(); -exit: LOG_ERR("Exited with code %" CHIP_ERROR_FORMAT, err.Format()); return err == CHIP_NO_ERROR ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/examples/pump-app/nrfconnect/main/AppTask.cpp b/examples/pump-app/nrfconnect/main/AppTask.cpp index 1b057c793a40a3..50dbb5224b5533 100644 --- a/examples/pump-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-app/nrfconnect/main/AppTask.cpp @@ -21,19 +21,20 @@ #include "LEDWidget.h" #include "PumpManager.h" #include "ThreadUtil.h" -#include -#include #include #include #include #include +#include +#include #include - #include #include - -#include +#include +#include +#include +#include #include #include @@ -68,8 +69,39 @@ using namespace ::chip::app::Clusters; AppTask AppTask::sAppTask; -int AppTask::Init() +CHIP_ERROR AppTask::Init() { + // Initialize CHIP stack + LOG_INF("Init CHIP stack"); + + CHIP_ERROR err = chip::Platform::MemoryInit(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("Platform::MemoryInit() failed"); + return err; + } + + err = PlatformMgr().InitChipStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("PlatformMgr().InitChipStack() failed"); + return err; + } + + err = ThreadStackMgr().InitThreadStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); + return err; + } + + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); + return err; + } + // Initialize LEDs LEDWidget::InitGpio(); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); @@ -83,50 +115,51 @@ int AppTask::Init() UpdateStatusLED(); + PumpMgr().Init(); + PumpMgr().SetCallbacks(ActionInitiated, ActionCompleted); + // Initialize buttons int ret = dk_buttons_init(ButtonEventHandler); if (ret) { LOG_ERR("dk_buttons_init() failed"); - return ret; + return chip::System::MapErrorZephyr(ret); } - // Initialize timer user data + // Initialize function button timer k_timer_init(&sFunctionTimer, &AppTask::TimerEventHandler, nullptr); k_timer_user_data_set(&sFunctionTimer, this); #ifdef CONFIG_MCUMGR_SMP_BT + // Initialize DFU over SMP GetDFUOverSMP().Init(RequestSMPAdvertisingStart); GetDFUOverSMP().ConfirmNewImage(); #endif - PumpMgr().Init(); - PumpMgr().SetCallbacks(ActionInitiated, ActionCompleted); - - // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); - - // Initialize device attestation config + // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); + ReturnErrorOnFailure(chip::Server::GetInstance().Init()); ConfigurationMgr().LogDeviceConfig(); - PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); + PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); -#if defined(CONFIG_CHIP_NFC_COMMISSIONING) || defined(CONFIG_MCUMGR_SMP_BT) + // Add CHIP event handler and start CHIP thread. + // Note that all the initialization code should happen prior to this point to avoid data races + // between the main and the CHIP threads. PlatformMgr().AddEventHandler(ChipEventHandler, 0); -#endif - return 0; -} - -int AppTask::StartApp() -{ - int ret = Init(); - if (ret) + err = PlatformMgr().StartEventLoopTask(); + if (err != CHIP_NO_ERROR) { - LOG_ERR("AppTask.Init() failed"); - return ret; + LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); } + return err; +} + +CHIP_ERROR AppTask::StartApp() +{ + ReturnErrorOnFailure(Init()); + AppEvent event = {}; while (true) @@ -134,6 +167,8 @@ int AppTask::StartApp() k_msgq_get(&sAppEventQueue, &event, K_FOREVER); DispatchEvent(&event); } + + return CHIP_NO_ERROR; } void AppTask::StartActionEventHandler(AppEvent * aEvent) diff --git a/examples/pump-app/nrfconnect/main/include/AppTask.h b/examples/pump-app/nrfconnect/main/include/AppTask.h index 61065ce73fc0c8..7ca19cdc923f5c 100644 --- a/examples/pump-app/nrfconnect/main/include/AppTask.h +++ b/examples/pump-app/nrfconnect/main/include/AppTask.h @@ -34,7 +34,7 @@ struct k_timer; class AppTask { public: - int StartApp(); + CHIP_ERROR StartApp(); void PostStartActionRequest(int32_t aActor, PumpManager::Action_t aAction); void PostEvent(AppEvent * event); @@ -43,7 +43,7 @@ class AppTask private: friend AppTask & GetAppTask(void); - int Init(); + CHIP_ERROR Init(); static void ActionInitiated(PumpManager::Action_t aAction, int32_t aActor); static void ActionCompleted(PumpManager::Action_t aAction, int32_t aActor); diff --git a/examples/pump-app/nrfconnect/main/main.cpp b/examples/pump-app/nrfconnect/main/main.cpp index b6c344116e4646..e1c0c6467e027e 100644 --- a/examples/pump-app/nrfconnect/main/main.cpp +++ b/examples/pump-app/nrfconnect/main/main.cpp @@ -21,65 +21,14 @@ #include -#include -#include - LOG_MODULE_REGISTER(app); using namespace ::chip; -using namespace ::chip::Inet; -using namespace ::chip::DeviceLayer; int main() { - int ret = 0; - CHIP_ERROR err = CHIP_NO_ERROR; - - err = chip::Platform::MemoryInit(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("Platform::MemoryInit() failed"); - goto exit; - } - - LOG_INF("Init CHIP stack"); - err = PlatformMgr().InitChipStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().InitChipStack() failed"); - goto exit; - } - - LOG_INF("Starting CHIP task"); - err = PlatformMgr().StartEventLoopTask(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); - goto exit; - } - - LOG_INF("Init Thread stack"); - err = ThreadStackMgr().InitThreadStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); - goto exit; - } - - err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); - goto exit; - } - - ret = GetAppTask().StartApp(); - if (ret != 0) - { - err = chip::System::MapErrorZephyr(ret); - } + CHIP_ERROR err = GetAppTask().StartApp(); -exit: LOG_ERR("Exited with code %" CHIP_ERROR_FORMAT, err.Format()); return err == CHIP_NO_ERROR ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp index dd4cd3d88a6b38..267bfaf7fa5926 100644 --- a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp @@ -21,19 +21,20 @@ #include "LEDWidget.h" #include "PumpManager.h" #include "ThreadUtil.h" -#include -#include #include #include #include #include +#include +#include #include - #include #include - -#include +#include +#include +#include +#include #include #include @@ -64,8 +65,39 @@ using namespace ::chip::DeviceLayer; AppTask AppTask::sAppTask; -int AppTask::Init() +CHIP_ERROR AppTask::Init() { + // Initialize CHIP stack + LOG_INF("Init CHIP stack"); + + CHIP_ERROR err = chip::Platform::MemoryInit(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("Platform::MemoryInit() failed"); + return err; + } + + err = PlatformMgr().InitChipStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("PlatformMgr().InitChipStack() failed"); + return err; + } + + err = ThreadStackMgr().InitThreadStack(); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); + return err; + } + + err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); + return err; + } + // Initialize LEDs LEDWidget::InitGpio(); LEDWidget::SetStateUpdateCallback(LEDStateUpdateHandler); @@ -79,50 +111,51 @@ int AppTask::Init() UpdateStatusLED(); + PumpMgr().Init(); + PumpMgr().SetCallbacks(ActionInitiated, ActionCompleted); + // Initialize buttons int ret = dk_buttons_init(ButtonEventHandler); if (ret) { LOG_ERR("dk_buttons_init() failed"); - return ret; + return chip::System::MapErrorZephyr(ret); } - // Initialize timer user data + // Initialize function button timer k_timer_init(&sFunctionTimer, &AppTask::TimerEventHandler, nullptr); k_timer_user_data_set(&sFunctionTimer, this); #ifdef CONFIG_MCUMGR_SMP_BT + // Initialize DFU over SMP GetDFUOverSMP().Init(RequestSMPAdvertisingStart); GetDFUOverSMP().ConfirmNewImage(); #endif - PumpMgr().Init(); - PumpMgr().SetCallbacks(ActionInitiated, ActionCompleted); - - // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); - - // Initialize device attestation config + // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); + ReturnErrorOnFailure(chip::Server::GetInstance().Init()); ConfigurationMgr().LogDeviceConfig(); - PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); + PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); -#if defined(CONFIG_CHIP_NFC_COMMISSIONING) || defined(CONFIG_MCUMGR_SMP_BT) + // Add CHIP event handler and start CHIP thread. + // Note that all the initialization code should happen prior to this point to avoid data races + // between the main and the CHIP threads. PlatformMgr().AddEventHandler(ChipEventHandler, 0); -#endif - return 0; -} - -int AppTask::StartApp() -{ - int ret = Init(); - if (ret) + err = PlatformMgr().StartEventLoopTask(); + if (err != CHIP_NO_ERROR) { - LOG_ERR("AppTask.Init() failed"); - return ret; + LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); } + return err; +} + +CHIP_ERROR AppTask::StartApp() +{ + ReturnErrorOnFailure(Init()); + AppEvent event = {}; while (true) @@ -130,6 +163,8 @@ int AppTask::StartApp() k_msgq_get(&sAppEventQueue, &event, K_FOREVER); DispatchEvent(&event); } + + return CHIP_NO_ERROR; } void AppTask::StartActionEventHandler(AppEvent * aEvent) diff --git a/examples/pump-controller-app/nrfconnect/main/include/AppTask.h b/examples/pump-controller-app/nrfconnect/main/include/AppTask.h index 61065ce73fc0c8..7ca19cdc923f5c 100644 --- a/examples/pump-controller-app/nrfconnect/main/include/AppTask.h +++ b/examples/pump-controller-app/nrfconnect/main/include/AppTask.h @@ -34,7 +34,7 @@ struct k_timer; class AppTask { public: - int StartApp(); + CHIP_ERROR StartApp(); void PostStartActionRequest(int32_t aActor, PumpManager::Action_t aAction); void PostEvent(AppEvent * event); @@ -43,7 +43,7 @@ class AppTask private: friend AppTask & GetAppTask(void); - int Init(); + CHIP_ERROR Init(); static void ActionInitiated(PumpManager::Action_t aAction, int32_t aActor); static void ActionCompleted(PumpManager::Action_t aAction, int32_t aActor); diff --git a/examples/pump-controller-app/nrfconnect/main/main.cpp b/examples/pump-controller-app/nrfconnect/main/main.cpp index b6c344116e4646..e1c0c6467e027e 100644 --- a/examples/pump-controller-app/nrfconnect/main/main.cpp +++ b/examples/pump-controller-app/nrfconnect/main/main.cpp @@ -21,65 +21,14 @@ #include -#include -#include - LOG_MODULE_REGISTER(app); using namespace ::chip; -using namespace ::chip::Inet; -using namespace ::chip::DeviceLayer; int main() { - int ret = 0; - CHIP_ERROR err = CHIP_NO_ERROR; - - err = chip::Platform::MemoryInit(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("Platform::MemoryInit() failed"); - goto exit; - } - - LOG_INF("Init CHIP stack"); - err = PlatformMgr().InitChipStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().InitChipStack() failed"); - goto exit; - } - - LOG_INF("Starting CHIP task"); - err = PlatformMgr().StartEventLoopTask(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("PlatformMgr().StartEventLoopTask() failed"); - goto exit; - } - - LOG_INF("Init Thread stack"); - err = ThreadStackMgr().InitThreadStack(); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ThreadStackMgr().InitThreadStack() failed"); - goto exit; - } - - err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_MinimalEndDevice); - if (err != CHIP_NO_ERROR) - { - LOG_ERR("ConnectivityMgr().SetThreadDeviceType() failed"); - goto exit; - } - - ret = GetAppTask().StartApp(); - if (ret != 0) - { - err = chip::System::MapErrorZephyr(ret); - } + CHIP_ERROR err = GetAppTask().StartApp(); -exit: LOG_ERR("Exited with code %" CHIP_ERROR_FORMAT, err.Format()); return err == CHIP_NO_ERROR ? EXIT_SUCCESS : EXIT_FAILURE; }