Add reader event callback set / unset api
[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_ENOUGH_RESOURCE:
191         case SCARD_ERROR_OUT_OF_MEMORY:
192                 /* LCOV_EXCL_START */
193                 error_code = SMARTCARD_ERROR_OUT_OF_MEMORY;
194                 errorstr = "SMARTCARD_ERROR_OUT_OF_MEMORY";
195                 break;
196                 /* LCOV_EXCL_STOP */
197         case SCARD_ERROR_NOT_INITIALIZED:
198         case SCARD_ERROR_SE_NOT_INITIALIZED:
199         case SCARD_ERROR_OPERATION_NOT_SUPPORTED:
200         case SCARD_ERROR_NEED_MORE_BUFFER:
201         case SCARD_ERROR_OPERATION_TIMEOUT:
202         case SCARD_ERROR_UNKNOWN:
203         default:
204                 /* LCOV_EXCL_START */
205                 error_code = SMARTCARD_ERROR_GENERAL;
206                 errorstr = "SMARTCARD_ERROR_GENERAL";
207                 /* LCOV_EXCL_STOP */
208         }
209
210         _ERR("smartcard func : %s, %s(0x%08x)\n", func, errorstr, error_code);
211
212         return error_code;
213 }
214 #endif
215
216 /* managing apis : initaialize, deinitialize */
217
218 int smartcard_initialize(void)
219 {
220 #ifndef TIZEN_SMARTCARD_SUPPORT
221         return SMARTCARD_ERROR_NOT_SUPPORTED;
222 #else
223         int ret = SMARTCARD_ERROR_NONE;
224
225         _BEGIN();
226         CHECK_SUPPORTED();
227
228         SMARTCARD_LOCK;
229
230                 if (se_service == NULL || ref_count > 0) {
231                         /* create se service instance. */
232                         se_service = se_service_create_instance_sync(NULL, &ret);
233                         if (se_service == NULL || ret != SCARD_ERROR_OK) {
234                                 SMARTCARD_UNLOCK; /* LCOV_EXCL_LINE */
235                                 if (ret == SCARD_ERROR_SECURITY_NOT_ALLOWED) /* LCOV_EXCL_LINE */
236                                         return SMARTCARD_ERROR_PERMISSION_DENIED;
237
238                                 return SMARTCARD_ERROR_GENERAL; /* LCOV_EXCL_LINE */
239                         }
240
241                         _is_initialize = true;
242                 }
243                 ref_count++;
244
245         SMARTCARD_UNLOCK;
246
247         _END();
248
249         return _convert_error_code(__func__, ret);
250 #endif
251 }
252
253 int smartcard_deinitialize(void)
254 {
255 #ifndef TIZEN_SMARTCARD_SUPPORT
256         return SMARTCARD_ERROR_NOT_SUPPORTED;
257 #else
258         int ret = SMARTCARD_ERROR_NONE;
259
260         _BEGIN();
261         CHECK_SUPPORTED();
262
263         SMARTCARD_LOCK;
264
265                 if (ref_count > 0) {
266                         ref_count--;
267                 } else {
268                         SMARTCARD_UNLOCK;
269                         return SMARTCARD_ERROR_NOT_INITIALIZED;
270                 }
271
272                 if (se_service != NULL && ref_count == 0) {
273                         se_service_shutdown(se_service);
274
275                         /* destroy se service instance. */
276                         ret = se_service_destroy_instance(se_service);
277                         if (ret != SCARD_ERROR_OK) {
278                                 /* LCOV_EXCL_START */
279                                 SMARTCARD_UNLOCK;
280                                 return SMARTCARD_ERROR_GENERAL;
281                                 /* LCOV_EXCL_STOP */
282                         }
283
284                         se_service = NULL;
285                         g_list_free(reader_list);
286                         g_list_free(session_list);
287                         g_list_free(channel_list);
288                         reader_list = NULL;
289                         session_list = NULL;
290                         channel_list = NULL;
291                         _is_initialize = false;
292                 }
293
294         SMARTCARD_UNLOCK;
295
296         _END();
297
298         return _convert_error_code(__func__, ret);
299 #endif
300 }
301
302
303 /*----------------<SE Service mapping api>------------------*/
304
305 int smartcard_get_version(char **version)
306 {
307 #ifndef TIZEN_SMARTCARD_SUPPORT
308         return SMARTCARD_ERROR_NOT_SUPPORTED;
309 #else
310         int ret = SMARTCARD_ERROR_NONE;
311
312         _BEGIN();
313
314         /* precondition check start */
315         CHECK_SUPPORTED();
316         CHECK_INIT();
317
318         cond_expr_ret(NULL == version, SMARTCARD_ERROR_INVALID_PARAMETER);
319
320         SMARTCARD_LOCK;
321
322         ret = se_service_get_version(se_service, version);
323
324         SMARTCARD_UNLOCK;
325
326         _END();
327
328         return _convert_error_code(__func__, ret);
329 #endif
330 }
331
332 int smartcard_get_readers(int **readers, int *length)
333 {
334 #ifndef TIZEN_SMARTCARD_SUPPORT
335         return SMARTCARD_ERROR_NOT_SUPPORTED;
336 #else
337         int i;
338         int ret = SMARTCARD_ERROR_NONE;
339
340         _BEGIN();
341
342         /* precondition check start */
343         CHECK_SUPPORTED();
344         CHECK_INIT();
345
346         cond_expr_ret(NULL == readers, SMARTCARD_ERROR_INVALID_PARAMETER);
347         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
348
349         /* precondition check end */
350
351         SMARTCARD_LOCK;
352
353         ret = se_service_get_readers(se_service, readers, length);
354
355         SMARTCARD_UNLOCK;
356
357         for (i = 0; i < *length; i++) {
358                 SMARTCARD_LOCK;
359
360                         if (g_list_find(g_list_first(reader_list), GINT_TO_POINTER(*(readers[i]))) == NULL)
361                                 reader_list = g_list_append(reader_list, GINT_TO_POINTER(*(readers[i])));
362
363                 SMARTCARD_UNLOCK;
364         }
365
366         _END();
367
368         return _convert_error_code(__func__, ret);
369 #endif
370 }
371
372 /*----------------<reader mapping api>------------------*/
373
374 int smartcard_reader_set_event_cb(smartcard_reader_event_cb cb, void *user_data)
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
385         CHECK_SUPPORTED();
386         CHECK_INIT();
387
388         cond_expr_ret(NULL == cb, SMARTCARD_ERROR_INVALID_PARAMETER);
389
390         /* precondition check end */
391
392         se_service_set_event_handler(se_service, (se_service_event_cb)cb, user_data);
393
394         _END();
395
396         return _convert_error_code(__func__, ret);
397 #endif
398 }
399
400 int smartcard_reader_unset_event_cb(void)
401 {
402 #ifndef TIZEN_SMARTCARD_SUPPORT
403         return SMARTCARD_ERROR_NOT_SUPPORTED;
404 #else
405         int ret = SMARTCARD_ERROR_NONE;
406
407         _BEGIN();
408
409         /* precondition check start */
410
411         CHECK_SUPPORTED();
412         CHECK_INIT();
413
414         /* precondition check end */
415
416         se_service_unset_event_handler(se_service);
417
418         _END();
419
420         return _convert_error_code(__func__, ret);
421 #endif
422 }
423
424 int smartcard_reader_get_name(int reader, char **reader_name)
425 {
426 #ifndef TIZEN_SMARTCARD_SUPPORT
427         return SMARTCARD_ERROR_NOT_SUPPORTED;
428 #else
429         int ret = SMARTCARD_ERROR_NONE;
430
431         _BEGIN();
432
433         /* precondition check start */
434         CHECK_SUPPORTED();
435         CHECK_INIT();
436
437         cond_expr_ret(NULL == reader_name, SMARTCARD_ERROR_INVALID_PARAMETER);
438
439         ret = _check_precondition_reader(reader);
440         cond_ret(ret);
441
442         /* precondition check end */
443
444         /* process */
445         ret = reader_get_name((reader_h)reader, reader_name);
446
447         if (ret != SCARD_ERROR_OK || strlen(*reader_name) == 0)
448                 return SMARTCARD_ERROR_GENERAL;
449
450         _END();
451
452         return SMARTCARD_ERROR_NONE;
453 #endif
454 }
455
456 int smartcard_reader_is_secure_element_present(int reader, bool *is_present)
457 {
458 #ifndef TIZEN_SMARTCARD_SUPPORT
459         return SMARTCARD_ERROR_NOT_SUPPORTED;
460 #else
461         int ret = SMARTCARD_ERROR_NONE;
462
463         _BEGIN();
464
465         /* precondition check start */
466         CHECK_SUPPORTED();
467         CHECK_INIT();
468
469         cond_expr_ret(NULL == is_present, SMARTCARD_ERROR_INVALID_PARAMETER);
470
471         ret = _check_precondition_reader(reader);
472         cond_ret(ret);
473
474         /* precondition check end */
475
476         ret = reader_is_secure_element_present((reader_h)reader, is_present);
477
478         _END();
479
480         return _convert_error_code(__func__, ret);
481 #endif
482 }
483
484 int smartcard_reader_open_session(int reader, int *session)
485 {
486 #ifndef TIZEN_SMARTCARD_SUPPORT
487         return SMARTCARD_ERROR_NOT_SUPPORTED;
488 #else
489         int ret = SMARTCARD_ERROR_NONE;
490
491         _BEGIN();
492
493         /* precondition check start */
494         CHECK_SUPPORTED();
495         CHECK_INIT();
496
497         ret = _check_precondition_reader(reader);
498         cond_ret(ret);
499
500         cond_expr_ret(NULL == session, SMARTCARD_ERROR_INVALID_PARAMETER);
501
502         /* precondition check end */
503
504         ret = reader_open_session_sync((reader_h)reader, session);
505
506         if (ret == SCARD_ERROR_OK) {
507                 SMARTCARD_LOCK;
508                         if (g_list_find(g_list_first(session_list), GINT_TO_POINTER(*session)) == NULL)
509                                 session_list = g_list_append(session_list, GINT_TO_POINTER(*session));
510                 SMARTCARD_UNLOCK;
511         }
512
513         _END();
514
515         return _convert_error_code(__func__, ret);
516 #endif
517 }
518
519 int smartcard_reader_close_sessions(int reader)
520 {
521 #ifndef TIZEN_SMARTCARD_SUPPORT
522         return SMARTCARD_ERROR_NOT_SUPPORTED;
523 #else
524         int ret = SMARTCARD_ERROR_NONE;
525
526         _BEGIN();
527
528         /* precondition check start */
529         CHECK_SUPPORTED();
530         CHECK_INIT();
531
532         ret = _check_precondition_reader(reader);
533         cond_ret(ret);
534
535         /* precondition check end */
536
537         ret = reader_close_sessions((reader_h)reader);
538
539         _END();
540
541         return _convert_error_code(__func__, ret);
542 #endif
543 }
544
545 /*----------------<session mapping api>------------------*/
546
547 int smartcard_session_get_reader(int session, int *reader)
548 {
549 #ifndef TIZEN_SMARTCARD_SUPPORT
550         return SMARTCARD_ERROR_NOT_SUPPORTED;
551 #else
552         int ret = SMARTCARD_ERROR_NONE;
553
554         _BEGIN();
555
556         /* precondition check start */
557         CHECK_SUPPORTED();
558         CHECK_INIT();
559
560         cond_expr_ret(NULL == reader, SMARTCARD_ERROR_INVALID_PARAMETER);
561
562         ret = _check_precondition_session(session);
563         cond_ret(ret);
564
565         /* precondition check end */
566
567         ret = session_get_reader((session_h)session, reader);
568
569         _END();
570
571         return _convert_error_code(__func__, ret);
572 #endif
573 }
574
575 int smartcard_session_get_atr(int session, unsigned char **atr, int *length)
576 {
577 #ifndef TIZEN_SMARTCARD_SUPPORT
578         return SMARTCARD_ERROR_NOT_SUPPORTED;
579 #else
580         int ret = SMARTCARD_ERROR_NONE;
581
582         _BEGIN();
583
584         /* precondition check start */
585         CHECK_SUPPORTED();
586         CHECK_INIT();
587
588         cond_expr_ret(NULL == atr, SMARTCARD_ERROR_INVALID_PARAMETER);
589         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
590
591         ret = _check_precondition_session(session);
592         cond_ret(ret);
593
594         *length = 0;
595         *atr = NULL;
596
597         /* precondition check end */
598         ret = session_get_atr_sync((session_h)session, atr, (unsigned int *)length);
599
600         if (ret == SCARD_ERROR_OK) {
601                 if (((*atr == NULL) && (*length != 0)) ||
602                         ((*atr != NULL) && (*length == 0))) {
603                         return SMARTCARD_ERROR_GENERAL;
604                 }
605         }
606
607         _END();
608
609         return _convert_error_code(__func__, ret);
610 #endif
611 }
612
613 int smartcard_session_close(int session)
614 {
615 #ifndef TIZEN_SMARTCARD_SUPPORT
616         return SMARTCARD_ERROR_NOT_SUPPORTED;
617 #else
618         int ret = SMARTCARD_ERROR_NONE;
619
620         _BEGIN();
621
622         /* precondition check start */
623         CHECK_SUPPORTED();
624         CHECK_INIT();
625
626         ret = _check_precondition_session(session);
627         cond_ret(ret);
628
629         /* precondition check end */
630
631         ret =   session_close_sync((session_h)session);
632
633         _END();
634
635         return _convert_error_code(__func__, ret);
636 #endif
637 }
638
639 int smartcard_session_is_closed(int session, bool *is_closed)
640 {
641 #ifndef TIZEN_SMARTCARD_SUPPORT
642         return SMARTCARD_ERROR_NOT_SUPPORTED;
643 #else
644         int ret = SMARTCARD_ERROR_NONE;
645
646         _BEGIN();
647
648         /* precondition check start */
649         CHECK_SUPPORTED();
650         CHECK_INIT();
651
652         cond_expr_ret(NULL == is_closed, SMARTCARD_ERROR_INVALID_PARAMETER);
653
654         ret = _check_precondition_session(session);
655         cond_ret(ret);
656
657         /* precondition check end */
658
659         ret = session_is_closed((session_h)session, is_closed);
660
661         _END();
662
663         return _convert_error_code(__func__, ret);
664 #endif
665 }
666
667 int smartcard_session_close_channels(int session)
668 {
669 #ifndef TIZEN_SMARTCARD_SUPPORT
670         return SMARTCARD_ERROR_NOT_SUPPORTED;
671 #else
672         int ret = SMARTCARD_ERROR_NONE;
673
674         _BEGIN();
675
676         /* precondition check start */
677         CHECK_SUPPORTED();
678         CHECK_INIT();
679
680         ret = _check_precondition_session(session);
681         cond_ret(ret);
682
683         /* precondition check end */
684
685         ret = session_close_channels((session_h)session);
686
687         _END();
688
689         return _convert_error_code(__func__, ret);
690 #endif
691 }
692
693 int smartcard_session_open_basic_channel(int session, unsigned char *aid, int aid_len,
694         unsigned char P2, int *channel)
695 {
696 #ifndef TIZEN_SMARTCARD_SUPPORT
697         return SMARTCARD_ERROR_NOT_SUPPORTED;
698 #else
699         int ret = SMARTCARD_ERROR_NONE;
700
701         _BEGIN();
702
703         /* precondition check start */
704         CHECK_SUPPORTED();
705         CHECK_INIT();
706
707         cond_expr_ret(NULL == channel, SMARTCARD_ERROR_INVALID_PARAMETER);
708
709         /* LCOV_EXCL_START */
710         ret = _check_precondition_session(session);
711         cond_ret(ret);
712
713         /* precondition check end */
714
715         ret = session_open_basic_channel_sync((session_h)session, aid, aid_len, P2, channel);
716
717         if (ret == SCARD_ERROR_OK) {
718                 SMARTCARD_LOCK;
719                         if (g_list_find(g_list_first(channel_list), GINT_TO_POINTER(*channel)) == NULL)
720                                 channel_list = g_list_append(channel_list, GINT_TO_POINTER(*channel));
721                 SMARTCARD_UNLOCK;
722         }
723
724         _END();
725
726         return _convert_error_code(__func__, ret);
727         /* LCOV_EXCL_STOP */
728 #endif
729 }
730
731 int smartcard_session_open_logical_channel(int session, unsigned char *aid, int aid_len,
732                 unsigned char P2, int *channel)
733 {
734 #ifndef TIZEN_SMARTCARD_SUPPORT
735         return SMARTCARD_ERROR_NOT_SUPPORTED;
736 #else
737         int ret = SMARTCARD_ERROR_NONE;
738
739         _BEGIN();
740
741         /* precondition check start */
742         CHECK_SUPPORTED();
743         CHECK_INIT();
744
745         cond_expr_ret(NULL == channel, SMARTCARD_ERROR_INVALID_PARAMETER);
746
747         /* LCOV_EXCL_START */
748         ret = _check_precondition_session(session);
749         cond_ret(ret);
750
751         /* precondition check end */
752
753         ret = session_open_logical_channel_sync((session_h)session, aid, aid_len, P2, channel);
754
755         if (ret == SCARD_ERROR_OK) {
756                 SMARTCARD_LOCK;
757                         if (g_list_find(g_list_first(channel_list), GINT_TO_POINTER(*channel)) == NULL)
758                                 channel_list = g_list_append(channel_list, GINT_TO_POINTER(*channel));
759                 SMARTCARD_UNLOCK;
760         }
761
762         _END();
763
764         return _convert_error_code(__func__, ret);
765         /* LCOV_EXCL_STOP */
766 #endif
767 }
768
769 /*----------------<channel mapping api>------------------*/
770
771 int smartcard_channel_close(int channel)
772 {
773 #ifndef TIZEN_SMARTCARD_SUPPORT
774         return SMARTCARD_ERROR_NOT_SUPPORTED;
775 #else
776         int ret = SMARTCARD_ERROR_NONE;
777
778         _BEGIN();
779
780         /* precondition check start */
781         CHECK_SUPPORTED();
782         CHECK_INIT();
783
784         ret = _check_precondition_channel(channel);
785         cond_ret(ret);
786
787         /* precondition check end */
788         /* LCOV_EXCL_START */
789         ret = channel_close_sync((channel_h)channel);
790
791         _END();
792
793         return _convert_error_code(__func__, ret);
794         /* LCOV_EXCL_STOP */
795 #endif
796 }
797
798 int smartcard_channel_is_basic_channel(int channel, bool *is_basic_channel)
799 {
800 #ifndef TIZEN_SMARTCARD_SUPPORT
801         return SMARTCARD_ERROR_NOT_SUPPORTED;
802 #else
803         int ret = SMARTCARD_ERROR_NONE;
804
805         _BEGIN();
806
807         /* precondition check start */
808         CHECK_SUPPORTED();
809         CHECK_INIT();
810
811         cond_expr_ret(NULL == is_basic_channel, SMARTCARD_ERROR_INVALID_PARAMETER);
812
813         /* LCOV_EXCL_START */
814         ret = _check_precondition_channel(channel);
815         cond_ret(ret);
816
817         /* precondition check end */
818
819         ret = channel_is_basic_channel((channel_h)channel, is_basic_channel);
820
821         _END();
822
823         return _convert_error_code(__func__, ret);
824         /* LCOV_EXCL_STOP */
825 #endif
826 }
827
828 int smartcard_channel_is_closed(int channel, bool *is_closed)
829 {
830 #ifndef TIZEN_SMARTCARD_SUPPORT
831         return SMARTCARD_ERROR_NOT_SUPPORTED;
832 #else
833         int ret = SMARTCARD_ERROR_NONE;
834
835         _BEGIN();
836
837         /* precondition check start */
838         CHECK_SUPPORTED();
839         CHECK_INIT();
840
841         cond_expr_ret(NULL == is_closed, SMARTCARD_ERROR_INVALID_PARAMETER);
842
843         /* LCOV_EXCL_START */
844         ret = _check_precondition_channel(channel);
845         cond_ret(ret);
846
847         /* precondition check end */
848
849         ret = channel_is_closed((channel_h)channel, is_closed);
850
851         _END();
852
853         return _convert_error_code(__func__, ret);
854         /* LCOV_EXCL_STOP */
855 #endif
856 }
857
858 int smartcard_channel_get_select_response(int channel, unsigned char **select_resp,
859         int *length)
860 {
861 #ifndef TIZEN_SMARTCARD_SUPPORT
862         return SMARTCARD_ERROR_NOT_SUPPORTED;
863 #else
864         size_t s_length;
865         int ret = SMARTCARD_ERROR_NONE;
866
867         _BEGIN();
868
869         /* precondition check start */
870         CHECK_SUPPORTED();
871         CHECK_INIT();
872
873         cond_expr_ret(NULL == select_resp, SMARTCARD_ERROR_INVALID_PARAMETER);
874         /* LCOV_EXCL_START */
875         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
876
877         ret = _check_precondition_channel(channel);
878         cond_ret(ret);
879
880         /* precondition check end */
881
882         ret = channel_get_select_response((channel_h)channel, select_resp, &s_length);
883         *length = (int)s_length;
884
885         _END();
886
887         return _convert_error_code(__func__, ret);
888         /* LCOV_EXCL_STOP */
889 #endif
890 }
891
892 int smartcard_channel_get_session(int channel, int *session)
893 {
894 #ifndef TIZEN_SMARTCARD_SUPPORT
895         return SMARTCARD_ERROR_NOT_SUPPORTED;
896 #else
897         int ret = SMARTCARD_ERROR_NONE;
898
899         _BEGIN();
900
901         /* precondition check start */
902         CHECK_SUPPORTED();
903         CHECK_INIT();
904
905         cond_expr_ret(NULL == session, SMARTCARD_ERROR_INVALID_PARAMETER);
906
907         /* LCOV_EXCL_START */
908         ret = _check_precondition_channel(channel);
909         cond_ret(ret);
910
911         /* precondition check end */
912
913         ret = channel_get_session((channel_h)channel, session);
914
915         _END();
916
917         return _convert_error_code(__func__, ret);
918         /* LCOV_EXCL_STOP */
919 #endif
920 }
921
922 int smartcard_channel_transmit(int channel, unsigned char *cmd, int cmd_len,
923         unsigned char **resp, int *length)
924 {
925 #ifndef TIZEN_SMARTCARD_SUPPORT
926         return SMARTCARD_ERROR_NOT_SUPPORTED;
927 #else
928         int ret = SMARTCARD_ERROR_NONE;
929
930         _BEGIN();
931
932         /* precondition check start */
933         CHECK_SUPPORTED();
934         CHECK_INIT();
935
936         cond_expr_ret(NULL == resp, SMARTCARD_ERROR_INVALID_PARAMETER);
937         /* LCOV_EXCL_START */
938         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
939
940         ret = _check_precondition_channel(channel);
941         cond_ret(ret);
942
943         /* precondition check end */
944
945         ret = channel_transmit_sync((channel_h)channel, cmd, cmd_len, resp, (unsigned int *)length);
946
947         _END();
948
949         return _convert_error_code(__func__, ret);
950         /* LCOV_EXCL_STOP */
951 #endif
952 }
953
954 int smartcard_channel_transmit_retrieve_response(int channel, unsigned char **resp, int *length)
955 {
956 #ifndef TIZEN_SMARTCARD_SUPPORT
957         return SMARTCARD_ERROR_NOT_SUPPORTED;
958 #else
959         size_t s_length;
960         int ret = SMARTCARD_ERROR_NONE;
961
962         _BEGIN();
963
964         /* precondition check start */
965         CHECK_SUPPORTED();
966         CHECK_INIT();
967
968         cond_expr_ret(NULL == resp, SMARTCARD_ERROR_INVALID_PARAMETER);
969         /* LCOV_EXCL_START */
970         cond_expr_ret(NULL == length, SMARTCARD_ERROR_INVALID_PARAMETER);
971
972         ret = _check_precondition_channel(channel);
973         cond_ret(ret);
974
975         /* precondition check end */
976
977         ret = channel_get_transmit_response((channel_h)channel, resp, &s_length);
978         *length = (int)s_length;
979
980         _END();
981
982         return _convert_error_code(__func__, ret);
983         /* LCOV_EXCL_STOP */
984 #endif
985 }
986
987 int smartcard_channel_select_next(int channel, bool *is_success)
988 {
989 #ifndef TIZEN_SMARTCARD_SUPPORT
990         return SMARTCARD_ERROR_NOT_SUPPORTED;
991 #else
992         int ret = SMARTCARD_ERROR_NONE;
993
994         _BEGIN();
995
996         /* precondition check start */
997         CHECK_SUPPORTED();
998         CHECK_INIT();
999
1000         cond_expr_ret(NULL == is_success, SMARTCARD_ERROR_INVALID_PARAMETER);
1001
1002         /* LCOV_EXCL_START */
1003         ret = _check_precondition_channel(channel);
1004         cond_ret(ret);
1005
1006         /* precondition check end */
1007
1008         ret = channel_select_next((channel_h)channel, is_success);
1009
1010         _END();
1011
1012         return _convert_error_code(__func__, ret);
1013         /* LCOV_EXCL_STOP */
1014 #endif
1015 }