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 _makeMessage(ndefRcords);
60 NdefMessage::NdefMessage(const std::vector<unsigned char> rawdata)
65 unsigned char *Urawdata = Util.toCharPtr(rawdata);
66 if (nfc_ndef_message_create_from_rawdata(&handle, Urawdata, rawdata.size()) != NFC_ERROR_NONE) {
69 ThrowMsg(PlatformException, "Can't create Ndef Message");
75 NdefMessage::~NdefMessage()
79 nfc_ndef_message_destroy(handle);
82 void NdefMessage::_makeMessage(const std::vector<void *> ndefRcords) {
83 if (nfc_ndef_message_create(&handle) != NFC_ERROR_NONE) {
85 ThrowMsg(PlatformException, "Can't create Ndef Message");
88 for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
89 nfc_ndef_record_h insertRecord;
91 if (!_copyNDEFRecord((nfc_ndef_record_h*)&ndefRcords[i], &insertRecord)) {
92 LogDebug("copyNDEFRecord fail!");
93 ThrowMsg(PlatformException, "Can't copy Record");
96 if (nfc_ndef_message_append_record(handle, insertRecord) != NFC_ERROR_NONE) {
97 LogError(i << " record can't be inserted. " << insertRecord << " : " << handle);
98 ThrowMsg(PlatformException, "Can't insert record in Ndef Message");
103 bool NdefMessage::_copyNDEFRecord(nfc_ndef_record_h *srcHandle, nfc_ndef_record_h *destHandle) {
104 nfc_record_tnf_e tnf;
105 unsigned char *typeName, *id, *payload;
106 int typeSize, idSize, payloadSize;
108 if (nfc_ndef_record_get_tnf(*srcHandle, &tnf) != NFC_ERROR_NONE)
111 if (nfc_ndef_record_get_type(*srcHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
114 if (nfc_ndef_record_get_id(*srcHandle, &id, &idSize) != NFC_ERROR_NONE)
117 if (nfc_ndef_record_get_payload(*srcHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
120 if (nfc_ndef_record_create(destHandle, tnf, typeName, typeSize, id, idSize, payload, payloadSize) != NFC_ERROR_NONE)
126 void *NdefMessage::getHandle() {
127 return (void *)handle;
130 void *NdefMessage::getRecordHandle(const long index) {
131 nfc_ndef_record_h recordHandle;
132 if (nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle) != NFC_ERROR_NONE)
133 ThrowMsg(PlatformException, "Can't get Ndef Record");
134 return (void *)recordHandle;
137 long NdefMessage::getRecordCount() {
139 if (nfc_ndef_message_get_record_count(handle, &count) != NFC_ERROR_NONE)
140 ThrowMsg(PlatformException, "Can't get Record Count");
142 LogDebug("record Count : " << count);
143 return static_cast<long>(count);
146 std::vector<unsigned char> NdefMessage::toByte() {
147 unsigned char *UrawData;
150 if (nfc_ndef_message_get_rawdata(handle, &UrawData, &size) != NFC_ERROR_NONE)
151 ThrowMsg(PlatformException, "Can't get serial bytes of NDEF message");
155 return Util.toVector(UrawData, size);
158 NdefRecordData NdefMessage::getNDEFRecord(const long index) {
159 nfc_ndef_record_h recordHandle;
160 if (nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle) != NFC_ERROR_NONE)
161 ThrowMsg(PlatformException, "Can't get Ndef Record");
163 NdefRecordData recordData;
165 nfc_record_tnf_e tnf;
166 unsigned char *typeName, *id, *payload;
167 int typeSize, idSize, payloadSize;
170 if (nfc_ndef_record_get_tnf(recordHandle, &tnf) != NFC_ERROR_NONE)
171 ThrowMsg(PlatformException, "Can't get record's tnf");
173 if (nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
174 ThrowMsg(PlatformException, "Can't get record's type");
176 if (nfc_ndef_record_get_id(recordHandle, &id, &idSize) != NFC_ERROR_NONE)
177 ThrowMsg(PlatformException, "Can't get record's id");
179 if (nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
180 ThrowMsg(PlatformException, "Can't get record's payload");
182 LogDebug("tnf : " <<tnf);
183 LogDebug("typeName : " << Util.byteToString(typeName, typeSize));
184 LogDebug("payload : " << Util.byteToString(payload, payloadSize));
186 recordData.properties.tnf = Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
187 recordData.properties.typeName = Util.toVector(typeName, typeSize);
188 recordData.properties.id = Util.toVector(id, idSize);
189 recordData.payload = Util.toVector(payload, payloadSize);
194 bool NdefMessage::insertNDEFRecord(const long index, const void *recordHandle) {
195 nfc_ndef_record_h insertRecord;
196 if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &insertRecord)) {
197 LogDebug("copyNDEFRecord fail!");
198 ThrowMsg(PlatformException, "Can't copy Record");
201 if (nfc_ndef_message_insert_record(handle, static_cast<int>(index), insertRecord) != NFC_ERROR_NONE)
202 ThrowMsg(PlatformException, "Can't insert ndef Record");
207 bool NdefMessage::appendNDEFRecord(const void *recordHandle) {
208 nfc_ndef_record_h appendRecord;
209 if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &appendRecord)) {
210 LogDebug("copyNDEFRecord fail!");
211 ThrowMsg(PlatformException, "Can't copy Record");
214 if (nfc_ndef_message_append_record(handle, appendRecord) != NFC_ERROR_NONE)
215 ThrowMsg(PlatformException, "Can't append ndef Record");
220 bool NdefMessage::removeNDEFRecord(const long index) {
221 if (nfc_ndef_message_remove_record(handle, static_cast<int>(index)) != NFC_ERROR_NONE)
222 ThrowMsg(PlatformException, "Can't remove ndef Record");
227 void NdefMessage::changeAllRecords(const std::vector<void *> ndefRcords) {
228 nfc_ndef_message_h newHandle = handle;
231 _makeMessage(ndefRcords);
232 if (newHandle != NULL)
233 nfc_ndef_message_destroy(newHandle);