Commit 6c9c7bc7 authored by Damien George's avatar Damien George
Browse files

stmhal: Implement generic select.select and select.poll.

parent c8c44a4c
...@@ -42,15 +42,16 @@ ...@@ -42,15 +42,16 @@
/// ///
/// This module provides the select function. /// This module provides the select function.
/// \function select(rlist, wlist, xlist[, timeout]) // This is just a skeleton. Individual functions must be implemented by a port.
mp_obj_t select_select(uint n_args, const mp_obj_t *args) { // For the following, specific types don't matter, only addresses.
return mp_obj_new_bytes((void*)mp_obj_int_get(ptr), mp_obj_int_get(size)); struct _dummy_t;
} extern struct _dummy_t mp_select_select_obj;
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(select_select_obj, 3, 4, select_select); extern struct _dummy_t mp_select_poll_obj;
STATIC const mp_map_elem_t mp_module_select_globals_table[] = { STATIC const mp_map_elem_t mp_module_select_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_select) }, { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_select) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&select_select_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_select_select_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&mp_select_poll_obj },
}; };
STATIC const mp_obj_dict_t mp_module_select_globals = { STATIC const mp_obj_dict_t mp_module_select_globals = {
......
...@@ -89,3 +89,4 @@ extern struct _dummy_t mp_sys_stderr_obj; ...@@ -89,3 +89,4 @@ extern struct _dummy_t mp_sys_stderr_obj;
// extmod modules // extmod modules
extern const mp_obj_module_t mp_module_uctypes; extern const mp_obj_module_t mp_module_uctypes;
extern const mp_obj_module_t mp_module_zlibd; extern const mp_obj_module_t mp_module_zlibd;
extern const mp_obj_module_t mp_module_select;
...@@ -200,6 +200,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = { ...@@ -200,6 +200,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
#if MICROPY_PY_ZLIBD #if MICROPY_PY_ZLIBD
{ MP_OBJ_NEW_QSTR(MP_QSTR_zlibd), (mp_obj_t)&mp_module_zlibd }, { MP_OBJ_NEW_QSTR(MP_QSTR_zlibd), (mp_obj_t)&mp_module_zlibd },
#endif #endif
#if MICROPY_PY_SELECT
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_module_select },
#endif
// extra builtin modules as defined by a port // extra builtin modules as defined by a port
MICROPY_PORT_BUILTIN_MODULES MICROPY_PORT_BUILTIN_MODULES
......
...@@ -237,7 +237,7 @@ typedef struct _mp_stream_p_t { ...@@ -237,7 +237,7 @@ typedef struct _mp_stream_p_t {
// are implementation-dependent, but will be exposed to user, e.g. via exception). // are implementation-dependent, but will be exposed to user, e.g. via exception).
mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode); mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode); mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
mp_uint_t (*ioctl(mp_obj_t obj, mp_uint_t request, int *errcode, ...); mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, int *errcode, ...);
mp_uint_t is_text : 1; // default is bytes, set this for text stream mp_uint_t is_text : 1; // default is bytes, set this for text stream
} mp_stream_p_t; } mp_stream_p_t;
......
...@@ -112,6 +112,7 @@ PY_O_BASENAME = \ ...@@ -112,6 +112,7 @@ PY_O_BASENAME = \
pfenv_printf.o \ pfenv_printf.o \
../extmod/moductypes.o \ ../extmod/moductypes.o \
../extmod/modzlibd.o \ ../extmod/modzlibd.o \
../extmod/modselect.o \
# prepend the build destination prefix to the py object files # prepend the build destination prefix to the py object files
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME)) PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))
......
...@@ -463,3 +463,11 @@ Q(deleter) ...@@ -463,3 +463,11 @@ Q(deleter)
Q(zlibd) Q(zlibd)
Q(decompress) Q(decompress)
#endif #endif
#if MICROPY_PY_SELECT
Q(select)
Q(poll)
Q(register)
Q(unregister)
Q(modify)
#endif
...@@ -102,6 +102,7 @@ SRC_C = \ ...@@ -102,6 +102,7 @@ SRC_C = \
modpyb.c \ modpyb.c \
modstm.c \ modstm.c \
modtime.c \ modtime.c \
modselect.c \
import.c \ import.c \
lexerfatfs.c \ lexerfatfs.c \
extint.c \ extint.c \
......
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include "stm32f4xx_hal.h"
#include "mpconfig.h"
#include "misc.h"
#include "nlr.h"
#include "qstr.h"
#include "obj.h"
#include "objlist.h"
/// \moduleref select
#define MP_IOCTL_POLL (0x100 | 1)
#define MP_IOCTL_POLL_RD (0x0001)
#define MP_IOCTL_POLL_WR (0x0002)
#define MP_IOCTL_POLL_HUP (0x0004)
#define MP_IOCTL_POLL_ERR (0x0008)
typedef struct _poll_obj_t {
mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, int *errcode, ...);
mp_uint_t flags;
mp_uint_t flags_ret;
} poll_obj_t;
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) {
for (mp_uint_t i = 0; i < obj_len; i++) {
mp_map_elem_t *elem = mp_map_lookup(poll_map, obj[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
if (elem->value == NULL) {
// object not found; get its ioctl and add it to the poll list
mp_obj_type_t *type = mp_obj_get_type(obj[i]);
if (type->stream_p == NULL || type->stream_p->ioctl == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with stream.ioctl required"));
}
poll_obj_t *poll_obj = m_new_obj(poll_obj_t);
poll_obj->ioctl = type->stream_p->ioctl;
poll_obj->flags = flags;
poll_obj->flags_ret = 0;
elem->value = poll_obj;
} else {
// object exists; update its flags
if (or_flags) {
((poll_obj_t*)elem->value)->flags |= flags;
} else {
((poll_obj_t*)elem->value)->flags = flags;
}
}
}
}
// poll each object in the map
STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) {
mp_uint_t n_ready = 0;
for (mp_uint_t i = 0; i < poll_map->alloc; ++i) {
if (!MP_MAP_SLOT_IS_FILLED(poll_map, i)) {
continue;
}
poll_obj_t *poll_obj = (poll_obj_t*)poll_map->table[i].value;
int errno;
mp_int_t ret = poll_obj->ioctl(poll_map->table[i].key, MP_IOCTL_POLL, &errno, poll_obj->flags);
poll_obj->flags_ret = ret;
if (ret == -1) {
// error doing ioctl
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
}
if (ret != 0) {
// object is ready
n_ready += 1;
if (rwx_num != NULL) {
if (ret & MP_IOCTL_POLL_RD) {
rwx_num[0] += 1;
}
if (ret & MP_IOCTL_POLL_WR) {
rwx_num[1] += 1;
}
if ((ret & ~(MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR)) != 0) {
rwx_num[2] += 1;
}
}
}
}
return n_ready;
}
/// \function select(rlist, wlist, xlist[, timeout])
STATIC mp_obj_t select_select(uint n_args, const mp_obj_t *args) {
// get array data from tuple/list arguments
mp_uint_t rwx_len[3];
mp_obj_t *r_array, *w_array, *x_array;
mp_obj_get_array(args[0], &rwx_len[0], &r_array);
mp_obj_get_array(args[1], &rwx_len[1], &w_array);
mp_obj_get_array(args[2], &rwx_len[2], &x_array);
// get timeout
mp_uint_t timeout = -1;
if (n_args == 4) {
if (args[3] != mp_const_none) {
float timeout_f = mp_obj_get_float(args[3]);
if (timeout_f >= 0) {
timeout = (mp_uint_t)(timeout_f * 1000);
}
}
}
// merge separate lists and get the ioctl function for each object
mp_map_t poll_map;
mp_map_init(&poll_map, rwx_len[0] + rwx_len[1] + rwx_len[2]);
poll_map_add(&poll_map, r_array, rwx_len[0], MP_IOCTL_POLL_RD, true);
poll_map_add(&poll_map, w_array, rwx_len[1], MP_IOCTL_POLL_WR, true);
poll_map_add(&poll_map, x_array, rwx_len[2], MP_IOCTL_POLL_ERR | MP_IOCTL_POLL_HUP, true);
mp_uint_t start_tick = HAL_GetTick();
rwx_len[0] = rwx_len[1] = rwx_len[2] = 0;
for (;;) {
// poll the objects
mp_uint_t n_ready = poll_map_poll(&poll_map, rwx_len);
if (n_ready > 0 || (timeout != -1 && HAL_GetTick() - start_tick >= timeout)) {
// one or more objects are ready, or we had a timeout
mp_obj_t list_array[3];
list_array[0] = mp_obj_new_list(rwx_len[0], NULL);
list_array[1] = mp_obj_new_list(rwx_len[1], NULL);
list_array[2] = mp_obj_new_list(rwx_len[2], NULL);
rwx_len[0] = rwx_len[1] = rwx_len[2] = 0;
for (mp_uint_t i = 0; i < poll_map.alloc; ++i) {
if (!MP_MAP_SLOT_IS_FILLED(&poll_map, i)) {
continue;
}
poll_obj_t *poll_obj = (poll_obj_t*)poll_map.table[i].value;
if (poll_obj->flags_ret & MP_IOCTL_POLL_RD) {
((mp_obj_list_t*)list_array[0])->items[rwx_len[0]++] = poll_map.table[i].key;
}
if (poll_obj->flags_ret & MP_IOCTL_POLL_WR) {
((mp_obj_list_t*)list_array[1])->items[rwx_len[1]++] = poll_map.table[i].key;
}
if ((poll_obj->flags_ret & ~(MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR)) != 0) {
((mp_obj_list_t*)list_array[2])->items[rwx_len[2]++] = poll_map.table[i].key;
}
}
mp_map_deinit(&poll_map);
return mp_obj_new_tuple(3, list_array);
}
__WFI();
}
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select);
/// \class Poll
typedef struct _mp_obj_poll_t {
mp_obj_base_t base;
mp_map_t poll_map;
} mp_obj_poll_t;
/// \method register(obj[, eventmask])
STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) {
mp_obj_poll_t *self = args[0];
mp_uint_t flags;
if (n_args == 3) {
flags = mp_obj_get_int(args[2]);
} else {
flags = MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR;
}
poll_map_add(&self->poll_map, &args[1], 1, flags, false);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
/// \method unregister(obj)
STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
mp_obj_poll_t *self = self_in;
mp_map_lookup(&self->poll_map, obj_in, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
// TODO raise KeyError if obj didn't exist in map
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
/// \method modify(obj, eventmask)
STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
mp_obj_poll_t *self = self_in;
mp_map_elem_t *elem = mp_map_lookup(&self->poll_map, obj_in, MP_MAP_LOOKUP);
if (elem == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IOError, "object was never registered"));
}
((poll_obj_t*)elem->value)->flags = mp_obj_get_int(eventmask_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
/// \method poll([timeout])
/// Timeout is in milliseconds.
STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
mp_obj_poll_t *self = args[0];
// work out timeout (its given already in ms)
mp_uint_t timeout = -1;
if (n_args == 2) {
if (args[1] != mp_const_none) {
mp_int_t timeout_i = mp_obj_get_int(args[1]);
if (timeout_i >= 0) {
timeout = timeout_i;
}
}
}
mp_uint_t start_tick = HAL_GetTick();
for (;;) {
// poll the objects
mp_uint_t n_ready = poll_map_poll(&self->poll_map, NULL);
if (n_ready > 0 || (timeout != -1 && HAL_GetTick() - start_tick >= timeout)) {
// one or more objects are ready, or we had a timeout
mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL);
mp_uint_t n_ready = 0;
for (mp_uint_t i = 0; i < self->poll_map.alloc; ++i) {
if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) {
continue;
}
poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value;
if (poll_obj->flags_ret != 0) {
mp_obj_t tuple[2] = {self->poll_map.table[i].key, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)};
ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple);
}
}
mp_map_deinit(&self->poll_map);
return ret_list;
}
__WFI();
}
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 2, poll_poll);
STATIC const mp_map_elem_t poll_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_register), (mp_obj_t)&poll_register_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_unregister), (mp_obj_t)&poll_unregister_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_modify), (mp_obj_t)&poll_modify_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&poll_poll_obj },
};
STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
STATIC const mp_obj_type_t mp_type_poll = {
{ &mp_type_type },
.name = MP_QSTR_poll,
.locals_dict = (mp_obj_t)&poll_locals_dict,
};
/// \function poll()
STATIC mp_obj_t select_poll(void) {
mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
poll->base.type = &mp_type_poll;
mp_map_init(&poll->poll_map, 0);
return poll;
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_select_poll_obj, select_poll);
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
#define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_IO_FILEIO (1)
#define MICROPY_PY_UCTYPES (1) #define MICROPY_PY_UCTYPES (1)
#define MICROPY_PY_ZLIBD (1) #define MICROPY_PY_ZLIBD (1)
#define MICROPY_PY_SELECT (1)
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment