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;
168 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
169 nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
171 NdefRecordData recordData;
173 nfc_record_tnf_e tnf;
174 unsigned char *typeName, *id, *payload;
175 int typeSize, idSize, payloadSize;
177 int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
178 throwNFCException(result, "Can't get record's tnf");
180 result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
181 throwNFCException(result, "Can't get record's type");
183 result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
184 throwNFCException(result, "Can't get record's id");
186 result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
187 throwNFCException(result, "Can't get record's payload");
189 LogDebug("tnf : " <<tnf);
190 LogDebug("typeName : " << byteToString(typeName, typeSize));
191 LogDebug("payload : " << byteToString(payload, payloadSize));
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);
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;
206 NdefRecordData recordData = getNDEFRecordData(*srcHandle);
208 unsigned char *typeName = toCharPtr(recordData.properties.typeName);
209 unsigned char *id = toCharPtr(recordData.properties.id);
210 unsigned char *payload = toCharPtr(recordData.payload);
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) {
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;
239 unsigned char *rawdata;
241 int result = nfc_ndef_message_get_rawdata(*srcHandle, &rawdata, &size);
243 if (result != NFC_ERROR_NONE)
246 if (nfc_ndef_message_create_from_rawdata(destHandle, rawdata, size) != NFC_ERROR_NONE) {
257 std::string NFCUtil::getNFCErrorString(const int errorCode) {
258 LogDebug ("Errorcode : " << errorCode);
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;
278 return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
281 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
282 LogDebug ("Errorcode : " << errorCode);
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:
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:
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";
315 return "UnknownError";
318 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
319 LogDebug ("Errorcode : " << static_cast<unsigned int>(errorCode));
320 LogDebug ("Message : " << message);
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);
330 case NFC_ERROR_NOT_ACTIVATED:
331 ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
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:
340 ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);