7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Copyright (C) 1999, 2000 Hugh Blemings & Pavel JanÃk ml.
11 Released under the terms of the GNU GPL, see file COPYING for more details.
14 Revision 1.1.1.2 2001/11/27 22:01:15 short
15 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
17 Revision 1.11 2001/11/27 12:19:01 pkot
18 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
20 Revision 1.10 2001/11/14 10:46:12 pkot
21 Small cleanup with __unices__
23 Revision 1.9 2001/09/14 12:15:28 pkot
24 Cleanups from 0.3.3 (part1)
26 Revision 1.8 2001/08/20 23:27:37 pkot
27 Add hardware shakehand to the link layer (Manfred Jonsson)
29 Revision 1.7 2001/07/03 00:03:36 pkot
30 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
32 Revision 1.6 2001/03/21 23:36:04 chris
33 Added the statemachine
34 This will break gnokii --identify and --monitor except for 6210/7110
36 Revision 1.5 2001/03/19 23:43:46 pkot
37 Solaris/ *BSD '#if defined' cleanup
39 Revision 1.4 2001/03/13 01:21:38 pkot
40 *BSD updates (Bert Driehuis)
42 Revision 1.3 2001/03/06 22:27:46 pkot
43 Misc docs and Makefiles updates and cleanups
45 Revision 1.2 2001/02/21 19:57:05 chris
46 More fiddling with the directory layout
52 /* Do not compile this file under Win32 systems. */
58 #include <sys/ioctl.h>
62 #include "devices/unixserial.h"
64 #ifdef HAVE_SYS_IOCTL_COMPAT_H
65 # include <sys/ioctl_compat.h>
68 #ifdef HAVE_SYS_SELECT_H
69 # include <sys/select.h>
72 /* If the target operating system does not have cfsetspeed, we can emulate
75 #ifndef HAVE_CFSETSPEED
76 # if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
77 # define cfsetspeed(t, speed) \
78 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
80 static int cfsetspeed(struct termios *t, int speed)
82 # ifdef HAVE_TERMIOS_CSPEED
87 # endif /* HAVE_TERMIOS_CSPEED */
90 # endif /* HAVE_CFSETISPEED && HAVE_CFSETOSPEED */
91 #endif /* HAVE_CFSETSPEED */
97 /* Structure to backup the setting of the terminal. */
98 struct termios serial_termios;
100 /* Open the serial port and store the settings. */
101 int serial_open(__const char *__file, int __oflag)
106 __fd = open(__file, __oflag);
108 perror("Gnokii serial_open: open");
112 retcode = tcgetattr(__fd, &serial_termios);
114 perror("Gnokii serial_open:tcgetattr");
115 /* Don't call serial_close since serial_termios is not valid */
123 /* Close the serial port and restore old settings. */
124 int serial_close(int __fd)
127 tcsetattr(__fd, TCSANOW, &serial_termios);
129 return (close(__fd));
132 /* Open a device with standard options. */
133 int serial_opendevice(__const char *__file, int __with_odd_parity,
134 int __with_async, int __with_hw_handshake)
141 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
143 if (fd < 0) return fd;
145 /* Allow process/thread to receive SIGIO */
147 retcode = fcntl(fd, F_SETOWN, getpid());
149 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
155 /* Make filedescriptor asynchronous. */
157 retcode = fcntl(fd, F_SETFL, FASYNC);
159 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
165 /* Initialise the port settings */
166 memcpy(&tp, &serial_termios, sizeof(struct termios));
168 /* Set port settings for canonical input processing */
169 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
170 if (__with_odd_parity) {
171 tp.c_cflag |= (PARENB | PARODD);
175 if (__with_hw_handshake)
176 tp.c_cflag |= CRTSCTS;
178 tp.c_cflag &= ~CRTSCTS;
185 retcode = tcflush(fd, TCIFLUSH);
187 perror("Gnokii serial_opendevice: tcflush");
192 retcode = tcsetattr(fd, TCSANOW, &tp);
194 perror("Gnokii serial_opendevice: tcsetattr");
202 /* Set the DTR and RTS bit of the serial device. */
203 void serial_setdtrrts(int __fd, int __dtr, int __rts)
210 ioctl(__fd, TIOCMBIS, &flags);
212 ioctl(__fd, TIOCMBIC, &flags);
217 ioctl(__fd, TIOCMBIS, &flags);
219 ioctl(__fd, TIOCMBIC, &flags);
223 int serial_select(int fd, struct timeval *timeout)
228 FD_SET(fd, &readfds);
230 return (select(fd + 1, &readfds, NULL, NULL, timeout));
234 /* Change the speed of the serial device. */
235 void serial_changespeed(int __fd, int __speed)
265 // This is not needed! We set up the speed via cfsetspeed
266 // t.c_cflag &= ~CBAUD;
267 // t.c_cflag |= speed;
268 if (cfsetspeed(&t, speed) == -1)
269 dprintf(_("Serial port speed setting failed\n"));
271 tcsetattr(__fd, TCSADRAIN, &t);
273 ioctl(__fd, TIOCGETP, &t);
278 ioctl(__fd, TIOCSETN, &t);
282 /* Read from serial device. */
283 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes)
285 return (read(__fd, __buf, __nbytes));
288 /* Write to serial device. */
289 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n)
291 return (write(__fd, __buf, __n));