9c726b931de53ab28abdb69d2a7630195cab53f7
[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 EventNFCSEType NFCUtil::convertTonfcSEType(unsigned short type) {
168         switch (static_cast<nfc_se_type_e>(type)) {
169                 case NFC_SE_TYPE_DISABLE:
170                         return NFC_SE_NONE;
171                 case NFC_SE_TYPE_ESE:
172                         return NFC_SE_ESE;
173                 case NFC_SE_TYPE_UICC:
174                         return NFC_SE_UICC;
175                 default:
176                         LogDebug("Wrong SE Type : " << type);
177                         return NFC_SE_INVALID;
178         }
179 }
180
181 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
182         nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
183
184         NdefRecordData recordData;
185
186         nfc_record_tnf_e tnf;
187         unsigned char *typeName, *id, *payload;
188         int typeSize, idSize, payloadSize;
189
190         int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
191         throwNFCException(result, "Can't get record's tnf");
192
193         result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
194         throwNFCException(result, "Can't get record's type");
195
196         result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
197         throwNFCException(result, "Can't get record's id");
198
199         result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
200         throwNFCException(result, "Can't get record's payload");
201
202         LogDebug("tnf : " <<tnf);
203         LogDebug("typeName : " << byteToString(typeName, typeSize));
204         LogDebug("payload : " << byteToString(payload, payloadSize));
205
206         recordData.properties.tnf = convertTonfcTNF(static_cast<unsigned short>(tnf));
207         recordData.properties.typeName = toVector(typeName, typeSize);
208         recordData.properties.id = toVector(id, idSize);
209         recordData.payload = toVector(payload, payloadSize);
210
211         return recordData;
212 }
213
214 bool NFCUtil::copyNDEFRecord(void **src, void **dest) {
215         nfc_ndef_record_h *srcHandle, *destHandle;
216         srcHandle = (nfc_ndef_record_h *)src;
217         destHandle = (nfc_ndef_record_h *)dest;
218
219         NdefRecordData recordData = getNDEFRecordData(*srcHandle);
220
221         unsigned char *typeName = toCharPtr(recordData.properties.typeName);
222         unsigned char *id = toCharPtr(recordData.properties.id);
223         unsigned char *payload = toCharPtr(recordData.payload);
224
225         if (nfc_ndef_record_create(destHandle, static_cast<nfc_record_tnf_e>(convertToTNF(recordData.properties.tnf)), typeName, recordData.properties.typeName.size(),
226                 id, recordData.properties.id.size(), payload, recordData.payload.size()) != NFC_ERROR_NONE) {
227                 if (typeName)
228                         free(typeName);
229                 if (id)
230                         free(id);
231                 if (payload)
232                         free(payload);
233
234                 return false;
235         }
236
237         if (typeName)
238                 free(typeName);
239         if (id)
240                 free(id);
241         if (payload)
242                 free(payload);
243
244         return true;
245 }
246
247 bool NFCUtil::copyNDEFMessage(void **src, void **dest) {
248         nfc_ndef_message_h *srcHandle, *destHandle;
249         srcHandle = (nfc_ndef_message_h *)src;
250         destHandle = (nfc_ndef_message_h *)dest;
251
252         unsigned char *rawdata;
253         int size;
254         int result = nfc_ndef_message_get_rawdata(*srcHandle, &rawdata, &size);
255
256         if (result != NFC_ERROR_NONE)
257                 return false;
258
259         if (nfc_ndef_message_create_from_rawdata(destHandle, rawdata, size) != NFC_ERROR_NONE) {
260                 if (rawdata)
261                         free(rawdata);
262                 return false;
263         }
264
265         if (rawdata)
266                 free(rawdata);
267         return true;
268 }
269
270 std::string NFCUtil::getNFCErrorString(const int errorCode) {
271         LogDebug ("Errorcode : " << errorCode);
272         switch(errorCode) {
273                 case NFC_ERROR_NONE:
274                         return "";
275                 case NFC_ERROR_INVALID_PARAMETER:
276                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
277                 case NFC_ERROR_INVALID_RECORD_TYPE:
278                         return DeviceAPI::Common::JSTizenException::INVALID_VALUES_ERROR;
279                 case NFC_ERROR_NO_DEVICE:
280                 case NFC_ERROR_OUT_OF_MEMORY:
281                 case NFC_ERROR_OPERATION_FAILED:
282                 case NFC_ERROR_DEVICE_BUSY:
283                         return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
284                 case NFC_ERROR_NOT_ACTIVATED:
285                         return DeviceAPI::Common::JSTizenException::SERVICE_NOT_AVAILABLE;
286                 case NFC_ERROR_NOT_SUPPORTED:
287                         return DeviceAPI::Common::JSTizenException::NOT_SUPPORTED_ERROR;
288                 case NFC_ERROR_TIMED_OUT:
289                         return DeviceAPI::Common::JSTizenException::TIMEOUT_ERROR;
290         }
291         return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
292 }
293
294 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
295         LogDebug ("Errorcode : " << errorCode);
296         switch(errorCode) {
297                 case NFC_ERROR_NONE:
298                         return "";
299                 case NFC_ERROR_INVALID_PARAMETER:
300                         return "Invalid Parameter";
301                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
302                         return "Invalid NDEF Message";
303                 case NFC_ERROR_INVALID_RECORD_TYPE:
304                         return "Invalid Record Type";
305                 case NFC_ERROR_NO_DEVICE:
306                         return "No Device";
307                 case NFC_ERROR_OUT_OF_MEMORY:
308                         return "Out Of Memory";
309                 case NFC_ERROR_NOT_SUPPORTED:
310                         return "NFC Not Supported";
311                 case NFC_ERROR_OPERATION_FAILED:
312                         return "Operation Failed";
313                 case NFC_ERROR_DEVICE_BUSY:
314                         return "Device Busy";
315                 case NFC_ERROR_NOT_ACTIVATED:
316                         return "NFC Not Activated";
317                 case NFC_ERROR_TIMED_OUT:
318                         return "Time Out";
319                 case NFC_ERROR_READ_ONLY_NDEF:
320                         return "Read Only NDEF";
321                 case NFC_ERROR_NO_SPACE_ON_NDEF:
322                         return "No Space On NDEF";
323                 case NFC_ERROR_NO_NDEF_MESSAGE:
324                         return "No NDEF Message";
325                 case NFC_ERROR_NOT_NDEF_FORMAT:
326                         return "Not NDEF Format";
327         }
328         return "UnknownError";
329 }
330
331 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
332         LogDebug ("Errorcode : " << static_cast<unsigned int>(errorCode));
333         LogDebug ("Message : " << message);
334
335         switch(errorCode) {
336                 case NFC_ERROR_NONE:
337                         return;
338                 case NFC_ERROR_INVALID_PARAMETER:
339                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
340                 case NFC_ERROR_INVALID_RECORD_TYPE:
341                         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, message);
342                         break;
343                 case NFC_ERROR_NOT_ACTIVATED:
344                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
345                         break;
346                 case NFC_ERROR_NOT_SUPPORTED:
347                 case NFC_ERROR_OPERATION_FAILED:
348                 case NFC_ERROR_DEVICE_BUSY:
349                 case NFC_ERROR_NO_DEVICE:
350                 case NFC_ERROR_TIMED_OUT:
351                 case NFC_ERROR_OUT_OF_MEMORY:
352                 default:
353                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);
354                         break;
355         }
356 }
357
358 }
359 }