From 4b1fcb624b3b2c1b9ddd900b8ce29a250234a8f6 Mon Sep 17 00:00:00 2001 From: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com> Date: Thu, 3 Aug 2023 08:52:14 -0400 Subject: [PATCH] add event For messages acks, Request ICD active mode when waiting on a ack (#28380) --- src/app/icd/ICDEventManager.cpp | 13 +++++++++++++ src/app/icd/ICDEventManager.h | 1 + src/app/icd/ICDManager.h | 1 + src/include/platform/CHIPDeviceEvent.h | 19 +++++++++++++++++++ src/messaging/ReliableMessageContext.cpp | 19 +++++++++++++++++++ src/messaging/ReliableMessageContext.h | 5 ----- 6 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/app/icd/ICDEventManager.cpp b/src/app/icd/ICDEventManager.cpp index a8e4e11de5fb1b..cbe3c5521a65d9 100644 --- a/src/app/icd/ICDEventManager.cpp +++ b/src/app/icd/ICDEventManager.cpp @@ -23,6 +23,7 @@ namespace chip { namespace app { uint8_t ICDEventManager::expectedMsgCount = 0; +uint8_t ICDEventManager::awaitingAckCount = 0; static_assert(UINT8_MAX >= CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS, "ICDEventManager::expectedMsgCount cannot hold count for the max exchange count"); @@ -97,7 +98,19 @@ void ICDEventManager::ICDEventHandler(const ChipDeviceEvent * event, intptr_t ar { icdManager->UpdateOperationState(ICDManager::OperationalState::ActiveMode); } + break; + case DeviceEventType::kICDMsgAckSyncEvent: + // When a Reliable Message Context is awaiting an ack, we keep the ICD in its active mode + if (event->AckSync.awaitingAck) + { + awaitingAckCount++; + } + else if (awaitingAckCount > 0) + { + awaitingAckCount--; + } + icdManager->SetKeepActiveModeRequirements(ICDManager::KeepActiveFlags::kAwaitingMsgAck, (awaitingAckCount != 0)); break; case DeviceEventType::kAppWakeUpEvent: icdManager->UpdateOperationState(ICDManager::OperationalState::ActiveMode); diff --git a/src/app/icd/ICDEventManager.h b/src/app/icd/ICDEventManager.h index fffbbb8dc334fb..c86b356e408657 100644 --- a/src/app/icd/ICDEventManager.h +++ b/src/app/icd/ICDEventManager.h @@ -48,6 +48,7 @@ class ICDEventManager */ static void ICDEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); static uint8_t expectedMsgCount; + static uint8_t awaitingAckCount; ICDManager * mICDManager; }; diff --git a/src/app/icd/ICDManager.h b/src/app/icd/ICDManager.h index 10a1704a670cfa..e5406f14615837 100644 --- a/src/app/icd/ICDManager.h +++ b/src/app/icd/ICDManager.h @@ -49,6 +49,7 @@ class ICDManager kCommissioningWindowOpen = 0x01, kFailSafeArmed = 0x02, kExpectingMsgResponse = 0x03, + kAwaitingMsgAck = 0x04, }; ICDManager() {} diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index 47f32fc1346d39..47573ba348fd78 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -274,6 +274,16 @@ enum PublicEventTypes */ kChipMsgRxEventHandled, + /** + * This event is used to sync the ICD with any Reliable Message exchange + * expecting an ack. The ICD shall stay in active Mode + * until the Reliable Message exchange post this event again + * informing the ICD that it is no longer awaiting the ack. + * + * This event contains an AckSync structure. + */ + kICDMsgAckSyncEvent, + /** * An application event occured that should wake up the system/device */ @@ -580,6 +590,15 @@ struct ChipDeviceEvent final bool wasReceived; bool clearsExpectedResponse; } RxEventContext; + + struct + { + /* + * Set to true when a Reliable Message Context is awaiting for a ack to a message sent + * Set to false when the Reliable Message Context is no longer awaiting for a ack + */ + bool awaitingAck; + } AckSync; }; void Clear() { memset(this, 0, sizeof(*this)); } diff --git a/src/messaging/ReliableMessageContext.cpp b/src/messaging/ReliableMessageContext.cpp index 06decfc320c41f..fcba107a734c5f 100644 --- a/src/messaging/ReliableMessageContext.cpp +++ b/src/messaging/ReliableMessageContext.cpp @@ -33,9 +33,12 @@ #include #include #include +#include #include #include +using namespace chip::DeviceLayer; + namespace chip { namespace Messaging { @@ -51,6 +54,22 @@ ReliableMessageMgr * ReliableMessageContext::GetReliableMessageMgr() return static_cast(this)->GetExchangeMgr()->GetReliableMessageMgr(); } +void ReliableMessageContext::SetMessageNotAcked(bool messageNotAcked) +{ + mFlags.Set(Flags::kFlagMessageNotAcked, messageNotAcked); + +#if CONFIG_DEVICE_LAYER + DeviceLayer::ChipDeviceEvent event; + event.Type = DeviceLayer::DeviceEventType::kICDMsgAckSyncEvent; + event.AckSync.awaitingAck = messageNotAcked; + CHIP_ERROR status = DeviceLayer::PlatformMgr().PostEvent(&event); + if (status != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to post AckSync event %" CHIP_ERROR_FORMAT, status.Format()); + } +#endif +} + CHIP_ERROR ReliableMessageContext::FlushAcks() { CHIP_ERROR err = CHIP_NO_ERROR; diff --git a/src/messaging/ReliableMessageContext.h b/src/messaging/ReliableMessageContext.h index 57ec4561740376..2ee9fe50a2f763 100644 --- a/src/messaging/ReliableMessageContext.h +++ b/src/messaging/ReliableMessageContext.h @@ -241,11 +241,6 @@ inline void ReliableMessageContext::SetAckPending(bool inAckPending) mFlags.Set(Flags::kFlagAckPending, inAckPending); } -inline void ReliableMessageContext::SetMessageNotAcked(bool messageNotAcked) -{ - mFlags.Set(Flags::kFlagMessageNotAcked, messageNotAcked); -} - inline void ReliableMessageContext::SetRequestingActiveMode(bool activeMode) { mFlags.Set(Flags::kFlagActiveMode, activeMode);