2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <Commons/Exception.h>
21 #include <JSTizenException.h>
22 #include <dpl/log/log.h>
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)) {
35 for (i = 0; i < size; i++)
38 LogDebug("result:" << byteToString(ch, size));
40 LogDebug("result: NULL");
44 unsigned char *NFCUtil::toCharPtr(std::vector<unsigned char> vec) {
46 unsigned char * chr = (unsigned char *) malloc(vec.size() * sizeof(unsigned char));
47 for (int i = 0; i < static_cast<int>(vec.size()); i++)
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);
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)");
66 memcpy(localbuffer, charBuf, buffer->size());
70 LogDebug("Size is 0");
72 LogDebug("Size is 0");
76 char *NFCUtil::byteToString(const unsigned char* buffer, const int size){
78 LogDebug("size is " << size << ". Too Big! It will copy some of data(255 bytes)");
80 static char localbuffer[256];
81 memset(localbuffer, 0, 256);
83 memcpy(localbuffer, buffer, size > 255 ? 255 : size);
85 LogDebug("Size is 0");
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;
118 return NFC_TAGTYPE_JEWEL_PICC;
119 case NFC_ISO15693_PICC:
120 return NFC_TAGTYPE_ISO15693_PICC;
121 case NFC_UNKNOWN_TARGET:
123 return NFC_TAGTYPE_UNKNOWN_TARGET;
127 unsigned short NFCUtil::convertToTNF(nfcTNF 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);
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);
143 return static_cast<unsigned short>(NFC_RECORD_TNF_EMPTY);
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:
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:
163 return NFC_TNF_EMPTY;
167 EventNFCSEType NFCUtil::convertTonfcSEType(unsigned short type) {
168 switch (static_cast<nfc_se_type_e>(type)) {
169 case NFC_SE_TYPE_DISABLE:
171 case NFC_SE_TYPE_ESE:
173 case NFC_SE_TYPE_UICC:
176 LogDebug("Wrong SE Type : " << type);
177 return NFC_SE_INVALID;
181 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
182 nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
184 NdefRecordData recordData;
186 nfc_record_tnf_e tnf;
187 unsigned char *typeName, *id, *payload;
188 int typeSize, idSize, payloadSize;
190 int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
191 throwNFCException(result, "Can't get record's tnf");
193 result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
194 throwNFCException(result, "Can't get record's type");
196 result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
197 throwNFCException(result, "Can't get record's id");
199 result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
200 throwNFCException(result, "Can't get record's payload");
202 LogDebug("tnf : " <<tnf);
203 LogDebug("typeName : " << byteToString(typeName, typeSize));
204 LogDebug("payload : " << byteToString(payload, payloadSize));
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);
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;
219 NdefRecordData recordData = getNDEFRecordData(*srcHandle);
221 unsigned char *typeName = toCharPtr(recordData.properties.typeName);
222 unsigned char *id = toCharPtr(recordData.properties.id);
223 unsigned char *payload = toCharPtr(recordData.payload);
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) {
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;
252 unsigned char *rawdata;
254 int result = nfc_ndef_message_get_rawdata(*srcHandle, &rawdata, &size);
256 if (result != NFC_ERROR_NONE)
259 if (nfc_ndef_message_create_from_rawdata(destHandle, rawdata, size) != NFC_ERROR_NONE) {
270 std::string NFCUtil::getNFCErrorString(const int errorCode) {
271 LogDebug ("Errorcode : " << errorCode);
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;
291 return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
294 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
295 LogDebug ("Errorcode : " << errorCode);
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:
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:
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";
328 return "UnknownError";
331 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
332 LogDebug ("Errorcode : " << static_cast<unsigned int>(errorCode));
333 LogDebug ("Message : " << message);
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);
343 case NFC_ERROR_NOT_ACTIVATED:
344 ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
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:
353 ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);