summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2021-09-17 11:48:37 +1000
committerDamien George <damien@micropython.org>2021-10-19 22:48:10 +1100
commitcc42b7c88b0b2b11e1e6d389c773e36545e2aef0 (patch)
treef1c61ac91d54ffc57ffe6ba420264a2b53ea8119
parent2ceeabf1800e816345baad76214e8d6fe73b2901 (diff)
unix/modusocket: Support MP_STREAM_POLL in unix socket_ioctl.
Allows asyncio reading of network sockets when MICROPY_PY_USELECT is used in the build configuration.
-rw-r--r--ports/unix/modusocket.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/ports/unix/modusocket.c b/ports/unix/modusocket.c
index 1c9ef3362..951cb7a21 100644
--- a/ports/unix/modusocket.c
+++ b/ports/unix/modusocket.c
@@ -46,6 +46,7 @@
#include "py/builtin.h"
#include "py/mphal.h"
#include "py/mpthread.h"
+#include <poll.h>
/*
The idea of this module is to implement reasonable minimum of
@@ -144,6 +145,29 @@ STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, i
case MP_STREAM_GET_FILENO:
return self->fd;
+ #if MICROPY_PY_USELECT
+ case MP_STREAM_POLL: {
+ 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 = self->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
+
default:
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;