[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / nfc_card_emulation.c
1 /*
2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include "nfc_common.h"
18
19 int nfc_se_open_secure_element_internal(
20         nfc_se_type_e se_type,
21         nfc_se_h *handle)
22 {
23         net_nfc_error_e result;
24         net_nfc_se_type_e type;
25         net_nfc_target_handle_h temp = NULL;
26
27         LOG_BEGIN();
28
29         CHECK_SUPPORTED(NFC_CE_FEATURE);
30
31         /* LCOV_EXCL_START */
32         CHECK_INIT();
33         CHECK_INVALID(handle == NULL);
34
35         switch (se_type) {
36         case NFC_SE_TYPE_ESE:
37                 type = NET_NFC_SE_TYPE_ESE;
38                 break;
39
40         case NFC_SE_TYPE_UICC:
41                 type = NET_NFC_SE_TYPE_UICC;
42                 break;
43
44         default:
45                 return NFC_ERROR_INVALID_PARAMETER;
46                 break;
47         }
48
49         result = net_nfc_client_se_open_internal_secure_element_sync(
50                 type,
51                 &temp);
52         if (result == NET_NFC_OK)
53                 *handle = (nfc_se_h)temp;
54
55         return nfc_common_convert_error_code(__func__, result);
56         /* LCOV_EXCL_STOP */
57 }
58
59
60 int nfc_se_send_apdu_internal(nfc_se_h handle,
61         unsigned char *cmd,
62         unsigned int cmd_len,
63         unsigned char **resp,
64         unsigned int *resp_len)
65 {
66         net_nfc_error_e result;
67         data_h command = NULL;
68
69         LOG_BEGIN();
70
71         CHECK_SUPPORTED(NFC_CE_FEATURE);
72
73         /* LCOV_EXCL_START */
74         CHECK_INIT();
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);
80
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);
84         }
85
86         *resp = NULL;
87         *resp_len = 0;
88
89         result = net_nfc_create_data(&command, cmd, cmd_len);
90         if (result == NET_NFC_OK) {
91                 data_h response = NULL;
92
93                 result = net_nfc_client_se_send_apdu_sync(
94                         (net_nfc_target_handle_h)handle,
95                         command,
96                         &response);
97                 if (result == NET_NFC_OK && response != NULL) {
98                         uint8_t *buffer;
99                         uint32_t length;
100
101                         buffer = net_nfc_get_data_buffer(response);
102                         length = net_nfc_get_data_length(response);
103
104                         if (buffer != NULL && length > 0) {
105                                 *resp = calloc(1, length);
106                                 if (*resp != NULL) {
107                                         memcpy(*resp, buffer, length);
108                                         *resp_len = length;
109                                 } else {
110                                         result = NET_NFC_ALLOC_FAIL;
111                                 }
112                         } else {
113                                 result = NET_NFC_NO_DATA_FOUND;
114                         }
115
116                         net_nfc_free_data(response);
117                 }
118
119                 net_nfc_free_data(command);
120         } else {
121                 LOG_ERR("net_nfc_create_data failed, [%d]", result);
122         }
123
124         return nfc_common_convert_error_code(__func__, result);
125         /* LCOV_EXCL_STOP */
126 }
127
128
129 int nfc_se_get_atr_internal(nfc_se_h handle, unsigned char **atr, unsigned int *atr_len)
130 {
131         net_nfc_error_e result;
132         data_h response = NULL;
133
134         LOG_BEGIN();
135
136         CHECK_SUPPORTED(NFC_CE_FEATURE);
137
138         /* LCOV_EXCL_START */
139         CHECK_INIT();
140         CHECK_INVALID(handle == NULL);
141         CHECK_INVALID(atr == NULL);
142         CHECK_INVALID(atr_len == NULL);
143
144         *atr = NULL;
145         *atr_len = 0;
146
147         result = net_nfc_client_se_get_atr_sync(
148                 (net_nfc_target_handle_h)handle,
149                 &response);
150         if (result == NET_NFC_OK && response != NULL) {
151                 uint8_t *buffer;
152                 uint32_t length;
153
154                 buffer = net_nfc_get_data_buffer(response);
155                 length = net_nfc_get_data_length(response);
156
157                 if (buffer != NULL && length > 0) {
158                         *atr = calloc(1, length);
159                         if (*atr != NULL) {
160                                 memcpy(*atr, buffer, length);
161                                 *atr_len = length;
162                         } else {
163                                 result = NET_NFC_ALLOC_FAIL;
164                         }
165                 } else {
166                         result = NET_NFC_NO_DATA_FOUND;
167                 }
168
169                 net_nfc_free_data(response);
170         }
171
172         return nfc_common_convert_error_code(__func__, result);
173         /* LCOV_EXCL_STOP */
174 }
175
176 int nfc_se_close_secure_element_internal(nfc_se_h handle)
177 {
178         net_nfc_error_e result;
179
180         LOG_BEGIN();
181
182         CHECK_SUPPORTED(NFC_CE_FEATURE);
183
184         /* LCOV_EXCL_START */
185         CHECK_INIT();
186
187         result = net_nfc_client_se_close_internal_secure_element_sync(
188                 (net_nfc_target_handle_h)handle);
189
190         return nfc_common_convert_error_code(__func__, result);
191         /* LCOV_EXCL_STOP */
192 }
193
194 int nfc_se_enable_card_emulation()
195 {
196         net_nfc_error_e result;
197
198         LOG_BEGIN();
199
200         CHECK_SUPPORTED(NFC_CE_FEATURE);
201
202         /* LCOV_EXCL_START */
203         CHECK_INIT();
204
205         result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMELATION_ENABLE);
206
207         return nfc_common_convert_error_code(__func__, result);
208         /* LCOV_EXCL_STOP */
209 }
210
211 int nfc_se_disable_card_emulation()
212 {
213         net_nfc_error_e result;
214
215         LOG_BEGIN();
216
217         CHECK_SUPPORTED(NFC_CE_FEATURE);
218
219         /* LCOV_EXCL_START */
220         CHECK_INIT();
221
222         result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMULATION_DISABLE);
223
224         return nfc_common_convert_error_code(__func__, result);
225         /* LCOV_EXCL_STOP */
226 }
227
228 int nfc_se_get_card_emulation_mode(nfc_se_card_emulation_mode_type_e *mode)
229 {
230         net_nfc_error_e result;
231         net_nfc_card_emulation_mode_t se_mode;
232
233         LOG_BEGIN();
234
235         CHECK_SUPPORTED(NFC_CE_FEATURE);
236
237         /* LCOV_EXCL_START */
238         CHECK_INIT();
239         CHECK_INVALID(mode == NULL);
240
241         result = net_nfc_get_card_emulation_mode_sync(&se_mode);
242
243         switch (se_mode) {
244         case NET_NFC_CARD_EMELATION_ENABLE:
245                 *mode = NFC_SE_CARD_EMULATION_MODE_ON;
246                 break;
247
248         case NET_NFC_CARD_EMULATION_DISABLE:
249                 *mode = NFC_SE_CARD_EMULATION_MODE_OFF;
250                 break;
251         }
252
253         return nfc_common_convert_error_code(__func__, result);;
254         /* LCOV_EXCL_STOP */
255 }
256
257 int nfc_hce_send_apdu_response(nfc_se_h handle, unsigned char *resp, unsigned int resp_len)
258 {
259         net_nfc_error_e result;
260         data_h data;
261
262         LOG_BEGIN();
263
264         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
265
266         /* LCOV_EXCL_START */
267         CHECK_INIT();
268         CHECK_INVALID(resp == NULL);
269         CHECK_INVALID(resp_len == 0);
270
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);
275         } else {
276                 LOG_ERR("net_nfc_create_data failed, [%d]", result);
277         }
278
279         return nfc_common_convert_error_code(__func__, result);
280         /* LCOV_EXCL_STOP */
281 }
282
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)
284 {
285         net_nfc_error_e result;
286
287         LOG_BEGIN();
288
289         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
290
291         /* LCOV_EXCL_START */
292         CHECK_INIT();
293
294         result = net_nfc_client_se_set_default_route_sync(powered_on_status, powered_off_status, low_battery_status);
295
296         return nfc_common_convert_error_code(__func__, result);
297         /* LCOV_EXCL_STOP */
298 }
299
300 int nfc_se_is_activated_handler_for_aid(nfc_se_type_e se_type, const char *aid, bool *is_activated_handler)
301 {
302         net_nfc_error_e result;
303
304         LOG_BEGIN();
305
306         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
307
308         /* LCOV_EXCL_START */
309         CHECK_INIT();
310         CHECK_INVALID(aid == NULL);
311         CHECK_INVALID(is_activated_handler == NULL);
312
313         result = net_nfc_client_se_is_activated_aid_handler_sync((net_nfc_se_type_e)se_type, aid, is_activated_handler);
314
315         return nfc_common_convert_error_code(__func__, result);
316         /* LCOV_EXCL_STOP */
317 }
318
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)
320 {
321         net_nfc_error_e result;
322
323         LOG_BEGIN();
324
325         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
326
327         /* LCOV_EXCL_START */
328         CHECK_INIT();
329         CHECK_INVALID(is_activated_handler == NULL);
330
331         result = net_nfc_client_se_is_activated_category_handler_sync((net_nfc_se_type_e)se_type, category, is_activated_handler);
332
333         return nfc_common_convert_error_code(__func__, result);
334         /* LCOV_EXCL_STOP */
335 }
336
337 int nfc_se_register_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
338 {
339         net_nfc_error_e result;
340
341         LOG_BEGIN();
342
343         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
344
345         /* LCOV_EXCL_START */
346         CHECK_INIT();
347         CHECK_INVALID(aid == NULL);
348
349         result = net_nfc_client_se_register_aids_sync((net_nfc_se_type_e)se_type, category, aid);
350
351         return nfc_common_convert_error_code(__func__, result);
352         /* LCOV_EXCL_STOP */
353 }
354
355 int nfc_se_unregister_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
356 {
357         net_nfc_error_e result;
358
359         LOG_BEGIN();
360
361         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
362
363         /* LCOV_EXCL_START */
364         CHECK_INIT();
365         CHECK_INVALID(aid == NULL);
366
367         result = net_nfc_client_se_unregister_aid_sync((net_nfc_se_type_e)se_type, category, aid);
368
369         return nfc_common_convert_error_code(__func__, result);
370         /* LCOV_EXCL_STOP */
371 }
372
373 int nfc_se_get_registered_aids_count(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, int *count)
374 {
375         net_nfc_error_e result;
376
377         LOG_BEGIN();
378
379         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
380
381         /* LCOV_EXCL_START */
382         CHECK_INIT();
383         CHECK_INVALID(count == NULL);
384
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,
388                         (size_t *)count);
389
390         return nfc_common_convert_error_code(__func__, result);
391         /* LCOV_EXCL_STOP */
392 }
393
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)
397 {
398         LOG_BEGIN();
399
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,
403                         aid,
404                         readonly,
405                         gdbus_nfc_context.on_se_registered_aid_cb_user_data);
406         }
407 }
408 /* LCOV_EXCL_STOP */
409
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)
411 {
412         net_nfc_error_e result;
413
414         LOG_BEGIN();
415
416         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
417
418         /* LCOV_EXCL_START */
419         CHECK_INIT();
420         CHECK_INVALID(callback == NULL);
421
422         gdbus_nfc_context.on_se_registered_aid_cb = callback;
423         gdbus_nfc_context.on_se_registered_aid_cb_user_data = user_data;
424
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,
428                 user_data);
429
430         return nfc_common_convert_error_code(__func__, result);
431         /* LCOV_EXCL_STOP */
432 }
433
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)
436 {
437         net_nfc_error_e result;
438
439         LOG_BEGIN();
440
441         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
442
443         /* LCOV_EXCL_START */
444         CHECK_INIT();
445
446         result = net_nfc_client_se_add_route_aid_sync(pkg_name, se_type, category, aid, unlock_required, power);
447
448         return nfc_common_convert_error_code(__func__, result);
449         /* LCOV_EXCL_STOP */
450 }
451
452 int nfc_se_remove_route_for_aid_internal(const char* pkg_name, const char *aid)
453 {
454         net_nfc_error_e result;
455
456         LOG_BEGIN();
457
458         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
459
460         /* LCOV_EXCL_START */
461         CHECK_INIT();
462
463         result = net_nfc_client_se_remove_route_aid_sync(pkg_name, aid);
464
465         return nfc_common_convert_error_code(__func__, result);
466         /* LCOV_EXCL_STOP */
467 }
468
469 int nfc_se_set_preferred_handler()
470 {
471         net_nfc_error_e result;
472
473         LOG_BEGIN();
474
475         CHECK_SUPPORTED(NFC_CE_FEATURE);
476
477         /* LCOV_EXCL_START */
478         CHECK_INIT();
479         CHECK_ACTIVATED();
480
481         result = net_nfc_client_se_set_preferred_handler_sync(true);
482
483         return nfc_common_convert_error_code(__func__, result);
484         /* LCOV_EXCL_STOP */
485 }
486
487 int nfc_se_unset_preferred_handler()
488 {
489         net_nfc_error_e result;
490
491         LOG_BEGIN();
492
493         CHECK_SUPPORTED(NFC_CE_FEATURE);
494
495         /* LCOV_EXCL_START */
496         CHECK_INIT();
497         CHECK_ACTIVATED();
498
499         result = net_nfc_client_se_set_preferred_handler_sync(false);
500
501         return nfc_common_convert_error_code(__func__, result);
502         /* LCOV_EXCL_STOP */
503 }
504