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;
51 return counter1
+ counter2
;
54 int blink_main(int argc
, char *argv
[])
56 unsigned int ledindex
;
58 unsigned long oldcount
= 0;
61 unsigned long threshold
;
62 unsigned long maxdatarate
;
66 // Check for correct number of arguments
68 fprintf(stderr
, "usage: blink interface led rate threshold\n");
72 // Fork new process, run in the background (daemon)
73 if (fork() != 0) return 0;
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)
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!
93 currblinkspeed
= (count
-oldcount
) / threshold
;
94 if (currblinkspeed
> maxspeed
)
95 currblinkspeed
= maxspeed
;
97 currblinkspeed
= maxspeed
;
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
));
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
);
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
);