uos_dupterm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 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. #include <string.h>
  28. #include "py/mpconfig.h"
  29. #include "py/runtime.h"
  30. #include "py/objtuple.h"
  31. #include "py/objarray.h"
  32. #include "py/stream.h"
  33. #include "lib/utils/interrupt_char.h"
  34. #if MICROPY_PY_OS_DUPTERM
  35. void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) {
  36. mp_obj_t term = MP_STATE_VM(dupterm_objs[dupterm_idx]);
  37. MP_STATE_VM(dupterm_objs[dupterm_idx]) = MP_OBJ_NULL;
  38. mp_printf(&mp_plat_print, msg);
  39. if (exc != MP_OBJ_NULL) {
  40. mp_obj_print_exception(&mp_plat_print, exc);
  41. }
  42. nlr_buf_t nlr;
  43. if (nlr_push(&nlr) == 0) {
  44. mp_stream_close(term);
  45. nlr_pop();
  46. } else {
  47. // Ignore any errors during stream closing
  48. }
  49. }
  50. int mp_uos_dupterm_rx_chr(void) {
  51. for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
  52. if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
  53. continue;
  54. }
  55. nlr_buf_t nlr;
  56. if (nlr_push(&nlr) == 0) {
  57. byte buf[1];
  58. int errcode;
  59. const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_VM(dupterm_objs[idx]));
  60. mp_uint_t out_sz = stream_p->read(MP_STATE_VM(dupterm_objs[idx]), buf, 1, &errcode);
  61. if (out_sz == 0) {
  62. nlr_pop();
  63. mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL);
  64. } else if (out_sz == MP_STREAM_ERROR) {
  65. // errcode is valid
  66. if (mp_is_nonblocking_error(errcode)) {
  67. nlr_pop();
  68. } else {
  69. mp_raise_OSError(errcode);
  70. }
  71. } else {
  72. // read 1 byte
  73. nlr_pop();
  74. if (buf[0] == mp_interrupt_char) {
  75. // Signal keyboard interrupt to be raised as soon as the VM resumes
  76. mp_keyboard_interrupt();
  77. return -2;
  78. }
  79. return buf[0];
  80. }
  81. } else {
  82. mp_uos_deactivate(idx, "dupterm: Exception in read() method, deactivating: ", MP_OBJ_FROM_PTR(nlr.ret_val));
  83. }
  84. }
  85. // No chars available
  86. return -1;
  87. }
  88. void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
  89. for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
  90. if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
  91. continue;
  92. }
  93. nlr_buf_t nlr;
  94. if (nlr_push(&nlr) == 0) {
  95. mp_stream_write(MP_STATE_VM(dupterm_objs[idx]), str, len, MP_STREAM_RW_WRITE);
  96. nlr_pop();
  97. } else {
  98. mp_uos_deactivate(idx, "dupterm: Exception in write() method, deactivating: ", MP_OBJ_FROM_PTR(nlr.ret_val));
  99. }
  100. }
  101. }
  102. STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) {
  103. mp_int_t idx = 0;
  104. if (n_args == 2) {
  105. idx = mp_obj_get_int(args[1]);
  106. }
  107. if (idx < 0 || idx >= MICROPY_PY_OS_DUPTERM) {
  108. mp_raise_ValueError("invalid dupterm index");
  109. }
  110. mp_obj_t previous_obj = MP_STATE_VM(dupterm_objs[idx]);
  111. if (previous_obj == MP_OBJ_NULL) {
  112. previous_obj = mp_const_none;
  113. }
  114. if (args[0] == mp_const_none) {
  115. MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL;
  116. } else {
  117. mp_get_stream_raise(args[0], MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
  118. MP_STATE_VM(dupterm_objs[idx]) = args[0];
  119. }
  120. return previous_obj;
  121. }
  122. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 1, 2, mp_uos_dupterm);
  123. #endif