datacalls decoding patches
[gnokii.git] / common / devices / unixserial.c
1 /*
2   
3   $Id$
4
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Released under the terms of the GNU GPL, see file COPYING for more details.
10
11 */
12
13 #include "misc.h"
14
15 /* Do not compile this file under Win32 systems. */
16
17 #ifndef WIN32
18
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <string.h>
23
24 #if __unices__
25 #  include <sys/file.h>
26 #endif
27
28 #include <termios.h>
29 #include "devices/unixserial.h"
30
31 #ifdef HAVE_SYS_IOCTL_COMPAT_H
32   #include <sys/ioctl_compat.h>
33 #endif
34
35 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #endif
38
39 /* If the target operating system does not have cfsetspeed, we can emulate
40    it. */
41
42 #ifndef HAVE_CFSETSPEED
43   #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
44      #define cfsetspeed(t, speed) \
45      (cfsetispeed(t, speed) || cfsetospeed(t, speed))
46   #else
47     static int cfsetspeed(struct termios *t, int speed) {
48     #ifdef HAVE_TERMIOS_CSPEED
49       t->c_ispeed = speed;
50       t->c_ospeed = speed;
51     #else
52       t->c_cflag |= speed;
53     #endif
54       return 0;
55     }
56   #endif
57 #endif
58
59 #ifndef O_NONBLOCK
60   #define O_NONBLOCK  0
61 #endif
62
63 /* Structure to backup the setting of the terminal. */
64
65 struct termios serial_termios;
66
67 /* Open the serial port and store the settings. */
68
69 int serial_open(__const char *__file, int __oflag) {
70
71   int __fd;
72   int retcode;
73
74   __fd = open(__file, __oflag);
75   if (__fd == -1) {
76     perror("Gnokii serial_open: open");
77     return (-1);
78   }
79
80   retcode=tcgetattr(__fd, &serial_termios);
81   if(retcode==-1) {
82     perror("Gnokii serial_open:tcgetattr");
83     /* Don't call serial_close since serial_termios is not valid */
84 #if 0
85     close(__fd);
86     return(-1);
87 #endif
88   }
89   
90   return __fd;
91 }
92
93 /* Close the serial port and restore old settings. */
94
95 int serial_close(int __fd) {
96
97   if (__fd >= 0)
98     tcsetattr(__fd, TCSANOW, &serial_termios);
99
100   return (close(__fd));
101 }
102
103 /* Open a device with standard options. */
104
105 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
106
107   int fd;
108   int retcode;
109   struct termios tp;
110
111   /* Open device */
112
113   fd = serial_open(__file, O_RDONLY | O_NOCTTY | O_NONBLOCK);
114
115   if (fd < 0) 
116     return fd;
117
118   /* Allow process/thread to receive SIGIO */
119
120 #if !(__unices__)
121   retcode = fcntl(fd, F_SETOWN, getpid());
122   if (retcode == -1){
123     perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
124 #if 0
125     serial_close(fd);
126     return(-1);
127 #endif
128   }
129 #endif
130
131   /* Make filedescriptor asynchronous. */
132
133   if (__with_async) {
134     retcode=fcntl(fd, F_SETFL, FASYNC);
135     if (retcode == -1){
136       perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
137 #if 0
138       serial_close(fd);
139       return(-1);
140 #endif
141     }
142   }
143   
144   /* Initialise the port settings */
145
146   memcpy(&tp, &serial_termios, sizeof(struct termios));
147
148   /* Set port settings for canonical input processing */
149
150   tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
151   if (__with_odd_parity) {
152     tp.c_cflag |= (PARENB | PARODD);
153     tp.c_iflag = 0;
154   }
155   else
156     tp.c_iflag = IGNPAR;
157   if (__with_hw_handshake)
158     tp.c_cflag |= CRTSCTS;
159   else
160     tp.c_cflag &= ~CRTSCTS;
161
162   tp.c_oflag = 0;
163   tp.c_lflag = 0;
164   tp.c_cc[VMIN] = 1;
165   tp.c_cc[VTIME] = 0;
166
167   retcode=tcflush(fd, TCIFLUSH);
168   if (retcode == -1) {
169     perror("Gnokii serial_opendevice: tcflush");
170 #if 0
171     serial_close(fd);
172     return(-1);
173 #endif
174   }
175
176   retcode=tcsetattr(fd, TCSANOW, &tp);
177   if (retcode == -1){
178     perror("Gnokii serial_opendevice: tcsetattr");
179 #if 0
180     serial_close(fd);
181     return(-1);
182 #endif
183   }
184
185   return fd;
186 }
187
188 /* Set the DTR and RTS bit of the serial device. */
189
190 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
191
192   unsigned int flags;
193
194   flags = TIOCM_DTR;
195
196   if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
197         else ioctl(__fd, TIOCMBIC, &flags);
198
199   flags = TIOCM_RTS;
200
201   if (__rts) ioctl(__fd, TIOCMBIS, &flags);
202         else ioctl(__fd, TIOCMBIC, &flags);
203 }
204
205
206 int serial_select(int fd, struct timeval *timeout) {
207
208   fd_set readfds;
209
210   FD_ZERO(&readfds);
211   FD_SET(fd, &readfds);
212
213   return (select(fd + 1, &readfds, NULL, NULL, timeout));
214
215 }
216
217
218 /* Change the speed of the serial device. */
219
220 void serial_changespeed(int __fd, int __speed) {
221
222 #ifndef SGTTY
223   struct termios t;
224 #else
225   struct sgttyb t;
226 #endif
227
228   int speed=B9600;
229
230   switch (__speed) {
231     case 9600:   speed = B9600;   break;
232     case 19200:  speed = B19200;  break;
233     case 38400:  speed = B38400;  break;
234     case 57600:  speed = B57600;  break;
235     case 115200: speed = B115200; break;
236   }
237
238 #ifndef SGTTY
239   tcgetattr(__fd, &t);
240
241   // This is not needed! We set up the speed via cfsetspeed
242   //  t.c_cflag &= ~CBAUD;
243   //  t.c_cflag |= speed;
244 #ifdef DEBUG
245   if (cfsetspeed(&t, speed) == -1)
246         fprintf(stdout,_("Serial port speed setting failed\n"));
247 #else
248   cfsetspeed(&t, speed);
249 #endif
250
251   tcsetattr(__fd, TCSADRAIN, &t);
252 #else
253   ioctl(__fd, TIOCGETP, &t);
254
255   t.sg_ispeed = speed;
256   t.sg_ospeed = speed;
257
258   ioctl(__fd, TIOCSETN, &t);
259 #endif
260 }
261
262 /* Read from serial device. */
263
264 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
265
266   return (read(__fd, __buf, __nbytes));
267 }
268
269 /* Write to serial device. */
270
271 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
272         
273 #if 0
274         return (write(__fd, __buf, __n));
275 #endif
276         return __n;
277 }
278
279 #endif  /* WIN32 */