1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <std.h>
#include "stm32f4xx_hal.h"
#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
#include "runtime.h"
#include "portmodules.h"
#include "hci.h"
#include "socket.h"
#include "netapp.h"
#define MP_ASSERT_TYPE(obj, type) \
do { \
__typeof__ (obj) _a = (obj); \
__typeof__ (type) _b = (type); \
if (!MP_OBJ_IS_TYPE(_a, _b)) { \
nlr_jump(mp_obj_new_exception_msg_varg( \
&mp_type_TypeError, \
"can't convert %s to %s", \
mp_obj_get_type_str(_a), \
_b->name)); \
} \
} while(0)
// select helper functions
STATIC void set_fds(int *nfds, mp_obj_t *fdlist, uint fdlist_len, fd_set *fdset) {
// clear fd set
FD_ZERO(fdset);
// add sockets to fd set
for (int i=0; i<fdlist_len; i++) {
socket_t *s = fdlist[i];
// check arg type
MP_ASSERT_TYPE(s, &socket_type);
// add to fd set
FD_SET(s->fd, fdset);
if (s->fd > (*nfds)) {
*nfds = s->fd;
}
}
}
STATIC void get_fds(mp_obj_t *fdlist, uint fdlist_len, mp_obj_t *fdlist_out, fd_set *fdset) {
for (int i=0; i<fdlist_len; i++) {
socket_t *s = fdlist[i];
if (FD_ISSET(s->fd, fdset)) {
socket_t *socket_obj = m_new_obj_with_finaliser(socket_t);
socket_obj->base.type = (mp_obj_t)&socket_type;
socket_obj->fd = s->fd;
mp_obj_list_append(fdlist_out, socket_obj);
}
}
}
STATIC mp_obj_t mod_select_select(uint n_args, const mp_obj_t *args) {
int nfds=0; //highest-numbered fd plus 1
timeval tv={0};
fd_set rfds, wfds, xfds;
mp_obj_t *rlist, *wlist, *xlist;
uint rlist_len, wlist_len, xlist_len;
// read args
mp_obj_get_array(args[0], &rlist_len, &rlist);
mp_obj_get_array(args[1], &wlist_len, &wlist);
mp_obj_get_array(args[2], &xlist_len, &xlist);
if (n_args == 4) {
float timeout = mp_obj_get_float(args[3]);
tv.tv_sec = (int)timeout;
tv.tv_usec = (timeout-(int)timeout)*1000*1000;
}
// add fds to their respective sets
set_fds(&nfds, rlist, rlist_len, &rfds);
set_fds(&nfds, wlist, wlist_len, &wfds);
set_fds(&nfds, xlist, xlist_len, &xfds);
// call select
nfds = select(nfds+1, &rfds, &wfds, &xfds, &tv);
// if any of the read sockets is closed, we add it to the read fd set,
// a subsequent call to recv() returns 0. This behavior is consistent with BSD.
for (int i=0; i<rlist_len; i++) {
socket_t *s = rlist[i];
if (mod_wlan_get_fd_state(s->fd)) {
FD_SET(s->fd, &rfds);
nfds = (nfds > s->fd)? nfds:s->fd;
}
}
// return value; a tuple of 3 lists
mp_obj_t fds[3] = {
mp_obj_new_list(0, NULL),
mp_obj_new_list(0, NULL),
mp_obj_new_list(0, NULL)
};
// On success, select() returns the number of file descriptors contained
// in the three returned descriptor sets which may be zero if the timeout
// expires before anything interesting happens, -1 is returned on error.
if (nfds == -1) { // select failed
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "select failed"));
} else if (nfds) { // an fd is ready
get_fds(rlist, rlist_len, fds[0], &rfds);
get_fds(wlist, wlist_len, fds[1], &wfds);
get_fds(xlist, xlist_len, fds[2], &xfds);
} // select timedout
return mp_obj_new_tuple(3, fds);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_select_select_obj, 3, 4, mod_select_select);
STATIC const mp_map_elem_t select_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_select) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mod_select_select_obj },
};
STATIC const mp_obj_dict_t select_module_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = MP_ARRAY_SIZE(select_module_globals_table),
.alloc = MP_ARRAY_SIZE(select_module_globals_table),
.table = (mp_map_elem_t*)select_module_globals_table,
},
};
const mp_obj_module_t select_module = {
.base = { &mp_type_module },
.name = MP_QSTR_select,
.globals = (mp_obj_dict_t*)&select_module_globals,
};
|