forked from macano953/uledd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.c
More file actions
354 lines (285 loc) · 6.4 KB
/
scene.c
File metadata and controls
354 lines (285 loc) · 6.4 KB
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
#include <libubox/avl.h>
#include <libubox/avl-cmp.h>
#include <libubox/blobmsg.h>
#include "blob_led.h"
#include "led.h"
#include "log.h"
#include "scene.h"
#include "timer.h"
enum scene_state {
SCENE_IDLE = 0,
SCENE_PAUSED,
SCENE_RUNNING,
};
struct scene_led {
struct avl_node avl;
struct blob_led *led;
};
struct scene {
char *name;
int timeout;
int priority;
struct avl_node avl;
struct avl_tree leds;
struct led_timer timer;
enum scene_state state;
struct scene *paused_by;
struct led_timer led_timer;
};
static struct avl_tree scene_tree = AVL_TREE_INIT(scene_tree, avl_strcmp, false, NULL);
static inline bool
is_running(struct scene *s)
{
return s->state == SCENE_RUNNING;
}
static inline bool
is_paused(struct scene *s)
{
return s->state == SCENE_PAUSED;
}
static struct scene_led *
scene_led_from_path(struct scene *s, const char *path)
{
struct scene_led *led;
return avl_find_element(&s->leds, path, led, avl);
}
static bool
has_shared_leds(struct scene *s1, struct scene *s2)
{
struct scene_led *item;
avl_for_each_element(&s1->leds, item, avl) {
if (scene_led_from_path(s2, item->led->path))
return true;
}
return false;
}
static const char *
state_str(enum scene_state state)
{
struct scene_state_str {
enum scene_state state;
const char* str;
};
static struct scene_state_str scene_state_tbl[] = {
{ SCENE_IDLE, "SCENE_IDLE" },
{ SCENE_PAUSED, "SCENE_PAUSED" },
{ SCENE_RUNNING, "SCENE_RUNNING" },
};
if (state > sizeof(scene_state_tbl))
return "unknown";
return scene_state_tbl[state].str;
}
static void
set_scene_state(struct scene *s, enum scene_state state)
{
s->state = state;
DEBUG(3, "%s to %s\n", s->name, state_str(state));
}
static void
stop_scene_leds(struct scene *s)
{
struct scene_led *item;
avl_for_each_element(&s->leds, item, avl) {
led_stop(led_from_path(item->led->path));
}
}
static void
stop_scene(struct scene *s)
{
if (is_running(s))
stop_scene_leds(s);
set_scene_state(s, SCENE_IDLE);
led_timer_cancel(&s->timer);
}
static void
pause_scene(struct scene *s, struct scene *paused_by)
{
if (is_running(s))
stop_scene_leds(s);
s->paused_by = paused_by;
set_scene_state(s, SCENE_PAUSED);
led_timer_cancel(&s->timer);
}
static bool
stage_scene(struct scene *s)
{
struct scene_led *item;
int timeout = 0;
avl_for_each_element(&s->leds, item, avl) {
led_add(item->led);
timeout = led_run(led_from_path(item->led->path));
}
if (timeout > 0)
led_timer_set(&s->led_timer, timeout);
s->paused_by = NULL;
set_scene_state(s, SCENE_RUNNING);
led_timer_set(&s->timer, s->timeout);
return true;
}
static bool
check_scene_priority(struct scene *run)
{
struct scene *cur;
avl_for_each_element(&scene_tree, cur, avl) {
if (is_running(cur)) {
if (!has_shared_leds(cur, run))
continue;
if (run->priority < cur->priority) {
DEBUG(3, "pausing %s (prio=%d) < %s (prio=%d)\n",
run->name, run->priority, cur->name,
cur->priority);
pause_scene(run, cur);
return false;
}
pause_scene(cur, run);
}
}
return true;
}
static bool
run_scene(struct scene *run)
{
if (!check_scene_priority(run))
return true;
return stage_scene(run);
}
static void
resume_paused_scenes(struct scene *paused_by)
{
struct scene *scene;
avl_for_each_element(&scene_tree, scene, avl) {
if (is_paused(scene) && (scene->paused_by == paused_by))
run_scene(scene);
}
}
static void
scene_timer_cb(struct led_timer *t)
{
struct scene *s = container_of(t, struct scene, timer);
stop_scene(s);
resume_paused_scenes(s);
}
static void
scene_led_timer_cb(struct led_timer *t)
{
struct scene *s = container_of(t, struct scene, led_timer);
struct scene_led *item;
int timeout = 0;
avl_for_each_element(&s->leds, item, avl)
timeout = led_handle_timer(led_from_path(item->led->path));
if (timeout > 0)
led_timer_set(&s->led_timer, timeout);
}
struct scene*
scene_add(const char *name, int timeout, int priority)
{
char *_name;
struct scene *scene = NULL;
scene = calloc_a(sizeof(*scene), &_name, strlen(name) + 1);
if (!scene)
return NULL;
avl_init(&scene->leds, avl_strcmp, false, NULL);
scene->timeout = timeout;
scene->priority = priority;
scene->name = strcpy(_name, name);
scene->avl.key = scene->name;
scene->timer.cb = scene_timer_cb;
scene->led_timer.cb = scene_led_timer_cb;
if (!avl_insert(&scene_tree, &scene->avl)) {
DEBUG(2, "add %s\n", scene_str(scene));
return scene;
}
free(scene);
return NULL;
}
static void
cleanup_scene(struct scene *s)
{
struct scene_led *tmp;
struct scene_led *item;
avl_for_each_element_safe(&s->leds, item, avl, tmp) {
avl_delete(&s->leds, &item->avl);
blob_led_done(item->led);
free(item);
}
free(s);
}
void
scene_led_add(struct scene *s, struct blob_led *b)
{
struct scene_led *n = malloc(sizeof(struct scene_led));
if (!n) {
blob_led_done(b);
return;
}
b->scene_led = 1;
n->led = b;
n->avl.key = b->path;
avl_insert(&s->leds, &n->avl);
led_add(b);
DEBUG(3, "%s\n", blob_led_str(n->led));
}
bool
scene_run(const char *name)
{
struct scene *new;
new = avl_find_element(&scene_tree, name, new, avl);
if (!new)
return false;
return run_scene(new);
}
bool
scene_stop(const char *name)
{
struct scene *new;
new = avl_find_element(&scene_tree, name, new, avl);
if (!new)
return false;
stop_scene(new);
resume_paused_scenes(new);
return true;
}
static void
dump_scene_leds_blobmsg(struct blob_buf *b, struct scene *s)
{
void *arr;
struct scene_led *o;
arr = blobmsg_open_array(b, "leds");
avl_for_each_element(&s->leds, o, avl)
led_state_blobmsg(b, led_from_path(o->led->path));
blobmsg_close_table(b, arr);
}
void
scenes_state_blobmsg(struct blob_buf *b)
{
void *arr;
struct scene *s;
arr = blobmsg_open_array(b, "scenes");
avl_for_each_element(&scene_tree, s, avl) {
void *t = blobmsg_open_table(b, "");
blobmsg_add_string(b, "name", s->name);
blobmsg_add_string(b, "state", state_str(s->state));
blobmsg_add_u32(b, "timeout", s->timeout);
blobmsg_add_u32(b, "priority", s->priority);
dump_scene_leds_blobmsg(b, s);
blobmsg_close_table(b, t);
}
blobmsg_close_table(b, arr);
}
const char*
scene_str(struct scene *s)
{
static char buf[255] = {0};
snprintf(buf, sizeof(buf), "%s priority=%d timeout=%d state=%s",
s->name, s->priority, s->timeout, state_str(s->state));
return buf;
}
void
scene_done()
{
struct scene *tmp;
struct scene *scene;
avl_remove_all_elements(&scene_tree, scene, avl, tmp) {
cleanup_scene(scene);
}
}