Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / NFC / NFCUtil.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
19
20 #include <Commons/Exception.h>
21 #include <JSTizenException.h>
22 #include <dpl/log/log.h>
23 #include <nfc.h>
24
25 #include "NFCUtil.h"
26
27 namespace DeviceAPI {
28 namespace NFC {
29
30 std::vector<unsigned char> NFCUtil::toVector(const unsigned char *ch, const int size) {
31         std::vector<unsigned char> vec;
32         if (ch && (size > 0)) {
33                 int i;
34                 
35                 for (i = 0; i < size; i++)
36                         vec.push_back(ch[i]);
37
38                 LogDebug("result:" << byteToString(ch, size));
39         } else
40                 LogDebug("result: NULL");
41         return vec;
42 }
43
44 unsigned char *NFCUtil::toCharPtr(std::vector<unsigned char> vec) {
45         if (vec.size() > 0) {
46                 unsigned char * chr = (unsigned char *) malloc(vec.size() * sizeof(unsigned char));
47                 for (int i = 0; i < static_cast<int>(vec.size()); i++)
48                         chr[i] = vec.at(i);
49
50                 return chr;
51         }
52
53         return NULL;
54 }
55
56 char *NFCUtil::byteToString(std::vector<unsigned char> *buffer){
57         static char localbuffer[256];
58         memset(localbuffer, 0, 256);
59         if (buffer->size() > 0) {
60                 unsigned char *charBuf = toCharPtr(*buffer);
61                 if (charBuf) {
62                         if (buffer->size() > 255) {
63                                 memcpy(localbuffer, charBuf, 255);
64                                 LogDebug("size is " << buffer->size() << ". Too Big! It will copy some of data(255 bytes)");
65                         } else
66                                 memcpy(localbuffer, charBuf, buffer->size());
67
68                         free(charBuf);
69                 } else
70                         LogDebug("Size is 0");
71         } else
72                 LogDebug("Size is 0");
73         return localbuffer;     
74 }
75
76 char *NFCUtil::byteToString(const unsigned char* buffer, const int size){
77         if (size > 255)
78                 LogDebug("size is " << size << ". Too Big! It will copy some of data(255 bytes)");
79
80         static char localbuffer[256];
81         memset(localbuffer, 0, 256);
82         if (size > 0)
83                 memcpy(localbuffer, buffer, size > 255 ? 255 : size);
84         else
85                 LogDebug("Size is 0");
86         return localbuffer;     
87 }
88
89 nfcTagType NFCUtil::convertTonfcTagType(const unsigned short type) {
90         switch (static_cast<nfc_tag_type_e>(type)) {
91         case NFC_GENERIC_PICC:
92                 return NFC_TAGTYPE_GENERIC_PICC;
93         case NFC_ISO14443_A_PICC:
94                 return NFC_TAGTYPE_ISO14443_A_PICC;
95         case NFC_ISO14443_4A_PICC:
96                 return NFC_TAGTYPE_ISO14443_4A_PICC;
97         case NFC_ISO14443_3A_PICC:
98                 return NFC_TAGTYPE_ISO14443_3A_PICC;
99         case NFC_MIFARE_MINI_PICC:
100                 return NFC_TAGTYPE_MIFARE_MINI_PICC;
101         case NFC_MIFARE_1K_PICC:
102                 return NFC_TAGTYPE_MIFARE_1K_PICC;
103         case NFC_MIFARE_4K_PICC:
104                 return NFC_TAGTYPE_MIFARE_4K_PICC;
105         case NFC_MIFARE_ULTRA_PICC:
106                 return NFC_TAGTYPE_MIFARE_ULTRA_PICC;
107         case NFC_MIFARE_DESFIRE_PICC:
108                 return NFC_TAGTYPE_MIFARE_DESFIRE_PICC;
109         case NFC_ISO14443_B_PICC:
110                 return NFC_TAGTYPE_ISO14443_B_PICC;
111         case NFC_ISO14443_4B_PICC:
112                 return NFC_TAGTYPE_ISO14443_4B_PICC;
113         case NFC_ISO14443_BPRIME_PICC:
114                 return NFC_TAGTYPE_ISO14443_BPRIME_PICC;
115         case NFC_FELICA_PICC:
116                 return NFC_TAGTYPE_FELICA_PICC;
117         case NFC_JEWEL_PICC:
118                 return NFC_TAGTYPE_JEWEL_PICC;
119         case NFC_ISO15693_PICC:
120                 return NFC_TAGTYPE_ISO15693_PICC;
121         case NFC_UNKNOWN_TARGET:
122         default :
123                 return NFC_TAGTYPE_UNKNOWN_TARGET;
124         }
125 }
126
127 unsigned short NFCUtil::convertToTNF(nfcTNF tnf) {
128         switch (tnf) {
129                 case NFC_TNF_WELL_KNOWN:
130                         return static_cast<unsigned short>(NFC_RECORD_TNF_WELL_KNOWN);
131                 case NFC_TNF_MIME_MEDIA:
132                         return static_cast<unsigned short>(NFC_RECORD_TNF_MIME_MEDIA);
133                 case NFC_TNF_URI:
134                         return static_cast<unsigned short>(NFC_RECORD_TNF_URI);
135                 case NFC_TNF_EXTERNAL_RTD:
136                         return static_cast<unsigned short>(NFC_RECORD_TNF_EXTERNAL_RTD);
137                 case NFC_TNF_UNKNOWN:
138                         return static_cast<unsigned short>(NFC_RECORD_TNF_UNKNOWN);
139                 case NFC_TNF_UNCHANGED:
140                         return static_cast<unsigned short>(NFC_RECORD_TNF_UNCHAGNED);
141                 case NFC_TNF_EMPTY:
142                 default:
143                         return static_cast<unsigned short>(NFC_RECORD_TNF_EMPTY);
144         }
145 }
146
147 nfcTNF NFCUtil::convertTonfcTNF(unsigned short tnf) {
148         switch (static_cast<nfc_record_tnf_e>(tnf)) {
149                 case NFC_RECORD_TNF_WELL_KNOWN:
150                         return NFC_TNF_WELL_KNOWN;
151                 case NFC_RECORD_TNF_MIME_MEDIA:
152                         return NFC_TNF_MIME_MEDIA;
153                 case NFC_RECORD_TNF_URI:
154                         return NFC_TNF_URI;
155                 case NFC_RECORD_TNF_EXTERNAL_RTD:
156                         return NFC_TNF_EXTERNAL_RTD;
157                 case NFC_RECORD_TNF_UNKNOWN:
158                         return NFC_TNF_UNKNOWN;
159                 case NFC_RECORD_TNF_UNCHAGNED:
160                         return NFC_TNF_UNCHANGED;
161                 case NFC_RECORD_TNF_EMPTY:
162                 default:
163                         return NFC_TNF_EMPTY;
164         }
165 }
166
167
168 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
169         nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
170
171         NdefRecordData recordData;
172
173         nfc_record_tnf_e tnf;
174         unsigned char *typeName, *id, *payload;
175         int typeSize, idSize, payloadSize;
176
177         int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
178         throwNFCException(result, "Can't get record's tnf");
179
180         result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
181         throwNFCException(result, "Can't get record's type");
182
183         result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
184         throwNFCException(result, "Can't get record's id");
185
186         result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
187         throwNFCException(result, "Can't get record's payload");
188
189         LogDebug("tnf : " <<tnf);
190         LogDebug("typeName : " << byteToString(typeName, typeSize));
191         LogDebug("payload : " << byteToString(payload, payloadSize));
192
193         recordData.properties.tnf = convertTonfcTNF(static_cast<unsigned short>(tnf));
194         recordData.properties.typeName = toVector(typeName, typeSize);
195         recordData.properties.id = toVector(id, idSize);
196         recordData.payload = toVector(payload, payloadSize);
197
198         return recordData;
199 }
200
201 bool NFCUtil::copyNDEFRecord(void **src, void **dest) {
202         nfc_ndef_record_h *srcHandle, *destHandle;
203         srcHandle = (nfc_ndef_record_h *)src;
204         destHandle = (nfc_ndef_record_h *)dest;
205
206         NdefRecordData recordData = getNDEFRecordData(*srcHandle);
207
208         unsigned char *typeName = toCharPtr(recordData.properties.typeName);
209         unsigned char *id = toCharPtr(recordData.properties.id);
210         unsigned char *payload = toCharPtr(recordData.payload);
211
212         if (nfc_ndef_record_create(destHandle, static_cast<nfc_record_tnf_e>(convertToTNF(recordData.properties.tnf)), typeName, recordData.properties.typeName.size(),
213                 id, recordData.properties.id.size(), payload, recordData.payload.size()) != NFC_ERROR_NONE) {
214                 if (typeName)
215                         free(typeName);
216                 if (id)
217                         free(id);
218                 if (payload)
219                         free(payload);
220
221                 return false;
222         }
223
224         if (typeName)
225                 free(typeName);
226         if (id)
227                 free(id);
228         if (payload)
229                 free(payload);
230
231         return true;
232 }
233
234 bool NFCUtil::copyNDEFMessage(void **src, void **dest) {
235         nfc_ndef_message_h *srcHandle, *destHandle;
236         srcHandle = (nfc_ndef_message_h *)src;
237         destHandle = (nfc_ndef_message_h *)dest;
238
239         unsigned char *rawdata;
240         int size;
241         int result = nfc_ndef_message_get_rawdata(*srcHandle, &rawdata, &size);
242
243         if (result != NFC_ERROR_NONE)
244                 return false;
245
246         if (nfc_ndef_message_create_from_rawdata(destHandle, rawdata, size) != NFC_ERROR_NONE) {
247                 if (rawdata)
248                         free(rawdata);
249                 return false;
250         }
251
252         if (rawdata)
253                 free(rawdata);
254         return true;
255 }
256
257 std::string NFCUtil::getNFCErrorString(const int errorCode) {
258         LogDebug ("Errorcode : " << errorCode);
259         switch(errorCode) {
260                 case NFC_ERROR_NONE:
261                         return "";
262                 case NFC_ERROR_INVALID_PARAMETER:
263                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
264                 case NFC_ERROR_INVALID_RECORD_TYPE:
265                         return DeviceAPI::Common::JSTizenException::INVALID_VALUES_ERROR;
266                 case NFC_ERROR_NO_DEVICE:
267                 case NFC_ERROR_OUT_OF_MEMORY:
268                 case NFC_ERROR_OPERATION_FAILED:
269                 case NFC_ERROR_DEVICE_BUSY:
270                         return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
271                 case NFC_ERROR_NOT_ACTIVATED:
272                         return DeviceAPI::Common::JSTizenException::SERVICE_NOT_AVAILABLE;
273                 case NFC_ERROR_NOT_SUPPORTED:
274                         return DeviceAPI::Common::JSTizenException::NOT_SUPPORTED_ERROR;
275                 case NFC_ERROR_TIMED_OUT:
276                         return DeviceAPI::Common::JSTizenException::TIMEOUT_ERROR;
277         }
278         return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
279 }
280
281 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
282         LogDebug ("Errorcode : " << errorCode);
283         switch(errorCode) {
284                 case NFC_ERROR_NONE:
285                         return "";
286                 case NFC_ERROR_INVALID_PARAMETER:
287                         return "Invalid Parameter";
288                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
289                         return "Invalid NDEF Message";
290                 case NFC_ERROR_INVALID_RECORD_TYPE:
291                         return "Invalid Record Type";
292                 case NFC_ERROR_NO_DEVICE:
293                         return "No Device";
294                 case NFC_ERROR_OUT_OF_MEMORY:
295                         return "Out Of Memory";
296                 case NFC_ERROR_NOT_SUPPORTED:
297                         return "NFC Not Supported";
298                 case NFC_ERROR_OPERATION_FAILED:
299                         return "Operation Failed";
300                 case NFC_ERROR_DEVICE_BUSY:
301                         return "Device Busy";
302                 case NFC_ERROR_NOT_ACTIVATED:
303                         return "NFC Not Activated";
304                 case NFC_ERROR_TIMED_OUT:
305                         return "Time Out";
306                 case NFC_ERROR_READ_ONLY_NDEF:
307                         return "Read Only NDEF";
308                 case NFC_ERROR_NO_SPACE_ON_NDEF:
309                         return "No Space On NDEF";
310                 case NFC_ERROR_NO_NDEF_MESSAGE:
311                         return "No NDEF Message";
312                 case NFC_ERROR_NOT_NDEF_FORMAT:
313                         return "Not NDEF Format";
314         }
315         return "UnknownError";
316 }
317
318 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
319         LogDebug ("Errorcode : " << static_cast<unsigned int>(errorCode));
320         LogDebug ("Message : " << message);
321
322         switch(errorCode) {
323                 case NFC_ERROR_NONE:
324                         return;
325                 case NFC_ERROR_INVALID_PARAMETER:
326                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
327                 case NFC_ERROR_INVALID_RECORD_TYPE:
328                         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, message);
329                         break;
330                 case NFC_ERROR_NOT_ACTIVATED:
331                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
332                         break;
333                 case NFC_ERROR_NOT_SUPPORTED:
334                 case NFC_ERROR_OPERATION_FAILED:
335                 case NFC_ERROR_DEVICE_BUSY:
336                 case NFC_ERROR_NO_DEVICE:
337                 case NFC_ERROR_TIMED_OUT:
338                 case NFC_ERROR_OUT_OF_MEMORY:
339                 default:
340                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);
341                         break;
342         }
343 }
344
345 }
346 }