[NFC] Adapter::cardEmulationMode implementation
authorMarcin Kaminski <marcin.ka@samsung.com>
Tue, 16 Dec 2014 18:09:29 +0000 (19:09 +0100)
committerMarcin Kaminski <marcin.ka@samsung.com>
Wed, 17 Dec 2014 14:04:07 +0000 (15:04 +0100)
Support for cardEmulationMode attribute of NFCAdaper added.

Change-Id: Ifc700942695702326387b662a596ed96bcf1d0d7
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
src/nfc/nfc_instance.h
src/nfc/nfc_util.cc

index 1e0670c349c0bb07ac7d1d682cc235592ef7b68b..31c5111a18057e79ad4e8d182b201d27c0553c8f 100644 (file)
@@ -188,5 +188,56 @@ void NFCAdapter::SetPowered(const picojson::value& args) {
 }
 
 
+std::string NFCAdapter::GetCardEmulationMode() {
+
+    LoggerD("Entered");
+
+    nfc_se_card_emulation_mode_type_e mode;
+    int ret = nfc_se_get_card_emulation_mode(&mode);
+
+    if (NFC_ERROR_NONE != ret) {
+        LoggerE("Failed to get card emulation mode %d", ret);
+        NFCUtil::throwNFCException(ret, "Failed to get card emulation mode");
+    }
+
+    return NFCUtil::toStringCardEmulationMode(mode);
+}
+
+void NFCAdapter::SetCardEmulationMode(std::string mode) {
+
+    LoggerD("Entered");
+
+    nfc_se_card_emulation_mode_type_e newmode =
+            NFCUtil::toCardEmulationMode(mode);
+    std::string current_mode = GetCardEmulationMode();
+
+    if (mode.compare(current_mode) == 0) {
+        LoggerD("Card emulation mode already set to given value (%s)",
+                mode.c_str());
+        return;
+    }
+
+    int ret = NFC_ERROR_NONE;
+    switch (newmode) {
+        case NFC_SE_CARD_EMULATION_MODE_OFF:
+            ret = nfc_se_disable_card_emulation();
+            break;
+        case NFC_SE_CARD_EMULATION_MODE_ON:
+            ret = nfc_se_enable_card_emulation();
+            break;
+        default:
+            // Should never go here - in case of invalid mode
+            // exception is thrown from convertert few lines above
+            LoggerE("Invalid card emulation mode: %s", mode.c_str());
+            throw InvalidValuesException("Invalid card emulation mode given");
+    }
+
+    if (NFC_ERROR_NONE != ret) {
+        LoggerE("Failed to set card emulation mode %d", ret);
+        NFCUtil::throwNFCException(ret, "Failed to set card emulation mode");
+    }
+}
+
+
 }// nfc
 }// extension
index e4294e430cf828a652596cf883d437d58c22229a..11e9dbf69955322d1c7bb264f86e765fa166ddba 100644 (file)
@@ -24,6 +24,11 @@ public:
     bool GetPowered();
     void SetPowered(const picojson::value& args);
 
+// cardEmulationModer getter and setter
+    std::string GetCardEmulationMode();
+    void SetCardEmulationMode(std::string mode);
+
+
     static NFCAdapter* GetInstance();
     NFCInstance *xwalk_instance;
 private:
index ed6401f6daae9cd915a8c66dd76197decda99ff2..37f8c64a676fb5ed88a12307359e5b4facf711a6 100644 (file)
@@ -98,14 +98,42 @@ function NFCAdapter() {
         return native_.getResultObject(ret);
     }
 
+    function cardEmulationModeGetter() {
+        var result = native_.callSync('NFCAdapter_cardEmulationModeGetter');
+
+        if (native_.isFailure(result)) {
+            throw new tizen.WebAPIException(0, result.error.message, result.error.name);
+        }
+
+        return native_.getResultObject(result);
+    }
+
+    function cardEmulationModeSetter(cem) {
+        // "NFCAdapter_cardEmulationModeSetter"
+
+        var args = validator_.validateArgs(arguments, [
+            {name: 'emulationMode', type: types_.STRING}
+        ]);
+
+        var result = native_.callSync(
+            'NFCAdapter_cardEmulationModeSetter',
+            { 'emulationMode': args.emulationMode}
+        );
+
+        if(native_.isFailure(result)) {
+            throw new tizen.WebAPIException(0, result.error.message, result.error.name);
+        }
+        return;
+    }
+
     Object.defineProperties(this, {
         powered:   {enumerable: true,
             set : function(){},
             get : poweredGetter
         },
         cardEmulationMode:   {enumerable: true,
-            set : function(){},
-            get : function(){}
+            set : cardEmulationModeSetter,
+            get : cardEmulationModeGetter
         },
         activeSecureElement:   {enumerable: true,
             set : function(){},
index a526af060e33c9e82dd124d17f4aedfe30ae0e52..b78a96c9c946c8e42de9f911f1b5a8ced8ddb066 100644 (file)
@@ -2,7 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "nfc/nfc_instance.h"
+#include "nfc_instance.h"
+#include "nfc_util.h"
 
 #include "common/picojson.h"
 #include "common/logger.h"
@@ -30,6 +31,8 @@ NFCInstance::NFCInstance() {
     REGISTER_SYNC("NFCManager_getDefaultAdapter", GetDefaultAdapter);
     REGISTER_SYNC("NFCManager_setExclusiveMode", SetExclusiveMode);
     REGISTER_SYNC("NFCAdapter_getPowered", GetPowered);
+    REGISTER_SYNC("NFCAdapter_cardEmulationModeSetter", CardEmulationModeSetter);
+    REGISTER_SYNC("NFCAdapter_cardEmulationModeGetter", CardEmulationModeGetter);
     REGISTER_SYNC("NFCAdapter_setPeerListener", SetPeerListener);
     REGISTER_SYNC("NFCAdapter_setTagListener", SetTagListener);
     REGISTER_SYNC("NFCAdapter_setPeerListener", SetPeerListener);
@@ -112,31 +115,11 @@ void NFCInstance::SetExclusiveMode(
     }
 
     if (NFC_ERROR_NONE != ret) {
-        LoggerE("setExclusiveModeForTransaction failed: %d", ret);
-        switch(ret) {
-            case NFC_ERROR_SECURITY_RESTRICTED:
-            {
-                auto ex = common::SecurityException("Not allowed to set exclusive mode");
-                ReportError(ex, out);
-                break;
-            }
-            case NFC_ERROR_OPERATION_FAILED:
-            {
-                auto ex = common::UnknownException("Setting exclusive mode failed (IPC fail)");
-                ReportError(ex, out);
-                break;
-            }
-            default:
-            {
-                auto ex = common::UnknownException("Unkown error");
-                ReportError(ex, out);
-                break;
-            }
-        }
-    }
-    else {
-        ReportSuccess(out);
+        LoggerE("setExclusiveMode() failed: %d", ret);
+        NFCUtil::throwNFCException(ret, "Failed to set exclusie mode");
     }
+    ReportSuccess(out);
+
 }
 
 void NFCInstance::SetPowered(
@@ -150,6 +133,32 @@ void NFCInstance::GetPowered(
     ReportSuccess(picojson::value(ret), out);
 }
 
+void NFCInstance::CardEmulationModeSetter(
+        const picojson::value& args, picojson::object& out) {
+
+    std::string mode = args.get("emulationMode").get<std::string>();
+    try {
+        NFCAdapter::GetInstance()->SetCardEmulationMode(mode);
+    }
+    catch(const common::PlatformException& ex) {
+        ReportError(ex, out);
+    }
+    ReportSuccess(out);
+}
+
+void NFCInstance::CardEmulationModeGetter(
+        const picojson::value& args, picojson::object& out) {
+
+    std::string mode;
+    try {
+        mode = NFCAdapter::GetInstance()->GetCardEmulationMode();
+    }
+    catch(const common::PlatformException& ex) {
+        ReportError(ex, out);
+    }
+    ReportSuccess(picojson::value(mode), out);
+}
+
 void NFCInstance::SetTagListener(
         const picojson::value& args, picojson::object& out) {
 
index 9481fdce1b3a6a998db9d98487dd5ced63c589fa..5c56074221284b99544f5013e5371fb4cbcf59de 100644 (file)
@@ -28,6 +28,8 @@ private:
     void SetExclusiveMode(const picojson::value& args, picojson::object& out);
     void SetPowered(const picojson::value& args, picojson::object& out);
     void GetPowered(const picojson::value& args, picojson::object& out);
+    void CardEmulationModeSetter(const picojson::value& args, picojson::object& out);
+    void CardEmulationModeGetter(const picojson::value& args, picojson::object& out);
     void SetTagListener(const picojson::value& args, picojson::object& out);
     void SetPeerListener(const picojson::value& args, picojson::object& out);
     void UnsetTagListener(const picojson::value& args, picojson::object& out);
index 290a74090d57f0d61f26f7d0b0c53bed36965bce..ed7f6a7fabf5158d2ca0a6d6c62e5b0389254f7c 100644 (file)
@@ -30,6 +30,7 @@ void NFCUtil::throwNFCException(const int errorCode, const char* message)
             throw InvalidValuesException(message);
             break;
         case NFC_ERROR_SECURITY_RESTRICTED:
+        case NFC_ERROR_PERMISSION_DENIED:
             throw SecurityException(message);
             break;
         case NFC_ERROR_NOT_ACTIVATED: