From 218592efa263eddec37b327c1fb4f87500b67abe Mon Sep 17 00:00:00 2001 From: arrmo Date: Sun, 8 Dec 2013 22:02:31 -0600 Subject: [PATCH] Add Blink for 2.4G / 5G Wireless Interfaces --- release/src/router/rc/Makefile | 4 ++ release/src/router/rc/blink.c | 116 ++++++++++++++++++++++++++++++++++++++++ release/src/router/rc/init.c | 5 +- release/src/router/rc/network.c | 14 +++-- release/src/router/rc/rc.c | 1 + release/src/router/rc/rc.h | 3 ++ 6 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 release/src/router/rc/blink.c diff --git a/release/src/router/rc/Makefile b/release/src/router/rc/Makefile index 9c721b68f9..3895f0d67a 100644 --- a/release/src/router/rc/Makefile +++ b/release/src/router/rc/Makefile @@ -16,6 +16,7 @@ OBJS += listen.o redial.o led.o qos.o forward.o misc.o mtd.o OBJS += buttons.o restrict.o gpio.o sched.o OBJS += new_qoslimit.o arpbind.o OBJS += tomatoanon.o +OBJS += blink.o ifeq ($(CONFIG_BCMWL6),y) OBJS += blink_5g.o @@ -95,7 +96,10 @@ install: all @cd $(INSTALLDIR)/sbin && ln -sf rc hotplug @cd $(INSTALLDIR)/sbin && ln -sf rc service @cd $(INSTALLDIR)/sbin && ln -sf rc buttons + @cd $(INSTALLDIR)/sbin && ln -sf rc blink +ifeq ($(CONFIG_BCMWL6),y) @cd $(INSTALLDIR)/sbin && ln -sf rc blink_5g +endif @cd $(INSTALLDIR)/sbin && ln -sf rc rcheck @cd $(INSTALLDIR)/sbin && ln -sf rc radio @cd $(INSTALLDIR)/sbin && ln -sf rc led diff --git a/release/src/router/rc/blink.c b/release/src/router/rc/blink.c new file mode 100644 index 0000000000..d79e2eafdd --- /dev/null +++ b/release/src/router/rc/blink.c @@ -0,0 +1,116 @@ +#include "rc.h" +#include + +static int rand_seed_by_time(void) +{ + time_t atime; + + time(&atime); + srand((unsigned long)atime); + + return rand(); +} + +static int find_led_name(char *ledname) +{ + int i = 0; + + while ((i < LED_COUNT) && (strcmp(ledname, led_names[i]))) + i++; + + if (i < LED_COUNT) + return(i); + else { + printf("blink: Invalid LED name\n"); + exit(2); + } +} + +static unsigned long get_wl_count(char *interface) +{ + FILE *f; + char buf[256]; + char *ifname, *p; + unsigned long counter1, counter2; + + if((f = fopen("/proc/net/dev", "r"))==NULL) return -1; + + fgets(buf, sizeof(buf), f); + fgets(buf, sizeof(buf), f); + + counter1=counter2=0; + + while (fgets(buf, sizeof(buf), f)) { + if((p=strchr(buf, ':'))==NULL) continue; + *p = 0; + if((ifname = strrchr(buf, ' '))==NULL) ifname = buf; + 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; + + } + fclose(f); + + return counter1; +} + +int blink_main(int argc, char *argv[]) +{ + static unsigned int blink = 0; + static unsigned int data = 0; + 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"); + return(1); + } + + if (fork() != 0) return 0; + setsid(); + signal(SIGCHLD, chld_reap); + + ledindex = find_led_name(argv[2]); + // check data per 10 count + 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); + } + } + led(ledindex, LED_ON); + } + else + usleep(50000); + + } +} + diff --git a/release/src/router/rc/init.c b/release/src/router/rc/init.c index aee34a82d1..e99613302c 100644 --- a/release/src/router/rc/init.c +++ b/release/src/router/rc/init.c @@ -1593,6 +1593,7 @@ static int init_nvram(void) mfr = "Netgear"; name = "WNDR4000"; features = SUP_SES | SUP_80211N | SUP_1000ET; + nvram_set("blink_wl", "1"); #ifdef TCONFIG_USB nvram_set("usb_uhci", "-1"); #endif @@ -1979,9 +1980,9 @@ static void sysinit(void) eval("buttons"); -#ifdef CONFIG_BCMWL6 +//#ifdef CONFIG_BCMWL6 eval("blink_5g"); -#endif +//#endif if (!noconsole) xstart("console"); diff --git a/release/src/router/rc/network.c b/release/src/router/rc/network.c index aee60be19e..8649ce6027 100644 --- a/release/src/router/rc/network.c +++ b/release/src/router/rc/network.c @@ -655,10 +655,18 @@ void start_wl(void) #ifdef CONFIG_BCMWL5 eval("wlconf", ifname, "start"); /* start wl iface */ - // Enable LED if wireless interface is enabled + // Enable LED if wireless interface is enabled, and turn on blink (traffic "control" of LED) if enabled if (nvram_get_int(wl_nvname("radio", unit, 0))) { - if (unit == 0) led(LED_WLAN, LED_ON); - else led(LED_5G, LED_ON); + if (unit == 0) { + led(LED_WLAN, LED_ON); + if (nvram_get_int("blink_wl")) + eval("blink", ifname, "wlan"); + } else { + led(LED_5G, LED_ON); + if (nvram_get_int("blink_wl")) + eval("blink", ifname, "5g"); + } + // } #endif // CONFIG_BCMWL5 } diff --git a/release/src/router/rc/rc.c b/release/src/router/rc/rc.c index 6e39c83331..032162a88b 100644 --- a/release/src/router/rc/rc.c +++ b/release/src/router/rc/rc.c @@ -102,6 +102,7 @@ static const applets_t applets[] = { { "mtd-erase", mtd_unlock_erase_main }, { "mtd-unlock", mtd_unlock_erase_main }, { "buttons", buttons_main }, + { "blink", blink_main }, #ifdef CONFIG_BCMWL6 { "blink_5g", blink_5g_main }, #endif diff --git a/release/src/router/rc/rc.h b/release/src/router/rc/rc.h index 379f4b9aa3..829b804fab 100644 --- a/release/src/router/rc/rc.h +++ b/release/src/router/rc/rc.h @@ -392,6 +392,9 @@ extern int mtd_unlock_erase_main(int argc, char *argv[]); // buttons.c extern int buttons_main(int argc, char *argv[]); +// blink.c +extern int blink_main(int argc, char *argv[]); + #ifdef CONFIG_BCMWL6 // blink_5g.c extern int blink_5g_main(int argc, char *argv[]); -- 2.11.4.GIT