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 io.serial
6 io.serial.unix.termios io.backend.unix unix unix.ffi
11 { [ os linux? ] [ "io.serial.unix.linux" ] }
12 { [ os bsd? ] [ "io.serial.unix.bsd" ] }
15 FUNCTION: speed_t cfgetispeed ( termios* t ) ;
16 FUNCTION: speed_t cfgetospeed ( termios* t ) ;
17 FUNCTION: int cfsetispeed ( termios* t, speed_t s ) ;
18 FUNCTION: int cfsetospeed ( termios* t, speed_t s ) ;
19 FUNCTION: int tcgetattr ( int i1, termios* t ) ;
20 FUNCTION: int tcsetattr ( int i1, int i2, termios* t ) ;
21 FUNCTION: int tcdrain ( int i1 ) ;
22 FUNCTION: int tcflow ( int i1, int i2 ) ;
23 FUNCTION: int tcflush ( int i1, int i2 ) ;
24 FUNCTION: int tcsendbreak ( int i1, int i2 ) ;
25 FUNCTION: void cfmakeraw ( termios* t ) ;
26 FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ;
28 : fd>duplex-stream ( fd -- duplex-stream )
30 [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
32 : open-rw ( path -- fd ) O_RDWR file-mode open-file ;
34 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
36 : open-unix-serial-port ( serial-port -- )
38 path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
42 : serial-fd ( serial -- fd )
43 stream>> in>> handle>> fd>> ;
45 : set-termios ( serial -- )
48 termios <struct> [ tcgetattr io-error ] keep
51 : configure-termios ( serial -- )
54 [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
55 [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
58 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
61 [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
64 : tciflush ( serial -- )
65 serial-fd TCIFLUSH tcflush io-error ;
67 : apply-termios ( serial -- )
69 [ termios>> ] bi tcsetattr io-error ;
71 M: unix open-serial ( serial -- serial' )
73 [ open-unix-serial-port ]
81 M: unix default-serial-flags
82 flags{ IGNPAR ICRNL } >>iflag
84 flags{ CS8 CLOCAL CREAD } >>cflag
85 flags{ ICANON } >>lflag ;