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