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 NdefMessage::~NdefMessage()
38 void *NdefMessage::makeNdefMessageHandle(std::vector<void *> &ndefRcords) {
40 if (ndefRcords.size() == 0)
43 nfc_ndef_message_h message = NULL;
44 int result = nfc_ndef_message_create(&message);
46 if (result != NFC_ERROR_NONE) {
47 LoggerE(util.getNFCErrorMessage(result));
49 nfc_ndef_message_destroy(message);
50 util.throwNFCException(result, "Can't create Ndef Message");
51 } else if (!message) {
52 LoggerE("No Message Handle");
53 ThrowMsg(PlatformException, "Can't create Ndef Message");
56 for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
57 if ((message == NULL) && (ndefRcords[i] != NULL))
58 nfc_ndef_record_destroy((nfc_ndef_record_h)ndefRcords[i]);
59 result = nfc_ndef_message_append_record(message, (nfc_ndef_record_h)ndefRcords[i]);
61 if (result != NFC_ERROR_NONE) {
62 LoggerE(i << " record can't be inserted. " << ndefRcords[i] << " : " << message);
64 nfc_ndef_message_destroy(message);
69 return (void *)message;
72 std::vector<unsigned char> NdefMessage::toByte(std::vector<void *> &ndefRcords) {
74 if (ndefRcords.size() == 0)
75 return util.toVector(NULL, 0);
77 nfc_ndef_message_h message = (nfc_ndef_message_h)makeNdefMessageHandle(ndefRcords);
79 ThrowMsg(ConversionException, "Invalid NDEF Message");
80 unsigned char *rawdata = NULL;
81 unsigned int size = 0;
82 int result = nfc_ndef_message_get_rawdata(message, &rawdata, &size);
84 std::vector<unsigned char> byteData = util.toVector(rawdata, size);
87 nfc_ndef_message_destroy(message);
91 if (result != NFC_ERROR_NONE)
92 util.throwNFCException(result, "Can't get serial bytes of NDEF message");
97 std::vector<NdefRecordData> NdefMessage::toNDEFRecords(const std::vector<unsigned char> &rawdata) {
100 unsigned char *messageRawdata = util.toCharPtr(rawdata);
101 nfc_ndef_message_h message = NULL;
104 int result = nfc_ndef_message_create_from_rawdata(&message, messageRawdata, rawdata.size());
106 free(messageRawdata);
108 if (result != NFC_ERROR_NONE)
109 util.throwNFCException(result, "Can't create Ndef Message");
111 ThrowMsg(PlatformException, "Can't create Ndef Message");
112 } catch (const WrtDeviceApis::Commons::Exception& err) {
113 LoggerE(err.GetClassName() << ":"<<err.GetMessage());
116 return toNDEFRecords((void *)message);
119 std::vector<NdefRecordData> NdefMessage::toNDEFRecords(void *message) {
122 std::vector<NdefRecordData> records;
129 int result = nfc_ndef_message_get_record_count((nfc_ndef_message_h)message, &count);
130 if (result != NFC_ERROR_NONE)
131 util.throwNFCException(result, "Can't get a record count of message");
132 for (int i=0; i<count; i++) {
133 NdefRecordData record;
135 record = _getNDEFRecord((nfc_ndef_message_h)message, i);
136 } catch(const WrtDeviceApis::Commons::Exception& err) {
137 LoggerE(err.GetClassName() << ":"<<err.GetMessage());
139 records.push_back(record);
141 } catch (const WrtDeviceApis::Commons::Exception& err) {
142 LoggerE(err.GetClassName() << ":"<<err.GetMessage());
147 nfc_ndef_message_destroy((nfc_ndef_message_h)message);
154 NdefRecordData NdefMessage::_getNDEFRecord(nfc_ndef_message_h handle, const long index) {
155 nfc_ndef_record_h recordHandle;
156 int result = nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle);
159 if (result != NFC_ERROR_NONE)
160 util.throwNFCException(result, "Can't get Ndef Record");
162 NdefRecordData recordData;
164 return util.getNDEFRecordData(recordHandle);