]> git.scottworley.com Git - tattlekey/blame - client/press.c
client: lint nits
[tattlekey] / client / press.c
CommitLineData
00ca9fa6 1#include "press.h"
e3ff9e0d 2#include "blink.h"
0527f229 3#include "config.h"
2dcbb2a0 4
0527f229 5static uint32_t press_next_send(press_t *s) {
2dcbb2a0
SW
6 return s->timestamp + (1 << s->send_count) - 1;
7}
8
0527f229
SW
9static uint32_t press_queue_next_send(queue_t *q) {
10 press_t press;
11 return queue_try_peek(q, &press) ? press_next_send(&press) : UINT32_MAX;
12}
13
e3ff9e0d
SW
14static bool next_send_less_than(void *user_data, pheap_node_id_t a,
15 pheap_node_id_t b) {
0527f229
SW
16 queue_t **sleeps = (queue_t **)user_data;
17 return press_queue_next_send(sleeps[a]) < press_queue_next_send(sleeps[b]);
2dcbb2a0 18}
67001c93 19
6e43b84f
SW
20static void *xcalloc(size_t nmemb, size_t size) {
21 void *p = calloc(nmemb, size);
22 if (p == NULL)
e3ff9e0d 23 signal_error_by_blinking();
6e43b84f
SW
24 return p;
25}
26
27press_pile_t *create_press_pile() {
28 press_pile_t *pp = (press_pile_t *)xcalloc(1, sizeof(press_pile_t));
0527f229
SW
29 pp->presses = (queue_t *)xcalloc(config_resend_count, sizeof(queue_t));
30 pp->sleeps = (queue_t **)xcalloc(config_resend_count, sizeof(queue_t *));
8c76d103 31 for (uint i = 0; i < config_resend_count; i++) {
a36c278f
SW
32 const uint paranoid_safety_fudge = 10;
33 uint element_count = paranoid_safety_fudge + (1 << i);
34 element_count = MAX(element_count, config_minimum_queue_size);
35 element_count = MIN(element_count, config_maximum_queue_size);
0527f229
SW
36 queue_init(&pp->presses[i], sizeof(press_t), element_count);
37 }
e3ff9e0d 38 pp->sleeps_heap =
0527f229 39 ph_create(config_resend_count, next_send_less_than, pp->sleeps);
e3ff9e0d
SW
40 if (pp->sleeps_heap == NULL)
41 signal_error_by_blinking();
42 return pp;
43}
44
f68e05b2 45void add_press(press_pile_t *pp, press_t *press) {
8c76d103 46 u16_t sc = press->send_count;
0527f229
SW
47 if (sc >= config_resend_count)
48 signal_error_by_blinking();
49 bool was_empty = queue_is_empty(&pp->presses[sc]);
50 /* No error check; blithely continue if the queue was full. */
51 queue_try_add(&pp->presses[sc], press);
52 if (was_empty) {
53 pheap_node_id_t i = ph_new_node(pp->sleeps_heap);
54 pp->sleeps[i] = &pp->presses[sc];
55 ph_insert_node(pp->sleeps_heap, i);
67001c93 56 }
67001c93 57}
b512ec26 58
e3ff9e0d
SW
59int32_t next_scheduled_send(press_pile_t *pp) {
60 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
b512ec26
SW
61 if (i == 0)
62 return -1;
0527f229 63 return press_queue_next_send(pp->sleeps[i]);
b512ec26 64}
9ae691e9
SW
65
66bool get_press_due_for_resend(press_pile_t *pp, uint32_t now, press_t *press) {
67 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
0527f229 68 if (i == 0 || press_queue_next_send(pp->sleeps[i]) > now)
9ae691e9 69 return false;
0527f229
SW
70 if (!queue_try_remove(pp->sleeps[i], press))
71 signal_error_by_blinking();
72 bool became_empty = queue_is_empty(pp->sleeps[i]);
73 if (ph_remove_head(pp->sleeps_heap, became_empty) != i)
9ae691e9 74 signal_error_by_blinking();
0527f229
SW
75 if (became_empty) {
76 pp->sleeps[i] = NULL;
77 } else {
78 ph_insert_node(pp->sleeps_heap, i);
79 }
9ae691e9
SW
80 return true;
81}