Modify terminal interface 28/19228/1 accepted/tizen_3.0.2014.q3_common accepted/tizen_3.0.m14.3_ivi accepted/tizen_generic tizen_3.0.2014.q3_common tizen_3.0.2014.q4_common tizen_3.0.2015.q1_common tizen_3.0.2015.q2_common tizen_3.0.m14.2_ivi tizen_3.0.m14.3_ivi tizen_3.0_ivi accepted/tizen/generic/20140415.185223 accepted/tizen/ivi/20140410.192456 accepted/tizen/mobile/20140702.071818 submit/tizen/20140409.110619 submit/tizen_mobile/20141120.000000 tizen_3.0.2014.q3_common_release tizen_3.0.m14.2_ivi_release tizen_3.0.m14.3_ivi_release tizen_3.0_ivi_release
authorWonkyu Kwon <wonkyu.kwon@samsung.com>
Wed, 16 Oct 2013 04:12:33 +0000 (13:12 +0900)
committerJi-hoon Jung <jh8801.jung@samsung.com>
Wed, 9 Apr 2014 06:09:16 +0000 (15:09 +0900)
 - implement open/close function and
   apply reference counter
 - remove scope locks because functions are synchronous type.

Change-Id: I9b9ac651fc8d46e931676a5b660232dedd077477
Signed-off-by: Ji-hoon Jung <jh8801.jung@samsung.com>
NFCTerminal.cpp
include/NFCTerminal.h
packaging/smartcard-plugin-nfc.spec

index fdb67cb..9fa9110 100644 (file)
@@ -22,6 +22,7 @@
 #include <glib.h>
 
 /* local header */
+#include "smartcard-types.h"
 #include "Debug.h"
 #include "TerminalInterface.h"
 #include "NFCTerminal.h"
@@ -76,15 +77,12 @@ EXPORT_API void destroy_instance(void *instance)
 
 namespace smartcard_service_api
 {
-       NFCTerminal::NFCTerminal() : Terminal(),
-       seHandle(NULL), opening(false), closed(true)
+       NFCTerminal::NFCTerminal() : Terminal(), seHandle(NULL),
+               present(false), referred(0)
        {
                name = (char *)se_name;
 
-               if (initialize())
-               {
-                       open();
-               }
+               initialize();
        }
 
        NFCTerminal *NFCTerminal::getInstance()
@@ -110,6 +108,11 @@ namespace smartcard_service_api
                if (ret == NFC_ERROR_NONE)
                {
                        initialized = true;
+
+                       if (open() == true) {
+                               present = true;
+                               close();
+                       }
                }
                else
                {
@@ -123,55 +126,47 @@ namespace smartcard_service_api
        {
                int ret;
 
-               if (isInitialized() && isClosed() == false && seHandle != NULL)
-               {
-                       close();
+               if (isClosed() == false) {
+                       /* close now */
+                       ret = nfc_se_close_secure_element(seHandle);
+                       if (ret == NFC_ERROR_NONE) {
+                               seHandle = NULL;
+                               closed = true;
+                               referred = 0;
+                       } else {
+                               _ERR("nfc_se_close_secure_element failed [%d]", ret);
+                       }
                }
 
                ret = nfc_manager_deinitialize();
-               if (ret != NFC_ERROR_NONE)
-               {
+               if (ret == NFC_ERROR_NONE) {
+                       initialized = false;
+               } else {
                        _ERR("nfc_manager_deinitialize failed [%d]", ret);
                }
        }
 
-       bool NFCTerminal::checkClosed()
-       {
-               bool result = false;
-
-               SCOPE_LOCK(mutex)
-               {
-                       result = (isInitialized() == true &&
-                                       isClosed() == true && opening == false);
-
-                       if (result == true) {
-                               opening = true;
-                       }
-               }
-
-               return result;
-       }
-
        bool NFCTerminal::open()
        {
                int ret;
 
                _BEGIN();
 
-               if (checkClosed() == true)
-               {
-                       ret = nfc_se_open_secure_element(NFC_SE_TYPE_ESE,
+               if (isInitialized()) {
+                       if (referred == 0) {
+                               ret = nfc_se_open_secure_element(NFC_SE_TYPE_ESE,
                                        &seHandle);
-                       if (ret == NFC_ERROR_NONE)
-                       {
-                               closed = false;
-                       }
-                       else
-                       {
-                               _ERR("net_nfc_client_se_open_internal_secure_element_sync failed [%d]", ret);
+                               if (ret == NFC_ERROR_NONE) {
+                                       closed = false;
+                                       referred++;
+                               } else {
+                                       _ERR("nfc_se_open_secure_element failed [%d]", ret);
+                               }
+                       } else {
+                               referred++;
                        }
 
-                       opening = false;
+                       _DBG("reference count [%d]", referred);
                }
 
                _END();
@@ -185,29 +180,30 @@ namespace smartcard_service_api
 
                _BEGIN();
 
-               if (isInitialized() && isClosed() == false)
+               if (isInitialized())
                {
-                       ret = nfc_se_close_secure_element(seHandle);
-                       if (ret == NFC_ERROR_NONE) {
-                       } else  {
-                               _ERR("net_nfc_client_se_close_internal_secure_element_sync failed [%d]", ret);
+                       if (referred <= 1) {
+                               ret = nfc_se_close_secure_element(seHandle);
+                               if (ret == NFC_ERROR_NONE) {
+                                       seHandle = NULL;
+                                       closed = true;
+                                       referred = 0;
+                               } else {
+                                       _ERR("nfc_se_close_secure_element failed [%d]", ret);
+                               }
+                       } else {
+                               referred--;
                        }
 
-                       seHandle = NULL;
-                       closed = true;
+                       _DBG("reference count [%d]", referred);
                }
 
                _END();
        }
 
-       bool NFCTerminal::isClosed() const
-       {
-               return closed;
-       }
-
        int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response)
        {
-               int rv = -1;
+               int rv = SCARD_ERROR_NOT_INITIALIZED;
 
                _BEGIN();
 
@@ -215,28 +211,25 @@ namespace smartcard_service_api
                {
                        if (command.size() > 0)
                        {
-                               SCOPE_LOCK(mutex)
+                               uint8_t *resp = NULL;
+                               uint32_t resp_len;
+
+                               rv = nfc_se_send_apdu(seHandle,
+                                       (uint8_t *)command.getBuffer(),
+                                       command.size(),
+                                       &resp,
+                                       &resp_len);
+                               if (rv == NFC_ERROR_NONE &&
+                                       resp != NULL)
                                {
-                                       uint8_t *resp = NULL;
-                                       uint32_t resp_len;
-
-                                       rv = nfc_se_send_apdu(seHandle,
-                                                       (uint8_t *)command.getBuffer(),
-                                                       command.size(),
-                                                       &resp,
-                                                       &resp_len);
-                                       if (rv == NFC_ERROR_NONE &&
-                                                       resp != NULL)
-                                       {
-                                               response.assign(resp, resp_len);
-
-                                               g_free(resp);
-                                       }
-                                       else
-                                       {
-                                               _ERR("net_nfc_send_apdu_sync failed, [%d]", rv);
-                                       }
+                                       response.assign(resp, resp_len);
+
+                                       g_free(resp);
                                }
+                               else
+                               {
+                                       _ERR("net_nfc_send_apdu_sync failed, [%d]", rv);
+                               }
                        }
                        else
                        {
@@ -261,23 +254,18 @@ namespace smartcard_service_api
 
                if (isClosed() == false)
                {
-                       SCOPE_LOCK(mutex)
-                       {
-                               uint8_t *temp = NULL;
-                               uint32_t temp_len;
-
-                               rv = nfc_se_get_atr(seHandle, &temp, &temp_len);
-                               if (rv == NFC_ERROR_NONE &&
-                                               temp != NULL)
-                               {
-                                       atr.assign(temp, temp_len);
+                       uint8_t *temp = NULL;
+                       uint32_t temp_len;
 
-                                       g_free(temp);
-                               }
-                               else
-                               {
-                                       _ERR("net_nfc_client_se_get_atr_sync failed");
-                               }
+                       rv = nfc_se_get_atr(seHandle, &temp, &temp_len);
+                       if (rv == NFC_ERROR_NONE && temp != NULL)
+                       {
+                               atr.assign(temp, temp_len);
+                               g_free(temp);
+                       }
+                       else
+                       {
+                               _ERR("net_nfc_client_se_get_atr_sync failed");
                        }
                }
                else
@@ -289,9 +277,4 @@ namespace smartcard_service_api
 
                return rv;
        }
-
-       bool NFCTerminal::isSecureElementPresence() const
-       {
-               return (isClosed() == false);
-       }
 } /* namespace smartcard_service_api */
index a2209d8..d61b5e6 100644 (file)
@@ -32,14 +32,12 @@ namespace smartcard_service_api
        private:
                PMutex mutex;
                nfc_se_h seHandle;
-               bool opening;
-               bool closed;
+               bool present;
+               int referred;
 
                NFCTerminal();
                ~NFCTerminal();
 
-               bool checkClosed();
-
        public:
                static NFCTerminal *getInstance();
 
@@ -47,16 +45,15 @@ namespace smartcard_service_api
                void finalize();
 
                bool open();
-               bool isClosed() const;
                void close();
 
-               bool isSecureElementPresence() const;
+               bool isSecureElementPresence() const { return present; }
 
                int transmitSync(const ByteArray &command, ByteArray &response);
                int getATRSync(ByteArray &atr);
 
-               int transmit(const ByteArray &command, terminalTransmitCallback callback, void *userParam) { return -1; };
-               int getATR(terminalGetATRCallback callback, void *userParam) { return -1; }
+               int transmit(const ByteArray &command, terminalTransmitCallback callback, void *userParam) { return SCARD_ERROR_NOT_SUPPORTED; };
+               int getATR(terminalGetATRCallback callback, void *userParam) { return SCARD_ERROR_NOT_SUPPORTED; }
        };
 } /* namespace smartcard_service_api */
 #endif /* NFCTERMINAL_H_ */
index 437a473..685a5ab 100755 (executable)
@@ -1,6 +1,6 @@
 Name:             smartcard-plugin-nfc
 Summary:          Smartcard plugin nfc
-Version:          0.0.9
+Version:          0.0.10
 Release:          0
 Group:            Network & Connectivity/NFC
 License:          Apache-2.0