]> gitweb.factorcode.org Git - factor.git/blob - extra/io/serial/unix/unix.factor
move some allocation words that don't really have much to do with c types out of...
[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 combinators io.ports io.streams.duplex system kernel
5 math math.bitwise vocabs.loader unix io.serial
6 io.serial.unix.termios io.backend.unix ;
7 IN: io.serial.unix
8
9 << {
10     { [ os linux? ] [ "io.serial.unix.linux" ] }
11     { [ os bsd? ] [ "io.serial.unix.bsd" ] }
12 } cond require >>
13
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 ) ;
26
27 : fd>duplex-stream ( fd -- duplex-stream )
28     <fd> init-fd
29     [ <input-port> ] [ <output-port> ] bi <duplex-stream> ;
30
31 : open-rw ( path -- fd ) O_RDWR file-mode open-file  ;
32 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
33
34 M: unix open-serial ( serial -- serial' )
35     dup
36     path>> { O_RDWR O_NOCTTY O_NDELAY } flags file-mode open-file
37     fd>duplex-stream >>stream ;
38
39 : serial-fd ( serial -- fd )
40     stream>> in>> handle>> fd>> ;
41
42 : get-termios ( serial -- termios )
43     serial-fd
44     "termios" <c-object> [ tcgetattr io-error ] keep ;
45
46 : configure-termios ( serial -- )
47     dup termios>>
48     {
49         [ [ iflag>> ] dip over [ set-termios-iflag ] [ 2drop ] if ]
50         [ [ oflag>> ] dip over [ set-termios-oflag ] [ 2drop ] if ]
51         [
52             [
53                 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
54             ] dip set-termios-cflag
55         ]
56         [ [ lflag>> ] dip over [ set-termios-lflag ] [ 2drop ] if ]
57     } 2cleave ;
58
59 : tciflush ( serial -- )
60     serial-fd TCIFLUSH tcflush io-error ;
61
62 : apply-termios ( serial -- )
63     [ serial-fd TCSANOW ]
64     [ termios>> ] bi tcsetattr io-error ;