From: Marcin Kaminski Date: Sat, 27 Dec 2014 17:28:54 +0000 (+0100) Subject: [NFC] NFCTag::readNDEF implementation added X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~712 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d5a268af9823a9c5ef58597dd5564eb2cdf84f13;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [NFC] NFCTag::readNDEF implementation added Code compiles without errors. Implementation partially verified after exporting NFCTag constructor - all error callbacks called properly, unable to test success callback Change-Id: Iea80a96276cf5c2a331bd06d5784573b34108cb5 Signed-off-by: Marcin Kaminski --- diff --git a/src/nfc/nfc_adapter.cc b/src/nfc/nfc_adapter.cc index 6e5ecace..444b6669 100644 --- a/src/nfc/nfc_adapter.cc +++ b/src/nfc/nfc_adapter.cc @@ -4,6 +4,7 @@ #include "nfc_adapter.h" #include "nfc_util.h" +#include "nfc_message_utils.h" #include @@ -835,6 +836,93 @@ void NFCAdapter::SetTagHandle(nfc_tag_h tag) { m_last_tag_handle = tag; } +static void tagReadNDEFCb(nfc_error_e result , nfc_ndef_message_h message , void *data) +{ + LoggerD("Entered"); + + if(!data) { + // Can not continue if unable to get callbackId + LoggerE("NULL callback id given"); + } + + double callbackId = *((double*)data); + delete (double*)data; + data = NULL; + + if(NFC_ERROR_NONE != result) { + // create exception and post error message (call error callback) + auto ex = UnknownException("Failed to read NDEF message"); + picojson::value event = createEventError(callbackId, ex); + NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + return; + } + + unsigned char *raw_data = NULL; + unsigned int size = 0; + + int ret = nfc_ndef_message_get_rawdata(message, &raw_data, &size); + if(NFC_ERROR_NONE != ret) { + LoggerE("Failed to get message data: %d", ret); + + // release data if any allocated + free(raw_data); + + // create exception and post error message (call error callback) + auto ex = UnknownException("Failed to retrieve NDEF message data"); + picojson::value event = createEventError(callbackId, ex); + NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + return; + } + + // create success event, fill it with data and call success callback + picojson::value event = createEventSuccess(callbackId); + picojson::object& obj = event.get(); + + NFCMessageUtils::ReportNdefMessageFromData(raw_data, size, obj); + + NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + free(raw_data); +} + +void NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) { + + LoggerD("Entered"); + double callbackId = args.get(CALLBACK_ID).get(); + LoggerD("Received callback id: %f", callbackId); + + if(!TagIsConnectedGetter(tag_id)) { + auto ex = UnknownException("Tag is no more connected"); + + picojson::value event = createEventError(callbackId, ex); + NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + return; + } + + double* callbackIdPointer = new double(callbackId); + int result = nfc_tag_read_ndef(m_last_tag_handle, tagReadNDEFCb, + (void*)(callbackIdPointer)); + if(NFC_ERROR_NONE != result) { + LoggerE("Failed to read NDEF message from tag: %d", result); + delete callbackIdPointer; + callbackIdPointer = NULL; + + // for permission related error throw exception ... + if(NFC_ERROR_SECURITY_RESTRICTED == result || + NFC_ERROR_PERMISSION_DENIED == result) { + throw SecurityException("Failed to read NDEF - permission denied"); + } + + LoggerE("Preparing error callback to call"); + // ... otherwise call error callback + std::string errName = NFCUtil::getNFCErrorString(result); + std::string errMessage = NFCUtil::getNFCErrorMessage(result); + auto ex = PlatformException(errName, errMessage); + + picojson::value event = createEventError(callbackId, ex); + NFCInstance::getInstance().PostMessage(event.serialize().c_str()); + } + +} }// nfc }// extension diff --git a/src/nfc/nfc_adapter.h b/src/nfc/nfc_adapter.h index a51c663a..5293309e 100644 --- a/src/nfc/nfc_adapter.h +++ b/src/nfc/nfc_adapter.h @@ -57,11 +57,15 @@ public: bool IsNDEFListenerSet(); // NFCTag related methods + // attributes std::string TagTypeGetter(int tag_id); bool TagIsSupportedNDEFGetter(int tag_id); unsigned int TagNDEFSizeGetter(int tag_id); NFCTagPropertiesT TagPropertiesGetter(int tag_id); bool TagIsConnectedGetter(int tag_id); + // methods + void TagReadNDEF(int tag_id, const picojson::value& args); + // listeners void SetTagListener(); void UnsetTagListener(); int GetNextTagId(); diff --git a/src/nfc/nfc_api.js b/src/nfc/nfc_api.js index 6b0883ab..298293cb 100644 --- a/src/nfc/nfc_api.js +++ b/src/nfc/nfc_api.js @@ -593,6 +593,37 @@ function NFCTag(tagid) { return native_.getResultObject(result); } + // Function defined here (not outside Tag "constructor" + // because access to internal _my_id variable is needed) + NFCTag.prototype.readNDEF = function() { + + var args = validator_.validateArgs(arguments, [ + { + name : 'readCallback', + type : types_.FUNCTION + }, + { + name : 'errorCallback', + type : types_.FUNCTION, + optional : true, + nullable : true + } + ]); + + native_.call('NFCTag_readNDEF', {'id' : _my_id}, + function(result) { + if (native_.isFailure(result)) { + if(!T_.isNullOrUndefined(args.errorCallback)) { + args.errorCallback(result.error); + } + } else { + var message = new tizen.NDEFMessage(result.records); + args.readCallback(message); + } + }); + + }; + Object.defineProperties(this, { type: { set: function() {}, @@ -622,10 +653,6 @@ function NFCTag(tagid) { }); } -NFCTag.prototype.readNDEF = function() { - -}; - NFCTag.prototype.writeNDEF = function() { }; diff --git a/src/nfc/nfc_instance.cc b/src/nfc/nfc_instance.cc index fb8b677f..bd45c99f 100644 --- a/src/nfc/nfc_instance.cc +++ b/src/nfc/nfc_instance.cc @@ -334,6 +334,16 @@ void NFCInstance::SetExclusiveModeForTransaction( void NFCInstance::ReadNDEF( const picojson::value& args, picojson::object& out) { + int tag_id = (int)args.get("id").get(); + LoggerD("Tag id: %d", tag_id); + + try { + NFCAdapter::GetInstance()->TagReadNDEF(tag_id, args); + ReportSuccess(out); + } + catch(const common::PlatformException& ex) { + ReportError(ex, out); + } } void NFCInstance::WriteNDEF(