2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* standard library header */
25 #include "smartcard-types.h"
27 #include "TerminalInterface.h"
28 #include "NFCTerminal.h"
31 #define EXPORT_API __attribute__((visibility("default")))
36 using namespace smartcard_service_api;
38 static const char *se_name = "eSE";
40 /* below functions will be called when dlopen or dlclose is called */
41 void __attribute__ ((constructor)) lib_init()
45 void __attribute__ ((destructor)) lib_fini()
49 /* below three functions must be implemented */
52 EXPORT_API const char *get_name()
57 EXPORT_API void *create_instance()
59 return (void *)NFCTerminal::getInstance();
62 EXPORT_API void destroy_instance(void *instance)
64 NFCTerminal *inst = (NFCTerminal *)instance;
66 if (inst == NFCTerminal::getInstance())
72 _ERR("instance is invalid : getInstance [%p], instance [%p]",
73 NFCTerminal::getInstance(), instance);
78 namespace smartcard_service_api
80 NFCTerminal::NFCTerminal() : Terminal(), seHandle(NULL),
81 present(false), referred(0)
83 name = (char *)se_name;
88 NFCTerminal *NFCTerminal::getInstance()
90 static NFCTerminal instance;
95 NFCTerminal::~NFCTerminal()
100 bool NFCTerminal::initialize()
104 if (initialized == true)
107 ret = nfc_manager_initialize_sync();
108 if (ret == NFC_ERROR_NONE)
112 if (open() == true) {
119 _ERR("net_nfc_initialize failed [%d]", ret);
125 void NFCTerminal::finalize()
129 if (isClosed() == false) {
131 ret = nfc_se_close_secure_element(seHandle);
132 if (ret == NFC_ERROR_NONE) {
137 _ERR("nfc_se_close_secure_element failed [%d]", ret);
141 ret = nfc_manager_deinitialize();
142 if (ret == NFC_ERROR_NONE) {
145 _ERR("nfc_manager_deinitialize failed [%d]", ret);
149 bool NFCTerminal::open()
155 if (isInitialized()) {
157 ret = nfc_se_open_secure_element(NFC_SE_TYPE_ESE,
159 if (ret == NFC_ERROR_NONE) {
163 _ERR("nfc_se_open_secure_element failed [%d]", ret);
169 _DBG("reference count [%d]", referred);
174 return (isClosed() == false);
177 void NFCTerminal::close()
186 ret = nfc_se_close_secure_element(seHandle);
187 if (ret == NFC_ERROR_NONE) {
192 _ERR("nfc_se_close_secure_element failed [%d]", ret);
198 _DBG("reference count [%d]", referred);
204 int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response)
206 int rv = SCARD_ERROR_NOT_INITIALIZED;
210 if (isClosed() == false)
212 if (command.size() > 0)
214 uint8_t *resp = NULL;
217 rv = nfc_se_send_apdu(seHandle,
218 (uint8_t *)command.getBuffer(),
222 if (rv == NFC_ERROR_NONE &&
225 response.assign(resp, resp_len);
231 _ERR("net_nfc_send_apdu_sync failed, [%d]", rv);
236 _ERR("invalid command");
249 int NFCTerminal::getATRSync(ByteArray &atr)
255 if (isClosed() == false)
257 uint8_t *temp = NULL;
260 rv = nfc_se_get_atr(seHandle, &temp, &temp_len);
261 if (rv == NFC_ERROR_NONE && temp != NULL)
263 atr.assign(temp, temp_len);
268 _ERR("net_nfc_client_se_get_atr_sync failed");
280 } /* namespace smartcard_service_api */