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_CAN"
12 
13 extern const mp_obj_type_t driver_can_type;
14 
15 #define CAN_JSON_CONFIG_PATH "/data/python/config/can.json"
16 
17 // this is the actual C-structure for our new object
18 typedef struct {
19     // base represents some basic information, like type
20     mp_obj_base_t Base;
21     // a member created by us
22     char *ModuleName;
23 } mp_can_obj_t;
24 
can_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)25 void can_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
26 {
27     LOGD(LOG_TAG, "entern %s;\n", __func__);
28     mp_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
29     mp_printf(print, "ModuleName(%s)", self->ModuleName);
30 }
31 
can_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)32 STATIC mp_obj_t can_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
33 {
34     LOGD(LOG_TAG, "entern  %s;\n", __func__);
35     mp_can_obj_t *driver_obj = m_new_obj(mp_can_obj_t);
36     if (!driver_obj) {
37         mp_raise_OSError(MP_EINVAL);
38     }
39 
40     driver_obj->Base.type = &driver_can_type;
41     driver_obj->ModuleName = "can";
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     void *instance = NULL;
51     if (n_args < 5) {
52         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, 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_can_obj_t *driver_obj = (mp_can_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(can_obj_open, 5, 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     void *instance = NULL;
72     if (n_args < 5) {
73         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, 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_can_obj_t *driver_obj = (mp_can_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(can_obj_close, 5, obj_close);
87 
obj_send(size_t n_args,const mp_obj_t * args)88 STATIC mp_obj_t obj_send(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     void *instance = NULL;
93     if (n_args < 5) {
94         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, 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_can_obj_t *driver_obj = (mp_can_obj_t *)self;
99     if (driver_obj == NULL) {
100         LOGE(LOG_TAG, "driver_obj is NULL\n");
101         return mp_const_none;
102     }
103     LOGD(LOG_TAG, "%s:out\n", __func__);
104 
105     return mp_const_none;
106 }
107 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(can_obj_send, 5, obj_send);
108 
obj_receive(size_t n_args,const mp_obj_t * args)109 STATIC mp_obj_t obj_receive(size_t n_args, const mp_obj_t *args)
110 {
111     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
112     int ret = -1;
113     void *instance = NULL;
114     if (n_args < 5) {
115         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
116         return mp_const_none;
117     }
118     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
119     mp_can_obj_t *driver_obj = (mp_can_obj_t *)self;
120     if (driver_obj == NULL) {
121         LOGE(LOG_TAG, "driver_obj is NULL\n");
122         return mp_const_none;
123     }
124     LOGD(LOG_TAG, "%s:out\n", __func__);
125 
126     return mp_const_none;
127 }
128 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(can_obj_receive, 5, obj_receive);
129 
130 STATIC const mp_rom_map_elem_t can_locals_dict_table[] = {
131     { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_CAN) },
132     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&can_obj_open) },
133     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&can_obj_close) },
134     { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&can_obj_send) },
135     { MP_ROM_QSTR(MP_QSTR_receive), MP_ROM_PTR(&can_obj_receive) },
136 };
137 
138 STATIC MP_DEFINE_CONST_DICT(can_locals_dict, can_locals_dict_table);
139 
140 const mp_obj_type_t driver_can_type = {
141     .base = { &mp_type_type },
142     .name = MP_QSTR_CAN,
143     .print = can_obj_print,
144     .make_new = can_obj_make_new,
145     .locals_dict = (mp_obj_dict_t *)&can_locals_dict,
146 };
147