diff options
| author | Andrew Leech <andrew.leech@planetinnovation.com.au> | 2021-08-13 16:02:41 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-10-19 22:47:18 +1100 |
| commit | 2ceeabf1800e816345baad76214e8d6fe73b2901 (patch) | |
| tree | b70a1acf82c9027acc4b67fd076aa0eb9516bc2f | |
| parent | ba940250a5b630018c8d9b0e21c5ed858a20450f (diff) | |
extmod/vfs_posix_file: Support MP_STREAM_POLL in vfs_posix_file_ioctl.
Allows asyncio reading of sys.stdin when MICROPY_PY_USELECT is used in the
build configuration.
| -rw-r--r-- | extmod/vfs_posix_file.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index f3eac98ce..837c5489b 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -37,6 +37,8 @@ #ifdef _WIN32 #define fsync _commit +#else +#include <poll.h> #endif typedef struct _mp_obj_vfs_posix_file_t { @@ -206,6 +208,32 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_ return 0; case MP_STREAM_GET_FILENO: return o->fd; + #if MICROPY_PY_USELECT + case MP_STREAM_POLL: { + #ifdef _WIN32 + mp_raise_NotImplementedError(MP_ERROR_TEXT("poll on file not available on win32")); + #else + mp_uint_t ret = 0; + uint8_t pollevents = 0; + if (arg & MP_STREAM_POLL_RD) { + pollevents |= POLLIN; + } + if (arg & MP_STREAM_POLL_WR) { + pollevents |= POLLOUT; + } + struct pollfd pfd = { .fd = o->fd, .events = pollevents }; + if (poll(&pfd, 1, 0) > 0) { + if (pfd.revents & POLLIN) { + ret |= MP_STREAM_POLL_RD; + } + if (pfd.revents & POLLOUT) { + ret |= MP_STREAM_POLL_WR; + } + } + return ret; + #endif + } + #endif default: *errcode = EINVAL; return MP_STREAM_ERROR; |
