This commit was manufactured by cvs2svn to create branch 'decode'.
[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     close(__fd);
85     return(-1);
86   }
87   
88   return __fd;
89 }
90
91 /* Close the serial port and restore old settings. */
92
93 int serial_close(int __fd) {
94
95   if (__fd >= 0)
96     tcsetattr(__fd, TCSANOW, &serial_termios);
97
98   return (close(__fd));
99 }
100
101 /* Open a device with standard options. */
102
103 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
104
105   int fd;
106   int retcode;
107   struct termios tp;
108
109   /* Open device */
110
111   fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
112
113   if (fd < 0) 
114     return fd;
115
116   /* Allow process/thread to receive SIGIO */
117
118 #if !(__unices__)
119   retcode = fcntl(fd, F_SETOWN, getpid());
120   if (retcode == -1){
121     perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
122     serial_close(fd);
123     return(-1);
124   }
125 #endif
126
127   /* Make filedescriptor asynchronous. */
128
129   if (__with_async) {
130     retcode=fcntl(fd, F_SETFL, FASYNC);
131     if (retcode == -1){
132       perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
133       serial_close(fd);
134       return(-1);
135     }
136   }
137   
138   /* Initialise the port settings */
139
140   memcpy(&tp, &serial_termios, sizeof(struct termios));
141
142   /* Set port settings for canonical input processing */
143
144   tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
145   if (__with_odd_parity) {
146     tp.c_cflag |= (PARENB | PARODD);
147     tp.c_iflag = 0;
148   }
149   else
150     tp.c_iflag = IGNPAR;
151   if (__with_hw_handshake)
152     tp.c_cflag |= CRTSCTS;
153   else
154     tp.c_cflag &= ~CRTSCTS;
155
156   tp.c_oflag = 0;
157   tp.c_lflag = 0;
158   tp.c_cc[VMIN] = 1;
159   tp.c_cc[VTIME] = 0;
160
161   retcode=tcflush(fd, TCIFLUSH);
162   if (retcode == -1) {
163     perror("Gnokii serial_opendevice: tcflush");
164     serial_close(fd);
165     return(-1);
166   }
167
168   retcode=tcsetattr(fd, TCSANOW, &tp);
169   if (retcode == -1){
170     perror("Gnokii serial_opendevice: tcsetattr");
171     serial_close(fd);
172     return(-1);
173   }
174
175   return fd;
176 }
177
178 /* Set the DTR and RTS bit of the serial device. */
179
180 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
181
182   unsigned int flags;
183
184   flags = TIOCM_DTR;
185
186   if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
187         else ioctl(__fd, TIOCMBIC, &flags);
188
189   flags = TIOCM_RTS;
190
191   if (__rts) ioctl(__fd, TIOCMBIS, &flags);
192         else ioctl(__fd, TIOCMBIC, &flags);
193 }
194
195
196 int serial_select(int fd, struct timeval *timeout) {
197
198   fd_set readfds;
199
200   FD_ZERO(&readfds);
201   FD_SET(fd, &readfds);
202
203   return (select(fd + 1, &readfds, NULL, NULL, timeout));
204
205 }
206
207
208 /* Change the speed of the serial device. */
209
210 void serial_changespeed(int __fd, int __speed) {
211
212 #ifndef SGTTY
213   struct termios t;
214 #else
215   struct sgttyb t;
216 #endif
217
218   int speed=B9600;
219
220   switch (__speed) {
221     case 9600:   speed = B9600;   break;
222     case 19200:  speed = B19200;  break;
223     case 38400:  speed = B38400;  break;
224     case 57600:  speed = B57600;  break;
225     case 115200: speed = B115200; break;
226   }
227
228 #ifndef SGTTY
229   tcgetattr(__fd, &t);
230
231   // This is not needed! We set up the speed via cfsetspeed
232   //  t.c_cflag &= ~CBAUD;
233   //  t.c_cflag |= speed;
234 #ifdef DEBUG
235   if (cfsetspeed(&t, speed) == -1)
236         fprintf(stdout,_("Serial port speed setting failed\n"));
237 #else
238   cfsetspeed(&t, speed);
239 #endif
240
241   tcsetattr(__fd, TCSADRAIN, &t);
242 #else
243   ioctl(__fd, TIOCGETP, &t);
244
245   t.sg_ispeed = speed;
246   t.sg_ospeed = speed;
247
248   ioctl(__fd, TIOCSETN, &t);
249 #endif
250 }
251
252 /* Read from serial device. */
253
254 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
255
256   return (read(__fd, __buf, __nbytes));
257 }
258
259 /* Write to serial device. */
260
261 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
262         
263         return (write(__fd, __buf, __n));
264 }
265
266 #endif  /* WIN32 */