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()
61 if (vconf_get_bool(VCONFKEY_NFC_ESE_DISABLE, &value) < 0)
64 return value ? NULL : (void *)NFCTerminal::getInstance();
67 EXPORT_API void destroy_instance(void *instance)
69 NFCTerminal *inst = (NFCTerminal *)instance;
71 if (inst == NFCTerminal::getInstance())
77 _ERR("instance is invalid : getInstance [%p], instance [%p]",
78 NFCTerminal::getInstance(), instance);
83 namespace smartcard_service_api
85 NFCTerminal::NFCTerminal() : Terminal(), seHandle(NULL),
86 present(false), referred(0)
88 name = (char *)se_name;
93 NFCTerminal *NFCTerminal::getInstance()
95 static NFCTerminal instance;
100 NFCTerminal::~NFCTerminal()
105 void NFCTerminal::onActivationChanged(bool activated, void *userData)
107 NFCTerminal *instance = (NFCTerminal *)userData;
109 _ERR("nfc state changed [%s]", activated ? "activated" : "deactivated");
111 if (activated == true) {
112 if (instance->present == false) {
113 if (instance->open() == true) {
114 instance->present = true;
117 if (instance->statusCallback != NULL) {
118 instance->statusCallback(
125 _ERR("ese open failed");
131 if (instance->present == true) {
132 instance->present = false;
134 if (instance->isClosed() == false) {
138 ret = nfc_se_close_secure_element_internal(
140 if (ret != NFC_ERROR_NONE) {
141 _ERR("nfc_se_close_secure_element failed [%d]", ret);
144 instance->seHandle = NULL;
145 instance->closed = true;
146 instance->referred = 0;
149 if (instance->statusCallback != NULL) {
150 instance->statusCallback(
152 NOTIFY_SE_NOT_AVAILABLE,
162 bool NFCTerminal::initialize()
166 if (initialized == true)
169 ret = nfc_manager_initialize();
170 if (ret == NFC_ERROR_NONE)
174 ret = nfc_manager_set_activation_changed_cb(
175 &NFCTerminal::onActivationChanged, this);
176 if (ret != NFC_ERROR_NONE) {
177 _ERR("nfc_manager_set_activation_changed_cb failed, [%d]", ret);
180 if (nfc_manager_is_activated() == true) {
181 if (open() == true) {
185 _ERR("ese open failed");
188 _ERR("nfc is not activated.");
193 _ERR("net_nfc_initialize failed [%d]", ret);
199 void NFCTerminal::finalize()
203 if (isClosed() == false) {
205 ret = nfc_se_close_secure_element_internal(seHandle);
206 if (ret != NFC_ERROR_NONE) {
207 _ERR("nfc_se_close_secure_element failed [%d]", ret);
217 nfc_manager_unset_activation_changed_cb();
219 ret = nfc_manager_deinitialize();
220 if (ret == NFC_ERROR_NONE) {
223 _ERR("nfc_manager_deinitialize failed [%d]", ret);
227 bool NFCTerminal::open()
233 if (isInitialized()) {
235 ret = nfc_se_open_secure_element_internal(NFC_SE_TYPE_ESE,
237 if (ret == NFC_ERROR_NONE) {
241 _ERR("nfc_se_open_secure_element_internal failed [%d]", ret);
247 _DBG("reference count [%d]", referred);
252 return (isClosed() == false);
255 void NFCTerminal::close()
266 ret = nfc_se_close_secure_element_internal(seHandle);
267 if (ret == NFC_ERROR_NONE) {
272 _ERR("nfc_se_close_secure_element_internal failed [%d]", ret);
278 _DBG("reference count [%d]", referred);
284 int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response)
286 int rv = SCARD_ERROR_NOT_INITIALIZED;
290 if (isClosed() == false)
292 if (command.size() > 0)
294 uint8_t *resp = NULL;
297 rv = nfc_se_send_apdu_internal(seHandle,
298 (uint8_t *)command.getBuffer(),
302 if (rv == NFC_ERROR_NONE &&
305 response.assign(resp, resp_len);
311 _ERR("nfc_se_send_apdu_internal failed, [%d]", rv);
316 _ERR("invalid command");
329 int NFCTerminal::getATRSync(ByteArray &atr)
335 if (isClosed() == false)
337 uint8_t *temp = NULL;
340 rv = nfc_se_get_atr_internal(seHandle, &temp, &temp_len);
341 if (rv == NFC_ERROR_NONE && temp != NULL)
343 atr.assign(temp, temp_len);
348 _ERR("nfc_se_get_atr_internal failed");
360 } /* namespace smartcard_service_api */