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 */
24 /* SLP library header */
26 #include "nfc_internal.h"
29 #include "smartcard-types.h"
31 #include "TerminalInterface.h"
32 #include "NFCTerminal.h"
35 #define EXPORT_API __attribute__((visibility("default")))
40 using namespace smartcard_service_api;
42 static const char *se_name = "eSE";
44 /* below functions will be called when dlopen or dlclose is called */
45 void __attribute__ ((constructor)) lib_init()
49 void __attribute__ ((destructor)) lib_fini()
53 /* below three functions must be implemented */
56 EXPORT_API const char *get_name()
61 EXPORT_API void *create_instance()
63 return (void *)NFCTerminal::getInstance();
66 EXPORT_API void destroy_instance(void *instance)
68 NFCTerminal *inst = (NFCTerminal *)instance;
70 if (inst == NFCTerminal::getInstance())
76 _ERR("instance is invalid : getInstance [%p], instance [%p]",
77 NFCTerminal::getInstance(), instance);
82 namespace smartcard_service_api
84 NFCTerminal::NFCTerminal() : Terminal(), seHandle(NULL),
85 present(false), referred(0)
87 name = (char *)se_name;
92 NFCTerminal *NFCTerminal::getInstance()
94 static NFCTerminal instance;
99 NFCTerminal::~NFCTerminal()
104 bool NFCTerminal::initialize()
108 if (initialized == true)
111 ret = nfc_manager_initialize();
112 if (ret == NFC_ERROR_NONE)
116 if (open() == true) {
123 _ERR("net_nfc_initialize failed [%d]", ret);
129 void NFCTerminal::finalize()
133 if (isClosed() == false) {
135 ret = nfc_se_close_secure_element_internal(seHandle);
136 if (ret == NFC_ERROR_NONE) {
141 _ERR("nfc_se_close_secure_element failed [%d]", ret);
145 ret = nfc_manager_deinitialize();
146 if (ret == NFC_ERROR_NONE) {
149 _ERR("nfc_manager_deinitialize failed [%d]", ret);
153 bool NFCTerminal::open()
159 if (isInitialized()) {
161 ret = nfc_se_open_secure_element_internal(NFC_SE_TYPE_ESE,
163 if (ret == NFC_ERROR_NONE) {
167 _ERR("nfc_se_open_secure_element failed [%d]", ret);
173 _DBG("reference count [%d]", referred);
178 return (isClosed() == false);
181 void NFCTerminal::close()
190 ret = nfc_se_close_secure_element_internal(seHandle);
191 if (ret == NFC_ERROR_NONE) {
196 _ERR("nfc_se_close_secure_element failed [%d]", ret);
202 _DBG("reference count [%d]", referred);
208 int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response)
210 int rv = SCARD_ERROR_NOT_INITIALIZED;
214 if (isClosed() == false)
216 if (command.size() > 0)
218 uint8_t *resp = NULL;
221 rv = nfc_se_send_apdu_internal(seHandle,
222 (uint8_t *)command.getBuffer(),
226 if (rv == NFC_ERROR_NONE &&
229 response.assign(resp, resp_len);
235 _ERR("net_nfc_send_apdu_sync failed, [%d]", rv);
240 _ERR("invalid command");
253 int NFCTerminal::getATRSync(ByteArray &atr)
259 if (isClosed() == false)
261 uint8_t *temp = NULL;
264 rv = nfc_se_get_atr_internal(seHandle, &temp, &temp_len);
265 if (rv == NFC_ERROR_NONE &&
268 atr.assign(temp, temp_len);
274 _ERR("net_nfc_client_se_get_atr_sync failed");
286 } /* namespace smartcard_service_api */