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_UDP"
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_udp_obj_t;
20
obj_createSocket(size_t n_args,const mp_obj_t * args)21 STATIC mp_obj_t obj_createSocket(size_t n_args, const mp_obj_t *args)
22 {
23 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
24 int ret = -1;
25 void *instance = NULL;
26 if (n_args < 5) {
27 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
28 n_args);
29 return mp_const_none;
30 }
31 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
32 mp_udp_obj_t *driver_obj = (mp_udp_obj_t *)self;
33 if (driver_obj == NULL) {
34 LOGE(LOG_TAG, "driver_obj is NULL\n");
35 return mp_const_none;
36 }
37 LOGD(LOG_TAG, "%s:out\n", __func__);
38
39 return mp_const_none;
40 }
41 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(mp_obj_udp_createSocket, 5,
42 obj_createSocket);
43
obj_bind(size_t n_args,const mp_obj_t * args)44 STATIC mp_obj_t obj_bind(size_t n_args, const mp_obj_t *args)
45 {
46 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
47 int ret = -1;
48 void *instance = NULL;
49 if (n_args < 5) {
50 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
51 n_args);
52 return mp_const_none;
53 }
54 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
55 mp_udp_obj_t *driver_obj = (mp_udp_obj_t *)self;
56 if (driver_obj == NULL) {
57 LOGE(LOG_TAG, "driver_obj is NULL\n");
58 return mp_const_none;
59 }
60 LOGD(LOG_TAG, "%s:out\n", __func__);
61
62 return mp_const_none;
63 }
64 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(mp_obj_udp_bind, 5, obj_bind);
65
obj_sendto(size_t n_args,const mp_obj_t * args)66 STATIC mp_obj_t obj_sendto(size_t n_args, const mp_obj_t *args)
67 {
68 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
69 int ret = -1;
70 void *instance = NULL;
71 if (n_args < 5) {
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_udp_obj_t *driver_obj = (mp_udp_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(mp_obj_udp_sendto, 5, obj_sendto);
87
obj_recvfrom(size_t n_args,const mp_obj_t * args)88 STATIC mp_obj_t obj_recvfrom(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__,
95 n_args);
96 return mp_const_none;
97 }
98 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
99 mp_udp_obj_t *driver_obj = (mp_udp_obj_t *)self;
100 if (driver_obj == NULL) {
101 LOGE(LOG_TAG, "driver_obj is NULL\n");
102 return mp_const_none;
103 }
104 LOGD(LOG_TAG, "%s:out\n", __func__);
105
106 return mp_const_none;
107 }
108 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(mp_obj_udp_recvfrom, 5, obj_recvfrom);
109
110 STATIC const mp_rom_map_elem_t udp_module_globals_table[] = {
111 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tcp) },
112 { MP_OBJ_NEW_QSTR(MP_QSTR_createSocket),
113 MP_ROM_PTR(&mp_obj_udp_createSocket) },
114 { MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&mp_obj_udp_bind) },
115 { MP_OBJ_NEW_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&mp_obj_udp_sendto) },
116 { MP_OBJ_NEW_QSTR(MP_QSTR_recvfrom), MP_ROM_PTR(&mp_obj_udp_recvfrom) },
117 };
118
119 STATIC MP_DEFINE_CONST_DICT(udp_module_globals, udp_module_globals_table);
120
121 const mp_obj_module_t udp_module = {
122 .base = { &mp_type_module },
123 .globals = (mp_obj_dict_t *)&udp_module_globals,
124 };
125