summaryrefslogtreecommitdiff
path: root/NFCTerminal.cpp
blob: 9fa911061f83234d71b5b42f137394d5b29a51dc (plain)
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
/*
 * 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()
{
	return (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();
	}

	bool NFCTerminal::initialize()
	{
		int ret;

		if (initialized == true)
			return initialized;

		ret = nfc_manager_initialize_sync();
		if (ret == NFC_ERROR_NONE)
		{
			initialized = true;

			if (open() == true) {
				present = true;
				close();
			}
		}
		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(seHandle);
			if (ret == NFC_ERROR_NONE) {
				seHandle = NULL;
				closed = true;
				referred = 0;
			} else {
				_ERR("nfc_se_close_secure_element failed [%d]", ret);
			}
		}

		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(NFC_SE_TYPE_ESE,
					&seHandle);
				if (ret == NFC_ERROR_NONE) {
					closed = false;
					referred++;
				} else {
					_ERR("nfc_se_open_secure_element 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) {
				ret = nfc_se_close_secure_element(seHandle);
				if (ret == NFC_ERROR_NONE) {
					seHandle = NULL;
					closed = true;
					referred = 0;
				} else {
					_ERR("nfc_se_close_secure_element 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(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("net_nfc_send_apdu_sync 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(seHandle, &temp, &temp_len);
			if (rv == NFC_ERROR_NONE && temp != NULL)
			{
				atr.assign(temp, temp_len);
				g_free(temp);
			}
			else
			{
				_ERR("net_nfc_client_se_get_atr_sync failed");
			}
		}
		else
		{
			_ERR("closed...");
		}

		_END();

		return rv;
	}
} /* namespace smartcard_service_api */