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;
29 CHECK_SUPPORTED(NFC_CE_FEATURE);
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;
71 CHECK_SUPPORTED(NFC_CE_FEATURE);
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;
136 CHECK_SUPPORTED(NFC_CE_FEATURE);
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;
182 CHECK_SUPPORTED(NFC_CE_FEATURE);
184 /* LCOV_EXCL_START */
187 result = net_nfc_client_se_close_internal_secure_element_sync(
188 (net_nfc_target_handle_h)handle);
190 return nfc_common_convert_error_code(__func__, result);
194 int nfc_se_enable_card_emulation()
196 net_nfc_error_e result;
200 CHECK_SUPPORTED(NFC_CE_FEATURE);
202 /* LCOV_EXCL_START */
205 result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMELATION_ENABLE);
207 return nfc_common_convert_error_code(__func__, result);
211 int nfc_se_disable_card_emulation()
213 net_nfc_error_e result;
217 CHECK_SUPPORTED(NFC_CE_FEATURE);
219 /* LCOV_EXCL_START */
222 result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMULATION_DISABLE);
224 return nfc_common_convert_error_code(__func__, result);
228 int nfc_se_get_card_emulation_mode(nfc_se_card_emulation_mode_type_e *mode)
230 net_nfc_error_e result;
231 net_nfc_card_emulation_mode_t se_mode;
235 CHECK_SUPPORTED(NFC_CE_FEATURE);
237 /* LCOV_EXCL_START */
239 CHECK_INVALID(mode == NULL);
241 result = net_nfc_get_card_emulation_mode_sync(&se_mode);
244 case NET_NFC_CARD_EMELATION_ENABLE:
245 *mode = NFC_SE_CARD_EMULATION_MODE_ON;
248 case NET_NFC_CARD_EMULATION_DISABLE:
249 *mode = NFC_SE_CARD_EMULATION_MODE_OFF;
253 return nfc_common_convert_error_code(__func__, result);;
257 int nfc_hce_send_apdu_response(nfc_se_h handle, unsigned char *resp, unsigned int resp_len)
259 net_nfc_error_e result;
264 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
266 /* LCOV_EXCL_START */
268 CHECK_INVALID(resp == NULL);
269 CHECK_INVALID(resp_len == 0);
271 result = net_nfc_create_data(&data, resp, resp_len);
272 if (result == NET_NFC_OK) {
273 result = net_nfc_client_hce_response_apdu_sync(handle, data);
274 net_nfc_free_data(data);
276 LOG_ERR("net_nfc_create_data failed, [%d]", result);
279 return nfc_common_convert_error_code(__func__, result);
283 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)
285 net_nfc_error_e result;
289 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
291 /* LCOV_EXCL_START */
294 result = net_nfc_client_se_set_default_route_sync(powered_on_status, powered_off_status, low_battery_status);
296 return nfc_common_convert_error_code(__func__, result);
300 int nfc_se_is_activated_handler_for_aid(nfc_se_type_e se_type, const char *aid, bool *is_activated_handler)
302 net_nfc_error_e result;
306 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
308 /* LCOV_EXCL_START */
310 CHECK_INVALID(aid == NULL);
311 CHECK_INVALID(is_activated_handler == NULL);
313 result = net_nfc_client_se_is_activated_aid_handler_sync((net_nfc_se_type_e)se_type, aid, is_activated_handler);
315 return nfc_common_convert_error_code(__func__, result);
319 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)
321 net_nfc_error_e result;
325 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
327 /* LCOV_EXCL_START */
329 CHECK_INVALID(is_activated_handler == NULL);
331 result = net_nfc_client_se_is_activated_category_handler_sync((net_nfc_se_type_e)se_type, category, is_activated_handler);
333 return nfc_common_convert_error_code(__func__, result);
337 int nfc_se_register_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
339 net_nfc_error_e result;
343 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
345 /* LCOV_EXCL_START */
347 CHECK_INVALID(aid == NULL);
349 result = net_nfc_client_se_register_aids_sync((net_nfc_se_type_e)se_type, category, aid);
351 return nfc_common_convert_error_code(__func__, result);
355 int nfc_se_unregister_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
357 net_nfc_error_e result;
361 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
363 /* LCOV_EXCL_START */
365 CHECK_INVALID(aid == NULL);
367 result = net_nfc_client_se_unregister_aid_sync((net_nfc_se_type_e)se_type, category, aid);
369 return nfc_common_convert_error_code(__func__, result);
373 int nfc_se_get_registered_aids_count(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, int *count)
375 net_nfc_error_e result;
379 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
381 /* LCOV_EXCL_START */
383 CHECK_INVALID(count == NULL);
385 result = net_nfc_client_se_get_registered_aids_count_sync(
386 (net_nfc_se_type_e)NFC_SE_TYPE_HCE,
387 (net_nfc_card_emulation_category_t)category,
390 return nfc_common_convert_error_code(__func__, result);
394 /* LCOV_EXCL_START */
395 static void _se_registered_aid_event_cb(net_nfc_se_type_e se_type,
396 const char *aid, bool readonly, void *user_data)
400 if (gdbus_nfc_context.on_se_registered_aid_cb != NULL) {
401 gdbus_nfc_context.on_se_registered_aid_cb(
402 (nfc_se_type_e)se_type,
405 gdbus_nfc_context.on_se_registered_aid_cb_user_data);
410 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)
412 net_nfc_error_e result;
416 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
418 /* LCOV_EXCL_START */
420 CHECK_INVALID(callback == NULL);
422 gdbus_nfc_context.on_se_registered_aid_cb = callback;
423 gdbus_nfc_context.on_se_registered_aid_cb_user_data = user_data;
425 result = net_nfc_client_se_foreach_registered_aids_sync((net_nfc_se_type_e)se_type,
426 (net_nfc_card_emulation_category_t)category,
427 (net_nfc_client_se_registered_aid_cb)_se_registered_aid_event_cb,
430 return nfc_common_convert_error_code(__func__, result);
434 int nfc_se_add_route_for_aid_internal(const char *aid, nfc_se_type_e se_type, const char* pkg_name, bool unlock_required,
435 nfc_se_power_type_e power, nfc_card_emulation_category_type_e category)
437 net_nfc_error_e result;
441 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
443 /* LCOV_EXCL_START */
446 result = net_nfc_client_se_add_route_aid_sync(pkg_name, se_type, category, aid, unlock_required, power);
448 return nfc_common_convert_error_code(__func__, result);
452 int nfc_se_remove_route_for_aid_internal(const char* pkg_name, const char *aid)
454 net_nfc_error_e result;
458 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
460 /* LCOV_EXCL_START */
463 result = net_nfc_client_se_remove_route_aid_sync(pkg_name, aid);
465 return nfc_common_convert_error_code(__func__, result);
469 int nfc_se_set_preferred_handler()
471 net_nfc_error_e result;
475 CHECK_SUPPORTED(NFC_CE_FEATURE);
477 /* LCOV_EXCL_START */
481 result = net_nfc_client_se_set_preferred_handler_sync(true);
483 return nfc_common_convert_error_code(__func__, result);
487 int nfc_se_unset_preferred_handler()
489 net_nfc_error_e result;
493 CHECK_SUPPORTED(NFC_CE_FEATURE);
495 /* LCOV_EXCL_START */
499 result = net_nfc_client_se_set_preferred_handler_sync(false);
501 return nfc_common_convert_error_code(__func__, result);