1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "py/builtin.h"
6 #include "py/mperrno.h"
7 #include "py/obj.h"
8 #include "py/runtime.h"
9 #include "ulog/ulog.h"
10 
11 #define LOG_TAG "DRIVER_UND"
12 
13 extern const mp_obj_type_t driver_und_type;
14 
15 // this is the actual C-structure for our new object
16 typedef struct {
17     // base represents some basic information, like type
18     mp_obj_base_t Base;
19     // a member created by us
20     char *ModuleName;
21 } mp_und_obj_t;
22 
und_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)23 void und_obj_print(const mp_print_t *print, mp_obj_t self_in,
24                    mp_print_kind_t kind)
25 {
26     LOGD(LOG_TAG, "entern %s;\n", __func__);
27     mp_und_obj_t *self = MP_OBJ_TO_PTR(self_in);
28     mp_printf(print, "ModuleName(%s)", self->ModuleName);
29 }
30 
und_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)31 STATIC mp_obj_t und_obj_make_new(const mp_obj_type_t *type, size_t n_args,
32                                  size_t n_kw, const mp_obj_t *args)
33 {
34     LOGD(LOG_TAG, "entern  %s;\n", __func__);
35     mp_und_obj_t *driver_obj = m_new_obj(mp_und_obj_t);
36     if (!driver_obj) {
37         mp_raise_OSError(MP_EINVAL);
38     }
39 
40     driver_obj->Base.type = &driver_und_type;
41     driver_obj->ModuleName = "und";
42 
43     return MP_OBJ_FROM_PTR(driver_obj);
44 }
45 
obj_open(size_t n_args,const mp_obj_t * args)46 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
47 {
48     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
49     int ret = -1;
50     if (n_args < 1) {
51         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
52              n_args);
53         return mp_const_none;
54     }
55     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
56     mp_und_obj_t *driver_obj = (mp_und_obj_t *)self;
57     if (driver_obj == NULL) {
58         LOGE(LOG_TAG, "driver_obj is NULL\n");
59         return mp_const_none;
60     }
61     LOGD(LOG_TAG, "%s:out\n", __func__);
62 
63     return mp_const_none;
64 }
65 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(und_obj_open, 1, obj_open);
66 
obj_close(size_t n_args,const mp_obj_t * args)67 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
68 {
69     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
70     int ret = -1;
71     if (n_args < 1) {
72         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
73              n_args);
74         return mp_const_none;
75     }
76     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
77     mp_und_obj_t *driver_obj = (mp_und_obj_t *)self;
78     if (driver_obj == NULL) {
79         LOGE(LOG_TAG, "driver_obj is NULL\n");
80         return mp_const_none;
81     }
82     LOGD(LOG_TAG, "%s:out\n", __func__);
83 
84     return mp_const_none;
85 }
86 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(und_obj_close, 1, obj_close);
87 
obj_start(size_t n_args,const mp_obj_t * args)88 STATIC mp_obj_t obj_start(size_t n_args, const mp_obj_t *args)
89 {
90     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
91     int ret = -1;
92     if (n_args < 1) {
93         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
94              n_args);
95         return mp_const_none;
96     }
97     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
98     mp_und_obj_t *driver_obj = (mp_und_obj_t *)self;
99     if (driver_obj == NULL) {
100         LOGE(LOG_TAG, "driver_obj is NULL\n");
101         return mp_const_none;
102     }
103 
104     // ret = und_init();
105     if (ret != 0) {
106         LOGE(LOG_TAG, "und_init failed\n");
107     }
108     LOGD(LOG_TAG, "%s:out\n", __func__);
109 
110     return MP_ROM_INT(ret);
111 }
112 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(und_obj_start, 1, obj_start);
113 
obj_update(size_t n_args,const mp_obj_t * args)114 STATIC mp_obj_t obj_update(size_t n_args, const mp_obj_t *args)
115 {
116     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
117     int ret = -1;
118     int32_t cap_idx;
119     int32_t reason_code;
120     if (n_args < 3) {
121         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
122              n_args);
123         return mp_const_none;
124     }
125     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
126     mp_und_obj_t *driver_obj = (mp_und_obj_t *)self;
127     if (driver_obj == NULL) {
128         LOGE(LOG_TAG, "driver_obj is NULL\n");
129         return mp_const_none;
130     }
131 
132     cap_idx = (uint32_t)mp_obj_get_int(args[1]);
133     reason_code = (uint32_t)mp_obj_get_int(args[2]);
134     LOGD(LOG_TAG, "%s: cap_idx = %d;\n", __func__, cap_idx);
135     LOGD(LOG_TAG, "%s: reason_code = %d;\n", __func__, reason_code);
136 
137     // ret = und_update_statis(cap_idx, reason_code);
138     if (ret != 0) {
139         LOGE(LOG_TAG, "und_update_statis failed\n");
140     }
141     LOGD(LOG_TAG, "%s:out\n", __func__);
142 
143     return MP_ROM_INT(ret);
144 }
145 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(und_obj_update, 3, obj_update);
146 
obj_stop(size_t n_args,const mp_obj_t * args)147 STATIC mp_obj_t obj_stop(size_t n_args, const mp_obj_t *args)
148 {
149     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
150     int ret = -1;
151     if (n_args < 1) {
152         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
153              n_args);
154         return mp_const_none;
155     }
156     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
157     mp_und_obj_t *driver_obj = (mp_und_obj_t *)self;
158     if (driver_obj == NULL) {
159         LOGE(LOG_TAG, "driver_obj is NULL\n");
160         return mp_const_none;
161     }
162 
163     // ret = und_deinit();
164     if (ret != 0) {
165         LOGE(LOG_TAG, "%s:und_deinit failed\n", __func__);
166     }
167     LOGD(LOG_TAG, "%s:out\n", __func__);
168 
169     return MP_ROM_INT(ret);
170 }
171 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(und_obj_stop, 1, obj_stop);
172 
173 STATIC const mp_rom_map_elem_t und_locals_dict_table[] = {
174     { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_UND) },
175     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&und_obj_open) },
176     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&und_obj_close) },
177     { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&und_obj_start) },
178     { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&und_obj_update) },
179     { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&und_obj_stop) },
180 };
181 
182 STATIC MP_DEFINE_CONST_DICT(und_locals_dict, und_locals_dict_table);
183 
184 const mp_obj_type_t driver_und_type = {
185     .base = { &mp_type_type },
186     .name = MP_QSTR_UND,
187     .print = und_obj_print,
188     .make_new = und_obj_make_new,
189     .locals_dict = (mp_obj_dict_t *)&und_locals_dict,
190 };
191