Updated Blink Function
[tomato.git] / release / src / router / rc / blink.c
blob30bfdc42dfcb27964dc7c8685472ec8aece4d2de
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;
48 fclose(f);
50 return counter1 + counter2;
53 int blink_main(int argc, char *argv[])
55 unsigned int ledindex;
56 unsigned long count;
57 unsigned long oldcount = 0;
58 uint32 radioStatus;
59 float maxspeed;
60 unsigned long threshold;
61 unsigned long maxdatarate;
62 float currblinkspeed;
63 int iter;
65 // Check for correct number of arguments
66 if (argc != 5) {
67 fprintf(stderr, "usage: blink interface led rate threshold\n");
68 return(1);
71 // Fork new process, run in the background (daemon)
72 if (fork() != 0) return 0;
73 setsid();
74 signal(SIGCHLD, chld_reap);
76 // Get the LED Index for the targeted LED
77 ledindex = find_led_name(argv[2]);
79 // And determine blink parameters
80 maxspeed = atof(argv[3]);
81 threshold = atol(argv[4]);
82 // Calculate Max Data Rate (Data Rate for maximum blink rate ... on average, exceed data threshold each interval)
83 maxdatarate = maxspeed * threshold;
85 // Loop Through, checking for new data (and blink accordingly ... max speed at max or higher data rate)
86 while(1){
88 // Get Radio Status ... only blink if Radio is Enabled (otherwise, just turn the LED off)
89 wl_ioctl(argv[1], WLC_GET_RADIO, &radioStatus, sizeof(radioStatus));
91 // radioStatus != 0 for Disabled, using a bit mask defined in wlioctl.h (i.e. 0 = enabled)
92 if (radioStatus == 0) {
93 // Get Data Count, check if sufficient data received for blink
94 count = get_wl_count(argv[1]);
96 if (count >= (oldcount + threshold)) {
97 // Sufficient Data Received, so blink - simulate rate, as /proc/net/dev is only updated once per second!
98 if (threshold != 0) {
99 currblinkspeed = (count-oldcount) / threshold;
100 if (currblinkspeed > maxspeed)
101 currblinkspeed = maxspeed;
102 } else
103 currblinkspeed = maxspeed;
104 oldcount = count;
105 // Simulate Blink for one second (until we get new data in /proc/net/dev)
106 for (iter=0; iter < currblinkspeed; iter++) {
107 led(ledindex, LED_OFF);
108 usleep((useconds_t)(0.5 * (1.0/currblinkspeed) * 1E6));
109 led(ledindex, LED_ON);
110 usleep((useconds_t)(0.5 * (1.0/currblinkspeed) * 1E6));
112 } else {
113 // Not enough Data, so don't blink ... and wait 200 ms for an update (as /proc/net/dev is only updated once per second!)
114 led(ledindex, LED_ON);
115 usleep(200000);
117 } else {
118 // Radio is disabled (in one of multiple ways), so disable LED ... and wait 5 seconds to check again
119 led(ledindex, LED_OFF);
120 sleep(5);