unix_mphal.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 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 <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/time.h>
  30. #include "py/mphal.h"
  31. #include "py/runtime.h"
  32. #include "extmod/misc.h"
  33. #ifndef _WIN32
  34. #include <signal.h>
  35. STATIC void sighandler(int signum) {
  36. if (signum == SIGINT) {
  37. #if MICROPY_ASYNC_KBD_INTR
  38. mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
  39. sigset_t mask;
  40. sigemptyset(&mask);
  41. // On entry to handler, its signal is blocked, and unblocked on
  42. // normal exit. As we instead perform longjmp, unblock it manually.
  43. sigprocmask(SIG_SETMASK, &mask, NULL);
  44. nlr_raise(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
  45. #else
  46. if (MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
  47. // this is the second time we are called, so die straight away
  48. exit(1);
  49. }
  50. mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
  51. MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception));
  52. #endif
  53. }
  54. }
  55. #endif
  56. void mp_hal_set_interrupt_char(char c) {
  57. // configure terminal settings to (not) let ctrl-C through
  58. if (c == CHAR_CTRL_C) {
  59. #ifndef _WIN32
  60. // enable signal handler
  61. struct sigaction sa;
  62. sa.sa_flags = 0;
  63. sa.sa_handler = sighandler;
  64. sigemptyset(&sa.sa_mask);
  65. sigaction(SIGINT, &sa, NULL);
  66. #endif
  67. } else {
  68. #ifndef _WIN32
  69. // disable signal handler
  70. struct sigaction sa;
  71. sa.sa_flags = 0;
  72. sa.sa_handler = SIG_DFL;
  73. sigemptyset(&sa.sa_mask);
  74. sigaction(SIGINT, &sa, NULL);
  75. #endif
  76. }
  77. }
  78. #if MICROPY_USE_READLINE == 1
  79. #include <termios.h>
  80. static struct termios orig_termios;
  81. void mp_hal_stdio_mode_raw(void) {
  82. // save and set terminal settings
  83. tcgetattr(0, &orig_termios);
  84. static struct termios termios;
  85. termios = orig_termios;
  86. termios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  87. termios.c_cflag = (termios.c_cflag & ~(CSIZE | PARENB)) | CS8;
  88. termios.c_lflag = 0;
  89. termios.c_cc[VMIN] = 1;
  90. termios.c_cc[VTIME] = 0;
  91. tcsetattr(0, TCSAFLUSH, &termios);
  92. }
  93. void mp_hal_stdio_mode_orig(void) {
  94. // restore terminal settings
  95. tcsetattr(0, TCSAFLUSH, &orig_termios);
  96. }
  97. #endif
  98. #if MICROPY_PY_OS_DUPTERM
  99. static int call_dupterm_read(size_t idx) {
  100. nlr_buf_t nlr;
  101. if (nlr_push(&nlr) == 0) {
  102. mp_obj_t read_m[3];
  103. mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_read, read_m);
  104. read_m[2] = MP_OBJ_NEW_SMALL_INT(1);
  105. mp_obj_t res = mp_call_method_n_kw(1, 0, read_m);
  106. if (res == mp_const_none) {
  107. return -2;
  108. }
  109. mp_buffer_info_t bufinfo;
  110. mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ);
  111. if (bufinfo.len == 0) {
  112. mp_printf(&mp_plat_print, "dupterm: EOF received, deactivating\n");
  113. MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL;
  114. return -1;
  115. }
  116. nlr_pop();
  117. return *(byte*)bufinfo.buf;
  118. } else {
  119. // Temporarily disable dupterm to avoid infinite recursion
  120. mp_obj_t save_term = MP_STATE_VM(dupterm_objs[idx]);
  121. MP_STATE_VM(dupterm_objs[idx]) = NULL;
  122. mp_printf(&mp_plat_print, "dupterm: ");
  123. mp_obj_print_exception(&mp_plat_print, nlr.ret_val);
  124. MP_STATE_VM(dupterm_objs[idx]) = save_term;
  125. }
  126. return -1;
  127. }
  128. #endif
  129. int mp_hal_stdin_rx_chr(void) {
  130. unsigned char c;
  131. #if MICROPY_PY_OS_DUPTERM
  132. // TODO only support dupterm one slot at the moment
  133. if (MP_STATE_VM(dupterm_objs[0]) != MP_OBJ_NULL) {
  134. int c;
  135. do {
  136. c = call_dupterm_read(0);
  137. } while (c == -2);
  138. if (c == -1) {
  139. goto main_term;
  140. }
  141. if (c == '\n') {
  142. c = '\r';
  143. }
  144. return c;
  145. } else {
  146. main_term:;
  147. #endif
  148. int ret = read(0, &c, 1);
  149. if (ret == 0) {
  150. c = 4; // EOF, ctrl-D
  151. } else if (c == '\n') {
  152. c = '\r';
  153. }
  154. return c;
  155. #if MICROPY_PY_OS_DUPTERM
  156. }
  157. #endif
  158. }
  159. void mp_hal_stdout_tx_strn(const char *str, size_t len) {
  160. int ret = write(1, str, len);
  161. mp_uos_dupterm_tx_strn(str, len);
  162. (void)ret; // to suppress compiler warning
  163. }
  164. // cooked is same as uncooked because the terminal does some postprocessing
  165. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
  166. mp_hal_stdout_tx_strn(str, len);
  167. }
  168. void mp_hal_stdout_tx_str(const char *str) {
  169. mp_hal_stdout_tx_strn(str, strlen(str));
  170. }
  171. mp_uint_t mp_hal_ticks_ms(void) {
  172. struct timeval tv;
  173. gettimeofday(&tv, NULL);
  174. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  175. }
  176. mp_uint_t mp_hal_ticks_us(void) {
  177. struct timeval tv;
  178. gettimeofday(&tv, NULL);
  179. return tv.tv_sec * 1000000 + tv.tv_usec;
  180. }