1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include "amp_config.h"
9 #include "amp_defines.h"
10 #include "aos_system.h"
11 #include "aos_pm.h"
12 #include "be_inl.h"
13 
14 
15 #define MOD_STR "Charger"
16 
17 
native_charger_conn_state_get(duk_context * ctx)18 static duk_ret_t native_charger_conn_state_get(duk_context *ctx)
19 {
20     int ret = -1;
21 	int state;
22 
23     if (aos_charger_connect_state_get(&state)) {
24         amp_error(MOD_STR, "get charger connection state fail");
25         goto out;
26     }
27 
28     ret = state;
29 out:
30     duk_push_int(ctx, ret);
31     return 1;
32 }
33 
native_charger_state_get(duk_context * ctx)34 static duk_ret_t native_charger_state_get(duk_context *ctx)
35 {
36     int ret = -1;
37     aos_charger_state_t state = AOS_CHARGER_STAT_SHUTDOWN;
38 
39     if (aos_charger_state_get(&state)) {
40         amp_error(MOD_STR, "get charger connection state fail");
41         goto out;
42     }
43 
44     ret = state;
45 out:
46     duk_push_int(ctx, ret);
47     return 1;
48 }
49 
native_charger_current_get(duk_context * ctx)50 static duk_ret_t native_charger_current_get(duk_context *ctx)
51 {
52     int ret = -1;
53 	int current;
54 
55     if (aos_charger_current_get(&current)) {
56         amp_error(MOD_STR, "get charger current fail");
57         goto out;
58     }
59 
60     ret = current;
61 out:
62     duk_push_int(ctx, ret);
63     return 1;
64 }
65 
native_charger_switch_set(duk_context * ctx)66 static duk_ret_t native_charger_switch_set(duk_context *ctx)
67 {
68     int ret = -1;
69     int val;
70 
71     if (!duk_is_number(ctx, 0)) {
72         amp_error(MOD_STR, "parameter must be (number)");
73         goto out;
74     }
75 
76     val = duk_get_int(ctx, 0);
77     if (val < 0) {
78         amp_error(MOD_STR, "parameter %d invalid", val);
79         goto out;
80     }
81 
82     if (aos_charger_switch_set(val)) {
83         amp_error(MOD_STR, "set charger switch fail");
84         goto out;
85     }
86     ret = 0;
87 
88 out:
89     duk_push_int(ctx, ret);
90     return 1;
91 }
92 
native_charger_connect_on(duk_context * ctx)93 static duk_ret_t native_charger_connect_on(duk_context *ctx)
94 {
95     int ret = -1;
96 
97     if (!duk_is_function(ctx, 0)) {
98         amp_warn(MOD_STR, "parameter must be (function)");
99         goto out;
100     }
101 
102     ret = 0;
103 out:
104     duk_push_int(ctx, ret);
105     return 1;
106 }
107 
108 
module_charger_register(void)109 void module_charger_register(void)
110 {
111     duk_context *ctx = be_get_context();
112     duk_push_object(ctx);
113 
114     AMP_ADD_FUNCTION("getConnectState", native_charger_conn_state_get, 0);
115     AMP_ADD_FUNCTION("getState",        native_charger_state_get, 0);
116     AMP_ADD_FUNCTION("getCurrent",      native_charger_current_get, 0);
117     AMP_ADD_FUNCTION("switch",          native_charger_switch_set, 1);
118     AMP_ADD_FUNCTION("onConnect",       native_charger_connect_on, 1);
119 
120     duk_put_prop_string(ctx, -2, "Charger");
121 }
122 
123