Skip to content

Commit

Permalink
[nfc] Move NFC code to the CHIP platform layer (#5665)
Browse files Browse the repository at this point in the history
* [nfc] Move NFC code to the CHIP platform layer

When NFC support was added to nRF Connect examples, NFC PR
had not yet been merged to the CHIP spec. Now that NFC is
officially one of supported methods of sharing the
commissioning information, it makes sense to move the NFC
code to the platform layer of the CHIP library.

* Disable NFC commissioning for shell example
  • Loading branch information
Damian-Nordic authored Apr 7, 2021
1 parent b382277 commit 5d00d1c
Show file tree
Hide file tree
Showing 20 changed files with 406 additions and 258 deletions.
1 change: 1 addition & 0 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER
chip_gn_arg_bool ("is_debug" CONFIG_DEBUG)
chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD)
chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4)
chip_gn_arg_bool ("chip_enable_nfc" CONFIG_CHIP_NFC_COMMISSIONING)
chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS)
chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS)
chip_gn_arg_bool ("chip_inet_config_enable_raw_endpoint" CONFIG_CHIP_BUILD_TESTS)
Expand Down
6 changes: 0 additions & 6 deletions examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ target_sources(app PRIVATE
${CHIP_ROOT}/src/app/clusters/network-commissioning/network-commissioning.cpp
)

if (CONFIG_CHIP_NFC_COMMISSIONING)
target_sources(app PRIVATE
${NRFCONNECT_COMMON}/util/NFCWidget.cpp
)
endif(CONFIG_CHIP_NFC_COMMISSIONING)

if (CONFIG_CHIP_PW_RPC)

set(PIGWEED_ROOT "${CHIP_ROOT}/third_party/pigweed/repo")
Expand Down
68 changes: 7 additions & 61 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,20 @@
#include "Service.h"
#include "ThreadUtil.h"

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

#include "attribute-storage.h"
#include "gen/attribute-id.h"
#include "gen/attribute-type.h"
#include "gen/cluster-id.h"

#include <platform/CHIPDeviceLayer.h>

#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <support/ErrorStr.h>
#include <system/SystemClock.h>

#include <dk_buttons_and_leds.h>
#include <logging/log.h>
#include <zephyr.h>

#include <algorithm>

LOG_MODULE_DECLARE(app);

namespace {
Expand All @@ -68,10 +60,6 @@ LEDWidget sStatusLED;
LEDWidget sUnusedLED;
LEDWidget sUnusedLED_1;

#ifdef CONFIG_CHIP_NFC_COMMISSIONING
NFCWidget sNFC;
#endif

bool sIsThreadProvisioned = false;
bool sIsThreadEnabled = false;
bool sHaveBLEConnections = false;
Expand Down Expand Up @@ -116,14 +104,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(ThreadProvisioningHandler, 0);
#endif

return 0;
Expand Down Expand Up @@ -384,20 +365,13 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent)
}

#ifdef CONFIG_CHIP_NFC_COMMISSIONING
if (!sNFC.IsTagEmulationStarted())
if (NFCMgr().IsTagEmulationStarted())
{
if (!(GetAppTask().StartNFCTag() < 0))
{
LOG_INF("Started NFC Tag emulation");
}
else
{
LOG_ERR("Starting NFC Tag failed");
}
LOG_INF("NFC Tag emulation is already started");
}
else
{
LOG_INF("NFC Tag emulation is already started");
ShareQRCodeOverNFC(chip::RendezvousInformationFlags::kBLE);
}
#endif

Expand All @@ -418,19 +392,11 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent)
}

#ifdef CONFIG_CHIP_NFC_COMMISSIONING
void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t arg)
void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t)
{
ARG_UNUSED(arg);
if ((event->Type == DeviceEventType::kServiceProvisioningChange) && ConnectivityMgr().IsThreadProvisioned())
if (event->Type == DeviceEventType::kCHIPoBLEAdvertisingChange && event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped)
{
if (sNFC.IsTagEmulationStarted())
{
const int result = sNFC.StopTagEmulation();
if (result)
{
LOG_ERR("Stopping NFC Tag emulation failed");
}
}
NFCMgr().StopTagEmulation();
}
}
#endif
Expand All @@ -447,26 +413,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(LightingManager::Action_t aAction, int32_t aActor)
{
if (aAction == LightingManager::ON_ACTION)
Expand Down
4 changes: 0 additions & 4 deletions examples/lighting-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ class AppTask

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/lock-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ target_sources(app PRIVATE
${LOCK_COMMON}/gen/call-command-handler.cpp
${LOCK_COMMON}/gen/callback-stub.cpp
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
${NRFCONNECT_COMMON}/util/NFCWidget.cpp
${NRFCONNECT_COMMON}/util/ThreadUtil.cpp
${NRFCONNECT_COMMON}/app/Service.cpp
${CHIP_ROOT}/src/app/server/DataModelHandler.cpp
Expand Down
75 changes: 11 additions & 64 deletions examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
#include "Service.h"
#include "ThreadUtil.h"

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

#include "attribute-storage.h"
#include "gen/attribute-id.h"
#include "gen/attribute-type.h"
Expand All @@ -38,12 +34,8 @@

#include <dk_buttons_and_leds.h>
#include <logging/log.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <zephyr.h>

#include <algorithm>

#define FACTORY_RESET_TRIGGER_TIMEOUT 3000
#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT 3000
#define APP_EVENT_QUEUE_SIZE 10
Expand All @@ -60,10 +52,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 @@ -108,16 +96,8 @@ 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(ThreadProvisioningHandler, 0);
#endif

return 0;
}

Expand Down Expand Up @@ -363,7 +343,7 @@ void AppTask::StartThreadHandler(AppEvent * aEvent)
LOG_ERR("Failed to add test pairing");
}

if (!chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned())
if (!ConnectivityMgr().IsThreadProvisioned())
{
StartDefaultThreadNetwork();
LOG_INF("Device is not commissioned to a Thread network. Starting with the default configuration.");
Expand All @@ -379,27 +359,22 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent)
if (aEvent->ButtonEvent.PinNo != BLE_ADVERTISEMENT_START_BUTTON)
return;

if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned())
if (ConnectivityMgr().IsThreadProvisioned())
{
LOG_INF("NFC Tag emulation and BLE advertisement not started - device is commissioned to a Thread network.");
return;
}

if (!sNFC.IsTagEmulationStarted())
#ifdef CONFIG_CHIP_NFC_COMMISSIONING
if (NFCMgr().IsTagEmulationStarted())
{
if (!(GetAppTask().StartNFCTag() < 0))
{
LOG_INF("Started NFC Tag emulation");
}
else
{
LOG_ERR("Starting NFC Tag failed");
}
LOG_INF("NFC Tag emulation is already started");
}
else
{
LOG_INF("NFC Tag emulation is already started");
ShareQRCodeOverNFC(chip::RendezvousInformationFlags::kBLE);
}
#endif

if (ConnectivityMgr().IsBLEAdvertisingEnabled())
{
Expand All @@ -418,19 +393,11 @@ void AppTask::StartBLEAdvertisementHandler(AppEvent * aEvent)
}

#ifdef CONFIG_CHIP_NFC_COMMISSIONING
void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t arg)
void AppTask::ThreadProvisioningHandler(const ChipDeviceEvent * event, intptr_t)
{
ARG_UNUSED(arg);
if ((event->Type == DeviceEventType::kServiceProvisioningChange) && ConnectivityMgr().IsThreadProvisioned())
if (event->Type == DeviceEventType::kCHIPoBLEAdvertisingChange && event->CHIPoBLEAdvertisingChange.Result == kActivity_Stopped)
{
if (sNFC.IsTagEmulationStarted())
{
const int result = sNFC.StopTagEmulation();
if (result)
{
LOG_ERR("Stopping NFC Tag emulation failed");
}
}
NFCMgr().StopTagEmulation();
}
}
#endif
Expand All @@ -447,26 +414,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(BoltLockManager::Action_t aAction, int32_t aActor)
{
// If the action has been initiated by the lock, update the bolt lock trait
Expand Down
4 changes: 0 additions & 4 deletions examples/lock-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class AppTask

void StartTimer(uint32_t aTimeoutInMs);

#ifdef CONFIG_CHIP_NFC_COMMISSIONING
int StartNFCTag();
#endif

enum Function_t
{
kFunction_NoneSelected = 0,
Expand Down
Loading

0 comments on commit 5d00d1c

Please sign in to comment.