diff options
Diffstat (limited to 'NFCTerminal.cpp')
-rw-r--r-- | NFCTerminal.cpp | 419 |
1 files changed, 139 insertions, 280 deletions
diff --git a/NFCTerminal.cpp b/NFCTerminal.cpp index f47a4b9..fdb67cb 100644 --- a/NFCTerminal.cpp +++ b/NFCTerminal.cpp @@ -1,25 +1,25 @@ /* -* Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - + * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ /* standard library header */ -#include <stdio.h> +#include <cstdio> +#include <cstring> #include <unistd.h> -#include <string.h> #include <sys/time.h> +#include <glib.h> /* local header */ #include "Debug.h" @@ -45,42 +45,45 @@ void __attribute__ ((destructor)) lib_fini() { } -/* below trhee functions must be implemented */ -extern "C" EXPORT_API const char *get_name() +/* below three functions must be implemented */ +extern "C" +{ +EXPORT_API const char *get_name() { return se_name; } -extern "C" EXPORT_API void *create_instance() +EXPORT_API void *create_instance() { return (void *)NFCTerminal::getInstance(); } -extern "C" EXPORT_API void destroy_instance(void *instance) +EXPORT_API void destroy_instance(void *instance) { NFCTerminal *inst = (NFCTerminal *)instance; + if (inst == NFCTerminal::getInstance()) { inst->finalize(); } else { - SCARD_DEBUG_ERR("instance is invalid : getInstance [%p], instance [%p]", NFCTerminal::getInstance(), instance); + _ERR("instance is invalid : getInstance [%p], instance [%p]", + NFCTerminal::getInstance(), instance); } } +} namespace smartcard_service_api { - NFCTerminal::NFCTerminal():Terminal() + NFCTerminal::NFCTerminal() : Terminal(), + seHandle(NULL), opening(false), closed(true) { - seHandle = NULL; - closed = true; name = (char *)se_name; if (initialize()) { - /* TODO : disable nfc library temporary */ -// open(); + open(); } } @@ -98,29 +101,19 @@ namespace smartcard_service_api bool NFCTerminal::initialize() { - int ret = 0; + int ret; - if (initialized == false) - { -#if 0 - if ((ret = net_nfc_client_initialize()) == NET_NFC_OK) - { - if ((ret = net_nfc_set_response_callback(&NFCTerminal::nfcResponseCallback, this)) == NET_NFC_OK) - { - SCARD_DEBUG("nfc initialize success"); + if (initialized == true) + return initialized; - initialized = true; - } - else - { - SCARD_DEBUG_ERR("net_nfc_set_response_callback failed [%d]", ret); - } - } - else - { - SCARD_DEBUG_ERR("net_nfc_initialize failed [%d]", ret); - } -#endif + ret = nfc_manager_initialize_sync(); + if (ret == NFC_ERROR_NONE) + { + initialized = true; + } + else + { + _ERR("net_nfc_initialize failed [%d]", ret); } return initialized; @@ -128,311 +121,177 @@ namespace smartcard_service_api void NFCTerminal::finalize() { + int ret; + if (isInitialized() && isClosed() == false && seHandle != NULL) { - net_nfc_client_se_close_internal_secure_element_sync(seHandle); + close(); + } + + ret = nfc_manager_deinitialize(); + if (ret != NFC_ERROR_NONE) + { + _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; + } } - net_nfc_client_deinitialize(); + return result; } bool NFCTerminal::open() { - bool result = true; - net_nfc_error_e ret; + int ret; - SCARD_BEGIN(); + _BEGIN(); - if (isClosed() == true) + if (checkClosed() == true) { -#if 0 - if ((ret = net_nfc_open_internal_secure_element(NET_NFC_SE_TYPE_ESE, this)) == NET_NFC_OK) + ret = nfc_se_open_secure_element(NFC_SE_TYPE_ESE, + &seHandle); + if (ret == NFC_ERROR_NONE) { -#ifndef ASYNC - int rv; - syncLock(); - if ((rv = waitTimedCondition(3)) == 0 && error == NET_NFC_OK) - { -#endif - SCARD_DEBUG("net_nfc_open_internal_secure_element returns [%d]", ret); -#ifndef ASYNC - } - else - { - SCARD_DEBUG_ERR("net_nfc_open_internal_secure_element failed cbResult [%d], rv [%d]", error, rv); - result = false; - } - syncUnlock(); -#endif + closed = false; } else { - SCARD_DEBUG_ERR("net_nfc_set_secure_element_type failed [%d]", ret); - result = false; + _ERR("net_nfc_client_se_open_internal_secure_element_sync failed [%d]", ret); } -#endif + + opening = false; } - SCARD_END(); + _END(); - return result; + return (isClosed() == false); } void NFCTerminal::close() { - net_nfc_error_e ret; + int ret; - SCARD_BEGIN(); + _BEGIN(); - if (isInitialized() && isClosed() == false && seHandle != NULL) + if (isInitialized() && isClosed() == false) { - if ((ret = net_nfc_client_se_close_internal_secure_element_sync(seHandle)) == NET_NFC_OK) - { -#ifndef ASYNC - int rv; - - syncLock(); - if ((rv = waitTimedCondition(3)) == 0 && error == NET_NFC_OK) - { -#endif - SCARD_DEBUG("net_nfc_close_internal_secure_element returns [%d]", ret); -#ifndef ASYNC - } - else - { - SCARD_DEBUG_ERR("net_nfc_close_internal_secure_element failed, error [%d], rv [%d]", error, rv); - } - syncUnlock(); -#endif - } - else - { - SCARD_DEBUG_ERR("net_nfc_close_internal_secure_element failed [%d]", ret); + 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); } + + seHandle = NULL; + closed = true; } - SCARD_END(); + _END(); } - bool NFCTerminal::isClosed() + bool NFCTerminal::isClosed() const { return closed; } - int NFCTerminal::transmitSync(ByteArray command, ByteArray &response) + int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response) { - int rv = 0; - data_h data; + int rv = -1; - SCARD_BEGIN(); + _BEGIN(); if (isClosed() == false) { - SCOPE_LOCK(mutex) + if (command.size() > 0) { - if (command.getLength() > 0) + SCOPE_LOCK(mutex) { - SCARD_DEBUG("command : %s", command.toString()); - -#ifndef ASYNC - response.releaseBuffer(); -#endif - net_nfc_create_data(&data, command.getBuffer(), command.getLength()); - net_nfc_client_se_send_apdu_sync(seHandle, data, NULL); -#ifndef ASYNC - syncLock(); - rv = waitTimedCondition(3); - - if (rv == 0 && error == NET_NFC_OK) + 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) { - SCARD_DEBUG("transmit success, length [%d]", response.getLength()); + response.assign(resp, resp_len); + + g_free(resp); } else { - SCARD_DEBUG_ERR("transmit failed, rv [%d], cbResult [%d]", rv, error); + _ERR("net_nfc_send_apdu_sync failed, [%d]", rv); } - syncUnlock(); - - rv = error; -#endif - net_nfc_free_data(data); - } - else - { - rv = -1; } } + else + { + _ERR("invalid command"); + } } else { - rv = -1; + _ERR("closed..."); } - SCARD_END(); + _END(); return rv; } int NFCTerminal::getATRSync(ByteArray &atr) { - int rv = 0; + int rv = -1; - SCARD_BEGIN(); + _BEGIN(); - SCOPE_LOCK(mutex) - { - /* TODO : implement nfc first */ - } - - SCARD_END(); - - return rv; - } - - bool NFCTerminal::isSecureElementPresence() - { - return (seHandle != NULL); - } - - void NFCTerminal::nfcResponseCallback(net_nfc_message_e message, net_nfc_error_e result, void *data , void *userContext, void *transData) - { - NFCTerminal *instance = (NFCTerminal *)userContext; - - SCARD_BEGIN(); - - if (instance == NULL) - { - SCARD_DEBUG_ERR("instance is null"); - return; - } - - switch(message) + if (isClosed() == false) { - case NET_NFC_MESSAGE_SET_SE : - SCARD_DEBUG("NET_NFC_MESSAGE_SET_SE"); - break; - - case NET_NFC_MESSAGE_GET_SE : - SCARD_DEBUG("NET_NFC_MESSAGE_GET_SE"); - break; - - case NET_NFC_MESSAGE_OPEN_INTERNAL_SE : - SCARD_DEBUG("NET_NFC_MESSAGE_OPEN_INTERNAL_SE"); - - if (result == NET_NFC_OK) - { - if (data != NULL) - { - instance->seHandle = (net_nfc_target_handle_h)data; - instance->closed = false; - } - else - { - SCARD_DEBUG_ERR("NET_NFC_MESSAGE_OPEN_INTERNAL_SE failed"); - } - } - else - { - SCARD_DEBUG_ERR("NET_NFC_MESSAGE_OPEN_INTERNAL_SE returns error [%d]", result); - } - - instance->error = result; - -#ifndef ASYNC - instance->syncLock(); - instance->signalCondition(); - instance->syncUnlock(); -#else - /* TODO : async process */ -#endif - break; - - case NET_NFC_MESSAGE_CLOSE_INTERNAL_SE : - SCARD_DEBUG("NET_NFC_MESSAGE_CLOSE_INTERNAL_SE"); - - if (result == NET_NFC_OK) - { - instance->closed = true; - } - else - { - SCARD_DEBUG_ERR("NET_NFC_MESSAGE_CLOSE_INTERNAL_SE failed [%d]", result); - } - - instance->error = result; - -#ifndef ASYNC - instance->syncLock(); - instance->signalCondition(); - instance->syncUnlock(); -#else - /* TODO : async process */ -#endif - break; - - case NET_NFC_MESSAGE_SEND_APDU_SE : + SCOPE_LOCK(mutex) { - data_h resp = (data_h)data; - - SCARD_DEBUG("NET_NFC_MESSAGE_SEND_APDU_SE"); + uint8_t *temp = NULL; + uint32_t temp_len; - if (result == NET_NFC_OK) + rv = nfc_se_get_atr(seHandle, &temp, &temp_len); + if (rv == NFC_ERROR_NONE && + temp != NULL) { - if (resp != NULL) - { - SCARD_DEBUG("apdu result length [%d]", net_nfc_get_data_length(resp)); - instance->response.setBuffer(net_nfc_get_data_buffer(resp), net_nfc_get_data_length(resp)); - } + atr.assign(temp, temp_len); + + g_free(temp); } else { - SCARD_DEBUG_ERR("NET_NFC_MESSAGE_OPEN_INTERNAL_SE failed [%d]", result); - } - - instance->error = result; - -#ifndef ASYNC - instance->syncLock(); - instance->signalCondition(); - instance->syncUnlock(); -#else - /* TODO : async process */ -#endif - } - break; - - case NET_NFC_MESSAGE_INIT : - { - instance->finalize(); - - /* send notification */ - if (instance->statusCallback != NULL) - { - instance->statusCallback((void *)se_name, NOTIFY_SE_AVAILABLE, 0, NULL); + _ERR("net_nfc_client_se_get_atr_sync failed"); } } - break; + } + else + { + _ERR("closed..."); + } - case NET_NFC_MESSAGE_DEINIT : - { - instance->initialize(); - if (instance->open() == true) - { - /* send notification */ - if (instance->statusCallback != NULL) - { - instance->statusCallback((void *)se_name, NOTIFY_SE_NOT_AVAILABLE, 0, NULL); - } - } - } - break; + _END(); - default: - SCARD_DEBUG("unknown message : [%d], [%d], [%p], [%p], [%p]", message, result, data, userContext, instance); - break; - } + return rv; + } - SCARD_END(); + bool NFCTerminal::isSecureElementPresence() const + { + return (isClosed() == false); } } /* namespace smartcard_service_api */ - |