Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
[gnokii.git] / common / devices / unixserial.c
1 /*
2   
3   $Id$
4
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   $Log$
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
16
17   Revision 1.1.1.1.12.1  2001/11/25 23:31:56  short
18   WARNING! dependency on: lace_cfgreader (branchpoint), lace_utils
19
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
31
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
34
35   Revision 1.10  2001/11/14 10:46:12  pkot
36   Small cleanup with __unices__
37
38   Revision 1.9  2001/09/14 12:15:28  pkot
39   Cleanups from 0.3.3 (part1)
40
41   Revision 1.8  2001/08/20 23:27:37  pkot
42   Add hardware shakehand to the link layer (Manfred Jonsson)
43
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)
46
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
50
51   Revision 1.5  2001/03/19 23:43:46  pkot
52   Solaris/ *BSD '#if defined' cleanup
53
54   Revision 1.4  2001/03/13 01:21:38  pkot
55   *BSD updates (Bert Driehuis)
56
57   Revision 1.3  2001/03/06 22:27:46  pkot
58   Misc docs and Makefiles updates and cleanups
59
60   Revision 1.2  2001/02/21 19:57:05  chris
61   More fiddling with the directory layout
62
63 */
64
65 /* [global] option "serial_write_usleep" default: */
66 #define SERIAL_WRITE_USLEEP_DEFAULT (-1)
67
68 #include "misc.h"
69 #include "cfgreader.h"
70
71 /* Do not compile this file under Win32 systems. */
72
73 #ifndef WIN32
74
75 #include <stdio.h>
76 #include <fcntl.h>
77 #include <sys/ioctl.h>
78 #include <string.h>
79 #include <limits.h>
80 #include <stdlib.h>
81 #include <errno.h>
82 #include <sys/types.h>
83 #include <sys/wait.h>
84 #include <sys/ioctl.h>
85
86 #include <termios.h>
87 #include "devices/unixserial.h"
88
89 #ifdef HAVE_SYS_IOCTL_COMPAT_H
90 #  include <sys/ioctl_compat.h>
91 #endif
92
93 #ifdef HAVE_SYS_SELECT_H
94 #  include <sys/select.h>
95 #endif
96
97 /* If the target operating system does not have cfsetspeed, we can emulate
98    it. */
99
100 #ifndef HAVE_CFSETSPEED
101 #  if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
102 #    define cfsetspeed(t, speed) \
103             (cfsetispeed(t, speed) || cfsetospeed(t, speed))
104 #  else
105 static int cfsetspeed(struct termios *t, int speed)
106 {
107 #    ifdef HAVE_TERMIOS_CSPEED
108         t->c_ispeed = speed;
109         t->c_ospeed = speed;
110 #    else
111         t->c_cflag |= speed;
112 #    endif                      /* HAVE_TERMIOS_CSPEED */
113         return 0;
114 }
115 #  endif                        /* HAVE_CFSETISPEED && HAVE_CFSETOSPEED */
116 #endif                          /* HAVE_CFSETSPEED */
117
118 #ifndef O_NONBLOCK
119 #  define O_NONBLOCK  0
120 #endif
121
122 /* Structure to backup the setting of the terminal. */
123 struct termios serial_termios;
124
125 /* Script handling: */
126
127 static void device_script_cfgfunc(const char *section, const char *key,
128                                   const char *value)
129 {
130         setenv(key, value, 1 /*overwrite */ );  /* errors ignored */
131 }
132
133 static int device_script(int fd, const char *section)
134 {
135         pid_t pid;
136         const char *scriptname = CFG_Get(CFG_Info, "global", section);
137
138         if (!scriptname)
139                 return (0);
140
141         errno = 0;
142         switch ((pid = fork())) {
143         case -1:
144                 fprintf(stderr,
145                         _("device_script(\"%s\"): fork() failure: %s!\n"),
146                         scriptname, strerror(errno));
147                 return (-1);
148
149         default:{               /* parent */
150                         int status;
151                         if (pid == waitpid(pid, &status, 0 /*options */ )
152                             && WIFEXITED(status) && !WEXITSTATUS(status))
153                                 return (0);
154                         fprintf(stderr,
155                                 _
156                                 ("device_script(\"%s\"): child script failure: %s, exit code=%d\n"),
157                                 scriptname,
158                                 (WIFEXITED(status) ? _("normal exit") :
159                                  _("abnormal exit")),
160                                 (WIFEXITED(status) ? WEXITSTATUS(status) :
161                                  -1));
162                         errno = EIO;
163                         return (-1);
164                 }
165
166         case 0:{                /* child */
167                         CFG_GetForeach(CFG_Info, section,
168                                        device_script_cfgfunc);
169                         errno = 0;
170                         if (0 != dup2(fd, 0) || 1 != dup2(fd, 1)
171                             || close(fd)) {
172                                 fprintf(stderr,
173                                         _
174                                         ("device_script(\"%s\"): file descriptor prepare: %s\n"),
175                                         scriptname, strerror(errno));
176                                 _exit(-1);
177                         }
178                         /* FIXME: close all open descriptors - how to track them?
179                          */
180                         execl("/bin/sh", "sh", "-c", scriptname, NULL);
181                         fprintf(stderr,
182                                 _
183                                 ("device_script(\"%s\"): execute script: %s\n"),
184                                 scriptname, strerror(errno));
185                         _exit(-1);
186                         /* NOTREACHED */
187                 }
188         }
189         /* NOTREACHED */
190 }
191
192 int serial_close_all_openfds[0x10];     /* -1 when entry not used, fd otherwise */
193 int serial_close(int __fd);
194
195 static void serial_close_all(void)
196 {
197         int i;
198
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]);
203 }
204
205 static void unixserial_interrupted(int signo)
206 {
207         exit(EXIT_FAILURE);
208         /* NOTREACHED */
209 }
210
211 /* Open the serial port and store the settings. */
212 int serial_open(__const char *__file, int __oflag)
213 {
214         int __fd;
215         int retcode, i;
216         static bool atexit_registered = false;
217
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);
223 #endif
224                 atexit(serial_close_all);
225                 atexit_registered = true;
226         }
227
228         __fd = open(__file, __oflag);
229         if (__fd == -1) {
230                 perror("Gnokii serial_open: open");
231                 return (-1);
232         }
233
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;
238                         break;
239                 }
240
241         retcode = tcgetattr(__fd, &serial_termios);
242         if (retcode == -1) {
243                 perror("Gnokii serial_open:tcgetattr");
244                 /* Don't call serial_close since serial_termios is not valid */
245                 close(__fd);
246                 return (-1);
247         }
248
249         return __fd;
250 }
251
252 /* Close the serial port and restore old settings. */
253 int serial_close(int __fd)
254 {
255         int i;
256
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 */
260
261         /* handle config file disconnect_script:
262          */
263         if (-1 == device_script(__fd, "disconnect_script"))
264                 fprintf(stderr,
265                         "Gnokii serial_close: disconnect_script\n");
266
267         if (__fd >= 0) {
268 #if 1                           /* HACK */
269                 serial_termios.c_cflag |= HUPCL;        /* production == 1 */
270 #else
271                 serial_termios.c_cflag &= ~HUPCL;       /* debugging  == 0 */
272 #endif
273
274                 tcsetattr(__fd, TCSANOW, &serial_termios);
275         }
276
277         return (close(__fd));
278 }
279
280 /* Open a device with standard options.
281  * Use value (-1) for "__with_hw_handshake" if its specification is required from the user
282  */
283 int serial_opendevice(__const char *__file, int __with_odd_parity,
284                       int __with_async, int __with_hw_handshake)
285 {
286         int fd;
287         int retcode;
288         struct termios tp;
289
290         /* handle config file handshake override: */
291         {
292                 char *s = CFG_Get(CFG_Info, "global", "handshake");
293
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;
300                 else if (s)
301                         fprintf(stderr,
302                                 _
303                                 ("Unrecognized [%s] option \"%s\", use \"%s\" or \"%s\" value, ignoring!"),
304                                 "global", "handshake", "software",
305                                 "hardware");
306
307                 if (__with_hw_handshake == -1) {
308                         fprintf(stderr,
309                                 _
310                                 ("[%s] option \"%s\" not found, trying to use \"%s\" value!"),
311                                 "global", "handshake", "software");
312                         __with_hw_handshake = false;
313                 }
314         }
315
316         /* Open device */
317
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.
320          */
321         fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
322
323         if (fd < 0)
324                 return fd;
325
326         /* Initialise the port settings */
327         memcpy(&tp, &serial_termios, sizeof(struct termios));
328
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);
333                 tp.c_iflag = 0;
334         } else
335                 tp.c_iflag = IGNPAR;
336         if (__with_hw_handshake)
337                 tp.c_cflag |= CRTSCTS;
338         else
339                 tp.c_cflag &= ~CRTSCTS;
340
341         tp.c_oflag = 0;
342         tp.c_lflag = 0;
343         tp.c_cc[VMIN] = 1;
344         tp.c_cc[VTIME] = 0;
345
346         retcode = tcflush(fd, TCIFLUSH);
347         if (retcode == -1) {
348                 perror("Gnokii serial_opendevice: tcflush");
349                 serial_close(fd);
350                 return (-1);
351         }
352
353         retcode = tcsetattr(fd, TCSANOW, &tp);
354         if (retcode == -1) {
355                 perror("Gnokii serial_opendevice: tcsetattr");
356                 serial_close(fd);
357                 return (-1);
358         }
359
360         /* Set speed */
361         {
362                 char *baudratestring =
363                     CFG_Get(CFG_Info, "global", "serial_baudrate");
364                 int baudrate = 0;
365
366                 if (baudratestring)
367                         baudrate = atoi(baudratestring);
368                 if (baudrate
369                     && GE_NONE != serial_changespeed(fd, baudrate))
370                         baudrate = 0;
371                 if (!baudrate)
372                         serial_changespeed(fd, 19200 /*default value */ );
373         }
374
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!
377          */
378
379         retcode = fcntl(fd, F_SETFL, 0);
380         if (retcode == -1) {
381                 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
382                 serial_close(fd);
383                 return (-1);
384         }
385
386         /* handle config file connect_script:
387          */
388         if (-1 == device_script(fd, "connect_script")) {
389                 fprintf(stderr,
390                         "Gnokii serial_opendevice: connect_script\n");
391                 serial_close(fd);
392                 return (-1);
393         }
394
395         /* Allow process/thread to receive SIGIO */
396
397 #if !(__unices__)
398         retcode = fcntl(fd, F_SETOWN, getpid());
399         if (retcode == -1) {
400                 perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
401                 serial_close(fd);
402                 return (-1);
403         }
404 #endif
405
406         /* Make filedescriptor asynchronous. */
407
408         /* We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
409          * by F_SETFL as a side-effect!
410          */
411         retcode =
412             fcntl(fd, F_SETFL, (__with_async ? FASYNC : 0) | FNONBLOCK);
413         if (retcode == -1) {
414                 perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
415                 serial_close(fd);
416                 return (-1);
417         }
418
419         return fd;
420 }
421
422 /* Set the DTR and RTS bit of the serial device. */
423 void serial_setdtrrts(int __fd, int __dtr, int __rts)
424 {
425         unsigned int flags;
426
427         flags = TIOCM_DTR;
428
429         if (__dtr)
430                 ioctl(__fd, TIOCMBIS, &flags);
431         else
432                 ioctl(__fd, TIOCMBIC, &flags);
433
434         flags = TIOCM_RTS;
435
436         if (__rts)
437                 ioctl(__fd, TIOCMBIS, &flags);
438         else
439                 ioctl(__fd, TIOCMBIC, &flags);
440 }
441
442
443 int serial_select(int fd, struct timeval *timeout)
444 {
445         fd_set readfds;
446
447         FD_ZERO(&readfds);
448         FD_SET(fd, &readfds);
449
450         return (select(fd + 1, &readfds, NULL, NULL, timeout));
451 }
452
453
454 /* Change the speed of the serial device.
455  * RETURNS: Success
456  */
457 GSM_Error serial_changespeed(int __fd, int __speed)
458 {
459         GSM_Error retcode = true;
460
461 #ifndef SGTTY
462         struct termios t;
463 #else
464         struct sgttyb t;
465 #endif
466         int speed = B9600;
467
468         switch (__speed) {
469         case 9600:
470                 speed = B9600;
471                 break;
472         case 19200:
473                 speed = B19200;
474                 break;
475         case 38400:
476                 speed = B38400;
477                 break;
478         case 57600:
479                 speed = B57600;
480                 break;
481         case 115200:
482                 speed = B115200;
483                 break;
484         default:
485                 fprintf(stderr, _("Serial port speed %d not supported!\n"),
486                         __speed);
487                 return (GE_NOTSUPPORTED);
488         }
489
490 #ifndef SGTTY
491         if (tcgetattr(__fd, &t))
492                 retcode = GE_INTERNALERROR;
493
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;
500         }
501
502         tcsetattr(__fd, TCSADRAIN, &t);
503 #else
504         if (ioctl(__fd, TIOCGETP, &t))
505                 retcode = GE_INTERNALERROR;
506
507         t.sg_ispeed = speed;
508         t.sg_ospeed = speed;
509
510         if (ioctl(__fd, TIOCSETN, &t))
511                 retcode = GE_INTERNALERROR;
512 #endif
513
514         return (retcode);
515 }
516
517 /* Read from serial device. */
518
519 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes)
520 {
521         return (read(__fd, __buf, __nbytes));
522 }
523
524 #if !defined(TIOCMGET) && defined(TIOCMODG)
525 #define TIOCMGET TIOCMODG
526 #endif
527
528 static void check_dcd(int __fd)
529 {
530 #ifdef TIOCMGET
531         int mcs;
532
533         if (ioctl(__fd, TIOCMGET, &mcs) || !(mcs & TIOCM_CAR)) {
534                 fprintf(stderr,
535                         _
536                         ("ERROR: Modem DCD is down and global/require_dcd parameter is set!\n"));
537                 exit(EXIT_FAILURE);     /* Hard quit of all threads */
538         }
539 #else
540         /* Impossible!! (eg. Coherent) */
541 #endif
542 }
543
544 /* Write to serial device. */
545
546 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n)
547 {
548
549         size_t r = 0;
550         ssize_t got;
551         static long serial_write_usleep = LONG_MIN;
552         static int require_dcd = -1;
553
554         if (serial_write_usleep == LONG_MIN) {
555                 char *s =
556                     CFG_Get(CFG_Info, "global", "serial_write_usleep");
557
558                 serial_write_usleep = (!s ?
559                                        SERIAL_WRITE_USLEEP_DEFAULT :
560                                        atol(CFG_Get
561                                             (CFG_Info, "global",
562                                              "serial_write_usleep")));
563         }
564
565         if (require_dcd == -1) {
566                 require_dcd =
567                     (!!CFG_Get(CFG_Info, "global", "require_dcd"));
568 #ifndef TIOCMGET
569                 if (require_dcd)
570                         fprintf(stderr,
571                                 _
572                                 ("WARNING: global/require_dcd argument was set but it is not supported on this system!\n"));
573 #endif
574         }
575
576         if (require_dcd)
577                 check_dcd(__fd);
578
579         if (serial_write_usleep < 0)
580                 return (write(__fd, __buf, __n));
581
582         while (__n > 0) {
583                 got = write(__fd, __buf, 1);
584                 if (got <= 0)
585                         return ((!r ? -1 : r));
586                 __buf++;
587                 __n--;
588                 r++;
589                 if (serial_write_usleep)
590                         usleep(serial_write_usleep);
591         }
592         return (r);
593 }
594
595 #endif                          /* WIN32 */