fixed compile errors
[platform/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 <nfc.h>
20 #include <JSWebAPIErrorFactory.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) && buffer)
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 void *NFCUtil::makeNDEFRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
125         LoggerD("entered");
126
127         if ((ndefRecordProperties.tnf < 0) || (ndefRecordProperties.tnf > 6))
128                 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Not Supported tnf");
129
130         nfc_ndef_record_h handle = NULL;
131
132         unsigned char *recordPayload = toCharPtr(payload);
133         unsigned int payloadSize = payload.size();
134         unsigned char *typeName = toCharPtr(ndefRecordProperties.typeName);
135         int typeSize = (static_cast<int>(ndefRecordProperties.typeName.size()) > 255) ? 255 : static_cast<int>(ndefRecordProperties.typeName.size());
136         unsigned char *id = toCharPtr(ndefRecordProperties.id);
137         int idSize = (static_cast<int>(ndefRecordProperties.id.size()) > 255) ? 255 : static_cast<int>(ndefRecordProperties.id.size());
138
139         int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(ndefRecordProperties.tnf), typeName, typeSize,
140                 id, idSize, recordPayload, payloadSize) ;
141
142         if (recordPayload)
143                 free(recordPayload);
144         if (typeName)
145                 free(typeName);
146         if (id)
147                 free(id);
148
149         if (result != NFC_ERROR_NONE) {
150                 if (handle)
151                         nfc_ndef_record_destroy(handle);
152                 throwNFCException(result, " Can't create Ndef Record");
153         } else if (!handle)
154                 ThrowMsg(PlatformException, " Can't create Ndef Record");
155         return (void *)handle;
156
157 }
158
159 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
160         if (!handle)
161                 ThrowMsg(PlatformException, "Invalid NDEFRecord");
162
163         nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
164
165         NdefRecordData recordData;
166
167         nfc_record_tnf_e tnf;
168         unsigned char *typeName, *id, *payload;
169         int typeSize, idSize;
170         unsigned int payloadSize;
171
172         int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
173         throwNFCException(result, "Can't get record's tnf");
174
175         result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
176         throwNFCException(result, "Can't get record's type");
177
178         result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
179         throwNFCException(result, "Can't get record's id");
180
181         result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
182         throwNFCException(result, "Can't get record's payload");
183
184         LoggerD("tnf : " <<tnf);
185         LoggerD("typeName : " << byteToString(typeName, typeSize));
186         LoggerD("payload : " << byteToString(payload, payloadSize));
187
188         recordData.properties.tnf = static_cast<short>(tnf);
189         recordData.properties.typeName = toVector(typeName, typeSize);
190         recordData.properties.id = toVector(id, idSize);
191         recordData.payload = toVector(payload, payloadSize);
192
193         return recordData;
194 }
195
196 std::string NFCUtil::getNFCErrorString(const int errorCode) {
197         LoggerD ("Errorcode : " << errorCode);
198         switch(errorCode) {
199                 case NFC_ERROR_NONE:
200                 case NFC_ERROR_ALREADY_ACTIVATED:
201                 case NFC_ERROR_ALREADY_DEACTIVATED:
202                         return "";
203                 case NFC_ERROR_INVALID_PARAMETER:
204                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
205                 case NFC_ERROR_INVALID_RECORD_TYPE:
206                         return DeviceAPI::Common::JSWebAPIErrorFactory::INVALID_VALUES_ERROR;
207                 case NFC_ERROR_NO_DEVICE:
208                 case NFC_ERROR_OUT_OF_MEMORY:
209                 case NFC_ERROR_OPERATION_FAILED:
210                 case NFC_ERROR_DEVICE_BUSY:
211                         return DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR;
212                 case NFC_ERROR_NOT_ACTIVATED:
213                         return DeviceAPI::Common::JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR;
214                 case NFC_ERROR_NOT_SUPPORTED:
215                         return DeviceAPI::Common::JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR;
216                 case NFC_ERROR_TIMED_OUT:
217                         return DeviceAPI::Common::JSWebAPIErrorFactory::TIMEOUT_ERROR;
218         }
219         return DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR;
220 }
221
222 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
223         LoggerD ("Errorcode : " << errorCode);
224         switch(errorCode) {
225                 case NFC_ERROR_NONE:
226                 case NFC_ERROR_ALREADY_ACTIVATED:
227                 case NFC_ERROR_ALREADY_DEACTIVATED:
228                         return "";
229                 case NFC_ERROR_INVALID_PARAMETER:
230                         return "Invalid Parameter";
231                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
232                         return "Invalid NDEF Message";
233                 case NFC_ERROR_INVALID_RECORD_TYPE:
234                         return "Invalid Record Type";
235                 case NFC_ERROR_NO_DEVICE:
236                         return "No Device";
237                 case NFC_ERROR_OUT_OF_MEMORY:
238                         return "Out Of Memory";
239                 case NFC_ERROR_NOT_SUPPORTED:
240                         return "NFC Not Supported";
241                 case NFC_ERROR_OPERATION_FAILED:
242                         return "Operation Failed";
243                 case NFC_ERROR_DEVICE_BUSY:
244                         return "Device Busy";
245                 case NFC_ERROR_NOT_ACTIVATED:
246                         return "NFC Not Activated";
247                 case NFC_ERROR_TIMED_OUT:
248                         return "Time Out";
249                 case NFC_ERROR_READ_ONLY_NDEF:
250                         return "Read Only NDEF";
251                 case NFC_ERROR_NO_SPACE_ON_NDEF:
252                         return "No Space On NDEF";
253                 case NFC_ERROR_NO_NDEF_MESSAGE:
254                         return "No NDEF Message";
255                 case NFC_ERROR_NOT_NDEF_FORMAT:
256                         return "Not NDEF Format";
257                 case NFC_ERROR_SECURITY_RESTRICTED:
258                         return "Security Restricted";
259         }
260         return "UnknownError";
261 }
262
263 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
264         LoggerD ("Errorcode : " << static_cast<unsigned int>(errorCode));
265
266         switch(errorCode) {
267                 case NFC_ERROR_NONE:
268                         return;
269                 case NFC_ERROR_INVALID_PARAMETER:
270                 case NFC_ERROR_INVALID_NDEF_MESSAGE:
271                 case NFC_ERROR_INVALID_RECORD_TYPE:
272                         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, message);
273                         break;
274                 case NFC_ERROR_NOT_ACTIVATED:
275                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
276                         break;
277                 case NFC_ERROR_SECURITY_RESTRICTED:
278                         ThrowMsg(WrtDeviceApis::Commons::SecurityException, message);
279                         break;
280                 case NFC_ERROR_NOT_SUPPORTED:
281                 case NFC_ERROR_OPERATION_FAILED:
282                 case NFC_ERROR_DEVICE_BUSY:
283                 case NFC_ERROR_NO_DEVICE:
284                 case NFC_ERROR_TIMED_OUT:
285                 case NFC_ERROR_OUT_OF_MEMORY:
286                 default:
287                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);
288                         break;
289         }
290 }
291
292 }
293 }