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.5 2002/04/03 00:08:07 short
15 Found in "gnokii-working" directory, some November-patches version
17 Revision 1.9 2001/09/14 12:15:28 pkot
18 Cleanups from 0.3.3 (part1)
20 Revision 1.8 2001/08/20 23:27:37 pkot
21 Add hardware shakehand to the link layer (Manfred Jonsson)
23 Revision 1.7 2001/07/03 00:03:36 pkot
24 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
26 Revision 1.6 2001/03/21 23:36:04 chris
27 Added the statemachine
28 This will break gnokii --identify and --monitor except for 6210/7110
30 Revision 1.5 2001/03/19 23:43:46 pkot
31 Solaris/ *BSD '#if defined' cleanup
33 Revision 1.4 2001/03/13 01:21:38 pkot
34 *BSD updates (Bert Driehuis)
36 Revision 1.3 2001/03/06 22:27:46 pkot
37 Misc docs and Makefiles updates and cleanups
39 Revision 1.2 2001/02/21 19:57:05 chris
40 More fiddling with the directory layout
45 /* [global] option "serial_write_usleep" default: */
46 #define SERIAL_WRITE_USLEEP_DEFAULT (-1)
49 #include "cfgreader.h"
51 /* Do not compile this file under Win32 systems. */
57 #include <sys/ioctl.h>
62 #include <sys/types.h>
64 #include <sys/ioctl.h>
67 # include <sys/file.h>
71 #include "devices/unixserial.h"
73 #ifdef HAVE_SYS_IOCTL_COMPAT_H
74 #include <sys/ioctl_compat.h>
77 #ifdef HAVE_SYS_SELECT_H
78 #include <sys/select.h>
81 /* If the target operating system does not have cfsetspeed, we can emulate
84 #ifndef HAVE_CFSETSPEED
85 #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
86 #define cfsetspeed(t, speed) \
87 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
89 static int cfsetspeed(struct termios *t, int speed) {
90 #ifdef HAVE_TERMIOS_CSPEED
105 /* Structure to backup the setting of the terminal. */
107 struct termios serial_termios;
109 /* Script handling: */
111 static void device_script_cfgfunc(const char *section,const char *key,const char *value)
113 setenv(key,value,1/*overwrite*/); /* errors ignored */
116 static int device_script(int fd, const char *section)
119 const char *scriptname = CFG_Get(CFG_Info, "global", section);
125 switch ((pid=fork())) {
127 fprintf(stderr,_("device_script(\"%s\"): fork() failure: %s!\n"),scriptname,strerror(errno));
130 default: { /* parent */
132 if (pid==waitpid(pid,&status,0/*options*/) && WIFEXITED(status) && !WEXITSTATUS(status))
134 fprintf(stderr,_("device_script(\"%s\"): child script failure: %s, exit code=%d\n"),scriptname,
135 (WIFEXITED(status) ? _("normal exit") : _("abnormal exit")),
136 (WIFEXITED(status) ? WEXITSTATUS(status) : -1));
141 case 0: { /* child */
142 CFG_GetForeach(CFG_Info,section,device_script_cfgfunc);
144 if (0!=dup2(fd,0) || 1!=dup2(fd,1) || close(fd)) {
145 fprintf(stderr,_("device_script(\"%s\"): file descriptor prepare: %s\n"),scriptname,strerror(errno));
148 /* FIXME: close all open descriptors - how to track them?
150 execl("/bin/sh","sh","-c",scriptname,NULL);
151 fprintf(stderr,_("device_script(\"%s\"): execute script: %s\n"),scriptname,strerror(errno));
159 int serial_close_all_openfds[0x10]; /* -1 when entry not used, fd otherwise */
160 int serial_close(int __fd);
162 static void serial_close_all(void)
166 dprintf("serial_close_all() executed\n");
167 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
168 if (serial_close_all_openfds[i]!=-1)
169 serial_close(serial_close_all_openfds[i]);
172 static void unixserial_interrupted(int signo)
178 /* Open the serial port and store the settings. */
180 int serial_open(__const char *__file, int __oflag) {
184 static bool atexit_registered=false;
186 if (!atexit_registered) {
187 memset(serial_close_all_openfds,-1,sizeof(serial_close_all_openfds));
188 #if 0 /* Disabled for now as atexit() functions are then called multiple times for pthreads! */
189 signal(SIGINT,unixserial_interrupted);
191 atexit(serial_close_all);
192 atexit_registered=true;
195 __fd = open(__file, __oflag);
197 perror("Gnokii serial_open: open");
201 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
202 if (serial_close_all_openfds[i]==-1 || serial_close_all_openfds[i]==__fd) {
203 serial_close_all_openfds[i]=__fd;
207 retcode=tcgetattr(__fd, &serial_termios);
209 perror("Gnokii serial_open:tcgetattr");
210 /* Don't call serial_close since serial_termios is not valid */
218 /* Close the serial port and restore old settings. */
220 int serial_close(int __fd) {
223 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
224 if (serial_close_all_openfds[i]==__fd)
225 serial_close_all_openfds[i]=-1; /* fd closed */
227 /* handle config file disconnect_script:
229 if (-1 == device_script(__fd,"disconnect_script"))
230 fprintf(stderr,"Gnokii serial_close: disconnect_script\n");
234 serial_termios.c_cflag |= HUPCL; /* production == 1 */
236 serial_termios.c_cflag &= ~HUPCL; /* debugging == 0 */
239 tcsetattr(__fd, TCSANOW, &serial_termios);
242 return (close(__fd));
245 /* Open a device with standard options.
246 * Use value (-1) for "__with_hw_handshake" if its specification is required from the user
248 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
254 /* handle config file handshake override: */
256 char *s=CFG_Get(CFG_Info, "global", "handshake");
258 if (s && (!strcasecmp(s,"software") || !strcasecmp(s,"rtscts")))
259 __with_hw_handshake=false;
260 else if (s && (!strcasecmp(s,"hardware") || !strcasecmp(s,"xonxoff")))
261 __with_hw_handshake=true;
263 fprintf(stderr,_("Unrecognized [%s] option \"%s\", use \"%s\" or \"%s\" value, ignoring!"),
264 "global","handshake","software","hardware");
266 if (__with_hw_handshake==-1) {
267 fprintf(stderr,_("[%s] option \"%s\" not found, trying to use \"%s\" value!"),
268 "global","handshake","software");
269 __with_hw_handshake=false;
275 /* O_NONBLOCK MUST be used here as the CLOCAL may be currently off
276 * and if DCD is down the "open" syscall would be stuck wating for DCD.
278 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
283 /* Initialise the port settings */
285 memcpy(&tp, &serial_termios, sizeof(struct termios));
287 /* Set port settings for canonical input processing */
289 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL;
290 if (__with_odd_parity) {
291 tp.c_cflag |= (PARENB | PARODD);
296 if (__with_hw_handshake)
297 tp.c_cflag |= CRTSCTS;
299 tp.c_cflag &= ~CRTSCTS;
306 retcode=tcflush(fd, TCIFLUSH);
308 perror("Gnokii serial_opendevice: tcflush");
313 retcode=tcsetattr(fd, TCSANOW, &tp);
315 perror("Gnokii serial_opendevice: tcsetattr");
322 char *baudratestring=CFG_Get(CFG_Info, "global", "serial_baudrate");
326 baudrate=atoi(baudratestring);
327 if (baudrate && !serial_changespeed(fd,baudrate))
330 serial_changespeed(fd,19200/*default value*/);
333 /* We need to turn off O_NONBLOCK now (we have CLOCAL set so it is safe).
334 * When we run some device script it really doesn't expect NONBLOCK!
337 retcode=fcntl(fd, F_SETFL, 0);
339 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
344 /* handle config file connect_script:
346 if (-1 == device_script(fd,"connect_script")) {
347 fprintf(stderr,"Gnokii serial_opendevice: connect_script\n");
352 /* Allow process/thread to receive SIGIO */
355 retcode = fcntl(fd, F_SETOWN, getpid());
357 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
363 /* Make filedescriptor asynchronous. */
365 /* We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
366 * by F_SETFL as a side-effect!
368 retcode=fcntl(fd, F_SETFL, (__with_async ? FASYNC : 0) | FNONBLOCK);
370 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
378 /* Set the DTR and RTS bit of the serial device. */
380 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
387 ioctl(__fd, TIOCMBIS, &flags);
389 ioctl(__fd, TIOCMBIC, &flags);
394 ioctl(__fd, TIOCMBIS, &flags);
396 ioctl(__fd, TIOCMBIC, &flags);
400 int serial_select(int fd, struct timeval *timeout) {
405 FD_SET(fd, &readfds);
407 return (select(fd + 1, &readfds, NULL, NULL, timeout));
412 /* Change the speed of the serial device.
416 bool serial_changespeed(int __fd, int __speed) {
428 case 9600: speed = B9600; break;
429 case 19200: speed = B19200; break;
430 case 38400: speed = B38400; break;
431 case 57600: speed = B57600; break;
432 case 115200: speed = B115200; break;
434 fprintf(stderr,_("Serial port speed %d not supported!\n"),__speed);
439 if (tcgetattr(__fd, &t))
442 // This is not needed! We set up the speed via cfsetspeed
443 // t.c_cflag &= ~CBAUD;
444 // t.c_cflag |= speed;
445 if (cfsetspeed(&t, speed) == -1) {
446 dprintf(_("Serial port speed setting failed\n"));
450 tcsetattr(__fd, TCSADRAIN, &t);
452 if (ioctl(__fd, TIOCGETP, &t))
458 if (ioctl(__fd, TIOCSETN, &t))
465 /* Read from serial device. */
467 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
469 return (read(__fd, __buf, __nbytes));
472 #if !defined(TIOCMGET) && defined(TIOCMODG)
473 #define TIOCMGET TIOCMODG
476 static void check_dcd(int __fd)
481 if (ioctl(__fd, TIOCMGET, &mcs) || !(mcs & TIOCM_CAR)) {
482 fprintf(stderr,_("ERROR: Modem DCD is down and global/require_dcd parameter is set!\n"));
483 exit(EXIT_FAILURE); /* Hard quit of all threads */
486 /* Impossible!! (eg. Coherent) */
490 /* Write to serial device. */
492 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
496 static long serial_write_usleep=LONG_MIN;
497 static int require_dcd=-1;
499 if (serial_write_usleep==LONG_MIN) {
500 char *s=CFG_Get(CFG_Info, "global", "serial_write_usleep");
502 serial_write_usleep=(!s ?
503 SERIAL_WRITE_USLEEP_DEFAULT : atol(CFG_Get(CFG_Info, "global", "serial_write_usleep")));
506 if (require_dcd==-1) {
507 require_dcd=(!!CFG_Get(CFG_Info, "global", "require_dcd"));
510 fprintf(stderr,_("WARNING: global/require_dcd argument was set but it is not supported on this system!\n"));
517 if (serial_write_usleep<0)
518 return(write(__fd, __buf, __n));
521 got=write(__fd, __buf, 1);
523 return((!r ? -1 : r));
527 if (serial_write_usleep)
528 usleep(serial_write_usleep);