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 */
26 #include "TerminalInterface.h"
27 #include "NFCTerminal.h"
30 #define EXPORT_API __attribute__((visibility("default")))
35 using namespace smartcard_service_api;
37 static const char *se_name = "eSE";
39 /* below functions will be called when dlopen or dlclose is called */
40 void __attribute__ ((constructor)) lib_init()
44 void __attribute__ ((destructor)) lib_fini()
48 /* below three functions must be implemented */
51 EXPORT_API const char *get_name()
56 EXPORT_API void *create_instance()
58 return (void *)NFCTerminal::getInstance();
61 EXPORT_API void destroy_instance(void *instance)
63 NFCTerminal *inst = (NFCTerminal *)instance;
65 if (inst == NFCTerminal::getInstance())
71 _ERR("instance is invalid : getInstance [%p], instance [%p]",
72 NFCTerminal::getInstance(), instance);
77 namespace smartcard_service_api
79 NFCTerminal::NFCTerminal() : Terminal(),
80 seHandle(NULL), opening(false), closed(true)
82 name = (char *)se_name;
90 NFCTerminal *NFCTerminal::getInstance()
92 static NFCTerminal instance;
97 NFCTerminal::~NFCTerminal()
102 bool NFCTerminal::initialize()
106 if (initialized == true)
109 ret = nfc_manager_initialize_sync();
110 if (ret == NFC_ERROR_NONE)
116 _ERR("net_nfc_initialize failed [%d]", ret);
122 void NFCTerminal::finalize()
126 if (isInitialized() && isClosed() == false && seHandle != NULL)
131 ret = nfc_manager_deinitialize();
132 if (ret != NFC_ERROR_NONE)
134 _ERR("nfc_manager_deinitialize failed [%d]", ret);
138 bool NFCTerminal::checkClosed()
144 result = (isInitialized() == true &&
145 isClosed() == true && opening == false);
147 if (result == true) {
155 bool NFCTerminal::open()
161 if (checkClosed() == true)
163 ret = nfc_se_open_secure_element(NFC_SE_TYPE_ESE,
165 if (ret == NFC_ERROR_NONE)
171 _ERR("net_nfc_client_se_open_internal_secure_element_sync failed [%d]", ret);
179 return (isClosed() == false);
182 void NFCTerminal::close()
188 if (isInitialized() && isClosed() == false)
190 ret = nfc_se_close_secure_element(seHandle);
191 if (ret == NFC_ERROR_NONE) {
193 _ERR("net_nfc_client_se_close_internal_secure_element_sync failed [%d]", ret);
203 bool NFCTerminal::isClosed() const
208 int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response)
214 if (isClosed() == false)
216 if (command.size() > 0)
220 uint8_t *resp = NULL;
223 rv = nfc_se_send_apdu(seHandle,
224 (uint8_t *)command.getBuffer(),
228 if (rv == NFC_ERROR_NONE &&
231 response.assign(resp, resp_len);
237 _ERR("net_nfc_send_apdu_sync failed, [%d]", rv);
243 _ERR("invalid command");
256 int NFCTerminal::getATRSync(ByteArray &atr)
262 if (isClosed() == false)
266 uint8_t *temp = NULL;
269 rv = nfc_se_get_atr(seHandle, &temp, &temp_len);
270 if (rv == NFC_ERROR_NONE &&
273 atr.assign(temp, temp_len);
279 _ERR("net_nfc_client_se_get_atr_sync failed");
293 bool NFCTerminal::isSecureElementPresence() const
295 return (isClosed() == false);
297 } /* namespace smartcard_service_api */