diff options
-rw-r--r-- | NFCTerminal.cpp | 169 | ||||
-rw-r--r-- | include/NFCTerminal.h | 13 | ||||
-rwxr-xr-x | packaging/smartcard-plugin-nfc.spec | 2 |
3 files changed, 82 insertions, 102 deletions
diff --git a/NFCTerminal.cpp b/NFCTerminal.cpp index fdb67cb..9fa9110 100644 --- a/NFCTerminal.cpp +++ b/NFCTerminal.cpp @@ -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 */ diff --git a/include/NFCTerminal.h b/include/NFCTerminal.h index a2209d8..d61b5e6 100644 --- a/include/NFCTerminal.h +++ b/include/NFCTerminal.h @@ -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_ */ diff --git a/packaging/smartcard-plugin-nfc.spec b/packaging/smartcard-plugin-nfc.spec index 437a473..685a5ab 100755 --- a/packaging/smartcard-plugin-nfc.spec +++ b/packaging/smartcard-plugin-nfc.spec @@ -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 |