-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpacket_queue.c
More file actions
67 lines (56 loc) · 1.03 KB
/
packet_queue.c
File metadata and controls
67 lines (56 loc) · 1.03 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
#include "packet_queue.h"
packet_queue_t pq_init(void)
{
packet_queue_t q = {
.head = 0,
.tail = 0,
.len = 0,
.packets = {},
};
return q;
}
void pq_push(packet_queue_t *q, ctap2hid_packet_t packet)
{
q->len++;
if (q->len > PACKET_QUEUE_LEN)
{
q->len--;
return;
}
q->packets[q->tail] = packet;
q->tail++;
if (q->tail > PACKET_QUEUE_LEN)
{
q->tail = 0;
}
}
void pq_pop(packet_queue_t *q)
{
if (q->len == 0)
return;
q->head++;
q->len--;
}
void pq_pop_n(packet_queue_t *q, uint8_t n)
{
for (; q->len > 0 && n > 0; n--)
pq_pop(q);
}
ctap2hid_packet_t *pq_peek_n(packet_queue_t *q, uint8_t n)
{
if (q->len <= n)
return NULL;
return &q->packets[(q->head + n) % PACKET_QUEUE_LEN];
}
ctap2hid_packet_t *pq_peek(packet_queue_t *q)
{
return pq_peek_n(q, 0);
}
bool pq_is_empty(packet_queue_t *q)
{
return q->len == 0;
}
bool pq_is_full(packet_queue_t *q)
{
return q->len == PACKET_QUEUE_LEN;
}