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.8.1 2001/11/27 23:06:09 short
15 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
17 Revision 1.1.1.1.2.1 2001/11/27 22:48:37 short
18 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
20 Revision 1.1.1.2 2001/11/27 22:01:15 short
21 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
23 Revision 1.11 2001/11/27 12:19:01 pkot
24 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
26 Revision 1.10 2001/11/14 10:46:12 pkot
27 Small cleanup with __unices__
29 Revision 1.9 2001/09/14 12:15:28 pkot
30 Cleanups from 0.3.3 (part1)
32 Revision 1.8 2001/08/20 23:27:37 pkot
33 Add hardware shakehand to the link layer (Manfred Jonsson)
35 Revision 1.7 2001/07/03 00:03:36 pkot
36 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
38 Revision 1.6 2001/03/21 23:36:04 chris
39 Added the statemachine
40 This will break gnokii --identify and --monitor except for 6210/7110
42 Revision 1.5 2001/03/19 23:43:46 pkot
43 Solaris/ *BSD '#if defined' cleanup
45 Revision 1.4 2001/03/13 01:21:38 pkot
46 *BSD updates (Bert Driehuis)
48 Revision 1.3 2001/03/06 22:27:46 pkot
49 Misc docs and Makefiles updates and cleanups
51 Revision 1.2 2001/02/21 19:57:05 chris
52 More fiddling with the directory layout
58 /* Do not compile this file under Win32 systems. */
64 #include <sys/ioctl.h>
68 #include "devices/unixserial.h"
70 #ifdef HAVE_SYS_IOCTL_COMPAT_H
71 # include <sys/ioctl_compat.h>
74 #ifdef HAVE_SYS_SELECT_H
75 # include <sys/select.h>
78 /* If the target operating system does not have cfsetspeed, we can emulate
81 #ifndef HAVE_CFSETSPEED
82 # if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
83 # define cfsetspeed(t, speed) \
84 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
86 static int cfsetspeed(struct termios *t, int speed)
88 # ifdef HAVE_TERMIOS_CSPEED
93 # endif /* HAVE_TERMIOS_CSPEED */
96 # endif /* HAVE_CFSETISPEED && HAVE_CFSETOSPEED */
97 #endif /* HAVE_CFSETSPEED */
100 # define O_NONBLOCK 0
103 /* Structure to backup the setting of the terminal. */
104 struct termios serial_termios;
106 /* Open the serial port and store the settings. */
107 int serial_open(__const char *__file, int __oflag)
112 __fd = open(__file, __oflag);
114 perror("Gnokii serial_open: open");
118 retcode = tcgetattr(__fd, &serial_termios);
120 perror("Gnokii serial_open:tcgetattr");
121 /* Don't call serial_close since serial_termios is not valid */
129 /* Close the serial port and restore old settings. */
130 int serial_close(int __fd)
133 tcsetattr(__fd, TCSANOW, &serial_termios);
135 return (close(__fd));
138 /* Open a device with standard options. */
139 int serial_opendevice(__const char *__file, int __with_odd_parity,
140 int __with_async, int __with_hw_handshake)
147 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
149 if (fd < 0) return fd;
151 /* Allow process/thread to receive SIGIO */
153 retcode = fcntl(fd, F_SETOWN, getpid());
155 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
161 /* Make filedescriptor asynchronous. */
163 retcode = fcntl(fd, F_SETFL, FASYNC);
165 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
171 /* Initialise the port settings */
172 memcpy(&tp, &serial_termios, sizeof(struct termios));
174 /* Set port settings for canonical input processing */
175 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
176 if (__with_odd_parity) {
177 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. */
209 void serial_setdtrrts(int __fd, int __dtr, int __rts)
216 ioctl(__fd, TIOCMBIS, &flags);
218 ioctl(__fd, TIOCMBIC, &flags);
223 ioctl(__fd, TIOCMBIS, &flags);
225 ioctl(__fd, TIOCMBIC, &flags);
229 int serial_select(int fd, struct timeval *timeout)
234 FD_SET(fd, &readfds);
236 return (select(fd + 1, &readfds, NULL, NULL, timeout));
240 /* Change the speed of the serial device. */
241 void serial_changespeed(int __fd, int __speed)
271 // This is not needed! We set up the speed via cfsetspeed
272 // t.c_cflag &= ~CBAUD;
273 // t.c_cflag |= speed;
274 if (cfsetspeed(&t, speed) == -1)
275 dprintf(_("Serial port speed setting failed\n"));
277 tcsetattr(__fd, TCSADRAIN, &t);
279 ioctl(__fd, TIOCGETP, &t);
284 ioctl(__fd, TIOCSETN, &t);
288 /* Read from serial device. */
289 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes)
291 return (read(__fd, __buf, __nbytes));
294 /* Write to serial device. */
295 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n)
297 return (write(__fd, __buf, __n));