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.6 2002/04/03 01:44:15 short
15 Implemented connection type "tcp" (GCT_TCP), use <hostname>:<port> as "port"
17 Revision 1.1.1.5 2002/04/03 00:08:07 short
18 Found in "gnokii-working" directory, some November-patches version
20 Revision 1.9 2001/09/14 12:15:28 pkot
21 Cleanups from 0.3.3 (part1)
23 Revision 1.8 2001/08/20 23:27:37 pkot
24 Add hardware shakehand to the link layer (Manfred Jonsson)
26 Revision 1.7 2001/07/03 00:03:36 pkot
27 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
29 Revision 1.6 2001/03/21 23:36:04 chris
30 Added the statemachine
31 This will break gnokii --identify and --monitor except for 6210/7110
33 Revision 1.5 2001/03/19 23:43:46 pkot
34 Solaris/ *BSD '#if defined' cleanup
36 Revision 1.4 2001/03/13 01:21:38 pkot
37 *BSD updates (Bert Driehuis)
39 Revision 1.3 2001/03/06 22:27:46 pkot
40 Misc docs and Makefiles updates and cleanups
42 Revision 1.2 2001/02/21 19:57:05 chris
43 More fiddling with the directory layout
48 /* [global] option "serial_write_usleep" default: */
49 #define SERIAL_WRITE_USLEEP_DEFAULT (-1)
52 #include "cfgreader.h"
54 /* Do not compile this file under Win32 systems. */
60 #include <sys/ioctl.h>
65 #include <sys/types.h>
67 #include <sys/ioctl.h>
70 # include <sys/file.h>
74 #include "devices/unixserial.h"
76 #ifdef HAVE_SYS_IOCTL_COMPAT_H
77 #include <sys/ioctl_compat.h>
80 #ifdef HAVE_SYS_SELECT_H
81 #include <sys/select.h>
84 /* If the target operating system does not have cfsetspeed, we can emulate
87 #ifndef HAVE_CFSETSPEED
88 #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
89 #define cfsetspeed(t, speed) \
90 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
92 static int cfsetspeed(struct termios *t, int speed) {
93 #ifdef HAVE_TERMIOS_CSPEED
108 /* Structure to backup the setting of the terminal. */
110 struct termios serial_termios;
112 /* Script handling: */
114 static void device_script_cfgfunc(const char *section,const char *key,const char *value)
116 setenv(key,value,1/*overwrite*/); /* errors ignored */
119 int device_script(int fd, const char *section)
122 const char *scriptname = CFG_Get(CFG_Info, "global", section);
128 switch ((pid=fork())) {
130 fprintf(stderr,_("device_script(\"%s\"): fork() failure: %s!\n"),scriptname,strerror(errno));
133 default: { /* parent */
135 if (pid==waitpid(pid,&status,0/*options*/) && WIFEXITED(status) && !WEXITSTATUS(status))
137 fprintf(stderr,_("device_script(\"%s\"): child script failure: %s, exit code=%d\n"),scriptname,
138 (WIFEXITED(status) ? _("normal exit") : _("abnormal exit")),
139 (WIFEXITED(status) ? WEXITSTATUS(status) : -1));
144 case 0: { /* child */
145 CFG_GetForeach(CFG_Info,section,device_script_cfgfunc);
147 if (0!=dup2(fd,0) || 1!=dup2(fd,1) || close(fd)) {
148 fprintf(stderr,_("device_script(\"%s\"): file descriptor prepare: %s\n"),scriptname,strerror(errno));
151 /* FIXME: close all open descriptors - how to track them?
153 execl("/bin/sh","sh","-c",scriptname,NULL);
154 fprintf(stderr,_("device_script(\"%s\"): execute script: %s\n"),scriptname,strerror(errno));
162 int serial_close_all_openfds[0x10]; /* -1 when entry not used, fd otherwise */
163 int serial_close(int __fd);
165 void serial_close_all(void)
169 dprintf("serial_close_all() executed\n");
170 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
171 if (serial_close_all_openfds[i]!=-1)
172 serial_close(serial_close_all_openfds[i]);
175 void unixserial_interrupted(int signo)
181 /* Open the serial port and store the settings. */
183 int serial_open(__const char *__file, int __oflag) {
187 static bool atexit_registered=false;
189 if (!atexit_registered) {
190 memset(serial_close_all_openfds,-1,sizeof(serial_close_all_openfds));
191 #if 0 /* Disabled for now as atexit() functions are then called multiple times for pthreads! */
192 signal(SIGINT,unixserial_interrupted);
194 atexit(serial_close_all);
195 atexit_registered=true;
198 __fd = open(__file, __oflag);
200 perror("Gnokii serial_open: open");
204 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
205 if (serial_close_all_openfds[i]==-1 || serial_close_all_openfds[i]==__fd) {
206 serial_close_all_openfds[i]=__fd;
210 retcode=tcgetattr(__fd, &serial_termios);
212 perror("Gnokii serial_open:tcgetattr");
213 /* Don't call serial_close since serial_termios is not valid */
221 /* Close the serial port and restore old settings. */
223 int serial_close(int __fd) {
226 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
227 if (serial_close_all_openfds[i]==__fd)
228 serial_close_all_openfds[i]=-1; /* fd closed */
230 /* handle config file disconnect_script:
232 if (-1 == device_script(__fd,"disconnect_script"))
233 fprintf(stderr,"Gnokii serial_close: disconnect_script\n");
237 serial_termios.c_cflag |= HUPCL; /* production == 1 */
239 serial_termios.c_cflag &= ~HUPCL; /* debugging == 0 */
242 tcsetattr(__fd, TCSANOW, &serial_termios);
245 return (close(__fd));
248 /* Open a device with standard options.
249 * Use value (-1) for "__with_hw_handshake" if its specification is required from the user
251 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
257 /* handle config file handshake override: */
259 char *s=CFG_Get(CFG_Info, "global", "handshake");
261 if (s && (!strcasecmp(s,"software") || !strcasecmp(s,"rtscts")))
262 __with_hw_handshake=false;
263 else if (s && (!strcasecmp(s,"hardware") || !strcasecmp(s,"xonxoff")))
264 __with_hw_handshake=true;
266 fprintf(stderr,_("Unrecognized [%s] option \"%s\", use \"%s\" or \"%s\" value, ignoring!"),
267 "global","handshake","software","hardware");
269 if (__with_hw_handshake==-1) {
270 fprintf(stderr,_("[%s] option \"%s\" not found, trying to use \"%s\" value!"),
271 "global","handshake","software");
272 __with_hw_handshake=false;
278 /* O_NONBLOCK MUST be used here as the CLOCAL may be currently off
279 * and if DCD is down the "open" syscall would be stuck wating for DCD.
281 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
286 /* Initialise the port settings */
288 memcpy(&tp, &serial_termios, sizeof(struct termios));
290 /* Set port settings for canonical input processing */
292 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL;
293 if (__with_odd_parity) {
294 tp.c_cflag |= (PARENB | PARODD);
299 if (__with_hw_handshake)
300 tp.c_cflag |= CRTSCTS;
302 tp.c_cflag &= ~CRTSCTS;
309 retcode=tcflush(fd, TCIFLUSH);
311 perror("Gnokii serial_opendevice: tcflush");
316 retcode=tcsetattr(fd, TCSANOW, &tp);
318 perror("Gnokii serial_opendevice: tcsetattr");
325 char *baudratestring=CFG_Get(CFG_Info, "global", "serial_baudrate");
329 baudrate=atoi(baudratestring);
330 if (baudrate && !serial_changespeed(fd,baudrate))
333 serial_changespeed(fd,19200/*default value*/);
336 /* We need to turn off O_NONBLOCK now (we have CLOCAL set so it is safe).
337 * When we run some device script it really doesn't expect NONBLOCK!
340 retcode=fcntl(fd, F_SETFL, 0);
342 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
347 /* handle config file connect_script:
349 if (-1 == device_script(fd,"connect_script")) {
350 fprintf(stderr,"Gnokii serial_opendevice: connect_script\n");
355 /* Allow process/thread to receive SIGIO */
358 retcode = fcntl(fd, F_SETOWN, getpid());
360 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
366 /* Make filedescriptor asynchronous. */
368 /* We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
369 * by F_SETFL as a side-effect!
371 retcode=fcntl(fd, F_SETFL, (__with_async ? FASYNC : 0) | FNONBLOCK);
373 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
381 /* Set the DTR and RTS bit of the serial device. */
383 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
390 ioctl(__fd, TIOCMBIS, &flags);
392 ioctl(__fd, TIOCMBIC, &flags);
397 ioctl(__fd, TIOCMBIS, &flags);
399 ioctl(__fd, TIOCMBIC, &flags);
403 int serial_select(int fd, struct timeval *timeout) {
408 FD_SET(fd, &readfds);
410 return (select(fd + 1, &readfds, NULL, NULL, timeout));
415 /* Change the speed of the serial device.
419 bool serial_changespeed(int __fd, int __speed) {
431 case 9600: speed = B9600; break;
432 case 19200: speed = B19200; break;
433 case 38400: speed = B38400; break;
434 case 57600: speed = B57600; break;
435 case 115200: speed = B115200; break;
437 fprintf(stderr,_("Serial port speed %d not supported!\n"),__speed);
442 if (tcgetattr(__fd, &t))
445 // This is not needed! We set up the speed via cfsetspeed
446 // t.c_cflag &= ~CBAUD;
447 // t.c_cflag |= speed;
448 if (cfsetspeed(&t, speed) == -1) {
449 dprintf(_("Serial port speed setting failed\n"));
453 tcsetattr(__fd, TCSADRAIN, &t);
455 if (ioctl(__fd, TIOCGETP, &t))
461 if (ioctl(__fd, TIOCSETN, &t))
468 /* Read from serial device. */
470 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
472 return (read(__fd, __buf, __nbytes));
475 #if !defined(TIOCMGET) && defined(TIOCMODG)
476 #define TIOCMGET TIOCMODG
479 static void check_dcd(int __fd)
484 if (ioctl(__fd, TIOCMGET, &mcs) || !(mcs & TIOCM_CAR)) {
485 fprintf(stderr,_("ERROR: Modem DCD is down and global/require_dcd parameter is set!\n"));
486 exit(EXIT_FAILURE); /* Hard quit of all threads */
489 /* Impossible!! (eg. Coherent) */
493 /* Write to serial device. */
495 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
499 static long serial_write_usleep=LONG_MIN;
500 static int require_dcd=-1;
502 if (serial_write_usleep==LONG_MIN) {
503 char *s=CFG_Get(CFG_Info, "global", "serial_write_usleep");
505 serial_write_usleep=(!s ?
506 SERIAL_WRITE_USLEEP_DEFAULT : atol(CFG_Get(CFG_Info, "global", "serial_write_usleep")));
509 if (require_dcd==-1) {
510 require_dcd=(!!CFG_Get(CFG_Info, "global", "require_dcd"));
513 fprintf(stderr,_("WARNING: global/require_dcd argument was set but it is not supported on this system!\n"));
520 if (serial_write_usleep<0)
521 return(write(__fd, __buf, __n));
524 got=write(__fd, __buf, 1);
526 return((!r ? -1 : r));
530 if (serial_write_usleep)
531 usleep(serial_write_usleep);