]> gitweb.factorcode.org Git - factor.git/blob - extra/io/serial/unix/unix.factor
Use flags{ instead of flags all over the place
[factor.git] / extra / io / serial / unix / unix.factor
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 io.serial
6 io.serial.unix.termios io.backend.unix unix unix.ffi
7 literals ;
8 IN: io.serial.unix
9
10 << {
11     { [ os linux? ] [ "io.serial.unix.linux" ] }
12     { [ os bsd? ] [ "io.serial.unix.bsd" ] }
13 } cond require >>
14
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 ) ;
27
28 : fd>duplex-stream ( fd -- duplex-stream )
29     <fd> init-fd
30     [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
31
32 : open-rw ( path -- fd ) O_RDWR file-mode open-file  ;
33 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
34
35 M: unix open-serial ( serial -- serial' )
36     dup
37     path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
38     fd>duplex-stream >>stream ;
39
40 : serial-fd ( serial -- fd )
41     stream>> in>> handle>> fd>> ;
42
43 : get-termios ( serial -- termios )
44     serial-fd
45     termios <struct> [ tcgetattr io-error ] keep ;
46
47 : configure-termios ( serial -- )
48     dup termios>>
49     {
50         [ [ iflag>> ] dip over [ (>>iflag) ] [ 2drop ] if ]
51         [ [ oflag>> ] dip over [ (>>oflag) ] [ 2drop ] if ]
52         [
53             [
54                 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
55             ] dip (>>cflag)
56         ]
57         [ [ lflag>> ] dip over [ (>>lflag) ] [ 2drop ] if ]
58     } 2cleave ;
59
60 : tciflush ( serial -- )
61     serial-fd TCIFLUSH tcflush io-error ;
62
63 : apply-termios ( serial -- )
64     [ serial-fd TCSANOW ]
65     [ termios>> ] bi tcsetattr io-error ;