2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <Commons/Exception.h>
20 #include <Tizen/Common/JSTizenException.h>
23 #include <dpl/log/log.h>
31 using namespace Api::NFC;
33 std::vector<unsigned char> NFCUtil::toVector(const unsigned char *ch, const int size) {
34 std::vector<unsigned char> vec;
35 if (ch && (size > 0)) {
38 for (i = 0; i < size; i++)
41 LogDebug("result:" << byteToString(ch, size));
45 unsigned char *NFCUtil::toCharPtr(std::vector<unsigned char> vec) {
47 unsigned char * chr = (unsigned char *) malloc(vec.size() * sizeof(unsigned char));
48 for (int i = 0; i < static_cast<int>(vec.size()); i++)
57 char *NFCUtil::byteToString(std::vector<unsigned char> *buffer){
58 static char localbuffer[256];
59 memset(localbuffer, 0, 256);
60 memcpy(localbuffer, toCharPtr(*buffer), buffer->size() > 255 ? 255 : buffer->size());
61 if (buffer->size() > 255)
62 LogDebug("size is " << buffer->size() << ". Too Big! It will copy some of data(255 bytes)");
66 char *NFCUtil::byteToString(const unsigned char* buffer, const int size){
68 LogDebug("size is " << size << ". Too Big! It will copy some of data(255 bytes)");
70 static char localbuffer[256];
71 memset(localbuffer, 0, 256);
72 memcpy(localbuffer, buffer, size > 255 ? 255 : size);
76 nfcTagType NFCUtil::convertTonfcTagType(const unsigned short type) {
77 switch (static_cast<nfc_tag_type_e>(type)) {
78 case NFC_GENERIC_PICC:
79 return NFC_TAGTYPE_GENERIC_PICC;
80 case NFC_ISO14443_A_PICC:
81 return NFC_TAGTYPE_ISO14443_A_PICC;
82 case NFC_ISO14443_4A_PICC:
83 return NFC_TAGTYPE_ISO14443_4A_PICC;
84 case NFC_ISO14443_3A_PICC:
85 return NFC_TAGTYPE_ISO14443_3A_PICC;
86 case NFC_MIFARE_MINI_PICC:
87 return NFC_TAGTYPE_MIFARE_MINI_PICC;
88 case NFC_MIFARE_1K_PICC:
89 return NFC_TAGTYPE_MIFARE_1K_PICC;
90 case NFC_MIFARE_4K_PICC:
91 return NFC_TAGTYPE_MIFARE_4K_PICC;
92 case NFC_MIFARE_ULTRA_PICC:
93 return NFC_TAGTYPE_MIFARE_ULTRA_PICC;
94 case NFC_MIFARE_DESFIRE_PICC:
95 return NFC_TAGTYPE_MIFARE_DESFIRE_PICC;
96 case NFC_ISO14443_B_PICC:
97 return NFC_TAGTYPE_ISO14443_B_PICC;
98 case NFC_ISO14443_4B_PICC:
99 return NFC_TAGTYPE_ISO14443_4B_PICC;
100 case NFC_ISO14443_BPRIME_PICC:
101 return NFC_TAGTYPE_ISO14443_BPRIME_PICC;
102 case NFC_FELICA_PICC:
103 return NFC_TAGTYPE_FELICA_PICC;
105 return NFC_TAGTYPE_JEWEL_PICC;
106 case NFC_ISO15693_PICC:
107 return NFC_TAGTYPE_ISO15693_PICC;
108 case NFC_UNKNOWN_TARGET:
110 return NFC_TAGTYPE_UNKNOWN_TARGET;
114 unsigned short NFCUtil::convertToTNF(nfcTNF tnf) {
116 case NFC_TNF_WELL_KNOWN:
117 return static_cast<unsigned short>(NFC_RECORD_TNF_WELL_KNOWN);
118 case NFC_TNF_MIME_MEDIA:
119 return static_cast<unsigned short>(NFC_RECORD_TNF_MIME_MEDIA);
121 return static_cast<unsigned short>(NFC_RECORD_TNF_URI);
122 case NFC_TNF_EXTERNAL_RTD:
123 return static_cast<unsigned short>(NFC_RECORD_TNF_EXTERNAL_RTD);
124 case NFC_TNF_UNKNOWN:
125 return static_cast<unsigned short>(NFC_RECORD_TNF_UNKNOWN);
126 case NFC_TNF_UNCHAGNED:
127 return static_cast<unsigned short>(NFC_RECORD_TNF_UNCHAGNED);
130 return static_cast<unsigned short>(NFC_RECORD_TNF_EMPTY);
134 nfcTNF NFCUtil::convertTonfcTNF(unsigned short tnf) {
135 switch (static_cast<nfc_record_tnf_e>(tnf)) {
136 case NFC_RECORD_TNF_WELL_KNOWN:
137 return NFC_TNF_WELL_KNOWN;
138 case NFC_RECORD_TNF_MIME_MEDIA:
139 return NFC_TNF_MIME_MEDIA;
140 case NFC_RECORD_TNF_URI:
142 case NFC_RECORD_TNF_EXTERNAL_RTD:
143 return NFC_TNF_EXTERNAL_RTD;
144 case NFC_RECORD_TNF_UNKNOWN:
145 return NFC_TNF_UNKNOWN;
146 case NFC_RECORD_TNF_UNCHAGNED:
147 return NFC_TNF_UNCHAGNED;
148 case NFC_RECORD_TNF_EMPTY:
150 return NFC_TNF_EMPTY;
153 NdefRecordData NFCUtil::getNDEFRecordData(void *handle) {
154 nfc_ndef_record_h recordHandle = static_cast<nfc_ndef_record_h>(handle);
156 NdefRecordData recordData;
158 nfc_record_tnf_e tnf;
159 unsigned char *typeName, *id, *payload;
160 int typeSize, idSize, payloadSize;
162 int result = nfc_ndef_record_get_tnf(recordHandle, &tnf);
163 throwNFCException(result, "Can't get record's tnf");
165 result = nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize);
166 throwNFCException(result, "Can't get record's type");
168 result = nfc_ndef_record_get_id(recordHandle, &id, &idSize);
169 throwNFCException(result, "Can't get record's id");
171 result = nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize);
172 throwNFCException(result, "Can't get record's payload");
174 LogDebug("tnf : " <<tnf);
175 LogDebug("typeName : " << byteToString(typeName, typeSize));
176 LogDebug("payload : " << byteToString(payload, payloadSize));
178 recordData.properties.tnf = convertTonfcTNF(static_cast<unsigned short>(tnf));
179 recordData.properties.typeName = toVector(typeName, typeSize);
180 recordData.properties.id = toVector(id, idSize);
181 recordData.payload = toVector(payload, payloadSize);
186 bool NFCUtil::copyNDEFRecord(void **src, void **dest) {
187 nfc_ndef_record_h *srcHandle, *destHandle;
188 srcHandle = (nfc_ndef_record_h *)src;
189 destHandle = (nfc_ndef_record_h *)dest;
191 NdefRecordData recordData = getNDEFRecordData(*srcHandle);
194 if (nfc_ndef_record_create(destHandle, static_cast<nfc_record_tnf_e>(convertToTNF(recordData.properties.tnf)), toCharPtr(recordData.properties.typeName), recordData.properties.typeName.size(),
195 toCharPtr(recordData.properties.id), recordData.properties.id.size(), toCharPtr(recordData.payload), recordData.payload.size()) != NFC_ERROR_NONE)
200 std::string NFCUtil::getNFCErrorString(const int errorCode) {
201 LogDebug ("Errorcode : " << errorCode);
205 case NFC_ERROR_INVALID_PARAMETER:
206 case NFC_ERROR_INVALID_NDEF_MESSAGE:
207 case NFC_ERROR_INVALID_RECORD_TYPE:
208 return Commons::JSTizenException::INVALID_VALUES_ERROR;
209 case NFC_ERROR_NO_DEVICE:
210 case NFC_ERROR_OUT_OF_MEMORY:
211 case NFC_ERROR_OPERATION_FAILED:
212 case NFC_ERROR_DEVICE_BUSY:
213 return Commons::JSTizenException::UNKNOWN_ERROR;
214 case NFC_ERROR_NOT_ACTIVATED:
215 return Commons::JSTizenException::SERVICE_NOT_AVAILABLE;
216 case NFC_ERROR_NOT_SUPPORTED:
217 return Commons::JSTizenException::NOT_SUPPORTED_ERROR;
218 case NFC_ERROR_TIMED_OUT:
219 return Commons::JSTizenException::TIMEOUT_ERROR;
221 return Commons::JSTizenException::UNKNOWN_ERROR;
224 std::string NFCUtil::getNFCErrorMessage(const int errorCode) {
225 LogDebug ("Errorcode : " << errorCode);
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:
250 return "UnknownError";
253 void NFCUtil::throwNFCException(const int errorCode, const std::string &message) {
254 LogDebug ("Errorcode : " << static_cast<unsigned int>(errorCode));
255 LogDebug ("Message : " << message);
258 case NFC_ERROR_INVALID_PARAMETER:
259 case NFC_ERROR_INVALID_NDEF_MESSAGE:
260 case NFC_ERROR_INVALID_RECORD_TYPE:
261 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, message);
263 case NFC_ERROR_NOT_SUPPORTED:
264 case NFC_ERROR_OPERATION_FAILED:
265 case NFC_ERROR_DEVICE_BUSY:
266 case NFC_ERROR_NO_DEVICE:
267 case NFC_ERROR_TIMED_OUT:
268 case NFC_ERROR_OUT_OF_MEMORY:
269 ThrowMsg(WrtDeviceApis::Commons::UnknownException, message);
271 case NFC_ERROR_NOT_ACTIVATED:
272 ThrowMsg(WrtDeviceApis::Commons::PlatformException, message);