1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 /* #define LOG_NDEBUG 0 */
6 #include <stdint.h>
7 
8 #include "amp_config.h"
9 #include "amp_defines.h"
10 #include "aos_hal_wdg.h"
11 #include "board_mgr.h"
12 #include "quickjs.h"
13 #include "quickjs_addon_common.h"
14 #include "aos_system.h"
15 
16 #define MOD_STR "WDG"
17 
18 #ifndef countof
19 #define countof(x) (sizeof(x) / sizeof((x)[0]))
20 #endif
21 
22 static wdg_dev_t wdg_dev = {0};
23 static JSClassID js_wdg_class_id;
24 
native_wdg_start(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)25 static JSValue native_wdg_start(JSContext *ctx, JSValueConst this_val,
26                           int argc, JSValueConst *argv)
27 {
28     int ret           = -1;
29     int32_t timeout   = 0;
30     int8_t result   = -1;
31     wdg_dev_t *handle = (wdg_dev_t *)&wdg_dev;
32 
33     if((argc < 1) || (0 != JS_ToInt32(ctx, &timeout, argv[0])))
34     {
35         amp_warn(MOD_STR, "parameter is invalid\n");
36         goto out;
37     }
38 
39     handle->config.timeout = timeout;
40     ret                    = aos_hal_wdg_init(handle);
41     handle->config.timeout = (ret == 0) ? timeout : 0;
42 
43 out:
44     return JS_NewInt32(ctx, result);
45 }
46 
native_wdg_feed(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)47 static JSValue native_wdg_feed(JSContext *ctx, JSValueConst this_val,
48                           int argc, JSValueConst *argv)
49 {
50     int8_t result   = -1;
51     wdg_dev_t *handle = (wdg_dev_t *)&wdg_dev;
52     aos_hal_wdg_reload(handle);
53     result = 0;
54 
55 out:
56     return JS_NewInt32(ctx, result);
57 }
58 
native_wdg_stop(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)59 static JSValue native_wdg_stop(JSContext *ctx, JSValueConst this_val,
60                           int argc, JSValueConst *argv)
61 {
62     int8_t result = -1;
63     wdg_dev_t *handle = (wdg_dev_t *)&wdg_dev;
64     aos_hal_wdg_finalize(handle);
65     handle->config.timeout = 0;
66     result = 0;
67 
68 out:
69     return JS_NewInt32(ctx, result);
70 }
71 
72 static JSClassDef js_wdg_class = {
73     "WDG",
74 };
75 
76 static const JSCFunctionListEntry js_wdg_funcs[] = {
77     JS_CFUNC_DEF("start", 1, native_wdg_start),
78     JS_CFUNC_DEF("stop", 0, native_wdg_stop ),
79     JS_CFUNC_DEF("feed", 0, native_wdg_feed ),
80 };
81 
js_wdg_init(JSContext * ctx,JSModuleDef * m)82 static int js_wdg_init(JSContext *ctx, JSModuleDef *m)
83 {
84     JSValue proto;
85 
86     JS_NewClassID(&js_wdg_class_id);
87 
88     JS_NewClass(JS_GetRuntime(ctx), js_wdg_class_id, &js_wdg_class);
89     proto = JS_NewObject(ctx);
90     JS_SetPropertyFunctionList(ctx, proto, js_wdg_funcs,
91                                countof(js_wdg_funcs));
92     JS_SetClassProto(ctx, js_wdg_class_id, proto);
93 
94     return JS_SetModuleExportList(ctx, m, js_wdg_funcs,
95                                   countof(js_wdg_funcs));
96 }
97 
js_init_module_wdg(JSContext * ctx,const char * module_name)98 JSModuleDef *js_init_module_wdg(JSContext *ctx, const char *module_name)
99 {
100     JSModuleDef *m;
101     m = JS_NewCModule(ctx, module_name, js_wdg_init);
102     if (!m)
103         return NULL;
104     JS_AddModuleExportList(ctx, m, js_wdg_funcs, countof(js_wdg_funcs));
105     return m;
106 }
107 
module_wdg_register(void)108 void module_wdg_register(void)
109 {
110     amp_debug(MOD_STR, "module_wdg_register");
111     JSContext *ctx = js_get_context();
112     aos_printf("module wdg register\n");
113     js_init_module_wdg(ctx, "WDG");
114 }
115