7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
15 /* Do not compile this file under Win32 systems. */
21 #include <sys/ioctl.h>
25 # include <sys/file.h>
29 #include "devices/unixserial.h"
31 #ifdef HAVE_SYS_IOCTL_COMPAT_H
32 #include <sys/ioctl_compat.h>
35 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
39 /* If the target operating system does not have cfsetspeed, we can emulate
42 #ifndef HAVE_CFSETSPEED
43 #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
44 #define cfsetspeed(t, speed) \
45 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
47 static int cfsetspeed(struct termios *t, int speed) {
48 #ifdef HAVE_TERMIOS_CSPEED
63 /* Structure to backup the setting of the terminal. */
65 static struct termios serial_termios;
67 /* Open the serial port and store the settings. */
72 int serial_open(__const char *__file, int __oflag) {
77 __fd = open(__file, __oflag);
79 perror("Gnokii serial_open: open");
83 retcode=tcgetattr(__fd, &serial_termios);
85 perror("Gnokii serial_open:tcgetattr");
86 /* Don't call serial_close since serial_termios is not valid */
94 /* Close the serial port and restore old settings. */
96 int serial_close(int __fd) {
99 tcsetattr(__fd, TCSANOW, &serial_termios);
101 return (close(__fd));
104 /* Open a device with standard options. */
106 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
114 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
120 /* Allow process/thread to receive SIGIO */
123 retcode = fcntl(fd, F_SETOWN, getpid());
125 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
131 /* Make filedescriptor asynchronous. */
134 retcode=fcntl(fd, F_SETFL, FASYNC);
136 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
143 /* Initialise the port settings */
145 memcpy(&tp, &serial_termios, sizeof(struct termios));
147 /* Set port settings for canonical input processing */
149 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
150 if (__with_odd_parity) {
151 tp.c_cflag |= (PARENB | PARODD);
156 if (__with_hw_handshake)
157 tp.c_cflag |= CRTSCTS;
159 tp.c_cflag &= ~CRTSCTS;
166 retcode=tcflush(fd, TCIFLUSH);
168 perror("Gnokii serial_opendevice: tcflush");
173 retcode=tcsetattr(fd, TCSANOW, &tp);
175 perror("Gnokii serial_opendevice: tcsetattr");
183 /* Set the DTR and RTS bit of the serial device. */
185 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
191 if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
192 else ioctl(__fd, TIOCMBIC, &flags);
196 if (__rts) ioctl(__fd, TIOCMBIS, &flags);
197 else ioctl(__fd, TIOCMBIC, &flags);
203 int serial_select(int fd, struct timeval *timeout) {
208 FD_SET(fd, &readfds);
210 return (select(fd + 1, &readfds, NULL, NULL, timeout));
217 /* Change the speed of the serial device. */
219 void serial_changespeed(int __fd, int __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;
239 fprintf(stdout,_("Serial port speed setting: tcgetattr()...\n"));
243 // This is not needed! We set up the speed via cfsetspeed
244 // t.c_cflag &= ~CBAUD;
245 // t.c_cflag |= speed;
247 fprintf(stdout,_("Serial port speed setting: cfsetspeed()...\n"));
250 if (cfsetspeed(&t, speed) == -1)
251 fprintf(stdout,_("Serial port speed setting failed\n"));
253 (void)cfsetspeed(&t, speed);
257 fprintf(stdout,_("Serial port speed setting: tcsetattr()...\n"));
259 tcsetattr(__fd, TCSANOW, &t);
261 ioctl(__fd, TIOCGETP, &t);
266 ioctl(__fd, TIOCSETN, &t);
269 fprintf(stdout,_("Serial port speed setting done\n"));
273 /* Read from serial device. */
275 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
278 return (read(__fd, __buf, __nbytes));
281 /* Write to serial device. */
283 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
286 return (write(__fd, __buf, __n));