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 <dpl/log/log.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/ScopedJSStringRef.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include "NFCConverter.h"
24 #include "JSNdefRecord.h"
25 #include "JSNdefMessage.h"
28 using namespace TizenApis::Api::NFC;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30 using namespace WrtDeviceApis;
33 #define NFC_FILTER_ATTRIBUTE_TAG_TYPES "tagTypes"
35 #define NFC_CHANGED_CALLBACK_ONATTATCH "onAttach"
36 #define NFC_CHANGED_CALLBACK_ONDETACH "onDetach"
41 vector<std::string> NFCConverter::m_allowedTagFilterProperties;
43 NFCConverter::NFCConverter(JSContextRef context) : Converter(context)
46 static bool init = initializeAllowedProperties();
50 NFCConverter::~NFCConverter()
55 TagFilterPtr NFCConverter::toNFCTagFilter(JSValueRef jsValue)
58 if (JSValueIsNull(m_context, jsValue) ||
59 JSValueIsUndefined(m_context, jsValue)) {
60 return TagFilterPtr(); //empty filter
62 TagFilterPtr filter(new TagFilter());
64 Validator validator(m_context);
66 if (!JSValueIsUndefined(m_context, jsValue)) {
67 vector<std::string> filter_values(toVectorOfStrings(jsValue));
69 LogDebug("NFCConverter, start setting filter values, tag_types.size() : " << filter_values.size());
70 for (unsigned int i=0; i<filter_values.size(); i++) {
71 nfcTagType filterValue = toNfcTagType(filter_values.at(i));
73 filter->addTagTypeValue(filterValue);
74 LogDebug("NFCConverter, tag_types.at(i): " << filter_values.at(i));
77 LogDebug("NFCConverter, filter size == " << filter->getFilterSize());
83 nfcTagType NFCConverter::toNfcTagType(std::string typeString) {
84 if (typeString == "ISO14443_A")
85 return NFC_TAGTYPE_ISO14443_A_PICC;
86 else if (typeString == "ISO14443_4A")
87 return NFC_TAGTYPE_ISO14443_4A_PICC;
88 else if (typeString == "ISO14443_3A")
89 return NFC_TAGTYPE_ISO14443_3A_PICC;
90 else if (typeString == "MIFARE_MINI")
91 return NFC_TAGTYPE_MIFARE_MINI_PICC;
92 else if (typeString == "MIFARE_1K")
93 return NFC_TAGTYPE_MIFARE_1K_PICC;
94 else if (typeString == "MIFARE_4K")
95 return NFC_TAGTYPE_MIFARE_4K_PICC;
96 else if (typeString == "MIFARE_ULTRA")
97 return NFC_TAGTYPE_MIFARE_ULTRA_PICC;
98 else if (typeString == "MIFARE_DESFIRE")
99 return NFC_TAGTYPE_MIFARE_DESFIRE_PICC;
100 else if (typeString == "ISO14443_B")
101 return NFC_TAGTYPE_ISO14443_B_PICC;
102 else if (typeString == "ISO14443_4B")
103 return NFC_TAGTYPE_ISO14443_4B_PICC;
104 else if (typeString == "ISO14443_BPRIME")
105 return NFC_TAGTYPE_ISO14443_BPRIME_PICC;
106 else if (typeString == "FELICA")
107 return NFC_TAGTYPE_FELICA_PICC;
108 else if (typeString == "JEWEL")
109 return NFC_TAGTYPE_JEWEL_PICC;
110 else if (typeString == "ISO15693")
111 return NFC_TAGTYPE_ISO15693_PICC;
113 ThrowMsg(Commons::InvalidArgumentException, "No Match Tag Type");
116 std::string NFCConverter::toNfcTagTypeString(nfcTagType type) {
118 case NFC_TAGTYPE_GENERIC_PICC:
119 return "GENERIC_TARGET";
121 case NFC_TAGTYPE_ISO14443_A_PICC:
124 case NFC_TAGTYPE_ISO14443_4A_PICC:
125 return "ISO14443_4A";
127 case NFC_TAGTYPE_ISO14443_3A_PICC:
128 return "ISO14443_3A";
130 case NFC_TAGTYPE_MIFARE_MINI_PICC:
131 return "MIFARE_MINI";
133 case NFC_TAGTYPE_MIFARE_1K_PICC:
136 case NFC_TAGTYPE_MIFARE_4K_PICC:
139 case NFC_TAGTYPE_MIFARE_ULTRA_PICC:
140 return "MIFARE_ULTRA";
142 case NFC_TAGTYPE_MIFARE_DESFIRE_PICC:
143 return "MIFARE_DESFIRE";
145 case NFC_TAGTYPE_ISO14443_B_PICC:
148 case NFC_TAGTYPE_ISO14443_4B_PICC:
149 return "ISO14443_4B";
151 case NFC_TAGTYPE_ISO14443_BPRIME_PICC:
152 return "ISO14443_BPRIME";
154 case NFC_TAGTYPE_FELICA_PICC:
157 case NFC_TAGTYPE_JEWEL_PICC:
160 case NFC_TAGTYPE_ISO15693_PICC:
163 case NFC_TAGTYPE_UNKNOWN_TARGET:
165 return "UNKNOWN_TARGET";
170 nfcTextEncodeUTF NFCConverter::toNfcTextEncodeUTF(std::string encodeString) {
171 if (encodeString == "UTF-8")
172 return NFC_TEXT_ENCODE_UTF_8;
173 else if (encodeString == "UTF-16")
174 return NFC_TEXT_ENCODE_UTF_16;
176 Throw(ConversionException);
179 std::string NFCConverter::toNfcTextEncodeUTFString(nfcTextEncodeUTF encode) {
181 case NFC_TEXT_ENCODE_UTF_8:
184 case NFC_TEXT_ENCODE_UTF_16:
188 Throw(ConversionException);
191 bool NFCConverter::initializeAllowedProperties()
194 //NFC Filter properties
195 m_allowedTagFilterProperties.push_back(NFC_FILTER_ATTRIBUTE_TAG_TYPES);
199 JSValueRef NFCConverter::toJSValueRef(const std::vector<unsigned char>& arg) {
200 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
202 if (NULL == jsResult) {
203 ThrowMsg(Commons::NullPointerException, "Could not create js array object");
206 for (std::size_t i = 0; i < arg.size(); ++i) {
207 JSValueRef tmpVal = JSValueMakeNumber(m_context, arg[i]);
208 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
209 ThrowMsg(Commons::UnknownException, "Could not insert value into js array");
216 JSValueRef NFCConverter::toJSValueRef(NdefRecordProperties props) {
217 JSObjectRef propertyRef = JSObjectMake(m_context, NULL, NULL);
219 ScopedJSStringRef jsTNFString(JSStringCreateWithUTF8CString("tnf"));
220 ScopedJSStringRef jsTypeString(JSStringCreateWithUTF8CString("typeName"));
221 ScopedJSStringRef jsIdString(JSStringCreateWithUTF8CString("id"));
223 JSObjectSetProperty(m_context, propertyRef, jsTNFString.get(), toJSValueRef(static_cast<int>(props.tnf)), kJSPropertyAttributeNone, NULL);
224 JSObjectSetProperty(m_context, propertyRef, jsTypeString.get(), toJSValueRef(props.typeName), kJSPropertyAttributeNone, NULL);
225 JSObjectSetProperty(m_context, propertyRef, jsIdString.get(), toJSValueRef(props.id), kJSPropertyAttributeNone, NULL);
230 JSValueRef NFCConverter::toJSValueRef(std::vector<NFCTagProperties> props) {
231 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
232 if (NULL == jsResult) {
233 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
234 "Could not create js array object");
237 for (std::size_t i = 0; i < props.size(); ++i) {
238 JSValueRef tmpVal = toJSValueRef(props[i].value);
239 if (!JSSetArrayElementInArrayList(m_context, jsResult, props[i].key.c_str(), tmpVal)) {
240 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
241 "Could not insert value into js array");
247 NdefRecordProperties NFCConverter::toNdefRecordProperties(JSValueRef value, JSValueRef* exception) {
248 if (JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value)) {
249 ThrowMsg(Commons::InvalidArgumentException,
250 "Message is JS null or JS undefined.");
252 JSObjectRef obj = toJSObjectRef(value);
255 LogError("Object is null");
256 ThrowMsg(Commons::NullPointerException, "Private object not initialized");
259 NdefRecordProperties props;
260 JSValueRef tnf_JSProperty = JSUtils::getJSProperty(m_context, value, "tnf", exception);
261 JSValueRef type_JSProperty = JSUtils::getJSProperty(m_context, value, "typeName", exception);
262 JSValueRef id_JSProperty = JSUtils::getJSProperty(m_context, value, "id", exception);
264 if (tnf_JSProperty != NULL && JSValueIsNumber(m_context, tnf_JSProperty)) {
265 props.tnf = static_cast<nfcTNF>(toLong(tnf_JSProperty));
268 props.typeName= toVectorOfUChars(type_JSProperty);
269 props.id= toVectorOfUChars(id_JSProperty);
274 std::vector<void *> NFCConverter::toVectorOfRecordHandles(const JSValueRef& arg) {
275 if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
276 ThrowMsg(Commons::InvalidArgumentException,
277 "NdefRecordArray is JS null or JS undefined.");
280 if (!JSIsArrayValue(m_context, arg)) {
281 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
284 std::vector<void *> result;
285 JSObjectRef objArg = toJSObjectRef(arg);
286 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
287 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
288 result.push_back(getRecordHandle(element));
293 void *NFCConverter::getRecordHandle(const JSValueRef& arg) {
294 if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) ||
295 (!JSValueIsObjectOfClass(m_context, arg, JSNdefRecord::getClassRef()))) {
296 ThrowMsg(Commons::InvalidArgumentException,
297 "Message is JS null or JS undefined.");
299 JSObjectRef obj = toJSObjectRef(arg);
302 LogError("Object is null");
303 ThrowMsg(Commons::NullPointerException, "Private object not initialized");
305 NdefRecordPrivObject* privateObject = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(obj));
306 if (!privateObject) {
307 LogError("Private object is not set.");
308 ThrowMsg(Commons::NullPointerException, "Private object not initialized");
311 INdefRecordPtr record = privateObject->getObject();
313 return record->getHandle();
316 void *NFCConverter::getMessageHandle(const JSValueRef& arg) {
317 if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) ||
318 (!JSValueIsObjectOfClass(m_context, arg, JSNdefMessage::getClassRef()))) {
319 ThrowMsg(Commons::InvalidArgumentException,
320 "Message is JS null or JS undefined.");
322 JSObjectRef obj = toJSObjectRef(arg);
325 LogError("Object is null");
326 ThrowMsg(Commons::NullPointerException, "Private object not initialized");
328 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(obj));
329 if (!privateObject) {
330 LogError("Private object is not set.");
331 ThrowMsg(Commons::NullPointerException, "Private object not initialized");
334 INdefMessagePtr message = privateObject->getObject();
336 return message->getHandle();
339 NFCChangedCallback NFCConverter::toNFCChangedCallback(const JSValueRef& arg) {
341 JSObjectRef object = toJSObjectRef(arg);
343 NFCChangedCallback result;
344 Validator validator(m_context);
346 result.onAttach= JSUtils::getJSPropertyOrUndefined(
347 m_context, object, NFC_CHANGED_CALLBACK_ONATTATCH
349 if (!validator.isNullOrUndefined(result.onAttach) &&
350 !validator.isCallback(result.onAttach)) {
351 ThrowMsg(Commons::ConversionException, "Not a valid callback.");
354 result.onDetach= JSUtils::getJSPropertyOrUndefined(
355 m_context, object, NFC_CHANGED_CALLBACK_ONDETACH
357 if (!validator.isNullOrUndefined(result.onDetach) &&
358 !validator.isCallback(result.onDetach)) {
359 ThrowMsg(Commons::ConversionException, "Not a valid callback.");