1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 Paul Sokolovsky
7  * Copyright (c) 2017-2019 Damien P. George
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 
28 #include <string.h>
29 #include "py/mpconfig.h"
30 
31 #include "py/runtime.h"
32 #include "py/objtuple.h"
33 #include "py/objarray.h"
34 #include "py/stream.h"
35 #include "extmod/misc.h"
36 #include "shared/runtime/interrupt_char.h"
37 
38 #if MICROPY_PY_OS_DUPTERM
39 
mp_uos_deactivate(size_t dupterm_idx,const char * msg,mp_obj_t exc)40 void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) {
41     mp_obj_t term = MP_STATE_VM(dupterm_objs[dupterm_idx]);
42     MP_STATE_VM(dupterm_objs[dupterm_idx]) = MP_OBJ_NULL;
43     mp_printf(&mp_plat_print, msg);
44     if (exc != MP_OBJ_NULL) {
45         mp_obj_print_exception(&mp_plat_print, exc);
46     }
47     nlr_buf_t nlr;
48     if (nlr_push(&nlr) == 0) {
49         mp_stream_close(term);
50         nlr_pop();
51     } else {
52         // Ignore any errors during stream closing
53     }
54 }
55 
mp_uos_dupterm_poll(uintptr_t poll_flags)56 uintptr_t mp_uos_dupterm_poll(uintptr_t poll_flags) {
57     uintptr_t poll_flags_out = 0;
58 
59     for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
60         mp_obj_t s = MP_STATE_VM(dupterm_objs[idx]);
61         if (s == MP_OBJ_NULL) {
62             continue;
63         }
64 
65         int errcode = 0;
66         mp_uint_t ret = 0;
67         const mp_stream_p_t *stream_p = mp_get_stream(s);
68         #if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM
69         if (mp_uos_dupterm_is_builtin_stream(s)) {
70             ret = stream_p->ioctl(s, MP_STREAM_POLL, poll_flags, &errcode);
71         } else
72         #endif
73         {
74             nlr_buf_t nlr;
75             if (nlr_push(&nlr) == 0) {
76                 ret = stream_p->ioctl(s, MP_STREAM_POLL, poll_flags, &errcode);
77                 nlr_pop();
78             } else {
79                 // Ignore error with ioctl
80             }
81         }
82 
83         if (ret != MP_STREAM_ERROR) {
84             poll_flags_out |= ret;
85             if (poll_flags_out == poll_flags) {
86                 // Finish early if all requested flags are set
87                 break;
88             }
89         }
90     }
91 
92     return poll_flags_out;
93 }
94 
mp_uos_dupterm_rx_chr(void)95 int mp_uos_dupterm_rx_chr(void) {
96     for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
97         if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
98             continue;
99         }
100 
101         #if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM
102         if (mp_uos_dupterm_is_builtin_stream(MP_STATE_VM(dupterm_objs[idx]))) {
103             byte buf[1];
104             int errcode = 0;
105             const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_VM(dupterm_objs[idx]));
106             mp_uint_t out_sz = stream_p->read(MP_STATE_VM(dupterm_objs[idx]), buf, 1, &errcode);
107             if (errcode == 0 && out_sz != 0) {
108                 return buf[0];
109             } else {
110                 continue;
111             }
112         }
113         #endif
114 
115         nlr_buf_t nlr;
116         if (nlr_push(&nlr) == 0) {
117             byte buf[1];
118             int errcode;
119             const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_VM(dupterm_objs[idx]));
120             mp_uint_t out_sz = stream_p->read(MP_STATE_VM(dupterm_objs[idx]), buf, 1, &errcode);
121             if (out_sz == 0) {
122                 nlr_pop();
123                 mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL);
124             } else if (out_sz == MP_STREAM_ERROR) {
125                 // errcode is valid
126                 if (mp_is_nonblocking_error(errcode)) {
127                     nlr_pop();
128                 } else {
129                     mp_raise_OSError(errcode);
130                 }
131             } else {
132                 // read 1 byte
133                 nlr_pop();
134                 if (buf[0] == mp_interrupt_char) {
135                     // Signal keyboard interrupt to be raised as soon as the VM resumes
136                     mp_sched_keyboard_interrupt();
137                     return -2;
138                 }
139                 return buf[0];
140             }
141         } else {
142             mp_uos_deactivate(idx, "dupterm: Exception in read() method, deactivating: ", MP_OBJ_FROM_PTR(nlr.ret_val));
143         }
144     }
145 
146     // No chars available
147     return -1;
148 }
149 
mp_uos_dupterm_tx_strn(const char * str,size_t len)150 void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
151     for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
152         if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
153             continue;
154         }
155 
156         #if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM
157         if (mp_uos_dupterm_is_builtin_stream(MP_STATE_VM(dupterm_objs[idx]))) {
158             int errcode = 0;
159             const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_VM(dupterm_objs[idx]));
160             stream_p->write(MP_STATE_VM(dupterm_objs[idx]), str, len, &errcode);
161             continue;
162         }
163         #endif
164 
165         nlr_buf_t nlr;
166         if (nlr_push(&nlr) == 0) {
167             mp_stream_write(MP_STATE_VM(dupterm_objs[idx]), str, len, MP_STREAM_RW_WRITE);
168             nlr_pop();
169         } else {
170             mp_uos_deactivate(idx, "dupterm: Exception in write() method, deactivating: ", MP_OBJ_FROM_PTR(nlr.ret_val));
171         }
172     }
173 }
174 
mp_uos_dupterm(size_t n_args,const mp_obj_t * args)175 STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) {
176     mp_int_t idx = 0;
177     if (n_args == 2) {
178         idx = mp_obj_get_int(args[1]);
179     }
180 
181     if (idx < 0 || idx >= MICROPY_PY_OS_DUPTERM) {
182         mp_raise_ValueError(MP_ERROR_TEXT("invalid dupterm index"));
183     }
184 
185     mp_obj_t previous_obj = MP_STATE_VM(dupterm_objs[idx]);
186     if (previous_obj == MP_OBJ_NULL) {
187         previous_obj = mp_const_none;
188     }
189     if (args[0] == mp_const_none) {
190         MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL;
191     } else {
192         mp_get_stream_raise(args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
193         MP_STATE_VM(dupterm_objs[idx]) = args[0];
194     }
195 
196     return previous_obj;
197 }
198 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 1, 2, mp_uos_dupterm);
199 
200 #endif
201