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.1.12.1 2001/11/25 23:31:56 short
15 WARNING! dependency on: lace_cfgreader (branchpoint), lace_utils
17 * new gnokiirc/global settings:
18 * connect_script/disconnect_script - needed for BIP but can be general
19 * entries in such sections passed as %ENV - ...
20 * provided chat script ("connect-et" now) w/o bugs of "ppp-6210-modem"
21 * serial_baudrate - used when not overriden by phone
22 * handshake = software/hardware - used when not overriden by phone
23 * require_dcd - kills Gnokii when modem drops connection - needed for BIP
24 * serial_write_usleep - waits between each character sent - for Siemens M20
25 * All open device fds are now closed and DTR/RTS-cleared on exit
26 * SIGINT abort not properly handled - clashing with pthreads
27 * Fixed hang-ons by fcntl(FASYNC) forgotting to specify also FNONBLOCK
29 Revision 1.1.1.1 2001/11/25 21:59:09 short
30 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
32 Revision 1.10 2001/11/14 10:46:12 pkot
33 Small cleanup with __unices__
35 Revision 1.9 2001/09/14 12:15:28 pkot
36 Cleanups from 0.3.3 (part1)
38 Revision 1.8 2001/08/20 23:27:37 pkot
39 Add hardware shakehand to the link layer (Manfred Jonsson)
41 Revision 1.7 2001/07/03 00:03:36 pkot
42 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
44 Revision 1.6 2001/03/21 23:36:04 chris
45 Added the statemachine
46 This will break gnokii --identify and --monitor except for 6210/7110
48 Revision 1.5 2001/03/19 23:43:46 pkot
49 Solaris/ *BSD '#if defined' cleanup
51 Revision 1.4 2001/03/13 01:21:38 pkot
52 *BSD updates (Bert Driehuis)
54 Revision 1.3 2001/03/06 22:27:46 pkot
55 Misc docs and Makefiles updates and cleanups
57 Revision 1.2 2001/02/21 19:57:05 chris
58 More fiddling with the directory layout
62 /* [global] option "serial_write_usleep" default: */
63 #define SERIAL_WRITE_USLEEP_DEFAULT (-1)
66 #include "cfgreader.h"
68 /* Do not compile this file under Win32 systems. */
74 #include <sys/ioctl.h>
79 #include <sys/types.h>
81 #include <sys/ioctl.h>
84 #include "devices/unixserial.h"
86 #ifdef HAVE_SYS_IOCTL_COMPAT_H
87 #include <sys/ioctl_compat.h>
90 #ifdef HAVE_SYS_SELECT_H
91 #include <sys/select.h>
94 /* If the target operating system does not have cfsetspeed, we can emulate
97 #ifndef HAVE_CFSETSPEED
98 #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
99 #define cfsetspeed(t, speed) \
100 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
102 static int cfsetspeed(struct termios *t, int speed) {
103 #ifdef HAVE_TERMIOS_CSPEED
118 /* Structure to backup the setting of the terminal. */
120 struct termios serial_termios;
122 /* Script handling: */
124 static void device_script_cfgfunc(const char *section,const char *key,const char *value)
126 setenv(key,value,1/*overwrite*/); /* errors ignored */
129 static int device_script(int fd, const char *section)
132 const char *scriptname = CFG_Get(CFG_Info, "global", section);
138 switch ((pid=fork())) {
140 fprintf(stderr,_("device_script(\"%s\"): fork() failure: %s!\n"),scriptname,strerror(errno));
143 default: { /* parent */
145 if (pid==waitpid(pid,&status,0/*options*/) && WIFEXITED(status) && !WEXITSTATUS(status))
147 fprintf(stderr,_("device_script(\"%s\"): child script failure: %s, exit code=%d\n"),scriptname,
148 (WIFEXITED(status) ? _("normal exit") : _("abnormal exit")),
149 (WIFEXITED(status) ? WEXITSTATUS(status) : -1));
154 case 0: { /* child */
155 CFG_GetForeach(CFG_Info,section,device_script_cfgfunc);
157 if (0!=dup2(fd,0) || 1!=dup2(fd,1) || close(fd)) {
158 fprintf(stderr,_("device_script(\"%s\"): file descriptor prepare: %s\n"),scriptname,strerror(errno));
161 /* FIXME: close all open descriptors - how to track them?
163 execl("/bin/sh","sh","-c",scriptname,NULL);
164 fprintf(stderr,_("device_script(\"%s\"): execute script: %s\n"),scriptname,strerror(errno));
172 int serial_close_all_openfds[0x10]; /* -1 when entry not used, fd otherwise */
173 int serial_close(int __fd);
175 static void serial_close_all(void)
179 dprintf("serial_close_all() executed\n");
180 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
181 if (serial_close_all_openfds[i]!=-1)
182 serial_close(serial_close_all_openfds[i]);
185 static void unixserial_interrupted(int signo)
191 /* Open the serial port and store the settings. */
193 int serial_open(__const char *__file, int __oflag) {
197 static bool atexit_registered=false;
199 if (!atexit_registered) {
200 memset(serial_close_all_openfds,-1,sizeof(serial_close_all_openfds));
201 #if 0 /* Disabled for now as atexit() functions are then called multiple times for pthreads! */
202 signal(SIGINT,unixserial_interrupted);
204 atexit(serial_close_all);
205 atexit_registered=true;
208 __fd = open(__file, __oflag);
210 perror("Gnokii serial_open: open");
214 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
215 if (serial_close_all_openfds[i]==-1 || serial_close_all_openfds[i]==__fd) {
216 serial_close_all_openfds[i]=__fd;
220 retcode=tcgetattr(__fd, &serial_termios);
222 perror("Gnokii serial_open:tcgetattr");
223 /* Don't call serial_close since serial_termios is not valid */
231 /* Close the serial port and restore old settings. */
233 int serial_close(int __fd) {
236 for (i=0;i<ARRAY_LEN(serial_close_all_openfds);i++)
237 if (serial_close_all_openfds[i]==__fd)
238 serial_close_all_openfds[i]=-1; /* fd closed */
240 /* handle config file disconnect_script:
242 if (-1 == device_script(__fd,"disconnect_script"))
243 fprintf(stderr,"Gnokii serial_close: disconnect_script\n");
247 serial_termios.c_cflag |= HUPCL; /* production == 1 */
249 serial_termios.c_cflag &= ~HUPCL; /* debugging == 0 */
252 tcsetattr(__fd, TCSANOW, &serial_termios);
255 return (close(__fd));
258 /* Open a device with standard options.
259 * Use value (-1) for "__with_hw_handshake" if its specification is required from the user
261 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
267 /* handle config file handshake override: */
269 char *s=CFG_Get(CFG_Info, "global", "handshake");
271 if (s && (!strcasecmp(s,"software") || !strcasecmp(s,"rtscts")))
272 __with_hw_handshake=false;
273 else if (s && (!strcasecmp(s,"hardware") || !strcasecmp(s,"xonxoff")))
274 __with_hw_handshake=true;
276 fprintf(stderr,_("Unrecognized [%s] option \"%s\", use \"%s\" or \"%s\" value, ignoring!"),
277 "global","handshake","software","hardware");
279 if (__with_hw_handshake==-1) {
280 fprintf(stderr,_("[%s] option \"%s\" not found, trying to use \"%s\" value!"),
281 "global","handshake","software");
282 __with_hw_handshake=false;
288 /* O_NONBLOCK MUST be used here as the CLOCAL may be currently off
289 * and if DCD is down the "open" syscall would be stuck wating for DCD.
291 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
296 /* Initialise the port settings */
298 memcpy(&tp, &serial_termios, sizeof(struct termios));
300 /* Set port settings for canonical input processing */
302 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL;
303 if (__with_odd_parity) {
304 tp.c_cflag |= (PARENB | PARODD);
309 if (__with_hw_handshake)
310 tp.c_cflag |= CRTSCTS;
312 tp.c_cflag &= ~CRTSCTS;
319 retcode=tcflush(fd, TCIFLUSH);
321 perror("Gnokii serial_opendevice: tcflush");
326 retcode=tcsetattr(fd, TCSANOW, &tp);
328 perror("Gnokii serial_opendevice: tcsetattr");
335 char *baudratestring=CFG_Get(CFG_Info, "global", "serial_baudrate");
339 baudrate=atoi(baudratestring);
340 if (baudrate && GE_NONE!=serial_changespeed(fd,baudrate))
343 serial_changespeed(fd,19200/*default value*/);
346 /* We need to turn off O_NONBLOCK now (we have CLOCAL set so it is safe).
347 * When we run some device script it really doesn't expect NONBLOCK!
350 retcode=fcntl(fd, F_SETFL, 0);
352 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
357 /* handle config file connect_script:
359 if (-1 == device_script(fd,"connect_script")) {
360 fprintf(stderr,"Gnokii serial_opendevice: connect_script\n");
365 /* Allow process/thread to receive SIGIO */
368 retcode = fcntl(fd, F_SETOWN, getpid());
370 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
376 /* Make filedescriptor asynchronous. */
378 /* We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
379 * by F_SETFL as a side-effect!
381 retcode=fcntl(fd, F_SETFL, (__with_async ? FASYNC : 0) | FNONBLOCK);
383 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
391 /* Set the DTR and RTS bit of the serial device. */
393 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
400 ioctl(__fd, TIOCMBIS, &flags);
402 ioctl(__fd, TIOCMBIC, &flags);
407 ioctl(__fd, TIOCMBIS, &flags);
409 ioctl(__fd, TIOCMBIC, &flags);
413 int serial_select(int fd, struct timeval *timeout) {
418 FD_SET(fd, &readfds);
420 return (select(fd + 1, &readfds, NULL, NULL, timeout));
425 /* Change the speed of the serial device.
429 GSM_Error serial_changespeed(int __fd, int __speed) {
430 GSM_Error retcode = true;
441 case 9600: speed = B9600; break;
442 case 19200: speed = B19200; break;
443 case 38400: speed = B38400; break;
444 case 57600: speed = B57600; break;
445 case 115200: speed = B115200; break;
447 fprintf(stderr,_("Serial port speed %d not supported!\n"),__speed);
448 return(GE_NOTSUPPORTED);
452 if (tcgetattr(__fd, &t))
453 retcode = GE_INTERNALERROR;
455 // This is not needed! We set up the speed via cfsetspeed
456 // t.c_cflag &= ~CBAUD;
457 // t.c_cflag |= speed;
458 if (cfsetspeed(&t, speed) == -1) {
459 dprintf(_("Serial port speed setting failed\n"));
460 retcode = GE_INTERNALERROR;
463 tcsetattr(__fd, TCSADRAIN, &t);
465 if (ioctl(__fd, TIOCGETP, &t))
466 retcode = GE_INTERNALERROR;
471 if (ioctl(__fd, TIOCSETN, &t))
472 retcode = GE_INTERNALERROR;
478 /* Read from serial device. */
480 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
482 return (read(__fd, __buf, __nbytes));
485 #if !defined(TIOCMGET) && defined(TIOCMODG)
486 #define TIOCMGET TIOCMODG
489 static void check_dcd(int __fd)
494 if (ioctl(__fd, TIOCMGET, &mcs) || !(mcs & TIOCM_CAR)) {
495 fprintf(stderr,_("ERROR: Modem DCD is down and global/require_dcd parameter is set!\n"));
496 exit(EXIT_FAILURE); /* Hard quit of all threads */
499 /* Impossible!! (eg. Coherent) */
503 /* Write to serial device. */
505 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
509 static long serial_write_usleep=LONG_MIN;
510 static int require_dcd=-1;
512 if (serial_write_usleep==LONG_MIN) {
513 char *s=CFG_Get(CFG_Info, "global", "serial_write_usleep");
515 serial_write_usleep=(!s ?
516 SERIAL_WRITE_USLEEP_DEFAULT : atol(CFG_Get(CFG_Info, "global", "serial_write_usleep")));
519 if (require_dcd==-1) {
520 require_dcd=(!!CFG_Get(CFG_Info, "global", "require_dcd"));
523 fprintf(stderr,_("WARNING: global/require_dcd argument was set but it is not supported on this system!\n"));
530 if (serial_write_usleep<0)
531 return(write(__fd, __buf, __n));
534 got=write(__fd, __buf, 1);
536 return((!r ? -1 : r));
540 if (serial_write_usleep)
541 usleep(serial_write_usleep);