-
Notifications
You must be signed in to change notification settings - Fork 6
/
mon.c
496 lines (447 loc) · 11 KB
/
mon.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "wago.h"
#include "mon.h"
#include "bus.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <event2/event.h>
#include <event2/buffer.h>
//struct _mon {
// enum mon_type typ;
// unsigned int id;
// unsigned char port,offset;
//};
struct _mon_priv;
struct _mon_priv {
struct _mon mon;
struct _mon_priv *next;
struct bufferevent *buf;
struct event *timer;
struct timeval delay;
struct timeval delay2;
struct timeval last;
unsigned short _port,_offset;
unsigned long count;
unsigned char state;
};
static struct _mon_priv *mon_list = NULL;
static int last_mon_id = 0;
static void counter_cb(evutil_socket_t sig, short events, void *user_data);
static void once_cb(evutil_socket_t sig, short events, void *user_data);
static void loop_cb(evutil_socket_t sig, short events, void *user_data);
static void keepalive_cb(evutil_socket_t sig, short events, void *user_data);
static inline struct evbuffer *outbuf(struct _mon_priv *mon) {
if (mon->buf == NULL)
return NULL;
return bufferevent_get_output(mon->buf);
}
int mon_new(enum mon_type typ, unsigned char port, unsigned char offset, struct bufferevent *buf,
unsigned int msec, unsigned int msec2)
{
struct _mon_priv *mon;
unsigned short _port = port;
unsigned short _offset = offset;
unsigned char state;
if (typ > _MON_UNKNOWN_OUT) {
if (bus_is_write_bit(&_port,&_offset) < 0)
return -1;
state = _bus_read_wbit(_port,_offset);
#ifdef DEMO
if (demo_state_skip) { } else
#endif
if (state) {
if (typ == MON_SET_ONCE || typ == MON_SET_LOOP) {
errno = EEXIST;
return -1;
}
} else {
if (typ == MON_CLEAR_ONCE || typ == MON_CLEAR_LOOP) {
errno = EEXIST;
return -1;
}
}
} else if (typ > _MON_UNKNOWN_IN) {
if (bus_is_read_bit(&_port,&_offset) < 0)
return -1;
state = _bus_read_bit(_port,_offset);
} else
state = 0;
mon = malloc(sizeof(*mon));
if (mon == NULL)
return -1;
memset(mon,0,sizeof(*mon));
mon->mon.id = ++last_mon_id;
mon->mon.typ = typ;
mon->mon.port = port;
mon->_port = _port;
mon->mon.offset = offset;
mon->_offset = _offset;
mon->state = state;
mon->buf = buf;
mon->delay.tv_sec = msec/1000;
mon->delay.tv_usec = 1000*(msec-1000*mon->delay.tv_sec);
if(typ < _MON_UNKNOWN_IN || typ > _MON_UNKNOWN_OUT) {
switch(typ) {
case MON_SET_LOOP:
case MON_CLEAR_LOOP:
mon->delay2.tv_sec = msec2/1000;
mon->delay2.tv_usec = 1000*(msec2-1000*mon->delay2.tv_sec);
mon->timer = event_new(base, -1, EV_TIMEOUT, loop_cb, mon);
break;
case MON_SET_ONCE:
case MON_CLEAR_ONCE:
mon->timer = event_new(base, -1, EV_TIMEOUT, once_cb, mon);
break;
case MON_KEEPALIVE:
mon->timer = event_new(base, -1, EV_TIMEOUT, keepalive_cb, mon);
break;
default:
break;
}
if(mon->timer == NULL || event_add(mon->timer, &mon->delay)) {
if(mon->timer) event_free(mon->timer);
free(mon);
return -1;
}
event_base_gettimeofday_cached(base, &mon->last);
switch(typ) {
case MON_SET_LOOP:
case MON_SET_ONCE:
_bus_write_bit(_port,_offset, 1);
mon->state = 1;
break;
case MON_CLEAR_LOOP:
case MON_CLEAR_ONCE:
_bus_write_bit(_port,_offset, 0);
mon->state = 0;
break;
default:
break;
}
bus_sync();
}
mon->next = mon_list;
mon_list = mon;
if(debug)
printf("New Monitor %s:%d: %d:%d > %d:%d %d\n",
mon_typname(mon->mon.typ),mon->mon.id, port,offset, _port,_offset, mon->state);
return mon->mon.id;
}
static void mon_free(struct _mon_priv *mon, struct bufferevent *buf)
{
struct evbuffer *out = outbuf(mon);
if (mon->timer) {
event_del(mon->timer);
event_free(mon->timer);
}
if(out)
evbuffer_add_printf(out, "!-%d Deleted.\n", mon->mon.id);
free(mon);
}
int mon_grab(int id, struct bufferevent *buf)
{
struct _mon_priv *mon = mon_list;
while(mon) {
if (mon->mon.id == id) {
if (mon->buf == NULL) {
mon->buf = buf;
return 0;
} else if (mon->buf == buf)
errno = EADDRINUSE;
else
errno = EBUSY;
return -1;
}
mon = mon->next;
}
errno = ENOENT;
return -1;
}
int mon_del(int id, struct bufferevent *buf)
{
struct _mon_priv **pmon = &mon_list;
while(1) {
struct _mon_priv *mon = *pmon;
if (mon == NULL) {
errno = ENOENT;
return -1;
}
if (mon->mon.id == id) {
*pmon = mon->next;
mon_free(mon,buf);
return 0;
}
pmon = &mon->next;
}
}
void mon_delbuf(struct bufferevent *buf)
{
struct _mon_priv **pmon = &mon_list;
while(1) {
struct _mon_priv *mon = *pmon;
if (mon == NULL)
return;
if (mon->buf == buf) {
mon->buf = NULL;
switch(mon->mon.typ) {
case MON_SET_ONCE:
case MON_SET_LOOP:
case MON_CLEAR_ONCE:
case MON_CLEAR_LOOP:
break;
default:
*pmon = mon->next;
mon_free(mon,buf);
continue;
}
}
pmon = &mon->next;
}
}
/* Enumerate the monitors. Return something != 0 to break the enumerator loop. */
//typedef int (*mon_enum_fn)(struct _mon *mon, void *priv);
int mon_enum(mon_enum_fn enum_fn, void *priv)
{
struct _mon_priv *mon;
int res = 0;
for(mon = mon_list; mon; mon = mon->next) {
res = (*enum_fn)(&mon->mon, priv);
if (res)
break;
}
return res;
}
const char *mon_typname(enum mon_type typ)
{
switch(typ) {
/* no ports */
case MON_KEEPALIVE:
return "keepalive";
/* read ports */
case MON_REPORT:
return "report change";
case MON_REPORT_H:
return "report change h";
case MON_REPORT_L:
return "report change l";
case MON_COUNT:
return "count changes";
case MON_COUNT_H:
return "count changes h";
case MON_COUNT_L:
return "count changes l";
/* write ports */
case MON_SET_ONCE:
return "timed set";
case MON_SET_LOOP:
return "PWM, on";
case MON_CLEAR_ONCE:
return "timed clear";
case MON_CLEAR_LOOP:
return "PWM, off";
default:
return "???";
}
}
static void
counter_cb(evutil_socket_t sig, short events, void *user_data)
{
struct _mon_priv *mon = (struct _mon_priv *)user_data;
struct evbuffer *out = outbuf(mon);
event_free(mon->timer);
mon->timer = NULL;
if(out)
evbuffer_add_printf(out, "!%d %ld\n", mon->mon.id, mon->count);
}
static void
once_cb(evutil_socket_t sig, short events, void *user_data)
{
struct _mon_priv *mon = (struct _mon_priv *)user_data;
struct evbuffer *out = outbuf(mon);
if(debug)
printf("monitor %d triggers\n", mon->mon.id);
if (
#ifdef DEMO
(demo_state_skip) ||
#endif
(_bus_read_wbit(mon->_port,mon->_offset) == mon->state)) {
_bus_write_bit(mon->_port,mon->_offset, !mon->state);
if(out)
evbuffer_add_printf(out, "!%d TRIGGER\n", mon->mon.id);
bus_sync();
} else {
if(out)
evbuffer_add_printf(out, "!-%d Already changed!\n", mon->mon.id);
}
event_free(mon->timer);
mon->timer = NULL;
mon_del(mon->mon.id, NULL);
}
static void
loop_cb(evutil_socket_t sig, short events, void *user_data)
{
struct _mon_priv *mon = (struct _mon_priv *)user_data;
struct evbuffer *out = outbuf(mon);
struct timeval tv;
if(_bus_read_wbit(mon->_port,mon->_offset) == mon->state) {
if(mon->mon.typ == MON_CLEAR_LOOP) {
mon->mon.typ = MON_SET_LOOP;
mon->state = 1;
} else {
mon->mon.typ = MON_CLEAR_LOOP;
mon->state = 0;
}
_bus_write_bit(mon->_port,mon->_offset, mon->state);
if(debug)
printf("monitor %d toggles: %c\n", mon->mon.id, mon->state ? 'H' : 'L');
tv = mon->delay;
mon->delay = mon->delay2;
mon->delay2 = tv;
event_add(mon->timer, &mon->delay);
event_base_gettimeofday_cached(base, &mon->last);
} else {
event_free(mon->timer);
mon->timer = NULL;
if(debug)
printf("monitor %d: ext change: %c\n", mon->mon.id, mon->state ? 'H' : 'L');
if(out)
evbuffer_add_printf(out, "!-%d DROP: saw external change in timer\n", mon->mon.id);
mon->buf = NULL;
mon_del(mon->mon.id, NULL);
}
}
static void
keepalive_cb(evutil_socket_t sig, short events, void *user_data)
{
struct _mon_priv *mon = (struct _mon_priv *)user_data;
struct evbuffer *out = outbuf(mon);
if (out) {
mon->count++;
evbuffer_add_printf(out, "!%d PING %ld\n", mon->mon.id, mon->count);
event_add(mon->timer, &mon->delay);
event_base_gettimeofday_cached(base, &mon->last);
} else {
event_free(mon->timer);
mon->buf = NULL;
mon_del(mon->mon.id, NULL);
}
}
/* check monitor state */
void mon_sync(void)
{
struct _mon_priv *mon,*mon2;
for(mon = mon_list; mon; mon = mon2) {
unsigned char state;
struct evbuffer *out = outbuf(mon);
mon2 = mon->next;
if (mon->mon.typ > _MON_UNKNOWN_OUT) {
#ifdef DEMO
if (demo_state_skip)
continue;
#endif
state = _bus_read_wbit(mon->_port,mon->_offset);
} else
state = _bus_read_bit(mon->_port,mon->_offset);
if(!state == !mon->state)
continue;
mon->state = state;
switch(mon->mon.typ) {
/* Outputs: if the output has been changed externally, die. */
case MON_SET_ONCE:
case MON_SET_LOOP:
_bus_write_bit(mon->_port,mon->_offset, 0);
goto clear_common;
case MON_CLEAR_ONCE:
case MON_CLEAR_LOOP:
_bus_write_bit(mon->_port,mon->_offset, 1);
clear_common:
if(debug)
printf("Mon%d: dropped, found %c\n", mon->mon.id, state?'H':'L');
if(out)
evbuffer_add_printf(out, "!-%d DROP %c: saw external change in loop\n", mon->mon.id, state?'H':'L');
mon->buf = NULL;
mon_del(mon->mon.id, NULL);
break;
/* Inputs: change reporting */
case MON_REPORT:
mon_report:
if(debug)
printf("Mon%d: %c\n", mon->mon.id, state?'H':'L');
if(out)
evbuffer_add_printf(out, "!%d %c\n", mon->mon.id, state?'H':'L');
break;
case MON_REPORT_H:
if(!state) continue;
goto mon_report;
case MON_REPORT_L:
if(state) continue;
goto mon_report;
/* Inputs: change counting */
case MON_COUNT:
mon_count:
mon->count++;
if (mon->timer == NULL) {
if(debug)
printf("Mon%d: %ld %ld.%06lu\n", mon->mon.id, mon->count, mon->delay.tv_sec,mon->delay.tv_usec);
mon->timer = event_new(base, -1, EV_TIMEOUT, counter_cb, mon);
if(mon->timer == NULL || event_add(mon->timer, &mon->delay)) {
if(out) {
evbuffer_add_printf(out, "!%d %ld\n", mon->mon.id, mon->count);
evbuffer_add_printf(out, "* Monitor timeout: error: %s\n", strerror(errno));
}
}
event_base_gettimeofday_cached(base, &mon->last);
} else {
if(debug)
printf("Mon%d: %ld\n", mon->mon.id, mon->count);
}
break;
case MON_COUNT_H:
if(!state) continue;
goto mon_count;
case MON_COUNT_L:
if(state) continue;
goto mon_count;
default:
continue;
}
}
}
const char *mon_detail(struct _mon *_mon)
{
struct _mon_priv *mon = (struct _mon_priv *)_mon;
char *buf;
struct timeval tv;
long int t,tu;
switch(mon->mon.typ) {
case MON_COUNT:
case MON_COUNT_H:
case MON_COUNT_L:
buf = malloc(20);
if(buf == NULL) return NULL;
sprintf(buf,"%ld",mon->count);
return buf;
case MON_SET_ONCE:
case MON_CLEAR_ONCE:
case MON_SET_LOOP:
case MON_CLEAR_LOOP:
buf = malloc(20);
if(buf == NULL) return NULL;
gettimeofday(&tv, NULL);
t = mon->last.tv_sec; tu = mon->last.tv_usec;
t += mon->delay.tv_sec; tu += mon->delay.tv_usec;
t -= tv.tv_sec; tu -= tv.tv_usec;
while (tu < 0) {
tu += 1000000;
t -= 1;
}
while (tu >= 1000000) {
tu -= 1000000;
t += 1;
}
sprintf(buf,"%ld.%03ld",t,tu/1000);
return buf;
default:
return NULL;
}
}