2 * Copyright (c) 1997, 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 * i4b daemon - curses fullscreen output
28 * -------------------------------------
32 * last edit-date: [Sat May 13 13:05:36 2006]
34 *---------------------------------------------------------------------------*/
40 #define CHPOS(cfgp) (((cfgp)->isdncontrollerused*2) + (cfgp)->isdnchannelused)
42 static void display_budget(void);
43 static void display_cards(void);
44 static void menuexit(WINDOW *menu_w);
46 /*---------------------------------------------------------------------------*
47 * init curses fullscreen display
48 *---------------------------------------------------------------------------*/
57 initscr(); /* curses init */
59 if((COLS < 80) || (LINES < 24))
61 llog(LL_ERR, "ERROR, minimal screensize must be 80x24, is %dx%d, terminating!",COLS, LINES);
68 uheight = ncontroller * 2; /* cards * b-channels */
69 lheight = LINES - uheight - 6 + 1; /* rest of display */
71 if((upper_w = newwin(uheight, COLS, UPPER_B, 0)) == NULL)
73 llog(LL_ERR, "ERROR, curses init upper window, terminating!");
77 if((mid_w = newwin(1, COLS, UPPER_B+uheight+1, 0)) == NULL)
79 llog(LL_ERR, "ERROR, curses init mid window, terminating!");
83 if((lower_w = newwin(lheight, COLS, UPPER_B+uheight+3, 0)) == NULL)
85 llog(LL_ERR, "ERROR, curses init lower window, LINES = %d, lheight = %d, uheight = %d, terminating!", LINES, lheight, uheight);
91 snprintf(buffer, sizeof(buffer), "----- isdn controller channel state ------------- isdnd %02d.%02d.%d [pid %d] -", VERSION, REL, STEP, (int)getpid());
93 while(strlen(buffer) < COLS && strlen(buffer) < sizeof(buffer) - 1)
102 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
103 addstr("c tei b remote iface dir outbytes obps inbytes ibps units");
105 snprintf(buffer, sizeof(buffer), "----- isdn userland interface state ------------------------------------------");
106 while(strlen(buffer) < COLS && strlen(buffer) < sizeof(buffer) - 1)
114 snprintf(buffer, sizeof(buffer), "----- isdnd logfile display --------------------------------------------------");
115 while(strlen(buffer) < COLS && strlen(buffer) < sizeof(buffer) - 1)
125 for(i=0, j=0; i <= ncontroller; i++, j+=2)
127 if(isdn_ctrl_tab[i].tei == -1)
128 mvwprintw(upper_w, j, H_CNTL, "%d --- 1 ", i);
130 mvwprintw(upper_w, j, H_CNTL, "%d %3d 1 ", i, isdn_ctrl_tab[i].tei);
131 mvwprintw(upper_w, j+1, H_CNTL, " L12 2 ");
135 for(i=0, j=0; i < nentries; i++) /* walk thru all entries */
137 p = &cfg_entry_tab[i]; /* get ptr to enry */
139 mvwprintw(mid_w, 0, j, "%s%d ", bdrivername(p->usrdevicename), p->usrdeviceunit);
143 j += ((strlen(bdrivername(p->usrdevicename)) + (p->usrdeviceunit > 9 ? 2 : 1) + 1));
147 wmove(lower_w, 0, 0);
153 /*---------------------------------------------------------------------------*
154 * curses menu for fullscreen command mode
155 *---------------------------------------------------------------------------*/
159 static char *menu[WMITEMS] =
161 "1 - (D)isplay refresh",
162 "2 - (H)angup (choose a channel)",
163 "3 - (R)eread config file",
164 "4 - (S)how card types",
165 "5 - (B)udget information",
166 "6 - (Q)uit the program",
173 struct timeval timeout;
175 /* create a new window in the lower screen area */
177 if((menu_w = newwin(WMENU_HGT, WMENU_LEN, WMENU_POSLN, WMENU_POSCO )) == NULL)
179 llog(LL_WRN, "ERROR, curses init menu window!");
183 /* create a border around the window */
185 box(menu_w, '|', '-');
190 mvwaddstr(menu_w, 0, (WMENU_LEN / 2) - (strlen(WMENU_TITLE) / 2), WMENU_TITLE);
193 /* fill the window with the menu options */
195 for(mpos=0; mpos <= (WMITEMS-1); mpos++)
196 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
198 /* highlight the first menu option */
202 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
212 FD_SET(STDIN_FILENO, &set);
213 timeout.tv_sec = WMTIMEOUT;
216 /* if no char is available within timeout, exit menu*/
218 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
226 case '\t': /* hilite next option */
227 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
232 mvwaddstr(menu_w, mpos + 2, 2, menu[mpos]);
236 case ('0'+WBUDGET+1): /* display budget info */
242 case ('0'+WREFRESH+1): /* display refresh */
248 case ('0'+WQUIT+1): /* quit program */
255 case ('0'+WHANGUP+1): /* hangup connection */
261 case ('0'+WREREAD+1): /* reread config file */
267 case ('0'+WSHOW+1): /* reread config file */
274 case '\r': /* exec highlighted option */
316 menuexit(WINDOW *menu_w)
318 int uheight = ncontroller * 2; /* cards * b-channels */
321 /* delete the menu window */
325 /* re-display the original lower window contents */
337 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
338 addstr("c tei b remote iface dir outbytes obps inbytes ibps units");
340 sprintf(buffer, "----- isdn userland interface state ------------------------------------------");
341 while(strlen(buffer) < COLS)
349 sprintf(buffer, "----- isdnd logfile display --------------------------------------------------");
350 while(strlen(buffer) < COLS)
361 /*---------------------------------------------------------------------------*
362 * display the charge in units
363 *---------------------------------------------------------------------------*/
365 display_charge(cfg_entry_t *cep)
367 mvwprintw(upper_w, CHPOS(cep), H_UNITS, "%d", cep->charge);
372 /*---------------------------------------------------------------------------*
373 * display the calculated charge in units
374 *---------------------------------------------------------------------------*/
376 display_ccharge(cfg_entry_t *cep, int units)
378 mvwprintw(upper_w, CHPOS(cep), H_UNITS, "(%d)", units);
383 /*---------------------------------------------------------------------------*
384 * display accounting information
385 *---------------------------------------------------------------------------*/
387 display_acct(cfg_entry_t *cep)
389 mvwprintw(upper_w, CHPOS(cep), H_OUT, "%-10d", cep->outbytes);
390 mvwprintw(upper_w, CHPOS(cep), H_OUTBPS, "%-4d", cep->outbps);
391 mvwprintw(upper_w, CHPOS(cep), H_IN, "%-10d", cep->inbytes);
392 mvwprintw(upper_w, CHPOS(cep), H_INBPS, "%-4d", cep->inbps);
396 /*---------------------------------------------------------------------------*
397 * display connect information
398 *---------------------------------------------------------------------------*/
400 display_connect(cfg_entry_t *cep)
404 /* remote telephone number */
408 if(cep->direction == DIR_IN)
409 snprintf(buffer, sizeof(buffer), "%s", get_alias(cep->real_phone_incoming.number));
411 snprintf(buffer, sizeof(buffer), "%s", get_alias(cep->remote_phone_dialout.number));
415 if(cep->direction == DIR_IN)
416 snprintf(buffer, sizeof(buffer), "%s/%s", cep->name, cep->real_phone_incoming.number);
418 snprintf(buffer, sizeof(buffer), "%s/%s", cep->name, cep->remote_phone_dialout.number);
421 buffer[H_IFN - H_TELN - 1] = '\0';
423 mvwprintw(upper_w, CHPOS(cep), H_TELN, "%s", buffer);
427 mvwprintw(upper_w, CHPOS(cep), H_IFN, "%s%d ",
428 bdrivername(cep->usrdevicename), cep->usrdeviceunit);
430 mvwprintw(upper_w, CHPOS(cep), H_IO,
431 cep->direction == DIR_OUT ? "out" : "in");
433 mvwprintw(upper_w, CHPOS(cep), H_OUT, "-");
434 mvwprintw(upper_w, CHPOS(cep), H_OUTBPS, "-");
435 mvwprintw(upper_w, CHPOS(cep), H_IN, "-");
436 mvwprintw(upper_w, CHPOS(cep), H_INBPS, "-");
444 /*---------------------------------------------------------------------------*
445 * erase line at disconnect time
446 *---------------------------------------------------------------------------*/
448 display_disconnect(cfg_entry_t *cep)
450 wmove(upper_w, CHPOS(cep),
460 /*---------------------------------------------------------------------------*
461 * display interface up/down information
462 *---------------------------------------------------------------------------*/
464 display_updown(cfg_entry_t *cep, int updown)
471 mvwprintw(mid_w, 0, cep->fs_position, "%s%d ",
472 bdrivername(cep->usrdevicename), cep->usrdeviceunit);
478 /*---------------------------------------------------------------------------*
479 * display interface up/down information
480 *---------------------------------------------------------------------------*/
482 display_l12stat(int controller, int layer, int state)
484 if(controller > ncontroller)
486 if(!(layer == 1 || layer == 2))
496 mvwprintw(upper_w, (controller*2)+1, H_TEI+1, "1");
498 mvwprintw(upper_w, (controller*2)+1, H_TEI+2, "2");
502 mvwprintw(upper_w, (controller*2)+1, H_TEI+2, "2");
504 mvwprintw(upper_w, (controller*2)+1, H_TEI+1, "1");
511 /*---------------------------------------------------------------------------*
513 *---------------------------------------------------------------------------*/
515 display_tei(int controller, int tei)
517 if(controller > ncontroller)
521 mvwprintw(upper_w, controller*2, H_TEI, "---");
523 mvwprintw(upper_w, controller*2, H_TEI, "%3d", tei);
528 /*---------------------------------------------------------------------------*
530 *---------------------------------------------------------------------------*/
534 static char bell[1] = { 0x07 };
535 write(STDOUT_FILENO, &bell[0], 1);
538 /*---------------------------------------------------------------------------*
539 * display channel information for shutdown
540 *---------------------------------------------------------------------------*/
548 int nlines, ncols, pos_x, pos_y;
550 struct timeval timeout;
551 cfg_entry_t *cep = NULL;
553 /* need this later to close the connection */
559 for (i = 0; i < ncontroller; i++)
561 if((get_controller_state(i)) != CTRL_UP)
563 if((ret_channel_state(i, CHAN_B1)) == CHAN_RUN)
565 if((ret_channel_state(i, CHAN_B2)) == CHAN_RUN)
571 if ((cc = (struct ctlr_chan *)malloc (cnt *
572 sizeof (struct ctlr_chan))) == NULL)
585 pos_y = WMENU_POSLN + 4;
586 pos_x = WMENU_POSCO + 10;
588 /* create a new window in the lower screen area */
590 if((chan_w = newwin(nlines, ncols, pos_y, pos_x )) == NULL)
592 llog(LL_WRN, "ERROR, curses init channel window!");
598 /* create a border around the window */
600 box(chan_w, '|', '-');
605 mvwaddstr(chan_w, 0, (ncols / 2) - (strlen("Channels") / 2), "Channels");
608 /* no active channels */
611 mvwaddstr(chan_w, 2, 2, "No active channels");
615 /* delete the channels window */
624 for (i = 0; i < ncontroller; i++)
626 if((get_controller_state(i)) != CTRL_UP)
629 if((ret_channel_state(i, CHAN_B1)) == CHAN_RUN)
631 snprintf(buffer, sizeof(buffer), "%d - Controller %d channel %s", ncols, i, "B1");
632 mvwaddstr(chan_w, nlines, 2, buffer);
633 cc[ncols - 1].cntl = i;
634 cc[ncols - 1].chn = CHAN_B1;
638 if((ret_channel_state(i, CHAN_B2)) == CHAN_RUN)
640 snprintf(buffer, sizeof(buffer), "%d - Controller %d channel %s", ncols, i, "B2");
641 mvwaddstr(chan_w, nlines, 2, buffer);
642 cc[ncols - 1].cntl = i;
643 cc[ncols - 1].chn = CHAN_B2;
654 FD_SET(STDIN_FILENO, &set);
655 timeout.tv_sec = WMTIMEOUT;
658 /* if no char is available within timeout, exit menu*/
660 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
663 ncols = wgetch(chan_w);
665 if (!(isdigit(ncols)))
671 nlines = ncols - '0';
673 if ((nlines == 0) || (nlines > cnt))
679 if((cep = get_cep_by_cc(cc[nlines-1].cntl, cc[nlines-1].chn))
682 llog(LL_CHD, "%05d %s manual disconnect (fullscreen menu)", cep->cdid, cep->name);
690 /* delete the channels window */
695 /*---------------------------------------------------------------------------*
696 * display card type information
697 *---------------------------------------------------------------------------*/
702 int nlines, ncols, pos_x, pos_y;
704 struct timeval timeout;
707 nlines = 6+ncontroller;
712 /* create a new window in the lower screen area */
714 if((chan_w = newwin(nlines, ncols, pos_y, pos_x )) == NULL)
716 llog(LL_WRN, "ERROR, curses init channel window!");
720 /* create a border around the window */
722 box(chan_w, '|', '-');
727 mvwaddstr(chan_w, 0, (ncols / 2) - (strlen("Cards") / 2), "Cards");
730 mvwprintw(chan_w, 2, 2, "ctrl description");
731 mvwprintw(chan_w, 3, 2, "---- ----------------------------------------------");
732 for (i = 0; i < ncontroller; i++)
734 mvwprintw(chan_w, 4+i, 2, " #%d %s", i,
735 name_of_controller(isdn_ctrl_tab[i].ctrl_type,
736 isdn_ctrl_tab[i].card_type));
742 FD_SET(STDIN_FILENO, &set);
743 timeout.tv_sec = WMTIMEOUT*2;
746 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)
756 /*---------------------------------------------------------------------------*
757 * display budget info
758 *---------------------------------------------------------------------------*/
763 int nlines, ncols, pos_x, pos_y;
765 struct timeval timeout;
777 pos_x = WMENU_POSCO-3;
779 for(i=0, j=0; i < nentries; i++) /* walk thru all entries */
781 cep = &cfg_entry_tab[i]; /* get ptr to entry */
783 if(cep->budget_callbackperiod && cep->budget_callbackncalls)
785 if(cep->budget_calloutperiod && cep->budget_calloutncalls)
794 /* create a new window in the lower screen area */
796 if((bud_w = newwin(nlines, ncols, pos_y, pos_x )) == NULL)
798 llog(LL_WRN, "ERROR, curses init budget window!");
803 uptime = difftime(now, starttime);
805 minutes = (time_t) (uptime / 60) % 60;
806 hours = (time_t) (uptime / (60*60)) % (60*60);
807 days = (time_t) (uptime / (60*60*24)) % (60*60*24);
809 uptime = uptime / (60*60);
811 /* create a border around the window */
813 box(bud_w, '|', '-');
818 mvwaddstr(bud_w, 0, (ncols / 2) - (strlen("Budget") / 2), "Budget");
821 mvwprintw(bud_w, 1, 2, "isdnd uptime: %d %s - %d %s - %d %s",
823 days == 1 ? "day" : "days",
825 hours == 1 ? "hour" : "hours",
827 minutes == 1 ? "minute" : "minutes");
829 mvwprintw(bud_w, 2, 2, "name t period rest ncall rest rqsts /hr rdone /hr rrjct /hr ");
830 mvwprintw(bud_w, 3, 2, "-------- - ------ ------ ----- ----- ----- ---- ----- ---- ----- ----");
832 for(i=0, j=4; i < nentries; i++) /* walk thru all entries */
834 cep = &cfg_entry_tab[i]; /* get ptr to enry */
836 if(cep->budget_calloutperiod && cep->budget_calloutncalls)
838 mvwprintw(bud_w, j, 2, "%-8s %c %-6d %-6ld %-5d %-5d %-5d %-4.1f %-5d %-4.1f %-5d %-4.1f",
841 cep->budget_calloutperiod,
842 (long)(cep->budget_calloutperiod_time - now),
843 cep->budget_calloutncalls,
844 cep->budget_calloutncalls_cnt,
845 cep->budget_callout_req,
846 (double)cep->budget_callout_req / uptime,
847 cep->budget_callout_done,
848 (double)cep->budget_callout_done / uptime,
849 cep->budget_callout_rej,
850 (double)cep->budget_callout_rej / uptime);
853 if(cep->budget_callbackperiod && cep->budget_callbackncalls)
855 mvwprintw(bud_w, j, 2, "%-8s %c %-6d %-6ld %-5d %-5d %-5d %-4.1f %-5d %-4.1f %-5d %-4.1f",
856 (cep->budget_calloutperiod && cep->budget_calloutncalls) ? "" : cep->name,
858 cep->budget_callbackperiod,
859 (long)(cep->budget_callbackperiod_time - now),
860 cep->budget_callbackncalls,
861 cep->budget_callbackncalls_cnt,
862 cep->budget_callback_req,
863 (double)cep->budget_callback_req / uptime,
864 cep->budget_callback_done,
865 (double)cep->budget_callback_done / uptime,
866 cep->budget_callback_rej,
867 (double)cep->budget_callback_rej / uptime);
875 FD_SET(STDIN_FILENO, &set);
876 timeout.tv_sec = WMTIMEOUT*3;
879 if((select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout)) <= 0)