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 "MOD_SYSTEMINFO"
12 
13 // this is the actual C-structure for our new object
14 typedef struct {
15     // base represents some basic information, like type
16     mp_obj_base_t Base;
17     // a member created by us
18     char *ModuleName;
19 } mp_system_obj_t;
obj_open(size_t n_args,const mp_obj_t * args)20 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
21 {
22     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
23     int ret = -1;
24     void *instance = NULL;
25     if (n_args < 1) {
26         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
27              n_args);
28         return mp_const_none;
29     }
30     LOGD(LOG_TAG, "%s:out\n", __func__);
31 
32     return mp_const_none;
33 }
34 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_open, 1, obj_open);
35 
obj_close(size_t n_args,const mp_obj_t * args)36 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
37 {
38     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
39     int ret = -1;
40     void *instance = NULL;
41     if (n_args < 1) {
42         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
43              n_args);
44         return mp_const_none;
45     }
46     LOGD(LOG_TAG, "%s:out\n", __func__);
47 
48     return mp_const_none;
49 }
50 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_close, 1, obj_close);
51 
obj_versions(size_t n_args,const mp_obj_t * args)52 STATIC mp_obj_t obj_versions(size_t n_args, const mp_obj_t *args)
53 {
54     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
55     int ret = -1;
56 
57     char *version = NULL;
58     if (n_args < 1) {
59         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
60              n_args);
61         return mp_const_none;
62     }
63     // version = amp_get_system_version();
64     LOGD(LOG_TAG, "%s:out\n", __func__);
65 
66     return MP_ROM_QSTR(version);
67 }
68 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_versions, 1, obj_versions);
69 
obj_version(size_t n_args,const mp_obj_t * args)70 STATIC mp_obj_t obj_version(size_t n_args, const mp_obj_t *args)
71 {
72     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
73     int ret = -1;
74 
75     // char version[AMP_VERSION_LENGTH] = {0};
76     if (n_args < 1) {
77         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
78              n_args);
79         return mp_const_none;
80     }
81     // amp_version_get(version);
82     LOGD(LOG_TAG, "%s:out\n", __func__);
83 
84     // return MP_ROM_QSTR(version);
85     return mp_const_none;
86 }
87 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_version, 1, obj_version);
88 
obj_platform(size_t n_args,const mp_obj_t * args)89 STATIC mp_obj_t obj_platform(size_t n_args, const mp_obj_t *args)
90 {
91     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
92     int ret = -1;
93     char *type = NULL;
94 
95     if (n_args < 1) {
96         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
97              n_args);
98         return mp_const_none;
99     }
100     // type = amp_get_platform_type();
101     LOGD(LOG_TAG, "%s:out\n", __func__);
102 
103     return MP_ROM_QSTR(type);
104 }
105 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_platform, 1, obj_platform);
106 
obj_uptime(size_t n_args,const mp_obj_t * args)107 STATIC mp_obj_t obj_uptime(size_t n_args, const mp_obj_t *args)
108 {
109     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
110     int ret = -1;
111 
112     if (n_args < 1) {
113         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
114              n_args);
115         return mp_const_none;
116     }
117     // ret = amp_uptime();
118     LOGD(LOG_TAG, "%s:out\n", __func__);
119 
120     return MP_ROM_INT(ret);
121 }
122 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_uptime, 1, obj_uptime);
123 
obj_memory_total(size_t n_args,const mp_obj_t * args)124 STATIC mp_obj_t obj_memory_total(size_t n_args, const mp_obj_t *args)
125 {
126     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
127     int ret = -1;
128 
129     // amp_heap_info_t heap_info;
130     if (n_args < 1) {
131         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
132              n_args);
133         return mp_const_none;
134     }
135     // ret = amp_heap_memory_info(&heap_info);
136     if (ret != 0) {
137         LOGE(LOG_TAG, "%s:amp_heap_memory_info failed\n", __func__);
138         return mp_const_none;
139     }
140     // LOGD(LOG_TAG, "%s:heap_info.heap_total = %d;\n", __func__,
141     // heap_info.heap_total); LOGD(LOG_TAG, "%s:heap_info.heap_used = %d;\n",
142     // __func__, heap_info.heap_used);
143     LOGD(LOG_TAG, "%s:out\n", __func__);
144 
145     // return MP_ROM_INT(heap_info.heap_total);
146 }
147 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_memory_total, 1,
148                                    obj_memory_total);
149 
obj_memory_used(size_t n_args,const mp_obj_t * args)150 STATIC mp_obj_t obj_memory_used(size_t n_args, const mp_obj_t *args)
151 {
152     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
153     int ret = -1;
154 
155     // amp_heap_info_t heap_info;
156     if (n_args < 1) {
157         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
158              n_args);
159         return mp_const_none;
160     }
161     // ret = amp_heap_memory_info(&heap_info);
162     if (ret != 0) {
163         LOGE(LOG_TAG, "%s:amp_heap_memory_info failed\n", __func__);
164         return mp_const_none;
165     }
166     // LOGD(LOG_TAG, "%s:heap_info.heap_total = %d;\n", __func__,
167     // heap_info.heap_total); LOGD(LOG_TAG, "%s:heap_info.heap_used = %d;\n",
168     // __func__, heap_info.heap_used);
169     LOGD(LOG_TAG, "%s:out\n", __func__);
170 
171     // return MP_ROM_INT(heap_info.heap_used);
172     return mp_const_none;
173 }
174 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_memory_used, 1, obj_memory_used);
175 
obj_gc(size_t n_args,const mp_obj_t * args)176 STATIC mp_obj_t obj_gc(size_t n_args, const mp_obj_t *args)
177 {
178     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
179     int ret = -1;
180     void *instance = NULL;
181     if (n_args < 1) {
182         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
183              n_args);
184         return mp_const_none;
185     }
186     LOGD(LOG_TAG, "%s:out\n", __func__);
187 
188     return mp_const_none;
189 }
190 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(system_obj_gc, 1, obj_gc);
191 
192 STATIC const mp_rom_map_elem_t systeminfo_locals_dict_table[] = {
193     { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_SystemInfo) },
194     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&system_obj_open) },
195     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&system_obj_close) },
196     { MP_ROM_QSTR(MP_QSTR_versions), MP_ROM_PTR(&system_obj_versions) },
197     { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&system_obj_version) },
198     { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&system_obj_platform) },
199     { MP_ROM_QSTR(MP_QSTR_uptime), MP_ROM_PTR(&system_obj_uptime) },
200     { MP_ROM_QSTR(MP_QSTR_memory_total), MP_ROM_PTR(&system_obj_memory_total) },
201     { MP_ROM_QSTR(MP_QSTR_memory_used), MP_ROM_PTR(&system_obj_memory_used) },
202     { MP_ROM_QSTR(MP_QSTR_gc), MP_ROM_PTR(&system_obj_gc) },
203 };
204 
205 STATIC MP_DEFINE_CONST_DICT(systeminfo_locals_dict,
206                             systeminfo_locals_dict_table);
207 
208 const mp_obj_module_t system_info_module = {
209     .base = { &mp_type_module },
210     .globals = (mp_obj_dict_t *)&systeminfo_locals_dict,
211 };
212