2 * Copyright (c) 1999, 2002 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 *---------------------------------------------------------------------------
27 * isdnphone - main module
28 * =======================
32 * last edit-date: [Tue Aug 27 16:38:55 2002]
34 *---------------------------------------------------------------------------*/
39 static void kbd_hdlr(void);
41 /*---------------------------------------------------------------------------*
42 * usage display and exit
43 *---------------------------------------------------------------------------*/
47 fprintf(stderr, "\n");
48 fprintf(stderr, "isdnphone - i4b phone program, version %d.%d.%d\n",VERSION, REL, STEP);
49 fprintf(stderr, "usage: isdnphone -d -h -k <string> -n <number> -u <unit> -v -w\n");
50 fprintf(stderr, " -d debug\n");
51 fprintf(stderr, " -h hangup\n");
52 fprintf(stderr, " -k string keypad string\n");
53 fprintf(stderr, " -n number dial number\n");
54 fprintf(stderr, " -u unit set unit number\n");
55 fprintf(stderr, " -v be verbose\n");
56 fprintf(stderr, " -w wait for response (with -n)\n");
57 fprintf(stderr, "\n");
61 /*---------------------------------------------------------------------------*
63 *---------------------------------------------------------------------------*/
65 main(int argc, char **argv)
80 numberbuffer[0] = '\0';
82 while ((c = getopt(argc, argv, "dhk:n:s:u:w")) != -1)
110 opt_unit = atoi(optarg);
111 if(opt_unit < 0 || opt_unit > 9)
130 sprintf(namebuffer,"%s%d", I4BTELDDEVICE, opt_unit);
132 if((dialerfd = init_dial(namebuffer)) == -1)
135 if(opt_n || opt_h || opt_k)
137 char commandbuffer[80];
140 /* commandline operation goes here */
145 sprintf(commandbuffer, "D%s*%s", number, subaddr);
147 sprintf(commandbuffer, "D%s", number);
152 sprintf(commandbuffer, "K%s", number);
157 sprintf(commandbuffer, "H");
160 if((ret = write(dialerfd, commandbuffer, strlen(commandbuffer))) < 0)
162 fprintf(stderr, "write commandbuffer failed: %s", strerror(errno));
170 if((read (dialerfd, &result, 1) < 0))
175 fprintf(stderr, "error, read failed: %s\n", strerror(errno));
184 printf("connected\n");
196 printf("disconnected\n");
202 printf("noanswer\n");
209 fprintf(stderr, "unknown response = 0x%2x!", result);
220 if((audiofd = init_audio(AUDIODEVICE)) == -1)
223 /* fullscreen operation here */
227 bschar = erasechar();
230 wmove(main_w, MW_NUMY, MW_NUX + curx);
238 struct timeval timeout;
242 FD_SET(STDIN_FILENO, &set);
243 if(STDIN_FILENO > maxfd)
244 maxfd = STDIN_FILENO;
246 FD_SET(dialerfd, &set);
250 if(state == ST_ACTIVE)
254 FD_SET(audiofd, &set);
272 /* if no char is available within timeout, do something */
275 ret = select(maxfd+1, &set, NULL, NULL, &timeout);
277 ret = select(maxfd+1, &set, NULL, NULL, NULL);
282 if((telfd != -1) && (FD_ISSET(telfd, &set)))
284 message("select from ISDN");
287 if((audiofd != -1) && (FD_ISSET(audiofd, &set)))
289 message("select from audio");
292 if(FD_ISSET(dialerfd, &set))
294 message("select from tel");
297 if(FD_ISSET(STDIN_FILENO, &set))
299 message("select from kbd");
309 /*---------------------------------------------------------------------------*
310 * keyboard character available handler
311 *---------------------------------------------------------------------------*/
317 kchar = wgetch(main_w); /* get char */
326 if((state == ST_IDLE) &&
327 (numberbuffer[0] != '\0'))
329 message("dialing .....");
330 do_dial(&numberbuffer[0]);
345 message("cannot exit while not idle!");
351 case CNTRL_L: /* refresh */
362 mvwaddch(main_w, MW_NUMY, MW_NUX + curx, ' ');
363 numberbuffer[curx] = '\0';
364 wmove(main_w, MW_NUMY, MW_NUX + curx);
381 if(curx > (TELNO_MAX-1))
384 mvwaddch(main_w, MW_NUMY, MW_NUX + curx, kchar);
386 numberbuffer[curx] = kchar;
390 numberbuffer[curx] = '\0';
392 message("press ENTER to dial number .....");
397 /*---------------------------------------------------------------------------*
399 *---------------------------------------------------------------------------*/
411 /*---------------------------------------------------------------------------*
413 *---------------------------------------------------------------------------*/
415 fatal(char *fmt, ...)
421 do_hangup(); /* failsafe */
432 fprintf(stderr, "\nFatal error: ");
433 vfprintf(stderr, fmt, ap);
434 fprintf(stderr, "\n\n");
441 /*---------------------------------------------------------------------------*
443 *---------------------------------------------------------------------------*/
445 message(char *fmt, ...)
456 wmove(main_w, MW_MSGY, MW_MSX);
457 vsnprintf(sbuf, MW_WIDTH-MW_MSX-1, fmt, ap);
458 waddstr(main_w, sbuf);
459 for(i=strlen(sbuf);i < MW_WIDTH-MW_MSX-2; i++)
461 wmove(main_w, MW_NUMY, MW_NUX + curx);
466 fprintf(stderr, "ERROR: ");
467 vfprintf(stderr, fmt, ap);
468 fprintf(stderr, "\n");
474 /*---------------------------------------------------------------------------*
476 *---------------------------------------------------------------------------*/
478 debug(char *fmt, ...)
487 vwprintw(dbg_w, fmt, ap);
493 /*---------------------------------------------------------------------------*
495 *---------------------------------------------------------------------------*/
497 newstate(int newstate)
501 if(newstate < 0 || newstate > ST_MAX)
503 message("newstate %d undefined!", newstate);
509 if(newstate == ST_ACTIVE)
511 char namebuffer[128];
513 sprintf(namebuffer,"%s%d", I4BTELDEVICE, opt_unit);
514 telfd = init_tel(namebuffer);
517 if(newstate == ST_IDLE)
523 wmove(main_w, MW_STATEY, MW_STX);
524 waddstr(main_w, states[newstate]);
526 for(i=strlen(states[newstate]);i < MW_WIDTH-MW_STX-2; i++)
529 wmove(main_w, MW_NUMY, MW_NUX + curx);