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.2 2001/11/27 23:34:48 short
15 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
17 Revision 1.1.1.1.12.1 2001/11/25 23:31:56 short
18 WARNING! dependency on: lace_cfgreader (branchpoint), lace_utils
20 * new gnokiirc/global settings:
21 * connect_script/disconnect_script - needed for BIP but can be general
22 * entries in such sections passed as %ENV - ...
23 * provided chat script ("connect-et" now) w/o bugs of "ppp-6210-modem"
24 * serial_baudrate - used when not overriden by phone
25 * handshake = software/hardware - used when not overriden by phone
26 * require_dcd - kills Gnokii when modem drops connection - needed for BIP
27 * serial_write_usleep - waits between each character sent - for Siemens M20
28 * All open device fds are now closed and DTR/RTS-cleared on exit
29 * SIGINT abort not properly handled - clashing with pthreads
30 * Fixed hang-ons by fcntl(FASYNC) forgotting to specify also FNONBLOCK
32 Revision 1.1.1.1 2001/11/25 21:59:09 short
33 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
35 Revision 1.10 2001/11/14 10:46:12 pkot
36 Small cleanup with __unices__
38 Revision 1.9 2001/09/14 12:15:28 pkot
39 Cleanups from 0.3.3 (part1)
41 Revision 1.8 2001/08/20 23:27:37 pkot
42 Add hardware shakehand to the link layer (Manfred Jonsson)
44 Revision 1.7 2001/07/03 00:03:36 pkot
45 Small fixes to let gnokii compile and work under solaris (thanks to Artur Kubiak)
47 Revision 1.6 2001/03/21 23:36:04 chris
48 Added the statemachine
49 This will break gnokii --identify and --monitor except for 6210/7110
51 Revision 1.5 2001/03/19 23:43:46 pkot
52 Solaris/ *BSD '#if defined' cleanup
54 Revision 1.4 2001/03/13 01:21:38 pkot
55 *BSD updates (Bert Driehuis)
57 Revision 1.3 2001/03/06 22:27:46 pkot
58 Misc docs and Makefiles updates and cleanups
60 Revision 1.2 2001/02/21 19:57:05 chris
61 More fiddling with the directory layout
65 /* [global] option "serial_write_usleep" default: */
66 #define SERIAL_WRITE_USLEEP_DEFAULT (-1)
69 #include "cfgreader.h"
71 /* Do not compile this file under Win32 systems. */
77 #include <sys/ioctl.h>
82 #include <sys/types.h>
84 #include <sys/ioctl.h>
87 #include "devices/unixserial.h"
89 #ifdef HAVE_SYS_IOCTL_COMPAT_H
90 # include <sys/ioctl_compat.h>
93 #ifdef HAVE_SYS_SELECT_H
94 # include <sys/select.h>
97 /* If the target operating system does not have cfsetspeed, we can emulate
100 #ifndef HAVE_CFSETSPEED
101 # if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
102 # define cfsetspeed(t, speed) \
103 (cfsetispeed(t, speed) || cfsetospeed(t, speed))
105 static int cfsetspeed(struct termios *t, int speed)
107 # ifdef HAVE_TERMIOS_CSPEED
112 # endif /* HAVE_TERMIOS_CSPEED */
115 # endif /* HAVE_CFSETISPEED && HAVE_CFSETOSPEED */
116 #endif /* HAVE_CFSETSPEED */
119 # define O_NONBLOCK 0
122 /* Structure to backup the setting of the terminal. */
123 struct termios serial_termios;
125 /* Script handling: */
127 static void device_script_cfgfunc(const char *section, const char *key,
130 setenv(key, value, 1 /*overwrite */ ); /* errors ignored */
133 static int device_script(int fd, const char *section)
136 const char *scriptname = CFG_Get(CFG_Info, "global", section);
142 switch ((pid = fork())) {
145 _("device_script(\"%s\"): fork() failure: %s!\n"),
146 scriptname, strerror(errno));
149 default:{ /* parent */
151 if (pid == waitpid(pid, &status, 0 /*options */ )
152 && WIFEXITED(status) && !WEXITSTATUS(status))
156 ("device_script(\"%s\"): child script failure: %s, exit code=%d\n"),
158 (WIFEXITED(status) ? _("normal exit") :
160 (WIFEXITED(status) ? WEXITSTATUS(status) :
167 CFG_GetForeach(CFG_Info, section,
168 device_script_cfgfunc);
170 if (0 != dup2(fd, 0) || 1 != dup2(fd, 1)
174 ("device_script(\"%s\"): file descriptor prepare: %s\n"),
175 scriptname, strerror(errno));
178 /* FIXME: close all open descriptors - how to track them?
180 execl("/bin/sh", "sh", "-c", scriptname, NULL);
183 ("device_script(\"%s\"): execute script: %s\n"),
184 scriptname, strerror(errno));
192 int serial_close_all_openfds[0x10]; /* -1 when entry not used, fd otherwise */
193 int serial_close(int __fd);
195 static void serial_close_all(void)
199 dprintf("serial_close_all() executed\n");
200 for (i = 0; i < ARRAY_LEN(serial_close_all_openfds); i++)
201 if (serial_close_all_openfds[i] != -1)
202 serial_close(serial_close_all_openfds[i]);
205 static void unixserial_interrupted(int signo)
211 /* Open the serial port and store the settings. */
212 int serial_open(__const char *__file, int __oflag)
216 static bool atexit_registered = false;
218 if (!atexit_registered) {
219 memset(serial_close_all_openfds, -1,
220 sizeof(serial_close_all_openfds));
221 #if 0 /* Disabled for now as atexit() functions are then called multiple times for pthreads! */
222 signal(SIGINT, unixserial_interrupted);
224 atexit(serial_close_all);
225 atexit_registered = true;
228 __fd = open(__file, __oflag);
230 perror("Gnokii serial_open: open");
234 for (i = 0; i < ARRAY_LEN(serial_close_all_openfds); i++)
235 if (serial_close_all_openfds[i] == -1
236 || serial_close_all_openfds[i] == __fd) {
237 serial_close_all_openfds[i] = __fd;
241 retcode = tcgetattr(__fd, &serial_termios);
243 perror("Gnokii serial_open:tcgetattr");
244 /* Don't call serial_close since serial_termios is not valid */
252 /* Close the serial port and restore old settings. */
253 int serial_close(int __fd)
257 for (i = 0; i < ARRAY_LEN(serial_close_all_openfds); i++)
258 if (serial_close_all_openfds[i] == __fd)
259 serial_close_all_openfds[i] = -1; /* fd closed */
261 /* handle config file disconnect_script:
263 if (-1 == device_script(__fd, "disconnect_script"))
265 "Gnokii serial_close: disconnect_script\n");
269 serial_termios.c_cflag |= HUPCL; /* production == 1 */
271 serial_termios.c_cflag &= ~HUPCL; /* debugging == 0 */
274 tcsetattr(__fd, TCSANOW, &serial_termios);
277 return (close(__fd));
280 /* Open a device with standard options.
281 * Use value (-1) for "__with_hw_handshake" if its specification is required from the user
283 int serial_opendevice(__const char *__file, int __with_odd_parity,
284 int __with_async, int __with_hw_handshake)
290 /* handle config file handshake override: */
292 char *s = CFG_Get(CFG_Info, "global", "handshake");
294 if (s && (!strcasecmp(s, "software")
295 || !strcasecmp(s, "rtscts")))
296 __with_hw_handshake = false;
297 else if (s && (!strcasecmp(s, "hardware")
298 || !strcasecmp(s, "xonxoff")))
299 __with_hw_handshake = true;
303 ("Unrecognized [%s] option \"%s\", use \"%s\" or \"%s\" value, ignoring!"),
304 "global", "handshake", "software",
307 if (__with_hw_handshake == -1) {
310 ("[%s] option \"%s\" not found, trying to use \"%s\" value!"),
311 "global", "handshake", "software");
312 __with_hw_handshake = false;
318 /* O_NONBLOCK MUST be used here as the CLOCAL may be currently off
319 * and if DCD is down the "open" syscall would be stuck wating for DCD.
321 fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
326 /* Initialise the port settings */
327 memcpy(&tp, &serial_termios, sizeof(struct termios));
329 /* Set port settings for canonical input processing */
330 tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL;
331 if (__with_odd_parity) {
332 tp.c_cflag |= (PARENB | PARODD);
336 if (__with_hw_handshake)
337 tp.c_cflag |= CRTSCTS;
339 tp.c_cflag &= ~CRTSCTS;
346 retcode = tcflush(fd, TCIFLUSH);
348 perror("Gnokii serial_opendevice: tcflush");
353 retcode = tcsetattr(fd, TCSANOW, &tp);
355 perror("Gnokii serial_opendevice: tcsetattr");
362 char *baudratestring =
363 CFG_Get(CFG_Info, "global", "serial_baudrate");
367 baudrate = atoi(baudratestring);
369 && GE_NONE != serial_changespeed(fd, baudrate))
372 serial_changespeed(fd, 19200 /*default value */ );
375 /* We need to turn off O_NONBLOCK now (we have CLOCAL set so it is safe).
376 * When we run some device script it really doesn't expect NONBLOCK!
379 retcode = fcntl(fd, F_SETFL, 0);
381 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
386 /* handle config file connect_script:
388 if (-1 == device_script(fd, "connect_script")) {
390 "Gnokii serial_opendevice: connect_script\n");
395 /* Allow process/thread to receive SIGIO */
398 retcode = fcntl(fd, F_SETOWN, getpid());
400 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
406 /* Make filedescriptor asynchronous. */
408 /* We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
409 * by F_SETFL as a side-effect!
412 fcntl(fd, F_SETFL, (__with_async ? FASYNC : 0) | FNONBLOCK);
414 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
422 /* Set the DTR and RTS bit of the serial device. */
423 void serial_setdtrrts(int __fd, int __dtr, int __rts)
430 ioctl(__fd, TIOCMBIS, &flags);
432 ioctl(__fd, TIOCMBIC, &flags);
437 ioctl(__fd, TIOCMBIS, &flags);
439 ioctl(__fd, TIOCMBIC, &flags);
443 int serial_select(int fd, struct timeval *timeout)
448 FD_SET(fd, &readfds);
450 return (select(fd + 1, &readfds, NULL, NULL, timeout));
454 /* Change the speed of the serial device.
457 GSM_Error serial_changespeed(int __fd, int __speed)
459 GSM_Error retcode = true;
485 fprintf(stderr, _("Serial port speed %d not supported!\n"),
487 return (GE_NOTSUPPORTED);
491 if (tcgetattr(__fd, &t))
492 retcode = GE_INTERNALERROR;
494 // This is not needed! We set up the speed via cfsetspeed
495 // t.c_cflag &= ~CBAUD;
496 // t.c_cflag |= speed;
497 if (cfsetspeed(&t, speed) == -1) {
498 dprintf(_("Serial port speed setting failed\n"));
499 retcode = GE_INTERNALERROR;
502 tcsetattr(__fd, TCSADRAIN, &t);
504 if (ioctl(__fd, TIOCGETP, &t))
505 retcode = GE_INTERNALERROR;
510 if (ioctl(__fd, TIOCSETN, &t))
511 retcode = GE_INTERNALERROR;
517 /* Read from serial device. */
519 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes)
521 return (read(__fd, __buf, __nbytes));
524 #if !defined(TIOCMGET) && defined(TIOCMODG)
525 #define TIOCMGET TIOCMODG
528 static void check_dcd(int __fd)
533 if (ioctl(__fd, TIOCMGET, &mcs) || !(mcs & TIOCM_CAR)) {
536 ("ERROR: Modem DCD is down and global/require_dcd parameter is set!\n"));
537 exit(EXIT_FAILURE); /* Hard quit of all threads */
540 /* Impossible!! (eg. Coherent) */
544 /* Write to serial device. */
546 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n)
551 static long serial_write_usleep = LONG_MIN;
552 static int require_dcd = -1;
554 if (serial_write_usleep == LONG_MIN) {
556 CFG_Get(CFG_Info, "global", "serial_write_usleep");
558 serial_write_usleep = (!s ?
559 SERIAL_WRITE_USLEEP_DEFAULT :
562 "serial_write_usleep")));
565 if (require_dcd == -1) {
567 (!!CFG_Get(CFG_Info, "global", "require_dcd"));
572 ("WARNING: global/require_dcd argument was set but it is not supported on this system!\n"));
579 if (serial_write_usleep < 0)
580 return (write(__fd, __buf, __n));
583 got = write(__fd, __buf, 1);
585 return ((!r ? -1 : r));
589 if (serial_write_usleep)
590 usleep(serial_write_usleep);