blob: 7e0ec049f1ff629b0686dc148179515d7246dccc [file] [log] [blame]
Paul Sokolovsky65971f52015-11-17 00:35:29 +02001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2014 Damien P. George
7 * Copyright (c) 2015 Paul Sokolovsky
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Paul Sokolovsky2d11b172015-12-13 08:47:00 +020028#include "py/mpconfig.h"
29
30#if MICROPY_PY_USELECT
31
Paul Sokolovsky65971f52015-11-17 00:35:29 +020032#include <stdio.h>
33#include <errno.h>
34#include <poll.h>
35
36#include "py/nlr.h"
37#include "py/obj.h"
38#include "py/objlist.h"
39#include "py/objtuple.h"
40#include "py/mphal.h"
41
Paul Sokolovskyc1481bb2015-12-11 23:36:29 +020042// Flags for poll()
43#define FLAG_ONESHOT (1)
44
Paul Sokolovsky65971f52015-11-17 00:35:29 +020045/// \class Poll - poll class
46
47typedef struct _mp_obj_poll_t {
48 mp_obj_base_t base;
49 unsigned short alloc;
50 unsigned short len;
51 struct pollfd *entries;
52} mp_obj_poll_t;
53
54/// \method register(obj[, eventmask])
Damien George1b0aab62016-01-03 11:53:44 +000055STATIC mp_obj_t poll_register(mp_uint_t n_args, const mp_obj_t *args) {
Damien George999cedb2015-11-27 17:01:44 +000056 mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovsky082b1212015-12-05 14:53:53 +020057 int fd = mp_obj_get_int(args[1]);
Paul Sokolovsky65971f52015-11-17 00:35:29 +020058 mp_uint_t flags;
59 if (n_args == 3) {
60 flags = mp_obj_get_int(args[2]);
61 } else {
62 flags = POLLIN | POLLOUT;
63 }
64
Paul Sokolovsky082b1212015-12-05 14:53:53 +020065 struct pollfd *free_slot = NULL;
66
67 struct pollfd *entry = self->entries;
68 for (int i = 0; i < self->len; i++, entry++) {
69 int entry_fd = entry->fd;
70 if (entry_fd == fd) {
71 entry->events = flags;
72 return mp_const_false;
Paul Sokolovsky698a6a92015-11-29 00:05:56 +020073 }
Paul Sokolovsky082b1212015-12-05 14:53:53 +020074 if (entry_fd == -1) {
75 free_slot = entry;
Paul Sokolovsky698a6a92015-11-29 00:05:56 +020076 }
Paul Sokolovsky65971f52015-11-17 00:35:29 +020077 }
78
Paul Sokolovsky082b1212015-12-05 14:53:53 +020079 if (free_slot == NULL) {
80 if (self->len >= self->alloc) {
81 self->entries = m_renew(struct pollfd, self->entries, self->alloc, self->alloc + 4);
82 self->alloc += 4;
83 }
84 free_slot = &self->entries[self->len++];
85 }
Paul Sokolovsky65971f52015-11-17 00:35:29 +020086
Paul Sokolovsky082b1212015-12-05 14:53:53 +020087 free_slot->fd = fd;
88 free_slot->events = flags;
89 free_slot->revents = 0;
90 return mp_const_true;
Paul Sokolovsky65971f52015-11-17 00:35:29 +020091}
92MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
93
94/// \method unregister(obj)
95STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
Damien George999cedb2015-11-27 17:01:44 +000096 mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky65971f52015-11-17 00:35:29 +020097 struct pollfd *entries = self->entries;
98 int fd = mp_obj_get_int(obj_in);
99 for (int i = self->len - 1; i >= 0; i--) {
100 if (entries->fd == fd) {
101 entries->fd = -1;
102 break;
103 }
104 entries++;
105 }
106
107 // TODO raise KeyError if obj didn't exist in map
108 return mp_const_none;
109}
110MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
111
112/// \method modify(obj, eventmask)
113STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
Damien George999cedb2015-11-27 17:01:44 +0000114 mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200115 struct pollfd *entries = self->entries;
116 int fd = mp_obj_get_int(obj_in);
117 for (int i = self->len - 1; i >= 0; i--) {
118 if (entries->fd == fd) {
119 entries->events = mp_obj_get_int(eventmask_in);
120 break;
121 }
122 entries++;
123 }
124
125 // TODO raise KeyError if obj didn't exist in map
126 return mp_const_none;
127}
128MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
129
130/// \method poll([timeout])
131/// Timeout is in milliseconds.
Damien George1b0aab62016-01-03 11:53:44 +0000132STATIC mp_obj_t poll_poll(mp_uint_t n_args, const mp_obj_t *args) {
Damien George999cedb2015-11-27 17:01:44 +0000133 mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200134
Paul Sokolovskyc1481bb2015-12-11 23:36:29 +0200135 // work out timeout (it's given already in ms)
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200136 int timeout = -1;
Paul Sokolovskyc1481bb2015-12-11 23:36:29 +0200137 int flags = 0;
138 if (n_args >= 2) {
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200139 if (args[1] != mp_const_none) {
140 mp_int_t timeout_i = mp_obj_get_int(args[1]);
141 if (timeout_i >= 0) {
142 timeout = timeout_i;
143 }
144 }
Paul Sokolovskyc1481bb2015-12-11 23:36:29 +0200145 if (n_args >= 3) {
146 flags = mp_obj_get_int(args[2]);
147 }
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200148 }
149
150 int n_ready = poll(self->entries, self->len, timeout);
151 RAISE_ERRNO(n_ready, errno);
152 if (n_ready == 0) {
153 return mp_const_empty_tuple;
154 }
155
Damien George999cedb2015-11-27 17:01:44 +0000156 mp_obj_list_t *ret_list = MP_OBJ_TO_PTR(mp_obj_new_list(n_ready, NULL));
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200157 int ret_i = 0;
158 struct pollfd *entries = self->entries;
Paul Sokolovsky19920e22015-11-28 17:34:20 +0200159 for (int i = 0; i < self->len; i++, entries++) {
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200160 if (entries->revents != 0) {
Damien George999cedb2015-11-27 17:01:44 +0000161 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200162 t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
163 t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
Damien George999cedb2015-11-27 17:01:44 +0000164 ret_list->items[ret_i++] = MP_OBJ_FROM_PTR(t);
Paul Sokolovskyc1481bb2015-12-11 23:36:29 +0200165 if (flags & FLAG_ONESHOT) {
166 entries->events = 0;
167 }
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200168 }
169 }
170
Damien George999cedb2015-11-27 17:01:44 +0000171 return MP_OBJ_FROM_PTR(ret_list);
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200172}
Paul Sokolovskyc1481bb2015-12-11 23:36:29 +0200173MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll);
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200174
Damien Georgecbf76742015-11-27 13:38:15 +0000175STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = {
176 { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) },
177 { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) },
178 { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) },
179 { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) },
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200180};
181STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
182
183STATIC const mp_obj_type_t mp_type_poll = {
184 { &mp_type_type },
185 .name = MP_QSTR_poll,
Damien George999cedb2015-11-27 17:01:44 +0000186 .locals_dict = (void*)&poll_locals_dict,
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200187};
188
189STATIC mp_obj_t select_poll(mp_uint_t n_args, const mp_obj_t *args) {
190 int alloc = 4;
191 if (n_args > 0) {
192 alloc = mp_obj_get_int(args[0]);
193 }
194 mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
195 poll->base.type = &mp_type_poll;
196 poll->entries = m_new(struct pollfd, alloc);
197 poll->alloc = alloc;
198 poll->len = 0;
Damien George999cedb2015-11-27 17:01:44 +0000199 return MP_OBJ_FROM_PTR(poll);
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200200}
201MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_poll_obj, 0, 1, select_poll);
202
Damien Georgecbf76742015-11-27 13:38:15 +0000203STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = {
204 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) },
205 { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) },
206 { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_ROM_INT(POLLIN) },
207 { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_ROM_INT(POLLOUT) },
208 { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_ROM_INT(POLLERR) },
209 { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_ROM_INT(POLLHUP) },
Paul Sokolovsky65971f52015-11-17 00:35:29 +0200210};
211
212STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table);
213
214const mp_obj_module_t mp_module_uselect = {
215 .base = { &mp_type_module },
216 .name = MP_QSTR_uselect,
217 .globals = (mp_obj_dict_t*)&mp_module_select_globals,
218};
Paul Sokolovsky2d11b172015-12-13 08:47:00 +0200219
220#endif // MICROPY_PY_USELECT