Few fixes
[tomato.git] / release / src-rt-6.x.4708 / router / rc / blink.c
blobd79e85fced0e9ce1a579e76ab24f051d056fe7bc
1 #include "rc.h"
2 #include <shared.h>
3 #include <string.h>
4 #include <wlutils.h>
6 // Remove defines below, rather pass as parameters ...
7 //#define BLINK_MAXSPEED 10 // Maximum Blink Speed, on/off cycles per second (floating point)
8 //#define BLINK_THRESHOLD 32768 // Data threshold ... once this much data received, then blink
10 static int find_led_name(char *ledname)
12 int i = 0;
14 while ((i < LED_COUNT) && (strcmp(ledname, led_names[i])))
15 i++;
17 if (i < LED_COUNT)
18 return(i);
19 else {
20 printf("blink: Invalid LED name\n");
21 exit(2);
25 static unsigned long get_wl_count(char *interface)
27 FILE *f;
28 char buf[256];
29 char *ifname, *p;
30 unsigned long counter1, counter2;
32 if((f = fopen("/proc/net/dev", "r"))==NULL) return -1;
34 fgets(buf, sizeof(buf), f);
35 fgets(buf, sizeof(buf), f);
37 counter1=counter2=0;
39 while (fgets(buf, sizeof(buf), f)) {
40 if((p=strchr(buf, ':'))==NULL) continue;
41 *p = 0;
42 if((ifname = strrchr(buf, ' '))==NULL) ifname = buf;
43 else ++ifname;
45 if(strcmp(ifname, interface)) continue;
46 if(sscanf(p+1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &counter1, &counter2)!=2) continue;
47 break;
49 fclose(f);
51 return counter1 + counter2;
54 int blink_main(int argc, char *argv[])
56 unsigned int ledindex;
57 unsigned long count;
58 unsigned long oldcount = 0;
59 uint32 radioStatus;
60 float maxspeed;
61 unsigned long threshold;
62 unsigned long maxdatarate;
63 float currblinkspeed;
64 int iter;
66 // Check for correct number of arguments
67 if (argc != 5) {
68 fprintf(stderr, "usage: blink interface led rate threshold\n");
69 return(1);
72 // Fork new process, run in the background (daemon)
73 if (fork() != 0) return 0;
74 setsid();
75 signal(SIGCHLD, chld_reap);
77 // Get the LED Index for the targeted LED
78 ledindex = find_led_name(argv[2]);
80 // And determine blink parameters
81 maxspeed = atof(argv[3]);
82 threshold = atol(argv[4]);
83 // Calculate Max Data Rate (Data Rate for maximum blink rate ... on average, exceed data threshold each interval)
84 maxdatarate = maxspeed * threshold;
86 // Loop Through, checking for new data (and blink accordingly ... max speed at max or higher data rate)
87 while(1){
88 // Get Data Count, check if sufficient data received for blink
89 count = get_wl_count(argv[1]);
90 if (count >= (oldcount + threshold)) {
91 // Sufficient Data Received, so blink - simulate rate, as /proc/net/dev is only updated once per second!
92 if (threshold != 0) {
93 currblinkspeed = (count-oldcount) / threshold;
94 if (currblinkspeed > maxspeed)
95 currblinkspeed = maxspeed;
96 } else
97 currblinkspeed = maxspeed;
98 oldcount = count;
100 // Simulate Blink for one second (until we get new data in /proc/net/dev)
101 for (iter=0; iter < currblinkspeed; iter++) {
102 led(ledindex, LED_OFF);
103 usleep((useconds_t)(0.5 * (1.0/currblinkspeed) * 1E6));
104 led(ledindex, LED_ON);
105 usleep((useconds_t)(0.5 * (1.0/currblinkspeed) * 1E6));
108 usleep(50000);
109 } else {
110 // Get Radio Status ... only blink if Radio is Enabled (otherwise, just turn the LED off)
111 wl_ioctl(argv[1], WLC_GET_RADIO, &radioStatus, sizeof(radioStatus));
113 // radioStatus != 0 for Disabled, using a bit mask defined in wlioctl.h (i.e. 0 = enabled)
114 if (radioStatus != 0) {
115 // Radio is disabled (in one of multiple ways), so disable LED ... and wait 5 seconds to check again
116 led(ledindex, LED_OFF);
117 sleep(5);
118 } else {
119 // Not enough Data, so don't blink ... and wait 200 ms for an update (as /proc/net/dev is only updated once per second!)
120 led(ledindex, LED_ON);
121 usleep(200000);