From 33027412e2d13466b1567696038d5858a57ffdb3 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Wed, 16 Nov 2022 14:54:34 +0100 Subject: [PATCH] [QPG] Move AppTask::Init function to reduce stack usage (#23595) * Moved AppTask Init function to Application_Init * Cleanup * Updated lock app with SDP011-1042 content * Missing UpdateLEDs call added * Fixed shell app build --- examples/lighting-app/qpg/include/AppTask.h | 2 +- examples/lighting-app/qpg/src/AppTask.cpp | 9 +- examples/lock-app/qpg/include/AppTask.h | 5 +- examples/lock-app/qpg/src/AppTask.cpp | 116 +++++++++++--------- examples/platform/qpg/app/main.cpp | 11 +- examples/shell/qpg/include/AppTask.h | 1 + examples/shell/qpg/src/AppTask.cpp | 5 + 7 files changed, 87 insertions(+), 62 deletions(-) diff --git a/examples/lighting-app/qpg/include/AppTask.h b/examples/lighting-app/qpg/include/AppTask.h index 7651124669546d..66c3003df11b0b 100644 --- a/examples/lighting-app/qpg/include/AppTask.h +++ b/examples/lighting-app/qpg/include/AppTask.h @@ -37,6 +37,7 @@ class AppTask { public: + CHIP_ERROR Init(); CHIP_ERROR StartAppTask(); static void AppTaskMain(void * pvParameter); @@ -49,7 +50,6 @@ class AppTask private: friend AppTask & GetAppTask(void); - CHIP_ERROR Init(); static void InitServer(intptr_t arg); static void OpenCommissioning(intptr_t arg); diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 27724b1f271329..d7dd3c282791f6 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -289,6 +289,8 @@ CHIP_ERROR AppTask::Init() ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + UpdateLEDs(); + return err; } @@ -296,13 +298,6 @@ void AppTask::AppTaskMain(void * pvParameter) { AppEvent event; - CHIP_ERROR err = sAppTask.Init(); - if (err != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "AppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format()); - return; - } - while (true) { BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY); diff --git a/examples/lock-app/qpg/include/AppTask.h b/examples/lock-app/qpg/include/AppTask.h index 997257ed8352bf..3219c5676036f9 100644 --- a/examples/lock-app/qpg/include/AppTask.h +++ b/examples/lock-app/qpg/include/AppTask.h @@ -41,6 +41,7 @@ class AppTask { public: + CHIP_ERROR Init(); CHIP_ERROR StartAppTask(); static void AppTaskMain(void * pvParameter); @@ -53,7 +54,6 @@ class AppTask private: friend AppTask & GetAppTask(void); - CHIP_ERROR Init(); static void InitServer(intptr_t arg); static void ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor); @@ -68,6 +68,9 @@ class AppTask static void LockActionEventHandler(AppEvent * aEvent); static void TimerEventHandler(chip::System::Layer * aLayer, void * aAppState); + static void MatterEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + static void UpdateLEDs(void); + void StartTimer(uint32_t aTimeoutMs); enum Function_t diff --git a/examples/lock-app/qpg/src/AppTask.cpp b/examples/lock-app/qpg/src/AppTask.cpp index 993a91068049b9..bc24d7fbbe8b46 100644 --- a/examples/lock-app/qpg/src/AppTask.cpp +++ b/examples/lock-app/qpg/src/AppTask.cpp @@ -54,7 +54,7 @@ using namespace chip::DeviceLayer; #define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000 #define OTA_START_TRIGGER_TIMEOUT 1500 -#define APP_TASK_STACK_SIZE (3 * 1024) +#define APP_TASK_STACK_SIZE (2 * 1024) #define APP_TASK_PRIORITY 2 #define APP_EVENT_QUEUE_SIZE 10 #define QPG_LOCK_ENDPOINT_ID (1) @@ -135,6 +135,8 @@ CHIP_ERROR AppTask::Init() { CHIP_ERROR err = CHIP_NO_ERROR; + PlatformMgr().AddEventHandler(MatterEventHandler, 0); + ChipLogProgress(NotSpecified, "Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); // Init ZCL Data Model and start server @@ -165,6 +167,8 @@ CHIP_ERROR AppTask::Init() ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + UpdateLEDs(); + return err; } @@ -172,63 +176,14 @@ void AppTask::AppTaskMain(void * pvParameter) { AppEvent event; - CHIP_ERROR err = sAppTask.Init(); - if (err != CHIP_NO_ERROR) - { - ChipLogError(NotSpecified, "AppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format()); - } - - ChipLogProgress(NotSpecified, "App Task started"); - while (true) { - BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10)); + BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY); while (eventReceived == pdTRUE) { sAppTask.DispatchEvent(&event); eventReceived = xQueueReceive(sAppEventQueue, &event, 0); } - - // Collect connectivity and configuration state from the CHIP stack. Because - // the CHIP event loop is being run in a separate task, the stack must be - // locked while these values are queried. However we use a non-blocking - // lock request (TryLockCHIPStack()) to avoid blocking other UI activities - // when the CHIP task is busy (e.g. with a long crypto operation). - if (PlatformMgr().TryLockChipStack()) - { - sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); - sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); - sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0); - PlatformMgr().UnlockChipStack(); - } - - // Update the status LED if factory reset has not been initiated. - // - // If system has "full connectivity", keep the LED On constantly. - // - // If thread and service provisioned, but not attached to the thread network - // yet OR no connectivity to the service OR subscriptions are not fully - // established THEN blink the LED Off for a short period of time. - // - // If the system has ble connection(s) uptill the stage above, THEN blink - // the LEDs at an even rate of 100ms. - // - // Otherwise, blink the LED ON for a very short time. - if (sAppTask.mFunction != kFunction_FactoryReset) - { - if (sIsThreadProvisioned && sIsThreadEnabled) - { - qvIO_LedBlink(SYSTEM_STATE_LED, 950, 50); - } - else if (sHaveBLEConnections) - { - qvIO_LedBlink(SYSTEM_STATE_LED, 100, 100); - } - else - { - qvIO_LedBlink(SYSTEM_STATE_LED, 50, 950); - } - } } } @@ -556,3 +511,62 @@ void AppTask::UpdateClusterState(void) ChipLogError(NotSpecified, "ERR: updating DoorLock %x", status); } } + +void AppTask::UpdateLEDs(void) +{ + // If system has "full connectivity", keep the LED On constantly. + // + // If thread and service provisioned, but not attached to the thread network + // yet OR no connectivity to the service OR subscriptions are not fully + // established THEN blink the LED Off for a short period of time. + // + // If the system has ble connection(s) uptill the stage above, THEN blink + // the LEDs at an even rate of 100ms. + // + // Otherwise, blink the LED ON for a very short time. + if (sIsThreadProvisioned && sIsThreadEnabled) + { + qvIO_LedBlink(SYSTEM_STATE_LED, 950, 50); + } + else if (sHaveBLEConnections) + { + qvIO_LedBlink(SYSTEM_STATE_LED, 100, 100); + } + else + { + qvIO_LedBlink(SYSTEM_STATE_LED, 50, 950); + } +} + +void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) +{ + switch (event->Type) + { + case DeviceEventType::kServiceProvisioningChange: { + sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; + UpdateLEDs(); + break; + } + + case DeviceEventType::kThreadConnectivityChange: { + sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); + UpdateLEDs(); + break; + } + + case DeviceEventType::kCHIPoBLEConnectionEstablished: { + sHaveBLEConnections = true; + UpdateLEDs(); + break; + } + + case DeviceEventType::kCHIPoBLEConnectionClosed: { + sHaveBLEConnections = false; + UpdateLEDs(); + break; + } + + default: + break; + } +} diff --git a/examples/platform/qpg/app/main.cpp b/examples/platform/qpg/app/main.cpp index c5ff8b9869daa5..3c21833f1362b6 100644 --- a/examples/platform/qpg/app/main.cpp +++ b/examples/platform/qpg/app/main.cpp @@ -97,12 +97,19 @@ void Application_Init(void) ChipLogProgress(NotSpecified, "Qorvo " APP_NAME " Launching"); ChipLogProgress(NotSpecified, "============================"); - CHIP_ERROR ret = GetAppTask().StartAppTask(); - if (ret != CHIP_NO_ERROR) + error = GetAppTask().Init(); + if (error != CHIP_NO_ERROR) { ChipLogError(NotSpecified, "GetAppTask().Init() failed"); return; } + + error = GetAppTask().StartAppTask(); + if (error != CHIP_NO_ERROR) + { + ChipLogError(NotSpecified, "GetAppTask().StartAppTask() failed"); + return; + } } void ChipEventHandler(const ChipDeviceEvent * aEvent, intptr_t /* arg */) diff --git a/examples/shell/qpg/include/AppTask.h b/examples/shell/qpg/include/AppTask.h index 45ea3bcb5b00f9..fb449697fb3959 100644 --- a/examples/shell/qpg/include/AppTask.h +++ b/examples/shell/qpg/include/AppTask.h @@ -29,6 +29,7 @@ class AppTask { public: + CHIP_ERROR Init(); CHIP_ERROR StartAppTask(); private: diff --git a/examples/shell/qpg/src/AppTask.cpp b/examples/shell/qpg/src/AppTask.cpp index fcad76e3855b45..c9c6af3261424c 100644 --- a/examples/shell/qpg/src/AppTask.cpp +++ b/examples/shell/qpg/src/AppTask.cpp @@ -26,3 +26,8 @@ CHIP_ERROR AppTask::StartAppTask() { return CHIP_NO_ERROR; } + +CHIP_ERROR AppTask::Init() +{ + return CHIP_NO_ERROR; +}