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.
18 #include <Commons/Exception.h>
19 #include <JSTizenException.h>
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)) {
32 for (i = 0; i < size; i++)
35 LoggerD("result:" << byteToString(ch, size));
37 LoggerD("result: NULL");
41 unsigned char *NFCUtil::toCharPtr(std::vector<unsigned char> vec) {
43 unsigned char * chr = (unsigned char *) malloc(vec.size() * sizeof(unsigned char));
44 for (int i = 0; i < static_cast<int>(vec.size()); i++)
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);
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)");
63 memcpy(localbuffer, charBuf, buffer->size());
73 char *NFCUtil::byteToString(const unsigned char* buffer, const int size){
75 LoggerD("size is " << size << ". Too Big! It will copy some of data(255 bytes)");
77 static char localbuffer[256];
78 memset(localbuffer, 0, 256);
80 memcpy(localbuffer, buffer, size > 255 ? 255 : size);
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;
115 return NFC_TAGTYPE_JEWEL_PICC;
116 case NFC_ISO15693_PICC:
117 return NFC_TAGTYPE_ISO15693_PICC;
118 case NFC_UNKNOWN_TARGET:
120 return NFC_TAGTYPE_UNKNOWN_TARGET;
124 unsigned short NFCUtil::convertToTNF(nfcTNF 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);
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);
140 return static_cast<unsigned short>(NFC_RECORD_TNF_EMPTY);
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:
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:
160 return NFC_TNF_EMPTY;
165 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
166 nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
168 NdefRecordData recordData;
170 nfc_record_tnf_e tnf;
171 unsigned char *typeName, *id, *payload;
172 int typeSize, idSize, payloadSize;
174 int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
175 throwNFCException(result, "Can't get record's tnf");
177 result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
178 throwNFCException(result, "Can't get record's type");
180 result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
181 throwNFCException(result, "Can't get record's id");
183 result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
184 throwNFCException(result, "Can't get record's payload");
186 LoggerD("tnf : " <<tnf);
187 LoggerD("typeName : " << byteToString(typeName, typeSize));
188 LoggerD("payload : " << byteToString(payload, payloadSize));
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);
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;
203 NdefRecordData recordData = getNDEFRecordData(*srcHandle);
205 unsigned char *typeName = toCharPtr(recordData.properties.typeName);
206 unsigned char *id = toCharPtr(recordData.properties.id);
207 unsigned char *payload = toCharPtr(recordData.payload);
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) {
231 std::string NFCUtil::getNFCErrorString(const int errorCode) {
232 LoggerD ("Errorcode : " << errorCode);
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;
252 return DeviceAPI::Common::JSTizenException::UNKNOWN_ERROR;
255 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
256 LoggerD ("Errorcode : " << errorCode);
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:
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:
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";
289 return "UnknownError";
292 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
293 LoggerD ("Errorcode : " << static_cast<unsigned int>(errorCode));
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);
303 case NFC_ERROR_NOT_ACTIVATED:
304 ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
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:
313 ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);