|
| 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 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <stdio.h> |
| 29 | + |
| 30 | +#include "stm32f4xx_hal.h" |
| 31 | + |
| 32 | +#include "mpconfig.h" |
| 33 | +#include "misc.h" |
| 34 | +#include "nlr.h" |
| 35 | +#include "qstr.h" |
| 36 | +#include "obj.h" |
| 37 | +#include "objlist.h" |
| 38 | + |
| 39 | +/// \moduleref select |
| 40 | + |
| 41 | +#define MP_IOCTL_POLL (0x100 | 1) |
| 42 | + |
| 43 | +#define MP_IOCTL_POLL_RD (0x0001) |
| 44 | +#define MP_IOCTL_POLL_WR (0x0002) |
| 45 | +#define MP_IOCTL_POLL_HUP (0x0004) |
| 46 | +#define MP_IOCTL_POLL_ERR (0x0008) |
| 47 | + |
| 48 | +typedef struct _poll_obj_t { |
| 49 | + mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, int *errcode, ...); |
| 50 | + mp_uint_t flags; |
| 51 | + mp_uint_t flags_ret; |
| 52 | +} poll_obj_t; |
| 53 | + |
| 54 | +STATIC void poll_map_add(mp_map_t *poll_map, const mp_obj_t *obj, mp_uint_t obj_len, mp_uint_t flags, bool or_flags) { |
| 55 | + for (mp_uint_t i = 0; i < obj_len; i++) { |
| 56 | + mp_map_elem_t *elem = mp_map_lookup(poll_map, obj[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); |
| 57 | + if (elem->value == NULL) { |
| 58 | + // object not found; get its ioctl and add it to the poll list |
| 59 | + mp_obj_type_t *type = mp_obj_get_type(obj[i]); |
| 60 | + if (type->stream_p == NULL || type->stream_p->ioctl == NULL) { |
| 61 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with stream.ioctl required")); |
| 62 | + } |
| 63 | + poll_obj_t *poll_obj = m_new_obj(poll_obj_t); |
| 64 | + poll_obj->ioctl = type->stream_p->ioctl; |
| 65 | + poll_obj->flags = flags; |
| 66 | + poll_obj->flags_ret = 0; |
| 67 | + elem->value = poll_obj; |
| 68 | + } else { |
| 69 | + // object exists; update its flags |
| 70 | + if (or_flags) { |
| 71 | + ((poll_obj_t*)elem->value)->flags |= flags; |
| 72 | + } else { |
| 73 | + ((poll_obj_t*)elem->value)->flags = flags; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// poll each object in the map |
| 80 | +STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) { |
| 81 | + mp_uint_t n_ready = 0; |
| 82 | + for (mp_uint_t i = 0; i < poll_map->alloc; ++i) { |
| 83 | + if (!MP_MAP_SLOT_IS_FILLED(poll_map, i)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + poll_obj_t *poll_obj = (poll_obj_t*)poll_map->table[i].value; |
| 88 | + int errno; |
| 89 | + mp_int_t ret = poll_obj->ioctl(poll_map->table[i].key, MP_IOCTL_POLL, &errno, poll_obj->flags); |
| 90 | + poll_obj->flags_ret = ret; |
| 91 | + |
| 92 | + if (ret == -1) { |
| 93 | + // error doing ioctl |
| 94 | + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno))); |
| 95 | + } |
| 96 | + |
| 97 | + if (ret != 0) { |
| 98 | + // object is ready |
| 99 | + n_ready += 1; |
| 100 | + if (rwx_num != NULL) { |
| 101 | + if (ret & MP_IOCTL_POLL_RD) { |
| 102 | + rwx_num[0] += 1; |
| 103 | + } |
| 104 | + if (ret & MP_IOCTL_POLL_WR) { |
| 105 | + rwx_num[1] += 1; |
| 106 | + } |
| 107 | + if ((ret & ~(MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR)) != 0) { |
| 108 | + rwx_num[2] += 1; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return n_ready; |
| 114 | +} |
| 115 | + |
| 116 | +/// \function select(rlist, wlist, xlist[, timeout]) |
| 117 | +STATIC mp_obj_t select_select(uint n_args, const mp_obj_t *args) { |
| 118 | + // get array data from tuple/list arguments |
| 119 | + mp_uint_t rwx_len[3]; |
| 120 | + mp_obj_t *r_array, *w_array, *x_array; |
| 121 | + mp_obj_get_array(args[0], &rwx_len[0], &r_array); |
| 122 | + mp_obj_get_array(args[1], &rwx_len[1], &w_array); |
| 123 | + mp_obj_get_array(args[2], &rwx_len[2], &x_array); |
| 124 | + |
| 125 | + // get timeout |
| 126 | + mp_uint_t timeout = -1; |
| 127 | + if (n_args == 4) { |
| 128 | + if (args[3] != mp_const_none) { |
| 129 | + float timeout_f = mp_obj_get_float(args[3]); |
| 130 | + if (timeout_f >= 0) { |
| 131 | + timeout = (mp_uint_t)(timeout_f * 1000); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // merge separate lists and get the ioctl function for each object |
| 137 | + mp_map_t poll_map; |
| 138 | + mp_map_init(&poll_map, rwx_len[0] + rwx_len[1] + rwx_len[2]); |
| 139 | + poll_map_add(&poll_map, r_array, rwx_len[0], MP_IOCTL_POLL_RD, true); |
| 140 | + poll_map_add(&poll_map, w_array, rwx_len[1], MP_IOCTL_POLL_WR, true); |
| 141 | + poll_map_add(&poll_map, x_array, rwx_len[2], MP_IOCTL_POLL_ERR | MP_IOCTL_POLL_HUP, true); |
| 142 | + |
| 143 | + mp_uint_t start_tick = HAL_GetTick(); |
| 144 | + rwx_len[0] = rwx_len[1] = rwx_len[2] = 0; |
| 145 | + for (;;) { |
| 146 | + // poll the objects |
| 147 | + mp_uint_t n_ready = poll_map_poll(&poll_map, rwx_len); |
| 148 | + |
| 149 | + if (n_ready > 0 || (timeout != -1 && HAL_GetTick() - start_tick >= timeout)) { |
| 150 | + // one or more objects are ready, or we had a timeout |
| 151 | + mp_obj_t list_array[3]; |
| 152 | + list_array[0] = mp_obj_new_list(rwx_len[0], NULL); |
| 153 | + list_array[1] = mp_obj_new_list(rwx_len[1], NULL); |
| 154 | + list_array[2] = mp_obj_new_list(rwx_len[2], NULL); |
| 155 | + rwx_len[0] = rwx_len[1] = rwx_len[2] = 0; |
| 156 | + for (mp_uint_t i = 0; i < poll_map.alloc; ++i) { |
| 157 | + if (!MP_MAP_SLOT_IS_FILLED(&poll_map, i)) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + poll_obj_t *poll_obj = (poll_obj_t*)poll_map.table[i].value; |
| 161 | + if (poll_obj->flags_ret & MP_IOCTL_POLL_RD) { |
| 162 | + ((mp_obj_list_t*)list_array[0])->items[rwx_len[0]++] = poll_map.table[i].key; |
| 163 | + } |
| 164 | + if (poll_obj->flags_ret & MP_IOCTL_POLL_WR) { |
| 165 | + ((mp_obj_list_t*)list_array[1])->items[rwx_len[1]++] = poll_map.table[i].key; |
| 166 | + } |
| 167 | + if ((poll_obj->flags_ret & ~(MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR)) != 0) { |
| 168 | + ((mp_obj_list_t*)list_array[2])->items[rwx_len[2]++] = poll_map.table[i].key; |
| 169 | + } |
| 170 | + } |
| 171 | + mp_map_deinit(&poll_map); |
| 172 | + return mp_obj_new_tuple(3, list_array); |
| 173 | + } |
| 174 | + __WFI(); |
| 175 | + } |
| 176 | +} |
| 177 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select); |
| 178 | + |
| 179 | +/// \class Poll |
| 180 | + |
| 181 | +typedef struct _mp_obj_poll_t { |
| 182 | + mp_obj_base_t base; |
| 183 | + mp_map_t poll_map; |
| 184 | +} mp_obj_poll_t; |
| 185 | + |
| 186 | +/// \method register(obj[, eventmask]) |
| 187 | +STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) { |
| 188 | + mp_obj_poll_t *self = args[0]; |
| 189 | + mp_uint_t flags; |
| 190 | + if (n_args == 3) { |
| 191 | + flags = mp_obj_get_int(args[2]); |
| 192 | + } else { |
| 193 | + flags = MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR; |
| 194 | + } |
| 195 | + poll_map_add(&self->poll_map, &args[1], 1, flags, false); |
| 196 | + return mp_const_none; |
| 197 | +} |
| 198 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register); |
| 199 | + |
| 200 | +/// \method unregister(obj) |
| 201 | +STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { |
| 202 | + mp_obj_poll_t *self = self_in; |
| 203 | + mp_map_lookup(&self->poll_map, obj_in, MP_MAP_LOOKUP_REMOVE_IF_FOUND); |
| 204 | + // TODO raise KeyError if obj didn't exist in map |
| 205 | + return mp_const_none; |
| 206 | +} |
| 207 | +MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister); |
| 208 | + |
| 209 | +/// \method modify(obj, eventmask) |
| 210 | +STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) { |
| 211 | + mp_obj_poll_t *self = self_in; |
| 212 | + mp_map_elem_t *elem = mp_map_lookup(&self->poll_map, obj_in, MP_MAP_LOOKUP); |
| 213 | + if (elem == NULL) { |
| 214 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_IOError, "object was never registered")); |
| 215 | + } |
| 216 | + ((poll_obj_t*)elem->value)->flags = mp_obj_get_int(eventmask_in); |
| 217 | + return mp_const_none; |
| 218 | +} |
| 219 | +MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify); |
| 220 | + |
| 221 | +/// \method poll([timeout]) |
| 222 | +/// Timeout is in milliseconds. |
| 223 | +STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { |
| 224 | + mp_obj_poll_t *self = args[0]; |
| 225 | + |
| 226 | + // work out timeout (its given already in ms) |
| 227 | + mp_uint_t timeout = -1; |
| 228 | + if (n_args == 2) { |
| 229 | + if (args[1] != mp_const_none) { |
| 230 | + mp_int_t timeout_i = mp_obj_get_int(args[1]); |
| 231 | + if (timeout_i >= 0) { |
| 232 | + timeout = timeout_i; |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + mp_uint_t start_tick = HAL_GetTick(); |
| 238 | + for (;;) { |
| 239 | + // poll the objects |
| 240 | + mp_uint_t n_ready = poll_map_poll(&self->poll_map, NULL); |
| 241 | + |
| 242 | + if (n_ready > 0 || (timeout != -1 && HAL_GetTick() - start_tick >= timeout)) { |
| 243 | + // one or more objects are ready, or we had a timeout |
| 244 | + mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL); |
| 245 | + mp_uint_t n_ready = 0; |
| 246 | + for (mp_uint_t i = 0; i < self->poll_map.alloc; ++i) { |
| 247 | + if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) { |
| 248 | + continue; |
| 249 | + } |
| 250 | + poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value; |
| 251 | + if (poll_obj->flags_ret != 0) { |
| 252 | + mp_obj_t tuple[2] = {self->poll_map.table[i].key, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)}; |
| 253 | + ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple); |
| 254 | + } |
| 255 | + } |
| 256 | + mp_map_deinit(&self->poll_map); |
| 257 | + return ret_list; |
| 258 | + } |
| 259 | + __WFI(); |
| 260 | + } |
| 261 | +} |
| 262 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 2, poll_poll); |
| 263 | + |
| 264 | +STATIC const mp_map_elem_t poll_locals_dict_table[] = { |
| 265 | + { MP_OBJ_NEW_QSTR(MP_QSTR_register), (mp_obj_t)&poll_register_obj }, |
| 266 | + { MP_OBJ_NEW_QSTR(MP_QSTR_unregister), (mp_obj_t)&poll_unregister_obj }, |
| 267 | + { MP_OBJ_NEW_QSTR(MP_QSTR_modify), (mp_obj_t)&poll_modify_obj }, |
| 268 | + { MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&poll_poll_obj }, |
| 269 | +}; |
| 270 | +STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); |
| 271 | + |
| 272 | +STATIC const mp_obj_type_t mp_type_poll = { |
| 273 | + { &mp_type_type }, |
| 274 | + .name = MP_QSTR_poll, |
| 275 | + .locals_dict = (mp_obj_t)&poll_locals_dict, |
| 276 | +}; |
| 277 | + |
| 278 | +/// \function poll() |
| 279 | +STATIC mp_obj_t select_poll(void) { |
| 280 | + mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t); |
| 281 | + poll->base.type = &mp_type_poll; |
| 282 | + mp_map_init(&poll->poll_map, 0); |
| 283 | + return poll; |
| 284 | +} |
| 285 | +MP_DEFINE_CONST_FUN_OBJ_0(mp_select_poll_obj, select_poll); |
0 commit comments