Skip to content

Commit

Permalink
[nrfconnect] Clean up NFC code (#8639)
Browse files Browse the repository at this point in the history
Formerly, the spec required that a programmable NFC Tag be
enabled only after a user physical action, such as a button
push. Now that the requirement has been removed, make NFC
start automatically when the BLE advertising starts in
nRF Connect examples.
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Aug 14, 2021
1 parent f9af564 commit 1113359
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 170 deletions.
6 changes: 3 additions & 3 deletions examples/lighting-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 17 additions & 14 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions examples/lock-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 17 additions & 14 deletions examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion examples/pump-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions examples/pump-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
74 changes: 16 additions & 58 deletions examples/pump-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
#include "Server.h"
#include "ThreadUtil.h"

#ifdef CONFIG_CHIP_NFC_COMMISSIONING
#include "NFCWidget.h"
#endif

#include "attribute-storage.h"

#include <app/common/gen/attribute-id.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions examples/pump-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion examples/pump-controller-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions examples/pump-controller-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 1113359

Please sign in to comment.