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
)
14 while ((i
< LED_COUNT
) && (strcmp(ledname
, led_names
[i
])))
20 printf("blink: Invalid LED name\n");
25 static unsigned long get_wl_count(char *interface
)
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
);
39 while (fgets(buf
, sizeof(buf
), f
)) {
40 if((p
=strchr(buf
, ':'))==NULL
) continue;
42 if((ifname
= strrchr(buf
, ' '))==NULL
) ifname
= buf
;
45 if(strcmp(ifname
, interface
)) continue;
46 if(sscanf(p
+1, "%lu%*u%*u%*u%*u%*u%*u%*u%lu", &counter1
, &counter2
)!=2) continue;
50 return counter1
+ counter2
;
53 int blink_main(int argc
, char *argv
[])
55 unsigned int ledindex
;
57 unsigned long oldcount
= 0;
60 unsigned long threshold
;
61 unsigned long maxdatarate
;
65 // Check for correct number of arguments
67 fprintf(stderr
, "usage: blink interface led rate threshold\n");
71 // Fork new process, run in the background (daemon)
72 if (fork() != 0) return 0;
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)
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!
99 currblinkspeed
= (count
-oldcount
) / threshold
;
100 if (currblinkspeed
> maxspeed
)
101 currblinkspeed
= maxspeed
;
103 currblinkspeed
= maxspeed
;
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
));
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
);
118 // Radio is disabled (in one of multiple ways), so disable LED ... and wait 5 seconds to check again
119 led(ledindex
, LED_OFF
);