]> git.decadent.org.uk Git - ap-utils.git/blobdiff - lib/stat.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / lib / stat.c
index c3d969ca7ce3b6d3bacc3c995c6d99cb286765d4..7ebe726a926bc266899692df6468de6358ade8e1 100644 (file)
 #include <fcntl.h>
 #include <signal.h>
 #include <string.h>
+#include <sys/time.h>
 #include <sys/wait.h>
 #include "ap-utils.h"
-#include "ap-curses.h"
 
-#define ERR_RET_PRESS _("Unable to get data from AP. Press Q to continue.")
-#define QHELP _("Q - quit to menu.")
-#define PIPE _("pipe error. Press any key.")
-#define FCNTL _("fcntl error. Press any key.")
-#define FORK _("fork error. Press any key.")
+#define ETH_STAT _("Ethernet Statistics")
+#define W_STAT _("Wireless Statistics")
 
 extern WINDOW *main_sub;
 extern short ap_type;
+extern int wait_mode;
 
 void EthStat()
 {
@@ -47,11 +45,9 @@ void EthStat()
     char EthTx[] = { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01,
        0x07, 0x02, 0x00
     };
-    char c;
     char message[1024];
-    pid_t pid;
-    int j, child_pipe[2];
     varbind varbinds[2];
+    int i;
 
     if (ap_type == ATMEL12350) {
        EthRx[5] = 0xE0;
@@ -60,43 +56,37 @@ void EthStat()
        EthTx[6] = 0x3E;
     }
     
-    if (pipe(child_pipe) == -1) {
-       print_helperr(PIPE);
-       goto exit;
-    }
-
-    if (fcntl(child_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
-       print_helperr(FCNTL);
-       goto exit;
-    }
-    print_title(_("Ethernet Statistics"));
-    print_help(QHELP);
+    print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, ETH_STAT);
     noecho();
 
-    switch (pid = fork()) {
-    case -1:
-       print_helperr(FORK);
-       goto exit;
-    case 0:
-       close(child_pipe[1]);
-       varbinds[0].oid = EthRx;
-       varbinds[0].len_oid = sizeof(EthRx);
-       varbinds[1].oid = EthTx;
-       varbinds[1].len_oid = sizeof(EthTx);
-
-      start:
-       varbinds[0].value = EthTx;
+    if (wait_mode == WAIT_TIMEOUT)
+        print_help(QT_HELP);
+
+    varbinds[0].oid = EthRx;
+    varbinds[0].len_oid = sizeof(EthRx);
+    varbinds[1].oid = EthTx;
+    varbinds[1].len_oid = sizeof(EthTx);
+
+    while (1) {
+//     varbinds[0].value = EthTx;
        varbinds[0].len_val = 0;
        varbinds[0].type = NULL_VALUE;
-       varbinds[1].value = EthTx;
+//     varbinds[1].value = EthTx;
        varbinds[1].len_val = 0;
        varbinds[1].type = NULL_VALUE;
 
+       if (wait_mode == WAIT_FOREVER)
+           print_help(WAIT_RET);
+
        if (snmp(varbinds, 2, GET) <= 0) {
-           print_helperr(ERR_RET_PRESS);
-           goto exit_child;
+           print_helperr(ERR_RET);
+           getch();
+           goto quit;
        }
 
+       if (wait_mode == WAIT_FOREVER)
+           print_help(QT_HELP);
+
        if (varbinds[0].len_val == 64) {
            if (EthRxStat)
                free(EthRxStat);
@@ -105,8 +95,9 @@ void EthStat()
                                                       len_val);
            memcpy(EthRxStat, varbinds[0].value, varbinds[0].len_val);
        } else {
-           print_helperr(_("EthRxStat packet error. Press Q to continue."));
-           goto exit_child;
+           print_helperr(_("EthRxStat packet error. Press any key."));
+           getch();
+           goto quit;
        }
 
        if (varbinds[1].len_val == 56) {
@@ -117,8 +108,9 @@ void EthStat()
                                                       len_val);
            memcpy(EthTxStat, varbinds[1].value, varbinds[1].len_val);
        } else {
-           print_helperr(_("EthTxStat packet error. Press Q to continue."));
-           goto exit_child;
+           print_helperr(_("EthTxStat packet error. Press any key."));
+           getch();
+           goto quit;
        }
        mvwaddstr(main_sub, 1, 2, _("Received:"));
        mvwaddstr(main_sub, 1, 30, _("Transmitted:"));
@@ -188,26 +180,26 @@ void EthStat()
                swap4(EthTxStat->TotalCollisionsTx));
        mvwaddstr(main_sub, 18, 2, message);
        wrefresh(main_sub);
-      exit_child:
-       switch (j = read(child_pipe[0], &c, 1)) {
-       case -1:
-           break;
-       default:
-           exit(0);
+
+       i = wait_key();
+       if (i == -1)
+           goto quit;
+
+       switch((char) i) {
+           case 'q':
+           case 'Q':
+               goto quit;
+           case 't':
+           case 'T':
+               wait_mode = (wait_mode == WAIT_FOREVER ?
+                   WAIT_TIMEOUT : WAIT_FOREVER);
+               print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
+                   ETH_STAT);
        }
-       sleep(1);
-       goto start;
-    default:
-       close(child_pipe[0]);
-       while (getch() != 'q');
-       write(child_pipe[1], "0", 1);
-       wait((int *) 0);
-       close(child_pipe[1]);
-       goto quit;
+
+       /* either timeout for user input (i == 0) or invalid key => continue */
     }
 
-  exit:
-    getch();
   quit:
     if (EthRxStat)
        free(EthRxStat);
@@ -215,7 +207,7 @@ void EthStat()
        free(EthTxStat);
 
     wclear(main_sub);
-    print_title("");
+    print_top(NULL, NULL);
     clear_main(0);
     return;
 }
@@ -228,10 +220,8 @@ void WirelessStat()
        { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x02, 0x03, 0x01,
        0x00
     };
-    char message[80], c;
-    pid_t pid;
-    int j, child_pipe[2];
-
+    char message[80];
+    int i;
     varbind varbinds[1];
 
     if (ap_type == ATMEL12350) {
@@ -239,39 +229,37 @@ void WirelessStat()
        Wireless[6] = 0x3E;
     }
 
-    if (pipe(child_pipe) == -1) {
-       print_helperr(PIPE);
-       goto exit;
-    }
-
-    if (fcntl(child_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
-       print_helperr(FCNTL);
-       goto exit;
-    }
-    print_title(_("Wireless Statistics"));
-    print_help(QHELP);
+    print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, W_STAT);
     noecho();
 
-    switch (pid = fork()) {
-    case -1:
-       print_helperr(FORK);
-       goto exit_child;
-    case 0:
-       close(child_pipe[1]);
-       varbinds[0].oid = Wireless;
-       varbinds[0].len_oid = sizeof(Wireless);
+    if (wait_mode == WAIT_TIMEOUT)
+       print_help(QT_HELP);
+
+    varbinds[0].oid = Wireless;
+    varbinds[0].len_oid = sizeof(Wireless);
 
-      start:
+    while (1) {
        varbinds[0].value = Wireless;
        varbinds[0].len_val = 0;
        varbinds[0].type = NULL_VALUE;
 
+       if (wait_mode == WAIT_FOREVER)
+           print_help(WAIT_RET);
+
        if (snmp(varbinds, 1, GET) <= 0) {
-           print_helperr(ERR_RET_PRESS);
-           goto exit_child;
+           print_helperr(ERR_RET);
+           getch();
+           goto quit;
        }
 
-       if (varbinds[0].len_val == 88) {
+       if (wait_mode == WAIT_FOREVER)
+           print_help(QT_HELP);
+
+       if (varbinds[0].len_val == 88 || varbinds[0].len_val == 104) {
+           /*
+            * 88 ... using traditional ATMEL 12350 MIB
+            * 104 .. using functionally enhanced ATMEL 12350 MIB by VERNET
+            */
            if (WirelessStat)
                free(WirelessStat);
            WirelessStat =
@@ -279,8 +267,9 @@ void WirelessStat()
            memcpy(WirelessStat, varbinds[0].value, varbinds[0].len_val);
        } else {
            print_helperr
-               (_("WirelessStat packet error. Press Q to continue."));
-           goto exit;
+               (_("WirelessStat packet error. Press any key."));
+           getch();
+           goto quit;
        }
 
        sprintf(message,
@@ -343,32 +332,48 @@ void WirelessStat()
                swap4(WirelessStat->InvalidPLCP));
        mvwaddstr(main_sub, 15, 1, message);
 
+       /* ATMEL12350 MIB 'VERNET' modification has in addition the following */
+       if (varbinds[0].len_val == 104) {
+           mvwaddstr(main_sub, 16, 1, "TransmittedPackets:");
+           sprintf(message, "* at 11 Mbps       %10u",
+               swap4(WirelessStat->TransmittedPackets_11Mbps));
+           mvwaddstr(main_sub, 17, 1, message);
+           sprintf(message, "* at 5.5 Mbps      %10u",
+               swap4(WirelessStat->TransmittedPackets_55Mbps));
+           mvwaddstr(main_sub, 18, 1, message);
+           sprintf(message, "* at 2 Mbps        %10u",
+               swap4(WirelessStat->TransmittedPackets_2Mbps));
+           mvwaddstr(main_sub, 19, 1, message);
+           sprintf(message, "* at 1 Mbps        %10u",
+               swap4(WirelessStat->TransmittedPackets_1Mbps));
+           mvwaddstr(main_sub, 20, 1, message);
+       }
        wrefresh(main_sub);
-      exit_child:
-       switch (j = read(child_pipe[0], &c, 1)) {
-       case -1:
-           break;
-       default:
-           exit(0);
+
+       i = wait_key();
+       if (i == -1)
+           goto quit;
+
+       switch((char) i) {
+           case 'q':
+           case 'Q':
+               goto quit;
+           case 't':
+           case 'T':
+               wait_mode = (wait_mode == WAIT_FOREVER ?
+                   WAIT_TIMEOUT : WAIT_FOREVER);
+               print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
+                   W_STAT);
        }
-       sleep(1);
-       goto start;
-    default:
-       close(child_pipe[0]);
-       while (getch() != 'q');
-       write(child_pipe[1], "0", 1);
-       wait((int *) 0);
-       close(child_pipe[1]);
-       goto quit;
+
+       /* either timeout for user input (i == 0) or invalid key => continue */
     }
 
-  exit:
-    getch();
   quit:
     if (WirelessStat)
        free(WirelessStat);
     wclear(main_sub);
-    print_title("");
+    print_top(NULL, NULL);
     clear_main(0);
 }
 
@@ -408,78 +413,70 @@ void nwn_wireless_stat()
     char oid_dot11WEPExcludedCount[] =
        { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x05, 0x01, 0x06, 0x01 };
 
-    char message[80], c;
-    pid_t pid;
-    int j, child_pipe[2], i;
+    char message[80];
+    int i;
 
     varbind varbinds[16];
     curs_set(0);
 
-    if (pipe(child_pipe) == -1) {
-       print_helperr(_("pipe error. Press any key"));
-       goto exit;
-    }
-
-    if (fcntl(child_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
-       print_helperr(_("fcntl error. Press any key"));
-       goto exit;
-    }
-    print_title(_("Wireless Statistics"));
-    print_help(_("Q - quit to menu."));
+    print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, W_STAT);
     noecho();
 
-    switch (pid = fork()) {
-    case -1:
-       print_helperr(_("fork error. Press any key"));
-       goto exit_child;
-    case 0:
-       close(child_pipe[1]);
-       varbinds[0].oid = oid_dot11TransmittedFragmentCount;
-       varbinds[0].len_oid = sizeof(oid_dot11TransmittedFragmentCount);
-       varbinds[1].oid = oid_dot11MulticastTransmittedFrameCount;
-       varbinds[1].len_oid =
-           sizeof(oid_dot11MulticastTransmittedFrameCount);
-       varbinds[2].oid = oid_dot11FailedCount;
-       varbinds[2].len_oid = sizeof(oid_dot11FailedCount);
-       varbinds[3].oid = oid_dot11RetryCount;
-       varbinds[3].len_oid = sizeof(oid_dot11RetryCount);
-       varbinds[4].oid = oid_dot11MultipleRetryCount;
-       varbinds[4].len_oid = sizeof(oid_dot11MultipleRetryCount);
-       varbinds[5].oid = oid_dot11FrameDuplicateCount;
-       varbinds[5].len_oid = sizeof(oid_dot11FrameDuplicateCount);
-       varbinds[6].oid = oid_dot11RTSSuccessCount;
-       varbinds[6].len_oid = sizeof(oid_dot11RTSSuccessCount);
-       varbinds[7].oid = oid_dot11RTSFailureCount;
-       varbinds[7].len_oid = sizeof(oid_dot11RTSFailureCount);
-       varbinds[8].oid = oid_dot11ACKFailureCount;
-       varbinds[8].len_oid = sizeof(oid_dot11ACKFailureCount);
-       varbinds[9].oid = oid_dot11ReceivedFragmentCount;
-       varbinds[9].len_oid = sizeof(oid_dot11ReceivedFragmentCount);
-       varbinds[10].oid = oid_dot11MulticastReceivedFrameCount;
-       varbinds[10].len_oid =
-           sizeof(oid_dot11MulticastReceivedFrameCount);
-       varbinds[11].oid = oid_dot11FCSErrorCount;
-       varbinds[11].len_oid = sizeof(oid_dot11FCSErrorCount);
-       varbinds[12].oid = oid_dot11TransmittedFrameCount;
-       varbinds[12].len_oid = sizeof(oid_dot11TransmittedFrameCount);
-       varbinds[13].oid = oid_dot11WEPUndecryptableCount;
-       varbinds[13].len_oid = sizeof(oid_dot11WEPUndecryptableCount);
-       varbinds[14].oid = oid_dot11WEPICVErrorCount;
-       varbinds[14].len_oid = sizeof(oid_dot11WEPICVErrorCount);
-       varbinds[15].oid = oid_dot11WEPExcludedCount;
-       varbinds[15].len_oid = sizeof(oid_dot11WEPExcludedCount);
-
-      start:
+    if (wait_mode == WAIT_TIMEOUT)
+       print_help(QT_HELP);
+
+    varbinds[0].oid = oid_dot11TransmittedFragmentCount;
+    varbinds[0].len_oid = sizeof(oid_dot11TransmittedFragmentCount);
+    varbinds[1].oid = oid_dot11MulticastTransmittedFrameCount;
+    varbinds[1].len_oid = sizeof(oid_dot11MulticastTransmittedFrameCount);
+    varbinds[2].oid = oid_dot11FailedCount;
+    varbinds[2].len_oid = sizeof(oid_dot11FailedCount);
+    varbinds[3].oid = oid_dot11RetryCount;
+    varbinds[3].len_oid = sizeof(oid_dot11RetryCount);
+    varbinds[4].oid = oid_dot11MultipleRetryCount;
+    varbinds[4].len_oid = sizeof(oid_dot11MultipleRetryCount);
+    varbinds[5].oid = oid_dot11FrameDuplicateCount;
+    varbinds[5].len_oid = sizeof(oid_dot11FrameDuplicateCount);
+    varbinds[6].oid = oid_dot11RTSSuccessCount;
+    varbinds[6].len_oid = sizeof(oid_dot11RTSSuccessCount);
+    varbinds[7].oid = oid_dot11RTSFailureCount;
+    varbinds[7].len_oid = sizeof(oid_dot11RTSFailureCount);
+    varbinds[8].oid = oid_dot11ACKFailureCount;
+    varbinds[8].len_oid = sizeof(oid_dot11ACKFailureCount);
+    varbinds[9].oid = oid_dot11ReceivedFragmentCount;
+    varbinds[9].len_oid = sizeof(oid_dot11ReceivedFragmentCount);
+    varbinds[10].oid = oid_dot11MulticastReceivedFrameCount;
+    varbinds[10].len_oid = sizeof(oid_dot11MulticastReceivedFrameCount);
+    varbinds[11].oid = oid_dot11FCSErrorCount;
+    varbinds[11].len_oid = sizeof(oid_dot11FCSErrorCount);
+    varbinds[12].oid = oid_dot11TransmittedFrameCount;
+    varbinds[12].len_oid = sizeof(oid_dot11TransmittedFrameCount);
+    varbinds[13].oid = oid_dot11WEPUndecryptableCount;
+    varbinds[13].len_oid = sizeof(oid_dot11WEPUndecryptableCount);
+    varbinds[14].oid = oid_dot11WEPICVErrorCount;
+    varbinds[14].len_oid = sizeof(oid_dot11WEPICVErrorCount);
+    varbinds[15].oid = oid_dot11WEPExcludedCount;
+    varbinds[15].len_oid = sizeof(oid_dot11WEPExcludedCount);
+
+    while (1) {
        for (i = 0; i < 16; i++) {
            varbinds[i].value = oid_dot11TransmittedFragmentCount;
            varbinds[i].len_val = 0;
            varbinds[i].type = 0x05;
        }
+
+       if (wait_mode == WAIT_FOREVER)
+           print_help(WAIT_RET);
+
        if (snmp(varbinds, 16, GET) <= 0) {
-           print_helperr(ERR_RET_PRESS);
-           goto exit_child;
+           print_helperr(ERR_RET);
+           getch();
+           goto quit;
        }
 
+       if (wait_mode == WAIT_FOREVER)
+           print_help(QT_HELP);
+
        sprintf(message, "FragmentTx       %10lu FragmentRx       %10lu",
                (long) swap4(*varbinds[0].value), (long) swap4(*varbinds[9].value));
        mvwaddstr(main_sub, 1, 1, message);
@@ -512,28 +509,29 @@ void nwn_wireless_stat()
                (long) swap4(*(varbinds[14].value)));
        mvwaddstr(main_sub, 10, 1, message);
        wrefresh(main_sub);
-      exit_child:
-       switch (j = read(child_pipe[0], &c, 1)) {
-       case -1:
-           break;
-       default:
-           exit(0);
+
+       i = wait_key();
+       if (i == -1)
+           goto quit;
+
+       switch((char) i) {
+           case 'q':
+           case 'Q':
+               goto quit;
+           case 't':
+           case 'T':
+               wait_mode = (wait_mode == WAIT_FOREVER ?
+                   WAIT_TIMEOUT : WAIT_FOREVER);
+               print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
+                   W_STAT);
        }
-       sleep(1);
-       goto start;
-    default:
-       close(child_pipe[0]);
-       while (getch() != 'q');
-       write(child_pipe[1], "0", 1);
-       wait((int *) 0);
-       close(child_pipe[1]);
-       goto quit;
+
+       /* either timeout for user input (i == 0) or invalid key => continue */
     }
 
-  exit:
-    getch();
   quit:
     wclear(main_sub);
-    print_title("");
+    print_top(NULL, NULL);
     clear_main(0);
 }
+