1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.syntax alien.data
4 classes.struct combinators io.ports io.streams.duplex
5 system kernel math math.bitwise vocabs.loader unix io.serial
6 io.serial.unix.termios io.backend.unix ;
10 { [ os linux? ] [ "io.serial.unix.linux" ] }
11 { [ os bsd? ] [ "io.serial.unix.bsd" ] }
14 FUNCTION: speed_t cfgetispeed ( termios* t ) ;
15 FUNCTION: speed_t cfgetospeed ( termios* t ) ;
16 FUNCTION: int cfsetispeed ( termios* t, speed_t s ) ;
17 FUNCTION: int cfsetospeed ( termios* t, speed_t s ) ;
18 FUNCTION: int tcgetattr ( int i1, termios* t ) ;
19 FUNCTION: int tcsetattr ( int i1, int i2, termios* t ) ;
20 FUNCTION: int tcdrain ( int i1 ) ;
21 FUNCTION: int tcflow ( int i1, int i2 ) ;
22 FUNCTION: int tcflush ( int i1, int i2 ) ;
23 FUNCTION: int tcsendbreak ( int i1, int i2 ) ;
24 FUNCTION: void cfmakeraw ( termios* t ) ;
25 FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
27 : fd>duplex-stream ( fd -- duplex-stream )
29 [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
31 : open-rw ( path -- fd ) O_RDWR file-mode open-file ;
32 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
34 M: unix open-serial ( serial -- serial' )
36 path>> { O_RDWR O_NOCTTY O_NDELAY } flags file-mode open-file
37 fd>duplex-stream >>stream ;
39 : serial-fd ( serial -- fd )
40 stream>> in>> handle>> fd>> ;
42 : get-termios ( serial -- termios )
44 termios <struct> [ tcgetattr io-error ] keep ;
46 : configure-termios ( serial -- )
49 [ [ iflag>> ] dip over [ (>>iflag) ] [ 2drop ] if ]
50 [ [ oflag>> ] dip over [ (>>oflag) ] [ 2drop ] if ]
53 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
56 [ [ lflag>> ] dip over [ (>>lflag) ] [ 2drop ] if ]
59 : tciflush ( serial -- )
60 serial-fd TCIFLUSH tcflush io-error ;
62 : apply-termios ( serial -- )
64 [ termios>> ] bi tcsetattr io-error ;