1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* 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 <cstdio>
#include <cstring>
#include <unistd.h>
#include <sys/time.h>
#include <glib.h>
/* local header */
#include "smartcard-types.h"
#include "Debug.h"
#include "TerminalInterface.h"
#include "NFCTerminal.h"
#ifndef EXPORT_API
#define EXPORT_API __attribute__((visibility("default")))
#endif
#define ASYNC
using namespace smartcard_service_api;
static const char *se_name = "eSE";
/* below functions will be called when dlopen or dlclose is called */
void __attribute__ ((constructor)) lib_init()
{
}
void __attribute__ ((destructor)) lib_fini()
{
}
/* below three functions must be implemented */
extern "C"
{
EXPORT_API const char *get_name()
{
return se_name;
}
EXPORT_API void *create_instance()
{
int value;
if (vconf_get_bool(VCONFKEY_NFC_ESE_DISABLE, &value) < 0)
return NULL;
return value ? NULL : (void *)NFCTerminal::getInstance();
}
EXPORT_API void destroy_instance(void *instance)
{
NFCTerminal *inst = (NFCTerminal *)instance;
if (inst == NFCTerminal::getInstance())
{
inst->finalize();
}
else
{
_ERR("instance is invalid : getInstance [%p], instance [%p]",
NFCTerminal::getInstance(), instance);
}
}
}
namespace smartcard_service_api
{
NFCTerminal::NFCTerminal() : Terminal(), seHandle(NULL),
present(false), referred(0)
{
name = (char *)se_name;
initialize();
}
NFCTerminal *NFCTerminal::getInstance()
{
static NFCTerminal instance;
return &instance;
}
NFCTerminal::~NFCTerminal()
{
finalize();
}
void NFCTerminal::onActivationChanged(bool activated, void *userData)
{
NFCTerminal *instance = (NFCTerminal *)userData;
_ERR("nfc state changed [%s]", activated ? "activated" : "deactivated");
if (activated == true) {
if (instance->present == false) {
if (instance->open() == true) {
instance->present = true;
instance->close();
if (instance->statusCallback != NULL) {
instance->statusCallback(
instance->getName(),
NOTIFY_SE_AVAILABLE,
SCARD_ERROR_OK,
NULL);
}
} else {
_ERR("ese open failed");
}
} else {
/* okay */
}
} else {
if (instance->present == true) {
instance->present = false;
if (instance->isClosed() == false) {
int ret;
/* close now */
ret = nfc_se_close_secure_element_internal(
instance->seHandle);
if (ret != NFC_ERROR_NONE) {
_ERR("nfc_se_close_secure_element failed [%d]", ret);
}
instance->seHandle = NULL;
instance->closed = true;
instance->referred = 0;
}
if (instance->statusCallback != NULL) {
instance->statusCallback(
instance->getName(),
NOTIFY_SE_NOT_AVAILABLE,
SCARD_ERROR_OK,
NULL);
}
} else {
/* okay */
}
}
}
bool NFCTerminal::initialize()
{
int ret;
if (initialized == true)
return initialized;
ret = nfc_manager_initialize();
if (ret == NFC_ERROR_NONE)
{
initialized = true;
ret = nfc_manager_set_activation_changed_cb(
&NFCTerminal::onActivationChanged, this);
if (ret != NFC_ERROR_NONE) {
_ERR("nfc_manager_set_activation_changed_cb failed, [%d]", ret);
}
if (nfc_manager_is_activated() == true) {
if (open() == true) {
present = true;
close();
} else {
_ERR("ese open failed");
}
} else {
_ERR("nfc is not activated.");
}
}
else
{
_ERR("net_nfc_initialize failed [%d]", ret);
}
return initialized;
}
void NFCTerminal::finalize()
{
int ret;
if (isClosed() == false) {
/* close now */
ret = nfc_se_close_secure_element_internal(seHandle);
if (ret != NFC_ERROR_NONE) {
_ERR("nfc_se_close_secure_element failed [%d]", ret);
}
seHandle = NULL;
closed = true;
referred = 0;
}
present = false;
nfc_manager_unset_activation_changed_cb();
ret = nfc_manager_deinitialize();
if (ret == NFC_ERROR_NONE) {
initialized = false;
} else {
_ERR("nfc_manager_deinitialize failed [%d]", ret);
}
}
bool NFCTerminal::open()
{
int ret;
_BEGIN();
if (isInitialized()) {
if (referred == 0) {
ret = nfc_se_open_secure_element_internal(NFC_SE_TYPE_ESE,
&seHandle);
if (ret == NFC_ERROR_NONE) {
closed = false;
referred++;
} else {
_ERR("nfc_se_open_secure_element_internal failed [%d]", ret);
}
} else {
referred++;
}
_DBG("reference count [%d]", referred);
}
_END();
return (isClosed() == false);
}
void NFCTerminal::close()
{
int ret;
_BEGIN();
if (isInitialized())
{
if (referred <= 1) {
g_usleep(1000000);
ret = nfc_se_close_secure_element_internal(seHandle);
if (ret == NFC_ERROR_NONE) {
seHandle = NULL;
closed = true;
referred = 0;
} else {
_ERR("nfc_se_close_secure_element_internal failed [%d]", ret);
}
} else {
referred--;
}
_DBG("reference count [%d]", referred);
}
_END();
}
int NFCTerminal::transmitSync(const ByteArray &command, ByteArray &response)
{
int rv = SCARD_ERROR_NOT_INITIALIZED;
_BEGIN();
if (isClosed() == false)
{
if (command.size() > 0)
{
uint8_t *resp = NULL;
uint32_t resp_len;
rv = nfc_se_send_apdu_internal(seHandle,
(uint8_t *)command.getBuffer(),
command.size(),
&resp,
&resp_len);
if (rv == NFC_ERROR_NONE &&
resp != NULL)
{
response.assign(resp, resp_len);
g_free(resp);
}
else
{
_ERR("nfc_se_send_apdu_internal failed, [%d]", rv);
}
}
else
{
_ERR("invalid command");
}
}
else
{
_ERR("closed...");
}
_END();
return rv;
}
int NFCTerminal::getATRSync(ByteArray &atr)
{
int rv = -1;
_BEGIN();
if (isClosed() == false)
{
uint8_t *temp = NULL;
uint32_t temp_len;
rv = nfc_se_get_atr_internal(seHandle, &temp, &temp_len);
if (rv == NFC_ERROR_NONE && temp != NULL)
{
atr.assign(temp, temp_len);
g_free(temp);
}
else
{
_ERR("nfc_se_get_atr_internal failed");
}
}
else
{
_ERR("closed...");
}
_END();
return rv;
}
} /* namespace smartcard_service_api */
|