b2e4f6e057f8be35059afaab0ad68cfa9d755b73
[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 #ifndef UCLINUX
120   /* Allow process/thread to receive SIGIO */
121
122 #if !(__unices__)
123   retcode = fcntl(fd, F_SETOWN, getpid());
124   if (retcode == -1){
125     perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
126     serial_close(fd);
127     return(-1);
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       serial_close(fd);
138       return(-1);
139     }
140   }
141 #endif /* UCLINUX */
142   
143   /* Initialise the port settings */
144
145   memcpy(&tp, &serial_termios, sizeof(struct termios));
146
147   /* Set port settings for canonical input processing */
148
149   tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
150   if (__with_odd_parity) {
151     tp.c_cflag |= (PARENB | PARODD);
152     tp.c_iflag = 0;
153   }
154   else
155     tp.c_iflag = IGNPAR;
156   if (__with_hw_handshake)
157     tp.c_cflag |= CRTSCTS;
158   else
159     tp.c_cflag &= ~CRTSCTS;
160
161   tp.c_oflag = 0;
162   tp.c_lflag = 0;
163   tp.c_cc[VMIN] = 1;
164   tp.c_cc[VTIME] = 0;
165
166   retcode=tcflush(fd, TCIFLUSH);
167   if (retcode == -1) {
168     perror("Gnokii serial_opendevice: tcflush");
169     serial_close(fd);
170     return(-1);
171   }
172
173   retcode=tcsetattr(fd, TCSANOW, &tp);
174   if (retcode == -1){
175     perror("Gnokii serial_opendevice: tcsetattr");
176     serial_close(fd);
177     return(-1);
178   }
179
180   return fd;
181 }
182
183 /* Set the DTR and RTS bit of the serial device. */
184
185 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
186
187   unsigned int flags;
188
189   flags = TIOCM_DTR;
190
191   if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
192         else ioctl(__fd, TIOCMBIC, &flags);
193
194   flags = TIOCM_RTS;
195
196   if (__rts) ioctl(__fd, TIOCMBIS, &flags);
197         else ioctl(__fd, TIOCMBIC, &flags);
198 }
199
200
201 #ifndef UCLINUX
202
203 int serial_select(int fd, struct timeval *timeout) {
204
205   fd_set readfds;
206
207   FD_ZERO(&readfds);
208   FD_SET(fd, &readfds);
209
210   return (select(fd + 1, &readfds, NULL, NULL, timeout));
211
212 }
213
214 #endif /* UCLINUX */
215
216
217 /* Change the speed of the serial device. */
218
219 void serial_changespeed(int __fd, int __speed) {
220
221 #ifndef SGTTY
222   struct termios t;
223 #else
224   struct sgttyb t;
225 #endif
226
227   int speed=B9600;
228
229   switch (__speed) {
230     case 9600:   speed = B9600;   break;
231     case 19200:  speed = B19200;  break;
232     case 38400:  speed = B38400;  break;
233     case 57600:  speed = B57600;  break;
234     case 115200: speed = B115200; break;
235   }
236
237 #ifndef SGTTY
238 #ifdef DEBUG
239   fprintf(stdout,_("Serial port speed setting: tcgetattr()...\n"));
240 #endif /* DEBUG */
241   tcgetattr(__fd, &t);
242
243   // This is not needed! We set up the speed via cfsetspeed
244   //  t.c_cflag &= ~CBAUD;
245   //  t.c_cflag |= speed;
246 #ifdef DEBUG
247   fprintf(stdout,_("Serial port speed setting: cfsetspeed()...\n"));
248 #endif /* DEBUG */
249 #ifdef DEBUG
250   if (cfsetspeed(&t, speed) == -1)
251         fprintf(stdout,_("Serial port speed setting failed\n"));
252 #else
253   (void)cfsetspeed(&t, speed);
254 #endif
255
256 #ifdef DEBUG
257   fprintf(stdout,_("Serial port speed setting: tcsetattr()...\n"));
258 #endif /* DEBUG */
259   tcsetattr(__fd, TCSANOW, &t);
260 #else
261   ioctl(__fd, TIOCGETP, &t);
262
263   t.sg_ispeed = speed;
264   t.sg_ospeed = speed;
265
266   ioctl(__fd, TIOCSETN, &t);
267 #endif
268 #ifdef DEBUG
269   fprintf(stdout,_("Serial port speed setting done\n"));
270 #endif /* DEBUG */
271 }
272
273 /* Read from serial device. */
274
275 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
276
277   LIVE;
278   return (read(__fd, __buf, __nbytes));
279 }
280
281 /* Write to serial device. */
282
283 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
284         
285         LIVE;
286         return (write(__fd, __buf, __n));
287 }
288
289 #endif  /* WIN32 */