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" ] }
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 ;
33 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
35 : open-unix-serial-port ( serial-port -- )
37 path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
41 : serial-fd ( serial -- fd )
42 stream>> in>> handle>> fd>> ;
44 : set-termios ( serial -- )
47 termios <struct> [ tcgetattr io-error ] keep
50 : configure-termios ( serial -- )
53 [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
54 [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
57 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
60 [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
63 : tciflush ( serial -- )
64 serial-fd TCIFLUSH tcflush io-error ;
66 : apply-termios ( serial -- )
68 [ termios>> ] bi tcsetattr io-error ;
70 M: unix open-serial ( serial -- serial' )
72 [ open-unix-serial-port ]
80 M: unix default-serial-flags
81 flags{ IGNPAR ICRNL } >>iflag
83 flags{ CS8 CLOCAL CREAD } >>cflag
84 flags{ ICANON } >>lflag ;