3 static char rcsid[] ATTR_UNUSED = "$Id$";
8 * Revision 1.2 1999/06/03 10:38:52 short
9 * Implemented remaining communication timeouts and maximum retry count.
11 * Revision 1.1.1.1 1999/05/26 13:06:26 short
12 * First alpha release.
29 #include <sys/types.h>
35 #ifdef HAVE_GETOPT_LONG
42 char *strdup(const char *s);
43 int kill(pid_t pid,int sig);
44 int snprintf(char *str,size_t n,const char *format,...);
45 int vsnprintf(char *str,size_t n,const char *format,va_list ap);
46 void usleep(unsigned long usec);
49 #define NELEM(x) (sizeof((x))/sizeof(*(x)))
56 /* ANSI C does not allow macro with variable arguments */
58 #define dB(a) dbg(fprintf a)
60 #define d1(n1) dB((dO,n1 ))
61 #define d2(n1,n2) dB((dO,n1,n2 ))
62 #define d3(n1,n2,n3) dB((dO,n1,n2,n3 ))
63 #define d4(n1,n2,n3,n4) dB((dO,n1,n2,n3,n4))
65 static const char version[]="This is MobilDock SMS sender (" PACKAGE " " VERSION ")\n";
73 static int dis_cleanup=0,devfd=-1;
75 static char *phone,*body,*device,*lockfile,*smsc,*maxretry,*readtime,*chartime,*cmdtime;
77 static long maxretryn=DEF_MAXRETRY,readtimen=DEF_READTIME,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME;
78 static size_t bodylen;
80 static char *devicename; /* path stripped */
81 static char lockreal[512],locked;
83 static struct termios restios;
84 static char restios_yes;
86 static void error(const char *fmt,...)
90 #ifndef PRINTF_WORKS_PM
95 if (fatal=='!' || fatal=='.' || fatal=='\n') fmt++;
98 #ifndef PRINTF_WORKS_PM
99 if (!(nfmt=strdup(fmt))) return;
101 if ((pm=(fmtl>=2 && !strcmp(fmt+fmtl-2,"%m")))) nfmt[fmtl-2]='\0';
104 fprintf(stderr,"%s: ",pname);
107 #ifdef PRINTF_WORKS_PM
115 #ifndef PRINTF_WORKS_PM
117 fputs(strerror(errno),stderr);
122 if (fatal!='\n') fputc((fatal=='.'?'.':'!'),stderr);
124 if (fatal=='!') exit(EXIT_FAILURE);
127 static void chk(void *p)
130 error("!Virtual memory exhausted");
133 static void cleanup(void)
136 if (dis_cleanup) return;
138 if (tcsetattr(devfd,TCSANOW,&restios))
139 error("Error restoring termios for device: %m");
142 if (locked && *lockreal) {
143 d2("Removing lockfile \"%s\"\n",lockreal);
144 if (unlink(lockreal))
145 error("Error removing my device lockfile \"%s\": %m",lockreal);
152 static void usage(void)
158 Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
159 [-l|--lockfile <lock>] [-s|--smsc <smsc #>] [-m|--maxretry <#>]\n\
160 [-r|--readtime <sec>] [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
161 [-f|--file] [-v|--verbose] [-h|--help] [-V|--version]\n\
162 <dest. phone> <msg text|msg filename>\n\
164 -c, --config\tRead this additional config file\n\
165 \t\t(def. \"" CONFIG_MAIN "\" and \"$HOME" CONFIG_HOME "\")\n\
166 -d, --device\tMobilDock on this serial device (def. \"" DEF_DEVICE "\")\n\
167 -l, --lockfile\tLock serial port by this file, \"%%s\" is basename of device\n\
169 -s, --smsc\tUse this SMS Center number (def. query from Siemens A1)\n\
170 -m, --maxretry\tMaximum retries of any command before giving up (def. %d)\n\
171 -r, --readtime\tSeconds for maximum wait time for response (def. %ds)\n\
172 -t, --chartime\tMilliseconds between each char on baud 19200 (def. %dms)\n\
173 -T, --cmdtime\tMilliseconds before each whole AT command (def. %dms)\n\
174 -f, --file\tRead contents of message from file instead\n\
175 -v, --verbose\tIncrease verbosity level, more \"-v\"s give more messages\n\
176 -h, --help\tPrint a summary of the options\n\
177 -V, --version\tPrint the version number\n\
178 \n",version,DEF_LOCKFILE,DEF_MAXRETRY,DEF_READTIME,DEF_CHARTIME,DEF_CMDTIME);
182 static const struct option longopts[]={
185 {"lockfile",1,0,'l'},
187 {"maxretry",1,0,'m'},
188 {"readtime",1,0,'r'},
189 {"chartime",1,0,'t'},
190 {"cmdtime" ,1,0,'T'},
192 {"verbose" ,0,0,'v'},
194 {"version" ,0,0,'V'}};
196 static void processargs(int argp,char **args,const char *from);
198 static char *cfgstack[MAXCFGLOOP];
199 static unsigned cfgstacki=0;
201 static void chkfclose(FILE *f,const char *fname)
204 error("Error closing \"%s\": %m",fname);
207 static void readfile(const char *fname,char quiet)
211 char *args[MAXCFGARGS],*d,*s,blank,quote;
215 static unsigned tot=0;
217 if (tot++>=MAXCFGNUM) {
218 if (tot==MAXCFGNUM+1) error("Too many config files to read, max is %d, break-out",MAXCFGNUM);
221 if (!(f=fopen(fname,"rt"))) {
222 if (!quiet) error("Can't open config file \"%s\" for r/o: %m",fname);
225 if (verbose>=2) error(".Reading config file \"%s\"",fname);
226 if (fseek(f,0,SEEK_END))
227 error("Error seeking to end of \"s\": %m",fname);
228 if ((size=ftell(f))<0)
229 size=0,error("Error measuring \"%s\": %m",fname);
231 error("File \"%s\" is too long, read only %ld bytes",fname,MAXCONFIG);
232 chk(buf=malloc((size?size:MAXCONFIG)+1));
234 got=fread(buf,1,(size?size:MAXCONFIG),f);
235 if (size && got!=size)
236 error("File \"%s\" read error, got only %u bytes of %ld",fname,got,size);
240 for (argp=1,d=s=buf,blank=1,quote=0;s<buf+got;s++) {
243 if (!quote && isspace(c)) {
247 if (verbose>=2) error("\nConfig \"%s\": arg#%d: %s",fname,argp-1,args[argp-1]);
252 if (argp>=NELEM(args)-1) {
253 error("Too many arguments in \"%s\", from offset %d ignored",fname,s-buf);
260 if (c=='\\') { *d++=*++s; continue; }
261 if (c=='"' || c=='\'') {
262 if (!quote ) { quote=c; continue; }
263 if ( quote==c) { quote=0; continue; }
269 processargs(argp,args,fname);
287 static void processargs(int argp,char **args,const char *from)
290 static unsigned seq=0;
294 optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
295 while ((optc=getopt_long(argp,args,"c:d:l:s:m:r:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
297 if (cfgstacki>=NELEM(cfgstack)) {
298 error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),optind);
301 chk(cfgstack[cfgstacki++]=strdup(optarg));
303 case 'd': case 'l': case 's': case 'm': case 'r': case 't': case 'T':
304 for (i=0;i<NELEM(optset);i++)
305 if (optset[i].c==optc) {
306 if (optset[i].stamp && optset[i].stamp!=seq) {
307 assert(!!*optset[i].var);
310 free(*optset[i].var);
311 chk(*optset[i].var=strdup(optarg));
315 assert(i<NELEM(optset));
324 fprintf(stderr,version);
328 error("\nLast getopt(3) error occured during parsing option %d from \"%s\"! Follows help:",optind-1,from);
332 if (!phone && optind<argp)
333 chk(phone=strdup(args[optind++]));
334 if (!body && optind<argp) {
338 for (i=optind,bodylen=argp-optind;i<argp;i++)
339 bodylen+=strlen(args[i]);
340 chk(body=malloc(bodylen));
341 for (d=body,i=optind;i<argp;i++) {
342 size_t l=strlen(args[i]);
346 assert(d<=body+bodylen);
348 assert(d==body+bodylen);
353 char *s=cfgstack[--cfgstacki];
355 assert(cfgstacki>=0);
358 assert(cfgstacki>=0 && cfgstacki<NELEM(cfgstack));
362 static const struct {
366 {&phone,"destination phone number"},
368 {&body ,"body text"},
369 {&device,"device for communication"},
372 static char **emptycheck[]={&smsc,&body};
374 static void lockclose(int fd)
377 error("Error closing lockfile \"%s\"",lockreal);
380 static inline void lockdevice(void)
390 if (fd!=-1) lockclose(fd);
392 if (delay) sleep(delay);
393 delay=DEVLOCK_PERIOD;
394 if (verbose>=3) error(".Checking the lockfile \"%s\"..",lockreal);
395 if ((fd=open(lockreal,O_RDONLY))==-1) break;
396 if ((got=read(fd,buf,sizeof(buf)-1))<=0) {
398 if (empty>=DEVLOCK_MAXEMPTY) {
399 error(".Lockfile \"%s\" is still not valid, removing it",lockreal);
405 assert(got<sizeof(buf));
407 if (sscanf(buf,"%d",&pid)!=1) goto isempty;
410 if (kill(pid,0) && errno!=ESRCH && errno!=EPERM)
411 error("Error during checking consciousness of PID %d: %m",pid);
412 if (errno!=ESRCH) continue;
413 error(".Lockfile \"%s\" is stale (PID %d), removing it",lockreal,pid);
416 if (unlink(lockreal))
417 error("Error removing foreign lockfile \"%s\": %m",lockreal);
421 if ((fd=open(lockreal,O_WRONLY|O_CREAT|O_EXCL,0644))==-1) {
422 if (errno==EEXIST) goto recheck;
423 error("!Error creating lockfile \"%s\": %m",lockreal);
426 got=VARPRINTF(buf,"%010d\n",getpid()); assert(got==11);
427 if (write(fd,buf,got)!=got)
428 error("!Error writing data to lockfile \"%s\": %m",lockreal);
432 static void sigalarm(int signo)
434 signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
435 if (verbose>=1) error("Timed out");
438 static void blocking(char yes)
440 static char state=-1;
441 if (state==yes) return;
442 if (fcntl(devfd,F_SETFL,(yes?0:O_NONBLOCK)))
443 error("!fcntl() on device for %sblocking mode: %m",(yes?"":"non-"));
447 static const char *record;
448 static char *catchdata;
449 static size_t catchdatal,catchdatasiz;
451 static void catched(const char *end)
458 if ((p=memchr(record,'\n',end-record))) end=p;
459 if ((len=end-record)) {
460 if (catchdatal+len>catchdatasiz)
461 chk(catchdata=realloc(catchdata,
462 (catchdatasiz=MAX(LINE_MAX,(catchdatal+len)*2))));
463 memcpy(catchdata+catchdatal,record,len);
467 assert(catchdatal<=catchdatasiz);
470 static void retrying(void)
474 if (++cnt>maxretryn) error("!Maximum command retry count (%d) exceeded",maxretryn);
475 if (verbose>=2) error(".Retrying phase, %d out of %d..",cnt,maxretryn);
478 static char *devcmd(const char *term,const char *catch,const char *send,...)
480 size_t l,bufl,terml,catchl,fragl,offs;
487 if (!term) term="\nOK\n";
488 if ((errout=(*send=='!'))) send++;
492 if (errout) return(NULL);
497 l=VARVPRINTF(buf,send,ap); bufl=l+1;
499 if (bufl>=sizeof(buf)-1) error("!Command too big (%d>%d)",bufl,sizeof(buf)-1);
500 if (verbose>=2) error(".devcmd(send=\"%s\",term=\"%s\",catch=\"%s\")",buf,term,catch);
501 buf[l]='\r'; buf[l+1]='\n';
502 for (offs=0,got=0;offs<bufl;offs++) {
504 usleep((offs?chartimen:cmdtimen)*1000);
505 if (!offs && tcflush(devfd,TCIOFLUSH))
506 error("Error flushing I/O queue of device: %m");
507 if (write(devfd,buf+offs,1)!=1) break;
510 error("Error forcing output of char %d of cmd \"%s\": %m",offs,buf);
514 error("Wrote only %d of %d bytes of command: %m",got,bufl);
518 if (!(terml=strlen(term))) {
523 catchl=strlen(catch);
524 fragl=MAX(terml,catchl);
527 fragl=MAX(fragl,MAX(strlen(ERROR_SUBSTR1),strlen(ERROR_SUBSTR2)));
534 got=read(devfd,buf+bufl,sizeof(buf)-1-bufl);
535 if (got==-1 && errno==EAGAIN) {
538 got=read(devfd,buf+bufl,1);
541 error("Could read device data (ret=%d): %m",got);
546 /* FIXME: '\0' conversion */
547 while (buf+bufl>s && (s=memchr(s,'\r',buf+bufl-s))) *s='\n';
548 catched(buf+bufl); assert(!record || record==buf+bufl);
549 assert(bufl<sizeof(buf));
552 assert(strlen(buf)==bufl);
553 /* d3(">%s|%s<\n",buf,term); */
554 if (strstr(buf,ERROR_SUBSTR1) || strstr(buf,ERROR_SUBSTR2)) {
555 error("Found ERROR response on command \"%s\"",send);
558 if (catch && bufl>=catchl && (hit=strstr(buf,catch))) {
560 catched(buf+bufl); assert(!record || record==buf+bufl);
562 if ( bufl>= terml && (hit=strstr(buf,term))) break;
563 memmove(buf,buf+bufl-(fragl-1),fragl-1);
565 if (record) record=buf+bufl;
570 if (!catch) return(NULL);
571 error("Data requested on command \"%s\" but no found after term \"%s\"",send,term);
578 if (verbose>=2) error(".Returning data \"%s\" for cmd \"%s\"",catchdata,send);
582 static int prepaddr(unsigned char *d,const char *addr)
588 if ((plus=(*addr=='+'))) addr++;
589 *++d=(plus?ADDR_INT:ADDR_NAT);
591 if (*addr<'0' || *addr>'9')
592 error("!Error during conversion of number at: %s",addr);
595 if ((flip=!flip)) *++d=0xF0|n;
596 else *d=(*d&0x0F)|(n<<4U);
601 static char *finalsmsc;
602 #define SMSCBINSIZE (1+1+(MAXNUMLEN+1)/2)
603 static char pdusmsc[SMSCBINSIZE*2+1];
605 static inline char tohex(unsigned char x)
608 if (x<10) return(x +'0');
612 static void textconv(char *d,unsigned char *s,size_t len)
622 static inline void smscset(void)
626 unsigned char bin[2+(MAXNUMLEN+1)/2];
628 if (smsc) devcmd(NULL,NULL,"\r\nAT+CSCA=\"%s\"",smsc);
629 s=devcmd(NULL,"\n+CSCA:","\r\nAT+CSCA?");
630 while (isspace(*s)) s++;
631 if (!*s || !strcmp(s,"EMPTY"))
632 error("!No SMS set in A1 found");
633 if (verbose>=1) error("\nFound default SMSC in A1: %s",s);
634 if (*s!='"') error("!No left-quote found in: %s",s);
635 if (!(t=strrchr(s+1,'"'))) error("!No right-quote found in: %s",s);
637 while (isspace(*t)) t++;
638 if (*t++!=',') error("!No comma found after quotes in: %s",s);
639 while (isspace(*t)) t++;
640 l=strtol(t,&serr,10);
641 if ((l!=ADDR_NAT && l!=ADDR_INT) || (serr && *serr))
642 error("!Type parse error in: %s",s);
643 if (l==ADDR_NAT || s[1]=='+') s++;
646 chk(finalsmsc=strdup(s));
647 if (verbose>=2) error("\nDecoded SMSC address: %s",finalsmsc);
648 bin[0]=1+(prepaddr(bin,finalsmsc)+1)/2;
649 textconv(pdusmsc,bin,bin[0]+1);
652 static char *pdudata;
654 static inline unsigned char charconv(char c,size_t offs)
656 if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')) return(c);
660 case '"': return(96);
665 error("Can't convert character '%c' (0x%02X) at offs %d (0-based), substituted '?'",
666 c,(unsigned char)c,offs);
670 static inline void genpdu(void)
672 static unsigned char pdu[64+MAXNUMLEN/2+(MAXBODYLEN*7)/8];
673 unsigned char *d=pdu;
675 char inb=0,outb=0,xb;
682 *d=i; d+=1+1+(i+1)/2;
686 if (bodylen>MAXBODYLEN) {
687 error("Body too large (%d>%d), cut",bodylen,MAXBODYLEN);
691 assert(d<pdu+sizeof(pdu));
692 while (bodylen || inb) {
694 assert(bodylen>0); assert(!!*body);
695 inreg=charconv(*body++,offs++);
704 d4("inb=%d,outb=%d,xb=%d\n",inb,outb,xb);
705 *d|=((inreg>>(unsigned)(7-inb))&((1<<xb)-1))<<(unsigned)(8-outb);
709 assert(d<pdu+sizeof(pdu));
710 pdudata=malloc(2*(d-pdu)+1);
711 textconv(pdudata,pdu,d-pdu);
717 const char *const msg;
719 { &maxretry,&maxretryn,"maxretry" },
720 { &readtime,&readtimen,"readtime" },
721 { &chartime,&chartimen,"chartime" },
722 { &cmdtime ,&cmdtimen ,"cmdtime" },
725 int main(int argc,char **argv)
735 signal(SIGTERM,(RETSIGTYPE (*)(int))cleanup);
736 signal(SIGQUIT,(RETSIGTYPE (*)(int))cleanup);
737 signal(SIGINT ,(RETSIGTYPE (*)(int))cleanup);
738 signal(SIGHUP ,(RETSIGTYPE (*)(int))cleanup);
739 processargs(argc,argv,"<command-line>");
740 if ((s=getenv("HOME"))) {
742 char *buf=malloc(l+50);
745 strcpy(buf+l,CONFIG_HOME);
749 readfile(CONFIG_MAIN,1);
751 for (i=0;i<NELEM(nullcheck);i++)
752 if (!*nullcheck[i].var) {
753 error("Missing parameter \"%s\"",nullcheck[i].name);
756 if (fatal) error("!Previous error%s considered unrecoverable",(fatal==1?"":"s"));
757 for (i=0;i<NELEM(emptycheck);i++)
758 if (*emptycheck[i] && !**emptycheck[i]) {
759 free(*emptycheck[i]);
762 if (!lockfile) lockfile=DEF_LOCKFILE;
763 if (!device) device=DEF_DEVICE;
764 if (body && readbody) {
772 error("\nPlease enter the SMS text body, end with EOF (ctrl-D):");
773 chk(body=malloc(BODYLOAD));
774 bodylen=fread(body,1,BODYLOAD,fin);
776 error("!Error reading stream \"%s\": %m",(finame?finame:"<stdin>"));
779 chkfclose(fin,finame);
783 for (i=0;i<NELEM(numarg);i++) {
785 if (!*numarg[i].sp) continue;
786 *numarg[i].ip=strtol(*numarg[i].sp,&serr,0);
787 if (*numarg[i].ip<0 || *numarg[i].ip>=LONG_MAX || !serr || *serr)
788 error("!Number parse error for parameter \"%s\" of \"%s\" at: %s",
789 numarg[i].msg,*numarg[i].sp,serr);
792 if (!strchr(device,'/')) {
793 size_t l=strlen(device);
794 chk(s=malloc(5+l+1));
800 devicename=strrchr(device,'/')+1; assert(!!(devicename-1));
801 for (i=0,s=lockfile;*s;s++) {
802 if (*s!='%') continue;
804 if (*s=='%') continue;
806 if (i) error("!Only one \"%%s\" permitted in lockfile format-string");
809 error("!Invalid format-character '%c' in lockfile format-string, only \"%%s\" allowed",*s);
813 if (lockfile && *lockfile && VARPRINTF(lockreal,lockfile,devicename)>0) {
814 if (verbose>=1) error(".Locking device \"%s\" by \"%s\"..",device,lockreal);
817 if (verbose>=1) error(".Opening device \"%s\"..",device);
818 if ((devfd=open(device,O_RDWR|O_NDELAY))<0)
819 error("!Cannot open device \"%s\" for rw-access: %m",device);
821 if (tcgetattr(devfd,&restios))
822 error("Unable to get termios settings: %m");
824 restios.c_cflag=(restios.c_cflag&~(CBAUD|CBAUDEX))|B0|HUPCL;
827 tios.c_iflag=IGNBRK|IGNPAR|IXON|IXOFF;
829 tios.c_cflag=CS8|CREAD|CLOCAL|B19200|HUPCL;
830 tios.c_lflag=IEXTEN|NOFLSH;
831 memset(tios.c_cc,_POSIX_VDISABLE,sizeof(tios.c_cc));
834 if (tcflush(devfd,TCIOFLUSH))
835 error("Error flushing termios (TCIOFLUSH) on device: %m");
836 if (tcsetattr(devfd,TCSANOW,&tios))
837 error("!Unable to set initial termios device settings");
839 signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
841 devcmd("",NULL,"\r\nAT\x1A");
842 devcmd(NULL,NULL,"\r\nAT");
844 devcmd(NULL,NULL,"\r\nAT+CMGF=0");
845 devcmd("\n> ",NULL,"\r\nAT+CMGS=%d",(strlen(pdusmsc)+strlen(pdudata))/2);
846 if (!(s=devcmd(NULL,"\n+CMGS:","!%s%s\x1A",pdusmsc,pdudata))) retrying();
848 while (isspace(*s)) s++;
849 if (verbose>=1) error("\nMessage successfuly sent with MR: %s",s);
850 devcmd(NULL,NULL,"\r\nAT");
852 return(EXIT_SUCCESS);