Update change log and spec for wrt-plugins-tizen_0.4.27
[framework/web/wrt-plugins-tizen.git] / src / NFC / NdefMessage.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <Commons/Exception.h>
19 #include "NdefMessage.h"
20 #include "NFCUtil.h"
21 #include <Logger.h>
22
23 namespace DeviceAPI {
24 namespace NFC {
25
26 using namespace WrtDeviceApis::Commons;
27
28 NdefMessage::NdefMessage()
29 {
30         LoggerD("entered");
31         recordPtr = NULL;
32         NFCUtil util;
33         int result = nfc_ndef_message_create(&handle);
34
35         if (result != NFC_ERROR_NONE) {
36                 handle = NULL;
37                 util.throwNFCException(result, "Can't create Ndef Message");
38         }
39 }
40
41 NdefMessage::NdefMessage(void *messageHandle)
42 {
43         LoggerD("entered");
44         recordPtr = NULL;
45         if (messageHandle == NULL) {
46                 handle = NULL;
47                 ThrowMsg(UnknownException, "Message Handler is Null Pointer.");
48         }
49
50         handle = static_cast<nfc_ndef_message_h>(messageHandle);
51 }
52
53 NdefMessage::NdefMessage(std::vector<void *> &ndefRcords)
54 {
55         LoggerD("entered");
56         recordPtr = NULL;
57         handle = NULL;
58         handle = static_cast<nfc_ndef_message_h>(makeMessage(ndefRcords));
59 }
60
61 NdefMessage::NdefMessage(const std::vector<unsigned char> &rawdata)
62 {
63         LoggerD("entered");
64         recordPtr = NULL;
65         handle = static_cast<nfc_ndef_message_h>(makeMessage(rawdata));
66 }
67
68 NdefMessage::~NdefMessage()
69 {
70         LoggerD("entered"<<recordPtr);
71         if (handle != NULL)
72                 nfc_ndef_message_destroy(handle);
73 }
74
75 void *NdefMessage::makeMessage(std::vector<void *> &ndefRcords) {
76         NFCUtil util;
77         nfc_ndef_message_h message;
78         int result = nfc_ndef_message_create(&message);
79
80         if (result != NFC_ERROR_NONE) {
81                 util.throwNFCException(result, "Can't create Ndef Message");
82         }
83
84         for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
85                 nfc_ndef_record_h insertRecord;
86
87                 if (!util.copyNDEFRecord(&ndefRcords[i], (void **)(&insertRecord))) {
88                         LoggerD("copyNDEFRecord fail!");
89                         ThrowMsg(UnknownException, "Can't copy Record");
90                 }
91
92                 result = nfc_ndef_message_append_record(message, insertRecord);
93
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");
98                 }
99         }
100         return (void*)message;
101 }
102
103 void *NdefMessage::makeMessage(const std::vector<unsigned char> &rawdata) {
104         NFCUtil util;
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());
108
109         if (result != NFC_ERROR_NONE) {
110                 handle = NULL;
111                 if (messageRawdata)
112                         free(messageRawdata);
113                 util.throwNFCException(result, "Can't create Ndef Message");
114         }
115         if (messageRawdata)
116                 free(messageRawdata);
117
118         return (void*)message;
119 }
120
121 void NdefMessage::setRecordesPtr(void *records) {
122         LoggerD("entered"<<records);
123         recordPtr = records;
124 }
125
126 void* NdefMessage::getRecordesPtr() {
127         LoggerD("entered"<<recordPtr);
128         
129         return recordPtr;
130 }
131
132 long NdefMessage::getRecordCount() {
133         int count;
134         int result = nfc_ndef_message_get_record_count(handle, &count);
135
136         NFCUtil util;
137         if (result != NFC_ERROR_NONE)
138                 util.throwNFCException(result, "Can't get Record Count");
139
140         LoggerD("record Count : " << count);
141         return static_cast<long>(count);
142 }
143
144 std::vector<unsigned char> NdefMessage::toByte() {
145         unsigned char *rawdata = NULL;
146         int size;
147         NFCUtil util;
148
149         if (getRecordCount() == 0)
150                 return util.toVector(NULL, 0);
151
152         int result = nfc_ndef_message_get_rawdata(handle, &rawdata, &size);
153
154         if (result != NFC_ERROR_NONE) {
155                 if (rawdata)
156                         free(rawdata);
157                 util.throwNFCException(result, "Can't get serial bytes of NDEF message");
158         }
159
160         std::vector<unsigned char> byteData = util.toVector(rawdata, size);
161         if (rawdata)
162                 free(rawdata);
163
164         return byteData;
165 }
166
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);
170
171         NFCUtil util;
172         if (result != NFC_ERROR_NONE)
173                 util.throwNFCException(result, "Can't get Ndef Record");
174
175         NdefRecordData recordData;
176
177         return util.getNDEFRecordData(recordHandle);
178 }
179
180
181 }
182 }