Skip to content

Commit

Permalink
[QPG] Move AppTask::Init function to reduce stack usage (#23595)
Browse files Browse the repository at this point in the history
* Moved AppTask Init function to Application_Init

* Cleanup

* Updated lock app with SDP011-1042 content

* Missing UpdateLEDs call added

* Fixed shell app build
  • Loading branch information
adamb-q authored and pull[bot] committed Jan 19, 2024
1 parent ef1b39c commit 1579506
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 62 deletions.
2 changes: 1 addition & 1 deletion examples/lighting-app/qpg/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AppTask
{

public:
CHIP_ERROR Init();
CHIP_ERROR StartAppTask();
static void AppTaskMain(void * pvParameter);

Expand All @@ -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);

Expand Down
9 changes: 2 additions & 7 deletions examples/lighting-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,15 @@ CHIP_ERROR AppTask::Init()
ConfigurationMgr().LogDeviceConfig();
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

UpdateLEDs();

return err;
}

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);
Expand Down
5 changes: 4 additions & 1 deletion examples/lock-app/qpg/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AppTask
{

public:
CHIP_ERROR Init();
CHIP_ERROR StartAppTask();
static void AppTaskMain(void * pvParameter);

Expand All @@ -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);
Expand All @@ -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
Expand Down
116 changes: 65 additions & 51 deletions examples/lock-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -165,70 +167,23 @@ CHIP_ERROR AppTask::Init()
ConfigurationMgr().LogDeviceConfig();
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));

UpdateLEDs();

return err;
}

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);
}
}
}
}

Expand Down Expand Up @@ -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;
}
}
11 changes: 9 additions & 2 deletions examples/platform/qpg/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */)
Expand Down
1 change: 1 addition & 0 deletions examples/shell/qpg/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AppTask
{

public:
CHIP_ERROR Init();
CHIP_ERROR StartAppTask();

private:
Expand Down
5 changes: 5 additions & 0 deletions examples/shell/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ CHIP_ERROR AppTask::StartAppTask()
{
return CHIP_NO_ERROR;
}

CHIP_ERROR AppTask::Init()
{
return CHIP_NO_ERROR;
}

0 comments on commit 1579506

Please sign in to comment.