Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | #include <stdio.h> |
| 29 | #include <errno.h> |
| 30 | #include <poll.h> |
| 31 | |
| 32 | #include "py/nlr.h" |
| 33 | #include "py/obj.h" |
| 34 | #include "py/objlist.h" |
| 35 | #include "py/objtuple.h" |
| 36 | #include "py/mphal.h" |
| 37 | |
| 38 | /// \class Poll - poll class |
| 39 | |
| 40 | typedef struct _mp_obj_poll_t { |
| 41 | mp_obj_base_t base; |
| 42 | unsigned short alloc; |
| 43 | unsigned short len; |
| 44 | struct pollfd *entries; |
| 45 | } mp_obj_poll_t; |
| 46 | |
| 47 | /// \method register(obj[, eventmask]) |
| 48 | STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) { |
| 49 | mp_obj_poll_t *self = args[0]; |
| 50 | mp_uint_t flags; |
| 51 | if (n_args == 3) { |
| 52 | flags = mp_obj_get_int(args[2]); |
| 53 | } else { |
| 54 | flags = POLLIN | POLLOUT; |
| 55 | } |
| 56 | |
Damien George | 54df549 | 2015-11-20 12:51:00 +0000 | [diff] [blame] | 57 | int i = 0; |
Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 58 | if (self->len < self->alloc) { |
| 59 | i = self->len++; |
| 60 | } else { |
Paul Sokolovsky | 698a6a9 | 2015-11-29 00:05:56 +0200 | [diff] [blame] | 61 | struct pollfd *entries = self->entries; |
| 62 | for (i = 0; i < self->len; i++, entries++) { |
| 63 | if (entries->fd == -1) { |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | if (entries->fd != -1) { |
| 68 | assert(0); |
| 69 | } |
Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | self->entries[i].fd = mp_obj_get_int(args[1]); |
| 73 | self->entries[i].events = flags; |
| 74 | self->entries[i].revents = 0; |
| 75 | |
| 76 | return mp_const_none; |
| 77 | } |
| 78 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register); |
| 79 | |
| 80 | /// \method unregister(obj) |
| 81 | STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { |
| 82 | mp_obj_poll_t *self = self_in; |
| 83 | struct pollfd *entries = self->entries; |
| 84 | int fd = mp_obj_get_int(obj_in); |
| 85 | for (int i = self->len - 1; i >= 0; i--) { |
| 86 | if (entries->fd == fd) { |
| 87 | entries->fd = -1; |
| 88 | break; |
| 89 | } |
| 90 | entries++; |
| 91 | } |
| 92 | |
| 93 | // TODO raise KeyError if obj didn't exist in map |
| 94 | return mp_const_none; |
| 95 | } |
| 96 | MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister); |
| 97 | |
| 98 | /// \method modify(obj, eventmask) |
| 99 | STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) { |
| 100 | mp_obj_poll_t *self = self_in; |
| 101 | struct pollfd *entries = self->entries; |
| 102 | int fd = mp_obj_get_int(obj_in); |
| 103 | for (int i = self->len - 1; i >= 0; i--) { |
| 104 | if (entries->fd == fd) { |
| 105 | entries->events = mp_obj_get_int(eventmask_in); |
| 106 | break; |
| 107 | } |
| 108 | entries++; |
| 109 | } |
| 110 | |
| 111 | // TODO raise KeyError if obj didn't exist in map |
| 112 | return mp_const_none; |
| 113 | } |
| 114 | MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify); |
| 115 | |
| 116 | /// \method poll([timeout]) |
| 117 | /// Timeout is in milliseconds. |
| 118 | STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { |
| 119 | mp_obj_poll_t *self = args[0]; |
| 120 | |
| 121 | // work out timeout (its given already in ms) |
| 122 | int timeout = -1; |
| 123 | if (n_args == 2) { |
| 124 | if (args[1] != mp_const_none) { |
| 125 | mp_int_t timeout_i = mp_obj_get_int(args[1]); |
| 126 | if (timeout_i >= 0) { |
| 127 | timeout = timeout_i; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | int n_ready = poll(self->entries, self->len, timeout); |
| 133 | RAISE_ERRNO(n_ready, errno); |
| 134 | if (n_ready == 0) { |
| 135 | return mp_const_empty_tuple; |
| 136 | } |
| 137 | |
| 138 | mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL); |
| 139 | int ret_i = 0; |
| 140 | struct pollfd *entries = self->entries; |
Paul Sokolovsky | 19920e2 | 2015-11-28 17:34:20 +0200 | [diff] [blame] | 141 | for (int i = 0; i < self->len; i++, entries++) { |
Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 142 | if (entries->revents != 0) { |
| 143 | mp_obj_tuple_t *t = mp_obj_new_tuple(2, NULL); |
| 144 | t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd); |
| 145 | t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents); |
| 146 | ret_list->items[ret_i++] = t; |
Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | return ret_list; |
| 151 | } |
| 152 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 2, poll_poll); |
| 153 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame^] | 154 | STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = { |
| 155 | { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) }, |
| 156 | { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) }, |
| 157 | { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) }, |
| 158 | { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) }, |
Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 159 | }; |
| 160 | STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); |
| 161 | |
| 162 | STATIC const mp_obj_type_t mp_type_poll = { |
| 163 | { &mp_type_type }, |
| 164 | .name = MP_QSTR_poll, |
| 165 | .locals_dict = (mp_obj_t)&poll_locals_dict, |
| 166 | }; |
| 167 | |
| 168 | STATIC mp_obj_t select_poll(mp_uint_t n_args, const mp_obj_t *args) { |
| 169 | int alloc = 4; |
| 170 | if (n_args > 0) { |
| 171 | alloc = mp_obj_get_int(args[0]); |
| 172 | } |
| 173 | mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t); |
| 174 | poll->base.type = &mp_type_poll; |
| 175 | poll->entries = m_new(struct pollfd, alloc); |
| 176 | poll->alloc = alloc; |
| 177 | poll->len = 0; |
| 178 | return poll; |
| 179 | } |
| 180 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_poll_obj, 0, 1, select_poll); |
| 181 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame^] | 182 | STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = { |
| 183 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) }, |
| 184 | { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) }, |
| 185 | { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_ROM_INT(POLLIN) }, |
| 186 | { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_ROM_INT(POLLOUT) }, |
| 187 | { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_ROM_INT(POLLERR) }, |
| 188 | { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_ROM_INT(POLLHUP) }, |
Paul Sokolovsky | 65971f5 | 2015-11-17 00:35:29 +0200 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table); |
| 192 | |
| 193 | const mp_obj_module_t mp_module_uselect = { |
| 194 | .base = { &mp_type_module }, |
| 195 | .name = MP_QSTR_uselect, |
| 196 | .globals = (mp_obj_dict_t*)&mp_module_select_globals, |
| 197 | }; |