Implemented remaining communication timeouts and maximum retry count.
[mdsms.git] / mdsms.c
diff --git a/mdsms.c b/mdsms.c
index f0837c6..af7fe28 100644 (file)
--- a/mdsms.c
+++ b/mdsms.c
@@ -5,6 +5,9 @@ static char rcsid[] ATTR_UNUSED = "$Id$";
 
 /*
  * $Log$
+ * Revision 1.2  1999/06/03 10:38:52  short
+ * Implemented remaining communication timeouts and maximum retry count.
+ *
  * Revision 1.1.1.1  1999/05/26 13:06:26  short
  * First alpha release.
  *
@@ -69,9 +72,9 @@ static int verbose
 static char *pname;
 static int dis_cleanup=0,devfd=-1;
 
-static char *phone,*body,*device,*lockfile,*smsc,*chartime,*cmdtime;
+static char *phone,*body,*device,*lockfile,*smsc,*maxretry,*readtime,*chartime,*cmdtime;
 static int readbody;
-static long chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME;
+static long maxretryn=DEF_MAXRETRY,readtimen=DEF_READTIME,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME;
 static size_t bodylen;
 
 static char *devicename; /* path stripped */
@@ -153,8 +156,8 @@ static void usage(void)
 %s\
 \n\
 Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
-             [-l|--lockfile <lock>] [-s|--smsc <smsc #>]\n\
-             [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
+             [-l|--lockfile <lock>] [-s|--smsc <smsc #>] [-m|--maxretry <#>]\n\
+             [-r|--readtime <sec>] [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
              [-f|--file] [-v|--verbose] [-h|--help] [-V|--version]\n\
              <dest. phone> <msg text|msg filename>\n\
 \n\
@@ -164,13 +167,15 @@ Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
  -l, --lockfile\tLock serial port by this file, \"%%s\" is basename of device\n\
 \t\t(def. \"%s\")\n\
  -s, --smsc\tUse this SMS Center number (def. query from Siemens A1)\n\
+ -m, --maxretry\tMaximum retries of any command before giving up (def. %d)\n\
+ -r, --readtime\tSeconds for maximum wait time for response (def. %ds)\n\
  -t, --chartime\tMilliseconds between each char on baud 19200 (def. %dms)\n\
  -T, --cmdtime\tMilliseconds before each whole AT command (def. %dms)\n\
  -f, --file\tRead contents of message from file instead\n\
  -v, --verbose\tIncrease verbosity level, more \"-v\"s give more messages\n\
  -h, --help\tPrint a summary of the options\n\
  -V, --version\tPrint the version number\n\
-\n",version,DEF_LOCKFILE,DEF_CHARTIME,DEF_CMDTIME);
+\n",version,DEF_LOCKFILE,DEF_MAXRETRY,DEF_READTIME,DEF_CHARTIME,DEF_CMDTIME);
        exit(EXIT_FAILURE);
 }
 
@@ -179,6 +184,8 @@ static const struct option longopts[]={
 {"device"  ,1,0,'d'},
 {"lockfile",1,0,'l'},
 {"smsc"    ,1,0,'s'},
+{"maxretry",1,0,'m'},
+{"readtime",1,0,'r'},
 {"chartime",1,0,'t'},
 {"cmdtime" ,1,0,'T'},
 {"file"    ,0,0,'f'},
@@ -271,6 +278,8 @@ static struct {
                { 'd',&device   },
                { 'l',&lockfile },
                { 's',&smsc     },
+               { 'm',&maxretry },
+               { 'r',&readtime },
                { 't',&chartime },
                { 'T',&cmdtime  },
        };
@@ -283,7 +292,7 @@ int i;
 
        seq++;
        optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
-       while ((optc=getopt_long(argp,args,"c:d:l:s:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
+       while ((optc=getopt_long(argp,args,"c:d:l:s:m:r:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
                case 'c':
                        if (cfgstacki>=NELEM(cfgstack)) {
                                error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),optind);
@@ -291,7 +300,7 @@ int i;
                                }
                        chk(cfgstack[cfgstacki++]=strdup(optarg));
                        break;
-               case 'd': case 'l': case 's': case 't': case 'T':
+               case 'd': case 'l': case 's': case 'm': case 'r': case 't': case 'T':
                        for (i=0;i<NELEM(optset);i++)
                                if (optset[i].c==optc) {
                                        if (optset[i].stamp && optset[i].stamp!=seq) {
@@ -422,6 +431,7 @@ remove:
 
 static void sigalarm(int signo)
 {
+       signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
        if (verbose>=1) error("Timed out");
 }
 
@@ -457,12 +467,19 @@ void *p;
        assert(catchdatal<=catchdatasiz);
 }
 
+static void retrying(void)
+{
+static int cnt=0;
+
+       if (++cnt>maxretryn) error("!Maximum command retry count (%d) exceeded",maxretryn);
+       if (verbose>=2) error(".Retrying phase, %d out of %d..",cnt,maxretryn);
+}
+
 static char *devcmd(const char *term,const char *catch,const char *send,...)
 {
 size_t l,bufl,terml,catchl,fragl,offs;
 char buf[LINE_MAX];
 ssize_t got;
-RETSIGTYPE (*origsig)(int);
 char *hit,*s;
 va_list ap;
 char errout;
@@ -471,8 +488,9 @@ char errout;
        if ((errout=(*send=='!'))) send++;
        if (0) {
 err:
+               alarm(0);
                if (errout) return(NULL);
-               if (verbose>=2) error(".Retrying last device command..");
+               retrying();
                }
        catchdatal=0;
        va_start(ap,send);
@@ -481,7 +499,6 @@ err:
        if (bufl>=sizeof(buf)-1) error("!Command too big (%d>%d)",bufl,sizeof(buf)-1);
        if (verbose>=2) error(".devcmd(send=\"%s\",term=\"%s\",catch=\"%s\")",buf,term,catch);
        buf[l]='\r'; buf[l+1]='\n';
-       origsig=signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
        for (offs=0,got=0;offs<bufl;offs++) {
                alarm(MAXSENDTIME);
                usleep((offs?chartimen:cmdtimen)*1000);
@@ -493,7 +510,6 @@ err:
                        error("Error forcing output of char %d of cmd \"%s\": %m",offs,buf);
                }
        alarm(0);
-       signal(SIGALRM,origsig);
        if (got!=bufl) {
                error("Wrote only %d of %d bytes of command: %m",got,bufl);
                goto err;
@@ -511,6 +527,7 @@ err:
        fragl=MAX(fragl,MAX(strlen(ERROR_SUBSTR1),strlen(ERROR_SUBSTR2)));
        bufl=0;
        record=NULL;
+       alarm(readtimen);
        for (;;) {
                blocking(0);
                errno=0;
@@ -548,6 +565,7 @@ err:
                        if (record) record=buf+bufl;
                        }
                }
+       alarm(0);
        if (!catchdatal) {
                if (!catch) return(NULL);
                error("Data requested on command \"%s\" but no found after term \"%s\"",send,term);
@@ -698,6 +716,8 @@ static struct {
        long *ip;
        const char *const msg;
        } numarg[]={
+               { &maxretry,&maxretryn,"maxretry" },
+               { &readtime,&readtimen,"readtime" },
                { &chartime,&chartimen,"chartime" },
                { &cmdtime ,&cmdtimen ,"cmdtime"  },
        };
@@ -816,13 +836,14 @@ size_t l=strlen(device);
        if (tcsetattr(devfd,TCSANOW,&tios))
                error("!Unable to set initial termios device settings");
 
+       signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
        do {
                devcmd("",NULL,"\r\nAT\x1A");
                devcmd(NULL,NULL,"\r\nAT");
                smscset();
                devcmd(NULL,NULL,"\r\nAT+CMGF=0");
                devcmd("\n> ",NULL,"\r\nAT+CMGS=%d",(strlen(pdusmsc)+strlen(pdudata))/2);
-               s=devcmd(NULL,"\n+CMGS:","!%s%s\x1A",pdusmsc,pdudata);
+               if (!(s=devcmd(NULL,"\n+CMGS:","!%s%s\x1A",pdusmsc,pdudata))) retrying();
                } while (!s);
        while (isspace(*s)) s++;
        if (verbose>=1) error("\nMessage successfuly sent with MR: %s",s);