summaryrefslogtreecommitdiff
path: root/src/standards/Tizen/NFC/NFCConverter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/standards/Tizen/NFC/NFCConverter.cpp')
-rwxr-xr-xsrc/standards/Tizen/NFC/NFCConverter.cpp366
1 files changed, 366 insertions, 0 deletions
diff --git a/src/standards/Tizen/NFC/NFCConverter.cpp b/src/standards/Tizen/NFC/NFCConverter.cpp
new file mode 100755
index 0000000..fb73b04
--- /dev/null
+++ b/src/standards/Tizen/NFC/NFCConverter.cpp
@@ -0,0 +1,366 @@
+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+#include <dpl/log.h>
+#include <CommonsJavaScript/Validator.h>
+#include <CommonsJavaScript/ScopedJSStringRef.h>
+#include <CommonsJavaScript/JSUtils.h>
+#include "NFCConverter.h"
+#include "JSNdefRecord.h"
+#include "JSNdefMessage.h"
+
+using namespace std;
+using namespace TizenApis::Api::NFC;
+using namespace WrtDeviceApis::CommonsJavaScript;
+using namespace WrtDeviceApis;
+
+
+#define NFC_FILTER_ATTRIBUTE_TAG_TYPES "tagTypes"
+
+#define NFC_CHANGED_CALLBACK_ONATTATCH "onAttach"
+#define NFC_CHANGED_CALLBACK_ONDETACH "onDetach"
+
+namespace TizenApis {
+namespace Tizen1_0 {
+
+vector<std::string> NFCConverter::m_allowedTagFilterProperties;
+
+NFCConverter::NFCConverter(JSContextRef context) : Converter(context)
+{
+ LogDebug("entered");
+ static bool init = initializeAllowedProperties();
+ (void) init;
+}
+
+NFCConverter::~NFCConverter()
+{
+ LogDebug("entered");
+}
+
+TagFilterPtr NFCConverter::toNFCTagFilter(JSValueRef jsValue)
+{
+ LogDebug("entered");
+ if (JSValueIsNull(m_context, jsValue) ||
+ JSValueIsUndefined(m_context, jsValue)) {
+ return TagFilterPtr(); //empty filter
+ }
+ TagFilterPtr filter(new TagFilter());
+
+ Validator validator(m_context);
+
+ if (!JSValueIsUndefined(m_context, jsValue)) {
+ vector<std::string> filter_values(toVectorOfStrings(jsValue));
+
+ LogDebug("NFCConverter, start setting filter values, tag_types.size() : " << filter_values.size());
+ for (unsigned int i=0; i<filter_values.size(); i++) {
+ nfcTagType filterValue = toNfcTagType(filter_values.at(i));
+
+ filter->addTagTypeValue(filterValue);
+ LogDebug("NFCConverter, tag_types.at(i): " << filter_values.at(i));
+ }
+ }
+ LogDebug("NFCConverter, filter size == " << filter->getFilterSize());
+
+ return filter;
+}
+
+
+nfcTagType NFCConverter::toNfcTagType(std::string typeString) {
+ if (typeString == "ISO14443_A")
+ return NFC_TAGTYPE_ISO14443_A_PICC;
+ else if (typeString == "ISO14443_4A")
+ return NFC_TAGTYPE_ISO14443_4A_PICC;
+ else if (typeString == "ISO14443_3A")
+ return NFC_TAGTYPE_ISO14443_3A_PICC;
+ else if (typeString == "MIFARE_MINI")
+ return NFC_TAGTYPE_MIFARE_MINI_PICC;
+ else if (typeString == "MIFARE_1K")
+ return NFC_TAGTYPE_MIFARE_1K_PICC;
+ else if (typeString == "MIFARE_4K")
+ return NFC_TAGTYPE_MIFARE_4K_PICC;
+ else if (typeString == "MIFARE_ULTRA")
+ return NFC_TAGTYPE_MIFARE_ULTRA_PICC;
+ else if (typeString == "MIFARE_DESFIRE")
+ return NFC_TAGTYPE_MIFARE_DESFIRE_PICC;
+ else if (typeString == "ISO14443_B")
+ return NFC_TAGTYPE_ISO14443_B_PICC;
+ else if (typeString == "ISO14443_4B")
+ return NFC_TAGTYPE_ISO14443_4B_PICC;
+ else if (typeString == "ISO14443_BPRIME")
+ return NFC_TAGTYPE_ISO14443_BPRIME_PICC;
+ else if (typeString == "FELICA")
+ return NFC_TAGTYPE_FELICA_PICC;
+ else if (typeString == "JEWEL")
+ return NFC_TAGTYPE_JEWEL_PICC;
+ else if (typeString == "ISO15693")
+ return NFC_TAGTYPE_ISO15693_PICC;
+ else
+ ThrowMsg(Commons::InvalidArgumentException, "No Match Tag Type");
+}
+
+std::string NFCConverter::toNfcTagTypeString(nfcTagType type) {
+ switch (type) {
+ case NFC_TAGTYPE_GENERIC_PICC:
+ return "GENERIC_TARGET";
+ break;
+ case NFC_TAGTYPE_ISO14443_A_PICC:
+ return "ISO14443_A";
+ break;
+ case NFC_TAGTYPE_ISO14443_4A_PICC:
+ return "ISO14443_4A";
+ break;
+ case NFC_TAGTYPE_ISO14443_3A_PICC:
+ return "ISO14443_3A";
+ break;
+ case NFC_TAGTYPE_MIFARE_MINI_PICC:
+ return "MIFARE_MINI";
+ break;
+ case NFC_TAGTYPE_MIFARE_1K_PICC:
+ return "MIFARE_1K";
+ break;
+ case NFC_TAGTYPE_MIFARE_4K_PICC:
+ return "MIFARE_4K";
+ break;
+ case NFC_TAGTYPE_MIFARE_ULTRA_PICC:
+ return "MIFARE_ULTRA";
+ break;
+ case NFC_TAGTYPE_MIFARE_DESFIRE_PICC:
+ return "MIFARE_DESFIRE";
+ break;
+ case NFC_TAGTYPE_ISO14443_B_PICC:
+ return "ISO14443_B";
+ break;
+ case NFC_TAGTYPE_ISO14443_4B_PICC:
+ return "ISO14443_4B";
+ break;
+ case NFC_TAGTYPE_ISO14443_BPRIME_PICC:
+ return "ISO14443_BPRIME";
+ break;
+ case NFC_TAGTYPE_FELICA_PICC:
+ return "FELICA";
+ break;
+ case NFC_TAGTYPE_JEWEL_PICC:
+ return "JEWEL";
+ break;
+ case NFC_TAGTYPE_ISO15693_PICC:
+ return "ISO15693";
+ break;
+ case NFC_TAGTYPE_UNKNOWN_TARGET:
+ default:
+ return "UNKNOWN_TARGET";
+ break;
+ }
+}
+
+nfcTextEncodeUTF NFCConverter::toNfcTextEncodeUTF(std::string encodeString) {
+ if (encodeString == "UTF-8")
+ return NFC_TEXT_ENCODE_UTF_8;
+ else if (encodeString == "UTF-16")
+ return NFC_TEXT_ENCODE_UTF_16;
+ else
+ Throw(ConversionException);
+}
+
+std::string NFCConverter::toNfcTextEncodeUTFString(nfcTextEncodeUTF encode) {
+ switch(encode) {
+ case NFC_TEXT_ENCODE_UTF_8:
+ return "UTF-8";
+ break;
+ case NFC_TEXT_ENCODE_UTF_16:
+ return "UTF-16";
+ break;
+ default:
+ Throw(ConversionException);
+ }
+}
+bool NFCConverter::initializeAllowedProperties()
+{
+ LogDebug("entered");
+ //NFC Filter properties
+ m_allowedTagFilterProperties.push_back(NFC_FILTER_ATTRIBUTE_TAG_TYPES);
+ return true;
+}
+
+JSValueRef NFCConverter::toJSValueRef(const std::vector<unsigned char>& arg) {
+ JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
+
+ if (NULL == jsResult) {
+ ThrowMsg(Commons::NullPointerException, "Could not create js array object");
+ }
+
+ for (std::size_t i = 0; i < arg.size(); ++i) {
+ JSValueRef tmpVal = JSValueMakeNumber(m_context, arg[i]);
+ if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
+ ThrowMsg(Commons::UnknownException, "Could not insert value into js array");
+ }
+ }
+
+ return jsResult;
+}
+
+JSValueRef NFCConverter::toJSValueRef(NdefRecordProperties props) {
+ JSObjectRef propertyRef = JSObjectMake(m_context, NULL, NULL);
+
+ ScopedJSStringRef jsTNFString(JSStringCreateWithUTF8CString("tnf"));
+ ScopedJSStringRef jsTypeString(JSStringCreateWithUTF8CString("typeName"));
+ ScopedJSStringRef jsIdString(JSStringCreateWithUTF8CString("id"));
+
+ JSObjectSetProperty(m_context, propertyRef, jsTNFString.get(), toJSValueRef(static_cast<int>(props.tnf)), kJSPropertyAttributeNone, NULL);
+ JSObjectSetProperty(m_context, propertyRef, jsTypeString.get(), toJSValueRef(props.typeName), kJSPropertyAttributeNone, NULL);
+ JSObjectSetProperty(m_context, propertyRef, jsIdString.get(), toJSValueRef(props.id), kJSPropertyAttributeNone, NULL);
+
+ return propertyRef;
+}
+
+JSValueRef NFCConverter::toJSValueRef(std::vector<NFCTagProperties> props) {
+ JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
+ if (NULL == jsResult) {
+ ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
+ "Could not create js array object");
+ }
+
+ for (std::size_t i = 0; i < props.size(); ++i) {
+ JSValueRef tmpVal = toJSValueRef(props[i].value);
+ if (!JSSetArrayElementInArrayList(m_context, jsResult, props[i].key.c_str(), tmpVal)) {
+ ThrowMsg(WrtDeviceApis::Commons::UnknownException,
+ "Could not insert value into js array");
+ }
+ }
+ return jsResult;
+}
+
+NdefRecordProperties NFCConverter::toNdefRecordProperties(JSValueRef value, JSValueRef* exception) {
+ if (JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value)) {
+ ThrowMsg(Commons::InvalidArgumentException,
+ "Message is JS null or JS undefined.");
+ }
+ JSObjectRef obj = toJSObjectRef(value);
+
+ if (!obj) {
+ LogError("Object is null");
+ ThrowMsg(Commons::NullPointerException, "Private object not initialized");
+ }
+
+ NdefRecordProperties props;
+ JSValueRef tnf_JSProperty = JSUtils::getJSProperty(m_context, value, "tnf", exception);
+ JSValueRef type_JSProperty = JSUtils::getJSProperty(m_context, value, "typeName", exception);
+ JSValueRef id_JSProperty = JSUtils::getJSProperty(m_context, value, "id", exception);
+
+ if (tnf_JSProperty != NULL && JSValueIsNumber(m_context, tnf_JSProperty)) {
+ props.tnf = static_cast<nfcTNF>(toLong(tnf_JSProperty));
+ }
+
+ props.typeName= toVectorOfUChars(type_JSProperty);
+ props.id= toVectorOfUChars(id_JSProperty);
+
+ return props;
+}
+
+std::vector<void *> NFCConverter::toVectorOfRecordHandles(const JSValueRef& arg) {
+ if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
+ ThrowMsg(Commons::InvalidArgumentException,
+ "NdefRecordArray is JS null or JS undefined.");
+ }
+
+ if (!JSIsArrayValue(m_context, arg)) {
+ ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
+ }
+
+ std::vector<void *> result;
+ JSObjectRef objArg = toJSObjectRef(arg);
+ for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
+ JSValueRef element = JSGetArrayElement(m_context, objArg, i);
+ result.push_back(getRecordHandle(element));
+ }
+ return result;
+}
+
+void *NFCConverter::getRecordHandle(const JSValueRef& arg) {
+ if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) ||
+ (!JSValueIsObjectOfClass(m_context, arg, JSNdefRecord::getClassRef()))) {
+ ThrowMsg(Commons::InvalidArgumentException,
+ "Message is JS null or JS undefined.");
+ }
+ JSObjectRef obj = toJSObjectRef(arg);
+
+ if (!obj) {
+ LogError("Object is null");
+ ThrowMsg(Commons::NullPointerException, "Private object not initialized");
+ }
+ NdefRecordPrivObject* privateObject = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(obj));
+ if (!privateObject) {
+ LogError("Private object is not set.");
+ ThrowMsg(Commons::NullPointerException, "Private object not initialized");
+ }
+
+ INdefRecordPtr record = privateObject->getObject();
+
+ return record->getHandle();
+}
+
+void *NFCConverter::getMessageHandle(const JSValueRef& arg) {
+ if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) ||
+ (!JSValueIsObjectOfClass(m_context, arg, JSNdefMessage::getClassRef()))) {
+ ThrowMsg(Commons::InvalidArgumentException,
+ "Message is JS null or JS undefined.");
+ }
+ JSObjectRef obj = toJSObjectRef(arg);
+
+ if (!obj) {
+ LogError("Object is null");
+ ThrowMsg(Commons::NullPointerException, "Private object not initialized");
+ }
+ NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(obj));
+ if (!privateObject) {
+ LogError("Private object is not set.");
+ ThrowMsg(Commons::NullPointerException, "Private object not initialized");
+ }
+
+ INdefMessagePtr message = privateObject->getObject();
+
+ return message->getHandle();
+}
+
+NFCChangedCallback NFCConverter::toNFCChangedCallback(const JSValueRef& arg) {
+ LogDebug("Entered");
+ JSObjectRef object = toJSObjectRef(arg);
+
+ NFCChangedCallback result;
+ Validator validator(m_context);
+
+ result.onAttach= JSUtils::getJSPropertyOrUndefined(
+ m_context, object, NFC_CHANGED_CALLBACK_ONATTATCH
+ );
+ if (!validator.isNullOrUndefined(result.onAttach) &&
+ !validator.isCallback(result.onAttach)) {
+ ThrowMsg(Commons::ConversionException, "Not a valid callback.");
+ }
+
+ result.onDetach= JSUtils::getJSPropertyOrUndefined(
+ m_context, object, NFC_CHANGED_CALLBACK_ONDETACH
+ );
+ if (!validator.isNullOrUndefined(result.onDetach) &&
+ !validator.isCallback(result.onDetach)) {
+ ThrowMsg(Commons::ConversionException, "Not a valid callback.");
+ }
+
+ return result;
+}
+
+}
+}