diff --git a/examples/lighting-app/nrfconnect/README.md b/examples/lighting-app/nrfconnect/README.md index 441fff34a2014b..eeb9335b41f4e5 100644 --- a/examples/lighting-app/nrfconnect/README.md +++ b/examples/lighting-app/nrfconnect/README.md @@ -82,9 +82,9 @@ device and the CHIP controller, where the controller has the commissioner role. To start the rendezvous, the controller must get the commissioning information from the CHIP device. The data payload is encoded within a QR code, printed to -the UART console, and shared using an NFC tag. For security reasons, you must -start NFC tag emulation manually after powering up the device by pressing -**Button 4**. +the UART console, and shared using an NFC tag. NFC tag emulation starts +automatically when Bluetooth LE advertising is started and stays enabled until +Bluetooth LE advertising timeout expires. #### Thread provisioning diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index 6df78cded9671d..3f13d6e98970ec 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -132,7 +132,7 @@ int AppTask::Init() PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); #ifdef CONFIG_CHIP_NFC_COMMISSIONING - PlatformMgr().AddEventHandler(ThreadProvisioningHandler, 0); + PlatformMgr().AddEventHandler(ChipEventHandler, 0); #endif return 0; @@ -410,17 +410,6 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) return; } -#ifdef CONFIG_CHIP_NFC_COMMISSIONING - if (NFCMgr().IsTagEmulationStarted()) - { - LOG_INF("NFC Tag emulation is already started"); - } - else - { - ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); - } -#endif - if (ConnectivityMgr().IsBLEAdvertisingEnabled()) { LOG_INF("BLE Advertisement is already enabled"); @@ -438,9 +427,23 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) } #ifdef CONFIG_CHIP_NFC_COMMISSIONING -void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t) +void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { - if (event->Type == DeviceEventType::kCHIPoBLEAdvertisingChange && event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) + if (event->Type != DeviceEventType::kCHIPoBLEAdvertisingChange) + return; + + if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Started) + { + if (NFCMgr().IsTagEmulationStarted()) + { + LOG_INF("NFC Tag emulation is already started"); + } + else + { + ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + } + } + else if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) { NFCMgr().StopTagEmulation(); } diff --git a/examples/lighting-app/nrfconnect/main/include/AppTask.h b/examples/lighting-app/nrfconnect/main/include/AppTask.h index e98921183eb1d5..dc5f7bdfdba7d7 100644 --- a/examples/lighting-app/nrfconnect/main/include/AppTask.h +++ b/examples/lighting-app/nrfconnect/main/include/AppTask.h @@ -56,7 +56,7 @@ class AppTask static void LightingActionEventHandler(AppEvent * aEvent); static void StartBLEAdvertisementHandler(AppEvent * aEvent); - static void ThreadProvisioningHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); static void ButtonEventHandler(uint32_t button_state, uint32_t has_changed); static void TimerEventHandler(k_timer * timer); diff --git a/examples/lock-app/nrfconnect/README.md b/examples/lock-app/nrfconnect/README.md index a7506116361675..ef4a8dcf03255c 100644 --- a/examples/lock-app/nrfconnect/README.md +++ b/examples/lock-app/nrfconnect/README.md @@ -80,9 +80,9 @@ role. To start the rendezvous, the controller must get the commissioning information from the CHIP device. The data payload is encoded within a QR code, printed to -the UART console, and shared using an NFC tag. For security reasons, you must -start NFC tag emulation manually after powering up the device by pressing -**Button 4**. +the UART console, and shared using an NFC tag. NFC tag emulation starts +automatically when Bluetooth LE advertising is started and stays enabled until +Bluetooth LE advertising timeout expires. #### Thread provisioning diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index d8d156f1fe3d70..2e59031f6afc30 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -123,7 +123,7 @@ int AppTask::Init() PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); #ifdef CONFIG_CHIP_NFC_COMMISSIONING - PlatformMgr().AddEventHandler(ThreadProvisioningHandler, 0); + PlatformMgr().AddEventHandler(ChipEventHandler, 0); #endif return 0; } @@ -410,17 +410,6 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) return; } -#ifdef CONFIG_CHIP_NFC_COMMISSIONING - if (NFCMgr().IsTagEmulationStarted()) - { - LOG_INF("NFC Tag emulation is already started"); - } - else - { - ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); - } -#endif - if (ConnectivityMgr().IsBLEAdvertisingEnabled()) { LOG_INF("BLE Advertisement is already enabled"); @@ -438,9 +427,23 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) } #ifdef CONFIG_CHIP_NFC_COMMISSIONING -void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t) +void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { - if (event->Type == DeviceEventType::kCHIPoBLEAdvertisingChange && event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) + if (event->Type != DeviceEventType::kCHIPoBLEAdvertisingChange) + return; + + if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Started) + { + if (NFCMgr().IsTagEmulationStarted()) + { + LOG_INF("NFC Tag emulation is already started"); + } + else + { + ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + } + } + else if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) { NFCMgr().StopTagEmulation(); } diff --git a/examples/lock-app/nrfconnect/main/include/AppTask.h b/examples/lock-app/nrfconnect/main/include/AppTask.h index 83c25665bbf7fa..faf41ba25e024d 100644 --- a/examples/lock-app/nrfconnect/main/include/AppTask.h +++ b/examples/lock-app/nrfconnect/main/include/AppTask.h @@ -53,7 +53,7 @@ class AppTask static void LockActionEventHandler(AppEvent * aEvent); static void StartBLEAdvertisementHandler(AppEvent * aEvent); - static void ThreadProvisioningHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); static void ButtonEventHandler(uint32_t buttons_state, uint32_t has_changed); static void TimerEventHandler(k_timer * timer); diff --git a/examples/pump-app/nrfconnect/CMakeLists.txt b/examples/pump-app/nrfconnect/CMakeLists.txt index 3e66d0aafd48cc..684245066f4a57 100644 --- a/examples/pump-app/nrfconnect/CMakeLists.txt +++ b/examples/pump-app/nrfconnect/CMakeLists.txt @@ -46,7 +46,6 @@ target_sources(app PRIVATE ${PUMP_COMMON}/gen/callback-stub.cpp ${PUMP_COMMON}/gen/IMClusterCommandHandler.cpp ${NRFCONNECT_COMMON}/util/LEDWidget.cpp - ${NRFCONNECT_COMMON}/util/NFCWidget.cpp ${NRFCONNECT_COMMON}/util/ThreadUtil.cpp ${CHIP_ROOT}/src/app/server/DataModelHandler.cpp ${CHIP_ROOT}/src/app/reporting/reporting-default-configuration.cpp diff --git a/examples/pump-app/nrfconnect/README.md b/examples/pump-app/nrfconnect/README.md index 7baf3af6250c09..2a130566662f17 100644 --- a/examples/pump-app/nrfconnect/README.md +++ b/examples/pump-app/nrfconnect/README.md @@ -72,9 +72,9 @@ role. To start the rendezvous, the controller must get the commissioning information from the CHIP device. The data payload is encoded within a QR code, printed to -the UART console, and shared using an NFC tag. For security reasons, you must -start NFC tag emulation manually after powering up the device by pressing -**Button 4**. +the UART console, and shared using an NFC tag. NFC tag emulation starts +automatically when Bluetooth LE advertising is started and stays enabled until +Bluetooth LE advertising timeout expires. #### Thread provisioning diff --git a/examples/pump-app/nrfconnect/main/AppTask.cpp b/examples/pump-app/nrfconnect/main/AppTask.cpp index a733076764717f..73c56d2c2fcf6a 100644 --- a/examples/pump-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-app/nrfconnect/main/AppTask.cpp @@ -24,10 +24,6 @@ #include "Server.h" #include "ThreadUtil.h" -#ifdef CONFIG_CHIP_NFC_COMMISSIONING -#include "NFCWidget.h" -#endif - #include "attribute-storage.h" #include @@ -58,10 +54,6 @@ static LEDWidget sLockLED; static LEDWidget sUnusedLED; static LEDWidget sUnusedLED_1; -#ifdef CONFIG_CHIP_NFC_COMMISSIONING -static NFCWidget sNFC; -#endif - static bool sIsThreadProvisioned = false; static bool sIsThreadEnabled = false; static bool sHaveBLEConnections = false; @@ -106,14 +98,7 @@ int AppTask::Init() PrintOnboardingCodes(chip::RendezvousInformationFlags::kBLE); #ifdef CONFIG_CHIP_NFC_COMMISSIONING - ret = sNFC.Init(ConnectivityMgr()); - if (ret) - { - LOG_ERR("NFC initialization failed"); - return ret; - } - - PlatformMgr().AddEventHandler(AppTask::ThreadProvisioningHandler, 0); + PlatformMgr().AddEventHandler(AppTask::ChipEventHandler, 0); #endif return 0; @@ -373,22 +358,6 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) return; } - if (!sNFC.IsTagEmulationStarted()) - { - if (!(GetAppTask().StartNFCTag() < 0)) - { - LOG_INF("Started NFC Tag emulation"); - } - else - { - LOG_ERR("Starting NFC Tag failed"); - } - } - else - { - LOG_INF("NFC Tag emulation is already started"); - } - if (ConnectivityMgr().IsBLEAdvertisingEnabled()) { LOG_INF("BLE Advertisement is already enabled"); @@ -406,16 +375,25 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) } #ifdef CONFIG_CHIP_NFC_COMMISSIONING -void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t arg) +void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { - ARG_UNUSED(arg); - if ((event->Type == DeviceEventType::kServiceProvisioningChange) && ConnectivityMgr().IsThreadProvisioned()) + if (event->Type != DeviceEventType::kCHIPoBLEAdvertisingChange) + return; + + if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Started) { - const int result = sNFC.StopTagEmulation(); - if (result) + if (NFCMgr().IsTagEmulationStarted()) { - LOG_ERR("Stopping NFC Tag emulation failed"); + LOG_INF("NFC Tag emulation is already started"); } + else + { + ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + } + } + else if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) + { + NFCMgr().StopTagEmulation(); } } #endif @@ -432,26 +410,6 @@ void AppTask::StartTimer(uint32_t aTimeoutInMs) mFunctionTimerActive = true; } -#ifdef CONFIG_CHIP_NFC_COMMISSIONING -int AppTask::StartNFCTag() -{ - // Get QR Code and emulate its content using NFC tag - std::string QRCode; - - int result = GetQRCode(QRCode, chip::RendezvousInformationFlags::kBLE); - VerifyOrExit(!result, ChipLogError(AppServer, "Getting QR code payload failed")); - - // TODO: Issue #4504 - Remove replacing spaces with _ after problem described in #415 will be fixed. - std::replace(QRCode.begin(), QRCode.end(), ' ', '_'); - - result = sNFC.StartTagEmulation(QRCode.c_str(), QRCode.size()); - VerifyOrExit(result >= 0, ChipLogError(AppServer, "Starting NFC Tag emulation failed")); - -exit: - return result; -} -#endif - void AppTask::ActionInitiated(PumpManager::Action_t aAction, int32_t aActor) { // If the action has been initiated by the lock, update the bolt lock trait diff --git a/examples/pump-app/nrfconnect/main/include/AppTask.h b/examples/pump-app/nrfconnect/main/include/AppTask.h index 2773fee59b79c9..5142f173e5360e 100644 --- a/examples/pump-app/nrfconnect/main/include/AppTask.h +++ b/examples/pump-app/nrfconnect/main/include/AppTask.h @@ -53,17 +53,13 @@ class AppTask static void LockActionEventHandler(AppEvent * aEvent); static void StartBLEAdvertisementHandler(AppEvent * aEvent); - static void ThreadProvisioningHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); static void ButtonEventHandler(uint32_t buttons_state, uint32_t has_changed); static void TimerEventHandler(k_timer * timer); void StartTimer(uint32_t aTimeoutInMs); -#ifdef CONFIG_CHIP_NFC_COMMISSIONING - int StartNFCTag(); -#endif - enum Function_t { kFunction_NoneSelected = 0, diff --git a/examples/pump-controller-app/nrfconnect/CMakeLists.txt b/examples/pump-controller-app/nrfconnect/CMakeLists.txt index cba16d76db03f4..638dc1a32b9053 100644 --- a/examples/pump-controller-app/nrfconnect/CMakeLists.txt +++ b/examples/pump-controller-app/nrfconnect/CMakeLists.txt @@ -46,7 +46,6 @@ target_sources(app PRIVATE ${PUMPC_COMMON}/gen/callback-stub.cpp ${PUMPC_COMMON}/gen/IMClusterCommandHandler.cpp ${NRFCONNECT_COMMON}/util/LEDWidget.cpp - ${NRFCONNECT_COMMON}/util/NFCWidget.cpp ${NRFCONNECT_COMMON}/util/ThreadUtil.cpp ${CHIP_ROOT}/src/app/server/DataModelHandler.cpp ${CHIP_ROOT}/src/app/reporting/reporting-default-configuration.cpp diff --git a/examples/pump-controller-app/nrfconnect/README.md b/examples/pump-controller-app/nrfconnect/README.md index 7baf3af6250c09..2a130566662f17 100644 --- a/examples/pump-controller-app/nrfconnect/README.md +++ b/examples/pump-controller-app/nrfconnect/README.md @@ -72,9 +72,9 @@ role. To start the rendezvous, the controller must get the commissioning information from the CHIP device. The data payload is encoded within a QR code, printed to -the UART console, and shared using an NFC tag. For security reasons, you must -start NFC tag emulation manually after powering up the device by pressing -**Button 4**. +the UART console, and shared using an NFC tag. NFC tag emulation starts +automatically when Bluetooth LE advertising is started and stays enabled until +Bluetooth LE advertising timeout expires. #### Thread provisioning diff --git a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp index a733076764717f..73c56d2c2fcf6a 100644 --- a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp @@ -24,10 +24,6 @@ #include "Server.h" #include "ThreadUtil.h" -#ifdef CONFIG_CHIP_NFC_COMMISSIONING -#include "NFCWidget.h" -#endif - #include "attribute-storage.h" #include @@ -58,10 +54,6 @@ static LEDWidget sLockLED; static LEDWidget sUnusedLED; static LEDWidget sUnusedLED_1; -#ifdef CONFIG_CHIP_NFC_COMMISSIONING -static NFCWidget sNFC; -#endif - static bool sIsThreadProvisioned = false; static bool sIsThreadEnabled = false; static bool sHaveBLEConnections = false; @@ -106,14 +98,7 @@ int AppTask::Init() PrintOnboardingCodes(chip::RendezvousInformationFlags::kBLE); #ifdef CONFIG_CHIP_NFC_COMMISSIONING - ret = sNFC.Init(ConnectivityMgr()); - if (ret) - { - LOG_ERR("NFC initialization failed"); - return ret; - } - - PlatformMgr().AddEventHandler(AppTask::ThreadProvisioningHandler, 0); + PlatformMgr().AddEventHandler(AppTask::ChipEventHandler, 0); #endif return 0; @@ -373,22 +358,6 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) return; } - if (!sNFC.IsTagEmulationStarted()) - { - if (!(GetAppTask().StartNFCTag() < 0)) - { - LOG_INF("Started NFC Tag emulation"); - } - else - { - LOG_ERR("Starting NFC Tag failed"); - } - } - else - { - LOG_INF("NFC Tag emulation is already started"); - } - if (ConnectivityMgr().IsBLEAdvertisingEnabled()) { LOG_INF("BLE Advertisement is already enabled"); @@ -406,16 +375,25 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent) } #ifdef CONFIG_CHIP_NFC_COMMISSIONING -void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t arg) +void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) { - ARG_UNUSED(arg); - if ((event->Type == DeviceEventType::kServiceProvisioningChange) && ConnectivityMgr().IsThreadProvisioned()) + if (event->Type != DeviceEventType::kCHIPoBLEAdvertisingChange) + return; + + if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Started) { - const int result = sNFC.StopTagEmulation(); - if (result) + if (NFCMgr().IsTagEmulationStarted()) { - LOG_ERR("Stopping NFC Tag emulation failed"); + LOG_INF("NFC Tag emulation is already started"); } + else + { + ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + } + } + else if (event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped) + { + NFCMgr().StopTagEmulation(); } } #endif @@ -432,26 +410,6 @@ void AppTask::StartTimer(uint32_t aTimeoutInMs) mFunctionTimerActive = true; } -#ifdef CONFIG_CHIP_NFC_COMMISSIONING -int AppTask::StartNFCTag() -{ - // Get QR Code and emulate its content using NFC tag - std::string QRCode; - - int result = GetQRCode(QRCode, chip::RendezvousInformationFlags::kBLE); - VerifyOrExit(!result, ChipLogError(AppServer, "Getting QR code payload failed")); - - // TODO: Issue #4504 - Remove replacing spaces with _ after problem described in #415 will be fixed. - std::replace(QRCode.begin(), QRCode.end(), ' ', '_'); - - result = sNFC.StartTagEmulation(QRCode.c_str(), QRCode.size()); - VerifyOrExit(result >= 0, ChipLogError(AppServer, "Starting NFC Tag emulation failed")); - -exit: - return result; -} -#endif - void AppTask::ActionInitiated(PumpManager::Action_t aAction, int32_t aActor) { // If the action has been initiated by the lock, update the bolt lock trait diff --git a/examples/pump-controller-app/nrfconnect/main/include/AppTask.h b/examples/pump-controller-app/nrfconnect/main/include/AppTask.h index 2773fee59b79c9..5142f173e5360e 100644 --- a/examples/pump-controller-app/nrfconnect/main/include/AppTask.h +++ b/examples/pump-controller-app/nrfconnect/main/include/AppTask.h @@ -53,17 +53,13 @@ class AppTask static void LockActionEventHandler(AppEvent * aEvent); static void StartBLEAdvertisementHandler(AppEvent * aEvent); - static void ThreadProvisioningHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); static void ButtonEventHandler(uint32_t buttons_state, uint32_t has_changed); static void TimerEventHandler(k_timer * timer); void StartTimer(uint32_t aTimeoutInMs); -#ifdef CONFIG_CHIP_NFC_COMMISSIONING - int StartNFCTag(); -#endif - enum Function_t { kFunction_NoneSelected = 0,