sys_stdio_mphal.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2017 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. STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) {
  78. return mp_const_none;
  79. }
  80. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stdio_obj___exit___obj, 4, 4, stdio_obj___exit__);
  81. // TODO gc hook to close the file if not already closed
  82. STATIC const mp_rom_map_elem_t stdio_locals_dict_table[] = {
  83. #if MICROPY_PY_SYS_STDIO_BUFFER
  84. { MP_ROM_QSTR(MP_QSTR_buffer), MP_ROM_PTR(&stdio_buffer_obj) },
  85. #endif
  86. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  87. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  88. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
  89. { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj)},
  90. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  91. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_identity_obj) },
  92. { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_identity_obj) },
  93. { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
  94. { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stdio_obj___exit___obj) },
  95. };
  96. STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
  97. STATIC const mp_stream_p_t stdio_obj_stream_p = {
  98. .read = stdio_read,
  99. .write = stdio_write,
  100. .is_text = true,
  101. };
  102. STATIC const mp_obj_type_t stdio_obj_type = {
  103. { &mp_type_type },
  104. .name = MP_QSTR_FileIO,
  105. // TODO .make_new?
  106. .print = stdio_obj_print,
  107. .getiter = mp_identity_getiter,
  108. .iternext = mp_stream_unbuffered_iter,
  109. .protocol = &stdio_obj_stream_p,
  110. .locals_dict = (mp_obj_dict_t*)&stdio_locals_dict,
  111. };
  112. const sys_stdio_obj_t mp_sys_stdin_obj = {{&stdio_obj_type}, .fd = STDIO_FD_IN};
  113. const sys_stdio_obj_t mp_sys_stdout_obj = {{&stdio_obj_type}, .fd = STDIO_FD_OUT};
  114. const sys_stdio_obj_t mp_sys_stderr_obj = {{&stdio_obj_type}, .fd = STDIO_FD_ERR};
  115. #if MICROPY_PY_SYS_STDIO_BUFFER
  116. STATIC mp_uint_t stdio_buffer_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
  117. for (uint i = 0; i < size; i++) {
  118. ((byte*)buf)[i] = mp_hal_stdin_rx_chr();
  119. }
  120. return size;
  121. }
  122. STATIC mp_uint_t stdio_buffer_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
  123. mp_hal_stdout_tx_strn(buf, size);
  124. return size;
  125. }
  126. STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = {
  127. .read = stdio_buffer_read,
  128. .write = stdio_buffer_write,
  129. .is_text = false,
  130. };
  131. STATIC const mp_obj_type_t stdio_buffer_obj_type = {
  132. { &mp_type_type },
  133. .name = MP_QSTR_FileIO,
  134. .print = stdio_obj_print,
  135. .getiter = mp_identity_getiter,
  136. .iternext = mp_stream_unbuffered_iter,
  137. .protocol = &stdio_buffer_obj_stream_p,
  138. .locals_dict = (mp_obj_dict_t*)&stdio_locals_dict,
  139. };
  140. STATIC const sys_stdio_obj_t stdio_buffer_obj = {{&stdio_buffer_obj_type}, .fd = 0}; // fd unused
  141. #endif