|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2014 Paul Sokolovsky |
| 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 <sys/types.h> |
| 28 | +#include <termios.h> |
| 29 | +#include <unistd.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +#include "mpconfig.h" |
| 33 | +#include "misc.h" |
| 34 | +#include "nlr.h" |
| 35 | +#include "qstr.h" |
| 36 | +#include "obj.h" |
| 37 | +#include "runtime.h" |
| 38 | +#include "objlist.h" |
| 39 | + |
| 40 | +#define RAISE_ERRNO(err_flag, error_val) \ |
| 41 | + { if (err_flag == -1) \ |
| 42 | + { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_val))); } } |
| 43 | + |
| 44 | +STATIC mp_obj_t mod_termios_tcgetattr(mp_obj_t fd_in) { |
| 45 | + struct termios term; |
| 46 | + int fd = mp_obj_get_int(fd_in); |
| 47 | + |
| 48 | + int res = tcgetattr(fd, &term); |
| 49 | + RAISE_ERRNO(res, errno); |
| 50 | + |
| 51 | + mp_obj_list_t *r = mp_obj_new_list(7, NULL); |
| 52 | + r->items[0] = MP_OBJ_NEW_SMALL_INT(term.c_iflag); |
| 53 | + r->items[1] = MP_OBJ_NEW_SMALL_INT(term.c_oflag); |
| 54 | + r->items[2] = MP_OBJ_NEW_SMALL_INT(term.c_cflag); |
| 55 | + r->items[3] = MP_OBJ_NEW_SMALL_INT(term.c_lflag); |
| 56 | + r->items[4] = MP_OBJ_NEW_SMALL_INT(cfgetispeed(&term)); |
| 57 | + r->items[5] = MP_OBJ_NEW_SMALL_INT(cfgetospeed(&term)); |
| 58 | + |
| 59 | + mp_obj_list_t *cc = mp_obj_new_list(NCCS, NULL); |
| 60 | + r->items[6] = cc; |
| 61 | + for (int i = 0; i < NCCS; i++) { |
| 62 | + if (i == VMIN || i == VTIME) { |
| 63 | + cc->items[i] = MP_OBJ_NEW_SMALL_INT(term.c_cc[i]); |
| 64 | + } else { |
| 65 | + // https://docs.python.org/3/library/termios.html says value is *string*, |
| 66 | + // but now way unicode chars could be there. |
| 67 | + cc->items[i] = mp_obj_new_bytes(&term.c_cc[i], 1); |
| 68 | + } |
| 69 | + } |
| 70 | + return r; |
| 71 | +} |
| 72 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_termios_tcgetattr_obj, mod_termios_tcgetattr); |
| 73 | + |
| 74 | +STATIC mp_obj_t mod_termios_tcsetattr(mp_obj_t fd_in, mp_obj_t when_in, mp_obj_t attrs_in) { |
| 75 | + struct termios term; |
| 76 | + int fd = mp_obj_get_int(fd_in); |
| 77 | + int when = mp_obj_get_int(when_in); |
| 78 | + assert(MP_OBJ_IS_TYPE(attrs_in, &mp_type_list)); |
| 79 | + mp_obj_list_t *attrs = attrs_in; |
| 80 | + |
| 81 | + term.c_iflag = mp_obj_get_int(attrs->items[0]); |
| 82 | + term.c_oflag = mp_obj_get_int(attrs->items[1]); |
| 83 | + term.c_cflag = mp_obj_get_int(attrs->items[2]); |
| 84 | + term.c_lflag = mp_obj_get_int(attrs->items[3]); |
| 85 | + |
| 86 | + mp_obj_list_t *cc = attrs->items[6]; |
| 87 | + for (int i = 0; i < NCCS; i++) { |
| 88 | + if (i == VMIN || i == VTIME) { |
| 89 | + term.c_cc[i] = mp_obj_get_int(cc->items[i]); |
| 90 | + } else { |
| 91 | + uint len; |
| 92 | + term.c_cc[i] = *mp_obj_str_get_data(cc->items[i], &len); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + int res = cfsetispeed(&term, mp_obj_get_int(attrs->items[4])); |
| 97 | + RAISE_ERRNO(res, errno); |
| 98 | + res = cfsetispeed(&term, mp_obj_get_int(attrs->items[5])); |
| 99 | + RAISE_ERRNO(res, errno); |
| 100 | + |
| 101 | + res = tcsetattr(fd, when, &term); |
| 102 | + RAISE_ERRNO(res, errno); |
| 103 | + return mp_const_none; |
| 104 | +} |
| 105 | +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_termios_tcsetattr_obj, mod_termios_tcsetattr); |
| 106 | + |
| 107 | +STATIC mp_obj_t mod_termios_setraw(mp_obj_t fd_in) { |
| 108 | + struct termios term; |
| 109 | + int fd = mp_obj_get_int(fd_in); |
| 110 | + int res = tcgetattr(fd, &term); |
| 111 | + RAISE_ERRNO(res, errno); |
| 112 | + |
| 113 | + term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); |
| 114 | + term.c_oflag = 0; |
| 115 | + term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8; |
| 116 | + term.c_lflag = 0; |
| 117 | + term.c_cc[VMIN] = 1; |
| 118 | + term.c_cc[VTIME] = 0; |
| 119 | + res = tcsetattr(fd, TCSAFLUSH, &term); |
| 120 | + RAISE_ERRNO(res, errno); |
| 121 | + return mp_const_none; |
| 122 | +} |
| 123 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_termios_setraw_obj, mod_termios_setraw); |
| 124 | + |
| 125 | +STATIC const mp_map_elem_t mp_module_termios_globals_table[] = { |
| 126 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_termios) }, |
| 127 | + { MP_OBJ_NEW_QSTR(MP_QSTR_tcgetattr), (mp_obj_t)&mod_termios_tcgetattr_obj }, |
| 128 | + { MP_OBJ_NEW_QSTR(MP_QSTR_tcsetattr), (mp_obj_t)&mod_termios_tcsetattr_obj }, |
| 129 | + { MP_OBJ_NEW_QSTR(MP_QSTR_setraw), (mp_obj_t)&mod_termios_setraw_obj }, |
| 130 | + |
| 131 | +#define C(name) { MP_OBJ_NEW_QSTR(MP_QSTR_ ## name), MP_OBJ_NEW_SMALL_INT(name) } |
| 132 | + C(TCSANOW), |
| 133 | + |
| 134 | + C(B9600), |
| 135 | + C(B57600), |
| 136 | + C(B115200), |
| 137 | +#undef C |
| 138 | +}; |
| 139 | + |
| 140 | +STATIC const mp_obj_dict_t mp_module_termios_globals = { |
| 141 | + .base = {&mp_type_dict}, |
| 142 | + .map = { |
| 143 | + .all_keys_are_qstrs = 1, |
| 144 | + .table_is_fixed_array = 1, |
| 145 | + .used = MP_ARRAY_SIZE(mp_module_termios_globals_table), |
| 146 | + .alloc = MP_ARRAY_SIZE(mp_module_termios_globals_table), |
| 147 | + .table = (mp_map_elem_t*)mp_module_termios_globals_table, |
| 148 | + }, |
| 149 | +}; |
| 150 | + |
| 151 | +const mp_obj_module_t mp_module_termios = { |
| 152 | + .base = { &mp_type_module }, |
| 153 | + .name = MP_QSTR_termios, |
| 154 | + .globals = (mp_obj_dict_t*)&mp_module_termios_globals, |
| 155 | +}; |
0 commit comments