1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.syntax combinators io.ports
4 io.streams.duplex io.unix.backend system kernel math math.bitwise
5 vocabs.loader unix serial serial.unix.termios ;
9 { [ os linux? ] [ "serial.unix.linux" ] }
10 { [ os bsd? ] [ "serial.unix.bsd" ] }
13 FUNCTION: speed_t cfgetispeed ( termios* t ) ;
14 FUNCTION: speed_t cfgetospeed ( termios* t ) ;
15 FUNCTION: int cfsetispeed ( termios* t, speed_t s ) ;
16 FUNCTION: int cfsetospeed ( termios* t, speed_t s ) ;
17 FUNCTION: int tcgetattr ( int i1, termios* t ) ;
18 FUNCTION: int tcsetattr ( int i1, int i2, termios* t ) ;
19 FUNCTION: int tcdrain ( int i1 ) ;
20 FUNCTION: int tcflow ( int i1, int i2 ) ;
21 FUNCTION: int tcflush ( int i1, int i2 ) ;
22 FUNCTION: int tcsendbreak ( int i1, int i2 ) ;
23 FUNCTION: void cfmakeraw ( termios* t ) ;
24 FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
26 : fd>duplex-stream ( fd -- duplex-stream )
28 [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
30 : open-rw ( path -- fd ) O_RDWR file-mode open-file ;
31 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
33 M: unix open-serial ( serial -- serial' )
35 path>> { O_RDWR O_NOCTTY O_NDELAY } flags file-mode open-file
36 fd>duplex-stream >>stream ;
38 : serial-fd ( serial -- fd )
39 stream>> in>> handle>> fd>> ;
41 : get-termios ( serial -- termios )
43 "termios" <c-object> [ tcgetattr io-error ] keep ;
45 : configure-termios ( serial -- )
48 [ [ iflag>> ] dip over [ set-termios-iflag ] [ 2drop ] if ]
49 [ [ oflag>> ] dip over [ set-termios-oflag ] [ 2drop ] if ]
52 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
53 ] dip set-termios-cflag
55 [ [ lflag>> ] dip over [ set-termios-lflag ] [ 2drop ] if ]
58 : tciflush ( serial -- )
59 serial-fd TCIFLUSH tcflush io-error ;
61 : apply-termios ( serial -- )
63 [ termios>> ] bi tcsetattr io-error ;