STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
DiabloPacketUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include "DiabloEnums.h" // For enums like PacketType
4#include "DiabloPackets.h" // For all packet data structures
5#include <stdint.h> // For standard integer types
6#include <vector> // For std::vector
7
8namespace daq {
9
10//==============================================================================
11// PACKET SERIALIZATION (Struct -> uint8_t* Buffer)
12//
13// These functions take high-level data structs, serialize them into a
14// byte buffer, and return the final number of bytes to be sent.
15// A return value of 0 indicates an error.
16//==============================================================================
17
18/**
19 * @brief Creates a complete Board Heartbeat packet in the provided buffer.
20 *
21 * This is a fixed-size packet sent periodically by a board to the server to
22 * indicate it is online and operational.
23 *
24 * @param data The heartbeat data to encode (firmware SHA-256 hash, board ID, state, etc.).
25 * @param timestamp_ms Value for PacketHeader.timestamp (e.g. Arduino millis() on firmware).
26 * @param buffer The output buffer to write the final packet into.
27 * @param buffer_size The total size of the output buffer, used for safety
28 * checks.
29 * @return The number of bytes written to the buffer (always
30 * sizeof(PacketHeader) + sizeof(BoardHeartbeatPacket)), or 0 on error.
31 */
32size_t create_board_heartbeat_packet(const BoardHeartbeatPacket &data,
33 uint32_t timestamp_ms,
34 uint8_t *buffer, size_t buffer_size);
35
36/**
37 * @brief Creates a complete Sensor Data packet in the provided buffer.
38 *
39 * This is a variable-size packet containing readings from one or more sensor
40 * data chunks. It consists of a header, a fixed-size body specifying the number
41 * of chunks and sensors, followed by the actual sensor data chunks and their
42 * datapoints.
43 *
44 * @param chunks A vector of SensorDataChunkCollection structs containing the
45 * sensor data.
46 * @param num_sensors The number of sensors that are included in the packet
47 * @param timestamp_ms Value for PacketHeader.timestamp.
48 * @param buffer The output buffer to write the final packet into.
49 * @param buffer_size The total size of the output buffer.
50 * @return The total number of bytes written to the buffer, or 0 on error.
51 */
52size_t
53create_sensor_data_packet(const std::vector<SensorDataChunkCollection> &chunks, const uint8_t num_sensors,
54 uint32_t timestamp_ms,
55 uint8_t *buffer, size_t buffer_size);
56
57/**
58 * @brief Creates a simple Abort Done packet.
59 *
60 * This packet is sent from a board to the server to acknowledge that it has
61 * successfully completed its abort sequence. It has no data payload.
62 *
63 * @param timestamp_ms Value for PacketHeader.timestamp.
64 * @param buffer The output buffer to write the final packet into.
65 * @param buffer_size The total size of the output buffer.
66 * @return The number of bytes written (always sizeof(PacketHeader)), or 0 on
67 * error.
68 */
69size_t create_abort_done_packet(uint32_t timestamp_ms,
70 uint8_t *buffer, size_t buffer_size);
71
72/**
73 * @brief Creates a complete Sensor Config packet in the provided buffer.
74 *
75 * Packet layout: PacketHeader + num_sensors (1B) + N sensor_ids (1B each) +
76 * reference_voltage (1B) + necessary_for_abort (1B) +
77 * [controller_ip (4B) if necessary_for_abort] + enable_serial_printing (1B).
78 *
79 * @param sensor_ids List of sensor IDs to include in the config.
80 * @param reference_voltage ADC reference voltage selection.
81 * @param necessary_for_abort Whether this board's sensors are needed for abort.
82 * @param controller_ip IP of the abort controller (only written when necessary_for_abort is true).
83 * @param enable_serial_printing 1 to enable serial printing, 0 to disable.
84 * @param timestamp_ms Value for PacketHeader.timestamp.
85 * @param buffer The output buffer to write the packet into.
86 * @param buffer_size The size of the provided buffer.
87 * @return The total size of the created packet, or 0 on error.
88 */
89size_t create_sensor_config_packet(const std::vector<uint8_t> &sensor_ids,
90 uint8_t reference_voltage,
91 bool necessary_for_abort,
92 uint32_t controller_ip,
93 uint8_t enable_serial_printing,
94 uint32_t timestamp_ms,
95 uint8_t *buffer, size_t buffer_size);
96
97/**
98 * @brief Creates a complete Actuator Command packet in the provided buffer.
99 *
100 * Packet layout: PacketHeader + ActuatorCommandPacket + N ActuatorCommand.
101 *
102 * @param commands The list of actuator commands to serialize.
103 * @param timestamp_ms Value for PacketHeader.timestamp.
104 * @param buffer The output buffer to write the packet into.
105 * @param buffer_size The size of the provided buffer.
106 * @return The total size of the created packet, or 0 on error.
107 */
108size_t create_actuator_command_packet(const std::vector<ActuatorCommand> &commands,
109 uint32_t timestamp_ms,
110 uint8_t *buffer, size_t buffer_size);
111
112/**
113 * @brief Creates a complete Self Test packet in the provided buffer.
114 *
115 * Packet layout: PacketHeader + SelfTestPacket (adc_good + num_sensors) + N SelfTestResult.
116 *
117 * @param adc_good 1 if the TDAC self-test passed (ADC is good), 0 if it failed.
118 * @param results The list of per-sensor self-test results to serialize.
119 * @param timestamp_ms Value for PacketHeader.timestamp.
120 * @param buffer The output buffer to write the packet into.
121 * @param buffer_size The size of the provided buffer.
122 * @return The total size of the created packet, or 0 on error.
123 */
124size_t create_self_test_packet(uint8_t adc_good,
125 const std::vector<SelfTestResult> &results,
126 uint32_t timestamp_ms,
127 uint8_t *buffer, size_t buffer_size);
128
129/**
130 * @brief Creates a complete Environmental Data packet in the provided buffer.
131 *
132 * Packet layout: PacketHeader + EnvironmentalDataPacket (temperature float °C,
133 * pressure uint32 Pa, humidity float %RH).
134 */
135size_t create_environmental_data_packet(float temperature_c,
136 uint32_t pressure_pa,
137 float humidity_rh,
138 uint32_t timestamp_ms,
139 uint8_t *buffer, size_t buffer_size);
140
141/**
142 * @brief Creates a complete Stacklight Command packet in the provided buffer.
143 *
144 * Packet layout: PacketHeader + StacklightCommandPacket (red, yellow, green, buzzer; 1 = on).
145 */
146size_t create_stacklight_command_packet(const StacklightCommandPacket &data,
147 uint32_t timestamp_ms,
148 uint8_t *buffer, size_t buffer_size);
149
150/**
151 * @brief Creates a LOGS packet (type 15): buffered board log text for the server.
152 *
153 * Layout: PacketHeader (LOGS) + flags (1B) + text_len (2B, uint16 LE) + text bytes.
154 * The board is identified server-side by UDP source IP (static 192.168.2.<board_id>),
155 * so no board_id field. `text` is newline-delimited log lines blobbed together.
156 *
157 * @param flags bit0 = TRUNCATED (some lines dropped on buffer overflow); rest reserved.
158 * @param text Log text bytes (ASCII, '\n'-delimited).
159 * @param text_len Number of text bytes.
160 * @param timestamp_ms Value for PacketHeader.timestamp.
161 * @param buffer Output buffer.
162 * @param buffer_size Size of the output buffer.
163 * @return Total bytes written (sizeof(PacketHeader) + 3 + text_len), or 0 on error.
164 */
165size_t create_log_packet(uint8_t flags, const uint8_t *text, uint16_t text_len,
166 uint32_t timestamp_ms,
167 uint8_t *buffer, size_t buffer_size);
168
169//==============================================================================
170// PACKET DESERIALIZATION (uint8_t* Buffer -> Struct)
171//==============================================================================
172
173/**
174 * @brief Parses a Board Heartbeat packet from buffer.
175 * @return true on success, false on error (size/type mismatch).
176 */
177bool parse_board_heartbeat_packet(const uint8_t *buffer, size_t buffer_size,
178 PacketHeader &header_out,
179 BoardHeartbeatPacket &data_out);
180
181/**
182 * @brief Parses a Server Heartbeat packet from buffer.
183 * @return true on success, false on error (size/type mismatch).
184 */
185bool parse_server_heartbeat_packet(const uint8_t *buffer, size_t buffer_size,
186 PacketHeader &header_out,
187 ServerHeartbeatPacket &data_out);
188
189/**
190 * @brief Parses a Sensor Data packet from buffer into chunk collections.
191 * @return true on success, false on error.
192 */
193bool parse_sensor_data_packet(const uint8_t *buffer, size_t buffer_size,
194 PacketHeader &header_out,
195 std::vector<SensorDataChunkCollection> &chunks_out);
196
197/**
198 * @brief Parses an Abort Done packet from buffer.
199 * @return true on success, false on error.
200 */
201bool parse_abort_done_packet(const uint8_t *buffer, size_t buffer_size,
202 PacketHeader &header_out);
203
204/**
205 * @brief Parses an Actuator Command packet from buffer.
206 * @return true on success, false on error.
207 */
208bool parse_actuator_command_packet(const uint8_t *buffer, size_t buffer_size,
209 PacketHeader &header_out,
210 std::vector<ActuatorCommand> &commands_out);
211
212/**
213 * @brief Parses a Self Test packet from buffer.
214 * @param adc_good_out Set to 1 if the TDAC self-test passed, 0 if it failed.
215 * @return true on success, false on error.
216 */
217bool parse_self_test_packet(const uint8_t *buffer, size_t buffer_size,
218 PacketHeader &header_out,
219 uint8_t &adc_good_out,
220 std::vector<SelfTestResult> &results_out);
221
222/**
223 * @brief Parses an Environmental Data packet from buffer.
224 * @return true on success, false on error (size/type mismatch).
225 */
226bool parse_environmental_data_packet(const uint8_t *buffer, size_t buffer_size,
227 PacketHeader &header_out,
228 EnvironmentalDataPacket &data_out);
229
230/**
231 * @brief Parses a Stacklight Command packet from buffer.
232 * @return true on success, false on error (size/type mismatch).
233 */
234bool parse_stacklight_command_packet(const uint8_t *buffer, size_t buffer_size,
235 PacketHeader &header_out,
236 StacklightCommandPacket &data_out);
237
238/**
239 * @brief Parses a Sensor Config packet from buffer.
240 *
241 * Mirrors the on-wire format produced by create_sensor_config_packet.
242 * If necessary_for_abort is false the controller_ip_out is set to 0.
243 *
244 * @return true on success, false on error (type/size mismatch or buffer too small).
245 */
246bool parse_sensor_config_packet(const uint8_t *buffer, size_t buffer_size,
247 PacketHeader &header_out,
248 std::vector<uint8_t> &sensor_ids_out,
249 uint8_t &reference_voltage_out,
250 bool &necessary_for_abort_out,
251 uint32_t &controller_ip_out,
252 uint8_t &enable_serial_printing_out);
253
254/**
255 * @brief Creates a complete PWM Actuator Command packet in the provided buffer.
256 *
257 * Packet layout: PacketHeader + PWMActuatorCommandPacket + N PWMActuatorCommand.
258 *
259 * @param commands The list of PWM actuator commands to serialize.
260 * @param timestamp_ms Value for PacketHeader.timestamp.
261 * @param buffer The output buffer to write the packet into.
262 * @param buffer_size The size of the provided buffer.
263 * @return The total size of the created packet, or 0 on error.
264 */
265size_t create_pwm_actuator_packet(const std::vector<PWMActuatorCommand> &commands,
266 uint32_t timestamp_ms,
267 uint8_t *buffer, size_t buffer_size);
268
269/**
270 * @brief Parses a PWM Actuator Command packet from buffer.
271 * @return true on success, false on error.
272 */
273bool parse_pwm_actuator_packet(const uint8_t *buffer, size_t buffer_size,
274 PacketHeader &header_out,
275 std::vector<PWMActuatorCommand> &commands_out);
276
277/**
278 * @brief Creates a complete Actuator Config packet in the provided buffer.
279 *
280 * Packet layout: standard PacketHeader (ACTUATOR_CONFIG, version, timestamp),
281 * then config body: is_abort_controller (1B), N (1B), N x AbortActuatorLocation (7B each),
282 * then X (1B), X x AbortPTLocation (9B each), then enable_serial_printing (1B).
283 *
284 * @param is_abort_controller 1 if this board is the abort controller, 0 otherwise.
285 * @param abort_actuators List of abort actuator entries (N entries, 7 bytes each).
286 * @param abort_pts List of abort PT entries (X entries, 9 bytes each).
287 * @param enable_serial_printing 1 to enable serial printing, 0 to disable.
288 * @param timestamp_ms Value for PacketHeader.timestamp.
289 * @param buffer The output buffer to write the packet into.
290 * @param buffer_size The size of the provided buffer.
291 * @return The total size of the created packet, or 0 on error (e.g. buffer too small).
292 */
294 uint8_t is_abort_controller,
295 const std::vector<AbortActuatorLocation> &abort_actuators,
296 const std::vector<AbortPTLocation> &abort_pts,
297 uint8_t enable_serial_printing,
298 uint32_t timestamp_ms,
299 uint8_t *buffer, size_t buffer_size);
300
301/**
302 * @brief Parses an Actuator Config packet from buffer.
303 * Reads standard PacketHeader (must be ACTUATOR_CONFIG), then config body into outputs.
304 * @return true on success, false on error (type/size mismatch or buffer too small).
305 */
306bool parse_actuator_config_packet(const uint8_t *buffer, size_t buffer_size,
307 PacketHeader &header_out,
308 uint8_t &is_abort_controller_out,
309 std::vector<AbortActuatorLocation> &abort_actuators_out,
310 std::vector<AbortPTLocation> &abort_pts_out,
311 uint8_t &enable_serial_printing_out);
312
313/**
314 * @brief Parses a LOGS packet (type 15) from buffer.
315 *
316 * Mirrors the on-wire format produced by create_log_packet:
317 * PacketHeader (LOGS) + flags (1B) + text_len (2B, uint16 LE) + text bytes.
318 * On success, text_out points into `buffer` (no copy) and is valid for the
319 * lifetime of `buffer`.
320 *
321 * @param flags_out bit0 = TRUNCATED.
322 * @param text_out Set to point at the first text byte inside `buffer`.
323 * @param text_len_out Number of text bytes.
324 * @return true on success, false on error (type/size mismatch or buffer too small).
325 */
326bool parse_log_packet(const uint8_t *buffer, size_t buffer_size,
327 PacketHeader &header_out,
328 uint8_t &flags_out,
329 const uint8_t *&text_out,
330 uint16_t &text_len_out);
331
332} // namespace daq
std::vector< daq::SensorDataChunkCollection > chunks
Definition main.cpp:55
static bool is_abort_controller
Definition main.cpp:63
static osMessageQueueId_t commands
Definition idle_task.c:28
bool parse_self_test_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, uint8_t &adc_good_out, std::vector< SelfTestResult > &results_out)
Parses a Self Test packet from buffer.
size_t create_sensor_data_packet(const std::vector< SensorDataChunkCollection > &chunks, const uint8_t num_sensors, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Sensor Data packet in the provided buffer.
bool parse_server_heartbeat_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, ServerHeartbeatPacket &data_out)
Parses a Server Heartbeat packet from buffer.
bool parse_log_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, uint8_t &flags_out, const uint8_t *&text_out, uint16_t &text_len_out)
Parses a LOGS packet (type 15) from buffer.
size_t create_abort_done_packet(uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a simple Abort Done packet.
bool parse_sensor_config_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, std::vector< uint8_t > &sensor_ids_out, uint8_t &reference_voltage_out, bool &necessary_for_abort_out, uint32_t &controller_ip_out, uint8_t &enable_serial_printing_out)
Parses a Sensor Config packet from buffer.
size_t create_log_packet(uint8_t flags, const uint8_t *text, uint16_t text_len, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a LOGS packet (type 15): buffered board log text for the server.
bool parse_board_heartbeat_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, BoardHeartbeatPacket &data_out)
Parses a Board Heartbeat packet from buffer.
size_t create_board_heartbeat_packet(const BoardHeartbeatPacket &data, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Board Heartbeat packet in the provided buffer.
bool parse_pwm_actuator_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, std::vector< PWMActuatorCommand > &commands_out)
Parses a PWM Actuator Command packet from buffer.
size_t create_pwm_actuator_packet(const std::vector< PWMActuatorCommand > &commands, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete PWM Actuator Command packet in the provided buffer.
size_t create_stacklight_command_packet(const StacklightCommandPacket &data, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Stacklight Command packet in the provided buffer.
bool parse_stacklight_command_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, StacklightCommandPacket &data_out)
Parses a Stacklight Command packet from buffer.
size_t create_environmental_data_packet(float temperature_c, uint32_t pressure_pa, float humidity_rh, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Environmental Data packet in the provided buffer.
bool parse_actuator_command_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, std::vector< ActuatorCommand > &commands_out)
Parses an Actuator Command packet from buffer.
size_t create_sensor_config_packet(const std::vector< uint8_t > &sensor_ids, uint8_t reference_voltage, bool necessary_for_abort, uint32_t controller_ip, uint8_t enable_serial_printing, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Sensor Config packet in the provided buffer.
bool parse_environmental_data_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, EnvironmentalDataPacket &data_out)
Parses an Environmental Data packet from buffer.
size_t create_self_test_packet(uint8_t adc_good, const std::vector< SelfTestResult > &results, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Self Test packet in the provided buffer.
bool parse_abort_done_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out)
Parses an Abort Done packet from buffer.
size_t create_actuator_config_packet(uint8_t is_abort_controller, const std::vector< AbortActuatorLocation > &abort_actuators, const std::vector< AbortPTLocation > &abort_pts, uint8_t enable_serial_printing, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Actuator Config packet in the provided buffer.
bool parse_actuator_config_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, uint8_t &is_abort_controller_out, std::vector< AbortActuatorLocation > &abort_actuators_out, std::vector< AbortPTLocation > &abort_pts_out, uint8_t &enable_serial_printing_out)
Parses an Actuator Config packet from buffer.
bool parse_sensor_data_packet(const uint8_t *buffer, size_t buffer_size, PacketHeader &header_out, std::vector< SensorDataChunkCollection > &chunks_out)
Parses a Sensor Data packet from buffer into chunk collections.
size_t create_actuator_command_packet(const std::vector< ActuatorCommand > &commands, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Actuator Command packet in the provided buffer.