Update change log and spec for wrt-plugins-tizen_0.4.27
[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 int payloadSize = payload.size();
95         unsigned char * typeName = util.toCharPtr(ndefRecordProperties.typeName);
96         int typeSize = (static_cast<int>(ndefRecordProperties.typeName.size()) > 255) ? 255 : static_cast<int>(ndefRecordProperties.typeName.size());
97         unsigned char * id = util.toCharPtr(ndefRecordProperties.id);
98         int idSize = (static_cast<int>(ndefRecordProperties.id.size()) > 255) ? 255 : static_cast<int>(ndefRecordProperties.id.size());
99
100         int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(util.convertToTNF(ndefRecordProperties.tnf)), typeName, typeSize,
101                 id, idSize, recordPayload, payloadSize) ;
102         if (recordPayload)
103                 free(recordPayload);
104         if (typeName)
105                 free(typeName);
106         if (id)
107                 free(id);
108
109         if (result != NFC_ERROR_NONE) {
110                 handle = NULL;
111                 util.throwNFCException(result, "Can't create Ndef Record");
112         }
113 }
114
115 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
116 {
117         LoggerD("entered");
118
119         int result = nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType)));
120
121         NFCUtil util;
122         if (result != NFC_ERROR_NONE) {
123                 handle = NULL;
124                 util.throwNFCException(result, "Can't create Ndef Text Record");
125         }
126 }
127
128 NdefRecord::NdefRecord(const std::string &uri)
129 {
130         LoggerD("entered");
131
132         int result = nfc_ndef_record_create_uri(&handle, uri.c_str());
133
134         NFCUtil util;
135         if (result != NFC_ERROR_NONE) {
136                 handle = NULL;
137                 util.throwNFCException(result, "Can't create Ndef Uri Record");
138         }
139 }
140
141 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
142 {
143         LoggerD("entered");
144
145         NFCUtil util;
146
147         unsigned char *mimeData = util.toCharPtr(data);
148         int result = nfc_ndef_record_create_mime(&handle, mimeType.c_str(), mimeData, static_cast<int>(data.size()));
149
150         if (mimeData)
151                 free(mimeData);
152
153         if (result != NFC_ERROR_NONE) {
154                 handle = NULL;
155                 util.throwNFCException(result, "Can't create Ndef Mime Record");
156         }
157 }
158
159 NdefRecord::~NdefRecord()
160 {
161         LoggerD("entered");
162         if (handle != NULL)
163                 nfc_ndef_record_destroy(handle);
164 }
165
166 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
167         switch(type) {
168                 case NFC_TEXT_ENCODE_UTF_8:
169                         return NFC_ENCODE_UTF_8;
170                 case NFC_TEXT_ENCODE_UTF_16:
171                         return NFC_ENCODE_UTF_16;
172         }
173 }
174
175 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
176         switch(type) {
177                 case NFC_ENCODE_UTF_8:
178                         return NFC_TEXT_ENCODE_UTF_8;
179                 case NFC_ENCODE_UTF_16:
180                         return NFC_TEXT_ENCODE_UTF_16;
181         }
182 }
183
184 void *NdefRecord::getHandle() {
185         return (void *)handle;
186 }
187
188 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
189         LoggerD("entered");
190         NdefRecordProperties props;
191         NFCUtil util;
192
193         props.tnf = getTNF();
194         props.typeName = getTypeName();
195         props.id = getID();
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         return true;
247 }
248
249 bool NdefRecord::getLangCode(char **langCode) {
250         LoggerD("entered");
251
252         int result = nfc_ndef_record_get_langcode(handle, langCode);
253         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
254                 return false;
255
256         NFCUtil util;
257         if (result != NFC_ERROR_NONE)
258                 util.throwNFCException(result, "Can't get langcode of record");
259
260         return true;
261 }
262 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
263         LoggerD("entered");
264
265         nfc_encode_type_e type;
266         int result = nfc_ndef_record_get_encode_type(handle, &type);
267         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
268                 return false;
269
270         NFCUtil util;
271         if (result != NFC_ERROR_NONE)
272                 util.throwNFCException(result, "Can't get encode type of record");
273
274         *encodeType = _convertToNfcEncodeType(type);
275         return true;
276 }
277
278 bool NdefRecord::getUri(char **uri) {
279         LoggerD("entered");
280         int result = nfc_ndef_record_get_uri(handle, uri);
281         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
282                 return false;
283
284         NFCUtil util;
285         if (result != NFC_ERROR_NONE)
286                 util.throwNFCException(result, "Can't get uri of record");
287
288         return true;
289 }
290
291 bool NdefRecord::getMimeType(char **mimeType) {
292         LoggerD("entered");
293         int result = nfc_ndef_record_get_mime_type(handle, mimeType);
294         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
295                 return false;
296
297         NFCUtil util;
298         if (result != NFC_ERROR_NONE)
299                 util.throwNFCException(result, "Can't get mime type of record");
300
301         return true;
302 }
303 std::vector<unsigned char> NdefRecord::getPayload() {
304         LoggerD("entered");
305         int size;
306         unsigned char *recordbuffer;
307         int result = nfc_ndef_record_get_payload(handle, &recordbuffer, &size);
308
309         NFCUtil util;
310         if (result != NFC_ERROR_NONE)
311                 util.throwNFCException(result, "Can't get record's payload");
312
313         return util.toVector(recordbuffer, size);
314 }
315
316 }
317 }