sys_stdio_mphal.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/obj.h"
  29. #include "py/stream.h"
  30. #include "py/mperrno.h"
  31. #include "py/mphal.h"
  32. // TODO make stdin, stdout and stderr writable objects so they can
  33. // be changed by Python code. This requires some changes, as these
  34. // objects are in a read-only module (py/modsys.c).
  35. /******************************************************************************/
  36. // MicroPython bindings
  37. #define STDIO_FD_IN (0)
  38. #define STDIO_FD_OUT (1)
  39. #define STDIO_FD_ERR (2)
  40. typedef struct _sys_stdio_obj_t {
  41. mp_obj_base_t base;
  42. int fd;
  43. } sys_stdio_obj_t;
  44. #if MICROPY_PY_SYS_STDIO_BUFFER
  45. STATIC const sys_stdio_obj_t stdio_buffer_obj;
  46. #endif
  47. void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  48. sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
  49. mp_printf(print, "<io.FileIO %d>", self->fd);
  50. }
  51. STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
  52. sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
  53. if (self->fd == STDIO_FD_IN) {
  54. for (uint i = 0; i < size; i++) {
  55. int c = mp_hal_stdin_rx_chr();
  56. if (c == '\r') {
  57. c = '\n';
  58. }
  59. ((byte *)buf)[i] = c;
  60. }
  61. return size;
  62. } else {
  63. *errcode = MP_EPERM;
  64. return MP_STREAM_ERROR;
  65. }
  66. }
  67. STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
  68. sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
  69. if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
  70. mp_hal_stdout_tx_strn_cooked(buf, size);
  71. return size;
  72. } else {
  73. *errcode = MP_EPERM;
  74. return MP_STREAM_ERROR;
  75. }
  76. }
  77. // TODO NOT IMPLEMENT STDIO_IOCTL ON RT-THREAD YET
  78. // STATIC mp_uint_t stdio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  79. // (void)self_in;
  80. // if (request == MP_STREAM_POLL) {
  81. // return mp_hal_stdio_poll(arg);
  82. // } else {
  83. // *errcode = MP_EINVAL;
  84. // return MP_STREAM_ERROR;
  85. // }
  86. // }
  87. STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) {
  88. return mp_const_none;
  89. }
  90. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__);
  91. // TODO gc hook to close the file if not already closed
  92. STATIC const mp_rom_map_elem_t stdio_locals_dict_table[] = {
  93. #if MICROPY_PY_SYS_STDIO_BUFFER
  94. { MP_ROM_QSTR(MP_QSTR_buffer), MP_ROM_PTR(&stdio_buffer_obj) },
  95. #endif
  96. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  97. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  98. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
  99. { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj)},
  100. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  101. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_identity_obj) },
  102. { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_identity_obj) },
  103. { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
  104. { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stdio_obj___exit___obj) },
  105. };
  106. STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
  107. STATIC const mp_stream_p_t stdio_obj_stream_p = {
  108. .read = stdio_read,
  109. .write = stdio_write,
  110. //.ioctl = stdio_ioctl,
  111. .is_text = true,
  112. };
  113. STATIC const mp_obj_type_t stdio_obj_type = {
  114. { &mp_type_type },
  115. .name = MP_QSTR_FileIO,
  116. // TODO .make_new?
  117. .print = stdio_obj_print,
  118. .getiter = mp_identity_getiter,
  119. .iternext = mp_stream_unbuffered_iter,
  120. .protocol = &stdio_obj_stream_p,
  121. .locals_dict = (mp_obj_dict_t *)&stdio_locals_dict,
  122. };
  123. const sys_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN};
  124. const sys_stdio_obj_t mp_sys_stdout_obj = {{&stdio_obj_type}, .fd = STDIO_FD_OUT};
  125. const sys_stdio_obj_t mp_sys_stderr_obj = {{&stdio_obj_type}, .fd = STDIO_FD_ERR};
  126. #if MICROPY_PY_SYS_STDIO_BUFFER
  127. STATIC mp_uint_t stdio_buffer_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
  128. for (uint i = 0; i < size; i++) {
  129. ((byte *)buf)[i] = mp_hal_stdin_rx_chr();
  130. }
  131. return size;
  132. }
  133. STATIC mp_uint_t stdio_buffer_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
  134. mp_hal_stdout_tx_strn(buf, size);
  135. return size;
  136. }
  137. STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = {
  138. .read = stdio_buffer_read,
  139. .write = stdio_buffer_write,
  140. .ioctl = stdio_ioctl,
  141. .is_text = false,
  142. };
  143. STATIC const mp_obj_type_t stdio_buffer_obj_type = {
  144. { &mp_type_type },
  145. .name = MP_QSTR_FileIO,
  146. .print = stdio_obj_print,
  147. .getiter = mp_identity_getiter,
  148. .iternext = mp_stream_unbuffered_iter,
  149. .protocol = &stdio_buffer_obj_stream_p,
  150. .locals_dict = (mp_obj_dict_t *)&stdio_locals_dict,
  151. };
  152. STATIC const sys_stdio_obj_t stdio_buffer_obj = {{&stdio_buffer_obj_type}, .fd = 0}; // fd unused
  153. #endif