py/stream: Move ad-hoc ioctl constants to stream.h and rename them.

The constants MP_IOCTL_POLL_xxx, which were stmhal-specific, are moved
from stmhal/pybioctl.h (now deleted) to py/stream.h.  And they are renamed
to MP_STREAM_POLL_xxx to be consistent with other such constants.

All uses of these constants have been updated.
This commit is contained in:
Damien George 2016-12-02 16:37:29 +11:00
parent 6194336d81
commit 304cfda8c4
9 changed files with 61 additions and 68 deletions

View File

@ -45,7 +45,6 @@
#include "modnetwork.h"
#include "modusocket.h"
#include "modwlan.h"
#include "pybioctl.h"
#include "pybrtc.h"
#include "debug.h"
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
@ -1461,7 +1460,7 @@ int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_s, int
int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno) {
mp_int_t ret;
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
int32_t sd = s->sock_base.sd;
@ -1473,13 +1472,13 @@ int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t
FD_ZERO(&xfds);
// set fds if needed
if (flags & MP_IOCTL_POLL_RD) {
if (flags & MP_STREAM_POLL_RD) {
FD_SET(sd, &rfds);
}
if (flags & MP_IOCTL_POLL_WR) {
if (flags & MP_STREAM_POLL_WR) {
FD_SET(sd, &wfds);
}
if (flags & MP_IOCTL_POLL_HUP) {
if (flags & MP_STREAM_POLL_HUP) {
FD_SET(sd, &xfds);
}
@ -1497,13 +1496,13 @@ int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t
// check return of select
if (FD_ISSET(sd, &rfds)) {
ret |= MP_IOCTL_POLL_RD;
ret |= MP_STREAM_POLL_RD;
}
if (FD_ISSET(sd, &wfds)) {
ret |= MP_IOCTL_POLL_WR;
ret |= MP_STREAM_POLL_WR;
}
if (FD_ISSET(sd, &xfds)) {
ret |= MP_IOCTL_POLL_HUP;
ret |= MP_STREAM_POLL_HUP;
}
} else {
*_errno = EINVAL;

View File

@ -46,7 +46,6 @@
#include "uart.h"
#include "pybuart.h"
#include "mpirq.h"
#include "pybioctl.h"
#include "pybsleep.h"
#include "mpexception.h"
#include "py/mpstate.h"
@ -630,14 +629,14 @@ STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t a
mp_uint_t ret;
uart_check_init(self);
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && uart_rx_any(self)) {
ret |= MP_IOCTL_POLL_RD;
if ((flags & MP_STREAM_POLL_RD) && uart_rx_any(self)) {
ret |= MP_STREAM_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && MAP_UARTSpaceAvail(self->reg)) {
ret |= MP_IOCTL_POLL_WR;
if ((flags & MP_STREAM_POLL_WR) && MAP_UARTSpaceAvail(self->reg)) {
ret |= MP_STREAM_POLL_WR;
}
} else {
*errcode = EINVAL;

View File

@ -32,9 +32,9 @@
#include "py/runtime.h"
#include "py/obj.h"
#include "py/objlist.h"
#include "py/stream.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "pybioctl.h"
// Flags for poll()
#define FLAG_ONESHOT (1)
@ -87,7 +87,7 @@ STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) {
poll_obj_t *poll_obj = (poll_obj_t*)poll_map->table[i].value;
int errcode;
mp_int_t ret = poll_obj->ioctl(poll_obj->obj, MP_IOCTL_POLL, poll_obj->flags, &errcode);
mp_int_t ret = poll_obj->ioctl(poll_obj->obj, MP_STREAM_POLL, poll_obj->flags, &errcode);
poll_obj->flags_ret = ret;
if (ret == -1) {
@ -99,13 +99,13 @@ STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) {
// object is ready
n_ready += 1;
if (rwx_num != NULL) {
if (ret & MP_IOCTL_POLL_RD) {
if (ret & MP_STREAM_POLL_RD) {
rwx_num[0] += 1;
}
if (ret & MP_IOCTL_POLL_WR) {
if (ret & MP_STREAM_POLL_WR) {
rwx_num[1] += 1;
}
if ((ret & ~(MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR)) != 0) {
if ((ret & ~(MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)) != 0) {
rwx_num[2] += 1;
}
}
@ -141,9 +141,9 @@ STATIC mp_obj_t select_select(uint n_args, const mp_obj_t *args) {
// 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);
poll_map_add(&poll_map, r_array, rwx_len[0], MP_STREAM_POLL_RD, true);
poll_map_add(&poll_map, w_array, rwx_len[1], MP_STREAM_POLL_WR, true);
poll_map_add(&poll_map, x_array, rwx_len[2], MP_STREAM_POLL_ERR | MP_STREAM_POLL_HUP, true);
mp_uint_t start_tick = mp_hal_ticks_ms();
rwx_len[0] = rwx_len[1] = rwx_len[2] = 0;
@ -163,13 +163,13 @@ STATIC mp_obj_t select_select(uint n_args, const mp_obj_t *args) {
continue;
}
poll_obj_t *poll_obj = (poll_obj_t*)poll_map.table[i].value;
if (poll_obj->flags_ret & MP_IOCTL_POLL_RD) {
if (poll_obj->flags_ret & MP_STREAM_POLL_RD) {
((mp_obj_list_t*)list_array[0])->items[rwx_len[0]++] = poll_obj->obj;
}
if (poll_obj->flags_ret & MP_IOCTL_POLL_WR) {
if (poll_obj->flags_ret & MP_STREAM_POLL_WR) {
((mp_obj_list_t*)list_array[1])->items[rwx_len[1]++] = poll_obj->obj;
}
if ((poll_obj->flags_ret & ~(MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR)) != 0) {
if ((poll_obj->flags_ret & ~(MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)) != 0) {
((mp_obj_list_t*)list_array[2])->items[rwx_len[2]++] = poll_obj->obj;
}
}
@ -195,7 +195,7 @@ STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) {
if (n_args == 3) {
flags = mp_obj_get_int(args[2]);
} else {
flags = MP_IOCTL_POLL_RD | MP_IOCTL_POLL_WR;
flags = MP_STREAM_POLL_RD | MP_STREAM_POLL_WR;
}
poll_map_add(&self->poll_map, &args[1], 1, flags, false);
return mp_const_none;
@ -300,10 +300,10 @@ STATIC const mp_map_elem_t mp_module_select_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uselect) },
{ 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 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLIN), MP_OBJ_NEW_SMALL_INT(MP_IOCTL_POLL_RD) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLOUT), MP_OBJ_NEW_SMALL_INT(MP_IOCTL_POLL_WR) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLERR), MP_OBJ_NEW_SMALL_INT(MP_IOCTL_POLL_ERR) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLHUP), MP_OBJ_NEW_SMALL_INT(MP_IOCTL_POLL_HUP) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLIN), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_RD) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLOUT), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_WR) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLERR), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_ERR) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLHUP), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_HUP) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table);

View File

@ -42,6 +42,12 @@
#define MP_STREAM_GET_DATA_OPTS (8) // Get data/message options
#define MP_STREAM_SET_DATA_OPTS (9) // Set data/message options
// These poll ioctl values are compatible with Linux
#define MP_STREAM_POLL_RD (0x0001)
#define MP_STREAM_POLL_WR (0x0004)
#define MP_STREAM_POLL_ERR (0x0008)
#define MP_STREAM_POLL_HUP (0x0010)
// Argument structure for MP_STREAM_SEEK
struct mp_stream_seek_t {
mp_off_t offset;

View File

@ -32,11 +32,11 @@
#include "py/objtuple.h"
#include "py/runtime.h"
#include "py/gc.h"
#include "py/stream.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "bufhelper.h"
#include "can.h"
#include "pybioctl.h"
#include "irq.h"
#if MICROPY_HW_ENABLE_CAN
@ -807,16 +807,16 @@ STATIC MP_DEFINE_CONST_DICT(pyb_can_locals_dict, pyb_can_locals_dict_table);
mp_uint_t can_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
pyb_can_obj_t *self = self_in;
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD)
if ((flags & MP_STREAM_POLL_RD)
&& ((__HAL_CAN_MSG_PENDING(&self->can, CAN_FIFO0) != 0)
|| (__HAL_CAN_MSG_PENDING(&self->can, CAN_FIFO1) != 0))) {
ret |= MP_IOCTL_POLL_RD;
ret |= MP_STREAM_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && (self->can.Instance->TSR & CAN_TSR_TME)) {
ret |= MP_IOCTL_POLL_WR;
if ((flags & MP_STREAM_POLL_WR) && (self->can.Instance->TSR & CAN_TSR_TME)) {
ret |= MP_STREAM_POLL_WR;
}
} else {
*errcode = MP_EINVAL;

View File

@ -41,7 +41,6 @@
#include "pin.h"
#include "genhdr/pins.h"
#include "spi.h"
#include "pybioctl.h"
#include "hci.h"
#include "socket.h"
@ -354,7 +353,7 @@ STATIC int cc3k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t ti
STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) {
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
int fd = socket->u_state;
@ -366,19 +365,19 @@ STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request
FD_ZERO(&xfds);
// set fds if needed
if (flags & MP_IOCTL_POLL_RD) {
if (flags & MP_STREAM_POLL_RD) {
FD_SET(fd, &rfds);
// A socked that just closed is available for reading. A call to
// recv() returns 0 which is consistent with BSD.
if (cc3k_get_fd_closed_state(fd)) {
ret |= MP_IOCTL_POLL_RD;
ret |= MP_STREAM_POLL_RD;
}
}
if (flags & MP_IOCTL_POLL_WR) {
if (flags & MP_STREAM_POLL_WR) {
FD_SET(fd, &wfds);
}
if (flags & MP_IOCTL_POLL_HUP) {
if (flags & MP_STREAM_POLL_HUP) {
FD_SET(fd, &xfds);
}
@ -396,13 +395,13 @@ STATIC int cc3k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request
// check return of select
if (FD_ISSET(fd, &rfds)) {
ret |= MP_IOCTL_POLL_RD;
ret |= MP_STREAM_POLL_RD;
}
if (FD_ISSET(fd, &wfds)) {
ret |= MP_IOCTL_POLL_WR;
ret |= MP_STREAM_POLL_WR;
}
if (FD_ISSET(fd, &xfds)) {
ret |= MP_IOCTL_POLL_HUP;
ret |= MP_STREAM_POLL_HUP;
}
} else {
*_errno = MP_EINVAL;

View File

@ -1,8 +0,0 @@
#define MP_IOCTL_POLL (0x100 | 1)
// These values are compatible with Linux, which are in turn
// compatible with iBCS2 spec.
#define MP_IOCTL_POLL_RD (0x0001)
#define MP_IOCTL_POLL_WR (0x0004)
#define MP_IOCTL_POLL_ERR (0x0008)
#define MP_IOCTL_POLL_HUP (0x0010)

View File

@ -34,7 +34,6 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "uart.h"
#include "pybioctl.h"
#include "irq.h"
//TODO: Add UART7/8 support for MCU_SERIES_F7
@ -901,14 +900,14 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
pyb_uart_obj_t *self = self_in;
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && uart_rx_any(self)) {
ret |= MP_IOCTL_POLL_RD;
if ((flags & MP_STREAM_POLL_RD) && uart_rx_any(self)) {
ret |= MP_STREAM_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_TXE)) {
ret |= MP_IOCTL_POLL_WR;
if ((flags & MP_STREAM_POLL_WR) && __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_TXE)) {
ret |= MP_STREAM_POLL_WR;
}
} else {
*errcode = MP_EINVAL;

View File

@ -40,7 +40,6 @@
#include "py/mperrno.h"
#include "bufhelper.h"
#include "usb.h"
#include "pybioctl.h"
#if defined(USE_USB_FS)
#define USB_PHY_ID USB_PHY_FS_ID
@ -502,14 +501,14 @@ STATIC mp_uint_t pyb_usb_vcp_write(mp_obj_t self_in, const void *buf, mp_uint_t
STATIC mp_uint_t pyb_usb_vcp_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && USBD_CDC_RxNum() > 0) {
ret |= MP_IOCTL_POLL_RD;
if ((flags & MP_STREAM_POLL_RD) && USBD_CDC_RxNum() > 0) {
ret |= MP_STREAM_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && USBD_CDC_TxHalfEmpty()) {
ret |= MP_IOCTL_POLL_WR;
if ((flags & MP_STREAM_POLL_WR) && USBD_CDC_TxHalfEmpty()) {
ret |= MP_STREAM_POLL_WR;
}
} else {
*errcode = MP_EINVAL;
@ -632,11 +631,11 @@ STATIC MP_DEFINE_CONST_DICT(pyb_usb_hid_locals_dict, pyb_usb_hid_locals_dict_tab
STATIC mp_uint_t pyb_usb_hid_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
if (request == MP_STREAM_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_WR) && USBD_HID_CanSendReport(&hUSBDDevice)) {
ret |= MP_IOCTL_POLL_WR;
if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&hUSBDDevice)) {
ret |= MP_STREAM_POLL_WR;
}
} else {
*errcode = MP_EINVAL;