]> gitweb.factorcode.org Git - factor.git/blob - extra/io/serial/linux/linux.factor
classes.struct: moving to new/boa instead of <struct>/<struct-boa>
[factor.git] / extra / io / serial / linux / linux.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://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 ;
6 IN: io.serial.linux
7
8 : fd>duplex-stream ( fd -- duplex-stream )
9     <fd> init-fd
10     [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
11
12 : open-rw ( path -- fd ) O_RDWR file-mode open-file  ;
13
14 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
15
16 : open-unix-serial-port ( serial-port -- )
17     [
18         path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
19         fd>duplex-stream
20     ] keep stream<< ;
21
22 : serial-fd ( serial -- fd )
23     stream>> in>> handle>> fd>> ;
24
25 : get-fd-termios ( fd -- serial )
26     termios new [ tcgetattr io-error ] keep ;
27
28 : set-termios ( serial -- )
29     [ serial-fd get-fd-termios ] keep termios<< ;
30
31 : configure-termios ( serial -- )
32     dup termios>>
33     {
34         [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
35         [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
36         [
37             [
38                 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
39             ] dip cflag<<
40         ]
41         [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
42     } 2cleave ;
43
44 : tciflush ( serial -- )
45     serial-fd TCIFLUSH tcflush io-error ;
46
47 : apply-termios ( serial -- )
48     [ serial-fd TCSANOW ]
49     [ termios>> ] bi tcsetattr io-error ;
50
51 M: unix open-serial ( serial -- serial' )
52     {
53         [ open-unix-serial-port ]
54         [ set-termios ]
55         [ configure-termios ]
56         [ tciflush ]
57         [ apply-termios ]
58         [ ]
59     } cleave ;
60
61 M: unix default-serial-flags
62     flags{ IGNPAR ICRNL } >>iflag
63     flags{ } >>oflag
64     flags{ CS8 CLOCAL CREAD } >>cflag
65     flags{ ICANON } >>lflag ;