]> gitweb.factorcode.org Git - factor.git/blob - extra/io/serial/unix/unix.factor
remove BSD,solaris,etc. misc platform support code
[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 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 } 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
33 : <file-rw> ( path -- stream ) open-rw fd>duplex-stream ;
34
35 : open-unix-serial-port ( serial-port -- )
36     [
37         path>> flags{ O_RDWR O_NOCTTY O_NDELAY } file-mode open-file
38         fd>duplex-stream
39     ] keep stream<< ;
40
41 : serial-fd ( serial -- fd )
42     stream>> in>> handle>> fd>> ;
43
44 : set-termios ( serial -- )
45     [
46         serial-fd
47         termios <struct> [ tcgetattr io-error ] keep
48     ] keep termios<< ;
49
50 : configure-termios ( serial -- )
51     dup termios>>
52     {
53         [ [ iflag>> ] dip over [ iflag<< ] [ 2drop ] if ]
54         [ [ oflag>> ] dip over [ oflag<< ] [ 2drop ] if ]
55         [
56             [
57                 [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor
58             ] dip cflag<<
59         ]
60         [ [ lflag>> ] dip over [ lflag<< ] [ 2drop ] if ]
61     } 2cleave ;
62
63 : tciflush ( serial -- )
64     serial-fd TCIFLUSH tcflush io-error ;
65
66 : apply-termios ( serial -- )
67     [ serial-fd TCSANOW ]
68     [ termios>> ] bi tcsetattr io-error ;
69
70 M: unix open-serial ( serial -- serial' )
71     {
72         [ open-unix-serial-port ]
73         [ set-termios ]
74         [ configure-termios ]
75         [ tciflush ]
76         [ apply-termios ]
77         [ ]
78     } cleave ;
79
80 M: unix default-serial-flags
81     flags{ IGNPAR ICRNL } >>iflag
82     flags{ } >>oflag
83     flags{ CS8 CLOCAL CREAD } >>cflag
84     flags{ ICANON } >>lflag ;