Skip to content

Commit

Permalink
[Telink] Add NFC Tag support (#31005)
Browse files Browse the repository at this point in the history
* add draft nfc tag implementation

* add nfc tag implementation

* fix nfc building issue

* Restyled by clang-format

---------

Co-authored-by: Dmytro Kashkarov <ur6lal@gmail.com>
Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent 51fa463 commit b0a7f9c
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/telink/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ matter_add_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_
matter_add_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD)
matter_add_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT)
matter_add_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4)
matter_add_gn_arg_bool ("chip_enable_nfc" CONFIG_CHIP_NFC_COMMISSIONING)
matter_add_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR)
matter_add_gn_arg_bool ("chip_enable_bootloader_mcuboot" CONFIG_BOOTLOADER_MCUBOOT)
matter_add_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS)
Expand Down
13 changes: 13 additions & 0 deletions config/telink/chip-module/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ config CHIP_APP_LOG_LEVEL
option only within the application. To set the logging level for the
Matter stack, use the MATTER_LOG_LEVEL configuration option.

config CHIP_NFC_COMMISSIONING
bool "Share onboarding payload in NFC tag"
default n
imply I2C
imply ST25DVXXKC
imply NFC
imply NFC_NDEF
imply NFC_NDEF_MSG
imply NFC_NDEF_RECORD
imply NFC_NDEF_URI_REC
imply NFC_NDEF_URI_MSG
help
Enables sharing the onboarding payload in the NFC tag.

# See config/zephyr/Kconfig for full definition
config CHIP_OTA_REQUESTOR
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/telink/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y
# Disable CHIP shell support
CONFIG_CHIP_LIB_SHELL=n

CONFIG_CHIP_NFC_COMMISSIONING=n

# Disable factory data support
CONFIG_CHIP_FACTORY_DATA=n
CONFIG_CHIP_FACTORY_DATA_BUILD=n
Expand Down
17 changes: 17 additions & 0 deletions examples/platform/telink/common/src/AppTaskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,23 @@ void AppTaskCommon::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /*
sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0;
#if CONFIG_CHIP_ENABLE_APPLICATION_STATUS_LED
UpdateStatusLED();
#endif
#ifdef CONFIG_CHIP_NFC_COMMISSIONING
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();
}
#endif
break;
case DeviceEventType::kThreadStateChange:
Expand Down
2 changes: 2 additions & 0 deletions src/platform/telink/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ static_library("telink") {
"ConnectivityManagerImpl.h",
"InetPlatformConfig.h",
"KeyValueStoreManagerImpl.h",
"NFCManagerImpl.cpp",
"NFCManagerImpl.h",
"PlatformManagerImpl.h",
"SystemPlatformConfig.h",
]
Expand Down
105 changes: 105 additions & 0 deletions src/platform/telink/NFCManagerImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2022-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 <platform/internal/CHIPDeviceLayerInternal.h>

#if CHIP_DEVICE_CONFIG_ENABLE_NFC
#include <platform/NFCManager.h>

#include <lib/support/logging/CHIPLogging.h>

#include <zephyr/kernel.h>
#include <zephyr/nfc/ndef/uri_msg.h>
#include <zephyr/nfc/nfc_tag.h>

void nfc_callback(const struct device * dev, enum nfc_tag_event event, const uint8_t * data, size_t data_len)
{
ARG_UNUSED(dev);
ARG_UNUSED(data);
ARG_UNUSED(data_len);
}

namespace chip {
namespace DeviceLayer {

NFCManagerImpl NFCManagerImpl::sInstance;

CHIP_ERROR NFCManagerImpl::_Init()
{
return CHIP_NO_ERROR;
}

CHIP_ERROR NFCManagerImpl::_StartTagEmulation(const char * payload, size_t payloadLength)
{
/* Set up NFC driver*/
uint8_t ndef_msg_buf[NDEF_MSG_BUF_SIZE];
uint32_t len = sizeof(ndef_msg_buf);

int err;

err = nfc_tag_init(dev, nfc_callback);
if (err != 0)
{
ChipLogError(DeviceLayer, "Cannot setup NFC subsys!");
return CHIP_ERROR_INTERNAL;
}

/* Set up Tag mode */
err = nfc_tag_set_type(dev, NFC_TAG_TYPE_T5T);
if (err != 0)
{
ChipLogError(DeviceLayer, "Cannot setup NFC Tag mode!");
return CHIP_ERROR_INTERNAL;
}

err = nfc_ndef_uri_msg_encode(NFC_URI_NONE, (const uint8_t *) payload, payloadLength, ndef_msg_buf, &len);
if (err != 0)
{
ChipLogError(DeviceLayer, "Cannot encode message!");
return CHIP_ERROR_INTERNAL;
}

err = nfc_tag_set_ndef(dev, ndef_msg_buf, len);
if (err != 0)
{
ChipLogError(DeviceLayer, "Cannot set payload!");
return CHIP_ERROR_INTERNAL;
}

err = nfc_tag_start(dev);
if (err != 0)
{
return CHIP_ERROR_INTERNAL;
}

sInstance.mIsStarted = true;
return CHIP_NO_ERROR;
}

CHIP_ERROR NFCManagerImpl::_StopTagEmulation()
{
if (nfc_tag_stop(dev))
{
return CHIP_ERROR_INTERNAL;
}
sInstance.mIsStarted = false;
return CHIP_NO_ERROR;
}

} // namespace DeviceLayer
} // namespace chip
#endif
63 changes: 63 additions & 0 deletions src/platform/telink/NFCManagerImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2022-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.
*/

#pragma once

namespace chip {
namespace DeviceLayer {

namespace {
#define NFC_DEV st25dvxxkc
#define DEV_PTR DEVICE_DT_GET(DT_NODELABEL(NFC_DEV))
#define NDEF_MSG_BUF_SIZE 128
} // namespace

class NFCManagerImpl final : public NFCManager
{
friend class NFCManager;

private:
// ===== Members that implement the NFCManager internal interface.

CHIP_ERROR _Init();
CHIP_ERROR _StartTagEmulation(const char * payload, size_t payloadLength);
CHIP_ERROR _StopTagEmulation();
bool _IsTagEmulationStarted() const { return mIsStarted; };

// ===== Members for internal use by this class.
bool mIsStarted;
const struct device * dev = DEV_PTR;
// ===== Members for internal use by the following friends.

friend NFCManager & NFCMgr();
friend NFCManagerImpl & NFCMgrImpl();

static NFCManagerImpl sInstance;
};

inline NFCManager & NFCMgr()
{
return NFCManagerImpl::sInstance;
}

inline NFCManagerImpl & NFCMgrImpl()
{
return NFCManagerImpl::sInstance;
}

} // namespace DeviceLayer
} // namespace chip

0 comments on commit b0a7f9c

Please sign in to comment.