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>
20 #include <JSWebAPIErrorFactory.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);
79 if ((size > 0) && buffer)
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 void *NFCUtil::makeNDEFRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
127 if ((ndefRecordProperties.tnf < 0) || (ndefRecordProperties.tnf > 6))
128 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Not Supported tnf");
130 nfc_ndef_record_h handle = NULL;
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());
139 int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(ndefRecordProperties.tnf), typeName, typeSize,
140 id, idSize, recordPayload, payloadSize) ;
149 if (result != NFC_ERROR_NONE) {
151 nfc_ndef_record_destroy(handle);
152 throwNFCException(result, " Can't create Ndef Record");
154 ThrowMsg(PlatformException, " Can't create Ndef Record");
155 return (void *)handle;
159 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
161 ThrowMsg(PlatformException, "Invalid NDEFRecord");
163 nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
165 NdefRecordData recordData;
167 nfc_record_tnf_e tnf;
168 unsigned char *typeName, *id, *payload;
169 int typeSize, idSize;
170 unsigned int payloadSize;
172 int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
173 throwNFCException(result, "Can't get record's tnf");
175 result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
176 throwNFCException(result, "Can't get record's type");
178 result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
179 throwNFCException(result, "Can't get record's id");
181 result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
182 throwNFCException(result, "Can't get record's payload");
184 LoggerD("tnf : " <<tnf);
185 LoggerD("typeName : " << byteToString(typeName, typeSize));
186 LoggerD("payload : " << byteToString(payload, payloadSize));
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);
196 std::string NFCUtil::getNFCErrorString(const int errorCode) {
197 LoggerD ("Errorcode : " << errorCode);
200 case NFC_ERROR_ALREADY_ACTIVATED:
201 case NFC_ERROR_ALREADY_DEACTIVATED:
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;
219 return DeviceAPI::Common::JSWebAPIErrorFactory::UNKNOWN_ERROR;
222 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
223 LoggerD ("Errorcode : " << errorCode);
226 case NFC_ERROR_ALREADY_ACTIVATED:
227 case NFC_ERROR_ALREADY_DEACTIVATED:
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:
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:
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";
260 return "UnknownError";
263 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
264 LoggerD ("Errorcode : " << static_cast<unsigned int>(errorCode));
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);
274 case NFC_ERROR_NOT_ACTIVATED:
275 ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);
277 case NFC_ERROR_SECURITY_RESTRICTED:
278 ThrowMsg(WrtDeviceApis::Commons::SecurityException, message);
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:
287 ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);