2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dpl/log/log.h>
20 #include <Commons/Exception.h>
22 #include "NdefMessage.h"
29 using namespace Api::NFC;
30 using namespace WrtDeviceApis::Commons;
32 NdefMessage::NdefMessage()
35 if (nfc_ndef_message_create(&handle) != NFC_ERROR_NONE) {
37 ThrowMsg(PlatformException, "Can't create Ndef Message");
41 NdefMessage::NdefMessage(void *messageHandle)
45 if (messageHandle == NULL) {
47 ThrowMsg(PlatformException, "Message Handler is Null Pointer.");
50 handle = static_cast<nfc_ndef_message_h>(messageHandle);
53 NdefMessage::NdefMessage(const std::vector<void *> ndefRcords)
57 if (nfc_ndef_message_create(&handle) != NFC_ERROR_NONE) {
59 ThrowMsg(PlatformException, "Can't create Ndef Message");
62 for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
63 nfc_ndef_record_h appendRecord;
64 if (!_copyNDEFRecord((nfc_ndef_record_h*)&ndefRcords[i], &appendRecord)) {
65 LogDebug("copyNDEFRecord fail!");
66 ThrowMsg(PlatformException, "Can't copy Record");
69 if (nfc_ndef_message_append_record(handle, appendRecord) != NFC_ERROR_NONE)
70 ThrowMsg(PlatformException, "Can't create Ndef Message");
74 NdefMessage::NdefMessage(const std::vector<unsigned char> rawdata)
79 unsigned char *Urawdata = Util.toCharPtr(rawdata);
80 if (nfc_ndef_message_create_from_rawdata(&handle, Urawdata, rawdata.size()) != NFC_ERROR_NONE) {
83 ThrowMsg(PlatformException, "Can't create Ndef Message");
89 NdefMessage::~NdefMessage()
93 nfc_ndef_message_destroy(handle);
96 bool NdefMessage::_copyNDEFRecord(nfc_ndef_record_h *srcHandle, nfc_ndef_record_h *destHandle) {
98 unsigned char *typeName, *id, *payload;
99 int typeSize, idSize, payloadSize;
101 if (nfc_ndef_record_get_tnf(*srcHandle, &tnf) != NFC_ERROR_NONE)
104 if (nfc_ndef_record_get_type(*srcHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
107 if (nfc_ndef_record_get_id(*srcHandle, &id, &idSize) != NFC_ERROR_NONE)
110 if (nfc_ndef_record_get_payload(*srcHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
113 if (nfc_ndef_record_create(destHandle, tnf, typeName, typeSize, id, idSize, payload, payloadSize) != NFC_ERROR_NONE)
119 void *NdefMessage::getHandle() {
120 return (void *)handle;
123 long NdefMessage::getRecordCount() {
125 if (nfc_ndef_message_get_record_count(handle, &count) != NFC_ERROR_NONE)
126 ThrowMsg(PlatformException, "Can't get Record Count");
128 LogDebug("record Count : " << count);
129 return static_cast<long>(count);
132 std::vector<unsigned char> NdefMessage::toByte() {
133 unsigned char *UrawData;
136 if (nfc_ndef_message_get_rawdata(handle, &UrawData, &size) != NFC_ERROR_NONE)
137 ThrowMsg(PlatformException, "Can't get serial bytes of NDEF message");
141 return Util.toVector(UrawData, size);
144 NdefRecordData NdefMessage::getNDEFRecord(const long index) {
145 nfc_ndef_record_h recordHandle;
146 if (nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle) != NFC_ERROR_NONE)
147 ThrowMsg(PlatformException, "Can't get Ndef Record");
149 NdefRecordData recordData;
151 nfc_record_tnf_e tnf;
152 unsigned char *typeName, *id, *payload;
153 int typeSize, idSize, payloadSize;
156 if (nfc_ndef_record_get_tnf(recordHandle, &tnf) != NFC_ERROR_NONE)
157 ThrowMsg(PlatformException, "Can't get record's tnf");
159 if (nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
160 ThrowMsg(PlatformException, "Can't get record's type");
162 if (nfc_ndef_record_get_id(recordHandle, &id, &idSize) != NFC_ERROR_NONE)
163 ThrowMsg(PlatformException, "Can't get record's id");
165 if (nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
166 ThrowMsg(PlatformException, "Can't get record's payload");
168 LogDebug("tnf : " <<tnf);
169 LogDebug("typeName : " << Util.byteToString(typeName, typeSize));
170 LogDebug("payload : " << Util.byteToString(payload, payloadSize));
172 recordData.properties.tnf = Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
173 recordData.properties.typeName = Util.toVector(typeName, typeSize);
174 recordData.properties.id = Util.toVector(id, idSize);
175 recordData.payload = Util.toVector(payload, payloadSize);
180 bool NdefMessage::insertNDEFRecord(const long index, const void *recordHandle) {
181 nfc_ndef_record_h insertRecord;
182 if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &insertRecord)) {
183 LogDebug("copyNDEFRecord fail!");
184 ThrowMsg(PlatformException, "Can't copy Record");
187 if (nfc_ndef_message_insert_record(handle, static_cast<int>(index), insertRecord) != NFC_ERROR_NONE)
188 ThrowMsg(PlatformException, "Can't insert ndef Record");
193 bool NdefMessage::appendNDEFRecord(const void *recordHandle) {
194 nfc_ndef_record_h appendRecord;
195 if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &appendRecord)) {
196 LogDebug("copyNDEFRecord fail!");
197 ThrowMsg(PlatformException, "Can't copy Record");
200 if (nfc_ndef_message_append_record(handle, appendRecord) != NFC_ERROR_NONE)
201 ThrowMsg(PlatformException, "Can't append ndef Record");
206 bool NdefMessage::removeNDEFRecord(const long index) {
207 if (nfc_ndef_message_remove_record(handle, static_cast<int>(index)) != NFC_ERROR_NONE)
208 ThrowMsg(PlatformException, "Can't remove ndef Record");