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 #include "nfc_common.h"
19 int nfc_se_open_secure_element_internal(
20 nfc_se_type_e se_type,
23 net_nfc_error_e result;
24 net_nfc_se_type_e type;
25 net_nfc_target_handle_h temp = NULL;
27 LOG_BEGIN(); // LCOV_EXCL_LINE
29 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
33 CHECK_INVALID(handle == NULL);
37 type = NET_NFC_SE_TYPE_ESE;
40 case NFC_SE_TYPE_UICC:
41 type = NET_NFC_SE_TYPE_UICC;
45 return NFC_ERROR_INVALID_PARAMETER;
49 result = net_nfc_client_se_open_internal_secure_element_sync(
52 if (result == NET_NFC_OK)
53 *handle = (nfc_se_h)temp;
55 return nfc_common_convert_error_code(__func__, result);
60 int nfc_se_send_apdu_internal(nfc_se_h handle,
64 unsigned int *resp_len)
66 net_nfc_error_e result;
67 data_h command = NULL; // LCOV_EXCL_LINE
69 LOG_BEGIN(); // LCOV_EXCL_LINE
71 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
75 CHECK_INVALID(handle == NULL);
76 CHECK_INVALID(cmd == NULL);
77 CHECK_INVALID(cmd_len == 0);
78 CHECK_INVALID(resp == NULL);
79 CHECK_INVALID(resp_len == NULL);
81 if (cmd_len == 3 && cmd[0] == 'A' && cmd[1] == 'T' && cmd[2] == 'R') {
82 LOG_DEBUG("cheat... invoke get atr");
83 return nfc_se_get_atr_internal(handle, resp, resp_len);
89 result = net_nfc_create_data(&command, cmd, cmd_len);
90 if (result == NET_NFC_OK) {
91 data_h response = NULL;
93 result = net_nfc_client_se_send_apdu_sync(
94 (net_nfc_target_handle_h)handle,
97 if (result == NET_NFC_OK && response != NULL) {
101 buffer = net_nfc_get_data_buffer(response);
102 length = net_nfc_get_data_length(response);
104 if (buffer != NULL && length > 0) {
105 *resp = calloc(1, length);
107 memcpy(*resp, buffer, length);
110 result = NET_NFC_ALLOC_FAIL;
113 result = NET_NFC_NO_DATA_FOUND;
116 net_nfc_free_data(response);
119 net_nfc_free_data(command);
121 LOG_ERR("net_nfc_create_data failed, [%d]", result);
124 return nfc_common_convert_error_code(__func__, result);
129 int nfc_se_get_atr_internal(nfc_se_h handle, unsigned char **atr, unsigned int *atr_len)
131 net_nfc_error_e result;
132 data_h response = NULL; // LCOV_EXCL_LINE
134 LOG_BEGIN(); // LCOV_EXCL_LINE
136 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
138 /* LCOV_EXCL_START */
140 CHECK_INVALID(handle == NULL);
141 CHECK_INVALID(atr == NULL);
142 CHECK_INVALID(atr_len == NULL);
147 result = net_nfc_client_se_get_atr_sync(
148 (net_nfc_target_handle_h)handle,
150 if (result == NET_NFC_OK && response != NULL) {
154 buffer = net_nfc_get_data_buffer(response);
155 length = net_nfc_get_data_length(response);
157 if (buffer != NULL && length > 0) {
158 *atr = calloc(1, length);
160 memcpy(*atr, buffer, length);
163 result = NET_NFC_ALLOC_FAIL;
166 result = NET_NFC_NO_DATA_FOUND;
169 net_nfc_free_data(response);
172 return nfc_common_convert_error_code(__func__, result);
176 int nfc_se_close_secure_element_internal(nfc_se_h handle)
178 net_nfc_error_e result;
180 LOG_BEGIN(); // LCOV_EXCL_LINE
182 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
184 /* LCOV_EXCL_START */
186 CHECK_INVALID(handle == NULL);
188 result = net_nfc_client_se_close_internal_secure_element_sync(
189 (net_nfc_target_handle_h)handle);
191 return nfc_common_convert_error_code(__func__, result);
195 int nfc_se_enable_card_emulation()
197 net_nfc_error_e result;
199 LOG_BEGIN(); // LCOV_EXCL_LINE
201 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
203 /* LCOV_EXCL_START */
206 result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMELATION_ENABLE);
208 return nfc_common_convert_error_code(__func__, result);
212 int nfc_se_disable_card_emulation()
214 net_nfc_error_e result;
218 CHECK_SUPPORTED(NFC_CE_FEATURE);
220 /* LCOV_EXCL_START */
223 result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMULATION_DISABLE);
225 return nfc_common_convert_error_code(__func__, result);
229 int nfc_se_get_card_emulation_mode(nfc_se_card_emulation_mode_type_e *mode)
231 net_nfc_error_e result;
232 net_nfc_card_emulation_mode_t se_mode;
234 LOG_BEGIN(); // LCOV_EXCL_LINE
236 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
238 /* LCOV_EXCL_START */
240 CHECK_INVALID(mode == NULL);
242 result = net_nfc_get_card_emulation_mode_sync(&se_mode);
245 case NET_NFC_CARD_EMELATION_ENABLE:
246 *mode = NFC_SE_CARD_EMULATION_MODE_ON;
249 case NET_NFC_CARD_EMULATION_DISABLE:
250 *mode = NFC_SE_CARD_EMULATION_MODE_OFF;
254 return nfc_common_convert_error_code(__func__, result);;
258 int nfc_hce_send_apdu_response(nfc_se_h handle, unsigned char *resp, unsigned int resp_len)
260 net_nfc_error_e result;
263 LOG_BEGIN(); // LCOV_EXCL_LINE
265 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
267 /* LCOV_EXCL_START */
269 CHECK_INVALID(resp == NULL);
270 CHECK_INVALID(resp_len == 0);
273 result = net_nfc_create_data(&data, resp, resp_len);
274 if (result == NET_NFC_OK) {
275 result = net_nfc_client_hce_response_apdu_sync(handle, data);
276 net_nfc_free_data(data);
278 LOG_ERR("net_nfc_create_data failed, [%d]", result);
281 return nfc_common_convert_error_code(__func__, result);
285 int nfc_se_set_default_route(nfc_se_type_e powered_on_status, nfc_se_type_e powered_off_status, nfc_se_type_e low_battery_status)
287 net_nfc_error_e result;
289 LOG_BEGIN(); // LCOV_EXCL_LINE
291 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
293 /* LCOV_EXCL_START */
296 result = net_nfc_client_se_set_default_route_sync(powered_on_status, powered_off_status, low_battery_status);
298 return nfc_common_convert_error_code(__func__, result);
302 int nfc_se_is_activated_handler_for_aid(nfc_se_type_e se_type, const char *aid, bool *is_activated_handler)
304 net_nfc_error_e result;
306 LOG_BEGIN(); // LCOV_EXCL_LINE
308 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
310 /* LCOV_EXCL_START */
312 CHECK_INVALID(aid == NULL);
313 CHECK_INVALID(is_activated_handler == NULL);
315 result = net_nfc_client_se_is_activated_aid_handler_sync((net_nfc_se_type_e)se_type, aid, is_activated_handler);
317 return nfc_common_convert_error_code(__func__, result);
321 int nfc_se_is_activated_handler_for_category(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, bool *is_activated_handler)
323 net_nfc_error_e result;
327 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
329 /* LCOV_EXCL_START */
331 CHECK_INVALID(is_activated_handler == NULL);
333 result = net_nfc_client_se_is_activated_category_handler_sync((net_nfc_se_type_e)se_type, category, is_activated_handler);
335 return nfc_common_convert_error_code(__func__, result);
339 int nfc_se_register_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
341 net_nfc_error_e result;
343 LOG_BEGIN(); // LCOV_EXCL_LINE
345 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
347 /* LCOV_EXCL_START */
349 CHECK_INVALID(aid == NULL);
351 result = net_nfc_client_se_register_aids_sync((net_nfc_se_type_e)se_type, category, aid);
353 return nfc_common_convert_error_code(__func__, result);
357 int nfc_se_unregister_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
359 net_nfc_error_e result;
361 LOG_BEGIN(); // LCOV_EXCL_LINE
363 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
365 /* LCOV_EXCL_START */
367 CHECK_INVALID(aid == NULL);
369 result = net_nfc_client_se_unregister_aid_sync((net_nfc_se_type_e)se_type, category, aid);
371 return nfc_common_convert_error_code(__func__, result);
375 int nfc_se_get_registered_aids_count(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, int *count)
377 net_nfc_error_e result;
379 LOG_BEGIN(); // LCOV_EXCL_LINE
381 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
383 /* LCOV_EXCL_START */
385 CHECK_INVALID(count == NULL);
388 result = net_nfc_client_se_get_registered_aids_count_sync(
389 (net_nfc_se_type_e)se_type,
390 (net_nfc_card_emulation_category_t)category,
393 return nfc_common_convert_error_code(__func__, result);
397 /* LCOV_EXCL_START */
398 static void _se_registered_aid_event_cb(net_nfc_se_type_e se_type,
399 const char *aid, bool readonly, void *user_data)
403 if (gdbus_nfc_context.on_se_registered_aid_cb != NULL) {
404 gdbus_nfc_context.on_se_registered_aid_cb(
405 (nfc_se_type_e)se_type,
408 gdbus_nfc_context.on_se_registered_aid_cb_user_data);
413 int nfc_se_foreach_registered_aids(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, nfc_se_registered_aid_cb callback, void *user_data)
415 net_nfc_error_e result;
417 LOG_BEGIN(); // LCOV_EXCL_LINE
419 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
421 /* LCOV_EXCL_START */
423 CHECK_INVALID(callback == NULL);
424 CHECK_INVALID(category < NFC_CARD_EMULATION_CATEGORY_PAYMENT || category > NFC_CARD_EMULATION_CATEGORY_OTHER);
427 gdbus_nfc_context.on_se_registered_aid_cb = callback;
428 gdbus_nfc_context.on_se_registered_aid_cb_user_data = user_data;
430 result = net_nfc_client_se_foreach_registered_aids_sync((net_nfc_se_type_e)se_type,
431 (net_nfc_card_emulation_category_t)category,
432 (net_nfc_client_se_registered_aid_cb)_se_registered_aid_event_cb,
435 return nfc_common_convert_error_code(__func__, result);
439 int nfc_se_add_route_for_aid_internal(const char *aid, nfc_se_type_e se_type, const char* pkg_name, bool unlock_required,
440 nfc_se_power_type_e power, nfc_card_emulation_category_type_e category)
442 net_nfc_error_e result;
444 LOG_BEGIN(); // LCOV_EXCL_LINE
446 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
448 /* LCOV_EXCL_START */
450 CHECK_INVALID(aid == NULL);
451 CHECK_INVALID(pkg_name == NULL);
453 result = net_nfc_client_se_add_route_aid_sync(pkg_name, se_type, category, aid, unlock_required, power);
455 return nfc_common_convert_error_code(__func__, result);
459 int nfc_se_remove_route_for_aid_internal(const char* pkg_name, const char *aid)
461 net_nfc_error_e result;
463 LOG_BEGIN(); // LCOV_EXCL_LINE
465 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
467 /* LCOV_EXCL_START */
469 CHECK_INVALID(pkg_name == NULL);
472 result = net_nfc_client_se_remove_package_aids_sync(pkg_name);
474 result = net_nfc_client_se_remove_route_aid_sync(pkg_name, aid);
476 return nfc_common_convert_error_code(__func__, result);
480 int nfc_se_foreach_registered_handlers(
481 nfc_card_emulation_category_type_e category,
482 nfc_se_registered_handler_cb callback, void *user_data)
484 net_nfc_error_e result;
486 LOG_BEGIN(); // LCOV_EXCL_LINE
488 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
490 CHECK_INVALID(callback == NULL);
492 result = net_nfc_client_se_foreach_registered_handlers_sync(category,
493 callback, user_data);
495 return nfc_common_convert_error_code(__func__, result);
498 int nfc_se_set_preferred_handler()
500 net_nfc_error_e result;
502 LOG_BEGIN(); // LCOV_EXCL_LINE
504 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
506 /* LCOV_EXCL_START */
510 result = net_nfc_client_se_set_preferred_handler_sync(true);
512 return nfc_common_convert_error_code(__func__, result);
516 int nfc_se_unset_preferred_handler()
518 net_nfc_error_e result;
520 LOG_BEGIN(); // LCOV_EXCL_LINE
522 CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
524 /* LCOV_EXCL_START */
528 result = net_nfc_client_se_set_preferred_handler_sync(false);
530 return nfc_common_convert_error_code(__func__, result);
534 int nfc_se_get_conflict_handlers_internal(const char *package,
535 nfc_card_emulation_category_type_e category, const char *aid, char ***handlers)
537 net_nfc_error_e result;
539 LOG_BEGIN(); // LCOV_EXCL_LINE
541 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
545 result = net_nfc_client_se_get_conflict_handlers_sync(package, category, aid, handlers);
547 return nfc_common_convert_error_code(__func__, result);