1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include <string.h>
6 #include <stdarg.h>
7 
8 #include "amp_config.h"
9 #include "aos_system.h"
10 #include "amp_defines.h"
11 #include "amp_task.h"
12 #include "be_inl.h"
13 
14 #include "smartcard.h"
15 
16 
17 #define MOD_STR        "Smartcard"
18 
native_smartcard_init(duk_context * ctx)19 static duk_ret_t native_smartcard_init(duk_context *ctx)
20 {
21     int ret;
22 
23     ret = smartcard_init();
24     if (ret) {
25         amp_error(MOD_STR, "smartcard init fail");
26         goto out;
27     }
28     ret = 0;
29 
30 out:
31     duk_push_int(ctx, ret);
32     return 1;
33 }
34 
native_smartcard_deinit(duk_context * ctx)35 static duk_ret_t native_smartcard_deinit(duk_context *ctx)
36 {
37     smartcard_deinit();
38     duk_push_int(ctx, 0);
39     return 1;
40 }
41 
native_smartcard_select(duk_context * ctx)42 static duk_ret_t native_smartcard_select(duk_context *ctx)
43 {
44     int ret = -1;
45     int type;
46     smartcard_change_operator_t _operator;
47 
48     if (!duk_is_number(ctx, 0)) {
49         amp_error(MOD_STR, "parameter must be (number)");
50         goto out;
51     }
52 
53     type = duk_get_int(ctx, 0);
54     switch (type) {
55     case 1:
56         _operator = SMARTCARD_CHANGE_TO_CM;
57         break;
58     case 2:
59         _operator = SMARTCARD_CHANGE_TO_CU;
60         break;
61     case 3:
62         _operator = SMARTCARD_CHANGE_TO_CT;
63         break;
64     case 4:
65         _operator = SMARTCARD_CHANGE_TO_NEXT;
66         break;
67     default:
68         amp_error(MOD_STR, "unknown operator %d", type);
69         return -1;
70     }
71 
72     ret = smartcard_change_operator(type);
73     if (ret) {
74         amp_error(MOD_STR, "change operator %d fail %d", type, ret);
75     }
76 
77 out:
78     duk_push_int(ctx, ret);
79     return 1;
80 }
81 
module_smartcard_clean(void)82 static void module_smartcard_clean(void)
83 {
84     smartcard_deinit();
85 }
86 
module_smartcard_register(void)87 void module_smartcard_register(void)
88 {
89     duk_context *ctx = be_get_context();
90 
91     amp_module_free_register(module_smartcard_clean);
92 
93     duk_push_object(ctx);
94 
95     AMP_ADD_FUNCTION("init",    native_smartcard_init, 0);
96     AMP_ADD_FUNCTION("deinit",  native_smartcard_deinit, 0);
97     AMP_ADD_FUNCTION("select",  native_smartcard_select, 1);
98 
99     duk_put_prop_string(ctx, -2, "smartcard");
100 }
101 
102