diff options
Diffstat (limited to 'mobile_src/NFC/NFCConverter.cpp')
-rwxr-xr-x | mobile_src/NFC/NFCConverter.cpp | 380 |
1 files changed, 380 insertions, 0 deletions
diff --git a/mobile_src/NFC/NFCConverter.cpp b/mobile_src/NFC/NFCConverter.cpp new file mode 100755 index 0000000..7d93350 --- /dev/null +++ b/mobile_src/NFC/NFCConverter.cpp @@ -0,0 +1,380 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// 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 <CommonsJavaScript/Validator.h> +#include <CommonsJavaScript/ScopedJSStringRef.h> +#include <CommonsJavaScript/JSUtils.h> +#include <JSUtil.h> +#include "NFCFactory.h" +#include "NFCConverter.h" +#include "JSNdefRecord.h" +#include "JSNdefRecordText.h" +#include "JSNdefRecordURI.h" +#include "JSNdefRecordMedia.h" +#include "JSNdefMessage.h" +#include <Logger.h> + +using namespace std; +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 DeviceAPI { +namespace NFC { + +vector<std::string> NFCConverter::m_allowedTagFilterProperties; + +NFCConverter::NFCConverter(JSContextRef context) : Converter(context) +{ + LoggerD("entered"); + static bool init = initializeAllowedProperties(); + (void) init; +} + +NFCConverter::~NFCConverter() +{ + LoggerD("entered"); +} + +TagFilterPtr NFCConverter::toNFCTagFilter(JSValueRef jsValue) +{ + LoggerD("entered"); + if (JSValueIsNull(m_context, jsValue) || + JSValueIsUndefined(m_context, jsValue)) { + return TagFilterPtr(); //empty filter + } + TagFilterPtr filter(new TagFilter()); + + if (!JSValueIsUndefined(m_context, jsValue)) { + vector<std::string> filter_values(toVectorOfStrings(jsValue)); + + LoggerD("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); + LoggerD("NFCConverter, tag_types.at(i): " << filter_values.at(i)); + } + } + LoggerD("NFCConverter, filter size == " << filter->getFilterSize()); + + return filter; +} + + +nfcTagType NFCConverter::toNfcTagType(std::string typeString) { + if (typeString == "GENERIC_TARGET") + return NFC_TAGTYPE_GENERIC_PICC; + else 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 if (typeString == "UNKNOWN_TARGET") + return NFC_TAGTYPE_UNKNOWN_TARGET; + else + ThrowMsg(Commons::ConversionException, "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 == "UTF16") + return NFC_TEXT_ENCODE_UTF_16; + else + return NFC_TEXT_ENCODE_UTF_8; + +} + +std::string NFCConverter::toNfcTextEncodeUTFString(nfcTextEncodeUTF encode) { + switch(encode) { + case NFC_TEXT_ENCODE_UTF_16: + return "UTF16"; + break; + case NFC_TEXT_ENCODE_UTF_8: + default: + return "UTF8"; + break; + } +} + +bool NFCConverter::initializeAllowedProperties() +{ + LoggerD("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::UnknownException, "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(NdefRecordData arg) { + if (arg.properties.tnf == NFC_TNF_MIME_MEDIA) + return JSNdefRecordMedia::createJSObject(m_context, arg.properties, arg.payload); + if (arg.properties.tnf == NFC_TNF_WELL_KNOWN) { + if (arg.properties.typeName.size() > 0) { + if (arg.properties.typeName[0] == 0x54) + return JSNdefRecordText::createJSObject(m_context, arg.properties, arg.payload); + if (arg.properties.typeName[0] == 0x55) + return JSNdefRecordURI::createJSObject(m_context, arg.properties, arg.payload); + } + } + return JSNdefRecord::createJSObject(m_context, arg.properties, arg.payload); +} + +JSValueRef NFCConverter::toJSValueRef(std::vector<NFCTagProperties> props) { + JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL); + if (NULL == jsResult) { + ThrowMsg(WrtDeviceApis::Commons::UnknownException, + "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; +} + +std::vector<unsigned char> NFCConverter::toVectorOfOctets(const JSValueRef& arg) { + return Common::JSUtil::JSArrayToType_<unsigned char>(m_context, arg, Common::JSUtil::JSValueToOctet); +} + +bool NFCConverter::isNdefRecord(const JSValueRef& arg) { + LoggerD("Entered"); + if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) { + LoggerD("Object is Null or Undefied"); + return false; + } + if (JSValueIsObjectOfClass(m_context, arg, JSNdefRecord::getClassRef()) + || JSValueIsObjectOfClass(m_context, arg, JSNdefRecordText::getClassRef()) + || JSValueIsObjectOfClass(m_context, arg, JSNdefRecordURI::getClassRef()) + || JSValueIsObjectOfClass(m_context, arg, JSNdefRecordMedia::getClassRef())) + return true; + + return false; +} + +std::vector<void *> NFCConverter::toVectorOfRecordHandles(const JSValueRef& arg) { + if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) { + ThrowMsg(Commons::ConversionException, + "NdefRecordArray is JS null or JS undefined."); + } + + if (!JSIsArrayValue(m_context, arg)) { + ThrowMsg(Commons::ConversionException, "Argument is not an JS array."); + } + + JSObjectRef obj = toJSObjectRef(arg); + + if (!obj) { + LoggerE("Object is null"); + ThrowMsg(Commons::ConversionException, "Object is null"); + } + + return toVectorOfRecordHandles(obj); +} + +std::vector<void *> NFCConverter::toVectorOfRecordHandles(const JSObjectRef& obj) { + std::vector<void *> result; + for (std::size_t i = 0; i < JSGetArrayLength(m_context, obj); ++i) { + JSValueRef element = JSGetArrayElement(m_context, obj, i); + try { + if (isNdefRecord(element)) + result.push_back(getRecordHandle(element)); + else + ThrowMsg(Commons::ConversionException, "JS array has items those are not NDEFRecord."); + } catch (const WrtDeviceApis::Commons::Exception& err) { + LoggerE(err.GetClassName() << ":"<<err.GetMessage()); + for (std::size_t j = 0; j < result.size(); ++j) { + if (result[j]) + nfc_ndef_record_destroy((nfc_ndef_record_h)result[j]); + } + ThrowMsg(Commons::ConversionException, "JS array has items those are not NDEFRecords or invalid records."); + } + } + return result; +} + +std::string NFCConverter::toRecordClassName(NdefRecordData arg) { + if (arg.properties.tnf == NFC_TNF_MIME_MEDIA) + return JSNdefRecordMedia::getClassInfo()->className; + if (arg.properties.tnf == NFC_TNF_WELL_KNOWN) { + if (arg.properties.typeName.size() > 0) { + if (arg.properties.typeName[0] == 0x54) + return JSNdefRecordText::getClassInfo()->className; + if (arg.properties.typeName[0] == 0x55) + return JSNdefRecordURI::getClassInfo()->className; + } + } + return JSNdefRecord::getClassInfo()->className; +} + +void *NFCConverter::getRecordHandle(const JSValueRef& arg) { + LoggerD("Entered"); + if (!isNdefRecord(arg)) { + ThrowMsg(Commons::ConversionException, + "Not record value"); + } + JSObjectRef obj = toJSObjectRef(arg); + + if (!obj) { + LoggerE("Object is null"); + ThrowMsg(Commons::ConversionException, "Object is null"); + } + NdefRecordPrivObject* privateObject = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(obj)); + if (!privateObject) { + LoggerE("Private object is not set."); + ThrowMsg(Commons::ConversionException, "Private object not set"); + } + + INdefRecordPtr record = privateObject->getObject(); + void *handle = record->getHandle(); + if (handle == NULL) + ThrowMsg(Commons::ConversionException, "Invalid Handle"); + return handle; +} + +NFCChangedCallback NFCConverter::toNFCChangedCallback(const JSValueRef& arg) { + LoggerD("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."); + } + + if (validator.isNullOrUndefined(result.onattach) && validator.isNullOrUndefined(result.ondetach)) + ThrowMsg(Commons::ConversionException, "Not a valid callback."); + + return result; +} + +} +} |