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.1.2.1 2001/11/27 22:48:37 short
15 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
17 Revision 1.1.1.2 2001/11/27 22:01:15 short
18 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
20 Revision 1.11 2001/11/27 12:19:01 pkot
21 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
23 Revision 1.10 2001/11/14 10:46:12 pkot
24 Small cleanup with __unices__
26 Revision 1.9 2001/09/14 12:15:28 pkot
27 Cleanups from 0.3.3 (part1)
29 Revision 1.8 2001/08/20 23:27:37 pkot
30 Add hardware shakehand to the link layer (Manfred Jonsson)
32 Revision 1.7 2001/07/03 00:03:36 pkot
33 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
35 Revision 1.6 2001/03/21 23:36:04 chris
36 Added the statemachine
37 This will break gnokii --identify and --monitor except for 6210/7110
39 Revision 1.5 2001/03/19 23:43:46 pkot
40 Solaris/ *BSD '#if defined' cleanup
42 Revision 1.4 2001/03/13 01:21:38 pkot
43 *BSD updates (Bert Driehuis)
45 Revision 1.3 2001/03/06 22:27:46 pkot
46 Misc docs and Makefiles updates and cleanups
48 Revision 1.2 2001/02/21 19:57:05 chris
49 More fiddling with the directory layout
55 /* Do not compile this file under Win32 systems. */
61 #include <sys/ioctl.h>
65 #include "devices/unixserial.h"
67 #ifdef HAVE_SYS_IOCTL_COMPAT_H
68 # include <sys/ioctl_compat.h>
71 #ifdef HAVE_SYS_SELECT_H
72 # include <sys/select.h>
75 /* If the target operating system does not have cfsetspeed, we can emulate
78 #ifndef HAVE_CFSETSPEED
79 # if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
80 # define cfsetspeed(t, speed) \
81 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
83 static int cfsetspeed(struct termios *t, int speed)
85 # ifdef HAVE_TERMIOS_CSPEED
90 # endif /* HAVE_TERMIOS_CSPEED */
93 # endif /* HAVE_CFSETISPEED && HAVE_CFSETOSPEED */
94 #endif /* HAVE_CFSETSPEED */
100 /* Structure to backup the setting of the terminal. */
101 struct termios serial_termios;
103 /* Open the serial port and store the settings. */
104 int serial_open(__const char *__file, int __oflag)
109 __fd = open(__file, __oflag);
111 perror("Gnokii serial_open: open");
115 retcode = tcgetattr(__fd, &serial_termios);
117 perror("Gnokii serial_open:tcgetattr");
118 /* Don't call serial_close since serial_termios is not valid */
126 /* Close the serial port and restore old settings. */
127 int serial_close(int __fd)
130 tcsetattr(__fd, TCSANOW, &serial_termios);
132 return (close(__fd));
135 /* Open a device with standard options. */
136 int serial_opendevice(__const char *__file, int __with_odd_parity,
137 int __with_async, int __with_hw_handshake)
144 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
146 if (fd < 0) return fd;
148 /* Allow process/thread to receive SIGIO */
150 retcode = fcntl(fd, F_SETOWN, getpid());
152 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
158 /* Make filedescriptor asynchronous. */
160 retcode = fcntl(fd, F_SETFL, FASYNC);
162 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
168 /* Initialise the port settings */
169 memcpy(&tp, &serial_termios, sizeof(struct termios));
171 /* Set port settings for canonical input processing */
172 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
173 if (__with_odd_parity) {
174 tp.c_cflag |= (PARENB | PARODD);
178 if (__with_hw_handshake)
179 tp.c_cflag |= CRTSCTS;
181 tp.c_cflag &= ~CRTSCTS;
188 retcode = tcflush(fd, TCIFLUSH);
190 perror("Gnokii serial_opendevice: tcflush");
195 retcode = tcsetattr(fd, TCSANOW, &tp);
197 perror("Gnokii serial_opendevice: tcsetattr");
205 /* Set the DTR and RTS bit of the serial device. */
206 void serial_setdtrrts(int __fd, int __dtr, int __rts)
213 ioctl(__fd, TIOCMBIS, &flags);
215 ioctl(__fd, TIOCMBIC, &flags);
220 ioctl(__fd, TIOCMBIS, &flags);
222 ioctl(__fd, TIOCMBIC, &flags);
226 int serial_select(int fd, struct timeval *timeout)
231 FD_SET(fd, &readfds);
233 return (select(fd + 1, &readfds, NULL, NULL, timeout));
237 /* Change the speed of the serial device. */
238 void serial_changespeed(int __fd, int __speed)
268 // This is not needed! We set up the speed via cfsetspeed
269 // t.c_cflag &= ~CBAUD;
270 // t.c_cflag |= speed;
271 if (cfsetspeed(&t, speed) == -1)
272 dprintf(_("Serial port speed setting failed\n"));
274 tcsetattr(__fd, TCSADRAIN, &t);
276 ioctl(__fd, TIOCGETP, &t);
281 ioctl(__fd, TIOCSETN, &t);
285 /* Read from serial device. */
286 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes)
288 return (read(__fd, __buf, __nbytes));
291 /* Write to serial device. */
292 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n)
294 return (write(__fd, __buf, __n));