[NFC] NFCTag::readNDEF implementation added
authorMarcin Kaminski <marcin.ka@samsung.com>
Sat, 27 Dec 2014 17:28:54 +0000 (18:28 +0100)
committerJerzy Pabich <j.pabich@samsung.com>
Mon, 29 Dec 2014 14:06:24 +0000 (23:06 +0900)
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 <marcin.ka@samsung.com>
src/nfc/nfc_adapter.cc
src/nfc/nfc_adapter.h
src/nfc/nfc_api.js
src/nfc/nfc_instance.cc

index 6e5ecace8d06d4b204a0b5d773eaac4ad4646415..444b666994c56e3d2aa9767b0f127f35a652ba82 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "nfc_adapter.h"
 #include "nfc_util.h"
+#include "nfc_message_utils.h"
 
 #include <glib.h>
 
@@ -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<picojson::object>();
+
+    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<double>();
+    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
index a51c663a84277a6fad0d2c1c8105dc906fa303c3..5293309e23a9acd69b3312dffa4db9498dbdf62e 100644 (file)
@@ -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();
index 6b0883abcaeb80992e3ffa8edea2d47813ec89d9..298293cb406984952d881ffc41c5c79cdb35ee6c 100644 (file)
@@ -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() {
 
 };
index fb8b677f5a2e21b9a5adab31463301dd984f81e5..bd45c99f135b005f549a836ace88f72e84a0e2ab 100644 (file)
@@ -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<double>();
+    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(