9594d890e21e20a5a5c3140fa78c5a299139262e
[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.5  2002/04/03 00:08:07  short
15   Found in "gnokii-working" directory, some November-patches version
16
17   Revision 1.9  2001/09/14 12:15:28  pkot
18   Cleanups from 0.3.3 (part1)
19
20   Revision 1.8  2001/08/20 23:27:37  pkot
21   Add hardware shakehand to the link layer (Manfred Jonsson)
22
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)
25
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
29
30   Revision 1.5  2001/03/19 23:43:46  pkot
31   Solaris/ *BSD '#if defined' cleanup
32
33   Revision 1.4  2001/03/13 01:21:38  pkot
34   *BSD updates (Bert Driehuis)
35
36   Revision 1.3  2001/03/06 22:27:46  pkot
37   Misc docs and Makefiles updates and cleanups
38
39   Revision 1.2  2001/02/21 19:57:05  chris
40   More fiddling with the directory layout
41
42
43 */
44
45 /* [global] option "serial_write_usleep" default: */
46 #define SERIAL_WRITE_USLEEP_DEFAULT (-1)
47
48 #include "misc.h"
49 #include "cfgreader.h"
50
51 /* Do not compile this file under Win32 systems. */
52
53 #ifndef WIN32
54
55 #include <stdio.h>
56 #include <fcntl.h>
57 #include <sys/ioctl.h>
58 #include <string.h>
59 #include <limits.h>
60 #include <stdlib.h>
61 #include <errno.h>
62 #include <sys/types.h>
63 #include <sys/wait.h>
64 #include <sys/ioctl.h>
65
66 #if __unices__
67 #  include <sys/file.h>
68 #endif
69
70 #include <termios.h>
71 #include "devices/unixserial.h"
72
73 #ifdef HAVE_SYS_IOCTL_COMPAT_H
74   #include <sys/ioctl_compat.h>
75 #endif
76
77 #ifdef HAVE_SYS_SELECT_H
78 #include <sys/select.h>
79 #endif
80
81 /* If the target operating system does not have cfsetspeed, we can emulate
82    it. */
83
84 #ifndef HAVE_CFSETSPEED
85   #if defined(HAVE_CFSETISPEED) && defined(HAVE_CFSETOSPEED)
86      #define cfsetspeed(t, speed) \
87      (cfsetispeed(t, speed) || cfsetospeed(t, speed))
88   #else
89     static int cfsetspeed(struct termios *t, int speed) {
90     #ifdef HAVE_TERMIOS_CSPEED
91       t->c_ispeed = speed;
92       t->c_ospeed = speed;
93     #else
94       t->c_cflag |= speed;
95     #endif
96       return 0;
97     }
98   #endif
99 #endif
100
101 #ifndef O_NONBLOCK
102   #define O_NONBLOCK  0
103 #endif
104
105 /* Structure to backup the setting of the terminal. */
106
107 struct termios serial_termios;
108
109 /* Script handling: */
110
111 static void device_script_cfgfunc(const char *section,const char *key,const char *value)
112 {
113   setenv(key,value,1/*overwrite*/);     /* errors ignored */
114 }
115
116 static int device_script(int fd, const char *section)
117 {
118 pid_t pid;
119 const char *scriptname = CFG_Get(CFG_Info, "global", section);
120
121   if (!scriptname)
122     return(0);
123
124   errno=0;
125   switch ((pid=fork())) {
126     case -1:
127       fprintf(stderr,_("device_script(\"%s\"): fork() failure: %s!\n"),scriptname,strerror(errno));
128       return(-1);
129
130     default: {  /* parent */
131 int status;
132       if (pid==waitpid(pid,&status,0/*options*/) && WIFEXITED(status) && !WEXITSTATUS(status))
133         return(0);
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));
137       errno=EIO;
138       return(-1);
139       }
140
141     case 0: {   /* child */
142       CFG_GetForeach(CFG_Info,section,device_script_cfgfunc);
143       errno=0;
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));
146         _exit(-1);
147         }
148       /* FIXME: close all open descriptors - how to track them?
149        */
150       execl("/bin/sh","sh","-c",scriptname,NULL);
151       fprintf(stderr,_("device_script(\"%s\"): execute script: %s\n"),scriptname,strerror(errno));
152       _exit(-1);
153       /* NOTREACHED */
154       }
155     }
156   /* NOTREACHED */
157 }
158
159 int serial_close_all_openfds[0x10];     /* -1 when entry not used, fd otherwise */
160 int serial_close(int __fd);
161
162 static void serial_close_all(void)
163 {
164   int i;
165
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]);
170 }
171
172 static void unixserial_interrupted(int signo)
173 {
174         exit(EXIT_FAILURE);
175         /* NOTREACHED */
176 }
177
178 /* Open the serial port and store the settings. */
179
180 int serial_open(__const char *__file, int __oflag) {
181
182   int __fd;
183   int retcode,i;
184   static bool atexit_registered=false;
185
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);
190 #endif
191     atexit(serial_close_all);
192     atexit_registered=true;
193     }
194
195   __fd = open(__file, __oflag);
196   if (__fd == -1) {
197     perror("Gnokii serial_open: open");
198     return (-1);
199   }
200
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;
204       break;
205       }
206
207   retcode=tcgetattr(__fd, &serial_termios);
208   if(retcode==-1) {
209     perror("Gnokii serial_open:tcgetattr");
210     /* Don't call serial_close since serial_termios is not valid */
211     close(__fd);
212     return(-1);
213   }
214   
215   return __fd;
216 }
217
218 /* Close the serial port and restore old settings. */
219
220 int serial_close(int __fd) {
221   int i;
222
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 */
226
227   /* handle config file disconnect_script:
228    */
229   if (-1 == device_script(__fd,"disconnect_script"))
230     fprintf(stderr,"Gnokii serial_close: disconnect_script\n");
231
232   if (__fd >= 0) {
233 #if 1 /* HACK */
234     serial_termios.c_cflag |= HUPCL;    /* production == 1 */
235 #else
236     serial_termios.c_cflag &= ~HUPCL;   /* debugging  == 0 */
237 #endif
238
239     tcsetattr(__fd, TCSANOW, &serial_termios);
240     }
241
242   return (close(__fd));
243 }
244
245 /* Open a device with standard options.
246  * Use value (-1) for "__with_hw_handshake" if its specification is required from the user
247  */
248 int serial_opendevice(__const char *__file, int __with_odd_parity, int __with_async, int __with_hw_handshake) {
249
250   int fd;
251   int retcode;
252   struct termios tp;
253
254   /* handle config file handshake override: */
255   {
256 char *s=CFG_Get(CFG_Info, "global", "handshake");
257
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;
262     else if (s)
263       fprintf(stderr,_("Unrecognized [%s] option \"%s\", use \"%s\" or \"%s\" value, ignoring!"),
264           "global","handshake","software","hardware");
265
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;
270       }
271   }
272
273   /* Open device */
274
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.
277    */
278   fd = serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK);
279
280   if (fd < 0) 
281     return fd;
282
283   /* Initialise the port settings */
284
285   memcpy(&tp, &serial_termios, sizeof(struct termios));
286
287   /* Set port settings for canonical input processing */
288
289   tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL;
290   if (__with_odd_parity) {
291     tp.c_cflag |= (PARENB | PARODD);
292     tp.c_iflag = 0;
293   }
294   else
295     tp.c_iflag = IGNPAR;
296   if (__with_hw_handshake)
297     tp.c_cflag |= CRTSCTS;
298   else
299     tp.c_cflag &= ~CRTSCTS;
300
301   tp.c_oflag = 0;
302   tp.c_lflag = 0;
303   tp.c_cc[VMIN] = 1;
304   tp.c_cc[VTIME] = 0;
305
306   retcode=tcflush(fd, TCIFLUSH);
307   if (retcode == -1) {
308     perror("Gnokii serial_opendevice: tcflush");
309     serial_close(fd);
310     return(-1);
311   }
312
313   retcode=tcsetattr(fd, TCSANOW, &tp);
314   if (retcode == -1){
315     perror("Gnokii serial_opendevice: tcsetattr");
316     serial_close(fd);
317     return(-1);
318   }
319
320   /* Set speed */
321   { 
322 char *baudratestring=CFG_Get(CFG_Info, "global", "serial_baudrate");
323 int baudrate=0;
324
325   if (baudratestring)
326     baudrate=atoi(baudratestring);
327   if (baudrate && !serial_changespeed(fd,baudrate))
328     baudrate=0;
329   if (!baudrate)
330     serial_changespeed(fd,19200/*default value*/);
331   }
332
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!
335    */
336
337   retcode=fcntl(fd, F_SETFL, 0);
338   if (retcode == -1){
339     perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
340     serial_close(fd);
341     return(-1);
342   }
343
344   /* handle config file connect_script:
345    */
346   if (-1 == device_script(fd,"connect_script")) {
347     fprintf(stderr,"Gnokii serial_opendevice: connect_script\n");
348     serial_close(fd);
349     return(-1);
350   }
351
352   /* Allow process/thread to receive SIGIO */
353
354 #if !(__unices__)
355   retcode = fcntl(fd, F_SETOWN, getpid());
356   if (retcode == -1){
357     perror("Gnokii serial_opendevice: fnctl(F_SETOWN)");
358     serial_close(fd);
359     return(-1);
360   }
361 #endif
362
363   /* Make filedescriptor asynchronous. */
364
365   /* We need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
366    * by F_SETFL as a side-effect!
367    */
368   retcode=fcntl(fd, F_SETFL, (__with_async ? FASYNC : 0) | FNONBLOCK);
369   if (retcode == -1){
370     perror("Gnokii serial_opendevice: fnctl(F_SETFL)");
371     serial_close(fd);
372     return(-1);
373   }
374   
375   return fd;
376 }
377
378 /* Set the DTR and RTS bit of the serial device. */
379
380 void serial_setdtrrts(int __fd, int __dtr, int __rts) {
381
382   unsigned int flags;
383
384   flags = TIOCM_DTR;
385
386   if (__dtr)
387     ioctl(__fd, TIOCMBIS, &flags);
388   else
389     ioctl(__fd, TIOCMBIC, &flags);
390
391   flags = TIOCM_RTS;
392
393   if (__rts)
394     ioctl(__fd, TIOCMBIS, &flags);
395   else
396     ioctl(__fd, TIOCMBIC, &flags);
397 }
398
399
400 int serial_select(int fd, struct timeval *timeout) {
401
402   fd_set readfds;
403
404   FD_ZERO(&readfds);
405   FD_SET(fd, &readfds);
406
407   return (select(fd + 1, &readfds, NULL, NULL, timeout));
408
409 }
410
411
412 /* Change the speed of the serial device.
413  * RETURNS: Success
414  */
415
416 bool serial_changespeed(int __fd, int __speed) {
417   bool retcode = true;
418
419 #ifndef SGTTY
420   struct termios t;
421 #else
422   struct sgttyb t;
423 #endif
424
425   int speed=B9600;
426
427   switch (__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;
433     default:
434       fprintf(stderr,_("Serial port speed %d not supported!\n"),__speed);
435       return(false);
436   }
437
438 #ifndef SGTTY
439   if (tcgetattr(__fd, &t))
440     retcode = false;
441
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"));
447     retcode = false;
448     }
449
450   tcsetattr(__fd, TCSADRAIN, &t);
451 #else
452   if (ioctl(__fd, TIOCGETP, &t))
453     retcode = false;
454
455   t.sg_ispeed = speed;
456   t.sg_ospeed = speed;
457
458   if (ioctl(__fd, TIOCSETN, &t))
459     retcode = false;
460 #endif
461
462   return(retcode);
463 }
464
465 /* Read from serial device. */
466
467 size_t serial_read(int __fd, __ptr_t __buf, size_t __nbytes) {
468
469   return (read(__fd, __buf, __nbytes));
470 }
471
472 #if !defined(TIOCMGET) && defined(TIOCMODG)
473 #define TIOCMGET TIOCMODG
474 #endif
475
476 static void check_dcd(int __fd)
477 {
478 #ifdef TIOCMGET
479 int mcs;
480
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 */
484                 }
485 #else
486         /* Impossible!! (eg. Coherent) */
487 #endif
488 }
489
490 /* Write to serial device. */
491
492 size_t serial_write(int __fd, __const __ptr_t __buf, size_t __n) {
493
494 size_t r=0;
495 ssize_t got;
496 static long serial_write_usleep=LONG_MIN;
497 static int require_dcd=-1;
498
499         if (serial_write_usleep==LONG_MIN) {
500 char *s=CFG_Get(CFG_Info, "global", "serial_write_usleep");
501
502                 serial_write_usleep=(!s ?
503                         SERIAL_WRITE_USLEEP_DEFAULT : atol(CFG_Get(CFG_Info, "global", "serial_write_usleep")));
504                 }
505
506         if (require_dcd==-1) {
507                 require_dcd=(!!CFG_Get(CFG_Info, "global", "require_dcd"));
508 #ifndef TIOCMGET
509                 if (require_dcd)
510                         fprintf(stderr,_("WARNING: global/require_dcd argument was set but it is not supported on this system!\n"));
511 #endif
512                 }
513
514         if (require_dcd)
515                 check_dcd(__fd);
516
517         if (serial_write_usleep<0)
518                 return(write(__fd, __buf, __n));
519
520         while (__n>0) {
521                 got=write(__fd, __buf, 1);
522                 if (got<=0)
523                         return((!r ? -1 : r));
524                 __buf++;
525                 __n--;
526                 r++;
527                 if (serial_write_usleep)
528                         usleep(serial_write_usleep);
529                 }
530         return(r);
531 }
532
533 #endif  /* WIN32 */