Skip to content

extmod/uos: Expose stream ioctl constants, an ioctl() function #10030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions extmod/moduos.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
#include "genhdr/mpversion.h"
#endif

#if MICROPY_PY_USELECT || MICROPY_PY_USELECT_POSIX
#include "py/stream.h"
#endif

#ifdef MICROPY_PY_UOS_INCLUDEFILE
#include MICROPY_PY_UOS_INCLUDEFILE
#endif
Expand Down Expand Up @@ -102,6 +106,21 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_uos_uname_obj, mp_uos_uname);

#endif

#if MICROPY_PY_USELECT || MICROPY_PY_USELECT_POSIX
STATIC mp_obj_t select_ioctl(mp_obj_t stream, mp_obj_t reg, mp_obj_t arg) {
const mp_stream_p_t *stream_p = mp_get_stream_raise(stream, MP_STREAM_OP_IOCTL);
int errcode = 0;
mp_int_t ret = stream_p->ioctl(stream, mp_obj_int_get_uint_checked(reg), (uintptr_t)mp_obj_int_get_uint_checked(arg), &errcode);
if (ret == -1) {
// error doing ioctl
mp_raise_OSError(errcode);
}

return mp_obj_new_int(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mp_select_ioctl_obj, select_ioctl);
#endif

STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },

Expand Down Expand Up @@ -168,6 +187,27 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_VfsPosix), MP_ROM_PTR(&mp_type_vfs_posix) },
#endif
#endif

#if MICROPY_PY_USELECT || MICROPY_PY_USELECT_POSIX
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_select_ioctl_obj) },

{ MP_ROM_QSTR(MP_QSTR_STREAM_FLUSH), MP_ROM_INT(MP_STREAM_FLUSH) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_SEEK), MP_ROM_INT(MP_STREAM_SEEK) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_POLL), MP_ROM_INT(MP_STREAM_POLL) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_CLOSE), MP_ROM_INT(MP_STREAM_CLOSE) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_TIMEOUT), MP_ROM_INT(MP_STREAM_TIMEOUT) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_GET_OPTS), MP_ROM_INT(MP_STREAM_GET_OPTS) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_SET_OPTS), MP_ROM_INT(MP_STREAM_SET_OPTS) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_GET_DATA_OPTS), MP_ROM_INT(MP_STREAM_GET_DATA_OPTS) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_SET_DATA_OPTS), MP_ROM_INT(MP_STREAM_SET_DATA_OPTS) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_GET_FILENO), MP_ROM_INT(MP_STREAM_GET_FILENO) },

{ MP_ROM_QSTR(MP_QSTR_STREAM_POLL_RD), MP_ROM_INT(MP_STREAM_POLL_RD) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_POLL_WR), MP_ROM_INT(MP_STREAM_POLL_WR) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_POLL_ERR), MP_ROM_INT(MP_STREAM_POLL_ERR) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_POLL_HUP), MP_ROM_INT(MP_STREAM_POLL_HUP) },
{ MP_ROM_QSTR(MP_QSTR_STREAM_POLL_NVAL), MP_ROM_INT(MP_STREAM_POLL_NVAL) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);

Expand Down
37 changes: 37 additions & 0 deletions tests/extmod/uos_ioctl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import uos
import uio

class TestIO(uio.IOBase):
def __init__(self):
self._read_rdy = False
self._write_rdy = False

def ioctl(self, req, arg):
if req == uos.STREAM_POLL:
ret = 0
if arg & uos.STREAM_POLL_RD and self._read_rdy:
ret |= uos.STREAM_POLL_RD
if arg & uos.STREAM_POLL_WR and self._write_rdy:
ret |= uos.STREAM_POLL_WR
return ret

raise Exception("Foo")


f = TestIO()

def _assert_ioctl_poll(expected):
assert f.ioctl(uos.STREAM_POLL, uos.STREAM_POLL_RD | uos.STREAM_POLL_WR) == expected

_assert_ioctl_poll(0)
f._read_rdy = True
_assert_ioctl_poll(uos.STREAM_POLL_RD)
f._write_rdy = True
_assert_ioctl_poll(uos.STREAM_POLL_RD | uos.STREAM_POLL_WR)

try:
uos.ioctl(f, 666, 0)
except Exception as exc:
assert repr(exc) == "Exception('Foo',)"
else:
assert False
Empty file added tests/extmod/uos_ioctl.py.exp
Empty file.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy