From 9a65a957681a3b1ca69f0b7d2802fdb82b02d40a Mon Sep 17 00:00:00 2001 From: arrmo Date: Wed, 11 Dec 2013 00:05:09 -0600 Subject: [PATCH] Updated Blink Function Blink now speed variable (based on traffic) - command line parameters for maximum rate / speed (of LED), data threshold in bytes. --- release/src/router/rc/blink.c | 114 +++++++++++++++++++++------------------ release/src/router/rc/network.c | 4 +- release/src/router/shared/misc.c | 1 - 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/release/src/router/rc/blink.c b/release/src/router/rc/blink.c index d79e2eafdd..30bfdc42df 100644 --- a/release/src/router/rc/blink.c +++ b/release/src/router/rc/blink.c @@ -1,15 +1,11 @@ #include "rc.h" #include +#include +#include -static int rand_seed_by_time(void) -{ - time_t atime; - - time(&atime); - srand((unsigned long)atime); - - return rand(); -} +// Remove defines below, rather pass as parameters ... +//#define BLINK_MAXSPEED 10 // Maximum Blink Speed, on/off cycles per second (floating point) +//#define BLINK_THRESHOLD 32768 // Data threshold ... once this much data received, then blink static int find_led_name(char *ledname) { @@ -47,70 +43,82 @@ static unsigned long get_wl_count(char *interface) else ++ifname; if(strcmp(ifname, interface)) continue; - - if(sscanf(p+1, "%lu%*u%*u%*u%*u%*u%*u%*u%*u%lu", &counter1, &counter2)!=2) continue; - + if(sscanf(p+1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &counter1, &counter2)!=2) continue; } fclose(f); - return counter1; + return counter1 + counter2; } int blink_main(int argc, char *argv[]) { - static unsigned int blink = 0; - static unsigned int data = 0; + unsigned int ledindex; unsigned long count; - static unsigned int ledindex; - int i; - static int j; - static int status = -1; - static int status_old; - - if (argc != 3) { - fprintf(stderr, "usage: blink interface led\n"); + unsigned long oldcount = 0; + uint32 radioStatus; + float maxspeed; + unsigned long threshold; + unsigned long maxdatarate; + float currblinkspeed; + int iter; + + // Check for correct number of arguments + if (argc != 5) { + fprintf(stderr, "usage: blink interface led rate threshold\n"); return(1); } + // Fork new process, run in the background (daemon) if (fork() != 0) return 0; setsid(); signal(SIGCHLD, chld_reap); - ledindex = find_led_name(argv[2]); - // check data per 10 count + // Get the LED Index for the targeted LED + ledindex = find_led_name(argv[2]); + + // And determine blink parameters + maxspeed = atof(argv[3]); + threshold = atol(argv[4]); + // Calculate Max Data Rate (Data Rate for maximum blink rate ... on average, exceed data threshold each interval) + maxdatarate = maxspeed * threshold; + + // Loop Through, checking for new data (and blink accordingly ... max speed at max or higher data rate) while(1){ - count = get_wl_count(argv[1]); - if(count && data!=count) { - blink = 1; - data = count; - } - else - blink = 0; - led(ledindex, LED_ON); - if(blink) { - j = rand_seed_by_time() % 3; - for(i=0;i<10;i++) { - usleep(33*1000); - status_old = status; - if (((i%2)==0) && (i > (3 + 2*j))) - status = 0; - else - status = 1; - - if (status != status_old) - { - if (status) - led(ledindex, LED_ON); - else - led(ledindex, LED_OFF); + // Get Radio Status ... only blink if Radio is Enabled (otherwise, just turn the LED off) + wl_ioctl(argv[1], WLC_GET_RADIO, &radioStatus, sizeof(radioStatus)); + + // radioStatus != 0 for Disabled, using a bit mask defined in wlioctl.h (i.e. 0 = enabled) + if (radioStatus == 0) { + // Get Data Count, check if sufficient data received for blink + count = get_wl_count(argv[1]); + + if (count >= (oldcount + threshold)) { + // Sufficient Data Received, so blink - simulate rate, as /proc/net/dev is only updated once per second! + if (threshold != 0) { + currblinkspeed = (count-oldcount) / threshold; + if (currblinkspeed > maxspeed) + currblinkspeed = maxspeed; + } else + currblinkspeed = maxspeed; + oldcount = count; + // Simulate Blink for one second (until we get new data in /proc/net/dev) + for (iter=0; iter < currblinkspeed; iter++) { + led(ledindex, LED_OFF); + usleep((useconds_t)(0.5 * (1.0/currblinkspeed) * 1E6)); + led(ledindex, LED_ON); + usleep((useconds_t)(0.5 * (1.0/currblinkspeed) * 1E6)); } + } else { + // Not enough Data, so don't blink ... and wait 200 ms for an update (as /proc/net/dev is only updated once per second!) + led(ledindex, LED_ON); + usleep(200000); } - led(ledindex, LED_ON); + } else { + // Radio is disabled (in one of multiple ways), so disable LED ... and wait 5 seconds to check again + led(ledindex, LED_OFF); + sleep(5); } - else - usleep(50000); - } } diff --git a/release/src/router/rc/network.c b/release/src/router/rc/network.c index 8649ce6027..b709b04002 100644 --- a/release/src/router/rc/network.c +++ b/release/src/router/rc/network.c @@ -660,11 +660,11 @@ void start_wl(void) if (unit == 0) { led(LED_WLAN, LED_ON); if (nvram_get_int("blink_wl")) - eval("blink", ifname, "wlan"); + eval("blink", ifname, "wlan", "20", "8192"); } else { led(LED_5G, LED_ON); if (nvram_get_int("blink_wl")) - eval("blink", ifname, "5g"); + eval("blink", ifname, "5g", "20", "8192"); } // } diff --git a/release/src/router/shared/misc.c b/release/src/router/shared/misc.c index 63e11f4076..bd663bb6f8 100644 --- a/release/src/router/shared/misc.c +++ b/release/src/router/shared/misc.c @@ -520,7 +520,6 @@ int get_radio(int unit) void set_radio(int on, int unit) { uint32 n; - // syslog(LOG_INFO, "Set Radio State, state = %d, unit = %d\n", on, unit); #ifndef WL_BSS_INFO_VERSION #error WL_BSS_INFO_VERSION #endif -- 2.11.4.GIT