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 struct termios serial_termios;
67 /* Open the serial port and store the settings. */
69 int serial_open(__const char *__file, int __oflag) {
74 __fd = open(__file, __oflag);
76 perror("Gnokii serial_open: open");
80 retcode=tcgetattr(__fd, &serial_termios);
82 perror("Gnokii serial_open:tcgetattr");
83 /* Don't call serial_close since serial_termios is not valid */
91 /* Close the serial port and restore old settings. */
93 int serial_close(int __fd) {
96 tcsetattr(__fd, TCSANOW, &serial_termios);
101 /* Open a device with standard options. */
103 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
111 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
116 /* Allow process/thread to receive SIGIO */
119 retcode = fcntl(fd, F_SETOWN, getpid());
121 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
127 /* Make filedescriptor asynchronous. */
130 retcode=fcntl(fd, F_SETFL, FASYNC);
132 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
138 /* Initialise the port settings */
140 memcpy(&tp, &serial_termios, sizeof(struct termios));
142 /* Set port settings for canonical input processing */
144 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
145 if (__with_odd_parity) {
146 tp.c_cflag |= (PARENB | PARODD);
151 if (__with_hw_handshake)
152 tp.c_cflag |= CRTSCTS;
154 tp.c_cflag &= ~CRTSCTS;
161 retcode=tcflush(fd, TCIFLUSH);
163 perror("Gnokii serial_opendevice: tcflush");
168 retcode=tcsetattr(fd, TCSANOW, &tp);
170 perror("Gnokii serial_opendevice: tcsetattr");
178 /* Set the DTR and RTS bit of the serial device. */
180 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
186 if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
187 else ioctl(__fd, TIOCMBIC, &flags);
191 if (__rts) ioctl(__fd, TIOCMBIS, &flags);
192 else ioctl(__fd, TIOCMBIC, &flags);
196 int serial_select(int fd, struct timeval *timeout) {
201 FD_SET(fd, &readfds);
203 return (select(fd + 1, &readfds, NULL, NULL, timeout));
208 /* Change the speed of the serial device. */
210 void serial_changespeed(int __fd, int __speed) {
221 case 9600: speed = B9600; break;
222 case 19200: speed = B19200; break;
223 case 38400: speed = B38400; break;
224 case 57600: speed = B57600; break;
225 case 115200: speed = B115200; break;
231 // This is not needed! We set up the speed via cfsetspeed
232 // t.c_cflag &= ~CBAUD;
233 // t.c_cflag |= speed;
235 if (cfsetspeed(&t, speed) == -1)
236 fprintf(stdout,_("Serial port speed setting failed\n"));
238 cfsetspeed(&t, speed);
241 tcsetattr(__fd, TCSADRAIN, &t);
243 ioctl(__fd, TIOCGETP, &t);
248 ioctl(__fd, TIOCSETN, &t);
252 /* Read from serial device. */
254 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
256 return (read(__fd, __buf, __nbytes));
259 /* Write to serial device. */
261 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
263 return (write(__fd, __buf, __n));