1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013-2019 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "py/obj.h"
31 #include "py/stream.h"
32 #include "py/mperrno.h"
33 #include "py/mphal.h"
34
35 // Modified bt HaaS begin
36 #if !MICROPY_VFS_POSIX && !MICROPY_VFS_POSIX_FILE //disabled by HaaS-AMP
37 // Modified bt HaaS end
38
39 // TODO make stdin, stdout and stderr writable objects so they can
40 // be changed by Python code. This requires some changes, as these
41 // objects are in a read-only module (py/modsys.c).
42
43 /******************************************************************************/
44 // MicroPython bindings
45
46 #define STDIO_FD_IN (0)
47 #define STDIO_FD_OUT (1)
48 #define STDIO_FD_ERR (2)
49
50 typedef struct _sys_stdio_obj_t {
51 mp_obj_base_t base;
52 int fd;
53 } sys_stdio_obj_t;
54
55 #if MICROPY_PY_SYS_STDIO_BUFFER
56 STATIC const sys_stdio_obj_t stdio_buffer_obj;
57 #endif
58
stdio_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)59 void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
60 sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
61 mp_printf(print, "<io.FileIO %d>", self->fd);
62 }
63
stdio_read(mp_obj_t self_in,void * buf,mp_uint_t size,int * errcode)64 STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
65 sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
66 if (self->fd == STDIO_FD_IN) {
67 for (uint i = 0; i < size; i++) {
68 int c = mp_hal_stdin_rx_chr();
69 if (c == '\r') {
70 c = '\n';
71 }
72 ((byte *)buf)[i] = c;
73 }
74 return size;
75 } else {
76 *errcode = MP_EPERM;
77 return MP_STREAM_ERROR;
78 }
79 }
80
stdio_write(mp_obj_t self_in,const void * buf,mp_uint_t size,int * errcode)81 STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
82 sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
83 if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
84 mp_hal_stdout_tx_strn_cooked(buf, size);
85 return size;
86 } else {
87 *errcode = MP_EPERM;
88 return MP_STREAM_ERROR;
89 }
90 }
91
stdio_ioctl(mp_obj_t self_in,mp_uint_t request,uintptr_t arg,int * errcode)92 STATIC mp_uint_t stdio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
93 (void)self_in;
94 if (request == MP_STREAM_POLL) {
95 return mp_hal_stdio_poll(arg);
96 } else {
97 *errcode = MP_EINVAL;
98 return MP_STREAM_ERROR;
99 }
100 }
101
stdio_obj___exit__(size_t n_args,const mp_obj_t * args)102 STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) {
103 return mp_const_none;
104 }
105 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__);
106
107 // TODO gc hook to close the file if not already closed
108
109 STATIC const mp_rom_map_elem_t stdio_locals_dict_table[] = {
110 #if MICROPY_PY_SYS_STDIO_BUFFER
111 { MP_ROM_QSTR(MP_QSTR_buffer), MP_ROM_PTR(&stdio_buffer_obj) },
112 #endif
113 { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
114 { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
115 { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
116 { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj)},
117 { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
118 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_identity_obj) },
119 { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_identity_obj) },
120 { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
121 { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stdio_obj___exit___obj) },
122 };
123
124 STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
125
126 STATIC const mp_stream_p_t stdio_obj_stream_p = {
127 .read = stdio_read,
128 .write = stdio_write,
129 .ioctl = stdio_ioctl,
130 .is_text = true,
131 };
132
133 STATIC const mp_obj_type_t stdio_obj_type = {
134 { &mp_type_type },
135 .name = MP_QSTR_FileIO,
136 // TODO .make_new?
137 .print = stdio_obj_print,
138 .getiter = mp_identity_getiter,
139 .iternext = mp_stream_unbuffered_iter,
140 .protocol = &stdio_obj_stream_p,
141 .locals_dict = (mp_obj_dict_t *)&stdio_locals_dict,
142 };
143
144 const sys_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN};
145 const sys_stdio_obj_t mp_sys_stdout_obj = {{&stdio_obj_type}, .fd = STDIO_FD_OUT};
146 const sys_stdio_obj_t mp_sys_stderr_obj = {{&stdio_obj_type}, .fd = STDIO_FD_ERR};
147
148 #if MICROPY_PY_SYS_STDIO_BUFFER
stdio_buffer_read(mp_obj_t self_in,void * buf,mp_uint_t size,int * errcode)149 STATIC mp_uint_t stdio_buffer_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
150 for (uint i = 0; i < size; i++) {
151 ((byte *)buf)[i] = mp_hal_stdin_rx_chr();
152 }
153 return size;
154 }
155
stdio_buffer_write(mp_obj_t self_in,const void * buf,mp_uint_t size,int * errcode)156 STATIC mp_uint_t stdio_buffer_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
157 mp_hal_stdout_tx_strn(buf, size);
158 return size;
159 }
160
161 STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = {
162 .read = stdio_buffer_read,
163 .write = stdio_buffer_write,
164 .ioctl = stdio_ioctl,
165 .is_text = false,
166 };
167
168 STATIC const mp_obj_type_t stdio_buffer_obj_type = {
169 { &mp_type_type },
170 .name = MP_QSTR_FileIO,
171 .print = stdio_obj_print,
172 .getiter = mp_identity_getiter,
173 .iternext = mp_stream_unbuffered_iter,
174 .protocol = &stdio_buffer_obj_stream_p,
175 .locals_dict = (mp_obj_dict_t *)&stdio_locals_dict,
176 };
177
178 STATIC const sys_stdio_obj_t stdio_buffer_obj = {{&stdio_buffer_obj_type}, .fd = 0}; // fd unused
179 #endif
180
181 // Modified bt HaaS begin
182 #endif
183 // Modified bt HaaS end
184