3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 #include "NFCWidget.h"
20 #include <nfc/ndef/uri_msg.h>
21 #include <nfc/ndef/uri_rec.h>
22 #include <nfc_t2t_lib.h>
25 int NFCWidget::Init(chip::DeviceLayer::ConnectivityManager & mgr)
27 mIsTagStarted = false;
28 return nfc_t2t_setup(FieldDetectionHandler, &mgr);
31 int NFCWidget::StartTagEmulation(const char * tagPayload, uint8_t tagPayloadLength)
33 uint32_t len = sizeof(mNdefBuffer);
37 nfc_ndef_uri_msg_encode(NFC_URI_NONE, reinterpret_cast<const uint8_t *>(tagPayload), tagPayloadLength, mNdefBuffer, &len);
38 VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_ndef_uri_msg_encode failed: %d", result));
40 result = nfc_t2t_payload_set(mNdefBuffer, len);
41 VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_t2t_payload_set failed: %d", result));
43 result = nfc_t2t_emulation_start();
44 VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_t2t_emulation_start failed: %d", result));
52 int NFCWidget::StopTagEmulation()
54 int result = nfc_t2t_emulation_stop();
56 VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_t2t_emulation_stop failed: %d", result));
58 memset(mNdefBuffer, 0, sizeof(mNdefBuffer));
60 mIsTagStarted = false;
66 bool NFCWidget::IsTagEmulationStarted() const
71 void NFCWidget::FieldDetectionHandler(void * context, enum nfc_t2t_event event, const uint8_t * data, size_t data_length)
76 ARG_UNUSED(data_length);