From 6ba517f0917cf82b0970e61083427e96168a0d80 Mon Sep 17 00:00:00 2001 From: chazikai24 <170334233@qq.com> Date: Thu, 5 Mar 2015 11:25:29 +0100 Subject: [PATCH] Fix up wlan/wan/usb leds,now all leds works well wifi toggle on/off now wlan led works good too it seems led_5g works well too,but now test Add Blink for 2.4G / 5G Wireless Interfaces --- release/src-rt-6.x.4708/router/rc/Makefile | 2 + release/src-rt-6.x.4708/router/rc/blink.c | 116 +++++++++++++++++++++++++++ release/src-rt-6.x.4708/router/rc/init.c | 1 + release/src-rt-6.x.4708/router/rc/network.c | 13 +++ release/src-rt-6.x.4708/router/rc/rc.c | 1 + release/src-rt-6.x.4708/router/rc/rc.h | 3 + release/src-rt-6.x.4708/router/rc/usb.c | 17 ++-- release/src-rt-6.x.4708/router/shared/led.c | 2 +- release/src-rt-6.x.4708/router/shared/misc.c | 11 ++- 9 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 release/src-rt-6.x.4708/router/rc/blink.c diff --git a/release/src-rt-6.x.4708/router/rc/Makefile b/release/src-rt-6.x.4708/router/rc/Makefile index cc518c6d19..67a3bc08a4 100644 --- a/release/src-rt-6.x.4708/router/rc/Makefile +++ b/release/src-rt-6.x.4708/router/rc/Makefile @@ -32,6 +32,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 ($(TCONFIG_BCMARM),y) OBJS += bcmutils.o @@ -129,6 +130,7 @@ endif @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 @cd $(INSTALLDIR)/sbin && ln -sf rc blink_5g @cd $(INSTALLDIR)/sbin && ln -sf rc rcheck @cd $(INSTALLDIR)/sbin && ln -sf rc radio diff --git a/release/src-rt-6.x.4708/router/rc/blink.c b/release/src-rt-6.x.4708/router/rc/blink.c new file mode 100644 index 0000000000..d79e2eafdd --- /dev/null +++ b/release/src-rt-6.x.4708/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-rt-6.x.4708/router/rc/init.c b/release/src-rt-6.x.4708/router/rc/init.c index 655b29a554..63a7e5cb4e 100644 --- a/release/src-rt-6.x.4708/router/rc/init.c +++ b/release/src-rt-6.x.4708/router/rc/init.c @@ -1540,6 +1540,7 @@ static int init_nvram(void) nvram_set("wl1_nctrlsb", "lower"); nvram_set("wl_country", "SG"); nvram_set("wl_country_code", "SG"); + nvram_set("blink_wl", "1"); // bcm4360ac_defaults - fix problem of loading driver failed with code 21 nvram_set("pci/1/1/aa2g", "7"); diff --git a/release/src-rt-6.x.4708/router/rc/network.c b/release/src-rt-6.x.4708/router/rc/network.c index e74e79ce2f..f334b44492 100644 --- a/release/src-rt-6.x.4708/router/rc/network.c +++ b/release/src-rt-6.x.4708/router/rc/network.c @@ -655,6 +655,19 @@ void start_wl(void) #ifdef CONFIG_BCMWL5 eval("wlconf", ifname, "start"); /* start wl iface */ + // 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); + 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 } free(lan_ifnames); diff --git a/release/src-rt-6.x.4708/router/rc/rc.c b/release/src-rt-6.x.4708/router/rc/rc.c index 4c1cf615a2..ace38fb631 100644 --- a/release/src-rt-6.x.4708/router/rc/rc.c +++ b/release/src-rt-6.x.4708/router/rc/rc.c @@ -109,6 +109,7 @@ static const applets_t applets[] = { { "mtd-unlock", mtd_unlock_erase_main }, #endif { "buttons", buttons_main }, + { "blink", blink_main }, #ifdef CONFIG_BCMWL6 { "blink_5g", blink_5g_main }, #endif diff --git a/release/src-rt-6.x.4708/router/rc/rc.h b/release/src-rt-6.x.4708/router/rc/rc.h index e1ee71d0a8..2c114cdc9b 100644 --- a/release/src-rt-6.x.4708/router/rc/rc.h +++ b/release/src-rt-6.x.4708/router/rc/rc.h @@ -402,6 +402,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[]); diff --git a/release/src-rt-6.x.4708/router/rc/usb.c b/release/src-rt-6.x.4708/router/rc/usb.c index 514bc3fde7..2ed100f10d 100644 --- a/release/src-rt-6.x.4708/router/rc/usb.c +++ b/release/src-rt-6.x.4708/router/rc/usb.c @@ -108,10 +108,11 @@ void start_usb(void) #ifdef LINUX26 i = do_led(LED_USB, LED_PROBE); if (i != 255) { - modprobe("ledtrig-usbdev"); + /*modprobe("ledtrig-usbdev"); modprobe("leds-usb"); sprintf(param, "%d", i); - f_write_string("/proc/leds-usb/gpio_pin", param, 0, 0); + f_write_string("/proc/leds-usb/gpio_pin", param, 0, 0);*/ + do_led(LED_USB, LED_OFF); } #endif #ifdef TCONFIG_USBAP @@ -331,8 +332,8 @@ void stop_usb(void) if (disabled || nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD); #ifdef LINUX26 - modprobe_r("leds-usb"); - modprobe_r("ledtrig-usbdev"); + //modprobe_r("leds-usb"); + //modprobe_r("ledtrig-usbdev"); led(LED_USB, LED_OFF); #endif @@ -911,7 +912,13 @@ static inline void usbled_proc(char *device, int add) if (strcmp(p, param) == 0) return; - f_write_string(add ? "/proc/leds-usb/add" : "/proc/leds-usb/remove", param, 0, 0); + // Remove legacy approach in the code here - rather, use do_led() function, which is designed to do this + // The reason for changing this ... some HW (like Netgear WNDR4000) don't work with direct GPIO write -> use do_led()! + //f_write_string(add ? "/proc/leds-usb/add" : "/proc/leds-usb/remove", param, 0, 0); + if (add) + do_led(LED_USB, LED_ON); + else + do_led(LED_USB, LED_OFF); } } #endif diff --git a/release/src-rt-6.x.4708/router/shared/led.c b/release/src-rt-6.x.4708/router/shared/led.c index 8060cc9433..b838f4b962 100644 --- a/release/src-rt-6.x.4708/router/shared/led.c +++ b/release/src-rt-6.x.4708/router/shared/led.c @@ -222,7 +222,7 @@ int do_led(int which, int mode) static int ac56u[] = { 255, 255, 255, 255, 255, -3, 255, -0, -14, 255}; static int n18u[] = { 255, 255, 6, 255, 255, 255, 255, 3, 14, 255}; static int r6250[] = { 11, 255, 15, 255, 255, 1, 255, 8, 8, 255}; - static int r6300v2[] = { 1, 255, 255, 255, 255, -11, 255, -8, -8, 255}; + static int r6300v2[] = { 11, 255, 10, 255, 255, 1, 255, 8, 8, 255}; static int r7000[] = { 255, 255, 255, 255, 255, -15, 255, -17, -18, 255}; static int dir868[] = { 255, 255, 3, 255, 255, -0, 255, 255, 255, 255}; static int ws880[] = { 255, 255, -12, 255, 255, 6, 255, 14, 14, 255}; diff --git a/release/src-rt-6.x.4708/router/shared/misc.c b/release/src-rt-6.x.4708/router/shared/misc.c index a230f8b1d8..426e9a7804 100644 --- a/release/src-rt-6.x.4708/router/shared/misc.c +++ b/release/src-rt-6.x.4708/router/shared/misc.c @@ -556,14 +556,19 @@ void set_radio(int on, int unit) n = on ? (WL_RADIO_SW_DISABLE << 16) : ((WL_RADIO_SW_DISABLE << 16) | 1); wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n)); if (!on) { - led(LED_WLAN, 0); - led(LED_DIAG, 0); + if (unit == 0) led(LED_WLAN, LED_OFF); + else led(LED_5G, LED_OFF); + } else { + if (unit == 0) led(LED_WLAN, LED_ON); + else led(LED_5G, LED_ON); } #else n = on ? 0 : WL_RADIO_SW_DISABLE; wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n)); if (!on) { - led(LED_DIAG, 0); + led(LED_WLAN, LED_OFF); + } else { + led(LED_WLAN, LED_ON); } #endif } -- 2.11.4.GIT