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