Skip to content

Commit 1553559

Browse files
adamb-qpull[bot]
authored andcommitted
[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
1 parent 9575803 commit 1553559

File tree

7 files changed

+87
-62
lines changed

7 files changed

+87
-62
lines changed

examples/lighting-app/qpg/include/AppTask.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class AppTask
3737
{
3838

3939
public:
40+
CHIP_ERROR Init();
4041
CHIP_ERROR StartAppTask();
4142
static void AppTaskMain(void * pvParameter);
4243

@@ -49,7 +50,6 @@ class AppTask
4950
private:
5051
friend AppTask & GetAppTask(void);
5152

52-
CHIP_ERROR Init();
5353
static void InitServer(intptr_t arg);
5454
static void OpenCommissioning(intptr_t arg);
5555

examples/lighting-app/qpg/src/AppTask.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -289,20 +289,15 @@ CHIP_ERROR AppTask::Init()
289289
ConfigurationMgr().LogDeviceConfig();
290290
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
291291

292+
UpdateLEDs();
293+
292294
return err;
293295
}
294296

295297
void AppTask::AppTaskMain(void * pvParameter)
296298
{
297299
AppEvent event;
298300

299-
CHIP_ERROR err = sAppTask.Init();
300-
if (err != CHIP_NO_ERROR)
301-
{
302-
ChipLogError(NotSpecified, "AppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
303-
return;
304-
}
305-
306301
while (true)
307302
{
308303
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY);

examples/lock-app/qpg/include/AppTask.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AppTask
4141
{
4242

4343
public:
44+
CHIP_ERROR Init();
4445
CHIP_ERROR StartAppTask();
4546
static void AppTaskMain(void * pvParameter);
4647

@@ -53,7 +54,6 @@ class AppTask
5354
private:
5455
friend AppTask & GetAppTask(void);
5556

56-
CHIP_ERROR Init();
5757
static void InitServer(intptr_t arg);
5858

5959
static void ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor);
@@ -68,6 +68,9 @@ class AppTask
6868
static void LockActionEventHandler(AppEvent * aEvent);
6969
static void TimerEventHandler(chip::System::Layer * aLayer, void * aAppState);
7070

71+
static void MatterEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
72+
static void UpdateLEDs(void);
73+
7174
void StartTimer(uint32_t aTimeoutMs);
7275

7376
enum Function_t

examples/lock-app/qpg/src/AppTask.cpp

+65-51
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ using namespace chip::DeviceLayer;
5454
#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000
5555
#define OTA_START_TRIGGER_TIMEOUT 1500
5656

57-
#define APP_TASK_STACK_SIZE (3 * 1024)
57+
#define APP_TASK_STACK_SIZE (2 * 1024)
5858
#define APP_TASK_PRIORITY 2
5959
#define APP_EVENT_QUEUE_SIZE 10
6060
#define QPG_LOCK_ENDPOINT_ID (1)
@@ -135,6 +135,8 @@ CHIP_ERROR AppTask::Init()
135135
{
136136
CHIP_ERROR err = CHIP_NO_ERROR;
137137

138+
PlatformMgr().AddEventHandler(MatterEventHandler, 0);
139+
138140
ChipLogProgress(NotSpecified, "Current Software Version: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
139141

140142
// Init ZCL Data Model and start server
@@ -165,70 +167,23 @@ CHIP_ERROR AppTask::Init()
165167
ConfigurationMgr().LogDeviceConfig();
166168
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
167169

170+
UpdateLEDs();
171+
168172
return err;
169173
}
170174

171175
void AppTask::AppTaskMain(void * pvParameter)
172176
{
173177
AppEvent event;
174178

175-
CHIP_ERROR err = sAppTask.Init();
176-
if (err != CHIP_NO_ERROR)
177-
{
178-
ChipLogError(NotSpecified, "AppTask.Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
179-
}
180-
181-
ChipLogProgress(NotSpecified, "App Task started");
182-
183179
while (true)
184180
{
185-
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
181+
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY);
186182
while (eventReceived == pdTRUE)
187183
{
188184
sAppTask.DispatchEvent(&event);
189185
eventReceived = xQueueReceive(sAppEventQueue, &event, 0);
190186
}
191-
192-
// Collect connectivity and configuration state from the CHIP stack. Because
193-
// the CHIP event loop is being run in a separate task, the stack must be
194-
// locked while these values are queried. However we use a non-blocking
195-
// lock request (TryLockCHIPStack()) to avoid blocking other UI activities
196-
// when the CHIP task is busy (e.g. with a long crypto operation).
197-
if (PlatformMgr().TryLockChipStack())
198-
{
199-
sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned();
200-
sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled();
201-
sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0);
202-
PlatformMgr().UnlockChipStack();
203-
}
204-
205-
// Update the status LED if factory reset has not been initiated.
206-
//
207-
// If system has "full connectivity", keep the LED On constantly.
208-
//
209-
// If thread and service provisioned, but not attached to the thread network
210-
// yet OR no connectivity to the service OR subscriptions are not fully
211-
// established THEN blink the LED Off for a short period of time.
212-
//
213-
// If the system has ble connection(s) uptill the stage above, THEN blink
214-
// the LEDs at an even rate of 100ms.
215-
//
216-
// Otherwise, blink the LED ON for a very short time.
217-
if (sAppTask.mFunction != kFunction_FactoryReset)
218-
{
219-
if (sIsThreadProvisioned && sIsThreadEnabled)
220-
{
221-
qvIO_LedBlink(SYSTEM_STATE_LED, 950, 50);
222-
}
223-
else if (sHaveBLEConnections)
224-
{
225-
qvIO_LedBlink(SYSTEM_STATE_LED, 100, 100);
226-
}
227-
else
228-
{
229-
qvIO_LedBlink(SYSTEM_STATE_LED, 50, 950);
230-
}
231-
}
232187
}
233188
}
234189

@@ -556,3 +511,62 @@ void AppTask::UpdateClusterState(void)
556511
ChipLogError(NotSpecified, "ERR: updating DoorLock %x", status);
557512
}
558513
}
514+
515+
void AppTask::UpdateLEDs(void)
516+
{
517+
// If system has "full connectivity", keep the LED On constantly.
518+
//
519+
// If thread and service provisioned, but not attached to the thread network
520+
// yet OR no connectivity to the service OR subscriptions are not fully
521+
// established THEN blink the LED Off for a short period of time.
522+
//
523+
// If the system has ble connection(s) uptill the stage above, THEN blink
524+
// the LEDs at an even rate of 100ms.
525+
//
526+
// Otherwise, blink the LED ON for a very short time.
527+
if (sIsThreadProvisioned && sIsThreadEnabled)
528+
{
529+
qvIO_LedBlink(SYSTEM_STATE_LED, 950, 50);
530+
}
531+
else if (sHaveBLEConnections)
532+
{
533+
qvIO_LedBlink(SYSTEM_STATE_LED, 100, 100);
534+
}
535+
else
536+
{
537+
qvIO_LedBlink(SYSTEM_STATE_LED, 50, 950);
538+
}
539+
}
540+
541+
void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t)
542+
{
543+
switch (event->Type)
544+
{
545+
case DeviceEventType::kServiceProvisioningChange: {
546+
sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned;
547+
UpdateLEDs();
548+
break;
549+
}
550+
551+
case DeviceEventType::kThreadConnectivityChange: {
552+
sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established);
553+
UpdateLEDs();
554+
break;
555+
}
556+
557+
case DeviceEventType::kCHIPoBLEConnectionEstablished: {
558+
sHaveBLEConnections = true;
559+
UpdateLEDs();
560+
break;
561+
}
562+
563+
case DeviceEventType::kCHIPoBLEConnectionClosed: {
564+
sHaveBLEConnections = false;
565+
UpdateLEDs();
566+
break;
567+
}
568+
569+
default:
570+
break;
571+
}
572+
}

examples/platform/qpg/app/main.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,19 @@ void Application_Init(void)
9797
ChipLogProgress(NotSpecified, "Qorvo " APP_NAME " Launching");
9898
ChipLogProgress(NotSpecified, "============================");
9999

100-
CHIP_ERROR ret = GetAppTask().StartAppTask();
101-
if (ret != CHIP_NO_ERROR)
100+
error = GetAppTask().Init();
101+
if (error != CHIP_NO_ERROR)
102102
{
103103
ChipLogError(NotSpecified, "GetAppTask().Init() failed");
104104
return;
105105
}
106+
107+
error = GetAppTask().StartAppTask();
108+
if (error != CHIP_NO_ERROR)
109+
{
110+
ChipLogError(NotSpecified, "GetAppTask().StartAppTask() failed");
111+
return;
112+
}
106113
}
107114

108115
void ChipEventHandler(const ChipDeviceEvent * aEvent, intptr_t /* arg */)

examples/shell/qpg/include/AppTask.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class AppTask
2929
{
3030

3131
public:
32+
CHIP_ERROR Init();
3233
CHIP_ERROR StartAppTask();
3334

3435
private:

examples/shell/qpg/src/AppTask.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ CHIP_ERROR AppTask::StartAppTask()
2626
{
2727
return CHIP_NO_ERROR;
2828
}
29+
30+
CHIP_ERROR AppTask::Init()
31+
{
32+
return CHIP_NO_ERROR;
33+
}

0 commit comments

Comments
 (0)