From 6d5de867d19beb4bd6664cc33281b164181e4c81 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Wed, 2 Nov 2022 09:41:06 +0100 Subject: [PATCH 01/11] Switched to callbacks --- examples/lighting-app/qpg/src/AppTask.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 2f99b8e97edca3..ce1019bcf201a6 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -260,6 +260,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 @@ -317,8 +319,8 @@ void AppTask::AppTaskMain(void * pvParameter) // when the CHIP task is busy (e.g. with a long crypto operation). if (PlatformMgr().TryLockChipStack()) { - sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); - sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); + // sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); + // sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0); PlatformMgr().UnlockChipStack(); } @@ -621,3 +623,18 @@ void AppTask::UpdateClusterState(void) ChipLogError(NotSpecified, "ERR: updating level %x", status); } } + +void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) +{ + if (event->Type == DeviceEventType::kServiceProvisioningChange && event->ServiceProvisioningChange.IsServiceProvisioned) + { + if (event->ServiceProvisioningChange.IsServiceProvisioned) + { + sIsThreadProvisioned = true; + } + else + { + sIsThreadProvisioned = false; + } + } +} From 42a7f96cbd39086296a6fa7db670fbd84b8420b4 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Wed, 2 Nov 2022 11:20:13 +0100 Subject: [PATCH 02/11] LEDs are behaving the same --- examples/lighting-app/qpg/include/AppTask.h | 2 ++ examples/lighting-app/qpg/src/AppTask.cpp | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/lighting-app/qpg/include/AppTask.h b/examples/lighting-app/qpg/include/AppTask.h index f91eb971c2ca17..f02b53968862c1 100644 --- a/examples/lighting-app/qpg/include/AppTask.h +++ b/examples/lighting-app/qpg/include/AppTask.h @@ -64,6 +64,8 @@ class AppTask static void LightingActionEventHandler(AppEvent * aEvent); static void TimerEventHandler(chip::System::Layer * aLayer, void * aAppState); + static void MatterEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + void StartTimer(uint32_t aTimeoutMs); void CancelTimer(void); diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index ce1019bcf201a6..ee95f560bf2c7f 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -626,15 +626,16 @@ void AppTask::UpdateClusterState(void) void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { - if (event->Type == DeviceEventType::kServiceProvisioningChange && event->ServiceProvisioningChange.IsServiceProvisioned) + if (event->Type == DeviceEventType::kServiceProvisioningChange) { - if (event->ServiceProvisioningChange.IsServiceProvisioned) - { - sIsThreadProvisioned = true; - } - else - { - sIsThreadProvisioned = false; - } + sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; } + else if (event->Type == DeviceEventType::kThreadConnectivityChange) + { + sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); + } + else + { + // we don't need to support more events for now + } } From b1227da3e2c41d0388304f2ad586a473cd123013 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Wed, 2 Nov 2022 13:52:14 +0100 Subject: [PATCH 03/11] Cleanup --- examples/lighting-app/qpg/src/AppTask.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index ee95f560bf2c7f..52049318795e7e 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -319,8 +319,6 @@ void AppTask::AppTaskMain(void * pvParameter) // 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(); } From dea3b9ae3b912316036dd65a2f1d32bf85f1b04c Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Thu, 3 Nov 2022 12:39:24 +0100 Subject: [PATCH 04/11] BLE is now also handled by events --- examples/lighting-app/qpg/src/AppTask.cpp | 78 +++++++++++------------ src/platform/qpg/BLEManagerImpl.cpp | 4 ++ 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 52049318795e7e..d03fb5cfb34ffd 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -305,51 +305,12 @@ void AppTask::AppTaskMain(void * pvParameter) 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()) - { - 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); - } - } } } @@ -624,6 +585,8 @@ void AppTask::UpdateClusterState(void) void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { + ChipLogProgress(NotSpecified, "MatterEventHandler: %x", event->Type); + if (event->Type == DeviceEventType::kServiceProvisioningChange) { sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; @@ -632,8 +595,39 @@ void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); } - else + else if (event->Type == DeviceEventType::kCHIPoBLEConnectionEstablished) { - // we don't need to support more events for now + sHaveBLEConnections = true; } + else if (event->Type == DeviceEventType::kCHIPoBLEConnectionClosed) + { + sHaveBLEConnections = false; + } + else + { + // we don't need to support any more event types for now so we silently ignore them + } + + // 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); + } } diff --git a/src/platform/qpg/BLEManagerImpl.cpp b/src/platform/qpg/BLEManagerImpl.cpp index 0b4fe14ee0bf9e..c9ca68237dbda7 100644 --- a/src/platform/qpg/BLEManagerImpl.cpp +++ b/src/platform/qpg/BLEManagerImpl.cpp @@ -201,8 +201,12 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) break; case DeviceEventType::kCHIPoBLEUnsubscribe: { + ChipDeviceEvent connEstEvent; + ChipLogProgress(DeviceLayer, "_OnPlatformEvent kCHIPoBLEUnsubscribe"); HandleUnsubscribeReceived(event->CHIPoBLEUnsubscribe.ConId, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); + connEstEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed; + PlatformMgr().PostEventOrDie(&connEstEvent); } break; From 13c91110f9efd8921b474be18979e6ec02937b46 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Thu, 3 Nov 2022 12:42:09 +0100 Subject: [PATCH 05/11] Better variable name --- src/platform/qpg/BLEManagerImpl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/platform/qpg/BLEManagerImpl.cpp b/src/platform/qpg/BLEManagerImpl.cpp index c9ca68237dbda7..f466491ff724c8 100644 --- a/src/platform/qpg/BLEManagerImpl.cpp +++ b/src/platform/qpg/BLEManagerImpl.cpp @@ -201,12 +201,12 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) break; case DeviceEventType::kCHIPoBLEUnsubscribe: { - ChipDeviceEvent connEstEvent; - + ChipDeviceEvent connClosedEvent; + ChipLogProgress(DeviceLayer, "_OnPlatformEvent kCHIPoBLEUnsubscribe"); HandleUnsubscribeReceived(event->CHIPoBLEUnsubscribe.ConId, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); - connEstEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed; - PlatformMgr().PostEventOrDie(&connEstEvent); + connClosedEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed; + PlatformMgr().PostEventOrDie(&connClosedEvent); } break; From 2c749bd0fb7d659ec8d94d3a29e8f9bb7fd7f635 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Thu, 3 Nov 2022 12:43:13 +0100 Subject: [PATCH 06/11] Logs removed --- examples/lighting-app/qpg/src/AppTask.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index d03fb5cfb34ffd..1b86bb1c16821f 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -585,8 +585,6 @@ void AppTask::UpdateClusterState(void) void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { - ChipLogProgress(NotSpecified, "MatterEventHandler: %x", event->Type); - if (event->Type == DeviceEventType::kServiceProvisioningChange) { sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; From 9241fe4dd788811b905a9b3129220205f31cf724 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Thu, 3 Nov 2022 12:47:28 +0100 Subject: [PATCH 07/11] switch-case is more elegant here --- examples/lighting-app/qpg/src/AppTask.cpp | 45 ++++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 1b86bb1c16821f..83a4a8e2dcaf0b 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -585,25 +585,34 @@ void AppTask::UpdateClusterState(void) void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { - if (event->Type == DeviceEventType::kServiceProvisioningChange) + switch(event->Type) { - sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; - } - else if (event->Type == DeviceEventType::kThreadConnectivityChange) - { - sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); - } - else if (event->Type == DeviceEventType::kCHIPoBLEConnectionEstablished) - { - sHaveBLEConnections = true; - } - else if (event->Type == DeviceEventType::kCHIPoBLEConnectionClosed) - { - sHaveBLEConnections = false; - } - else - { - // we don't need to support any more event types for now so we silently ignore them + case DeviceEventType::kServiceProvisioningChange: + { + sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; + break; + } + + case DeviceEventType::kThreadConnectivityChange: + { + sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); + break; + } + + case DeviceEventType::kCHIPoBLEConnectionEstablished: + { + sHaveBLEConnections = true; + break; + } + + case DeviceEventType::kCHIPoBLEConnectionClosed: + { + sHaveBLEConnections = false; + break; + } + + default: + break; } // If system has "full connectivity", keep the LED On constantly. From c9e84f56d82d522bbd8f0a98e1c6f34432883bd0 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Thu, 3 Nov 2022 13:35:28 +0100 Subject: [PATCH 08/11] Moved LED handling to separate func --- examples/lighting-app/qpg/include/AppTask.h | 1 + examples/lighting-app/qpg/src/AppTask.cpp | 49 ++++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/examples/lighting-app/qpg/include/AppTask.h b/examples/lighting-app/qpg/include/AppTask.h index f02b53968862c1..7651124669546d 100644 --- a/examples/lighting-app/qpg/include/AppTask.h +++ b/examples/lighting-app/qpg/include/AppTask.h @@ -65,6 +65,7 @@ class AppTask 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); void CancelTimer(void); diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 83a4a8e2dcaf0b..018a5eefc9f91f 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -583,6 +583,32 @@ void AppTask::UpdateClusterState(void) } } +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) @@ -615,26 +641,5 @@ void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) break; } - // 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); - } + UpdateLEDs(); } From b5bda591dbf550a0f9ce059fd60b143e216db650 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Mon, 7 Nov 2022 13:34:01 +0100 Subject: [PATCH 09/11] AppTask stack reduced to 2kB --- examples/lighting-app/qpg/src/AppTask.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 018a5eefc9f91f..f3ebd96033c1bd 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -57,7 +57,7 @@ using namespace ::chip::DeviceLayer; #define FACTORY_RESET_TRIGGER_TIMEOUT 3000 #define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000 -#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_LIGHT_ENDPOINT_ID (1) From a8d249afc960f0cfc4333cc7868bf5af76b132be Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 7 Nov 2022 12:43:15 +0000 Subject: [PATCH 10/11] Restyled by clang-format --- examples/lighting-app/qpg/src/AppTask.cpp | 40 ++++++++++------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index f3ebd96033c1bd..65af1985c7c63e 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -611,33 +611,29 @@ void AppTask::UpdateLEDs(void) void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { - switch(event->Type) + switch (event->Type) { - case DeviceEventType::kServiceProvisioningChange: - { - sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; - break; - } + case DeviceEventType::kServiceProvisioningChange: { + sIsThreadProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned; + break; + } - case DeviceEventType::kThreadConnectivityChange: - { - sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); - break; - } + case DeviceEventType::kThreadConnectivityChange: { + sIsThreadEnabled = (event->ThreadConnectivityChange.Result == kConnectivity_Established); + break; + } - case DeviceEventType::kCHIPoBLEConnectionEstablished: - { - sHaveBLEConnections = true; - break; - } + case DeviceEventType::kCHIPoBLEConnectionEstablished: { + sHaveBLEConnections = true; + break; + } - case DeviceEventType::kCHIPoBLEConnectionClosed: - { - sHaveBLEConnections = false; - break; - } + case DeviceEventType::kCHIPoBLEConnectionClosed: { + sHaveBLEConnections = false; + break; + } - default: + default: break; } From 23cedb37751815a929d797d00064c64606097371 Mon Sep 17 00:00:00 2001 From: Adam Bodurka Date: Tue, 8 Nov 2022 08:44:31 +0100 Subject: [PATCH 11/11] Updating LEDs only on connectivity change --- examples/lighting-app/qpg/src/AppTask.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 65af1985c7c63e..27724b1f271329 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -615,27 +615,29 @@ void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t) { 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; } - - UpdateLEDs(); }