Put hex finctionality into seperate file
[jelmer/at89prog.git] / pins-serial.c
1 /* 
2         at89prog
3         (c) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <stdio.h>
21 #include "pins.h"
22 #include <sys/termios.h>
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <string.h>
26 #include <sys/io.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/time.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/ioctl.h>
34
35 static int fd = 0;
36
37 #define SER_PIN_TXD 0
38 #define SER_PIN_DTR 1
39 #define SER_PIN_DSR 2
40 #define SER_PIN_RTS 3
41 #define SER_PIN_CTS 4 
42
43 static char *available_pins[] = { "TXD", "DTR", "DSR", "RTS", "CTS", NULL };
44
45 static int ser_init(char *location) 
46 {
47         fd = open(location?location:"/dev/ttyS0", O_RDWR | O_NOCTTY);
48         if(fd < 0) {
49                 perror("Error opening serial port");
50                 return -1;
51         }
52
53         return 0;
54 }
55
56 static int ser_close()
57 {
58         close(fd);
59         return 0;
60 }
61
62 static void ser_set(int p)
63 {
64         int status;
65
66         if(ioctl (fd, TIOCMGET, &status) < 0) {
67                 perror("ioctl");
68                 return;
69         }
70         
71         switch(p) {
72         case SER_PIN_TXD: 
73                 if(ioctl (fd, TIOCSBRK, 0) < 0) perror("ioctl");
74                 break;
75         case SER_PIN_DTR: status |= TIOCM_DTR; break;
76         case SER_PIN_DSR: status |= TIOCM_LE; break;
77         case SER_PIN_RTS: status |= TIOCM_RTS; break;
78         case SER_PIN_CTS: status |= TIOCM_CTS; break;
79         }
80         if(ioctl (fd, TIOCMSET, &status) < 0) perror("ioctl");
81 }
82
83 static void ser_clear(int p)
84 {
85         int status;
86
87         if(ioctl (fd, TIOCMGET, &status) < 0) {
88                 perror("ioctl");
89                 return;
90         }
91         
92         switch(p) {
93         case SER_PIN_TXD: 
94                 if(ioctl (fd, TIOCCBRK, 0) < 0) perror("ioctl"); 
95                   break;
96         case SER_PIN_DTR: status &=~TIOCM_DTR; break;
97         case SER_PIN_DSR: status &=~TIOCM_LE; break;
98         case SER_PIN_RTS: status &=~TIOCM_RTS; break;
99         case SER_PIN_CTS: status &=~TIOCM_CTS; break;
100         default:
101                 fprintf(stderr, "Write not available for pin %s\n", available_pins[p]);
102                 break;
103         }
104         
105         if(ioctl (fd, TIOCMSET, &status) < 0) perror("ioctl");
106 }
107
108 static int ser_get(int p)
109 {
110         int status;
111         if(ioctl(fd, TIOCMGET, &status) < 0) perror("ioctl");
112         switch(p) {
113         case SER_PIN_DTR: return (status & TIOCM_DTR?1:0); 
114         case SER_PIN_RTS: return (status & TIOCM_RTS?1:0);
115         case SER_PIN_DSR: return (status & TIOCM_DSR?1:0);
116         case SER_PIN_CTS: return (status & TIOCM_CTS?0:1);
117         default:
118                 fprintf(stderr, "Read not available for pin %s\n", available_pins[p]);
119                 return -1;
120         }
121 }
122
123 struct pins_backend serial = {
124         "serial",
125         available_pins,
126         ser_init,
127         ser_set,
128         ser_clear,
129         ser_get,
130         ser_close
131 };