STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
radio_fake.cpp
Go to the documentation of this file.
1#include "radio_fake.h"
2
3#include <assert.h>
4#include <string.h>
5
6namespace radio_link {
7namespace {
8
9constexpr unsigned kQueueDepth = 8; // packets the fake can hold at once
10constexpr unsigned kMaxPacket = 255; // the biggest a uint8_t length can say
11
12struct Packet {
13 uint8_t data[kMaxPacket];
14 uint8_t len;
15};
16
18unsigned g_head = 0; // next packet to hand out
19unsigned g_count = 0;
20bool g_chip_present = true;
22uint8_t g_tx_len = 0;
23unsigned g_tx_count = 0;
26
27} // namespace
28
29namespace fake {
30
31void reset(bool chip_present) {
32 g_head = 0;
33 g_count = 0;
34 g_chip_present = chip_present;
35 g_tx_len = 0;
36 g_tx_count = 0;
37 g_send_result = RadioStatus::Ok;
38 g_next_receive = RadioStatus::Ok;
39}
40
42 g_next_receive = result;
43}
44
46 g_send_result = result;
47}
48
49bool inject(const uint8_t* bytes, uint8_t len) {
50 if (g_count >= kQueueDepth || len == 0) {
51 return false;
52 }
53 const unsigned slot = (g_head + g_count) % kQueueDepth;
54 memcpy(g_rx[slot].data, bytes, len);
55 g_rx[slot].len = len;
56 g_count++;
57 return true;
58}
59
60uint8_t last_sent(uint8_t* out, uint8_t out_cap) {
61 const uint8_t n = g_tx_len < out_cap ? g_tx_len : out_cap;
62 memcpy(out, g_tx, n);
63 return n;
64}
65
66unsigned sent_count() {
67 return g_tx_count;
68}
69
70} // namespace fake
71
73 return g_chip_present ? RadioStatus::Ok : RadioStatus::ErrNotFound;
74}
75
76RadioStatus radio_send(const uint8_t* bytes, uint8_t len) {
77 // radio.h: a packet is 1 to 255 bytes. Sending nothing (or from nullptr)
78 // is a caller bug, so stop the test run loudly instead of answering Ok.
79 assert(bytes != nullptr && len > 0);
80 if (!g_chip_present) {
82 }
83 if (g_send_result != RadioStatus::Ok) {
84 return g_send_result;
85 }
86 memcpy(g_tx, bytes, len);
87 g_tx_len = len;
88 g_tx_count++;
89 return RadioStatus::Ok;
90}
91
92RadioStatus radio_receive(uint8_t* buf, uint8_t max_len, uint8_t& len) {
93 if (g_next_receive != RadioStatus::Ok) {
96 return result;
97 }
98 if (g_count == 0) {
100 }
101 const unsigned slot = g_head;
102 g_head = (g_head + 1) % kQueueDepth;
103 g_count--;
104 if (g_rx[slot].len > max_len) {
105 return RadioStatus::ErrTooLong; // packet thrown away, as radio.h says
106 }
107 memcpy(buf, g_rx[slot].data, g_rx[slot].len);
108 len = g_rx[slot].len;
109 return RadioStatus::Ok;
110}
111
112} // namespace radio_link
json_pointer result
Definition json.hpp:14064
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474