-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <ur6lal@gmail.com> Co-authored-by: Restyled.io <commits@restyled.io>
- Loading branch information
1 parent
51fa463
commit b0a7f9c
Showing
7 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |