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 */
93 /* Close the serial port and restore old settings. */
95 int serial_close(int __fd) {
98 tcsetattr(__fd, TCSANOW, &serial_termios);
100 return (close(__fd));
103 /* Open a device with standard options. */
105 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
113 fd = serial_open(__file, O_RDONLY | O_NOCTTY | O_NONBLOCK);
118 /* Allow process/thread to receive SIGIO */
121 retcode = fcntl(fd, F_SETOWN, getpid());
123 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)");
144 /* Initialise the port settings */
146 memcpy(&tp, &serial_termios, sizeof(struct termios));
148 /* Set port settings for canonical input processing */
150 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
151 if (__with_odd_parity) {
152 tp.c_cflag |= (PARENB | PARODD);
157 if (__with_hw_handshake)
158 tp.c_cflag |= CRTSCTS;
160 tp.c_cflag &= ~CRTSCTS;
167 retcode=tcflush(fd, TCIFLUSH);
169 perror("Gnokii serial_opendevice: tcflush");
176 retcode=tcsetattr(fd, TCSANOW, &tp);
178 perror("Gnokii serial_opendevice: tcsetattr");
188 /* Set the DTR and RTS bit of the serial device. */
190 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
196 if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
197 else ioctl(__fd, TIOCMBIC, &flags);
201 if (__rts) ioctl(__fd, TIOCMBIS, &flags);
202 else ioctl(__fd, TIOCMBIC, &flags);
206 int serial_select(int fd, struct timeval *timeout) {
211 FD_SET(fd, &readfds);
213 return (select(fd + 1, &readfds, NULL, NULL, timeout));
218 /* Change the speed of the serial device. */
220 void serial_changespeed(int __fd, int __speed) {
231 case 9600: speed = B9600; break;
232 case 19200: speed = B19200; break;
233 case 38400: speed = B38400; break;
234 case 57600: speed = B57600; break;
235 case 115200: speed = B115200; break;
241 // This is not needed! We set up the speed via cfsetspeed
242 // t.c_cflag &= ~CBAUD;
243 // t.c_cflag |= speed;
245 if (cfsetspeed(&t, speed) == -1)
246 fprintf(stdout,_("Serial port speed setting failed\n"));
248 cfsetspeed(&t, speed);
251 tcsetattr(__fd, TCSADRAIN, &t);
253 ioctl(__fd, TIOCGETP, &t);
258 ioctl(__fd, TIOCSETN, &t);
262 /* Read from serial device. */
264 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
266 return (read(__fd, __buf, __nbytes));
269 /* Write to serial device. */
271 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
274 return (write(__fd, __buf, __n));