From: Dongjin Choi Date: Tue, 20 Aug 2013 05:50:07 +0000 (+0900) Subject: Update change log and spec for wrt-plugins-tizen_0.4.61 X-Git-Tag: accepted/tizen/20131028.214051~6^2~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e31c4f1d2c34585d52f07dd61bd80235e86636e7;p=platform%2Fframework%2Fweb%2Fwrt-plugins-tizen.git Update change log and spec for wrt-plugins-tizen_0.4.61 [model] REDWOOD [binary_type] PDA [customer] OPEN [Issue] N/A [Problem] Intensive TC fail [Cause] When constructing NDEFMessage ,NDEFRecord, NDEFRecordText, NDEFRecordURI or NDEFRecordMedia, it checks if arguments are valid or not. And if they are not valid, it sends an exception. [Solution] It will not send exception in the cases. It will use default values instead of invalid arguments when setting attributes or constructing them with invalid ones or send exceptions when calling APIs that use them. [Issue#] Docomo QA 3 [Problem] Even if User-Agent changed, it is not applied to download API [Cause] Download API requires Header setting [Solution] Set User-Agent to download API [Issue] N/A [Problem] TCT Fail [Cause] It doesn't raise exception if the type of listener's argument is undefined. And if it doesn't have any valid argument, it raises exception. [Solution] It will check arguments's types using ArgumentValidator. [Issue] N/A [Problem] can assign null value to title. [Cause] don't use StaticValue. [Solution] using StaticValue. [Issue#] N/A [Problem] getAppContext fails not on independent WebApp [Cause] This function have got appID from platform API [Solution] API must get appID from WRT [Issue#] P130815-05559 [Problem] "undefined" value is returned when rrule is set to null. [Cause] No clear spec description. [Solution] Return null when rrule is set to null. [team] WebAPI [request] N/A [horizontal_expansion] N/A UnitTC passed. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 38b193c..2eef951 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,12 +95,13 @@ ENDIF(ENABLE_OPTIONAL_SE) # ----------------------------------------------------------------------------- # CFlags # ----------------------------------------------------------------------------- +SET(OPTIMIZATION_FLAGS "-falign-functions -falign-jumps -falign-loops -falign-labels -freorder-blocks -ftree-vect-loop-version") SET(CMAKE_C_FLAGS_PROFILING "-O0 -g -pg") SET(CMAKE_CXX_FLAGS_PROFILING "-O0 -std=c++0x -g -pg") SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -std=c++0x -g") -SET(CMAKE_C_FLAGS_RELEASE "-O2 -g") -SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -std=c++0x -g") +SET(CMAKE_C_FLAGS_RELEASE "-Os -g ${OPTIMIZATION_FLAGS}") +SET(CMAKE_CXX_FLAGS_RELEASE "-Os -std=c++0x -g ${OPTIMIZATION_FLAGS}") ADD_DEFINITIONS("-DCLIENT_IPC_THREAD") ADD_DEFINITIONS("-DEXPORT_API=") ADD_DEFINITIONS("-Wall") diff --git a/packaging/wrt-plugins-tizen.spec b/packaging/wrt-plugins-tizen.spec index cdec69c..e928e2f 100755 --- a/packaging/wrt-plugins-tizen.spec +++ b/packaging/wrt-plugins-tizen.spec @@ -1,6 +1,6 @@ Name: wrt-plugins-tizen Summary: JavaScript plugins for WebRuntime -Version: 0.4.60 +Version: 0.4.61 Release: 0 Group: Development/Libraries License: Apache License, Version 2.0 @@ -90,6 +90,12 @@ Wrt-plugin-tizen development headers export LDFLAGS+="-Wl,--rpath=%{PREFIX} -Wl,--as-needed" +%if 0%{?tizen_build_binary_release_type_eng} +export CFLAGS="$CFLAGS -DTIZEN_ENGINEER_MODE" +export CXXFLAGS="$CXXFLAGS -DTIZEN_ENGINEER_MODE" +export FFLAGS="$FFLAGS -DTIZEN_ENGINEER_MODE" +%endif + cmake \ -DENABLE_OPTIONAL_BT=YES \ -DENABLE_OPTIONAL_CALL_HISTORY=YES \ diff --git a/src/Application/ApplicationManager.cpp b/src/Application/ApplicationManager.cpp index d9a3b06..f13f930 100644 --- a/src/Application/ApplicationManager.cpp +++ b/src/Application/ApplicationManager.cpp @@ -696,56 +696,73 @@ ApplicationContextPtr ApplicationManager::getAppContext(const std::string id) int ret = 0; std::string contextId = id; - int pid; + std::string appId; - if (contextId.empty()) - { - //pid = getpid(); - pid = getppid(); + int selfpid = getppid(); + if(contextId.empty()) + { std::stringstream sstr; - sstr << pid; + sstr << selfpid; contextId = sstr.str(); + + appId = get_current_app_id(); } else { + int pid = 0; std::stringstream(contextId) >> pid; if (pid <= 0) { LoggerE("Given contextId is wrong"); ThrowMsg(NotFoundException, "Given contextId is wrong"); } - } - char *app_id = NULL; - - TIME_TRACER_ITEM_BEGIN("(getAppContext)app_manager_get_app_id", 0); - ret = app_manager_get_app_id(pid, &app_id); - TIME_TRACER_ITEM_END("(getAppContext)app_manager_get_app_id", 0); - if(ret != APP_MANAGER_ERROR_NONE) - { - if(app_id) - free(app_id); + if(pid == selfpid) + { + std::stringstream sstr; + sstr << selfpid; + contextId = sstr.str(); - switch(ret) + appId = get_current_app_id(); + } + else { - case APP_MANAGER_ERROR_NO_SUCH_APP: - case APP_MANAGER_ERROR_INVALID_PARAMETER: - LoggerE("app_manager_get_app_id error : no such app"); - ThrowMsg(NotFoundException, "app_manager_get_app_id error : no such app"); - default: - LoggerE("app_manager_get_app_id error (" << ret << ")"); - ThrowMsg(UnknownException, "app_manager_get_app_id error : unknown error"); + char *app_id = NULL; + + TIME_TRACER_ITEM_BEGIN("(getAppContext)app_manager_get_app_id", 0); + ret = app_manager_get_app_id(pid, &app_id); + TIME_TRACER_ITEM_END("(getAppContext)app_manager_get_app_id", 0); + if(ret != APP_MANAGER_ERROR_NONE) + { + if(app_id) + free(app_id); + + switch(ret) + { + case APP_MANAGER_ERROR_NO_SUCH_APP: + case APP_MANAGER_ERROR_INVALID_PARAMETER: + LoggerE("app_manager_get_app_id error : no such app"); + ThrowMsg(NotFoundException, "app_manager_get_app_id error : no such app"); + break; + default: + LoggerE("app_manager_get_app_id error (" << ret << ")"); + ThrowMsg(UnknownException, "app_manager_get_app_id error : unknown error"); + break; + } + } + + appId = app_id; + + if(app_id) + free(app_id); } } ApplicationContextPtr appContext(new ApplicationContext()); - appContext->setAppId(app_id); + appContext->setAppId(appId); appContext->setContextId(contextId); - if(app_id) - free(app_id); - return appContext; } diff --git a/src/Calendar/JSCalendarEvent.cpp b/src/Calendar/JSCalendarEvent.cpp index c5691b8..0969560 100755 --- a/src/Calendar/JSCalendarEvent.cpp +++ b/src/Calendar/JSCalendarEvent.cpp @@ -471,9 +471,10 @@ JSValueRef JSCalendarEvent::getPropertyRecurrenceRule(JSContextRef context, } if (NULL==rrule) { + LogInfo("Null rrule."); return JSValueMakeNull(context); } else if (EventRecurrenceRule::NO_RECURRENCE==rrule->getFrequency()) { - return JSValueMakeUndefined(context); + return JSValueMakeNull(context); } else { return JSCalendarRecurrenceRule::createJSCalendarRecurrenceRule(priv->getContext(), rrule); } @@ -501,8 +502,7 @@ bool JSCalendarEvent::setPropertyRecurrenceRule(JSContextRef context, } if (JSValueIsNull(context, value)) { - EventRecurrenceRulePtr rrule( new EventRecurrenceRule() ); - event->setRecurrenceRule(rrule); + event->getRecurrenceRule()->setFrequency(EventRecurrenceRule::NO_RECURRENCE); } else if (!JSValueIsObjectOfClass(context, value, JSCalendarRecurrenceRule::getClassRef())) { ThrowMsg(ConversionException, "Wrong parameter type."); } else { diff --git a/src/Calendar/JSCalendarRecurrenceRule.cpp b/src/Calendar/JSCalendarRecurrenceRule.cpp index 592691c..61e5a48 100755 --- a/src/Calendar/JSCalendarRecurrenceRule.cpp +++ b/src/Calendar/JSCalendarRecurrenceRule.cpp @@ -310,7 +310,7 @@ bool JSCalendarRecurrenceRule::setProperty(JSContextRef context, rrule->setFrequency(converter.toRecurrenceFrequency(frequency)); return true; } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) { - unsigned short interval = (unsigned short)(converter.toULong(value)); + unsigned short interval = (unsigned short)(converter.toLong(value)); rrule->setInterval(interval); return true; } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) { diff --git a/src/Download/DownloadManager.cpp b/src/Download/DownloadManager.cpp index 414b2ee..376dfda 100644 --- a/src/Download/DownloadManager.cpp +++ b/src/Download/DownloadManager.cpp @@ -317,7 +317,7 @@ void DownloadManager::removeCallbackFromMap(long downloadId) { } } -long DownloadManager::start(DownloadRequest *request, DownloadCallback *downloadCallback) +long DownloadManager::start(DownloadRequest *request, DownloadCallback *downloadCallback, std::string& userAgent) { int ret; int downloadId = 0; @@ -332,6 +332,14 @@ long DownloadManager::start(DownloadRequest *request, DownloadCallback *download std::string networkType = request->getNetworkType(); std::map httpHeader = request->getHttpHeader(); + std::map webAppHttpHeader = getWebAppHttpHeader(); + + // initialize webAppHttpHeader + if (!userAgent.empty()) { + std::pair userAgentPair("User-Agent", userAgent); + webAppHttpHeader.insert(userAgentPair); + } + if (url.empty()) { throw InvalidValuesException("Invalid DownloadRequest.url."); } @@ -389,6 +397,15 @@ long DownloadManager::start(DownloadRequest *request, DownloadCallback *download } } + // set httpHeaders with wrt's HTTPHeaders. + // Values given by user prior to the wrt's. + for(std::map::const_iterator iter = webAppHttpHeader.begin(); iter != webAppHttpHeader.end(); ++iter) { + if(httpHeader.find(iter->first) == httpHeader.end()) { + std::pair headerPair(iter->first, iter->second); + httpHeader.insert(headerPair); + } + } + // set httpHeaders std::map::const_iterator iter; for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) { @@ -683,5 +700,14 @@ void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCal setCallbackToMap(downloadId, downloadCallback); } +std::map DownloadManager::getWebAppHttpHeader() +{ + std::map webAppHttpHeader; + + // TODO get HTTPHeaders from WRT + + return webAppHttpHeader; +} + } // Download } // DeviceAPI diff --git a/src/Download/DownloadManager.h b/src/Download/DownloadManager.h index 5fd77d3..6df2774 100644 --- a/src/Download/DownloadManager.h +++ b/src/Download/DownloadManager.h @@ -33,7 +33,7 @@ public: DownloadManager(); virtual ~DownloadManager(); - long start(DownloadRequest *request, DownloadCallback *downloadCallback); + long start(DownloadRequest *request, DownloadCallback *downloadCallback, std::string& userAgent); void cancel(long downloadId); void pause(long downloadId); void resume(long downloadId); @@ -50,6 +50,8 @@ public: private: typedef std::map DownloadCallbackMapT; DownloadCallbackMapT mDownloadCallbacks; + + std::map getWebAppHttpHeader(); }; } diff --git a/src/Download/JSDownloadManager.cpp b/src/Download/JSDownloadManager.cpp index 85fba3e..c1e8022 100644 --- a/src/Download/JSDownloadManager.cpp +++ b/src/Download/JSDownloadManager.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "plugin_config.h" @@ -38,6 +39,49 @@ using namespace DeviceAPI::Common; namespace DeviceAPI { namespace Download { +namespace { + std::string getUserAgent(JSContextRef context) + { + std::string userAgent; + + JSValueRef jsException = NULL; + JSObjectRef jsObjWindow = JSContextGetGlobalObject(context); + JSStringRefWrapper jsStrWrpNavigator("navigator"); + JSValueRef jsValNavigator = JSObjectGetProperty(context, jsObjWindow, jsStrWrpNavigator.get(), &jsException); + if(jsException != NULL) { + LoggerW("Fail to get userAgent while getting navigator value."); + return userAgent; + } + + JSObjectRef jsObjNavigator = JSValueToObject(context, jsValNavigator, &jsException); + if(jsException != NULL) { + LoggerW("Fail to get userAgent while getting navigator object."); + return userAgent; + } + + JSStringRefWrapper jsStrWrtUserAgent("userAgent"); + JSValueRef jsValUserAgent = JSObjectGetProperty(context, jsObjNavigator, jsStrWrtUserAgent.get(), &jsException); + if(jsException != NULL) { + LoggerW("Fail to get userAgent while getting userAgent value."); + return userAgent; + } + + JSStringRef jsStrUserAgent = JSValueToStringCopy(context, jsValUserAgent, &jsException); + if(jsException != NULL) { + LoggerW("Fail to get userAgent while getting userAgent string."); + return userAgent; + } + + size_t size = JSStringGetLength(jsStrUserAgent); + char cstrUserAgent[size*3]; + JSStringGetUTF8CString(jsStrUserAgent, cstrUserAgent, size*3); + + userAgent = cstrUserAgent; + + return userAgent; + } +} + JSClassDefinition JSDownloadManager::m_classInfo = { 0, kJSClassAttributeNone, @@ -143,8 +187,10 @@ JSValueRef JSDownloadManager::startDownload(JSContextRef context, downloadCallback = new DownloadCallback(GlobalContextManager::getInstance()->getGlobalContext(context), downloadCallbackObj); } - // perform - long downloadId = downloadManager->start(downloadRequest, downloadCallback); + // getting userAgent from navigator.userAgent (to be replaced by other method) + std::string userAgent = getUserAgent(context); + + long downloadId = downloadManager->start(downloadRequest, downloadCallback, userAgent); TIME_TRACER_ITEM_END(__FUNCTION__, 0); return JSUtil::toJSValueRef(context, downloadId); } catch (const BasePlatformException &err) { diff --git a/src/NFC/CMakeLists.txt b/src/NFC/CMakeLists.txt index eea7f19..64c3a72 100755 --- a/src/NFC/CMakeLists.txt +++ b/src/NFC/CMakeLists.txt @@ -41,6 +41,9 @@ SET(SRCS_IMPL NFCTag.cpp NdefMessage.cpp NdefRecord.cpp + NdefRecordText.cpp + NdefRecordURI.cpp + NdefRecordMedia.cpp NFCTarget.cpp NFCUtil.cpp ) diff --git a/src/NFC/EventTagAction.h b/src/NFC/EventTagAction.h index 97295e5..bab0153 100755 --- a/src/NFC/EventTagAction.h +++ b/src/NFC/EventTagAction.h @@ -22,6 +22,7 @@ #include #include "EventNFC.h" #include +#include namespace DeviceAPI { namespace NFC { @@ -34,7 +35,7 @@ protected: void setNdefMessageHandle(void *handle) { messageHandle = handle; } void *getNdefMessageHandle() { return messageHandle; } public: - EventTagAction() { } + EventTagAction() : messageHandle(NULL) { } }; class EventTagActionRead : public EventNFCTemplate @@ -51,6 +52,10 @@ private: class EventTagActionWrite : public EventTagAction { public: + ~EventTagActionWrite() { + if (messageHandle) + nfc_ndef_message_destroy((nfc_ndef_message_h)messageHandle); + } void writeNdef(void *handle) {setNdefMessageHandle(handle);} void *getNdefForWriting() {return getNdefMessageHandle();} EventTagActionWrite(){ } diff --git a/src/NFC/EventTargetAction.h b/src/NFC/EventTargetAction.h index b5d0b34..38dfed5 100755 --- a/src/NFC/EventTargetAction.h +++ b/src/NFC/EventTargetAction.h @@ -24,6 +24,7 @@ #include #include #include "EventNFC.h" +#include namespace DeviceAPI { namespace NFC { @@ -43,6 +44,10 @@ private: class EventTargetActionSend : public EventNFCTemplate { public: + ~EventTargetActionSend() { + if (message) + nfc_ndef_message_destroy((nfc_ndef_message_h)message); + } void *getMessageForSending() {return message;} EventTargetActionSend(void *handle) :message(handle){ } private: diff --git a/src/NFC/INdefMessage.h b/src/NFC/INdefMessage.h index 1563c25..e607e01 100755 --- a/src/NFC/INdefMessage.h +++ b/src/NFC/INdefMessage.h @@ -22,6 +22,7 @@ #include #include +#include #include "NdefRecordProperties.h" namespace DeviceAPI { @@ -33,13 +34,12 @@ class INdefMessage INdefMessage() {} virtual ~INdefMessage() {} - virtual std::vector toByte()= 0; - virtual long getRecordCount() = 0; - virtual NdefRecordData getNDEFRecord(const long index) = 0; - virtual void *makeMessage(std::vector &ndefRcords) = 0; - virtual void *makeMessage(const std::vector &rawdata) = 0; - virtual void setRecordesPtr(void *records) = 0; - virtual void* getRecordesPtr() = 0; + virtual std::vector toByte(std::vector &ndefRcords)= 0; + virtual void *makeNdefMessageHandle(std::vector &ndefRcords) = 0; + virtual std::vector toNDEFRecords(const std::vector &rawdata) = 0; + virtual std::vector toNDEFRecords(void *message) = 0; + + Common::PropertyBag mLocalProperty; }; typedef DPL::SharedPtr INdefMessagePtr; diff --git a/src/NFC/INdefRecord.h b/src/NFC/INdefRecord.h index 5d93e39..4c93269 100755 --- a/src/NFC/INdefRecord.h +++ b/src/NFC/INdefRecord.h @@ -20,31 +20,27 @@ #ifndef _ABSTRACT_LAYER_INDEFRECORD_H_ #define _ABSTRACT_LAYER_INDEFRECORD_H_ -#include #include +#include #include "NdefRecordProperties.h" namespace DeviceAPI { namespace NFC { +using namespace WrtDeviceApis::Commons; class INdefRecord { public: INdefRecord() {} virtual ~INdefRecord() {} - virtual void *getHandle() = 0; - virtual NdefRecordProperties getNDEFRecordProperties() = 0; - virtual nfcTNF getTNF() = 0; - virtual std::vector getTypeName() = 0; - virtual std::vector getID() = 0; - virtual std::vector getPayload() = 0; - virtual bool getText(char **text) = 0; - virtual bool getLangCode(char **langCode) = 0; - virtual bool getEncodeType(nfcTextEncodeUTF *encodeType) = 0; - virtual bool getUri(char **uri) = 0; - virtual bool getMimeType(char **mimeType) = 0; - protected: + virtual NdefRecordData getNDEFRecordData() = 0; + virtual void *getHandle() = 0; + virtual std::string getText() {ThrowMsg(NotFoundException, "Not Found text attribute");} + virtual std::string getLanguageCode() {ThrowMsg(NotFoundException, "Not Found languageCode attribute");} + virtual nfcTextEncodeUTF getEncoding() {ThrowMsg(NotFoundException, "Not Found encoding attribute");} + virtual std::string getUri( ) {ThrowMsg(NotFoundException, "Not Found uri attribute");} + virtual std::string getMimeType() {ThrowMsg(NotFoundException, "Not Found mimeType attribute");} }; diff --git a/src/NFC/JSNFCTag.cpp b/src/NFC/JSNFCTag.cpp index c96c8e4..62dc324 100755 --- a/src/NFC/JSNFCTag.cpp +++ b/src/NFC/JSNFCTag.cpp @@ -292,8 +292,26 @@ JSValueRef JSNFCTag::writeNDEF(JSContextRef context, callbackManager->setOnError(arguments[2]); EventTagActionWritePtr event(new EventTagActionWrite()); - NFCConverter convert(context); - void *messageHandle = convert.copiedMessage(ndefMessageObj); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context); + NFCConverter convert(global_context); + void *messageHandle = NULL; + try { + NdefMessagePrivObject* ndefMessagePrivateObj = static_cast(JSObjectGetPrivate(ndefMessageObj)); + if (!ndefMessagePrivateObj) { + LogError("NDEF Message Private object is not set."); + ThrowMsg(ConversionException, "Private object is not set"); + } + INdefMessagePtr ndefMessage(ndefMessagePrivateObj->getObject()); + JSValueRef recordsValue = (ndefMessage->mLocalProperty).getProperty(global_context, TIZEN_NDEFMESSAGE_RECORDS); + std::vector records = convert.toVectorOfRecordHandles(recordsValue); + messageHandle = ndefMessage->makeNdefMessageHandle(records); + } catch (WrtDeviceApis::Commons::Exception& err) { + LoggerE(err.GetClassName() << ":"<writeNdef(messageHandle); event->setPrivateData( DPL::StaticPointerCast(callbackManager)); event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance()); @@ -312,7 +330,7 @@ JSValueRef JSNFCTag::writeNDEF(JSContextRef context, return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, "Not Support NDEF"); } Catch (InvalidArgumentException) { LoggerE("writeNDEF InvalidArgumentException"); - callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values")); + callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage())); return JSValueMakeUndefined(context); } Catch (PlatformException) { LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); @@ -364,10 +382,10 @@ JSValueRef JSNFCTag::transceive(JSContextRef context, callbackManager->setOnError(arguments[2]); EventTagActionTransceivePtr event(new EventTagActionTransceive()); - Converter convert(context); + NFCConverter convert(context); std::vector data; if (dataObj) - data= convert.toVectorOfUChars(arguments[0]); + data= convert.toVectorOfOctets(arguments[0]); event->transceive(data); event->setPrivateData( DPL::StaticPointerCast(callbackManager) ); event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance()); diff --git a/src/NFC/JSNFCTarget.cpp b/src/NFC/JSNFCTarget.cpp index 01ba2a7..c1eaa88 100755 --- a/src/NFC/JSNFCTarget.cpp +++ b/src/NFC/JSNFCTarget.cpp @@ -308,7 +308,26 @@ JSValueRef JSNFCTarget::sendNDEF(JSContextRef context, if (validator.toFunction(2, true)) callbackManager->setOnError(arguments[2]); - void *messageHandle = convert.copiedMessage(ndefMessageObj); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context); + NFCConverter convert(global_context); + void *messageHandle = NULL; + try { + NdefMessagePrivObject* ndefMessagePrivateObj = static_cast(JSObjectGetPrivate(ndefMessageObj)); + if (!ndefMessagePrivateObj) { + LogError("NDEF Message Private object is not set."); + ThrowMsg(ConversionException, "Private object is not set"); + } + INdefMessagePtr ndefMessage(ndefMessagePrivateObj->getObject()); + JSValueRef recordsValue = (ndefMessage->mLocalProperty).getProperty(global_context, TIZEN_NDEFMESSAGE_RECORDS); + std::vector records = convert.toVectorOfRecordHandles(recordsValue); + messageHandle = ndefMessage->makeNdefMessageHandle(records); + } catch (WrtDeviceApis::Commons::Exception& err) { + LoggerE(err.GetClassName() << ":"<setObject(thisObject); event->setPrivateData( DPL::StaticPointerCast(callbackManager)); @@ -324,7 +343,7 @@ JSValueRef JSNFCTarget::sendNDEF(JSContextRef context, return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); } Catch (InvalidArgumentException) { LoggerE("sendNDEF InvalidArgumentException"); - callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values")); + callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage())); return JSValueMakeUndefined(context); } Catch (PlatformException) { LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); diff --git a/src/NFC/JSNdefMessage.cpp b/src/NFC/JSNdefMessage.cpp old mode 100644 new mode 100755 index a5e978e..4553d9e --- a/src/NFC/JSNdefMessage.cpp +++ b/src/NFC/JSNdefMessage.cpp @@ -38,10 +38,6 @@ using namespace DeviceAPI::Common; using namespace WrtDeviceApis::Commons; using namespace WrtDeviceApis::CommonsJavaScript; -#define TIZEN_NDEFMESSAGE_ATTRIBUTENAME "NDEFMessage" -#define TIZEN_NDEFMESSAGE_RECORDCOUNT "recordCount" -#define TIZEN_NDEFMESSAGE_RECORDS "records" - namespace DeviceAPI { namespace NFC { @@ -57,7 +53,7 @@ namespace NFC { finalize, NULL, //hasProperty, NULL, - setProperty, //setProperty, + NULL, //setProperty, NULL, //DeleteProperty, NULL, //GetPropertyNames, NULL, //CallAsFunction, @@ -86,46 +82,57 @@ JSClassRef JSNdefMessage::m_jsClassRef = JSClassCreate(JSNdefMessage::getClassIn JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, void *messageHandle) { LoggerD("entered"); - INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(messageHandle); - return createJSObject(context, ndefMessage); + INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(); + std::vector records = ndefMessage->toNDEFRecords(messageHandle); + JSValueRef recordArray = NULL; + if (records.size() > 0) { + JSValueRef valueArray[records.size()]; + NFCConverter convert(context); + for (std::size_t i = 0; i < records.size(); ++i) { + valueArray[i] = convert.toJSValueRef(records[i]); + } + recordArray = JSCreateArrayObject(context, records.size(), valueArray); + } else + recordArray = JSCreateArrayObject(context, 0, NULL); + return createJSObject(context, ndefMessage, recordArray); } -JSObjectRef JSNdefMessage::createJSObject(JSContextRef context) { +/* +JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector ndefRcords) { LoggerD("entered"); - INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(); + INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords); return createJSObject(context, ndefMessage); -} +}*/ -JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, INdefMessagePtr message) { +JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector rawdata) { LoggerD("entered"); + INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(); NFCConverter convert(context); - JSValueRef records = convert.toJSNdefRecordArray(message); - JSValueProtect(context, records); - message->setRecordesPtr((void*)records); - NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, message); - - if (!priv) { - ThrowMsg(NullPointerException, "Can not new a NdefMessage object"); - } - - JSObjectRef obj = JSObjectMake(context, getClassRef(), priv); - return obj; + std::vector records = ndefMessage->toNDEFRecords(rawdata); + JSValueRef recordArray = NULL; + if (records.size() > 0) { + JSValueRef valueArray[records.size()]; + NFCConverter convert(context); + for (std::size_t i = 0; i < records.size(); ++i) { + valueArray[i] = convert.toJSValueRef(records[i]); + } + recordArray = JSCreateArrayObject(context, records.size(), valueArray); + } else + recordArray = JSCreateArrayObject(context, 0, NULL); + return createJSObject(context, ndefMessage, recordArray); } -JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector ndefRcords) { +JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, INdefMessagePtr message, JSValueRef recordArray) { LoggerD("entered"); - INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords); - return createJSObject(context, ndefMessage); -} - -JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector rawdata) { - LoggerD("entered"); + if ((message->mLocalProperty).setArrayProperty(context, TIZEN_NDEFMESSAGE_RECORDS, recordArray) == false) + LoggerE("Can't set property"); - INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(rawdata); - return createJSObject(context, ndefMessage); + NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, message); + JSObjectRef obj = JSObjectMake(context, getClassRef(), priv); + return obj; } void JSNdefMessage::initialize(JSContextRef context, JSObjectRef object) { @@ -138,8 +145,6 @@ void JSNdefMessage::finalize(JSObjectRef object) NdefMessagePrivObject *priv = static_cast(JSObjectGetPrivate(object)); if (priv) { INdefMessagePtr ndefMessage(priv->getObject()); - LoggerD("getRecordesPtr:"<getRecordesPtr()); - JSValueUnprotect(priv->getContext(), (JSValueRef)(ndefMessage->getRecordesPtr())); JSObjectSetPrivate(object, NULL); LoggerD("Deleting NdefMessage object"); delete priv; @@ -164,23 +169,29 @@ JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor { LoggerD("entered"); - Try { - ArgumentValidator validator(ctx, argumentCount, arguments); - JSObjectRef objArg = validator.toArrayObject(0, true); - JSObjectRef result = NULL; - JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); - - if (objArg == NULL) - result = createJSObject(global_context); - else { - NFCConverter convert(ctx); + ArgumentValidator validator(ctx, argumentCount, arguments); + JSObjectRef objArg = NULL; + try { + if (argumentCount > 0) + objArg = validator.toArrayObject(0); + } catch(...) { + LoggerE("First parameter is not an array"); + } + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); - if (JSGetArrayLength(ctx, objArg) > 0) { + JSObjectRef result = NULL; + Try { + if (objArg != NULL) { + NFCConverter convert(global_context); + unsigned int arrayLen = JSGetArrayLength(ctx, objArg); + if (arrayLen > 0) { bool isRecordArray = false; bool isByteArray = false; - for (std::size_t i = 0; i < JSGetArrayLength(ctx, objArg); ++i) { - JSValueRef element = JSGetArrayElement(ctx, objArg, i); - bool bNdefRecord = convert.isNdefRecord(element); + JSValueRef valueArray[arrayLen]; + + for (unsigned int i = 0; i < arrayLen; ++i) { + valueArray[i] = JSGetArrayElement(ctx, objArg, i); + bool bNdefRecord = convert.isNdefRecord(valueArray[i]); LoggerD("isNdefRecord : " << bNdefRecord); if (bNdefRecord) isRecordArray = true; @@ -189,38 +200,32 @@ JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor if (isRecordArray && isByteArray) break; } - if (isRecordArray && isByteArray) - ThrowMsg(ConversionException, "Parameter's type is not matched"); - else if (isRecordArray) - result = createJSObject(global_context, convert.toVectorOfRecordHandles(objArg)); - else - result = createJSObject(global_context, convert.toVectorOfUChars(arguments[0])); - - } else - result = createJSObject(global_context); + if (!isRecordArray && isByteArray) { + result = createJSObject(global_context, convert.toVectorOfOctets(arguments[0])); + } else { + INdefMessagePtr ndefMessage = NFCFactory::getInstance().createNDEFMessageObject(); + JSValueRef recordArray = JSCreateArrayObject(global_context, arrayLen, valueArray); + result = createJSObject(global_context, ndefMessage, recordArray); + } + } } - if (result) { - JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); - JSObjectSetProperty(global_context, result, ctorName, constructor, - kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); - JSStringRelease(ctorName); - return result; - } else - throw TypeMismatchException("Parameter Type Mismatch"); - } Catch(BasePlatformException){ - return JSWebAPIErrorFactory::postException(ctx, exception, _rethrown_exception); - } Catch(ConversionException) { - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } Catch (InvalidArgumentException) { - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"); - } Catch (PlatformException) { - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); - } Catch (WrtDeviceApis::Commons::UnknownException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - } Catch (WrtDeviceApis::Commons::Exception) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); + } Catch(WrtDeviceApis::Commons::Exception) { + LoggerE(_rethrown_exception.GetClassName() << ": " << _rethrown_exception.GetMessage()); + } Catch(BasePlatformException) { + LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage()); } - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); + + if (result == NULL) + result = createJSObject(global_context); + + if (result) { + JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); + JSObjectSetProperty(global_context, result, ctorName, constructor, + kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); + JSStringRelease(ctorName); + } + return result; + } JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object, @@ -236,14 +241,37 @@ JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object, } INdefMessagePtr ndefMessage(privateObject->getObject()); - NFCConverter convert(context); + NFCConverter convert(context); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context); + JSValueRef recordsValue = (ndefMessage->mLocalProperty).getProperty(global_context, TIZEN_NDEFMESSAGE_RECORDS); + LoggerD("propertyName : " << convert.toString(propertyName)); if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFMESSAGE_RECORDCOUNT)) { - std::vector records = convert.toVectorOfRecordHandles(static_cast(ndefMessage->getRecordesPtr())); - return convert.toJSValueRefLong(records.size()); + if (JSValueIsNull(global_context, recordsValue) || JSValueIsUndefined(global_context, recordsValue)) { + ThrowMsg(ConversionException, + "NdefRecordArray is JS null or JS undefined."); + } + + if (!JSIsArrayValue(global_context, recordsValue)) { + ThrowMsg(ConversionException, "Argument is not an JS array."); + } + + JSObjectRef obj = convert.toJSObjectRef(recordsValue); + + if (!obj) { + LoggerE("Object is null"); + ThrowMsg(ConversionException, "Object is null"); + } + long count = 0; + for (std::size_t i = 0; i < JSGetArrayLength(global_context, obj); ++i) { + JSValueRef element = JSGetArrayElement(global_context, obj, i); + if (convert.isNdefRecord(element)) + count++; + } + return convert.toJSValueRefLong(count); } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFMESSAGE_RECORDS)) { - return static_cast(ndefMessage->getRecordesPtr()); + return recordsValue; } } Catch (ConversionException) { LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); @@ -274,17 +302,27 @@ bool JSNdefMessage::setProperty(JSContextRef context, JSObjectRef object, NFCConverter convert(context); - if (JSValueIsNull(context, value) || JSValueIsUndefined(context, value) || !JSIsArrayValue(context, value)) { LoggerE("value is invald."); ThrowMsg(ConversionException, "value is invald."); } INdefMessagePtr ndefMessage(privateObject->getObject()); - JSValueUnprotect(privateObject->getContext(), (JSValueRef)ndefMessage->getRecordesPtr()); - JSValueProtect(privateObject->getContext(), value); - ndefMessage->setRecordesPtr((void *)value); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context); - return true; + JSObjectRef obj = convert.toJSObjectRef(value); + + if (!obj) { + LoggerE("Object is null"); + ThrowMsg(ConversionException, "Object is null"); + } + unsigned int arrayLen = JSGetArrayLength(global_context, obj); + JSValueRef valueArray[arrayLen]; + for (std::size_t i = 0; i < arrayLen; ++i) { + JSValueRef element = JSGetArrayElement(global_context, obj, i); + valueArray[i] = element; + } + JSValueRef recordsArray = JSCreateArrayObject(global_context, arrayLen, valueArray); + return (ndefMessage->mLocalProperty).setArrayProperty(global_context, TIZEN_NDEFMESSAGE_RECORDS, recordsArray); } } Catch (NullPointerException) { LoggerE("NullPointerException: " << _rethrown_exception.GetMessage()); @@ -321,22 +359,31 @@ JSValueRef JSNdefMessage::toByte(JSContextRef context, INdefMessagePtr ndefMessage(privateObject->getObject()); NFCConverter convert(context); - std::vector records = convert.toVectorOfRecordHandles(static_cast(ndefMessage->getRecordesPtr())); - INdefMessagePtr newMessage = NFCFactory::getInstance().createNDEFMessageObject(records); - return convert.toJSValueRef(newMessage->toByte()); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context); + std::vector records; + try { + JSValueRef recordsValue = (ndefMessage->mLocalProperty).getProperty(global_context, TIZEN_NDEFMESSAGE_RECORDS); + records = convert.toVectorOfRecordHandles(recordsValue); + } catch (WrtDeviceApis::Commons::Exception& err) { + LoggerE(err.GetClassName() << ":"<toByte(records)); } Catch (ConversionException) { LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } Catch (WrtDeviceApis::Commons::UnknownException) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); - } Catch (PlatformException) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); } Catch(NullPointerException) { LoggerE("Exception: " << _rethrown_exception.GetMessage()); return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); + } Catch(InvalidArgumentException) { + LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage()); } Catch (WrtDeviceApis::Commons::Exception) { LoggerE("Exception: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage()); } return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); diff --git a/src/NFC/JSNdefMessage.h b/src/NFC/JSNdefMessage.h index 6080af1..5062d60 100755 --- a/src/NFC/JSNdefMessage.h +++ b/src/NFC/JSNdefMessage.h @@ -24,6 +24,10 @@ #include #include "INdefMessage.h" +#define TIZEN_NDEFMESSAGE_ATTRIBUTENAME "NDEFMessage" +#define TIZEN_NDEFMESSAGE_RECORDCOUNT "recordCount" +#define TIZEN_NDEFMESSAGE_RECORDS "records" + namespace DeviceAPI { namespace NFC { @@ -37,10 +41,8 @@ public: static const JSClassRef getClassRef(); - static JSObjectRef createJSObject(JSContextRef context); - static JSObjectRef createJSObject(JSContextRef context, void *messageHandle); - static JSObjectRef createJSObject(JSContextRef context, INdefMessagePtr message); - static JSObjectRef createJSObject(JSContextRef context, std::vector ndefRcords); + static JSObjectRef createJSObject(JSContextRef context, void *messageHandle = NULL); + static JSObjectRef createJSObject(JSContextRef context, INdefMessagePtr message, JSValueRef recordArray); static JSObjectRef createJSObject(JSContextRef context, std::vector rawdata); static JSObjectRef constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); diff --git a/src/NFC/JSNdefRecord.cpp b/src/NFC/JSNdefRecord.cpp old mode 100644 new mode 100755 index df1f4fd..2608aea --- a/src/NFC/JSNdefRecord.cpp +++ b/src/NFC/JSNdefRecord.cpp @@ -87,13 +87,15 @@ const JSClassDefinition* JSNdefRecord::getClassInfo() { JSClassRef JSNdefRecord::m_jsClassRef = JSClassCreate(JSNdefRecord::getClassInfo()); -JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordData &ndefRecordData) { + +JSObjectRef JSNdefRecord::createJSObject(JSContextRef context) { LoggerD("Entered"); - return createJSObject(context, ndefRecordData.properties, ndefRecordData.payload); + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject()); + } -JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector payload) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload)); +JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector payload, bool isValid) { + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload, isValid)); } JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, std::vector rawdata) { @@ -131,52 +133,57 @@ JSObjectRef JSNdefRecord::constructor(JSContextRef ctx, JSObjectRef constructor, { LoggerD("entered"); + JSObjectRef result = NULL; + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); Try { ArgumentValidator validator(ctx, argumentCount, arguments); NFCConverter convert(ctx); - JSObjectRef result = NULL; - JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); - if (argumentCount < 3) { - if (validator.toArrayObject(0)) - result = createJSObject(global_context, convert.toVectorOfUChars(arguments[0])); + if (argumentCount == 0) { + throw TypeMismatchException("Argument count is 0."); + } else if (argumentCount < 3) { + if (!JSIsArrayValue(ctx, arguments[0])) + throw TypeMismatchException("Raw data is not array."); + else + result = createJSObject(global_context, convert.toVectorOfOctets(arguments[0])); } else { NdefRecordProperties prop; - prop.tnf = static_cast(validator.toNumber(0)); - if (validator.toArrayObject(1)) - prop.typeName = convert.toVectorOfUChars(arguments[1]); + bool isValid = true; + prop.tnf = static_cast(validator.toNumber(0)); + if (!JSIsArrayValue(ctx, arguments[1])) + isValid = false; + else + prop.typeName = convert.toVectorOfOctets(arguments[1]); std::vector payload; - if (validator.toArrayObject(2)) - payload = convert.toVectorOfUChars(arguments[2]); - if (validator.toArrayObject(3, true)) - prop.id = convert.toVectorOfUChars(arguments[3]); - result = createJSObject(global_context, prop, payload); + if (!JSIsArrayValue(ctx, arguments[2])) + isValid = false; + else + payload = convert.toVectorOfOctets(arguments[2]); + if ((argumentCount > 3) && JSIsArrayValue(ctx, arguments[3])) + prop.id = convert.toVectorOfOctets(arguments[3]); + result = createJSObject(global_context, prop, payload, isValid); } - if (result) { - JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); - JSObjectSetProperty(global_context, result, ctorName, constructor, - kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); - JSStringRelease(ctorName); - return result; - } else - throw TypeMismatchException("Parameter Type Mismatch"); } Catch (BasePlatformException) { - return JSWebAPIErrorFactory::postException(ctx, exception, _rethrown_exception); + LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage()); } Catch(ConversionException) { LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); } Catch (InvalidArgumentException) { LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"); } Catch (PlatformException) { LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); } Catch (WrtDeviceApis::Commons::UnknownException) { LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); } Catch (WrtDeviceApis::Commons::Exception) { LoggerE("Exception: " << _rethrown_exception.GetMessage()); } - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); + if (result == NULL) + result = createJSObject(global_context); + + JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); + JSObjectSetProperty(global_context, result, ctorName, constructor, + kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); + JSStringRelease(ctorName); + return result; } JSValueRef JSNdefRecord::getProperty(JSContextRef context, JSObjectRef object, @@ -190,16 +197,16 @@ JSValueRef JSNdefRecord::getProperty(JSContextRef context, JSObjectRef object, ThrowMsg(NullPointerException, "Private object not set."); } INdefRecordPtr ndefRecord = priv->getObject(); - + NdefRecordData recordData = ndefRecord->getNDEFRecordData(); LoggerD("propertyName : " << convert.toString(propertyName)); if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_TNF)) - return convert.toJSValueRef(ndefRecord->getTNF()); + return convert.toJSValueRef(recordData.properties.tnf); else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_TYPE)) - return convert.toJSValueRef(ndefRecord->getTypeName()); + return convert.toJSValueRef(recordData.properties.typeName); else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_ID)) - return convert.toJSValueRef(ndefRecord->getID()); + return convert.toJSValueRef(recordData.properties.id); else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_PAYLOAD)) - return convert.toJSValueRef(ndefRecord->getPayload()); + return convert.toJSValueRef(recordData.payload); } Catch (ConversionException) { LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); } Catch (WrtDeviceApis::Commons::UnknownException) { diff --git a/src/NFC/JSNdefRecord.h b/src/NFC/JSNdefRecord.h index 1397366..a78a47b 100755 --- a/src/NFC/JSNdefRecord.h +++ b/src/NFC/JSNdefRecord.h @@ -36,10 +36,9 @@ public: static const JSClassRef getClassRef(); + static JSObjectRef createJSObject(JSContextRef context); static JSObjectRef createJSObject(JSContextRef context, - const NdefRecordData &ndefRecordData); - static JSObjectRef createJSObject(JSContextRef context, - const NdefRecordProperties &ndefRecordProperties, std::vector payload); + const NdefRecordProperties &ndefRecordProperties, std::vector payload, bool isValid = true); static JSObjectRef createJSObject(JSContextRef context, std::vector rawdata); static JSObjectRef createJSObject(JSContextRef context, INdefRecordPtr ndefRecord); diff --git a/src/NFC/JSNdefRecordMedia.cpp b/src/NFC/JSNdefRecordMedia.cpp old mode 100644 new mode 100755 index 01a2241..6bfcc5b --- a/src/NFC/JSNdefRecordMedia.cpp +++ b/src/NFC/JSNdefRecordMedia.cpp @@ -80,20 +80,16 @@ const JSClassDefinition* JSNdefRecordMedia::getClassInfo() { JSClassRef JSNdefRecordMedia::m_jsClassRef = JSClassCreate(JSNdefRecordMedia::getClassInfo()); JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector payload) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload)); + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordMediaObject(ndefRecordProperties, payload)); } -JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const std::string &mimeType, std::vector data) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(mimeType, data)); +JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const std::string &mimeType, std::vector data, bool isValid) { + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordMediaObject(mimeType, data, isValid)); } JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) { NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord); - if (!priv) { - ThrowMsg(NullPointerException, "Can not new a NdefRecord object"); - } - JSObjectRef obj = JSObjectMake(context, getClassRef(), priv); return obj; } @@ -110,40 +106,37 @@ JSObjectRef JSNdefRecordMedia::constructor(JSContextRef ctx, JSObjectRef constru { LoggerD("entered"); - Try { - ArgumentValidator validator(ctx, argumentCount, arguments); - std::string mimeType = validator.toString(0); - std::vector data; - JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); + std::vector data; + std::string mimeType = ""; + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); + bool isValid = true; + + ArgumentValidator validator(ctx, argumentCount, arguments); + try { + mimeType = validator.toString(0); + } catch (...) { + isValid = false; + LoggerE("Can't convert mimeType"); + } - Converter convert(ctx); + try { + NFCConverter convert(ctx); if (validator.toArrayObject(1)) - data = convert.toVectorOfUChars(arguments[1]); - - JSObjectRef result = createJSObject(global_context, mimeType, data); - - JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); - JSObjectSetProperty(global_context, result, ctorName, constructor, - kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); - JSStringRelease(ctorName); - return result; - } Catch (BasePlatformException) { - return JSWebAPIErrorFactory::postException(ctx, exception, _rethrown_exception); - } Catch(ConversionException) { - LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } Catch (InvalidArgumentException) { - LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"); - } Catch (PlatformException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); - } Catch (WrtDeviceApis::Commons::UnknownException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - } Catch (WrtDeviceApis::Commons::Exception) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); + data = convert.toVectorOfOctets(arguments[1]); + } catch (...) { + isValid = false; + LoggerE("Can't convert data"); } - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); + + + JSObjectRef result = createJSObject(global_context, mimeType, data, isValid); + + JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); + JSObjectSetProperty(global_context, result, ctorName, constructor, + kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); + JSStringRelease(ctorName); + return result; + } JSValueRef JSNdefRecordMedia::getProperty(JSContextRef context, JSObjectRef object, @@ -158,14 +151,7 @@ JSValueRef JSNdefRecordMedia::getProperty(JSContextRef context, JSObjectRef obje ThrowMsg(NullPointerException, "Private object not set."); } INdefRecordPtr ndefRecord = priv->getObject(); - char * mimeType = NULL; - if (ndefRecord->getMimeType(&mimeType)) { - std::string result(mimeType); - free(mimeType); - LoggerD("mimeType : " << result); - return convert.toJSValueRef(result); - } - LoggerD("This record is not MEDIA Type"); + return convert.toJSValueRef(ndefRecord->getMimeType()); } } Catch (ConversionException) { diff --git a/src/NFC/JSNdefRecordMedia.h b/src/NFC/JSNdefRecordMedia.h index c1f4b95..070be2c 100755 --- a/src/NFC/JSNdefRecordMedia.h +++ b/src/NFC/JSNdefRecordMedia.h @@ -36,7 +36,7 @@ public: const NdefRecordProperties &ndefRecordProperties, std::vector payload); static JSObjectRef createJSObject(JSContextRef context, - const std::string &mimeType, std::vector data); + const std::string &mimeType, std::vector data, bool isValid = true); static JSObjectRef createJSObject(JSContextRef context, INdefRecordPtr ndefRecord); static JSObjectRef constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); diff --git a/src/NFC/JSNdefRecordText.cpp b/src/NFC/JSNdefRecordText.cpp old mode 100644 new mode 100755 index eb920c7..bd78ed8 --- a/src/NFC/JSNdefRecordText.cpp +++ b/src/NFC/JSNdefRecordText.cpp @@ -85,20 +85,16 @@ const JSClassDefinition* JSNdefRecordText::getClassInfo() { JSClassRef JSNdefRecordText::m_jsClassRef = JSClassCreate(JSNdefRecordText::getClassInfo()); JSObjectRef JSNdefRecordText::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector payload) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload)); + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordTextObject(ndefRecordProperties, payload)); } -JSObjectRef JSNdefRecordText::createJSObject(JSContextRef context, const std::string &text, const std::string &langCode, const short encodeType) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(text, langCode, encodeType)); +JSObjectRef JSNdefRecordText::createJSObject(JSContextRef context, const std::string &text, const std::string &langCode, const nfcTextEncodeUTF encodeType, bool isValid) { + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordTextObject(text, langCode, encodeType, isValid)); } JSObjectRef JSNdefRecordText::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) { NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord); - if (!priv) { - ThrowMsg(NullPointerException, "Can not new a NdefRecord object"); - } - JSObjectRef obj = JSObjectMake(context, getClassRef(), priv); return obj; } @@ -115,43 +111,49 @@ JSObjectRef JSNdefRecordText::constructor(JSContextRef ctx, JSObjectRef construc { LoggerD("entered"); - Try { - ArgumentValidator validator(ctx, argumentCount, arguments); - JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); - - NFCConverter convert(ctx); - short encodeType = NFC_TEXT_ENCODE_UTF_8; - - std::string text = validator.toString(0); - std::string languageCode = validator.toString(1); - std::string encoding = validator.toString(2, true); - if (encoding != "") - encodeType = convert.toNfcTextEncodeUTF(encoding); - - JSObjectRef result = createJSObject(global_context, text, languageCode, encodeType); - JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); - JSObjectSetProperty(global_context, result, ctorName, constructor, - kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); - JSStringRelease(ctorName); - - return result; - } Catch (BasePlatformException) { - return JSWebAPIErrorFactory::postException(ctx, exception, _rethrown_exception); - } Catch(ConversionException) { - LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } Catch (InvalidArgumentException) { - LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"); - } Catch (PlatformException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); - } Catch (WrtDeviceApis::Commons::UnknownException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - } Catch (WrtDeviceApis::Commons::Exception) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); + ArgumentValidator validator(ctx, argumentCount, arguments); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); + + bool isValid = true; + + NFCConverter convert(ctx); + nfcTextEncodeUTF encodeType = NFC_TEXT_ENCODE_UTF_8; + + std::string text = ""; + std::string languageCode = ""; + std::string encoding = ""; + + try { + text = validator.toString(0); + } catch (...) { + isValid = false; + LoggerE("Can't convert text"); + } + + try { + languageCode = validator.toString(1); + } catch (...) { + isValid = false; + LoggerE("Can't convert languageCode"); + } + + try { + encoding = validator.toString(2, true); + } catch (...) { + encoding = ""; + LoggerE("Can't convert encoding"); } - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); + + if (encoding != "") + encodeType = convert.toNfcTextEncodeUTF(encoding); + + JSObjectRef result = createJSObject(global_context, text, languageCode, encodeType, isValid); + JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); + JSObjectSetProperty(global_context, result, ctorName, constructor, + kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); + JSStringRelease(ctorName); + + return result; } JSValueRef JSNdefRecordText::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { @@ -166,32 +168,13 @@ JSValueRef JSNdefRecordText::getProperty(JSContextRef context, JSObjectRef objec INdefRecordPtr ndefRecord = priv->getObject(); if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_TEXT_TEXT)) { - char * text = NULL; - if (ndefRecord->getText(&text)) { - std::string result(text); - free(text); - LoggerD("text : " << result); - return convert.toJSValueRef(result); - } - LoggerD("This record is not Text Type"); + return convert.toJSValueRef(ndefRecord->getText()); } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_TEXT_LANGCODE)) { - char * langCode = NULL; - if (ndefRecord->getLangCode(&langCode)) { - std::string result(langCode); - free(langCode); - LoggerD("langCode : " << result); - return convert.toJSValueRef(result); - } - LoggerD("This record is not Text Type"); + return convert.toJSValueRef(ndefRecord->getLanguageCode()); } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_TEXT_ENCODING)) { - nfcTextEncodeUTF encodeType = NFC_TEXT_ENCODE_UTF_16; - if (ndefRecord->getEncodeType(&encodeType)) { - LoggerD("encodeType : " << encodeType); - return convert.toJSValueRef(convert.toNfcTextEncodeUTFString(encodeType)); - } - LoggerD("This record is not Text Type"); + nfcTextEncodeUTF encodeType = ndefRecord->getEncoding(); + return convert.toJSValueRef(convert.toNfcTextEncodeUTFString(encodeType)); } - } Catch (ConversionException) { LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); } Catch (WrtDeviceApis::Commons::UnknownException) { @@ -200,6 +183,8 @@ JSValueRef JSNdefRecordText::getProperty(JSContextRef context, JSObjectRef objec LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage()); } Catch (NullPointerException) { LoggerE("NullPointerException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::NotFoundException) { + LoggerE("NotFoundException: " << _rethrown_exception.GetMessage()); } Catch (WrtDeviceApis::Commons::Exception) { LoggerE("Exception: " << _rethrown_exception.GetMessage()); } diff --git a/src/NFC/JSNdefRecordText.h b/src/NFC/JSNdefRecordText.h index 87e9181..09fdb8a 100755 --- a/src/NFC/JSNdefRecordText.h +++ b/src/NFC/JSNdefRecordText.h @@ -36,7 +36,7 @@ public: const NdefRecordProperties &ndefRecordProperties, std::vector payload); static JSObjectRef createJSObject(JSContextRef context, const std::string &text, - const std::string &langCode, const short encodeType); + const std::string &langCode, const nfcTextEncodeUTF encodeType, bool isValid = true); static JSObjectRef createJSObject(JSContextRef context, INdefRecordPtr ndefRecord); static JSObjectRef constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); diff --git a/src/NFC/JSNdefRecordURI.cpp b/src/NFC/JSNdefRecordURI.cpp old mode 100644 new mode 100755 index 285f025..052a342 --- a/src/NFC/JSNdefRecordURI.cpp +++ b/src/NFC/JSNdefRecordURI.cpp @@ -22,12 +22,12 @@ #include #include -#include #include #include #include #include "NFCFactory.h" +#include "NFCConverter.h" #include "JSNdefRecordURI.h" #include @@ -89,20 +89,16 @@ void JSNdefRecordURI::finalize(JSObjectRef object) { } JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector payload) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload)); + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordUriObject(ndefRecordProperties, payload)); } -JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const std::string &uri) { - return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(uri)); +JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const std::string &uri, bool isValid) { + return createJSObject(context, NFCFactory::getInstance().createNDEFRecordUriObject(uri, isValid)); } JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) { NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord); - if (!priv) { - ThrowMsg(NullPointerException, "Can not new a NdefRecord object"); - } - JSObjectRef obj = JSObjectMake(context, getClassRef(), priv); return obj; } @@ -111,34 +107,24 @@ JSObjectRef JSNdefRecordURI::constructor(JSContextRef ctx, JSObjectRef construct { LoggerD("entered"); - Try { - ArgumentValidator validator(ctx, argumentCount, arguments); - JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); + ArgumentValidator validator(ctx, argumentCount, arguments); + JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx); + bool isValid = true; + std::string uri = ""; - JSObjectRef result = createJSObject(global_context, validator.toString(0)); + try { + uri = validator.toString(0); + } catch (...) { + isValid = false; + LoggerE("Can't convert text"); + } + JSObjectRef result = createJSObject(global_context, uri, isValid); - JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); - JSObjectSetProperty(global_context, result, ctorName, constructor, - kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); - JSStringRelease(ctorName); + JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor"); + JSObjectSetProperty(global_context, result, ctorName, constructor, + kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL); + JSStringRelease(ctorName); return result; - } Catch (BasePlatformException) { - return JSWebAPIErrorFactory::postException(ctx, exception, _rethrown_exception); - } Catch(ConversionException) { - LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } Catch (InvalidArgumentException) { - LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"); - } Catch (PlatformException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); - } Catch (WrtDeviceApis::Commons::UnknownException) { - LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); - } Catch (WrtDeviceApis::Commons::Exception) { - LoggerE("Exception: " << _rethrown_exception.GetMessage()); - } - return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); } JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { @@ -151,14 +137,8 @@ JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object ThrowMsg(NullPointerException, "Private object not set."); } INdefRecordPtr ndefRecord = priv->getObject(); - char * uri = NULL; - if (ndefRecord->getUri(&uri)) { - Converter convert(context); - std::string result(uri); - free(uri); - return convert.toJSValueRef(result); - } - LoggerD("This record is not URI Type"); + NFCConverter convert(context); + return convert.toJSValueRef(ndefRecord->getUri()); } } Catch (ConversionException) { LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); @@ -168,6 +148,8 @@ JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); } Catch (NullPointerException) { LoggerE("NullPointerException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::NotFoundException) { + LoggerE("NotFoundException: " << _rethrown_exception.GetMessage()); } Catch (WrtDeviceApis::Commons::Exception) { LoggerE("Exception: " << _rethrown_exception.GetMessage()); } diff --git a/src/NFC/JSNdefRecordURI.h b/src/NFC/JSNdefRecordURI.h index c446607..6d233da 100755 --- a/src/NFC/JSNdefRecordURI.h +++ b/src/NFC/JSNdefRecordURI.h @@ -35,7 +35,7 @@ public: static JSObjectRef createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector payload); - static JSObjectRef createJSObject(JSContextRef context, const std::string &uri); + static JSObjectRef createJSObject(JSContextRef context, const std::string &uri, bool isValid = true); static JSObjectRef createJSObject(JSContextRef context, INdefRecordPtr ndefRecord); diff --git a/src/NFC/NFCAdapter.cpp b/src/NFC/NFCAdapter.cpp old mode 100644 new mode 100755 diff --git a/src/NFC/NFCConverter.cpp b/src/NFC/NFCConverter.cpp old mode 100644 new mode 100755 index 3df45e4..7d93350 --- a/src/NFC/NFCConverter.cpp +++ b/src/NFC/NFCConverter.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include "NFCFactory.h" #include "NFCConverter.h" #include "JSNdefRecord.h" @@ -172,36 +173,23 @@ std::string NFCConverter::toNfcTagTypeString(nfcTagType type) { } nfcTextEncodeUTF NFCConverter::toNfcTextEncodeUTF(std::string encodeString) { - if (encodeString == "UTF8") - return NFC_TEXT_ENCODE_UTF_8; - else if (encodeString == "UTF16") + if (encodeString == "UTF16") return NFC_TEXT_ENCODE_UTF_16; else - Throw(ConversionException); + return NFC_TEXT_ENCODE_UTF_8; + } std::string NFCConverter::toNfcTextEncodeUTFString(nfcTextEncodeUTF encode) { switch(encode) { - case NFC_TEXT_ENCODE_UTF_8: - return "UTF8"; - break; case NFC_TEXT_ENCODE_UTF_16: return "UTF16"; break; + case NFC_TEXT_ENCODE_UTF_8: default: - Throw(ConversionException); - } -} - -nfcTNF NFCConverter::toNfcTNF(const JSValueRef& arg) { - long value = toLong(arg); - LoggerD("toNfcTNF, value == " << value); - - if ((value < NFC_TNF_EMPTY) || (value > NFC_TNF_UNCHANGED)) { - ThrowMsg(InvalidArgumentException, "It is not a correct tnf value"); + return "UTF8"; + break; } - - return static_cast(value); } bool NFCConverter::initializeAllowedProperties() @@ -260,15 +248,8 @@ JSValueRef NFCConverter::toJSValueRef(std::vector props) { return jsResult; } -JSValueRef NFCConverter::toJSNdefRecordArray(INdefMessagePtr ndefMessage) { - long recordCount = ndefMessage->getRecordCount(); - std::vector jsResult; - - for (long i = 0; i < recordCount; i++) { - JSValueRef ndefRecord = toJSValueRef(ndefMessage->getNDEFRecord(i)); - jsResult.push_back(ndefRecord); - } - return toJSValueRef(jsResult); +std::vector NFCConverter::toVectorOfOctets(const JSValueRef& arg) { + return Common::JSUtil::JSArrayToType_(m_context, arg, Common::JSUtil::JSValueToOctet); } bool NFCConverter::isNdefRecord(const JSValueRef& arg) { @@ -308,13 +289,21 @@ std::vector NFCConverter::toVectorOfRecordHandles(const JSValueRef& arg) std::vector NFCConverter::toVectorOfRecordHandles(const JSObjectRef& obj) { std::vector result; - for (std::size_t i = 0; i < JSGetArrayLength(m_context, obj); ++i) { JSValueRef element = JSGetArrayElement(m_context, obj, i); - if (isNdefRecord(element)) - result.push_back(getRecordHandle(element)); - else - ThrowMsg(Commons::ConversionException, "JS array has items those are not NDEFRecord."); + 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() << ":"<getObject(); - - return record->getHandle(); -} - -void *NFCConverter::copiedMessage(const JSValueRef& arg) { - if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) || - (!JSValueIsObjectOfClass(m_context, arg, JSNdefMessage::getClassRef()))) { - ThrowMsg(Commons::ConversionException, - "Message is JS null or JS undefined."); - } - JSObjectRef obj = toJSObjectRef(arg); - - if (!obj) { - LoggerE("Object is null"); - ThrowMsg(Commons::ConversionException, "Object is null"); - } - return copiedMessage(obj); -} - -void *NFCConverter::copiedMessage(const JSObjectRef& obj) { - NdefMessagePrivObject* privateObject = static_cast(JSObjectGetPrivate(obj)); - if (!privateObject) { - LoggerE("Private object is not set."); - ThrowMsg(Commons::ConversionException, "Private object is not set"); - } - - INdefMessagePtr message = privateObject->getObject(); - std::vector records = toVectorOfRecordHandles(static_cast(message->getRecordesPtr())); - return message->makeMessage(records); + void *handle = record->getHandle(); + if (handle == NULL) + ThrowMsg(Commons::ConversionException, "Invalid Handle"); + return handle; } NFCChangedCallback NFCConverter::toNFCChangedCallback(const JSValueRef& arg) { diff --git a/src/NFC/NFCConverter.h b/src/NFC/NFCConverter.h index 15c9e27..15d68f2 100755 --- a/src/NFC/NFCConverter.h +++ b/src/NFC/NFCConverter.h @@ -48,18 +48,15 @@ class NFCConverter : public WrtDeviceApis::CommonsJavaScript::Converter std::string toNfcTagTypeString(nfcTagType type); nfcTextEncodeUTF toNfcTextEncodeUTF(std::string encodeString); std::string toNfcTextEncodeUTFString(nfcTextEncodeUTF encode); - nfcTNF toNfcTNF(const JSValueRef& arg); JSValueRef toJSValueRef(const std::vector& arg); JSValueRef toJSValueRef(std::vector props); JSValueRef toJSValueRef(NdefRecordData arg); bool isNdefRecord(const JSValueRef& arg); + std::vector toVectorOfOctets(const JSValueRef& arg); std::vector toVectorOfRecordHandles(const JSValueRef& arg); std::vector toVectorOfRecordHandles(const JSObjectRef& obj); std::string toRecordClassName(NdefRecordData arg); void *getRecordHandle(const JSValueRef& arg); - void *copiedMessage(const JSValueRef& arg); - void *copiedMessage(const JSObjectRef& obj); - JSValueRef toJSNdefRecordArray(INdefMessagePtr ndefMessage); NFCChangedCallback toNFCChangedCallback(const JSValueRef& arg); private: bool initializeAllowedProperties(); diff --git a/src/NFC/NFCFactory.cpp b/src/NFC/NFCFactory.cpp index 4c73f28..cc85435 100755 --- a/src/NFC/NFCFactory.cpp +++ b/src/NFC/NFCFactory.cpp @@ -21,6 +21,9 @@ #include "NFCAdapter.h" #include "NdefMessage.h" #include "NdefRecord.h" +#include "NdefRecordMedia.h" +#include "NdefRecordText.h" +#include "NdefRecordURI.h" #include "NFCTag.h" #include "NFCTarget.h" @@ -35,40 +38,40 @@ INdefMessagePtr NFCFactory::createNDEFMessageObject() { return INdefMessagePtr( new NdefMessage() ); } -INdefMessagePtr NFCFactory::createNDEFMessageObject(void *messageHandle) { - return INdefMessagePtr( new NdefMessage(messageHandle) ); +INdefRecordPtr NFCFactory::createNDEFRecordObject() { + return INdefRecordPtr( new NdefRecord() ); } -INdefMessagePtr NFCFactory::createNDEFMessageObject(std::vector ndefRcords) { - return INdefMessagePtr( new NdefMessage(ndefRcords) ); +INdefRecordPtr NFCFactory::createNDEFRecordObject(std::vector data) { + return INdefRecordPtr( new NdefRecord(data) ); } -INdefMessagePtr NFCFactory::createNDEFMessageObject(std::vector rawdata) { - return INdefMessagePtr( new NdefMessage(rawdata) ); +INdefRecordPtr NFCFactory::createNDEFRecordObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload, bool isValid) { + return INdefRecordPtr( new NdefRecord(ndefRecordProperties, payload, isValid) ); } -INdefRecordPtr NFCFactory::createNDEFRecordObject() { - return INdefRecordPtr( new NdefRecord() ); +INdefRecordPtr NFCFactory::createNDEFRecordTextObject(const std::string &text, const std::string &langCode, const nfcTextEncodeUTF encodeType, bool isValid) { + return INdefRecordPtr( new NdefRecordText(text, langCode, encodeType, isValid) ); } -INdefRecordPtr NFCFactory::createNDEFRecordObject(std::vector data) { - return INdefRecordPtr( new NdefRecord(data) ); +INdefRecordPtr NFCFactory::createNDEFRecordTextObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload) { + return INdefRecordPtr( new NdefRecordText(ndefRecordProperties, payload) ); } -INdefRecordPtr NFCFactory::createNDEFRecordObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload) { - return INdefRecordPtr( new NdefRecord(ndefRecordProperties, payload) ); +INdefRecordPtr NFCFactory::createNDEFRecordUriObject(const std::string &uri, bool isValid) { + return INdefRecordPtr( new NdefRecordUri(uri, isValid) ); } -INdefRecordPtr NFCFactory::createNDEFRecordObject(const std::string &text, const std::string &langCode, const short encodeType) { - return INdefRecordPtr( new NdefRecord(text, langCode, encodeType) ); +INdefRecordPtr NFCFactory::createNDEFRecordUriObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload) { + return INdefRecordPtr( new NdefRecordUri(ndefRecordProperties, payload) ); } -INdefRecordPtr NFCFactory::createNDEFRecordObject(const std::string &uri) { - return INdefRecordPtr( new NdefRecord(uri) ); +INdefRecordPtr NFCFactory::createNDEFRecordMediaObject(const std::string &mimeType, const std::vector data, bool isValid) { + return INdefRecordPtr( new NdefRecordMedia(mimeType, data, isValid) ); } -INdefRecordPtr NFCFactory::createNDEFRecordObject(const std::string &mimeType, const std::vector data) { - return INdefRecordPtr( new NdefRecord(mimeType, data) ); +INdefRecordPtr NFCFactory::createNDEFRecordMediaObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload) { + return INdefRecordPtr( new NdefRecordMedia(ndefRecordProperties, payload) ); } INFCTagPtr NFCFactory::createNFCTagObject(void *tagHandle) { diff --git a/src/NFC/NFCFactory.h b/src/NFC/NFCFactory.h index 6d1bc66..b536ff4 100755 --- a/src/NFC/NFCFactory.h +++ b/src/NFC/NFCFactory.h @@ -39,15 +39,15 @@ private: public: INFCAdapterPtr createNFCAdapterObject(); INdefMessagePtr createNDEFMessageObject(); - INdefMessagePtr createNDEFMessageObject(void *messageHandle); - INdefMessagePtr createNDEFMessageObject(std::vector ndefRcords); - INdefMessagePtr createNDEFMessageObject(std::vector rawdata); INdefRecordPtr createNDEFRecordObject(); INdefRecordPtr createNDEFRecordObject(std::vector data); - INdefRecordPtr createNDEFRecordObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload); - INdefRecordPtr createNDEFRecordObject(const std::string &text, const std::string &langCode, const short encodeType); - INdefRecordPtr createNDEFRecordObject(const std::string &uri); - INdefRecordPtr createNDEFRecordObject(const std::string &mimeType, const std::vector data); + INdefRecordPtr createNDEFRecordObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload, bool isValid = true); + INdefRecordPtr createNDEFRecordTextObject(const std::string &text, const std::string &langCode, const nfcTextEncodeUTF encodeType, bool isValid = true); + INdefRecordPtr createNDEFRecordTextObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload); + INdefRecordPtr createNDEFRecordUriObject(const std::string &uri, bool isValid = true); + INdefRecordPtr createNDEFRecordUriObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload); + INdefRecordPtr createNDEFRecordMediaObject(const std::string &mimeType, const std::vector data, bool isValid = true); + INdefRecordPtr createNDEFRecordMediaObject(const NdefRecordProperties &ndefRecordProperties, std::vector payload); INFCTagPtr createNFCTagObject(void *tagHandle); INFCTargetPtr createNFCTargetObject(void *targetHandle); diff --git a/src/NFC/NFCStaticController.cpp b/src/NFC/NFCStaticController.cpp old mode 100644 new mode 100755 diff --git a/src/NFC/NFCTag.cpp b/src/NFC/NFCTag.cpp old mode 100644 new mode 100755 index 573a81c..befb109 --- a/src/NFC/NFCTag.cpp +++ b/src/NFC/NFCTag.cpp @@ -243,9 +243,6 @@ void NFCTag::writeNdefManualAnswer(int result) { LoggerD("Enter"); if (m_EventTagActionWritePtr.Get() != NULL) { - if (nfc_ndef_message_destroy(static_cast(m_EventTagActionWritePtr->getNdefForWriting())) != NFC_ERROR_NONE) - LoggerE("Can't destroy NdefMessage"); - if ((nfc_error_e)result == NFC_ERROR_NONE) { m_EventTagActionWritePtr->setResult(TRUE); } else { diff --git a/src/NFC/NFCTarget.cpp b/src/NFC/NFCTarget.cpp old mode 100644 new mode 100755 index 5a4231a..79c179f --- a/src/NFC/NFCTarget.cpp +++ b/src/NFC/NFCTarget.cpp @@ -145,9 +145,6 @@ void NFCTarget::sendNDEF(const EventTargetActionSendPtr& event) { void NFCTarget::sendNDEFManualAnswer(unsigned int result) { LoggerD("Enter"); if (m_EventTargetActionSendPtr.Get() != NULL) { - if (nfc_ndef_message_destroy(static_cast(m_EventTargetActionSendPtr->getMessageForSending())) != NFC_ERROR_NONE) - LoggerE("Can't destroy NdefMessage"); - if ((nfc_error_e)result == NFC_ERROR_NONE) { m_EventTargetActionSendPtr->setResult(TRUE); } else { diff --git a/src/NFC/NFCUtil.cpp b/src/NFC/NFCUtil.cpp old mode 100644 new mode 100755 index 52acff8..70a5cb0 --- a/src/NFC/NFCUtil.cpp +++ b/src/NFC/NFCUtil.cpp @@ -121,46 +121,39 @@ nfcTagType NFCUtil::convertTonfcTagType(const unsigned short type) { } } -unsigned short NFCUtil::convertToTNF(nfcTNF tnf) { - switch (tnf) { - case NFC_TNF_WELL_KNOWN: - return static_cast(NFC_RECORD_TNF_WELL_KNOWN); - case NFC_TNF_MIME_MEDIA: - return static_cast(NFC_RECORD_TNF_MIME_MEDIA); - case NFC_TNF_URI: - return static_cast(NFC_RECORD_TNF_URI); - case NFC_TNF_EXTERNAL_RTD: - return static_cast(NFC_RECORD_TNF_EXTERNAL_RTD); - case NFC_TNF_UNKNOWN: - return static_cast(NFC_RECORD_TNF_UNKNOWN); - case NFC_TNF_UNCHANGED: - return static_cast(NFC_RECORD_TNF_UNCHAGNED); - case NFC_TNF_EMPTY: - default: - return static_cast(NFC_RECORD_TNF_EMPTY); - } -} +void *NFCUtil::makeNDEFRecord(const NdefRecordProperties &ndefRecordProperties, std::vector payload) { + LoggerD("entered"); -nfcTNF NFCUtil::convertTonfcTNF(unsigned short tnf) { - switch (static_cast(tnf)) { - case NFC_RECORD_TNF_WELL_KNOWN: - return NFC_TNF_WELL_KNOWN; - case NFC_RECORD_TNF_MIME_MEDIA: - return NFC_TNF_MIME_MEDIA; - case NFC_RECORD_TNF_URI: - return NFC_TNF_URI; - case NFC_RECORD_TNF_EXTERNAL_RTD: - return NFC_TNF_EXTERNAL_RTD; - case NFC_RECORD_TNF_UNKNOWN: - return NFC_TNF_UNKNOWN; - case NFC_RECORD_TNF_UNCHAGNED: - return NFC_TNF_UNCHANGED; - case NFC_RECORD_TNF_EMPTY: - default: - return NFC_TNF_EMPTY; + if ((ndefRecordProperties.tnf < 0) || (ndefRecordProperties.tnf > 6)) + ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Not Supported tnf"); + + nfc_ndef_record_h handle = NULL; + + unsigned char *recordPayload = toCharPtr(payload); + unsigned int payloadSize = payload.size(); + unsigned char *typeName = toCharPtr(ndefRecordProperties.typeName); + int typeSize = (static_cast(ndefRecordProperties.typeName.size()) > 255) ? 255 : static_cast(ndefRecordProperties.typeName.size()); + unsigned char *id = toCharPtr(ndefRecordProperties.id); + int idSize = (static_cast(ndefRecordProperties.id.size()) > 255) ? 255 : static_cast(ndefRecordProperties.id.size()); + + int result = nfc_ndef_record_create(&handle, static_cast(ndefRecordProperties.tnf), typeName, typeSize, + id, idSize, recordPayload, payloadSize) ; + + if (recordPayload) + free(recordPayload); + if (typeName) + free(typeName); + if (id) + free(id); + + if (result != NFC_ERROR_NONE) { + if (handle) + nfc_ndef_record_destroy(handle); + throwNFCException(result, " Can't create Ndef Record"); } -} + return (void *)handle; +} NdefRecordData NFCUtil::getNDEFRecordData(void *handle) { nfc_ndef_record_h recordHandle = static_cast(handle); @@ -187,7 +180,7 @@ NdefRecordData NFCUtil::getNDEFRecordData(void *handle) { LoggerD("typeName : " << byteToString(typeName, typeSize)); LoggerD("payload : " << byteToString(payload, payloadSize)); - recordData.properties.tnf = convertTonfcTNF(static_cast(tnf)); + recordData.properties.tnf = static_cast(tnf); recordData.properties.typeName = toVector(typeName, typeSize); recordData.properties.id = toVector(id, idSize); recordData.payload = toVector(payload, payloadSize); @@ -195,43 +188,12 @@ NdefRecordData NFCUtil::getNDEFRecordData(void *handle) { return recordData; } -bool NFCUtil::copyNDEFRecord(void **src, void **dest) { - nfc_ndef_record_h *srcHandle, *destHandle; - srcHandle = (nfc_ndef_record_h *)src; - destHandle = (nfc_ndef_record_h *)dest; - - NdefRecordData recordData = getNDEFRecordData(*srcHandle); - - unsigned char *typeName = toCharPtr(recordData.properties.typeName); - unsigned char *id = toCharPtr(recordData.properties.id); - unsigned char *payload = toCharPtr(recordData.payload); - - if (nfc_ndef_record_create(destHandle, static_cast(convertToTNF(recordData.properties.tnf)), typeName, recordData.properties.typeName.size(), - id, recordData.properties.id.size(), payload, recordData.payload.size()) != NFC_ERROR_NONE) { - if (typeName) - free(typeName); - if (id) - free(id); - if (payload) - free(payload); - - return false; - } - - if (typeName) - free(typeName); - if (id) - free(id); - if (payload) - free(payload); - - return true; -} - std::string NFCUtil::getNFCErrorString(const int errorCode) { LoggerD ("Errorcode : " << errorCode); switch(errorCode) { case NFC_ERROR_NONE: + case NFC_ERROR_ALREADY_ACTIVATED: + case NFC_ERROR_ALREADY_DEACTIVATED: return ""; case NFC_ERROR_INVALID_PARAMETER: case NFC_ERROR_INVALID_NDEF_MESSAGE: @@ -256,6 +218,8 @@ std::string NFCUtil::getNFCErrorMessage(const int errorCode) { LoggerD ("Errorcode : " << errorCode); switch(errorCode) { case NFC_ERROR_NONE: + case NFC_ERROR_ALREADY_ACTIVATED: + case NFC_ERROR_ALREADY_DEACTIVATED: return ""; case NFC_ERROR_INVALID_PARAMETER: return "Invalid Parameter"; @@ -285,6 +249,8 @@ std::string NFCUtil::getNFCErrorMessage(const int errorCode) { return "No NDEF Message"; case NFC_ERROR_NOT_NDEF_FORMAT: return "Not NDEF Format"; + case NFC_ERROR_SECURITY_RESTRICTED: + return "Security Restricted"; } return "UnknownError"; } @@ -303,6 +269,9 @@ void NFCUtil::throwNFCException(const int errorCode, const std::string &message) case NFC_ERROR_NOT_ACTIVATED: ThrowMsg(WrtDeviceApis::Commons::PlatformException, message); break; + case NFC_ERROR_SECURITY_RESTRICTED: + ThrowMsg(WrtDeviceApis::Commons::SecurityException, message); + break; case NFC_ERROR_NOT_SUPPORTED: case NFC_ERROR_OPERATION_FAILED: case NFC_ERROR_DEVICE_BUSY: diff --git a/src/NFC/NFCUtil.h b/src/NFC/NFCUtil.h index 1ec835b..563bc53 100755 --- a/src/NFC/NFCUtil.h +++ b/src/NFC/NFCUtil.h @@ -40,13 +40,11 @@ class NFCUtil char *byteToString(const unsigned char* buffer, const int size); char *byteToString(std::vector *buffer); nfcTagType convertTonfcTagType(const unsigned short type); - unsigned short convertToTNF(nfcTNF tnf); - nfcTNF convertTonfcTNF(unsigned short tnf); NdefRecordData getNDEFRecordData(void *handle); - bool copyNDEFRecord(void **src, void **dest); std::string getNFCErrorString(const int errorCode); std::string getNFCErrorMessage(const int errorCode); void throwNFCException(const int errorCode, const std::string &message); + void *makeNDEFRecord(const NdefRecordProperties &ndefRecordProperties, std::vector payload); }; } diff --git a/src/NFC/NdefMessage.cpp b/src/NFC/NdefMessage.cpp index 4e006d5..b0240ad 100755 --- a/src/NFC/NdefMessage.cpp +++ b/src/NFC/NdefMessage.cpp @@ -28,143 +28,125 @@ using namespace WrtDeviceApis::Commons; NdefMessage::NdefMessage() { LoggerD("entered"); - recordPtr = NULL; - NFCUtil util; - int result = nfc_ndef_message_create(&handle); - - if (result != NFC_ERROR_NONE) { - handle = NULL; - util.throwNFCException(result, "Can't create Ndef Message"); - } -} - -NdefMessage::NdefMessage(void *messageHandle) -{ - LoggerD("entered"); - recordPtr = NULL; - if (messageHandle == NULL) { - handle = NULL; - ThrowMsg(UnknownException, "Message Handler is Null Pointer."); - } - - handle = static_cast(messageHandle); -} - -NdefMessage::NdefMessage(std::vector &ndefRcords) -{ - LoggerD("entered"); - recordPtr = NULL; - handle = NULL; - handle = static_cast(makeMessage(ndefRcords)); -} - -NdefMessage::NdefMessage(const std::vector &rawdata) -{ - LoggerD("entered"); - recordPtr = NULL; - handle = static_cast(makeMessage(rawdata)); } NdefMessage::~NdefMessage() { - LoggerD("entered"< &ndefRcords) { +void *NdefMessage::makeNdefMessageHandle(std::vector &ndefRcords) { NFCUtil util; - nfc_ndef_message_h message; + if (ndefRcords.size() == 0) + return NULL; + + nfc_ndef_message_h message = NULL; int result = nfc_ndef_message_create(&message); if (result != NFC_ERROR_NONE) { + LoggerE(util.getNFCErrorMessage(result)); + if (message) + nfc_ndef_message_destroy(message); util.throwNFCException(result, "Can't create Ndef Message"); } for (int i = 0 ; i < static_cast(ndefRcords.size()); i++) { - nfc_ndef_record_h insertRecord; - - if (!util.copyNDEFRecord(&ndefRcords[i], (void **)(&insertRecord))) { - LoggerD("copyNDEFRecord fail!"); - ThrowMsg(UnknownException, "Can't copy Record"); - } - - result = nfc_ndef_message_append_record(message, insertRecord); + if ((message == NULL) && (ndefRcords[i] != NULL)) + nfc_ndef_record_destroy((nfc_ndef_record_h)ndefRcords[i]); + result = nfc_ndef_message_append_record(message, (nfc_ndef_record_h)ndefRcords[i]); if (result != NFC_ERROR_NONE) { - LoggerE(i << " record can't be inserted. " << insertRecord << " : " << message); - nfc_ndef_message_destroy(message); - util.throwNFCException(result, "Can't insert record in Ndef Message"); + LoggerE(i << " record can't be inserted. " << ndefRcords[i] << " : " << message); + if (message) + nfc_ndef_message_destroy(message); + message = NULL; } } - return (void*)message; + + return (void *)message; } -void *NdefMessage::makeMessage(const std::vector &rawdata) { +std::vector NdefMessage::toByte(std::vector &ndefRcords) { NFCUtil util; - unsigned char *messageRawdata = util.toCharPtr(rawdata); - nfc_ndef_message_h message; - int result = nfc_ndef_message_create_from_rawdata(&message, messageRawdata, rawdata.size()); - - if (result != NFC_ERROR_NONE) { - handle = NULL; - if (messageRawdata) - free(messageRawdata); - util.throwNFCException(result, "Can't create Ndef Message"); - } - if (messageRawdata) - free(messageRawdata); - - return (void*)message; -} + if (ndefRcords.size() == 0) + return util.toVector(NULL, 0); -void NdefMessage::setRecordesPtr(void *records) { - LoggerD("entered"< byteData = util.toVector(rawdata, size); -long NdefMessage::getRecordCount() { - int count; - int result = nfc_ndef_message_get_record_count(handle, &count); + if (message) + nfc_ndef_message_destroy(message); + if (rawdata) + free(rawdata); - NFCUtil util; if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get Record Count"); + util.throwNFCException(result, "Can't get serial bytes of NDEF message"); - LoggerD("record Count : " << count); - return static_cast(count); + return byteData; } -std::vector NdefMessage::toByte() { - unsigned char *rawdata = NULL; - int size; +std::vector NdefMessage::toNDEFRecords(const std::vector &rawdata) { + LoggerD("entered"); NFCUtil util; + unsigned char *messageRawdata = util.toCharPtr(rawdata); + nfc_ndef_message_h message = NULL; - if (getRecordCount() == 0) - return util.toVector(NULL, 0); + try { + int result = nfc_ndef_message_create_from_rawdata(&message, messageRawdata, rawdata.size()); + if (messageRawdata) + free(messageRawdata); - int result = nfc_ndef_message_get_rawdata(handle, &rawdata, &size); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't create Ndef Message"); + } catch (const WrtDeviceApis::Commons::Exception& err) { + LoggerE(err.GetClassName() << ":"< NdefMessage::toNDEFRecords(void *message) { + LoggerD("entered"); + NFCUtil util; + std::vector records; + + if (message == NULL) + return records; + + try { + int count = 0; + int result = nfc_ndef_message_get_record_count((nfc_ndef_message_h)message, &count); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't get a record count of message"); + for (int i=0; i byteData = util.toVector(rawdata, size); - if (rawdata) - free(rawdata); - return byteData; + if (message) + nfc_ndef_message_destroy((nfc_ndef_message_h)message); + message = NULL; + + return records; } -NdefRecordData NdefMessage::getNDEFRecord(const long index) { + +NdefRecordData NdefMessage::_getNDEFRecord(nfc_ndef_message_h handle, const long index) { nfc_ndef_record_h recordHandle; int result = nfc_ndef_message_get_record(handle, static_cast(index), &recordHandle); @@ -177,6 +159,5 @@ NdefRecordData NdefMessage::getNDEFRecord(const long index) { return util.getNDEFRecordData(recordHandle); } - } } diff --git a/src/NFC/NdefMessage.h b/src/NFC/NdefMessage.h index 92ebbf7..765e4c2 100755 --- a/src/NFC/NdefMessage.h +++ b/src/NFC/NdefMessage.h @@ -33,22 +33,14 @@ class NdefMessage : public INdefMessage friend class NFCFactory; public: NdefMessage(); - NdefMessage(void *messageHandle); - NdefMessage(std::vector &ndefRcords); - NdefMessage(const std::vector &rawdata); virtual ~NdefMessage(); - virtual std::vector toByte(); - virtual long getRecordCount(); - virtual NdefRecordData getNDEFRecord(const long index); - - virtual void *makeMessage(std::vector &ndefRcords); - virtual void *makeMessage(const std::vector &rawdata); - virtual void setRecordesPtr(void *records); - virtual void* getRecordesPtr(); - private: - nfc_ndef_message_h handle; - void *recordPtr; + virtual std::vector toByte(std::vector &ndefRcords); + virtual void *makeNdefMessageHandle(std::vector &ndefRcords); + virtual std::vector toNDEFRecords(const std::vector &rawdata); + virtual std::vector toNDEFRecords(void *message); + private: + NdefRecordData _getNDEFRecord(nfc_ndef_message_h handle, const long index); }; } diff --git a/src/NFC/NdefRecord.cpp b/src/NFC/NdefRecord.cpp index 23a6fc6..802b0e0 100755 --- a/src/NFC/NdefRecord.cpp +++ b/src/NFC/NdefRecord.cpp @@ -15,7 +15,6 @@ // limitations under the License. // -#include #include "NdefRecord.h" #include "NFCUtil.h" #include @@ -25,292 +24,79 @@ namespace NFC { using namespace WrtDeviceApis::Commons; -NdefRecord::NdefRecord() -{ - handle = NULL; -} +NdefRecord::NdefRecord() : validStatus(INVALID_STATE) {} -NdefRecord::NdefRecord(std::vector data) +NdefRecord::NdefRecord(std::vector data) : validStatus(UNKNOWN_STATE) { LoggerD("entered"); - nfc_ndef_message_h messageHandle; + nfc_ndef_message_h messageHandle = NULL; + NFCUtil util; unsigned char *rawdata = util.toCharPtr(data); - int result = nfc_ndef_message_create_from_rawdata(&messageHandle, rawdata, data.size()); - - if (result != NFC_ERROR_NONE) { - if (messageHandle) - nfc_ndef_message_destroy(messageHandle); - - messageHandle = NULL; - if (rawdata) - free(rawdata); - rawdata = NULL; - util.throwNFCException(result, "Can't create Ndef Record"); - } - - int count; - result = nfc_ndef_message_get_record_count(messageHandle, &count); - if ((result != NFC_ERROR_NONE) || (count != 1)) { - nfc_ndef_message_destroy(messageHandle); - messageHandle = NULL; - if (rawdata) - free(rawdata); - rawdata = NULL; - util.throwNFCException(result, "Can't create Ndef Record"); + try { + int result = nfc_ndef_message_create_from_rawdata(&messageHandle, rawdata, data.size()); + + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't create Ndef Message from data"); + + int count; + result = nfc_ndef_message_get_record_count(messageHandle, &count); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't get record count"); + + nfc_ndef_record_h recordHandle = NULL; + result = nfc_ndef_message_get_record(messageHandle, 0, &recordHandle); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't get Ndef Record"); + + ndefRecordData = util.getNDEFRecordData((void *)recordHandle); + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + LoggerE(err.GetClassName() << ":"< payload) -{ - LoggerD("entered"); - - NFCUtil util; - - unsigned char * recordPayload = util.toCharPtr(payload); - unsigned int payloadSize = payload.size(); - unsigned char * typeName = util.toCharPtr(ndefRecordProperties.typeName); - int typeSize = (static_cast(ndefRecordProperties.typeName.size()) > 255) ? 255 : static_cast(ndefRecordProperties.typeName.size()); - unsigned char * id = util.toCharPtr(ndefRecordProperties.id); - int idSize = (static_cast(ndefRecordProperties.id.size()) > 255) ? 255 : static_cast(ndefRecordProperties.id.size()); - - int result = nfc_ndef_record_create(&handle, static_cast(util.convertToTNF(ndefRecordProperties.tnf)), typeName, typeSize, - id, idSize, recordPayload, payloadSize) ; - if (recordPayload) - free(recordPayload); - if (typeName) - free(typeName); - if (id) - free(id); - - if (result != NFC_ERROR_NONE) { - handle = NULL; - util.throwNFCException(result, "Can't create Ndef Record"); - } -} - -NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType) +NdefRecord::NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector payload, bool isValid) : validStatus(UNKNOWN_STATE) { LoggerD("entered"); - int result = nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast(encodeType))); + ndefRecordData.properties = ndefRecordProperties; + ndefRecordData.payload = payload; - NFCUtil util; - if (result != NFC_ERROR_NONE) { - handle = NULL; - util.throwNFCException(result, "Can't create Ndef Text Record"); - } -} - -NdefRecord::NdefRecord(const std::string &uri) -{ - LoggerD("entered"); - - int result = nfc_ndef_record_create_uri(&handle, uri.c_str()); - - NFCUtil util; - if (result != NFC_ERROR_NONE) { - handle = NULL; - util.throwNFCException(result, "Can't create Ndef Uri Record"); - } -} - -NdefRecord::NdefRecord(const std::string &mimeType, const std::vector data) -{ - LoggerD("entered"); - - NFCUtil util; - - unsigned char *mimeData = util.toCharPtr(data); - int result = nfc_ndef_record_create_mime(&handle, mimeType.c_str(), mimeData, static_cast(data.size())); - - if (mimeData) - free(mimeData); - - if (result != NFC_ERROR_NONE) { - handle = NULL; - util.throwNFCException(result, "Can't create Ndef Mime Record"); - } + if (!isValid) + validStatus = INVALID_STATE; } NdefRecord::~NdefRecord() { LoggerD("entered"); - if (handle != NULL) - nfc_ndef_record_destroy(handle); -} - -nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) { - switch(type) { - case NFC_TEXT_ENCODE_UTF_8: - return NFC_ENCODE_UTF_8; - case NFC_TEXT_ENCODE_UTF_16: - return NFC_ENCODE_UTF_16; - } -} - -nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) { - switch(type) { - case NFC_ENCODE_UTF_8: - return NFC_TEXT_ENCODE_UTF_8; - case NFC_ENCODE_UTF_16: - return NFC_TEXT_ENCODE_UTF_16; - } } void *NdefRecord::getHandle() { - return (void *)handle; -} - -NdefRecordProperties NdefRecord::getNDEFRecordProperties() { - LoggerD("entered"); - NdefRecordProperties props; + if (validStatus == INVALID_STATE) + return NULL; NFCUtil util; - props.tnf = getTNF(); - props.typeName = getTypeName(); - props.id = getID(); - - return props; -} - -nfcTNF NdefRecord::getTNF() { - nfc_record_tnf_e tnf; - int result = nfc_ndef_record_get_tnf(handle, &tnf); - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get record's tnf"); - - return util.convertTonfcTNF(static_cast(tnf)); -} - -std::vector NdefRecord::getTypeName() { - unsigned char *typeName; - int typeSize; - int result = nfc_ndef_record_get_type(handle, &typeName, &typeSize); - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get record's type"); - - return util.toVector(typeName, typeSize); -} - -std::vector NdefRecord::getID() { - unsigned char *id; - int idSize; - int result = nfc_ndef_record_get_id(handle, &id, &idSize); - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get record's id"); - - return util.toVector(id, idSize); -} - -bool NdefRecord::getText(char **text) { - LoggerD("entered"); - - int result = nfc_ndef_record_get_text(handle, text); - if (result == NFC_ERROR_INVALID_RECORD_TYPE) - return false; - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get text of record"); - - return true; -} - -bool NdefRecord::getLangCode(char **langCode) { - LoggerD("entered"); - - int result = nfc_ndef_record_get_langcode(handle, langCode); - if (result == NFC_ERROR_INVALID_RECORD_TYPE) - return false; - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get langcode of record"); - - return true; -} -bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) { - LoggerD("entered"); - - nfc_encode_type_e type; - int result = nfc_ndef_record_get_encode_type(handle, &type); - if (result == NFC_ERROR_INVALID_RECORD_TYPE) - return false; - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get encode type of record"); - - *encodeType = _convertToNfcEncodeType(type); - return true; -} - -bool NdefRecord::getUri(char **uri) { - LoggerD("entered"); - int result = nfc_ndef_record_get_uri(handle, uri); - if (result == NFC_ERROR_INVALID_RECORD_TYPE) - return false; - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get uri of record"); - - return true; -} - -bool NdefRecord::getMimeType(char **mimeType) { - LoggerD("entered"); - int result = nfc_ndef_record_get_mime_type(handle, mimeType); - if (result == NFC_ERROR_INVALID_RECORD_TYPE) - return false; - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get mime type of record"); - - return true; -} -std::vector NdefRecord::getPayload() { - LoggerD("entered"); - int size; - unsigned char *recordbuffer; - int result = nfc_ndef_record_get_payload(handle, &recordbuffer, &size); - - NFCUtil util; - if (result != NFC_ERROR_NONE) - util.throwNFCException(result, "Can't get record's payload"); - - return util.toVector(recordbuffer, size); + void *handle = NULL; + try { + handle = util.makeNDEFRecord(ndefRecordData.properties, ndefRecordData.payload); + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + handle = NULL; + LoggerE(err.GetClassName() << ":"< #include #include "INdefRecord.h" - - +#include "NFCFactory.h" namespace DeviceAPI { namespace NFC { @@ -34,27 +33,16 @@ class NdefRecord : public INdefRecord friend class NFCFactory; public: NdefRecord(); - NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector payload); + NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector payload, bool isValid = true); NdefRecord(std::vector data); - NdefRecord(const std::string &text, const std::string &langCode, const short encodeType); - NdefRecord(const std::string &uri); - NdefRecord(const std::string &mimeType, const std::vector data); virtual ~NdefRecord(); virtual void *getHandle(); - virtual NdefRecordProperties getNDEFRecordProperties(); - virtual nfcTNF getTNF(); - virtual std::vector getTypeName(); - virtual std::vector getID(); - virtual std::vector getPayload(); - virtual bool getText(char **text); - virtual bool getLangCode(char **langCode); - virtual bool getEncodeType(nfcTextEncodeUTF *encodeType); - virtual bool getUri(char **uri); - virtual bool getMimeType(char **mimeType); - private: - nfc_ndef_record_h handle; - nfc_encode_type_e _convertToEncodeType(const nfcTextEncodeUTF type); - nfcTextEncodeUTF _convertToNfcEncodeType(const nfc_encode_type_e type); + virtual NdefRecordData getNDEFRecordData() {return ndefRecordData;} + + protected: + NdefRecordData ndefRecordData; + ndefStatus validStatus; + }; } diff --git a/src/NFC/NdefRecordMedia.cpp b/src/NFC/NdefRecordMedia.cpp new file mode 100755 index 0000000..386fc78 --- /dev/null +++ b/src/NFC/NdefRecordMedia.cpp @@ -0,0 +1,98 @@ +// +// 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 +#include "NdefRecordMedia.h" +#include "NFCUtil.h" +#include + +namespace DeviceAPI { +namespace NFC { + +using namespace WrtDeviceApis::Commons; + +NdefRecordMedia::NdefRecordMedia(const NdefRecordProperties &ndefRecordProperties, std::vector payload) +{ + LoggerD("entered"); + + ndefRecordData.properties = ndefRecordProperties; + ndefRecordData.payload = payload; + + NFCUtil util; + nfc_ndef_record_h handle = NULL; + char *mime = NULL; + try { + handle = (nfc_ndef_record_h)util.makeNDEFRecord(ndefRecordProperties, payload); + + int result = nfc_ndef_record_get_mime_type(handle, &mime); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, " Can't get mime type"); + mimeType = mime; + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + LoggerE(err.GetClassName() << ":"< data, bool isValid) +{ + LoggerD("entered"); + + this->mimeType = mimeType; + + if (!isValid) { + validStatus = INVALID_STATE; + return; + } + + NFCUtil util; + nfc_ndef_record_h handle = NULL; + unsigned char *mimeData = util.toCharPtr(data); + try { + int result = nfc_ndef_record_create_mime(&handle, mimeType.c_str(), mimeData, static_cast(data.size())); + + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't create Ndef Mime Record"); + + ndefRecordData = util.getNDEFRecordData(handle); + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + LoggerE(err.GetClassName() << ":"< + + +namespace DeviceAPI { +namespace NFC { + +class NdefRecordMedia : public NdefRecord +{ + friend class NFCFactory; + public: + NdefRecordMedia(const NdefRecordProperties &ndefRecordProperties, std::vector payload); + NdefRecordMedia(const std::string &mimeType, const std::vector data, bool isValid = true); + virtual ~NdefRecordMedia(); + virtual std::string getMimeType() {return mimeType;} + protected: + std::string mimeType; +}; + +} +} + +#endif /* _NDEFRECORDMEDIA_H_ */ diff --git a/src/NFC/NdefRecordProperties.h b/src/NFC/NdefRecordProperties.h index 63bf732..2021617 100755 --- a/src/NFC/NdefRecordProperties.h +++ b/src/NFC/NdefRecordProperties.h @@ -40,13 +40,20 @@ enum nfcTextEncodeUTF { NFC_TEXT_ENCODE_UTF_8 = 0, NFC_TEXT_ENCODE_UTF_16 = 1 }; + +enum ndefStatus { + UNKNOWN_STATE, + VALID_STATE, + INVALID_STATE +}; + struct NdefRecordProperties { - nfcTNF tnf; + short tnf; std::vector typeName ; std::vector id ; NdefRecordProperties() - : tnf(NFC_TNF_EMPTY) + : tnf(NFC_TNF_UNKNOWN) { } }; @@ -58,7 +65,7 @@ struct NdefRecordData NdefRecordData() {} }; -typedef DPL::SharedPtr NdefRecordPropertiesPtr; +//typedef DPL::SharedPtr NdefRecordPropertiesPtr; } // NFC } // DeviceAPI diff --git a/src/NFC/NdefRecordText.cpp b/src/NFC/NdefRecordText.cpp new file mode 100755 index 0000000..3615afe --- /dev/null +++ b/src/NFC/NdefRecordText.cpp @@ -0,0 +1,132 @@ +// +// 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 +#include "NdefRecordText.h" +#include "NFCUtil.h" +#include + +namespace DeviceAPI { +namespace NFC { + +using namespace WrtDeviceApis::Commons; + +NdefRecordText::NdefRecordText(const NdefRecordProperties &ndefRecordProperties, std::vector payload) +{ + LoggerD("entered"); + + ndefRecordData.properties = ndefRecordProperties; + ndefRecordData.payload = payload; + + NFCUtil util; + nfc_ndef_record_h handle = NULL; + char *recordText = NULL; + char *recordLang = NULL; + try { + handle = (nfc_ndef_record_h)util.makeNDEFRecord(ndefRecordProperties, payload); + + int result = nfc_ndef_record_get_text(handle, &recordText); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, " Can't get text"); + text = std::string(recordText); + + result = nfc_ndef_record_get_langcode(handle, &recordLang); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, " Can't get languageCode"); + languageCode = std::string(recordLang); + + nfc_encode_type_e type; + result = nfc_ndef_record_get_encode_type(handle, &type); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, " Can't get encoding"); + + encoding = _convertToNfcEncodeType(type); + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + encoding = NFC_TEXT_ENCODE_UTF_8; + LoggerE(err.GetClassName() << ":"<text = text; + languageCode = langCode; + encoding = encodeType; + + if (!isValid) { + validStatus = INVALID_STATE; + return; + } + + nfc_ndef_record_h handle = NULL; + try { + int result = nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(encodeType)); + + NFCUtil util; + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, "Can't create Ndef Text Record"); + + ndefRecordData = util.getNDEFRecordData(handle); + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + LoggerE(err.GetClassName() << ":"< payload); + NdefRecordText(const std::string &text, const std::string &langCode, const nfcTextEncodeUTF encodeType, bool isValid=true); + virtual ~NdefRecordText(); + virtual std::string getText() {return text;} + virtual std::string getLanguageCode() {return languageCode;} + virtual nfcTextEncodeUTF getEncoding() {return encoding;} + private: + std::string text; + std::string languageCode; + nfcTextEncodeUTF encoding; + + nfc_encode_type_e _convertToEncodeType(const nfcTextEncodeUTF &type); + nfcTextEncodeUTF _convertToNfcEncodeType(const nfc_encode_type_e type); +}; + +} +} + +#endif /* _NDEFRECORDTEXT_H_ */ diff --git a/src/NFC/NdefRecordURI.cpp b/src/NFC/NdefRecordURI.cpp new file mode 100755 index 0000000..7e7d687 --- /dev/null +++ b/src/NFC/NdefRecordURI.cpp @@ -0,0 +1,93 @@ +// +// 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 +#include "NdefRecordURI.h" +#include "NFCUtil.h" +#include + +namespace DeviceAPI { +namespace NFC { + +using namespace WrtDeviceApis::Commons; + +NdefRecordUri::NdefRecordUri(const NdefRecordProperties &ndefRecordProperties, std::vector payload) +{ + LoggerD("entered"); + + ndefRecordData.properties = ndefRecordProperties; + ndefRecordData.payload = payload; + + NFCUtil util; + nfc_ndef_record_h handle = NULL; + char *recordUri = NULL; + try { + handle = (nfc_ndef_record_h)util.makeNDEFRecord(ndefRecordProperties, payload); + + int result = nfc_ndef_record_get_uri(handle, &recordUri); + if (result != NFC_ERROR_NONE) + util.throwNFCException(result, " Can't get uri"); + uri = std::string(recordUri); + + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + LoggerE(err.GetClassName() << ":"<uri = uri; + + if (!isValid) { + validStatus = INVALID_STATE; + return; + } + + nfc_ndef_record_h handle = NULL; + try { + int result = nfc_ndef_record_create_uri(&handle, uri.c_str()); + + NFCUtil util; + if (result != NFC_ERROR_NONE) { + util.throwNFCException(result, "Can't create Ndef Uri Record"); + } + ndefRecordData = util.getNDEFRecordData(handle); + validStatus = VALID_STATE; + } catch (const WrtDeviceApis::Commons::Exception& err) { + validStatus = INVALID_STATE; + LoggerE(err.GetClassName() << ":"< payload); + NdefRecordUri(const std::string &uri, bool isValid = true); + virtual ~NdefRecordUri(); + virtual std::string getUri( ) {return uri;} + private: + std::string uri; + +}; + +} +} + +#endif /* _NDEFRECORDURI_H_ */ diff --git a/src/NFC/ResponseDispatcher.cpp b/src/NFC/ResponseDispatcher.cpp old mode 100644 new mode 100755 diff --git a/src/NetworkBearerSelection/NetworkBearerSelection.cpp b/src/NetworkBearerSelection/NetworkBearerSelection.cpp index 2230cfd..cd1c8c1 100755 --- a/src/NetworkBearerSelection/NetworkBearerSelection.cpp +++ b/src/NetworkBearerSelection/NetworkBearerSelection.cpp @@ -170,6 +170,11 @@ void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerSelection return; } connection_profile_get_name(m_profileHandle, &defaultProfileName); + if (defaultProfileName == NULL) { + LoggerE("default profile is not exist."); + makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR); + return; + } if (connection_get_current_profile(m_connectionHandle, &profileHandle) != CONNECTION_ERROR_NONE) { LoggerD("Fail to get current profile handle"); @@ -177,6 +182,11 @@ void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerSelection return; } connection_profile_get_name(profileHandle, ¤tProfileName); + if (currentProfileName == NULL) { + LoggerE("current profile is not exist."); + makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR); + return; + } if (strcmp(defaultProfileName, currentProfileName) != 0) { NewtorkBearerSelectionPendingEvent *pendingEvent = new NewtorkBearerSelectionPendingEvent((void *)this, event); @@ -193,14 +203,9 @@ void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerSelection registStateChangeListener(event); } - if(defaultProfileName != NULL) { free(defaultProfileName); - } - - if(currentProfileName != NULL) { free(currentProfileName); } -} void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerReleasePtr &event) { @@ -227,9 +232,8 @@ void NetworkBearerSelection::OnRequestReceived(const EventNetworkBearerReleasePt return; } - NewtorkBearerReleasePendingEvent *pendingEvent = new NewtorkBearerReleasePendingEvent((void *)this, event); - if (m_isConnectionOpen) { + NewtorkBearerReleasePendingEvent *pendingEvent = new NewtorkBearerReleasePendingEvent((void *)this, event); if (connection_close_profile(m_connectionHandle, m_profileHandle, connection_closed_callback, pendingEvent) != CONNECTION_ERROR_NONE) { LoggerD("connection close failed"); delete pendingEvent; @@ -274,7 +278,6 @@ void NetworkBearerSelection::registStateChangeListener(const EventNetworkBearerS if(!host_entry) { LoggerD("gethostbyname is failed"); makeRequestCallback(event, CONNECTION_STATE_INVALID_VALUES_ERROR); - if (connection_close_profile(m_connectionHandle, m_profileHandle, connection_closed_callback2, NULL) != CONNECTION_ERROR_NONE) { LoggerD("connection close failed"); makeRequestCallback(event, CONNECTION_STATE_PLATFORM_ERROR); diff --git a/src/Notification/JSStatusNotification.cpp b/src/Notification/JSStatusNotification.cpp index 471351a..eefb695 100755 --- a/src/Notification/JSStatusNotification.cpp +++ b/src/Notification/JSStatusNotification.cpp @@ -64,6 +64,7 @@ JSStaticValue JSStatusNotification::m_property[] = {STATUS_NOTIFICATION_LIGHT_ONTIME, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone }, {STATUS_NOTIFICATION_LIGHT_OFFTIME, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone }, {STATUS_NOTIFICATION_VIBRATION, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone }, + {NOTIFICATION_TITLE, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone }, {0, 0, 0, 0} }; @@ -137,27 +138,24 @@ bool JSStatusNotification::setProperty(JSContextRef context, unsigned long onPeriod = JSUtil::JSValueToULong(context, value); LoggerI(" LEDonPeriod = " << onPeriod); priv->setLightOnTime(onPeriod); - - // JSUtil::setProperty(context, object, STATUS_NOTIFICATION_LIGHT_ONTIME, - // JSUtil::toJSValueRef(context, onPeriod), kJSPropertyAttributeNone); } else if ( property == STATUS_NOTIFICATION_LIGHT_OFFTIME) { unsigned long offPeriod = JSUtil::JSValueToULong(context, value); LoggerI(" LEDoffPeriod = " << offPeriod); priv->setLightOffTime(offPeriod); - - // JSUtil::setProperty(context, object, STATUS_NOTIFICATION_LIGHT_OFFTIME, - // JSUtil::toJSValueRef(context, offPeriod), kJSPropertyAttributeNone); } else if ( property == STATUS_NOTIFICATION_VIBRATION) { bool vibration = JSUtil::JSValueToBoolean(context, value); LoggerI(" vibration = " << vibration); priv->setDefaultVibration(vibration); - - // JSUtil::setProperty(context, object, STATUS_NOTIFICATION_VIBRATION, - // JSUtil::toJSValueRef(context, vibration), kJSPropertyAttributeNone); + } + else if ( property == NOTIFICATION_TITLE) + { + std::string title = JSUtil::JSValueToString(context, value); + LoggerI(" title = " << title); + priv->setTitle(title); } } @@ -254,6 +252,11 @@ JSValueRef JSStatusNotification::getProperty(JSContextRef context, bool vibration = priv->getDefaultVibration(); return JSUtil::toJSValueRef(context, vibration); } + else if ( property == NOTIFICATION_TITLE) + { + std::string title = priv->getTitle(); + return JSUtil::toJSValueRef(context, title); + } } catch ( const BasePlatformException& err) { diff --git a/src/SecureElement/JSSEService.cpp b/src/SecureElement/JSSEService.cpp index 903179b..a6bb992 100755 --- a/src/SecureElement/JSSEService.cpp +++ b/src/SecureElement/JSSEService.cpp @@ -19,12 +19,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include "SEFactory.h" #include "SEResponseDispatcher.h" #include "JSSEService.h" @@ -201,36 +201,34 @@ JSValueRef JSSEService::registerSEListener(JSContextRef context, AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS); TIZEN_SYNC_ACCESS_HANDLER(status, context, exception); - Validator validator(context, exception); + SEListener seListener; try { ArgumentValidator argValidator(context, argumentCount, arguments); - if (!argValidator.toObject(0)) - throw TypeMismatchException("Parameter is not Object"); + JSObjectRef listenerObj = argValidator.toCallbackObject(0, false, "onSEReady", "onSENotReady", NULL); + JSValueRef onSEReady = JSUtil::getProperty(context, listenerObj, "onSEReady", exception); + if (!JSValueIsUndefined(context, onSEReady)) + seListener.onSEReady = onSEReady; + + JSValueRef onSENotReady = JSUtil::getProperty(context, listenerObj, "onSENotReady", exception); + if (!JSValueIsUndefined(context, onSENotReady)) + seListener.onSENotReady = onSENotReady; }catch(const BasePlatformException& err){ return JSWebAPIErrorFactory::postException(context, exception, err); } - SEConverter convert(context); - SEListener seListener; - if (!validator.isCallback(arguments[0])) { - seListener = convert.toSEListener(arguments[0]); - } else { - /* 1st argument must be SEListener. */ - LoggerE("SEListener must has onSEReady and onSENotReady"); - return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } + Try { + SEConverter convert(context); + SEServicePrivObject* privateObject = static_cast(JSObjectGetPrivate(thisObject)); + if (NULL == privateObject) { + LoggerE("private object is null"); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); + } - SEServicePrivObject* privateObject = static_cast(JSObjectGetPrivate(thisObject)); - if (NULL == privateObject) { - LoggerE("private object is null"); - return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); - } + ISEServicePtr seService(privateObject->getObject()); + JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), seListener.onSEReady, seListener.onSENotReady, true, true); + JSValueProtect(privateObject->getContext(), thisObject); - ISEServicePtr seService(privateObject->getObject()); - JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), seListener.onSEReady, seListener.onSENotReady, true, true); - JSValueProtect(privateObject->getContext(), thisObject); - Try { EventSEStateChangedEmitterPtr emitter(new EventSEStateChangedEmitter); emitter->setListener(&SEResponseDispatcher::getInstance()); emitter->setEventPrivateData(DPL::StaticPointerCast(callbackManager)); diff --git a/src/SecureElement/SEConverter.cpp b/src/SecureElement/SEConverter.cpp index b2211c8..ac355e1 100644 --- a/src/SecureElement/SEConverter.cpp +++ b/src/SecureElement/SEConverter.cpp @@ -26,9 +26,6 @@ using namespace WrtDeviceApis::CommonsJavaScript; using namespace WrtDeviceApis; using namespace DeviceAPI::Common; -#define SE_LISTENER_ONSEREADY "onSEReady" -#define SE_LISTENER_ONSENOTREADY "onSENotReady" - namespace DeviceAPI { namespace SecureElement { @@ -61,35 +58,6 @@ JSValueRef SEConverter::toJSValueRef(const std::vector& arg) { return jsResult; } -SEListener SEConverter::toSEListener(const JSValueRef& arg) { - LoggerD("Entered"); - JSObjectRef object = toJSObjectRef(arg); - - SEListener result; - Validator validator(m_context); - - result.onSEReady= JSUtils::getJSPropertyOrUndefined( - m_context, object, SE_LISTENER_ONSEREADY - ); - if (!validator.isNullOrUndefined(result.onSEReady) && - !validator.isCallback(result.onSEReady)) { - ThrowMsg(Commons::ConversionException, "Not a valid callback."); - } - - result.onSENotReady= JSUtils::getJSPropertyOrUndefined( - m_context, object, SE_LISTENER_ONSENOTREADY - ); - if (!validator.isNullOrUndefined(result.onSENotReady) && - !validator.isCallback(result.onSENotReady)) { - ThrowMsg(Commons::ConversionException, "Not a valid callback."); - } - - if (validator.isNullOrUndefined(result.onSEReady) && validator.isNullOrUndefined(result.onSENotReady)) - ThrowMsg(Commons::ConversionException, "Not a valid callback."); - - return result; -} - JSValueRef SEConverter::makeSeErrorObject(std::string error, std::string message) { return JSWebAPIErrorFactory::makeErrorObject(m_context, error, message); } diff --git a/src/SecureElement/SEConverter.h b/src/SecureElement/SEConverter.h index 286f58e..2e4e41a 100755 --- a/src/SecureElement/SEConverter.h +++ b/src/SecureElement/SEConverter.h @@ -38,7 +38,6 @@ class SEConverter : public WrtDeviceApis::CommonsJavaScript::Converter virtual ~SEConverter(); JSValueRef toJSValueRef(const std::vector& arg); - SEListener toSEListener(const JSValueRef& arg); JSValueRef makeSeErrorObject(std::string error, std::string message); }; diff --git a/src/Systeminfo/Systeminfo.cpp b/src/Systeminfo/Systeminfo.cpp index 69ee5cb..3a557e3 100755 --- a/src/Systeminfo/Systeminfo.cpp +++ b/src/Systeminfo/Systeminfo.cpp @@ -632,6 +632,12 @@ void Systeminfo::OnRequestReceived(const EventGetSysteminfoPtr& event) simState = strdup("UNKNOWN"); break; } + if (simState == NULL) { + LoggerE("get fail sim state"); + event->makeSimObject(); + EventRequestReceiver::ManualAnswer(event); + return; + } event->setSimState(simState); if(strcmp(simState, "READY") == 0) { if (tel_get_sim_imsi(m_tapiHandle, &imsi) == TAPI_API_SUCCESS) { @@ -652,9 +658,7 @@ void Systeminfo::OnRequestReceived(const EventGetSysteminfoPtr& event) event->makeSimObject(); EventRequestReceiver::ManualAnswer(event); } - if (simState) { - free(simState); - } + free(simState); } else { LoggerE("get fail sim state"); event->makeSimObject();