Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / nrfconnect / util / NFCWidget.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 #include "NFCWidget.h"
19
20 #include <nfc/ndef/uri_msg.h>
21 #include <nfc/ndef/uri_rec.h>
22 #include <nfc_t2t_lib.h>
23 #include <zephyr.h>
24
25 int NFCWidget::Init(chip::DeviceLayer::ConnectivityManager & mgr)
26 {
27     mIsTagStarted = false;
28     return nfc_t2t_setup(FieldDetectionHandler, &mgr);
29 }
30
31 int NFCWidget::StartTagEmulation(const char * tagPayload, uint8_t tagPayloadLength)
32 {
33     uint32_t len = sizeof(mNdefBuffer);
34     int result   = 0;
35
36     result =
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));
39
40     result = nfc_t2t_payload_set(mNdefBuffer, len);
41     VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_t2t_payload_set failed: %d", result));
42
43     result = nfc_t2t_emulation_start();
44     VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_t2t_emulation_start failed: %d", result));
45
46     mIsTagStarted = true;
47
48 exit:
49     return result;
50 }
51
52 int NFCWidget::StopTagEmulation()
53 {
54     int result = nfc_t2t_emulation_stop();
55
56     VerifyOrExit(result >= 0, ChipLogProgress(AppServer, "nfc_t2t_emulation_stop failed: %d", result));
57
58     memset(mNdefBuffer, 0, sizeof(mNdefBuffer));
59
60     mIsTagStarted = false;
61
62 exit:
63     return result;
64 }
65
66 bool NFCWidget::IsTagEmulationStarted() const
67 {
68     return mIsTagStarted;
69 }
70
71 void NFCWidget::FieldDetectionHandler(void * context, enum nfc_t2t_event event, const uint8_t * data, size_t data_length)
72 {
73     ARG_UNUSED(context);
74     ARG_UNUSED(event);
75     ARG_UNUSED(data);
76     ARG_UNUSED(data_length);
77 }