From 11539318c45c19c25e5107a5287bc71329b8cbda Mon Sep 17 00:00:00 2001 From: Kamil Kasperczyk <66371704+kkasperczyk-no@users.noreply.github.com> Date: Tue, 18 Apr 2023 13:04:46 +0200 Subject: [PATCH] [nrfconnect] Implemented OnSubscriptionRequested callback in examples (#26130) Added OnSubscriptionRequested callback example implementation to make the Matter accessory device negotiate subscription report interval instead of always accepting the requested value. --- .../nrfconnect/chip-module/Kconfig.features | 23 +++++++++++ .../nrfconnect/CMakeLists.txt | 4 ++ examples/light-switch-app/nrfconnect/Kconfig | 3 ++ .../nrfconnect/main/AppTask.cpp | 8 ++++ .../nrfconnect/main/include/AppTask.h | 4 ++ examples/lock-app/nrfconnect/CMakeLists.txt | 4 ++ examples/lock-app/nrfconnect/Kconfig | 3 ++ examples/lock-app/nrfconnect/main/AppTask.cpp | 8 ++++ .../nrfconnect/main/include/AppTask.h | 4 ++ examples/platform/nrfconnect/util/ICDUtil.cpp | 40 +++++++++++++++++++ .../nrfconnect/util/include/ICDUtil.h | 33 +++++++++++++++ examples/window-app/nrfconnect/CMakeLists.txt | 4 ++ .../window-app/nrfconnect/main/AppTask.cpp | 8 ++++ .../nrfconnect/main/include/AppTask.h | 4 ++ examples/window-app/nrfconnect/prj.conf | 3 ++ .../window-app/nrfconnect/prj_no_dfu.conf | 3 ++ .../window-app/nrfconnect/prj_release.conf | 3 ++ 17 files changed, 159 insertions(+) create mode 100644 examples/platform/nrfconnect/util/ICDUtil.cpp create mode 100644 examples/platform/nrfconnect/util/include/ICDUtil.h diff --git a/config/nrfconnect/chip-module/Kconfig.features b/config/nrfconnect/chip-module/Kconfig.features index 99d13e9d56aa59..0501e351681e9a 100644 --- a/config/nrfconnect/chip-module/Kconfig.features +++ b/config/nrfconnect/chip-module/Kconfig.features @@ -171,4 +171,27 @@ config CHIP_WIFI_CONNECTION_RECOVERY_JITTER a random jitter interval is added to it to avoid periodicity. The random jitter is selected within range [-JITTER; +JITTER]. +config CHIP_ICD_SUBSCRIPTION_HANDLING + bool "Enables platform specific handling of ICD subscriptions" + help + Enables platform specific implementation that handles ICD subscription requests + and selects subscription report interval value considering maximum interval preferred + by the publisher. + +if CHIP_ICD_SUBSCRIPTION_HANDLING + +config CHIP_MAX_PREFERRED_SUBSCRIPTION_REPORT_INTERVAL + int "Maximum preferred interval of sending subscription reports (s)" + default 60 + help + Provides maximum preferred interval to be used by a publisher for negotiation + of the final maximum subscription report interval, after receiving a subscription + request from the initiator. This value should be selected as a compromise between + keeping the power consumption low due to not sending reports too often, and allowing + the initiator device to detect the publisher absence reasonably fast due to not sending + the reports too rarely. The current algorithm is to select bigger value from the one + requested by the initiator and the one preferred by the publisher. + +endif # CHIP_ICD_SUBSCRIPTION_HANDLING + endif # CHIP diff --git a/examples/light-switch-app/nrfconnect/CMakeLists.txt b/examples/light-switch-app/nrfconnect/CMakeLists.txt index 9e0f317f948fab..7512e45ce45211 100644 --- a/examples/light-switch-app/nrfconnect/CMakeLists.txt +++ b/examples/light-switch-app/nrfconnect/CMakeLists.txt @@ -70,6 +70,10 @@ if(CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/DFUOverSMP.cpp) endif() +if(CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING) + target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/ICDUtil.cpp) +endif() + chip_configure_data_model(app INCLUDE_SERVER ZAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../light-switch-common/light-switch-app.zap diff --git a/examples/light-switch-app/nrfconnect/Kconfig b/examples/light-switch-app/nrfconnect/Kconfig index 52da3138003335..73f8d8cf318a65 100644 --- a/examples/light-switch-app/nrfconnect/Kconfig +++ b/examples/light-switch-app/nrfconnect/Kconfig @@ -47,6 +47,9 @@ config NRF_WIFI_LOW_POWER endif # CHIP_WIFI +config CHIP_ICD_SUBSCRIPTION_HANDLING + default y + rsource "../../../config/nrfconnect/chip-module/Kconfig.features" rsource "../../../config/nrfconnect/chip-module/Kconfig.defaults" source "Kconfig.zephyr" diff --git a/examples/light-switch-app/nrfconnect/main/AppTask.cpp b/examples/light-switch-app/nrfconnect/main/AppTask.cpp index 1adb749a1cc931..4419f8615be960 100644 --- a/examples/light-switch-app/nrfconnect/main/AppTask.cpp +++ b/examples/light-switch-app/nrfconnect/main/AppTask.cpp @@ -44,6 +44,10 @@ #include "OTAUtil.h" #endif +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING +#include +#endif + #include #include #include @@ -219,6 +223,10 @@ CHIP_ERROR AppTask::Init() ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(RendezvousInformationFlags(RendezvousInformationFlag::kBLE)); +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING + chip::app::InteractionModelEngine::GetInstance()->RegisterReadHandlerAppCallback(&GetICDUtil()); +#endif + // Add CHIP event handler and start CHIP thread. // Note that all the initialization code should happen prior to this point // to avoid data races between the main and the CHIP threads. diff --git a/examples/light-switch-app/nrfconnect/main/include/AppTask.h b/examples/light-switch-app/nrfconnect/main/include/AppTask.h index 5c7e2278976c00..4e0a264af03667 100644 --- a/examples/light-switch-app/nrfconnect/main/include/AppTask.h +++ b/examples/light-switch-app/nrfconnect/main/include/AppTask.h @@ -31,6 +31,10 @@ #include "DFUOverSMP.h" #endif +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING +#include "ICDUtil.h" +#endif + #include struct k_timer; diff --git a/examples/lock-app/nrfconnect/CMakeLists.txt b/examples/lock-app/nrfconnect/CMakeLists.txt index 7e7dc3ce49b4f2..4be8f9927b5c22 100644 --- a/examples/lock-app/nrfconnect/CMakeLists.txt +++ b/examples/lock-app/nrfconnect/CMakeLists.txt @@ -71,3 +71,7 @@ endif() if(CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/DFUOverSMP.cpp) endif() + +if(CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING) + target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/ICDUtil.cpp) +endif() diff --git a/examples/lock-app/nrfconnect/Kconfig b/examples/lock-app/nrfconnect/Kconfig index 622d825f80e3ef..384993c130f77d 100644 --- a/examples/lock-app/nrfconnect/Kconfig +++ b/examples/lock-app/nrfconnect/Kconfig @@ -59,6 +59,9 @@ config NRF_WIFI_LOW_POWER endif # CHIP_WIFI +config CHIP_ICD_SUBSCRIPTION_HANDLING + default y + rsource "../../../config/nrfconnect/chip-module/Kconfig.features" rsource "../../../config/nrfconnect/chip-module/Kconfig.defaults" source "Kconfig.zephyr" diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index c6b6a1fd5b6654..61b96e266e6038 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -45,6 +45,10 @@ #include "OTAUtil.h" #endif +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING +#include +#endif + #include #include #include @@ -212,6 +216,10 @@ CHIP_ERROR AppTask::Init() ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING + chip::app::InteractionModelEngine::GetInstance()->RegisterReadHandlerAppCallback(&GetICDUtil()); +#endif + // Add CHIP event handler and start CHIP thread. // Note that all the initialization code should happen prior to this point to avoid data races // between the main and the CHIP threads. diff --git a/examples/lock-app/nrfconnect/main/include/AppTask.h b/examples/lock-app/nrfconnect/main/include/AppTask.h index 841362ed6ba8ce..402752d6412738 100644 --- a/examples/lock-app/nrfconnect/main/include/AppTask.h +++ b/examples/lock-app/nrfconnect/main/include/AppTask.h @@ -33,6 +33,10 @@ #include "DFUOverSMP.h" #endif +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING +#include "ICDUtil.h" +#endif + struct k_timer; struct Identify; diff --git a/examples/platform/nrfconnect/util/ICDUtil.cpp b/examples/platform/nrfconnect/util/ICDUtil.cpp new file mode 100644 index 00000000000000..fd2130c6c22dcb --- /dev/null +++ b/examples/platform/nrfconnect/util/ICDUtil.cpp @@ -0,0 +1,40 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ICDUtil.h" + +ICDUtil ICDUtil::sICDUtil; + +CHIP_ERROR ICDUtil::OnSubscriptionRequested(chip::app::ReadHandler & aReadHandler, chip::Transport::SecureSession & aSecureSession) +{ + uint16_t agreedMaxInterval = CONFIG_CHIP_MAX_PREFERRED_SUBSCRIPTION_REPORT_INTERVAL; + uint16_t requestedMinInterval = 0; + uint16_t requestedMaxInterval = 0; + aReadHandler.GetReportingIntervals(requestedMinInterval, requestedMaxInterval); + + if (requestedMaxInterval > agreedMaxInterval) + { + agreedMaxInterval = requestedMaxInterval; + } + else if (agreedMaxInterval > kSubscriptionMaxIntervalPublisherLimit) + { + agreedMaxInterval = kSubscriptionMaxIntervalPublisherLimit; + } + + return aReadHandler.SetReportingIntervals(agreedMaxInterval); +} diff --git a/examples/platform/nrfconnect/util/include/ICDUtil.h b/examples/platform/nrfconnect/util/include/ICDUtil.h new file mode 100644 index 00000000000000..33db1e97a051ac --- /dev/null +++ b/examples/platform/nrfconnect/util/include/ICDUtil.h @@ -0,0 +1,33 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +class ICDUtil : public chip::app::ReadHandler::ApplicationCallback +{ + CHIP_ERROR OnSubscriptionRequested(chip::app::ReadHandler & aReadHandler, + chip::Transport::SecureSession & aSecureSession) override; + friend ICDUtil & GetICDUtil(); + static ICDUtil sICDUtil; +}; + +inline ICDUtil & GetICDUtil() +{ + return ICDUtil::sICDUtil; +} diff --git a/examples/window-app/nrfconnect/CMakeLists.txt b/examples/window-app/nrfconnect/CMakeLists.txt index 92b43dc05fc779..21bb4caeb6194b 100644 --- a/examples/window-app/nrfconnect/CMakeLists.txt +++ b/examples/window-app/nrfconnect/CMakeLists.txt @@ -73,3 +73,7 @@ endif() if(CONFIG_MCUMGR_SMP_BT) target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/DFUOverSMP.cpp) endif() + +if(CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING) + target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/ICDUtil.cpp) +endif() diff --git a/examples/window-app/nrfconnect/main/AppTask.cpp b/examples/window-app/nrfconnect/main/AppTask.cpp index deb0cffb5e1a44..b0b3886a0a802f 100644 --- a/examples/window-app/nrfconnect/main/AppTask.cpp +++ b/examples/window-app/nrfconnect/main/AppTask.cpp @@ -35,6 +35,10 @@ #include "OTAUtil.h" #endif +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING +#include +#endif + #include #include #include @@ -191,6 +195,10 @@ CHIP_ERROR AppTask::Init() ConfigurationMgr().LogDeviceConfig(); PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING + chip::app::InteractionModelEngine::GetInstance()->RegisterReadHandlerAppCallback(&GetICDUtil()); +#endif + // Add CHIP event handler and start CHIP thread. // Note that all the initialization code should happen prior to this point to avoid data races // between the main and the CHIP threads diff --git a/examples/window-app/nrfconnect/main/include/AppTask.h b/examples/window-app/nrfconnect/main/include/AppTask.h index 9fbed1b2efba3b..4d134a791fa8fd 100644 --- a/examples/window-app/nrfconnect/main/include/AppTask.h +++ b/examples/window-app/nrfconnect/main/include/AppTask.h @@ -30,6 +30,10 @@ #include "DFUOverSMP.h" #endif +#ifdef CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING +#include "ICDUtil.h" +#endif + struct k_timer; struct Identify; diff --git a/examples/window-app/nrfconnect/prj.conf b/examples/window-app/nrfconnect/prj.conf index 3dc711bce86098..0f941b5aa1e6e8 100644 --- a/examples/window-app/nrfconnect/prj.conf +++ b/examples/window-app/nrfconnect/prj.conf @@ -36,6 +36,9 @@ CONFIG_CHIP_THREAD_SSED=y CONFIG_CHIP_SED_IDLE_INTERVAL=500 CONFIG_CHIP_SED_ACTIVE_INTERVAL=500 +# Enable ICD subscription platform specific handling +CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING=y + # Bluetooth Low Energy configuration CONFIG_BT_DEVICE_NAME="MatterWinCov" diff --git a/examples/window-app/nrfconnect/prj_no_dfu.conf b/examples/window-app/nrfconnect/prj_no_dfu.conf index d2da9b1a184eaa..b6caf7b75454e1 100644 --- a/examples/window-app/nrfconnect/prj_no_dfu.conf +++ b/examples/window-app/nrfconnect/prj_no_dfu.conf @@ -41,6 +41,9 @@ CONFIG_CHIP_THREAD_SSED=y CONFIG_CHIP_SED_IDLE_INTERVAL=500 CONFIG_CHIP_SED_ACTIVE_INTERVAL=500 +# Enable ICD subscription platform specific handling +CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING=y + # Bluetooth Low Energy configuration CONFIG_BT_DEVICE_NAME="MatterWinCov" diff --git a/examples/window-app/nrfconnect/prj_release.conf b/examples/window-app/nrfconnect/prj_release.conf index 336eb9ea95169f..bcf59cd9e35879 100644 --- a/examples/window-app/nrfconnect/prj_release.conf +++ b/examples/window-app/nrfconnect/prj_release.conf @@ -41,6 +41,9 @@ CONFIG_CHIP_THREAD_SSED=y CONFIG_CHIP_SED_IDLE_INTERVAL=500 CONFIG_CHIP_SED_ACTIVE_INTERVAL=500 +# Enable ICD subscription platform specific handling +CONFIG_CHIP_ICD_SUBSCRIPTION_HANDLING=y + # Bluetooth Low Energy configuration CONFIG_BT_DEVICE_NAME="MatterWinCov"