Skip to content

Commit 4f9ebad

Browse files
committed
modtermios: Add "termios" unix module, subset of CPython's.
Also provides setraw() function from "tty" module (which in CPython is implemented in Python). The idea here is that 95% of "termios" module usage is to set raw mode to allow access to normal serial devices. Then, instead of exporting gazillion termios symbols, it's better to implement it in C, and export minimal number of symbols (mostly baud rates and drain values).
1 parent 72b115c commit 4f9ebad

5 files changed

Lines changed: 175 additions & 0 deletions

File tree

unix/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ ifeq ($(MICROPY_PY_TIME),1)
5151
CFLAGS_MOD += -DMICROPY_PY_TIME=1
5252
SRC_MOD += modtime.c
5353
endif
54+
ifeq ($(MICROPY_PY_TERMIOS),1)
55+
CFLAGS_MOD += -DMICROPY_PY_TERMIOS=1
56+
SRC_MOD += modtermios.c
57+
endif
5458
ifeq ($(MICROPY_PY_FFI),1)
5559
LIBFFI_LDFLAGS_MOD := $(shell pkg-config --libs libffi)
5660
LIBFFI_CFLAGS_MOD := $(shell pkg-config --cflags libffi)

unix/modtermios.c

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
};

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070

7171
extern const struct _mp_obj_module_t mp_module_os;
7272
extern const struct _mp_obj_module_t mp_module_time;
73+
extern const struct _mp_obj_module_t mp_module_termios;
7374
extern const struct _mp_obj_module_t mp_module_socket;
7475
extern const struct _mp_obj_module_t mp_module_ffi;
7576

@@ -89,6 +90,7 @@ extern const struct _mp_obj_module_t mp_module_ffi;
8990
MICROPY_PY_TIME_DEF \
9091
{ MP_OBJ_NEW_QSTR(MP_QSTR_microsocket), (mp_obj_t)&mp_module_socket }, \
9192
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \
93+
{ MP_OBJ_NEW_QSTR(MP_QSTR_termios), (mp_obj_t)&mp_module_termios }, \
9294

9395
// type definitions for the specific machine
9496

unix/mpconfigport.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ MICROPY_USE_READLINE = 1
99
# Subset of CPython time module
1010
MICROPY_PY_TIME = 1
1111

12+
# Subset of CPython termios module
13+
MICROPY_PY_TERMIOS = 1
14+
1215
# ffi module requires libffi (libffi-dev Debian package)
1316
MICROPY_PY_FFI = 1

unix/qstrdefsport.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ Q(SO_ERROR)
8383
Q(SO_KEEPALIVE)
8484
Q(SO_LINGER)
8585
Q(SO_REUSEADDR)
86+
87+
#if MICROPY_PY_TERMIOS
88+
Q(termios)
89+
Q(tcgetattr)
90+
Q(tcsetattr)
91+
Q(setraw)
92+
Q(TCSANOW)
93+
Q(B9600)
94+
Q(B57600)
95+
Q(B115200)
96+
#endif

0 commit comments

Comments
 (0)