]> git.scottworley.com Git - tattlekey/blob - client/press.c
client: Change wire format!: Expand `ago` field: 16 bits → 32 bits
[tattlekey] / client / press.c
1 /* tattlekey: A one-key UDP keyboard
2 * Copyright (C) 2023 Scott Worley <scottworley@scottworley.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include "press.h"
19 #include "blink.h"
20 #include "config.h"
21
22 static uint32_t press_next_send(press_t *s) {
23 return s->timestamp + (1 << s->send_count) - 1;
24 }
25
26 static uint32_t press_queue_next_send(queue_t *q) {
27 press_t press;
28 return queue_try_peek(q, &press) ? press_next_send(&press) : UINT32_MAX;
29 }
30
31 static bool next_send_less_than(void *user_data, pheap_node_id_t a,
32 pheap_node_id_t b) {
33 queue_t **sleeps = (queue_t **)user_data;
34 return press_queue_next_send(sleeps[a]) < press_queue_next_send(sleeps[b]);
35 }
36
37 static void *xcalloc(size_t nmemb, size_t size) {
38 void *p = calloc(nmemb, size);
39 if (p == NULL)
40 signal_error_by_blinking();
41 return p;
42 }
43
44 press_pile_t *create_press_pile() {
45 press_pile_t *pp = (press_pile_t *)xcalloc(1, sizeof(press_pile_t));
46 pp->presses = (queue_t *)xcalloc(config_resend_count, sizeof(queue_t));
47 pp->sleeps = (queue_t **)xcalloc(config_resend_count, sizeof(queue_t *));
48 for (uint i = 0; i < config_resend_count; i++) {
49 const uint paranoid_safety_fudge = 10;
50 uint element_count = paranoid_safety_fudge + (1 << i);
51 element_count = MAX(element_count, config_minimum_queue_size);
52 element_count = MIN(element_count, config_maximum_queue_size);
53 queue_init(&pp->presses[i], sizeof(press_t), element_count);
54 }
55 pp->sleeps_heap =
56 ph_create(config_resend_count, next_send_less_than, pp->sleeps);
57 if (pp->sleeps_heap == NULL)
58 signal_error_by_blinking();
59 return pp;
60 }
61
62 void add_press(press_pile_t *pp, press_t *press) {
63 u16_t sc = press->send_count;
64 if (sc >= config_resend_count)
65 signal_error_by_blinking();
66 bool was_empty = queue_is_empty(&pp->presses[sc]);
67 /* No error check; blithely continue if the queue was full. */
68 queue_try_add(&pp->presses[sc], press);
69 if (was_empty) {
70 pheap_node_id_t i = ph_new_node(pp->sleeps_heap);
71 pp->sleeps[i] = &pp->presses[sc];
72 ph_insert_node(pp->sleeps_heap, i);
73 }
74 }
75
76 int32_t next_scheduled_send(press_pile_t *pp) {
77 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
78 if (i == 0)
79 return -1;
80 return press_queue_next_send(pp->sleeps[i]);
81 }
82
83 bool get_press_due_for_resend(press_pile_t *pp, uint32_t now, press_t *press) {
84 pheap_node_id_t i = ph_peek_head(pp->sleeps_heap);
85 if (i == 0 || press_queue_next_send(pp->sleeps[i]) > now)
86 return false;
87 if (!queue_try_remove(pp->sleeps[i], press))
88 signal_error_by_blinking();
89 bool became_empty = queue_is_empty(pp->sleeps[i]);
90 if (ph_remove_head(pp->sleeps_heap, became_empty) != i)
91 signal_error_by_blinking();
92 if (became_empty) {
93 pp->sleeps[i] = NULL;
94 } else {
95 ph_insert_node(pp->sleeps_heap, i);
96 }
97 return true;
98 }