From b0a7f9ce852214527ecdd04819fd94cc87375f80 Mon Sep 17 00:00:00 2001 From: Alex Tsitsiura Date: Thu, 14 Dec 2023 16:36:40 +0200 Subject: [PATCH] [Telink] Add NFC Tag support (#31005) * add draft nfc tag implementation * add nfc tag implementation * fix nfc building issue * Restyled by clang-format --------- Co-authored-by: Dmytro Kashkarov Co-authored-by: Restyled.io --- config/telink/chip-module/CMakeLists.txt | 1 + config/telink/chip-module/Kconfig | 13 +++ examples/lighting-app/telink/prj.conf | 2 + .../telink/common/src/AppTaskCommon.cpp | 17 +++ src/platform/telink/BUILD.gn | 2 + src/platform/telink/NFCManagerImpl.cpp | 105 ++++++++++++++++++ src/platform/telink/NFCManagerImpl.h | 63 +++++++++++ 7 files changed, 203 insertions(+) create mode 100644 src/platform/telink/NFCManagerImpl.cpp create mode 100644 src/platform/telink/NFCManagerImpl.h diff --git a/config/telink/chip-module/CMakeLists.txt b/config/telink/chip-module/CMakeLists.txt index 8e798c058525c6..f9f4b933cccf5d 100644 --- a/config/telink/chip-module/CMakeLists.txt +++ b/config/telink/chip-module/CMakeLists.txt @@ -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) diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig index 93ebc31a0f426e..1148af804eee0b 100644 --- a/config/telink/chip-module/Kconfig +++ b/config/telink/chip-module/Kconfig @@ -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 diff --git a/examples/lighting-app/telink/prj.conf b/examples/lighting-app/telink/prj.conf index 566c993ee92dca..4bd1798b99e560 100644 --- a/examples/lighting-app/telink/prj.conf +++ b/examples/lighting-app/telink/prj.conf @@ -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 diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index cd3831caa40c1c..1ba800e5e1bcf0 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -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: diff --git a/src/platform/telink/BUILD.gn b/src/platform/telink/BUILD.gn index 840015f0764ae2..ec645d1773ce9f 100644 --- a/src/platform/telink/BUILD.gn +++ b/src/platform/telink/BUILD.gn @@ -45,6 +45,8 @@ static_library("telink") { "ConnectivityManagerImpl.h", "InetPlatformConfig.h", "KeyValueStoreManagerImpl.h", + "NFCManagerImpl.cpp", + "NFCManagerImpl.h", "PlatformManagerImpl.h", "SystemPlatformConfig.h", ] diff --git a/src/platform/telink/NFCManagerImpl.cpp b/src/platform/telink/NFCManagerImpl.cpp new file mode 100644 index 00000000000000..bb40b55305da73 --- /dev/null +++ b/src/platform/telink/NFCManagerImpl.cpp @@ -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 + +#if CHIP_DEVICE_CONFIG_ENABLE_NFC +#include + +#include + +#include +#include +#include + +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 diff --git a/src/platform/telink/NFCManagerImpl.h b/src/platform/telink/NFCManagerImpl.h new file mode 100644 index 00000000000000..27ba6e0f7c5c5d --- /dev/null +++ b/src/platform/telink/NFCManagerImpl.h @@ -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