Add invalid parameter case
[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(); // LCOV_EXCL_LINE
28
29         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
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; // LCOV_EXCL_LINE
68
69         LOG_BEGIN(); // LCOV_EXCL_LINE
70
71         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
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; // LCOV_EXCL_LINE
133
134         LOG_BEGIN(); // LCOV_EXCL_LINE
135
136         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
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(); // LCOV_EXCL_LINE
181
182         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
183
184         /* LCOV_EXCL_START */
185         CHECK_INIT();
186         CHECK_INVALID(handle == NULL);
187
188         result = net_nfc_client_se_close_internal_secure_element_sync(
189                 (net_nfc_target_handle_h)handle);
190
191         return nfc_common_convert_error_code(__func__, result);
192         /* LCOV_EXCL_STOP */
193 }
194
195 int nfc_se_enable_card_emulation()
196 {
197         net_nfc_error_e result;
198
199         LOG_BEGIN(); // LCOV_EXCL_LINE
200
201         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
202
203         /* LCOV_EXCL_START */
204         CHECK_INIT();
205
206         result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMELATION_ENABLE);
207
208         return nfc_common_convert_error_code(__func__, result);
209         /* LCOV_EXCL_STOP */
210 }
211
212 int nfc_se_disable_card_emulation()
213 {
214         net_nfc_error_e result;
215
216         LOG_BEGIN();
217
218         CHECK_SUPPORTED(NFC_CE_FEATURE);
219
220         /* LCOV_EXCL_START */
221         CHECK_INIT();
222
223         result = net_nfc_set_card_emulation_mode_sync(NET_NFC_CARD_EMULATION_DISABLE);
224
225         return nfc_common_convert_error_code(__func__, result);
226         /* LCOV_EXCL_STOP */
227 }
228
229 int nfc_se_get_card_emulation_mode(nfc_se_card_emulation_mode_type_e *mode)
230 {
231         net_nfc_error_e result;
232         net_nfc_card_emulation_mode_t se_mode;
233
234         LOG_BEGIN(); // LCOV_EXCL_LINE
235
236         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
237
238         /* LCOV_EXCL_START */
239         CHECK_INIT();
240         CHECK_INVALID(mode == NULL);
241
242         result = net_nfc_get_card_emulation_mode_sync(&se_mode);
243
244         switch (se_mode) {
245         case NET_NFC_CARD_EMELATION_ENABLE:
246                 *mode = NFC_SE_CARD_EMULATION_MODE_ON;
247                 break;
248
249         case NET_NFC_CARD_EMULATION_DISABLE:
250                 *mode = NFC_SE_CARD_EMULATION_MODE_OFF;
251                 break;
252         }
253
254         return nfc_common_convert_error_code(__func__, result);;
255         /* LCOV_EXCL_STOP */
256 }
257
258 int nfc_hce_send_apdu_response(nfc_se_h handle, unsigned char *resp, unsigned int resp_len)
259 {
260         net_nfc_error_e result;
261         data_h data;
262
263         LOG_BEGIN(); // LCOV_EXCL_LINE
264
265         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
266
267         /* LCOV_EXCL_START */
268         CHECK_INIT();
269         CHECK_INVALID(resp == NULL);
270         CHECK_INVALID(resp_len == 0);
271         CHECK_ACTIVATED();
272
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);
277         } else {
278                 LOG_ERR("net_nfc_create_data failed, [%d]", result);
279         }
280
281         return nfc_common_convert_error_code(__func__, result);
282         /* LCOV_EXCL_STOP */
283 }
284
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)
286 {
287         net_nfc_error_e result;
288
289         LOG_BEGIN(); // LCOV_EXCL_LINE
290
291         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
292
293         /* LCOV_EXCL_START */
294         CHECK_INIT();
295
296         result = net_nfc_client_se_set_default_route_sync(powered_on_status, powered_off_status, low_battery_status);
297
298         return nfc_common_convert_error_code(__func__, result);
299         /* LCOV_EXCL_STOP */
300 }
301
302 int nfc_se_is_activated_handler_for_aid(nfc_se_type_e se_type, const char *aid, bool *is_activated_handler)
303 {
304         net_nfc_error_e result;
305
306         LOG_BEGIN(); // LCOV_EXCL_LINE
307
308         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
309
310         /* LCOV_EXCL_START */
311         CHECK_INIT();
312         CHECK_INVALID(aid == NULL);
313         CHECK_INVALID(is_activated_handler == NULL);
314
315         result = net_nfc_client_se_is_activated_aid_handler_sync((net_nfc_se_type_e)se_type, aid, is_activated_handler);
316
317         return nfc_common_convert_error_code(__func__, result);
318         /* LCOV_EXCL_STOP */
319 }
320
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)
322 {
323         net_nfc_error_e result;
324
325         LOG_BEGIN();
326
327         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
328
329         /* LCOV_EXCL_START */
330         CHECK_INIT();
331         CHECK_INVALID(is_activated_handler == NULL);
332
333         result = net_nfc_client_se_is_activated_category_handler_sync((net_nfc_se_type_e)se_type, category, is_activated_handler);
334
335         return nfc_common_convert_error_code(__func__, result);
336         /* LCOV_EXCL_STOP */
337 }
338
339 int nfc_se_register_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
340 {
341         net_nfc_error_e result;
342
343         LOG_BEGIN(); // LCOV_EXCL_LINE
344
345         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
346
347         /* LCOV_EXCL_START */
348         CHECK_INIT();
349         CHECK_INVALID(aid == NULL);
350
351         result = net_nfc_client_se_register_aids_sync((net_nfc_se_type_e)se_type, category, aid);
352
353         return nfc_common_convert_error_code(__func__, result);
354         /* LCOV_EXCL_STOP */
355 }
356
357 int nfc_se_unregister_aid(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, const char *aid)
358 {
359         net_nfc_error_e result;
360
361         LOG_BEGIN(); // LCOV_EXCL_LINE
362
363         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
364
365         /* LCOV_EXCL_START */
366         CHECK_INIT();
367         CHECK_INVALID(aid == NULL);
368
369         result = net_nfc_client_se_unregister_aid_sync((net_nfc_se_type_e)se_type, category, aid);
370
371         return nfc_common_convert_error_code(__func__, result);
372         /* LCOV_EXCL_STOP */
373 }
374
375 int nfc_se_get_registered_aids_count(nfc_se_type_e se_type, nfc_card_emulation_category_type_e category, int *count)
376 {
377         net_nfc_error_e result;
378
379         LOG_BEGIN(); // LCOV_EXCL_LINE
380
381         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
382
383         /* LCOV_EXCL_START */
384         CHECK_INIT();
385         CHECK_INVALID(count == NULL);
386         CHECK_ACTIVATED();
387
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,
391                         (size_t *)count);
392
393         return nfc_common_convert_error_code(__func__, result);
394         /* LCOV_EXCL_STOP */
395 }
396
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)
400 {
401         LOG_BEGIN();
402
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,
406                         aid,
407                         readonly,
408                         gdbus_nfc_context.on_se_registered_aid_cb_user_data);
409         }
410 }
411 /* LCOV_EXCL_STOP */
412
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)
414 {
415         net_nfc_error_e result;
416
417         LOG_BEGIN(); // LCOV_EXCL_LINE
418
419         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
420
421         /* LCOV_EXCL_START */
422         CHECK_INIT();
423         CHECK_INVALID(callback == NULL);
424         CHECK_INVALID(category < NFC_CARD_EMULATION_CATEGORY_PAYMENT || category > NFC_CARD_EMULATION_CATEGORY_OTHER);
425         CHECK_ACTIVATED();
426
427         gdbus_nfc_context.on_se_registered_aid_cb = callback;
428         gdbus_nfc_context.on_se_registered_aid_cb_user_data = user_data;
429
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,
433                 user_data);
434
435         return nfc_common_convert_error_code(__func__, result);
436         /* LCOV_EXCL_STOP */
437 }
438
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)
441 {
442         net_nfc_error_e result;
443
444         LOG_BEGIN(); // LCOV_EXCL_LINE
445
446         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
447
448         /* LCOV_EXCL_START */
449         CHECK_INIT();
450         CHECK_INVALID(aid == NULL);
451         CHECK_INVALID(pkg_name == NULL);
452
453         result = net_nfc_client_se_add_route_aid_sync(pkg_name, se_type, category, aid, unlock_required, power);
454
455         return nfc_common_convert_error_code(__func__, result);
456         /* LCOV_EXCL_STOP */
457 }
458
459 int nfc_se_remove_route_for_aid_internal(const char* pkg_name, const char *aid)
460 {
461         net_nfc_error_e result;
462
463         LOG_BEGIN(); // LCOV_EXCL_LINE
464
465         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
466
467         /* LCOV_EXCL_START */
468         CHECK_INIT();
469         CHECK_INVALID(pkg_name == NULL);
470
471         if (aid == NULL)
472                 result = net_nfc_client_se_remove_package_aids_sync(pkg_name);
473         else
474                 result = net_nfc_client_se_remove_route_aid_sync(pkg_name, aid);
475
476         return nfc_common_convert_error_code(__func__, result);
477         /* LCOV_EXCL_STOP */
478 }
479
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)
483 {
484         net_nfc_error_e result;
485
486         LOG_BEGIN(); // LCOV_EXCL_LINE
487
488         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
489         CHECK_INIT();
490         CHECK_INVALID(callback == NULL);
491
492         result = net_nfc_client_se_foreach_registered_handlers_sync(category,
493                 callback, user_data);
494
495         return nfc_common_convert_error_code(__func__, result);
496 }
497
498 int nfc_se_set_preferred_handler()
499 {
500         net_nfc_error_e result;
501
502         LOG_BEGIN(); // LCOV_EXCL_LINE
503
504         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
505
506         /* LCOV_EXCL_START */
507         CHECK_INIT();
508         CHECK_ACTIVATED();
509
510         result = net_nfc_client_se_set_preferred_handler_sync(true);
511
512         return nfc_common_convert_error_code(__func__, result);
513         /* LCOV_EXCL_STOP */
514 }
515
516 int nfc_se_unset_preferred_handler()
517 {
518         net_nfc_error_e result;
519
520         LOG_BEGIN(); // LCOV_EXCL_LINE
521
522         CHECK_SUPPORTED(NFC_CE_FEATURE); // LCOV_EXCL_LINE
523
524         /* LCOV_EXCL_START */
525         CHECK_INIT();
526         CHECK_ACTIVATED();
527
528         result = net_nfc_client_se_set_preferred_handler_sync(false);
529
530         return nfc_common_convert_error_code(__func__, result);
531         /* LCOV_EXCL_STOP */
532 }
533
534 int nfc_se_get_conflict_handlers_internal(const char *package,
535         nfc_card_emulation_category_type_e category, const char *aid, char ***handlers)
536 {
537         net_nfc_error_e result;
538
539         LOG_BEGIN(); // LCOV_EXCL_LINE
540
541         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE); // LCOV_EXCL_LINE
542         CHECK_INIT();
543         CHECK_ACTIVATED();
544
545         result = net_nfc_client_se_get_conflict_handlers_sync(package, category, aid, handlers);
546
547         return nfc_common_convert_error_code(__func__, result);
548 }