3 static char rcsid[] ATTR_UNUSED = "$Id$";
19 #include <sys/types.h>
26 #ifdef HAVE_GETOPT_LONG
32 #define NELEM(x) (sizeof((x))/sizeof(*(x)))
39 /* ANSI C does not allow macro with variable arguments */
41 #define dB(a) dbg(fprintf a)
43 #define d1(n1) dB((dO,n1 ))
44 #define d2(n1,n2) dB((dO,n1,n2 ))
45 #define d3(n1,n2,n3) dB((dO,n1,n2,n3 ))
46 #define d4(n1,n2,n3,n4) dB((dO,n1,n2,n3,n4))
48 static const char version[]="This is Nokia 9110 logo SMS sender (" PACKAGE " " VERSION ")\n";
56 static int dis_cleanup=0,devfd=-1;
58 static char *phone,*logoname,*device,*logname,*lockfile,*smsc,*maxretry,*readtime,*chartime,*cmdtime,*baud,*gsmnet;
59 static long maxretryn=DEF_MAXRETRY,readtimen=DEF_READTIME,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME,baudn=DEF_BAUD;
61 static char *devicename; /* path stripped */
62 static char lockreal[512],locked;
64 static struct termios restios,tios;
65 static char restios_yes;
69 #ifndef PRINTF_WORKS_PM
72 const char *fmt,va_list ap)
82 if (gethostname(host,sizeof(host))) strcpy(host,"<ERROR>");
86 if ((s=strchr(ctm,'\n'))) *s='\0';
87 fprintf(logf,"%s %s %s[%d]: ",ctm,host,pname,mypid);
88 vfprintf(logf,fmt,ap);
89 #ifndef PRINTF_WORKS_PM
90 if (outerr) fputs(strerror(errno),logf);
96 static void logmsg(const char *fmt,...)
101 #ifndef PRINTF_WORKS_PM
108 static void error(const char *fmt,...)
112 #ifndef PRINTF_WORKS_PM
117 if (fatal=='!' || fatal=='.' || fatal=='\n') fmt++;
120 #ifndef PRINTF_WORKS_PM
121 if (!(nfmt=strdup(fmt))) return;
123 if ((pm=(fmtl>=2 && !strcmp(fmt+fmtl-2,"%m")))) nfmt[fmtl-2]='\0';
126 fprintf(stderr,"%s: ",pname);
129 #ifdef PRINTF_WORKS_PM
135 if (fatal=='!') vlogmsg(
136 #ifdef PRINTF_WORKS_PM
144 #ifndef PRINTF_WORKS_PM
146 fputs(strerror(errno),stderr);
151 if (fatal!='\n') fputc((fatal=='.'?'.':'!'),stderr);
153 if (fatal=='!') exit(EXIT_FAILURE);
156 static void chk(void *p)
159 error("!Virtual memory exhausted");
162 static void cleanup(void)
165 if (dis_cleanup) return;
167 if (tcsetattr(devfd,TCSANOW,&restios))
168 error("Error restoring termios for device: %m");
171 if (locked && *lockreal) {
172 d2("Removing lockfile \"%s\"\n",lockreal);
173 if (unlink(lockreal))
174 error("Error removing my device lockfile \"%s\": %m",lockreal);
181 static void usage(void)
187 Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
188 [-L|--log <file>] [-b|--baud <rate>]\n\
189 [-l|--lockfile <lock>] [-s|--smsc <smsc #>] [-m|--maxretry <#>]\n\
190 [-r|--readtime <sec>] [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
191 [-v|--verbose] [-h|--help] [-V|--version]\n\
192 <dest. phone> <logo filename> [<GSMnet id>]\n\
194 -c, --config\tRead this additional config file\n\
195 \t\t(def. \"" CONFIG_MAIN "\" and \"$HOME" CONFIG_HOME "\")\n\
196 -d, --device\tNokia on this serial device (def. \"" DEF_DEVICE "\")\n\
197 -L, --log\tLog all important messages to this file (def. \"" DEF_LOGNAME "\")\n\
198 -b, --baud\tSet baudrate, 2400-57600 supported (def. %d)\n\
199 -l, --lockfile\tLock serial port by this file, \"%%s\" is basename of device\n\
201 -s, --smsc\tUse this SMS Center number (def. query from Nokia 9110)\n\
202 -m, --maxretry\tMaximum retries of any command before giving up (def. %d)\n\
203 -r, --readtime\tSeconds for maximum wait time for response (def. %ds)\n\
204 -t, --chartime\tMilliseconds between each char (def. %dms)\n\
205 -T, --cmdtime\tMilliseconds before each whole AT command (def. %dms)\n\
206 -v, --verbose\tIncrease verbosity level, more \"-v\"s give more messages\n\
207 -h, --help\tPrint a summary of the options\n\
208 -V, --version\tPrint the version number\n\
209 <GSMnet id>\t* Oper. logo: Enter custom network code MccMnc, e.g. 23002\n\
210 \t\t* Oper. logo: Specify \"" WORD_NET "\" to read network code from NOL file\n\
211 \t\t* Group gfx : Specify \"" WORD_GROUP "\" to send logo as group graphics\n\
212 \n",version,DEF_BAUD,DEF_LOCKFILE,DEF_MAXRETRY,DEF_READTIME,DEF_CHARTIME,DEF_CMDTIME);
216 static const struct option longopts[]={
221 {"lockfile",1,0,'l'},
223 {"maxretry",1,0,'m'},
224 {"readtime",1,0,'r'},
225 {"chartime",1,0,'t'},
226 {"cmdtime" ,1,0,'T'},
227 {"verbose" ,0,0,'v'},
229 {"version" ,0,0,'V'}};
231 static void processargs(int argp,char **args,const char *from);
233 static char *cfgstack[MAXCFGLOOP];
234 static unsigned cfgstacki=0;
236 static void chkfclose(FILE *f,const char *fname)
239 error("Error closing \"%s\": %m",fname);
242 static void readfile(const char *fname,char quiet)
246 char *args[MAXCFGARGS],*d,*s,blank,quote;
250 static unsigned tot=0;
252 if (tot++>=MAXCFGNUM) {
253 if (tot==MAXCFGNUM+1) error("Too many config files to read, max is %d, break-out",MAXCFGNUM);
256 if (!(f=fopen(fname,"rt"))) {
257 if (!quiet) error("Can't open config file \"%s\" for r/o: %m",fname);
260 if (verbose>=2) error(".Reading config file \"%s\"",fname);
261 if (fseek(f,0,SEEK_END))
262 error("Error seeking to end of \"s\": %m",fname);
263 if ((size=ftell(f))<0)
264 size=0,error("Error measuring \"%s\": %m",fname);
266 error("File \"%s\" is too long, read only %ld bytes",fname,MAXCONFIG);
267 chk(buf=malloc((size?size:MAXCONFIG)+1));
269 got=fread(buf,1,(size?size:MAXCONFIG),f);
270 if (size && got!=size)
271 error("File \"%s\" read error, got only %u bytes of %ld",fname,got,size);
275 for (argp=1,d=s=buf,blank=1,quote=0;s<buf+got;s++) {
278 if (!quote && isspace(c)) {
282 if (verbose>=2) error("\nConfig \"%s\": arg#%d: %s",fname,argp-1,args[argp-1]);
287 if (argp>=NELEM(args)-1) {
288 error("Too many arguments in \"%s\", from offset %d ignored",fname,s-buf);
295 if (c=='\\') { *d++=*++s; continue; }
296 if (c=='"' || c=='\'') {
297 if (!quote ) { quote=c; continue; }
298 if ( quote==c) { quote=0; continue; }
304 processargs(argp,args,fname);
324 static void processargs(int argp,char **args,const char *from)
327 static unsigned seq=0;
331 optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
332 while ((optc=getopt_long(argp,args,"c:d:L:l:s:m:r:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
334 if (cfgstacki>=NELEM(cfgstack)) {
335 error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),optind);
338 chk(cfgstack[cfgstacki++]=strdup(optarg));
340 case 'd': case 'L': case 'b': case 'l': case 's': case 'm': case 'r': case 't': case 'T':
341 for (i=0;i<NELEM(optset);i++)
342 if (optset[i].c==optc) {
343 if (optset[i].stamp && optset[i].stamp!=seq) {
344 assert(!!*optset[i].var);
347 free(*optset[i].var);
348 chk(*optset[i].var=strdup(optarg));
352 assert(i<NELEM(optset));
358 fprintf(stderr,version);
362 error("\nLast getopt(3) error occured during parsing option %d from \"%s\"! Follows help:",optind-1,from);
366 if (!phone && optind<argp) {
369 chk(phone=strdup(args[optind++]));
370 s=(s1=phone)+(*phone=='+');
371 while (*s && s-s1<MAXNUMLEN)
373 error("!Invalid digit '%c' in destination phone number - at offset %d of option %d from \"%s\": %s",
374 *s,s-phone,optind,from,args[optind]);
376 error("!Destination phone number too long, max. %d digits allowed in option %d from \"%s\": %s",
377 s-s1,optind,from,args[optind]);
379 if (!logoname && optind<argp)
380 chk(logoname=strdup(args[optind++]));
381 if (!gsmnet && optind<argp) {
384 chk(gsmnet=strdup(args[optind++]));
385 if (strcasecmp(gsmnet,WORD_NET) && strcasecmp(gsmnet,WORD_GROUP)) {
386 for (d=s=gsmnet;*s;s++) {
387 if (isdigit(*s)) { *d++=*s; continue; }
388 if (isspace(*s)) continue;
389 error("\nInvalid characted '%c' in GSMnet at offs %d: %s",
390 *s,s-gsmnet,args[optind]);
395 error("\nGSMnet is required to have exactly 5 digits or to be\n\
396 either \"" WORD_NET "\" or \"" WORD_GROUP "\", but found length %d: %s",
397 d-gsmnet,args[optind]);
402 error("\nGSMnet option %d from \"%s\" rejected due to previous errors: %s",
403 optind,from,args[optind]);
410 error("\nExcessive option %d from \"%s\" ignored: %s",optind,from,args[optind]);
412 char *s=cfgstack[--cfgstacki];
414 assert(cfgstacki>=0);
417 assert(cfgstacki>=0 && cfgstacki<NELEM(cfgstack));
421 static const struct {
425 {&phone,"destination phone number"},
427 {&logoname,"logo filename"},
428 {&gsmnet,"GSM operator network code"},
429 {&device,"device for communication"},
432 static char **emptycheck[]={&logname,&smsc,&logoname,&gsmnet};
434 static void lockclose(int fd)
437 error("Error closing lockfile \"%s\"",lockreal);
440 static inline void lockdevice(void)
450 if (fd!=-1) lockclose(fd);
452 if (delay) sleep(delay);
453 delay=DEVLOCK_PERIOD;
454 if (verbose>=3) error(".Checking the lockfile \"%s\"..",lockreal);
455 if ((fd=open(lockreal,O_RDONLY))==-1) break;
456 if ((got=read(fd,buf,sizeof(buf)-1))<=0) {
458 if (empty>=DEVLOCK_MAXEMPTY) {
459 error(".Lockfile \"%s\" is still not valid, removing it",lockreal);
465 assert(got<sizeof(buf));
467 if (sscanf(buf,"%d",&pid)!=1) goto isempty;
470 if (kill(pid,0) && errno!=ESRCH && errno!=EPERM)
471 error("Error during checking consciousness of PID %d: %m",pid);
472 if (errno!=ESRCH) continue;
473 error(".Lockfile \"%s\" is stale (PID %d), removing it",lockreal,pid);
476 if (unlink(lockreal))
477 error("Error removing foreign lockfile \"%s\": %m",lockreal);
481 if ((fd=open(lockreal,O_WRONLY|O_CREAT|O_EXCL,0644))==-1) {
482 if (errno==EEXIST) goto recheck;
483 error("!Error creating lockfile \"%s\": %m",lockreal);
486 got=VARPRINTF(buf,"%010d\n",getpid()); assert(got==11);
487 if (write(fd,buf,got)!=got)
488 error("!Error writing data to lockfile \"%s\": %m",lockreal);
492 static char wasalarm=0;
493 static void sigalarm(int signo)
495 signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
497 if (verbose>=1) error("Timed out");
500 static void blocking(char yes)
502 static char state=-1;
503 if (state==yes) return;
504 if (fcntl(devfd,F_SETFL,(yes?0:O_NONBLOCK)))
505 error("!fcntl() on device for %sblocking mode: %m",(yes?"":"non-"));
509 static const char *record;
510 static char *catchdata;
511 static size_t catchdatal,catchdatasiz;
513 static void catched(const char *end)
520 if ((p=memchr(record,'\n',end-record))) end=p;
521 if ((len=end-record)) {
522 if (catchdatal+len>catchdatasiz)
523 chk(catchdata=realloc(catchdata,
524 (catchdatasiz=MAX(LINE_MAX,(catchdatal+len)*2))));
525 memcpy(catchdata+catchdatal,record,len);
529 assert(catchdatal<=catchdatasiz);
532 static int retrycnt=0;
533 static void retrying(void)
535 if (++retrycnt>maxretryn) error("!Maximum command retry count (%d) exceeded",maxretryn);
536 if (verbose>=2) error(".Retrying phase, %d out of %d..",retrycnt,maxretryn);
539 static char *devcmd(const char *term,const char *catch,const char *send,...)
541 size_t l,bufl,terml,catchl,fragl,offs;
548 if (!term) term="\nOK\n";
549 if ((errout=(*send=='!'))) send++;
553 if (errout) return(NULL);
558 l=VARVPRINTF(buf,send,ap); bufl=l+1;
560 if (bufl>=sizeof(buf)-1) error("!Command too big (%d>%d)",bufl,sizeof(buf)-1);
561 if (verbose>=2) error(".devcmd(send=\"%s\",term=\"%s\",catch=\"%s\")",buf,term,catch);
562 buf[l]='\r'; buf[l+1]='\n';
563 for (offs=0,got=0;offs<bufl;offs++) {
565 usleep((offs?chartimen:cmdtimen)*1000);
566 if (!offs && tcflush(devfd,TCIOFLUSH))
567 error("Error flushing I/O queue of device: %m");
568 if (write(devfd,buf+offs,1)!=1) break;
571 error("Error forcing output of char %d of cmd \"%s\": %m",offs,buf);
575 error("Wrote only %d of %d bytes of command: %m",got,bufl);
579 if (!(terml=strlen(term))) {
584 catchl=strlen(catch);
585 fragl=MAX(terml,catchl);
588 fragl=MAX(fragl,MAX(strlen(ERROR_SUBSTR1),strlen(ERROR_SUBSTR2)));
596 got=read(devfd,buf+bufl,sizeof(buf)-1-bufl);
597 if (got==-1 && errno==EAGAIN) {
600 got=read(devfd,buf+bufl,1);
603 if (wasalarm) error("!Maximum response timeout (%ds) exceeded",readtimen);
604 error("Couldn't read device data (ret=%d): %m",got);
609 /* FIXME: '\0' conversion */
610 while (buf+bufl>s && (s=memchr(s,'\r',buf+bufl-s))) *s='\n';
611 catched(buf+bufl); assert(!record || record==buf+bufl);
612 assert(bufl<sizeof(buf));
615 assert(strlen(buf)==bufl);
616 /* d3(">%s|%s<\n",buf,term); */
617 if (strstr(buf,ERROR_SUBSTR1) || strstr(buf,ERROR_SUBSTR2)) {
618 error("Found ERROR response on command \"%s\"",send);
621 if (catch && bufl>=catchl && (hit=strstr(buf,catch))) {
623 catched(buf+bufl); assert(!record || record==buf+bufl);
625 if ( bufl>= terml && (hit=strstr(buf,term))) break;
626 memmove(buf,buf+bufl-(fragl-1),fragl-1);
628 if (record) record=buf+bufl;
633 if (!catch) return(NULL);
634 error("Data requested on command \"%s\" but no found after term \"%s\"",send,term);
641 if (verbose>=2) error(".Returning data \"%s\" for cmd \"%s\"",catchdata,send);
645 static inline char tohex(unsigned char x)
648 if (x<10) return(x +'0');
652 static inline void textconv(char *d,unsigned char *s,size_t len)
662 static inline void smscset(void)
667 if (smsc) devcmd(NULL,NULL,"\r\nAT+CSCA=\"%s\"",smsc);
668 s=devcmd(NULL,"\n+CSCA:","\r\nAT+CSCA?");
669 while (isspace(*s)) s++;
670 if (!*s || !strcmp(s,"EMPTY"))
671 error("!No SMS set in Nokia found");
672 if (verbose>=1) error("\nFound default SMSC in Nokia: %s",s);
673 if (*s!='"') error("!No left-quote found in: %s",s);
674 if (!(t=strrchr(s+1,'"'))) error("!No right-quote found in: %s",s);
676 while (isspace(*t)) t++;
677 if (*t++!=',') error("!No comma found after quotes in: %s",s);
678 while (isspace(*t)) t++;
679 l=strtol(t,&serr,10);
680 if ((l!=ADDR_NAT && l!=ADDR_INT) || (serr && *serr))
681 error("!Type parse error in: %s",s);
682 if (l==ADDR_NAT || s[1]=='+') s++;
685 if (verbose>=2) error("\nDecoded SMSC address: %s",s);
688 /* Logo format shamelessly stolen from GNokii-0.3.0: http://www.gnokii.org/
689 * Beware - Nokia Smart Messaging specification 1.0.0 and 2.0.0 is incompatible
690 * with Nokia current product line implementation
691 * http://www.forum.nokia.com/developers/smartmsg/download/ssm2_0_0.pdf
694 static char hexdata[140*2+1];
696 static inline void logoread(void)
699 char buf[32+140*8+1];
700 unsigned char bin[140]={
701 0x06, /* UDH length */
704 0x15, 0x83, /* dest port (group gfx) */
705 0x00, 0x00 /* src port (unused) */
712 #define WORD(n) (((unsigned char)buf[(n)])|(((unsigned char)buf[(n)+1])<<8))
714 if (!(f=fopen(logoname,"rb")))
715 error("!Cannot open logo file \"%s\" for r/o: %m",logoname);
716 got=fread(buf,1,sizeof(buf),f);
717 if (got>=20 && !memcmp(buf,"NOL",4)) {
718 VARPRINTF2(gsmnetf,"%03.3u%02.2u",WORD(6),WORD(8));
719 assert(strlen(gsmnetf)==5);
721 if (verbose>=1) error(".Reading NOL file \"%s\", GSMnet \"%s\", word@4=%d..",
722 logoname,gsmnetf,WORD(4));
724 else if (got>=16 && !memcmp(buf,"NGG",4)) {
726 if (verbose>=1) error(".Reading NGG file \"%s\", word@4=%d..",
727 logoname,gsmnetf,WORD(4));
729 else error("!Unknown file format of logo file \"%s\"");
730 if (!strcasecmp(gsmnet,WORD_NET)) {
731 if (!*gsmnetf) error("!NOL network code detection requested but NOL file not loaded, please specify network code");
734 if (!strcasecmp(gsmnet,WORD_GROUP) || !*gsmnet) {
735 error("\nSending logo as: group graphics");
739 error("\nSending logo as: operator logo for \"%s\"",gsmnet);
740 bin[4]=0x82; /* dest port 0x1582 */
743 sizex=WORD(r); sizey=WORD(r+2);
744 if (verbose>=2) error(".Magic words: @+4=%d, @+6=%d, @+8=%d",
745 WORD(r+4),WORD(r+6),WORD(r+8));
747 if (sizex<1 || sizex>255
748 || sizey<1 || sizey>255) error("!Invalid size: %dx%d",sizex,sizey);
749 chars=((bits=sizex*sizey)+7)/8;
750 if (r+bits>got) error("!Logo file \"%s\" too short - actual=%d, need(%dx%d)=%d",
751 logoname,got,sizex,sizey,r+chars);
753 if (verbose>=1) error("Ignoring trailing garbage in \"%s\", used only %d bytes",logoname,r+bits);
754 if ((got=(7+(gsmnet?3:0)+4+chars))>140)
755 error("!SMS size would be %d bytes but 140 is maximum",got);
758 bin[w++]=((gsmnet[1]&0x0F)<<4)|(gsmnet[0]&0x0F);
759 bin[w++]=0xF0 |(gsmnet[2]&0x0F);
760 bin[w++]=((gsmnet[4]&0x0F)<<4)|(gsmnet[3]&0x0F);
762 bin[w++]=0x00; /* RFU by Nokia */
763 bin[w++]=sizex; bin[w++]=sizey;
764 bin[w++]=0x01; /* one B/W plane */
767 for (bit=0x80;(bits>0) && (bit>0);bits--,bit>>=1) {
768 if (buf[r]!='0' && buf[r]!='1')
769 error("!Invalid character (neither '0' nor '1')in logo file \"%s\" at offset 0x%X",
771 if (buf[r++]=='1') bin[w]|=bit;
775 assert(chars==-1); assert(bits==0); assert(w==got); assert(w<=140);
776 textconv(hexdata,bin,w);
777 if (verbose>=2) error("\nWill send hexdata: %s",hexdata);
783 const char *const msg;
785 { &maxretry,&maxretryn,"maxretry" },
786 { &readtime,&readtimen,"readtime" },
787 { &chartime,&chartimen,"chartime" },
788 { &cmdtime ,&cmdtimen ,"cmdtime" },
789 { &baud ,&baudn ,"baud" },
792 int main(int argc,char **argv)
799 if ((s=strrchr((pname=*argv),'/'))) pname=s+1;
801 signal(SIGTERM,(RETSIGTYPE (*)(int))cleanup);
802 signal(SIGQUIT,(RETSIGTYPE (*)(int))cleanup);
803 signal(SIGINT ,(RETSIGTYPE (*)(int))cleanup);
804 signal(SIGHUP ,(RETSIGTYPE (*)(int))cleanup);
805 processargs(argc,argv,"<command-line>");
806 if ((s=getenv("HOME"))) {
808 char *buf=malloc(l+50);
811 strcpy(buf+l,CONFIG_HOME);
815 readfile(CONFIG_MAIN,1);
817 for (i=0;i<NELEM(nullcheck);i++)
818 if (!*nullcheck[i].var) {
819 error("Missing parameter \"%s\"",nullcheck[i].name);
822 if (fatal) error("!Previous error%s considered unrecoverable",(fatal==1?"":"s"));
823 for (i=0;i<NELEM(emptycheck);i++)
824 if (*emptycheck[i] && !**emptycheck[i]) {
825 free(*emptycheck[i]);
828 if (!logname) logname=DEF_LOGNAME;
829 if (!lockfile) lockfile=DEF_LOCKFILE;
830 if (!device) device=DEF_DEVICE;
833 for (i=0;i<NELEM(numarg);i++) {
835 if (!*numarg[i].sp) continue;
836 *numarg[i].ip=strtol(*numarg[i].sp,&serr,0);
837 if (*numarg[i].ip<0 || *numarg[i].ip>=LONG_MAX || !serr || *serr)
838 error("!Number parse error for parameter \"%s\" of \"%s\" at: %s",
839 numarg[i].msg,*numarg[i].sp,serr);
842 if (!strchr(device,'/')) {
843 size_t l=strlen(device);
844 chk(s=malloc(5+l+1));
850 devicename=strrchr(device,'/')+1; assert(!!(devicename-1));
851 for (i=0,s=lockfile;*s;s++) {
852 if (*s!='%') continue;
854 if (*s=='%') continue;
856 if (i) error("!Only one \"%%s\" permitted in lockfile format-string");
859 error("!Invalid format-character '%c' in lockfile format-string, only \"%%s\" allowed",*s);
863 if (!(logf=fopen(logname,"a")))
864 error("!Error opening log \"%s\" for append: %m",logname);
865 logmsg("Starting up: " PACKAGE " " VERSION);
868 case 2400: portbaud= B2400; break;
869 case 4800: portbaud= B4800; break;
870 case 9600: portbaud= B9600; break;
871 case 19200: portbaud=B19200; break;
872 case 38400: portbaud=B38400; break;
873 case 57600: portbaud=B57600; break;
875 error("!Specified baudrate %d is not supported",baudn);
877 if (verbose>=2) error(".Will use baudrate %d with hexval 0x%X",baudn,portbaud);
879 if (lockfile && *lockfile && VARPRINTF(lockreal,lockfile,devicename)>0) {
881 if (verbose>=1) error(".Locking device \"%s\" by \"%s\"..",device,lockreal);
885 if ((end-=start)>LOCKREPORT)
886 logmsg("Device lock succeeded after %d seconds",end);
888 if (verbose>=1) error(".Opening device \"%s\"..",device);
889 if ((devfd=open(device,O_RDWR|O_NDELAY))<0)
890 error("!Cannot open device \"%s\" for rw-access: %m",device);
892 if (tcgetattr(devfd,&restios))
893 error("Unable to get termios settings: %m");
895 restios.c_cflag=(restios.c_cflag&~(CBAUD|CBAUDEX))|B0|HUPCL;
898 tios.c_iflag=IGNBRK|IGNPAR|IXON|IXOFF;
900 tios.c_cflag=CS8|CREAD|CLOCAL|HUPCL|portbaud;
901 tios.c_lflag=IEXTEN|NOFLSH;
902 memset(tios.c_cc,_POSIX_VDISABLE,sizeof(tios.c_cc));
905 cfsetispeed(&tios,portbaud);
906 if (cfsetospeed(&tios,portbaud)|cfsetispeed(&tios,portbaud))
907 error("Error setting termios baudrate on device: %m");
908 if (tcflush(devfd,TCIOFLUSH))
909 error("Error flushing termios (TCIOFLUSH) on device: %m");
910 if (tcsetattr(devfd,TCSANOW,&tios))
911 error("!Unable to set initial termios device settings: %m");
913 signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
915 devcmd("",NULL,"\r\nAT\033");
916 devcmd(NULL,NULL,"\r\nAT");
918 devcmd(NULL,NULL,"\r\nAT+CSMP=81,,0,245");
919 devcmd("\n> ",NULL,"\r\nAT+CMGS=\"%s\"",phone);
920 if (!(s=devcmd(NULL,"\n+CMGS:","!%s\032",hexdata))) {
924 devcmd(NULL,NULL,"\r\nAT+CSMP=17,,0,0");
925 devcmd(NULL,NULL,"\r\nAT");
927 while (isspace(*s)) s++;
928 if (verbose>=1) error("\nMessage successfuly sent with MR: %s",s);
929 devcmd(NULL,NULL,"\r\nAT");
931 logmsg("SMS sent (after %d retries), message reference: %s",retrycnt,s);
932 return(EXIT_SUCCESS);