]> git.scottworley.com Git - tattlekey/blame - client/press.c
client: get_press_due_for_resend()
[tattlekey] / client / press.c
CommitLineData
00ca9fa6 1#include "press.h"
e3ff9e0d 2#include "blink.h"
2dcbb2a0 3
17461dec 4static uint32_t next_send(press_t *s) {
2dcbb2a0
SW
5 return s->timestamp + (1 << s->send_count) - 1;
6}
7
e3ff9e0d
SW
8static bool next_send_less_than(void *user_data, pheap_node_id_t a,
9 pheap_node_id_t b) {
17461dec
SW
10 press_t *presses = (press_t *)user_data;
11 return next_send(&presses[a]) < next_send(&presses[b]);
2dcbb2a0 12}
67001c93 13
e3ff9e0d
SW
14press_pile_t *create_press_pile() {
15 press_pile_t *pp = (press_pile_t *)malloc(sizeof(press_pile_t));
16 if (pp == NULL)
17 signal_error_by_blinking();
18 pp->presses = calloc(PICO_PHEAP_MAX_ENTRIES, sizeof(press_t));
19 if (pp->presses == NULL)
20 signal_error_by_blinking();
21 pp->sleeps_heap =
22 ph_create(PICO_PHEAP_MAX_ENTRIES, next_send_less_than, pp->presses);
23 if (pp->sleeps_heap == NULL)
24 signal_error_by_blinking();
25 return pp;
26}
27
f68e05b2 28void add_press(press_pile_t *pp, press_t *press) {
e3ff9e0d 29 pheap_node_id_t i = ph_new_node(pp->sleeps_heap);
67001c93
SW
30 if (i == 0) {
31 /* TODO: Don't drop new presses just because sleeps_heap is full of old
32 * presses. */
33 return;
34 }
f68e05b2 35 pp->presses[i] = *press;
e3ff9e0d 36 ph_insert_node(pp->sleeps_heap, i);
67001c93 37}
b512ec26 38
e3ff9e0d
SW
39int32_t next_scheduled_send(press_pile_t *pp) {
40 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
b512ec26
SW
41 if (i == 0)
42 return -1;
e3ff9e0d 43 return next_send(&pp->presses[i]);
b512ec26 44}
9ae691e9
SW
45
46bool get_press_due_for_resend(press_pile_t *pp, uint32_t now, press_t *press) {
47 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
48 if (i == 0 || next_send(&pp->presses[i]) > now)
49 return false;
50 if (ph_remove_head(pp->sleeps_heap, true) != i)
51 signal_error_by_blinking();
52 *press = pp->presses[i];
53 return true;
54}