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 2001/11/25 21:59:09 short
15 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
17 Revision 1.10 2001/11/14 10:46:12 pkot
18 Small cleanup with __unices__
20 Revision 1.9 2001/09/14 12:15:28 pkot
21 Cleanups from 0.3.3 (part1)
23 Revision 1.8 2001/08/20 23:27:37 pkot
24 Add hardware shakehand to the link layer (Manfred Jonsson)
26 Revision 1.7 2001/07/03 00:03:36 pkot
27 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
29 Revision 1.6 2001/03/21 23:36:04 chris
30 Added the statemachine
31 This will break gnokii --identify and --monitor except for 6210/7110
33 Revision 1.5 2001/03/19 23:43:46 pkot
34 Solaris/ *BSD '#if defined' cleanup
36 Revision 1.4 2001/03/13 01:21:38 pkot
37 *BSD updates (Bert Driehuis)
39 Revision 1.3 2001/03/06 22:27:46 pkot
40 Misc docs and Makefiles updates and cleanups
42 Revision 1.2 2001/02/21 19:57:05 chris
43 More fiddling with the directory layout
49 /* Do not compile this file under Win32 systems. */
55 #include <sys/ioctl.h>
59 #include "devices/unixserial.h"
61 #ifdef HAVE_SYS_IOCTL_COMPAT_H
62 #include <sys/ioctl_compat.h>
65 #ifdef HAVE_SYS_SELECT_H
66 #include <sys/select.h>
69 /* If the target operating system does not have cfsetspeed, we can emulate
72 #ifndef HAVE_CFSETSPEED
73 #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
74 #define cfsetspeed(t, speed) \
75 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
77 static int cfsetspeed(struct termios *t, int speed) {
78 #ifdef HAVE_TERMIOS_CSPEED
93 /* Structure to backup the setting of the terminal. */
95 struct termios serial_termios;
97 /* Open the serial port and store the settings. */
99 int serial_open(__const char *__file, int __oflag) {
104 __fd = open(__file, __oflag);
106 perror("Gnokii serial_open: open");
110 retcode=tcgetattr(__fd, &serial_termios);
112 perror("Gnokii serial_open:tcgetattr");
113 /* Don't call serial_close since serial_termios is not valid */
121 /* Close the serial port and restore old settings. */
123 int serial_close(int __fd) {
126 tcsetattr(__fd, TCSANOW, &serial_termios);
128 return (close(__fd));
131 /* Open a device with standard options. */
133 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
141 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
146 /* Allow process/thread to receive SIGIO */
149 retcode = fcntl(fd, F_SETOWN, getpid());
151 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
157 /* Make filedescriptor asynchronous. */
160 retcode=fcntl(fd, F_SETFL, FASYNC);
162 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
168 /* Initialise the port settings */
170 memcpy(&tp, &serial_termios, sizeof(struct termios));
172 /* Set port settings for canonical input processing */
174 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
175 if (__with_odd_parity) {
176 tp.c_cflag |= (PARENB | PARODD);
181 if (__with_hw_handshake)
182 tp.c_cflag |= CRTSCTS;
184 tp.c_cflag &= ~CRTSCTS;
191 retcode=tcflush(fd, TCIFLUSH);
193 perror("Gnokii serial_opendevice: tcflush");
198 retcode=tcsetattr(fd, TCSANOW, &tp);
200 perror("Gnokii serial_opendevice: tcsetattr");
208 /* Set the DTR and RTS bit of the serial device. */
210 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
217 ioctl(__fd, TIOCMBIS, &flags);
219 ioctl(__fd, TIOCMBIC, &flags);
224 ioctl(__fd, TIOCMBIS, &flags);
226 ioctl(__fd, TIOCMBIC, &flags);
230 int serial_select(int fd, struct timeval *timeout) {
235 FD_SET(fd, &readfds);
237 return (select(fd + 1, &readfds, NULL, NULL, timeout));
242 /* Change the speed of the serial device. */
244 void serial_changespeed(int __fd, int __speed) {
255 case 9600: speed = B9600; break;
256 case 19200: speed = B19200; break;
257 case 38400: speed = B38400; break;
258 case 57600: speed = B57600; break;
259 case 115200: speed = B115200; break;
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. */
284 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
286 return (read(__fd, __buf, __nbytes));
289 /* Write to serial device. */
291 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
293 return (write(__fd, __buf, __n));