wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / NFC / NdefRecord.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 "NdefRecord.h"
20 #include "NFCUtil.h"
21 #include <Logger.h>
22
23 namespace DeviceAPI {
24 namespace NFC {
25
26 using namespace WrtDeviceApis::Commons;
27
28 NdefRecord::NdefRecord()
29 {
30         handle = NULL;
31 }
32
33 NdefRecord::NdefRecord(std::vector<unsigned char> data)
34 {
35         LoggerD("entered");
36
37         nfc_ndef_message_h messageHandle;
38         NFCUtil util;
39         unsigned char *rawdata = util.toCharPtr(data);
40         int result = nfc_ndef_message_create_from_rawdata(&messageHandle, rawdata, data.size());
41
42         if (result != NFC_ERROR_NONE) {
43                 if (messageHandle)
44                         nfc_ndef_message_destroy(messageHandle);
45
46                 messageHandle = NULL;
47                 if (rawdata)
48                         free(rawdata);
49                 rawdata = NULL;
50                 util.throwNFCException(result, "Can't create Ndef Record");
51         }
52
53         int count;
54         result = nfc_ndef_message_get_record_count(messageHandle, &count);
55         if ((result != NFC_ERROR_NONE) || (count != 1)) {
56                 nfc_ndef_message_destroy(messageHandle);
57                 messageHandle = NULL;
58                 if (rawdata)
59                         free(rawdata);
60                 rawdata = NULL;
61                 util.throwNFCException(result, "Can't create Ndef Record");
62         }
63
64         nfc_ndef_record_h recordHandle;
65         result = nfc_ndef_message_get_record(messageHandle, 0, &recordHandle);
66         if (result != NFC_ERROR_NONE) {
67                 nfc_ndef_message_destroy(messageHandle);
68                 messageHandle = NULL;
69                 if (rawdata)
70                         free(rawdata);
71                 rawdata = NULL;
72                 util.throwNFCException(result, "Can't create Ndef Record");
73         }
74
75         if (!util.copyNDEFRecord((void **)&recordHandle, (void **)&handle)) {
76                 nfc_ndef_message_destroy(messageHandle);
77                 if (rawdata)
78                         free(rawdata);
79                 ThrowMsg(UnknownException, "Can't copy Ndef Record");
80         }
81
82         nfc_ndef_message_destroy(messageHandle);
83         if (rawdata)
84                 free(rawdata);
85 }
86
87 NdefRecord::NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload)
88 {
89         LoggerD("entered");
90
91         NFCUtil util;
92
93         unsigned char * recordPayload = util.toCharPtr(payload);
94         unsigned char * typeName = util.toCharPtr(ndefRecordProperties.typeName);
95         unsigned char * id = util.toCharPtr(ndefRecordProperties.id);
96
97         LoggerD("typeName : " << util.byteToString(typeName, ndefRecordProperties.typeName.size()));
98         int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(util.convertToTNF(ndefRecordProperties.tnf)), typeName, ndefRecordProperties.typeName.size(),
99                 id, ndefRecordProperties.id.size(), recordPayload, payload.size()) ;
100         if (recordPayload)
101                 free(recordPayload);
102         if (typeName)
103                 free(typeName);
104         if (id)
105                 free(id);
106
107         if (result != NFC_ERROR_NONE) {
108                 handle = NULL;
109                 util.throwNFCException(result, "Can't create Ndef Record");
110         }
111 }
112
113 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
114 {
115         LoggerD("entered");
116
117         int result = nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType)));
118
119         NFCUtil util;
120         if (result != NFC_ERROR_NONE) {
121                 handle = NULL;
122                 util.throwNFCException(result, "Can't create Ndef Text Record");
123         }
124 }
125
126 NdefRecord::NdefRecord(const std::string &uri)
127 {
128         LoggerD("entered");
129
130         int result = nfc_ndef_record_create_uri(&handle, uri.c_str());
131
132         NFCUtil util;
133         if (result != NFC_ERROR_NONE) {
134                 handle = NULL;
135                 util.throwNFCException(result, "Can't create Ndef Uri Record");
136         }
137 }
138
139 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
140 {
141         LoggerD("entered");
142
143         NFCUtil util;
144
145         unsigned char *mimeData = util.toCharPtr(data);
146         int result = nfc_ndef_record_create_mime(&handle, mimeType.c_str(), mimeData, static_cast<int>(data.size()));
147
148         if (mimeData)
149                 free(mimeData);
150
151         if (result != NFC_ERROR_NONE) {
152                 handle = NULL;
153                 util.throwNFCException(result, "Can't create Ndef Mime Record");
154         }
155 }
156
157 NdefRecord::~NdefRecord()
158 {
159         LoggerD("entered");
160         if (handle != NULL)
161                 nfc_ndef_record_destroy(handle);
162 }
163
164 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
165         switch(type) {
166                 case NFC_TEXT_ENCODE_UTF_8:
167                         return NFC_ENCODE_UTF_8;
168                 case NFC_TEXT_ENCODE_UTF_16:
169                         return NFC_ENCODE_UTF_16;
170         }
171 }
172
173 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
174         switch(type) {
175                 case NFC_ENCODE_UTF_8:
176                         return NFC_TEXT_ENCODE_UTF_8;
177                 case NFC_ENCODE_UTF_16:
178                         return NFC_TEXT_ENCODE_UTF_16;
179         }
180 }
181
182 void *NdefRecord::getHandle() {
183         return (void *)handle;
184 }
185
186 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
187         LoggerD("entered");
188         NdefRecordProperties props;
189         NFCUtil util;
190
191         props.tnf = getTNF();
192         LoggerD("tnf : " << props.tnf);
193         props.typeName = getTypeName();
194         props.id = getID();
195         LoggerD("typeName : " << util.byteToString(&props.typeName));
196
197         return props;
198 }
199
200 nfcTNF NdefRecord::getTNF() {
201         nfc_record_tnf_e tnf;
202         int result = nfc_ndef_record_get_tnf(handle, &tnf);
203
204         NFCUtil util;
205         if (result != NFC_ERROR_NONE)
206                 util.throwNFCException(result, "Can't get record's tnf");
207
208         return util.convertTonfcTNF(static_cast<unsigned short>(tnf));
209 }
210
211 std::vector<unsigned char> NdefRecord::getTypeName() {
212         unsigned char *typeName;
213         int typeSize;
214         int result = nfc_ndef_record_get_type(handle, &typeName, &typeSize);
215
216         NFCUtil util;
217         if (result != NFC_ERROR_NONE)
218                 util.throwNFCException(result, "Can't get record's type");
219
220         return util.toVector(typeName, typeSize);
221 }
222
223 std::vector<unsigned char> NdefRecord::getID() {
224         unsigned char *id;
225         int idSize;
226         int result = nfc_ndef_record_get_id(handle, &id, &idSize);
227
228         NFCUtil util;
229         if (result != NFC_ERROR_NONE)
230                 util.throwNFCException(result, "Can't get record's id");
231
232         return util.toVector(id, idSize);
233 }
234
235 bool NdefRecord::getText(char **text) {
236         LoggerD("entered");
237
238         int result = nfc_ndef_record_get_text(handle, text);
239         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
240                 return false;
241
242         NFCUtil util;
243         if (result != NFC_ERROR_NONE)
244                 util.throwNFCException(result, "Can't get text of record");
245
246         LoggerD("text : " << *text);
247         return true;
248 }
249
250 bool NdefRecord::getLangCode(char **langCode) {
251         LoggerD("entered");
252
253         int result = nfc_ndef_record_get_langcode(handle, langCode);
254         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
255                 return false;
256
257         NFCUtil util;
258         if (result != NFC_ERROR_NONE)
259                 util.throwNFCException(result, "Can't get langcode of record");
260
261         LoggerD("langCode : " << *langCode);
262         return true;
263 }
264 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
265         LoggerD("entered");
266
267         nfc_encode_type_e type;
268         int result = nfc_ndef_record_get_encode_type(handle, &type);
269         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
270                 return false;
271
272         NFCUtil util;
273         if (result != NFC_ERROR_NONE)
274                 util.throwNFCException(result, "Can't get encode type of record");
275
276         *encodeType = _convertToNfcEncodeType(type);
277         LoggerD("encodeType : " << *encodeType);
278         return true;
279 }
280
281 bool NdefRecord::getUri(char **uri) {
282         LoggerD("entered");
283         int result = nfc_ndef_record_get_uri(handle, uri);
284         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
285                 return false;
286
287         NFCUtil util;
288         if (result != NFC_ERROR_NONE)
289                 util.throwNFCException(result, "Can't get uri of record");
290
291         LoggerD("uri : " << *uri);
292         return true;
293 }
294
295 bool NdefRecord::getMimeType(char **mimeType) {
296         LoggerD("entered");
297         int result = nfc_ndef_record_get_mime_type(handle, mimeType);
298         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
299                 return false;
300
301         NFCUtil util;
302         if (result != NFC_ERROR_NONE)
303                 util.throwNFCException(result, "Can't get mime type of record");
304
305         LoggerD("mimeType : " << *mimeType);
306         return true;
307 }
308 std::vector<unsigned char> NdefRecord::getPayload() {
309         LoggerD("entered");
310         int size;
311         unsigned char *recordbuffer;
312         int result = nfc_ndef_record_get_payload(handle, &recordbuffer, &size);
313
314         NFCUtil util;
315         if (result != NFC_ERROR_NONE)
316                 util.throwNFCException(result, "Can't get record's payload");
317
318         LoggerD("payload : " << util.byteToString(recordbuffer, size));
319         return util.toVector(recordbuffer, size);
320 }
321
322 }
323 }