2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <Commons/Exception.h>
19 #include "NdefMessage.h"
26 using namespace WrtDeviceApis::Commons;
28 NdefMessage::NdefMessage()
33 int result = nfc_ndef_message_create(&handle);
35 if (result != NFC_ERROR_NONE) {
37 util.throwNFCException(result, "Can't create Ndef Message");
41 NdefMessage::NdefMessage(void *messageHandle)
45 if (messageHandle == NULL) {
47 ThrowMsg(UnknownException, "Message Handler is Null Pointer.");
50 handle = static_cast<nfc_ndef_message_h>(messageHandle);
53 NdefMessage::NdefMessage(std::vector<void *> &ndefRcords)
58 handle = static_cast<nfc_ndef_message_h>(makeMessage(ndefRcords));
61 NdefMessage::NdefMessage(const std::vector<unsigned char> &rawdata)
65 handle = static_cast<nfc_ndef_message_h>(makeMessage(rawdata));
68 NdefMessage::~NdefMessage()
70 LoggerD("entered"<<recordPtr);
72 nfc_ndef_message_destroy(handle);
75 void *NdefMessage::makeMessage(std::vector<void *> &ndefRcords) {
77 nfc_ndef_message_h message;
78 int result = nfc_ndef_message_create(&message);
80 if (result != NFC_ERROR_NONE) {
81 util.throwNFCException(result, "Can't create Ndef Message");
84 for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
85 nfc_ndef_record_h insertRecord;
87 if (!util.copyNDEFRecord(&ndefRcords[i], (void **)(&insertRecord))) {
88 LoggerD("copyNDEFRecord fail!");
89 ThrowMsg(UnknownException, "Can't copy Record");
92 result = nfc_ndef_message_append_record(message, insertRecord);
94 if (result != NFC_ERROR_NONE) {
95 LoggerE(i << " record can't be inserted. " << insertRecord << " : " << message);
96 nfc_ndef_message_destroy(message);
97 util.throwNFCException(result, "Can't insert record in Ndef Message");
100 return (void*)message;
103 void *NdefMessage::makeMessage(const std::vector<unsigned char> &rawdata) {
105 unsigned char *messageRawdata = util.toCharPtr(rawdata);
106 nfc_ndef_message_h message;
107 int result = nfc_ndef_message_create_from_rawdata(&message, messageRawdata, rawdata.size());
109 if (result != NFC_ERROR_NONE) {
112 free(messageRawdata);
113 util.throwNFCException(result, "Can't create Ndef Message");
116 free(messageRawdata);
118 return (void*)message;
121 void NdefMessage::setRecordesPtr(void *records) {
122 LoggerD("entered"<<records);
126 void* NdefMessage::getRecordesPtr() {
127 LoggerD("entered"<<recordPtr);
132 long NdefMessage::getRecordCount() {
134 int result = nfc_ndef_message_get_record_count(handle, &count);
137 if (result != NFC_ERROR_NONE)
138 util.throwNFCException(result, "Can't get Record Count");
140 LoggerD("record Count : " << count);
141 return static_cast<long>(count);
144 std::vector<unsigned char> NdefMessage::toByte() {
145 unsigned char *rawdata = NULL;
149 if (getRecordCount() == 0)
150 return util.toVector(NULL, 0);
152 int result = nfc_ndef_message_get_rawdata(handle, &rawdata, &size);
154 if (result != NFC_ERROR_NONE) {
157 util.throwNFCException(result, "Can't get serial bytes of NDEF message");
160 std::vector<unsigned char> byteData = util.toVector(rawdata, size);
167 NdefRecordData NdefMessage::getNDEFRecord(const long index) {
168 nfc_ndef_record_h recordHandle;
169 int result = nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle);
172 if (result != NFC_ERROR_NONE)
173 util.throwNFCException(result, "Can't get Ndef Record");
175 NdefRecordData recordData;
177 return util.getNDEFRecordData(recordHandle);