1 ! Copyright (C) 2008 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.struct combinators io.backend.unix
4 io.ports io.serial io.streams.duplex kernel libc literals math
5 system unix unix.ffi io.serial.linux.ffi ;
8 : fd>duplex-stream ( fd -- duplex-stream )
10 [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
12 : open-rw ( path -- fd ) O_RDWR file-mode open-file ;
14 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
16 : open-unix-serial-port ( serial-port -- )
18 path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
22 : serial-fd ( serial -- fd )
23 stream>> in>> handle>> fd>> ;
25 : get-fd-termios ( fd -- serial )
26 termios new [ tcgetattr io-error ] keep ;
28 : set-termios ( serial -- )
29 [ serial-fd get-fd-termios ] keep termios<< ;
31 : configure-termios ( serial -- )
34 [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
35 [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
38 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
41 [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
44 : tciflush ( serial -- )
45 serial-fd TCIFLUSH tcflush io-error ;
47 : apply-termios ( serial -- )
49 [ termios>> ] bi tcsetattr io-error ;
51 M: unix open-serial ( serial -- serial' )
53 [ open-unix-serial-port ]
61 M: unix default-serial-flags
62 flags{ IGNPAR ICRNL } >>iflag
64 flags{ CS8 CLOCAL CREAD } >>cflag
65 flags{ ICANON } >>lflag ;