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);
119 /* Allow process/thread to receive SIGIO */
122 retcode = fcntl(fd, F_SETOWN, getpid());
124 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
130 /* Make filedescriptor asynchronous. */
133 retcode=fcntl(fd, F_SETFL, FASYNC);
135 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
141 /* Initialise the port settings */
143 memcpy(&tp, &serial_termios, sizeof(struct termios));
145 /* Set port settings for canonical input processing */
147 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD;
148 if (__with_odd_parity) {
149 tp.c_cflag |= (PARENB | PARODD);
154 if (__with_hw_handshake)
155 tp.c_cflag |= CRTSCTS;
157 tp.c_cflag &= ~CRTSCTS;
164 retcode=tcflush(fd, TCIFLUSH);
166 perror("Gnokii serial_opendevice: tcflush");
171 retcode=tcsetattr(fd, TCSANOW, &tp);
173 perror("Gnokii serial_opendevice: tcsetattr");
181 /* Set the DTR and RTS bit of the serial device. */
183 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
189 if (__dtr) ioctl(__fd, TIOCMBIS, &flags);
190 else ioctl(__fd, TIOCMBIC, &flags);
194 if (__rts) ioctl(__fd, TIOCMBIS, &flags);
195 else ioctl(__fd, TIOCMBIC, &flags);
201 int serial_select(int fd, struct timeval *timeout) {
206 FD_SET(fd, &readfds);
208 return (select(fd + 1, &readfds, NULL, NULL, timeout));
215 /* Change the speed of the serial device. */
217 void serial_changespeed(int __fd, int __speed) {
228 case 9600: speed = B9600; break;
229 case 19200: speed = B19200; break;
230 case 38400: speed = B38400; break;
231 case 57600: speed = B57600; break;
232 case 115200: speed = B115200; break;
237 fprintf(stdout,_("Serial port speed setting: tcgetattr()...\n"));
241 // This is not needed! We set up the speed via cfsetspeed
242 // t.c_cflag &= ~CBAUD;
243 // t.c_cflag |= speed;
245 fprintf(stdout,_("Serial port speed setting: cfsetspeed()...\n"));
248 if (cfsetspeed(&t, speed) == -1)
249 fprintf(stdout,_("Serial port speed setting failed\n"));
251 (void)cfsetspeed(&t, speed);
255 fprintf(stdout,_("Serial port speed setting: tcsetattr()...\n"));
257 tcsetattr(__fd, TCSANOW, &t);
259 ioctl(__fd, TIOCGETP, &t);
264 ioctl(__fd, TIOCSETN, &t);
267 fprintf(stdout,_("Serial port speed setting done\n"));
271 /* Read from serial device. */
273 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
276 return (read(__fd, __buf, __nbytes));
279 /* Write to serial device. */
281 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
284 return (write(__fd, __buf, __n));