-
Notifications
You must be signed in to change notification settings - Fork 1
/
status_lights.c
478 lines (409 loc) · 14.1 KB
/
status_lights.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* status_lights.c - control lights on the router to convey cloud status
*
* Copyright (C) 2012, Greg Johnson
* Released under the terms of the GNU GPL v2.0.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* $Id: status_lights.c,v 1.6 2012-02-22 18:55:25 greg Exp $
*/
static const char Version[] = "Version "
"$Id: status_lights.c,v 1.6 2012-02-22 18:55:25 greg Exp $";
/* cause lights on the front of the box to blink, indicating state of the
* box and the cloud.
*
* /tmp/cloud_status has 3 numbers on successive lines indicating status.
*
* Example:
* 3 # three boxes in the cloud
* 200 # my weakest stp link
* 1 # number of boxes that have a below-threshold stp link
*
* the dmz light blinks out the number of boxes in the cloud, and then
* the number of boxes that have no weak stp links. In the above example,
* the dmz light would blink 3 times then 2 times.
*
* the power light reflects the status of the box itself. If its weakest
* stp link is above threshold (175 currently), it stays on solid. If
* not, it alternates between on solid and fast-blink.
*/
/* in debug mode, set things up for execution on a development box without
* real lights, and don't do interrupt-based timing. instead, do things
* interactively from an interactive prompt.
* in normal (non-debug) mode, set up timer-based periodic interrupts and
* have that drive the program.
*/
// #define DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include "util.h"
#define INT_FREQ_USEC 500000
enum { DMZ, SESSION, DIAG };
enum { START_LED, STOP_LED };
static int version = 2;
static int my_weakest_stp_link;
static int status_threshold = 175;
static int cloud_weak_box_count;
static int cloud_box_count;
static char *cloud_status_file = "/tmp/cloud_status";
static int count = 10;
#define MY_STATUS_ON_DSEC 8
#define MY_STATUS_OFF_DSEC 2
/* set power light to solid-on or to fast-blink */
#define POWER_LIGHT_ON_SOLID 0
#define POWER_LIGHT_BLINK 1
/* set dmz light on or off */
#define DMZ_LIGHT_ON 1
#define DMZ_LIGHT_OFF 0
static int status_state = POWER_LIGHT_ON_SOLID;
static int status_tick_count = 0;
/* dfa states for the cloud status (dmz light) */
#define CLOUD_PRE_COUNT_GAP 1
#define CLOUD_PRE_COUNT_GAP1 2
#define CLOUD_COUNT 3
#define CLOUD_PRE_OK_GAP 4
#define CLOUD_PRE_OK_GAP1 5
#define CLOUD_PRE_OK_GAP2 6
#define CLOUD_OK_COUNT 7
/* amount of times to spend in each state */
#define CLOUD_STATUS_FLICKER_ON_DSEC 4
#define CLOUD_STATUS_FLICKER_OFF_DSEC 2
#define CLOUD_STATUS_ON_DSEC 2
#define CLOUD_STATUS_OFF_DSEC 2
#define CLOUD_STATUS_OFF_GAP_DSEC 4
static int cloud_state = CLOUD_PRE_COUNT_GAP;
static int cloud_light_state = DMZ_LIGHT_OFF;
static int cloud_count;
static int cloud_tick_count = 0;
/* get the state of the lights (on, off, blinking etc.),
* for wrt54g v 2.0 and later i think
*/
static unsigned int read_gpio(char *device)
{
FILE *fp;
unsigned int val;
if( (fp=fopen(device, "r")) ){
fread(&val, 4, 1, fp);
fclose(fp);
// fprintf(stderr, "----- gpio %s = [%X]\n",device,val);
return val;
}
else{
perror(device);
return 0;
}
}
/* set the state of the lights (on, off, blinking etc.),
* for wrt54g v 2.0 and later i think
*/
static unsigned int write_gpio(char *device, unsigned int val)
{
FILE *fp;
if( (fp=fopen(device, "w")) ){
fwrite(&val, 4, 1, fp);
fclose(fp);
//fprintf(stderr, "----- set gpio %s = [%X]\n",device,val);
return 1;
}
else{
perror(device);
return 0;
}
}
/* set the state of the specified light (power light or dmz light)
* on or off.
* for wrt54g v 2.0 and later i think
*/
static void set_light_v2(bool_t on_off_light, bool_t dmz_light)
{
unsigned int control,in,outen,out;
control=read_gpio("/dev/gpio/control");
in=read_gpio("/dev/gpio/in");
out=read_gpio("/dev/gpio/out");
outen=read_gpio("/dev/gpio/outen");
if (dmz_light) {
/* dmz light is gpio 7 */
write_gpio("/dev/gpio/control",control & 0x7f);
write_gpio("/dev/gpio/outen",outen | 0x80);
if (!on_off_light) {
write_gpio("/dev/gpio/out",out | 0x80);
} else /*if (act == START_LED)*/ {
write_gpio("/dev/gpio/out",out & 0x7f);
}
} else {
write_gpio("/dev/gpio/control",control & 0xfd);
write_gpio("/dev/gpio/outen",outen | 0x02);
if (!on_off_light) { // stop blinking
write_gpio("/dev/gpio/out",out | 0x02);
} else /* if (act == START_LED) */ { // start blinking
write_gpio("/dev/gpio/out",out & 0xfd);
}
}
}
/* set the state of the specified light (power light or dmz light)
* on or off.
* for wrt54g earlier than v 2.0 i think
*/
static void set_lights(int power_light_setting, int dmz_light_setting)
{
if (version == 2) {
set_light_v2(power_light_setting == 1, false /* diag light */);
set_light_v2(dmz_light_setting == 1, true /* dmz light */);
} else {
/* put value in /proc/sys/diag:
* 0 - power light solid, dmz light off
* 1 - power light solid, dmz light on
* 4 - power light blink, dmz light off
* 5 - power light blink, dmz light on
*/
FILE *f;
int set_um = 0;
// fprintf(stderr, "hi from set_lights..\n");
if (dmz_light_setting) { set_um = 1; }
if (power_light_setting) { set_um += 4; }
if ((f = fopen("/proc/sys/diag", "w")) == NULL) {
fprintf(stderr, "could not open /proc/sys/diag: %s\n",
strerror(errno));
return;
}
fprintf(f, "%d\n", set_um);
fclose(f);
}
}
#if 0
/* for running the program on a development box with no real lights */
static void db_set_lights(bool_t on_off_light, bool_t dmz_light)
{
printf(" %c %c%c", on_off_light ? '*' : 'o', dmz_light ? '*' : 'o',
#ifdef DEBUG
'\n'
#else
'\r'
#endif
);
fflush(stdout);
}
#endif
/* this is the routine that periodically gets called from the timer interrupt.
* it periodically reads the file containing cloud box status information,
* and uses a state machine to decide how to set the lights on the front
* of the box to reflect state.
*/
static void repeated(int arg)
{
FILE *f;
int result;
if (++count >= 10) {
bool_t bad = false;
count = 0;
f = fopen(cloud_status_file, "r");
if (f == NULL) {
fprintf(stderr, "could not open %s: %s\n",
cloud_status_file, strerror(errno));
bad = true;
goto done;
}
if (1 != fscanf(f, "%d", &cloud_box_count)) { goto done; }
while ((result = fgetc(f)) != '\n' && result != EOF);
if (result == EOF) { bad = true; goto done; }
if (1 != fscanf(f, "%d", &my_weakest_stp_link)) { goto done; }
while ((result = fgetc(f)) != '\n' && result != EOF);
if (result == EOF) { bad = true; goto done; }
if (1 != fscanf(f, "%d", &cloud_weak_box_count)) { goto done; }
while ((result = fgetc(f)) != '\n' && result != EOF);
if (result == EOF) { bad = true; goto done; }
done:
if (f != NULL) { fclose(f); }
if (bad) { return; }
}
if (--status_tick_count <= 0) {
status_tick_count = MY_STATUS_ON_DSEC + MY_STATUS_OFF_DSEC;
if (my_weakest_stp_link >= status_threshold
|| my_weakest_stp_link == -1)
{
status_state = POWER_LIGHT_ON_SOLID;
} else {
status_state = POWER_LIGHT_BLINK;
}
} else if (status_tick_count <= MY_STATUS_ON_DSEC) {
status_state = POWER_LIGHT_ON_SOLID;
}
#if 0
fprintf(stderr, "cloud_tick_count: %d, cloud_state %d, "
"cloud_light_state %d, cloud_count %d\n",
cloud_tick_count, cloud_state,
cloud_light_state, cloud_state);
#endif
if (--cloud_tick_count <= 0) {
switch (cloud_state) {
case CLOUD_PRE_COUNT_GAP :
cloud_state = CLOUD_PRE_COUNT_GAP1;
/* we know we have at least one, namely ourselves */
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_FLICKER_ON_DSEC;
break;
case CLOUD_PRE_COUNT_GAP1 :
if (cloud_light_state == DMZ_LIGHT_ON) {
cloud_light_state = DMZ_LIGHT_OFF;
cloud_tick_count = CLOUD_STATUS_FLICKER_OFF_DSEC;
} else {
cloud_state = CLOUD_COUNT;
/* we know we have at least one, namely ourselves */
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_ON_DSEC;
cloud_count = 0;
}
break;
case CLOUD_COUNT :
if (cloud_light_state == DMZ_LIGHT_ON) {
cloud_light_state = DMZ_LIGHT_OFF;
cloud_tick_count = CLOUD_STATUS_OFF_DSEC;
} else {
cloud_count++;
if (cloud_count >= cloud_box_count) {
cloud_state = CLOUD_PRE_OK_GAP;
cloud_tick_count = CLOUD_STATUS_OFF_GAP_DSEC
- CLOUD_STATUS_OFF_DSEC;
} else {
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_ON_DSEC;
}
}
break;
case CLOUD_PRE_OK_GAP :
cloud_state = CLOUD_PRE_OK_GAP2;
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_FLICKER_ON_DSEC;
break;
case CLOUD_PRE_OK_GAP1 :
if (cloud_light_state == DMZ_LIGHT_ON) {
cloud_light_state = DMZ_LIGHT_OFF;
cloud_tick_count = CLOUD_STATUS_FLICKER_ON_DSEC;
} else {
cloud_state = CLOUD_PRE_OK_GAP2;
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_FLICKER_ON_DSEC;
}
break;
case CLOUD_PRE_OK_GAP2 :
if (cloud_light_state == DMZ_LIGHT_ON) {
cloud_light_state = DMZ_LIGHT_OFF;
cloud_tick_count = CLOUD_STATUS_FLICKER_OFF_DSEC;
} else if (cloud_box_count == cloud_weak_box_count) {
cloud_state = CLOUD_PRE_COUNT_GAP;
cloud_tick_count = CLOUD_STATUS_OFF_GAP_DSEC;
} else {
cloud_state = CLOUD_OK_COUNT;
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_ON_DSEC;
cloud_count = 0;
}
break;
case CLOUD_OK_COUNT :
if (cloud_light_state == DMZ_LIGHT_ON) {
cloud_light_state = DMZ_LIGHT_OFF;
cloud_tick_count = CLOUD_STATUS_OFF_DSEC;
} else {
cloud_count++;
if (cloud_count >= cloud_box_count - cloud_weak_box_count) {
cloud_state = CLOUD_PRE_COUNT_GAP;
cloud_tick_count = CLOUD_STATUS_OFF_GAP_DSEC
- CLOUD_STATUS_OFF_DSEC;
} else {
cloud_light_state = DMZ_LIGHT_ON;
cloud_tick_count = CLOUD_STATUS_ON_DSEC;
}
}
break;
}
}
// fprintf(stderr, "after: %d, cloud_state %d, cloud_light_state %d, "
// "cloud_count %d\n", cloud_tick_count, cloud_state,
// cloud_light_state, cloud_state);
set_lights(status_state, cloud_light_state);
// db_set_lights(status_state, cloud_light_state);
}
static int diag = -1;
static int dmz = -1;
static void usage()
{
fprintf(stderr, "usage:\nstatus_lights \\\n"
" [-v N] \\\n"
" [-d diag_light -z dmz_light] \\\n");
exit(1);
}
static void process_args(int argc, char **argv)
{
int c;
while ((c = getopt(argc, argv, "d:z:v:")) != -1) {
switch (c) {
case 'd' :
if (sscanf(optarg, "%d", &diag) != 1) {
fprintf(stderr, "DEBUG: invalid diag %s\n", optarg);
exit(1);
}
break;
case 'z' :
if (sscanf(optarg, "%d", &dmz) != 1) {
fprintf(stderr, "DEBUG: invalid dmz %s\n", optarg);
exit(1);
}
break;
case 'v' :
if (sscanf(optarg, "%d", &version) != 1) {
fprintf(stderr, "DEBUG: invalid version %s\n", optarg);
exit(1);
}
break;
default :
usage();
}
}
}
int main(int argc, char **argv)
{
struct itimerval timer;
#ifndef DEBUG
int result;
#endif
struct sigaction action;
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = INT_FREQ_USEC;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = INT_FREQ_USEC;
memset(&action, 0, sizeof(action));
action.sa_handler = repeated;
#ifndef DEBUG
sigaction(SIGALRM, &action, NULL);
result = setitimer(ITIMER_REAL, &timer, 0);
#endif
process_args(argc, argv);
if (diag != -1 || dmz != -1) {
if ((diag != -1) != (dmz != -1)) { usage(); }
fprintf(stderr, "setting %d %d\n", diag, dmz);
set_lights(diag, dmz);
exit(0);
}
set_lights(status_state, cloud_light_state);
// db_set_lights(status_state, cloud_light_state);
/* unless in debug mode, just sleep forever and have the periodic
* interrupts drive things.
*/
while (true) {
#ifdef DEBUG
fprintf(stderr, "fire when ready.. ");
if (getchar() == 'q') { return 0; }
repeated(0);
#else
sleep(3600);
#endif
}
}