Few fixes
[tomato.git] / release / src-rt-6.x.4708 / router / rc / blink_5g.c
blob1c33cbf24ed2e1a8a9c5856b03755d5c6461a3c6
1 #include "rc.h"
2 #include <shared.h>
4 static char *interface = NULL;
5 static int rand_seed_by_time(void)
7 time_t atime;
9 time(&atime);
10 srand((unsigned long)atime);
12 return rand();
15 static unsigned long get_5g_count()
17 FILE *f;
18 char buf[256];
19 char *ifname, *p;
20 unsigned long counter1, counter2;
22 if((f = fopen("/proc/net/dev", "r"))==NULL) return -1;
24 fgets(buf, sizeof(buf), f);
25 fgets(buf, sizeof(buf), f);
27 counter1=counter2=0;
29 while (fgets(buf, sizeof(buf), f)) {
30 if((p=strchr(buf, ':'))==NULL) continue;
31 *p = 0;
32 if((ifname = strrchr(buf, ' '))==NULL) ifname = buf;
33 else ++ifname;
35 if(strcmp(ifname, interface)) continue;
37 if(sscanf(p+1, "%lu%*u%*u%*u%*u%*u%*u%*u%*u%lu", &counter1, &counter2)!=2) continue;
38 break;
40 fclose(f);
42 return counter1;
45 int get_lanports_status(void)
47 int r = 0;
48 FILE *f;
49 char s[128], a[16];
51 if ((f = popen("/usr/sbin/robocfg showports", "r")) != NULL) {
52 while (fgets(s, sizeof(s), f)) {
53 if ((sscanf(s, "Port 1: %s", a) == 1) ||
54 (sscanf(s, "Port 2: %s", a) == 1) ||
55 (sscanf(s, "Port 3: %s", a) == 1) ||
56 (sscanf(s, "Port 4: %s", a) == 1)) {
57 if (strncmp(a, "DOWN", 4)) {
58 r++;
62 fclose(f);
65 return r;
68 int blink_5g_main(int argc, char *argv[])
70 static unsigned int blink_5g = 0;
71 static unsigned int data_5g = 0;
72 unsigned long count_5g;
73 int i;
74 int model;
75 static int j;
76 static int status = -1;
77 static int status_old;
78 if (fork() != 0) return 0;
79 setsid();
80 signal(SIGCHLD, chld_reap);
81 #define INTERFACE_MAXLEN 10
82 interface = calloc(INTERFACE_MAXLEN,1);
83 char *tmp_interface = nvram_get("blink_5g_interface");
84 if(tmp_interface)
85 strncpy(interface,tmp_interface, INTERFACE_MAXLEN);
86 // check data per 10 count
87 model = get_model();
88 while(1){
89 if (model == MODEL_WS880) {
90 sleep(5);
91 if (get_lanports_status()) {
92 led(LED_BRIDGE, LED_ON);
94 else {
95 led(LED_BRIDGE, LED_OFF);
97 continue;
99 if(!tmp_interface){
100 sleep(5);
101 tmp_interface = nvram_get("blink_5g_interface");
102 if(tmp_interface)
103 strncpy(interface,tmp_interface, INTERFACE_MAXLEN);
104 continue;
106 count_5g = get_5g_count();
107 if(count_5g && data_5g!=count_5g) {
108 blink_5g = 1;
109 data_5g = count_5g;
111 else
112 blink_5g = 0;
113 led(LED_5G, LED_ON);
115 if(blink_5g) {
116 j = rand_seed_by_time() % 3;
117 for(i=0;i<10;i++) {
118 usleep(33*1000);
119 status_old = status;
120 if (((i%2)==0) && (i > (3 + 2*j)))
121 status = 0;
122 else
123 status = 1;
125 if (status != status_old)
127 if (status)
128 led(LED_5G, LED_ON);
129 else
130 led(LED_5G, LED_OFF);
133 led(LED_5G, LED_ON);
136 usleep(50000);