diff options
author | Damien George <damien.p.george@gmail.com> | 2019-06-24 16:20:33 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-07-01 17:10:12 +1000 |
commit | 964ae328cd00260d9a017a4e67909694fded9902 (patch) | |
tree | 1d594aa8d0252067cf4e0808b5469d4e7c07ba2c /extmod/uos_dupterm.c | |
parent | b7da67cdaaf32317cfc9a3940bd58f2aab4976c9 (diff) |
extmod/uos_dupterm: Add mp_uos_dupterm_poll to poll all dupterms.
Diffstat (limited to 'extmod/uos_dupterm.c')
-rw-r--r-- | extmod/uos_dupterm.c | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/extmod/uos_dupterm.c b/extmod/uos_dupterm.c index 42cb21444..9a8f0af4e 100644 --- a/extmod/uos_dupterm.c +++ b/extmod/uos_dupterm.c @@ -4,7 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2016 Paul Sokolovsky - * Copyright (c) 2017 Damien P. George + * Copyright (c) 2017-2019 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 @@ -53,6 +53,45 @@ void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) { } } +uintptr_t mp_uos_dupterm_poll(uintptr_t poll_flags) { + uintptr_t poll_flags_out = 0; + + for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) { + mp_obj_t s = MP_STATE_VM(dupterm_objs[idx]); + if (s == MP_OBJ_NULL) { + continue; + } + + int errcode = 0; + mp_uint_t ret = 0; + const mp_stream_p_t *stream_p = mp_get_stream(s); + #if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM + if (mp_uos_dupterm_is_builtin_stream(s)) { + ret = stream_p->ioctl(s, MP_STREAM_POLL, poll_flags, &errcode); + } else + #endif + { + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + ret = stream_p->ioctl(s, MP_STREAM_POLL, poll_flags, &errcode); + nlr_pop(); + } else { + // Ignore error with ioctl + } + } + + if (ret != MP_STREAM_ERROR) { + poll_flags_out |= ret; + if (poll_flags_out == poll_flags) { + // Finish early if all requested flags are set + break; + } + } + } + + return poll_flags_out; +} + int mp_uos_dupterm_rx_chr(void) { for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) { if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) { |