[NFC] NFCTag attributes implemented
authorMarcin Kaminski <marcin.ka@samsung.com>
Sat, 27 Dec 2014 13:58:52 +0000 (14:58 +0100)
committerMarcin Kaminski <marcin.ka@samsung.com>
Sat, 27 Dec 2014 17:30:09 +0000 (18:30 +0100)
Added full implementation for following NFCTag attrs:
- type
- isSupportedNDEF
- ndefSize
- properties
If tag is disconnected or core function fails to get
some information then 'undefined' is returned and
it is intentional.

Change-Id: I6887464940651e9a7c0b00b591c2cc23993f36cb
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 4b2f259..6e5ecac 100644 (file)
@@ -645,7 +645,84 @@ bool NFCAdapter::IsNDEFListenerSet() {
 
 
 // NFCTag related functions
-bool NFCAdapter::IsTagConnected(int tag_id) {
+std::string NFCAdapter::TagTypeGetter(int tag_id) {
+
+    LoggerD("Entered");
+
+    nfc_tag_type_e type = NFC_UNKNOWN_TARGET;
+
+    int err = nfc_tag_get_type(m_last_tag_handle, &type);
+    if(NFC_ERROR_NONE != err) {
+        LOGE("Failed to get tag type: %d", err);
+        NFCUtil::throwNFCException(err, "Failed to get tag type");
+    }
+
+    return NFCUtil::toStringNFCTag(type);
+}
+
+bool NFCAdapter::TagIsSupportedNDEFGetter(int tag_id) {
+
+    LoggerD("Entered");
+
+    bool result = false;
+
+    int err = nfc_tag_is_support_ndef(m_last_tag_handle, &result);
+    if(NFC_ERROR_NONE != err) {
+        LOGE("Failed to check if NDEF is supported %d", err);
+        NFCUtil::throwNFCException(err,
+                "Failed to check if NDEF is supported");
+    }
+
+    return result;
+}
+
+unsigned int NFCAdapter::TagNDEFSizeGetter(int tag_id) {
+
+    LoggerD("Entered");
+
+    unsigned int result = 0;
+
+    int err = nfc_tag_get_ndef_size(m_last_tag_handle, &result);
+    if(NFC_ERROR_NONE != err) {
+        LOGE("Failed to get tag NDEF size: %d, %s", err);
+        NFCUtil::throwNFCException(err,
+                "Failed to get tag NDEF size");
+    }
+    return result;
+}
+
+static bool tagPropertiesGetterCb(const char *key,
+        const unsigned char *value,
+        int value_size,
+        void *user_data)
+{
+    if (user_data) {
+        UCharVector tag_info = NFCUtil::toVector(value, value_size);
+        (static_cast<NFCTagPropertiesT*>(user_data))->push_back(
+                std::make_pair(key, tag_info));
+        return true;
+    }
+    return false;
+}
+
+NFCTagPropertiesT NFCAdapter::TagPropertiesGetter(int tag_id) {
+
+    LoggerD("Entered");
+
+    NFCTagPropertiesT result;
+
+    int err = nfc_tag_foreach_information(m_last_tag_handle,
+            tagPropertiesGetterCb, (void*)&result);
+    if(NFC_ERROR_NONE != err) {
+        LoggerE("Error occured while getting NFC properties: %d", err);
+        NFCUtil::throwNFCException(err,
+                "Error occured while getting NFC properties");
+    }
+
+    return result;
+}
+
+bool NFCAdapter::TagIsConnectedGetter(int tag_id) {
 
     LoggerD("Entered");
 
index f6c62b3..a51c663 100644 (file)
@@ -6,10 +6,12 @@
 #define NFC_NFC_ADAPTER_H_
 
 #include "nfc/nfc_instance.h"
+#include "nfc/nfc_util.h"
 
 #include "common/picojson.h"
 #include <memory>
 #include <nfc.h>
+#include <list>
 
 #ifdef APP_CONTROL_SETTING_SUPPORT
 #include <app_control.h>
@@ -20,6 +22,8 @@ namespace nfc {
 
 class NFCInstance;
 
+typedef std::list<std::pair<std::string, UCharVector>> NFCTagPropertiesT;
+
 class NFCAdapter {
 public:
     bool GetPowered();
@@ -53,7 +57,11 @@ public:
     bool IsNDEFListenerSet();
 
 // NFCTag related methods
-    bool IsTagConnected(int tag_id);
+    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);
     void SetTagListener();
     void UnsetTagListener();
     int GetNextTagId();
index c8f7eda..b2b8a51 100644 (file)
@@ -569,7 +569,18 @@ function NFCTag(tagid) {
         if (native_.isFailure(result)) {
             return;
         }
-        return native_.getResultObject(result);
+
+        console.log("Current result: " + result);
+
+        var result_array = new Object();
+        for (var i in result.result) {
+            var current = result.result[i];
+            var keys = Object.keys(current);
+            for (var x in keys) {
+                result_array[keys[x]] = current[keys[x]];
+            }
+        }
+        return result_array;
     }
 
     function IsConnectedGetter() {
index 576d64a..cf445c9 100644 (file)
@@ -458,7 +458,7 @@ void NFCInstance::TagTypeGetter(
 
     try {
         // Function below throws exception if core API call fails
-        if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+        if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
             LoggerE("Tag with id %d is not connected anymore", tag_id);
             // If tag is not connected then attribute's value
             // should be undefined
@@ -466,9 +466,8 @@ void NFCInstance::TagTypeGetter(
             return;
         }
 
-        // TODO: implement this stub
-        LoggerW("Stub function used!");
-        std::string tag_type = NFCUtil::toStringNFCTag(NFC_UNKNOWN_TARGET);
+        std::string tag_type =
+                NFCAdapter::GetInstance()->TagTypeGetter(tag_id);
 
         ReportSuccess(picojson::value(tag_type), out);
     }
@@ -488,7 +487,7 @@ void NFCInstance::TagIsSupportedNDEFGetter(
 
     try {
         // Function below throws exception if core API call fails
-        if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+        if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
             LoggerE("Tag with id %d is not connected anymore", tag_id);
             // If tag is not connected then attribute's value
             // should be undefined
@@ -496,14 +495,13 @@ void NFCInstance::TagIsSupportedNDEFGetter(
             return;
         }
 
-        // TODO: implement this stub
-        LoggerW("Stub function used!");
-        bool is_supported = true;
+        bool is_supported =
+                NFCAdapter::GetInstance()->TagIsSupportedNDEFGetter(tag_id);
 
         ReportSuccess(picojson::value(is_supported), out);
     }
     catch(const PlatformException& ex) {
-        LoggerE("Failed to check tag connection");
+        LoggerE("Failed to check is NDEF supported");
         ReportError(ex, out);
     }
 
@@ -519,7 +517,7 @@ void NFCInstance::TagNDEFSizeGetter(
 
     try {
         // Function below throws exception if core API call fails
-        if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+        if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
             LoggerE("Tag with id %d is not connected anymore", tag_id);
             // If tag is not connected then attribute's value
             // should be undefined
@@ -527,14 +525,13 @@ void NFCInstance::TagNDEFSizeGetter(
             return;
         }
 
-        // TODO: implement this stub
-        LoggerW("Stub function used!");
-        int ndef_size = 1234;
+        unsigned int ndef_size =
+                NFCAdapter::GetInstance()->TagNDEFSizeGetter(tag_id);
 
         ReportSuccess(picojson::value((double)ndef_size), out);
     }
     catch(const PlatformException& ex) {
-        LoggerE("Failed to check tag connection");
+        LoggerE("Failed to get tag NDEF size");
         ReportError(ex, out);
     }
 
@@ -549,7 +546,7 @@ void NFCInstance::TagPropertiesGetter(
     LoggerD("Tag id: %d", tag_id);
     try {
         // Function below throws exception if core API call fails
-        if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+        if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id)) {
             LoggerE("Tag with id %d is not connected anymore", tag_id);
             // If tag is not connected then attribute's value
             // should be undefined
@@ -557,12 +554,30 @@ void NFCInstance::TagPropertiesGetter(
             return;
         }
 
-        // TODO: implement this stub
-        LoggerW("Stub function used!");
-        ReportSuccess(out);
+        NFCTagPropertiesT result =
+                NFCAdapter::GetInstance()->TagPropertiesGetter(tag_id);
+
+        picojson::value properties = picojson::value(picojson::array());
+        picojson::array& properties_array = properties.get<picojson::array>();
+        for (auto it = result.begin() ; it != result.end(); it++) {
+            picojson::value val = picojson::value(picojson::object());
+            picojson::object& obj = val.get<picojson::object>();
+
+            picojson::value value_vector = picojson::value(picojson::array());
+            picojson::array& value_vector_obj = value_vector.get<picojson::array>();
+
+            for (size_t i = 0 ; i < it->second.size(); i++) {
+                value_vector_obj.push_back(picojson::value(
+                        std::to_string(it->second[i])));
+            }
+
+            obj.insert(std::make_pair(it->first, value_vector));
+            properties_array.push_back(val);
+        }
+        ReportSuccess(properties, out);
     }
     catch(const PlatformException& ex) {
-        LoggerE("Failed to check tag connection");
+        LoggerE("Failed to tag properties");
         ReportError(ex, out);
     }
 }
@@ -575,7 +590,7 @@ void NFCInstance::TagIsConnectedGetter(
     int tag_id = (int)args.get("id").get<double>();
     LoggerD("Tag id: %d", tag_id);
     try {
-        bool connected = NFCAdapter::GetInstance()->IsTagConnected(tag_id);
+        bool connected = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id);
         ReportSuccess(picojson::value(connected), out);
     }
     catch(const PlatformException& ex) {