fd7474f73338f6f6b8f231db09ee56bb13e5f8bf
[platform/core/api/smartcard.git] / src / smartcard.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 #include <unistd.h>
17 #include <glib.h>
18 #include <gio/gio.h>
19 #include <stdlib.h>
20
21 #include <dlog.h>
22 #include <system_info.h>
23
24 #include "smartcard.h"
25 #include "smartcard_debug.h"
26
27 #ifdef TIZEN_SMARTCARD_SUPPORT
28 #include "smartcard-service.h"
29
30 #define  SE_FEATURE "http://tizen.org/feature/network.secure_element"
31 #define  SE_UICC_FEATURE "http://tizen.org/feature/network.secure_element.uicc"
32 #define  SE_ESE_FEATURE "http://tizen.org/feature/network.secure_element.ese"
33
34 #define  SMARTCARD_LOCK \
35 do { \
36         pthread_mutex_lock(&mutex); \
37 } while (0);
38
39 #define  SMARTCARD_UNLOCK \
40 do { \
41         pthread_mutex_unlock(&mutex); \
42 } while (0);
43
44 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45
46 int ref_count;
47 se_service_h se_service;
48 bool _is_initialize = false;
49
50 GList *reader_list;
51 GList *session_list;
52 GList *channel_list;
53
54 #define CHECK_SUPPORTED() \
55 do { \
56         { \
57                 if (_is_smartcard_supported() == false) { \
58                         return SMARTCARD_ERROR_NOT_SUPPORTED; \
59                 } \
60         } \
61 } while (0);
62
63
64 #define CHECK_INIT() \
65 do { \
66         SMARTCARD_LOCK; \
67         { \
68                 if (_is_initialize == false) { \
69                         SMARTCARD_UNLOCK; \
70                         return SMARTCARD_ERROR_NOT_INITIALIZED; \
71                 } \
72         } \
73         SMARTCARD_UNLOCK; \
74 } while (0);
75
76 static bool _is_smartcard_supported()
77 {
78         bool ret = false;
79         bool is_supported_se = false;
80         bool is_supported_se_uicc = false;
81         bool is_supported_se_ese = false;
82
83         system_info_get_platform_bool(SE_FEATURE, &is_supported_se);
84         system_info_get_platform_bool(SE_UICC_FEATURE, &is_supported_se_uicc);
85         system_info_get_platform_bool(SE_ESE_FEATURE, &is_supported_se_ese);
86
87         if (is_supported_se && (is_supported_se_uicc || is_supported_se_ese))
88                 ret = true;
89
90         return ret;
91 }
92
93 static int _check_precondition_reader(int handle)
94 {
95         CHECK_INIT();
96
97         SMARTCARD_LOCK;
98         if (g_list_find(g_list_first(reader_list), GINT_TO_POINTER(handle)) == NULL) {
99                 SMARTCARD_UNLOCK;
100                 return SMARTCARD_ERROR_INVALID_PARAMETER;
101         }
102         SMARTCARD_UNLOCK;
103
104         return SMARTCARD_ERROR_NONE;
105 }
106
107 static int _check_precondition_session(int handle)
108 {
109         CHECK_INIT();
110
111         SMARTCARD_LOCK;
112         if (g_list_find(g_list_first(session_list), GINT_TO_POINTER(handle)) == NULL) {
113                 SMARTCARD_UNLOCK;
114                 return SMARTCARD_ERROR_INVALID_PARAMETER;
115         }
116         SMARTCARD_UNLOCK;
117
118         return SMARTCARD_ERROR_NONE;
119 }
120
121 static int _check_precondition_channel(int handle)
122 {
123         CHECK_INIT();
124
125         SMARTCARD_LOCK;
126         if (g_list_find(g_list_first(channel_list), GINT_TO_POINTER(handle)) == NULL) {
127                 SMARTCARD_UNLOCK;
128                 return SMARTCARD_ERROR_INVALID_PARAMETER;
129         }
130         /* LCOV_EXCL_START */
131         SMARTCARD_UNLOCK;
132
133         return SMARTCARD_ERROR_NONE;
134         /* LCOV_EXCL_STOP */
135 }
136
137 static smartcard_error_e _convert_error_code(const char *func, int native_error_code)
138 {
139         smartcard_error_e error_code = SMARTCARD_ERROR_NONE;
140         char *errorstr = NULL;
141
142         switch (native_error_code) {
143         case SCARD_ERROR_OK:
144                 error_code = SMARTCARD_ERROR_NONE;
145                 errorstr = "SMARTCARD_ERROR_NONE";
146                 break;
147         case SCARD_ERROR_NOT_SUPPORTED:
148                 /* LCOV_EXCL_START */
149                 error_code = SMARTCARD_ERROR_OPERATION_NOT_SUPPORTED;
150                 errorstr = "SMARTCARD_ERROR_OPERATION_NOT_SUPPORTED";
151                 break;
152                 /* LCOV_EXCL_STOP */
153         case SCARD_ERROR_UNAVAILABLE:
154                 /* LCOV_EXCL_START */
155                 error_code = SMARTCARD_ERROR_CHANNEL_NOT_AVAILABLE;
156                 errorstr = "SMARTCARD_ERROR_CHANNEL_NOT_AVAILABLE";
157                 break;
158                 /* LCOV_EXCL_STOP */
159         case SCARD_ERROR_IPC_FAILED:
160         case SCARD_ERROR_IO_FAILED:
161                 /* LCOV_EXCL_START */
162                 error_code = SMARTCARD_ERROR_IO_ERROR;
163                 errorstr = "SMARTCARD_ERROR_IO_ERROR";
164                 break;
165                 /* LCOV_EXCL_STOP */
166         case SCARD_ERROR_SECURITY_NOT_ALLOWED:
167                 /* LCOV_EXCL_START */
168                 error_code = SMARTCARD_ERROR_PERMISSION_DENIED;
169                 errorstr = "SMARTCARD_ERROR_PERMISSION_DENIED";
170                 break;
171                 /* LCOV_EXCL_STOP */
172         case SCARD_ERROR_ILLEGAL_STATE:
173                 /* LCOV_EXCL_START */
174                 error_code = SMARTCARD_ERROR_ILLEGAL_STATE;
175                 errorstr = "SMARTCARD_ERROR_ILLEGAL_STATE";
176                 break;
177                 /* LCOV_EXCL_STOP */
178         case SCARD_ERROR_ILLEGAL_PARAM:
179                 /* LCOV_EXCL_START */
180                 error_code = SMARTCARD_ERROR_INVALID_PARAMETER;
181                 errorstr = "SMARTCARD_ERROR_INVALID_PARAMETER";
182                 break;
183                 /* LCOV_EXCL_STOP */
184         case SCARD_ERROR_ILLEGAL_REFERENCE:
185                 /* LCOV_EXCL_START */
186                 error_code = SMARTCARD_ERROR_ILLEGAL_REFERENCE;
187                 errorstr = "SMARTCARD_ERROR_ILLEGAL_REFERENCE";
188                 break;
189                 /* LCOV_EXCL_STOP */
190         case SCARD_ERROR_NOT_INITIALIZED:
191         case SCARD_ERROR_SE_NOT_INITIALIZED:
192         case SCARD_ERROR_OPERATION_NOT_SUPPORTED:
193         case SCARD_ERROR_NEED_MORE_BUFFER:
194         case SCARD_ERROR_OPERATION_TIMEOUT:
195         case SCARD_ERROR_NOT_ENOUGH_RESOURCE:
196         case SCARD_ERROR_OUT_OF_MEMORY:
197         case SCARD_ERROR_UNKNOWN:
198         default:
199                 /* LCOV_EXCL_START */
200                 error_code = SMARTCARD_ERROR_GENERAL;
201                 errorstr = "SMARTCARD_ERROR_GENERAL";
202                 /* LCOV_EXCL_STOP */
203         }
204
205         _ERR("smartcard func : %s, %s(0x%08x)\n", func, errorstr, error_code);
206
207         return error_code;
208 }
209 #endif
210
211 /* managing apis : initaialize, deinitialize */
212
213 int smartcard_initialize(void)
214 {
215 #ifndef TIZEN_SMARTCARD_SUPPORT
216         return SMARTCARD_ERROR_NOT_SUPPORTED;
217 #else
218         int ret = SMARTCARD_ERROR_NONE;
219
220         _BEGIN();
221         CHECK_SUPPORTED();
222
223         SMARTCARD_LOCK;
224
225                 if (se_service == NULL || ref_count > 0) {
226                         /* create se service instance. */
227                         se_service = se_service_create_instance_sync(NULL, &ret);
228                         if (se_service == NULL || ret != SCARD_ERROR_OK) {
229                                 SMARTCARD_UNLOCK; /* LCOV_EXCL_LINE */
230                                 if (ret == SCARD_ERROR_SECURITY_NOT_ALLOWED) /* LCOV_EXCL_LINE */
231                                         return SMARTCARD_ERROR_PERMISSION_DENIED;
232
233                                 return SMARTCARD_ERROR_GENERAL; /* LCOV_EXCL_LINE */
234                         }
235
236                         _is_initialize = true;
237                 }
238                 ref_count++;
239
240         SMARTCARD_UNLOCK;
241
242         _END();
243
244         return _convert_error_code(__func__, ret);
245 #endif
246 }
247
248 int smartcard_deinitialize(void)
249 {
250 #ifndef TIZEN_SMARTCARD_SUPPORT
251         return SMARTCARD_ERROR_NOT_SUPPORTED;
252 #else
253         int ret = SMARTCARD_ERROR_NONE;
254
255         _BEGIN();
256         CHECK_SUPPORTED();
257
258         SMARTCARD_LOCK;
259
260                 if (ref_count > 0) {
261                         ref_count--;
262                 } else {
263                         SMARTCARD_UNLOCK;
264                         return SMARTCARD_ERROR_NOT_INITIALIZED;
265                 }
266
267                 if (se_service != NULL && ref_count == 0) {
268                         se_service_shutdown(se_service);
269
270                         /* destroy se service instance. */
271                         ret = se_service_destroy_instance(se_service);
272                         if (ret != SCARD_ERROR_OK) {
273                                 /* LCOV_EXCL_START */
274                                 SMARTCARD_UNLOCK;
275                                 return SMARTCARD_ERROR_GENERAL;
276                                 /* LCOV_EXCL_STOP */
277                         }
278
279                         se_service = NULL;
280                         g_list_free(reader_list);
281                         g_list_free(session_list);
282                         g_list_free(channel_list);
283                         reader_list = NULL;
284                         session_list = NULL;
285                         channel_list = NULL;
286                         _is_initialize = false;
287                 }
288
289         SMARTCARD_UNLOCK;
290
291         _END();
292
293         return _convert_error_code(__func__, ret);
294 #endif
295 }
296
297
298 /*----------------<SE Service mapping api>------------------*/
299
300 int smartcard_get_readers(int **readers, int *length)
301 {
302 #ifndef TIZEN_SMARTCARD_SUPPORT
303         return SMARTCARD_ERROR_NOT_SUPPORTED;
304 #else
305         int i;
306         int ret = SMARTCARD_ERROR_NONE;
307
308         _BEGIN();
309
310         /* precondition check start */
311         CHECK_SUPPORTED();
312         CHECK_INIT();
313
314         cond_expr_ret(NULL == readers, SMARTCARD_ERROR_INVALID_PARAMETER);
315         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
316
317         /* precondition check end */
318
319         SMARTCARD_LOCK;
320
321         ret = se_service_get_readers(se_service, readers, length);
322
323         SMARTCARD_UNLOCK;
324
325         for (i = 0; i < *length; i++) {
326                 SMARTCARD_LOCK;
327
328                         if (g_list_find(g_list_first(reader_list), GINT_TO_POINTER(*(readers[i]))) == NULL)
329                                 reader_list = g_list_append(reader_list, GINT_TO_POINTER(*(readers[i])));
330
331                 SMARTCARD_UNLOCK;
332         }
333
334         _END();
335
336         return _convert_error_code(__func__, ret);
337 #endif
338 }
339
340 /*----------------<reader mapping api>------------------*/
341
342 int smartcard_reader_get_name(int reader, char **reader_name)
343 {
344 #ifndef TIZEN_SMARTCARD_SUPPORT
345         return SMARTCARD_ERROR_NOT_SUPPORTED;
346 #else
347         int ret = SMARTCARD_ERROR_NONE;
348
349         _BEGIN();
350
351         /* precondition check start */
352         CHECK_SUPPORTED();
353         CHECK_INIT();
354
355         cond_expr_ret(NULL == reader_name, SMARTCARD_ERROR_INVALID_PARAMETER);
356
357         ret = _check_precondition_reader(reader);
358         cond_ret(ret);
359
360         /* precondition check end */
361
362         /* process */
363         ret = reader_get_name((reader_h)reader, reader_name);
364
365         if (ret != SCARD_ERROR_OK || strlen(*reader_name) == 0)
366                 return SMARTCARD_ERROR_GENERAL;
367
368         _END();
369
370         return SMARTCARD_ERROR_NONE;
371 #endif
372 }
373
374 int smartcard_reader_is_secure_element_present(int reader, bool *is_present)
375 {
376 #ifndef TIZEN_SMARTCARD_SUPPORT
377         return SMARTCARD_ERROR_NOT_SUPPORTED;
378 #else
379         int ret = SMARTCARD_ERROR_NONE;
380
381         _BEGIN();
382
383         /* precondition check start */
384         CHECK_SUPPORTED();
385         CHECK_INIT();
386
387         cond_expr_ret(NULL == is_present, SMARTCARD_ERROR_INVALID_PARAMETER);
388
389         ret = _check_precondition_reader(reader);
390         cond_ret(ret);
391
392         /* precondition check end */
393
394         ret = reader_is_secure_element_present((reader_h)reader, is_present);
395
396         _END();
397
398         return _convert_error_code(__func__, ret);
399 #endif
400 }
401
402 int smartcard_reader_open_session(int reader, int *session)
403 {
404 #ifndef TIZEN_SMARTCARD_SUPPORT
405         return SMARTCARD_ERROR_NOT_SUPPORTED;
406 #else
407         int ret = SMARTCARD_ERROR_NONE;
408
409         _BEGIN();
410
411         /* precondition check start */
412         CHECK_SUPPORTED();
413         CHECK_INIT();
414
415         ret = _check_precondition_reader(reader);
416         cond_ret(ret);
417
418         cond_expr_ret(NULL == session, SMARTCARD_ERROR_INVALID_PARAMETER);
419
420         /* precondition check end */
421
422         ret = reader_open_session_sync((reader_h)reader, session);
423
424         if (ret == SCARD_ERROR_OK) {
425                 SMARTCARD_LOCK;
426                         if (g_list_find(g_list_first(session_list), GINT_TO_POINTER(*session)) == NULL)
427                                 session_list = g_list_append(session_list, GINT_TO_POINTER(*session));
428                 SMARTCARD_UNLOCK;
429         }
430
431         _END();
432
433         return _convert_error_code(__func__, ret);
434 #endif
435 }
436
437 int smartcard_reader_close_sessions(int reader)
438 {
439 #ifndef TIZEN_SMARTCARD_SUPPORT
440         return SMARTCARD_ERROR_NOT_SUPPORTED;
441 #else
442         int ret = SMARTCARD_ERROR_NONE;
443
444         _BEGIN();
445
446         /* precondition check start */
447         CHECK_SUPPORTED();
448         CHECK_INIT();
449
450         ret = _check_precondition_reader(reader);
451         cond_ret(ret);
452
453         /* precondition check end */
454
455         ret = reader_close_sessions((reader_h)reader);
456
457         _END();
458
459         return _convert_error_code(__func__, ret);
460 #endif
461 }
462
463 /*----------------<session mapping api>------------------*/
464
465 int smartcard_session_get_reader(int session, int *reader)
466 {
467 #ifndef TIZEN_SMARTCARD_SUPPORT
468         return SMARTCARD_ERROR_NOT_SUPPORTED;
469 #else
470         int ret = SMARTCARD_ERROR_NONE;
471
472         _BEGIN();
473
474         /* precondition check start */
475         CHECK_SUPPORTED();
476         CHECK_INIT();
477
478         cond_expr_ret(NULL == reader, SMARTCARD_ERROR_INVALID_PARAMETER);
479
480         ret = _check_precondition_session(session);
481         cond_ret(ret);
482
483         /* precondition check end */
484
485         ret = session_get_reader((session_h)session, reader);
486
487         _END();
488
489         return _convert_error_code(__func__, ret);
490 #endif
491 }
492
493 int smartcard_session_get_atr(int session, unsigned char **atr, int *length)
494 {
495 #ifndef TIZEN_SMARTCARD_SUPPORT
496         return SMARTCARD_ERROR_NOT_SUPPORTED;
497 #else
498         int ret = SMARTCARD_ERROR_NONE;
499
500         _BEGIN();
501
502         /* precondition check start */
503         CHECK_SUPPORTED();
504         CHECK_INIT();
505
506         cond_expr_ret(NULL == atr, SMARTCARD_ERROR_INVALID_PARAMETER);
507         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
508
509         ret = _check_precondition_session(session);
510         cond_ret(ret);
511
512         *length = 0;
513         *atr = NULL;
514
515         /* precondition check end */
516         ret = session_get_atr_sync((session_h)session, atr, (unsigned int *)length);
517
518         if (ret == SCARD_ERROR_OK) {
519                 if (((*atr == NULL) && (*length != 0)) ||
520                         ((*atr != NULL) && (*length == 0))) {
521                         return SMARTCARD_ERROR_GENERAL;
522                 }
523         }
524
525         _END();
526
527         return _convert_error_code(__func__, ret);
528 #endif
529 }
530
531 int smartcard_session_close(int session)
532 {
533 #ifndef TIZEN_SMARTCARD_SUPPORT
534         return SMARTCARD_ERROR_NOT_SUPPORTED;
535 #else
536         int ret = SMARTCARD_ERROR_NONE;
537
538         _BEGIN();
539
540         /* precondition check start */
541         CHECK_SUPPORTED();
542         CHECK_INIT();
543
544         ret = _check_precondition_session(session);
545         cond_ret(ret);
546
547         /* precondition check end */
548
549         ret =   session_close_sync((session_h)session);
550
551         _END();
552
553         return _convert_error_code(__func__, ret);
554 #endif
555 }
556
557 int smartcard_session_is_closed(int session, bool *is_closed)
558 {
559 #ifndef TIZEN_SMARTCARD_SUPPORT
560         return SMARTCARD_ERROR_NOT_SUPPORTED;
561 #else
562         int ret = SMARTCARD_ERROR_NONE;
563
564         _BEGIN();
565
566         /* precondition check start */
567         CHECK_SUPPORTED();
568         CHECK_INIT();
569
570         cond_expr_ret(NULL == is_closed, SMARTCARD_ERROR_INVALID_PARAMETER);
571
572         ret = _check_precondition_session(session);
573         cond_ret(ret);
574
575         /* precondition check end */
576
577         ret = session_is_closed((session_h)session, is_closed);
578
579         _END();
580
581         return _convert_error_code(__func__, ret);
582 #endif
583 }
584
585 int smartcard_session_close_channels(int session)
586 {
587 #ifndef TIZEN_SMARTCARD_SUPPORT
588         return SMARTCARD_ERROR_NOT_SUPPORTED;
589 #else
590         int ret = SMARTCARD_ERROR_NONE;
591
592         _BEGIN();
593
594         /* precondition check start */
595         CHECK_SUPPORTED();
596         CHECK_INIT();
597
598         ret = _check_precondition_session(session);
599         cond_ret(ret);
600
601         /* precondition check end */
602
603         ret = session_close_channels((session_h)session);
604
605         _END();
606
607         return _convert_error_code(__func__, ret);
608 #endif
609 }
610
611 int smartcard_session_open_basic_channel(int session, unsigned char *aid, int aid_len,
612         unsigned char P2, int *channel)
613 {
614 #ifndef TIZEN_SMARTCARD_SUPPORT
615         return SMARTCARD_ERROR_NOT_SUPPORTED;
616 #else
617         int ret = SMARTCARD_ERROR_NONE;
618
619         _BEGIN();
620
621         /* precondition check start */
622         CHECK_SUPPORTED();
623         CHECK_INIT();
624
625         cond_expr_ret(NULL == channel, SMARTCARD_ERROR_INVALID_PARAMETER);
626
627         /* LCOV_EXCL_START */
628         ret = _check_precondition_session(session);
629         cond_ret(ret);
630
631         /* precondition check end */
632
633         ret = session_open_basic_channel_sync((session_h)session, aid, aid_len, P2, channel);
634
635         if (ret == SCARD_ERROR_OK) {
636                 SMARTCARD_LOCK;
637                         if (g_list_find(g_list_first(channel_list), GINT_TO_POINTER(*channel)) == NULL)
638                                 channel_list = g_list_append(channel_list, GINT_TO_POINTER(*channel));
639                 SMARTCARD_UNLOCK;
640         }
641
642         _END();
643
644         return _convert_error_code(__func__, ret);
645         /* LCOV_EXCL_STOP */
646 #endif
647 }
648
649 int smartcard_session_open_logical_channel(int session, unsigned char *aid, int aid_len,
650                 unsigned char P2, int *channel)
651 {
652 #ifndef TIZEN_SMARTCARD_SUPPORT
653         return SMARTCARD_ERROR_NOT_SUPPORTED;
654 #else
655         int ret = SMARTCARD_ERROR_NONE;
656
657         _BEGIN();
658
659         /* precondition check start */
660         CHECK_SUPPORTED();
661         CHECK_INIT();
662
663         cond_expr_ret(NULL == channel, SMARTCARD_ERROR_INVALID_PARAMETER);
664
665         /* LCOV_EXCL_START */
666         ret = _check_precondition_session(session);
667         cond_ret(ret);
668
669         /* precondition check end */
670
671         ret = session_open_logical_channel_sync((session_h)session, aid, aid_len, P2, channel);
672
673         if (ret == SCARD_ERROR_OK) {
674                 SMARTCARD_LOCK;
675                         if (g_list_find(g_list_first(channel_list), GINT_TO_POINTER(*channel)) == NULL)
676                                 channel_list = g_list_append(channel_list, GINT_TO_POINTER(*channel));
677                 SMARTCARD_UNLOCK;
678         }
679
680         _END();
681
682         return _convert_error_code(__func__, ret);
683         /* LCOV_EXCL_STOP */
684 #endif
685 }
686
687 /*----------------<channel mapping api>------------------*/
688
689 int smartcard_channel_close(int channel)
690 {
691 #ifndef TIZEN_SMARTCARD_SUPPORT
692         return SMARTCARD_ERROR_NOT_SUPPORTED;
693 #else
694         int ret = SMARTCARD_ERROR_NONE;
695
696         _BEGIN();
697
698         /* precondition check start */
699         CHECK_SUPPORTED();
700         CHECK_INIT();
701
702         ret = _check_precondition_channel(channel);
703         cond_ret(ret);
704
705         /* precondition check end */
706         /* LCOV_EXCL_START */
707         ret = channel_close_sync((channel_h)channel);
708
709         _END();
710
711         return _convert_error_code(__func__, ret);
712         /* LCOV_EXCL_STOP */
713 #endif
714 }
715
716 int smartcard_channel_is_basic_channel(int channel, bool *is_basic_channel)
717 {
718 #ifndef TIZEN_SMARTCARD_SUPPORT
719         return SMARTCARD_ERROR_NOT_SUPPORTED;
720 #else
721         int ret = SMARTCARD_ERROR_NONE;
722
723         _BEGIN();
724
725         /* precondition check start */
726         CHECK_SUPPORTED();
727         CHECK_INIT();
728
729         cond_expr_ret(NULL == is_basic_channel, SMARTCARD_ERROR_INVALID_PARAMETER);
730
731         /* LCOV_EXCL_START */
732         ret = _check_precondition_channel(channel);
733         cond_ret(ret);
734
735         /* precondition check end */
736
737         ret = channel_is_basic_channel((channel_h)channel, is_basic_channel);
738
739         _END();
740
741         return _convert_error_code(__func__, ret);
742         /* LCOV_EXCL_STOP */
743 #endif
744 }
745
746 int smartcard_channel_is_closed(int channel, bool *is_closed)
747 {
748 #ifndef TIZEN_SMARTCARD_SUPPORT
749         return SMARTCARD_ERROR_NOT_SUPPORTED;
750 #else
751         int ret = SMARTCARD_ERROR_NONE;
752
753         _BEGIN();
754
755         /* precondition check start */
756         CHECK_SUPPORTED();
757         CHECK_INIT();
758
759         cond_expr_ret(NULL == is_closed, SMARTCARD_ERROR_INVALID_PARAMETER);
760
761         /* LCOV_EXCL_START */
762         ret = _check_precondition_channel(channel);
763         cond_ret(ret);
764
765         /* precondition check end */
766
767         ret = channel_is_closed((channel_h)channel, is_closed);
768
769         _END();
770
771         return _convert_error_code(__func__, ret);
772         /* LCOV_EXCL_STOP */
773 #endif
774 }
775
776 int smartcard_channel_get_select_response(int channel, unsigned char **select_resp,
777         int *length)
778 {
779 #ifndef TIZEN_SMARTCARD_SUPPORT
780         return SMARTCARD_ERROR_NOT_SUPPORTED;
781 #else
782         size_t s_length;
783         int ret = SMARTCARD_ERROR_NONE;
784
785         _BEGIN();
786
787         /* precondition check start */
788         CHECK_SUPPORTED();
789         CHECK_INIT();
790
791         cond_expr_ret(NULL == select_resp, SMARTCARD_ERROR_INVALID_PARAMETER);
792         /* LCOV_EXCL_START */
793         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
794
795         ret = _check_precondition_channel(channel);
796         cond_ret(ret);
797
798         /* precondition check end */
799
800         ret = channel_get_select_response((channel_h)channel, select_resp, &s_length);
801         *length = (int)s_length;
802
803         _END();
804
805         return _convert_error_code(__func__, ret);
806         /* LCOV_EXCL_STOP */
807 #endif
808 }
809
810 int smartcard_channel_get_session(int channel, int *session)
811 {
812 #ifndef TIZEN_SMARTCARD_SUPPORT
813         return SMARTCARD_ERROR_NOT_SUPPORTED;
814 #else
815         int ret = SMARTCARD_ERROR_NONE;
816
817         _BEGIN();
818
819         /* precondition check start */
820         CHECK_SUPPORTED();
821         CHECK_INIT();
822
823         cond_expr_ret(NULL == session, SMARTCARD_ERROR_INVALID_PARAMETER);
824
825         /* LCOV_EXCL_START */
826         ret = _check_precondition_channel(channel);
827         cond_ret(ret);
828
829         /* precondition check end */
830
831         ret = channel_get_session((channel_h)channel, session);
832
833         _END();
834
835         return _convert_error_code(__func__, ret);
836         /* LCOV_EXCL_STOP */
837 #endif
838 }
839
840 int smartcard_channel_transmit(int channel, unsigned char *cmd, int cmd_len,
841         unsigned char **resp, int *length)
842 {
843 #ifndef TIZEN_SMARTCARD_SUPPORT
844         return SMARTCARD_ERROR_NOT_SUPPORTED;
845 #else
846         int ret = SMARTCARD_ERROR_NONE;
847
848         _BEGIN();
849
850         /* precondition check start */
851         CHECK_SUPPORTED();
852         CHECK_INIT();
853
854         cond_expr_ret(NULL == resp, SMARTCARD_ERROR_INVALID_PARAMETER);
855         /* LCOV_EXCL_START */
856         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
857
858         ret = _check_precondition_channel(channel);
859         cond_ret(ret);
860
861         /* precondition check end */
862
863         ret = channel_transmit_sync((channel_h)channel, cmd, cmd_len, resp, (unsigned int *)length);
864
865         _END();
866
867         return _convert_error_code(__func__, ret);
868         /* LCOV_EXCL_STOP */
869 #endif
870 }
871
872 int smartcard_channel_transmit_retrieve_response(int channel, unsigned char **resp, int *length)
873 {
874 #ifndef TIZEN_SMARTCARD_SUPPORT
875         return SMARTCARD_ERROR_NOT_SUPPORTED;
876 #else
877         size_t s_length;
878         int ret = SMARTCARD_ERROR_NONE;
879
880         _BEGIN();
881
882         /* precondition check start */
883         CHECK_SUPPORTED();
884         CHECK_INIT();
885
886         cond_expr_ret(NULL == resp, SMARTCARD_ERROR_INVALID_PARAMETER);
887         /* LCOV_EXCL_START */
888         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
889
890         ret = _check_precondition_channel(channel);
891         cond_ret(ret);
892
893         /* precondition check end */
894
895         ret = channel_get_transmit_response((channel_h)channel, resp, &s_length);
896         *length = (int)s_length;
897
898         _END();
899
900         return _convert_error_code(__func__, ret);
901         /* LCOV_EXCL_STOP */
902 #endif
903 }
904
905 int smartcard_channel_select_next(int channel, bool *is_success)
906 {
907 #ifndef TIZEN_SMARTCARD_SUPPORT
908         return SMARTCARD_ERROR_NOT_SUPPORTED;
909 #else
910         int ret = SMARTCARD_ERROR_NONE;
911
912         _BEGIN();
913
914         /* precondition check start */
915         CHECK_SUPPORTED();
916         CHECK_INIT();
917
918         cond_expr_ret(NULL == is_success, SMARTCARD_ERROR_INVALID_PARAMETER);
919
920         /* LCOV_EXCL_START */
921         ret = _check_precondition_channel(channel);
922         cond_ret(ret);
923
924         /* precondition check end */
925
926         ret = channel_select_next((channel_h)channel, is_success);
927
928         _END();
929
930         return _convert_error_code(__func__, ret);
931         /* LCOV_EXCL_STOP */
932 #endif
933 }