1use std::fmt;
5
6cfg_if::cfg_if! {
7 if #[cfg(feature = "random")] {
8 use crate::FromRandom;
9 extern crate rand;
10 use rand::Rng;
11 }
12}
13
14#[cfg(feature = "pybindings")]
15use pyo3::pyclass;
16#[cfg(feature = "pybindings")]
17use pyo3::pymethods;
18
19#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, serde::Deserialize, serde::Serialize)]
22#[cfg_attr(feature = "pybindings", pyclass)]
23#[repr(u8)]
24pub enum PacketType {
25 Unknown = 0u8,
26 RBEvent = 20u8,
27 TofEvent = 21u8,
28 RBWaveform = 22u8,
29 TofEventSummary = 23u8,
30 HeartBeatDataSink = 40u8,
31 MasterTrigger = 60u8, TriggerConfig = 61u8,
33 MTBHeartbeat = 62u8,
34 EVTBLDRHeartbeat = 63u8,
35 RBChannelMaskConfig = 64u8,
36 TofRBConfig = 68u8,
37 AnalysisEngineConfig = 69u8,
38 RBEventHeader = 70u8, TOFEventBuilderConfig = 71u8,
40 DataPublisherConfig = 72u8,
41 TofRunConfig = 73u8,
42 CPUMoniData = 80u8,
43 MonitorMtb = 90u8,
44 RBMoniData = 100u8,
45 PBMoniData = 101u8,
46 LTBMoniData = 102u8,
47 PAMoniData = 103u8,
48 RBEventMemoryView = 120u8, RBCalibration = 130u8,
51 TofCommand = 140u8,
52 TofCommandV2 = 141u8,
53 TofResponse = 142u8,
54 RBCommand = 150u8,
56 RBPing = 160u8,
58 PreampBiasConfig = 161u8,
59 RunConfig = 162u8,
60 LTBThresholdConfig = 163u8,
61 TofDetectorStatus = 171u8,
65 ConfigBinary = 201u8,
68 LiftofRBBinary = 202u8,
69 LiftofBinaryService = 203u8,
70 LiftofCCBinary = 204u8,
71 RBCalibrationFlightV = 210u8,
72 RBCalibrationFlightT = 211u8,
73 BfswAckPacket = 212u8,
76 MultiPacket = 255u8,
78}
79
80impl fmt::Display for PacketType {
81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82 let r = serde_json::to_string(self).unwrap_or(
83 String::from("Error - Don't understand packet type!"));
84 write!(f, "<PacketType: {}>", r)
85 }
86}
87
88impl From<u8> for PacketType {
89 fn from(value: u8) -> Self {
90 match value {
91 0 => PacketType::Unknown,
92 20 => PacketType::RBEvent,
93 21 => PacketType::TofEvent,
94 22 => PacketType::RBWaveform,
95 23 => PacketType::TofEventSummary,
96 40 => PacketType::HeartBeatDataSink,
97 60 => PacketType::MasterTrigger,
98 61 => PacketType::TriggerConfig,
99 62 => PacketType::MTBHeartbeat,
100 63 => PacketType::EVTBLDRHeartbeat,
101 64 => PacketType::RBChannelMaskConfig,
102 68 => PacketType::TofRBConfig,
103 69 => PacketType::AnalysisEngineConfig,
104 70 => PacketType::RBEventHeader,
105 72 => PacketType::DataPublisherConfig,
106 73 => PacketType::TofRunConfig,
107 80 => PacketType::CPUMoniData,
108 90 => PacketType::MonitorMtb,
109 100 => PacketType::RBMoniData,
110 101 => PacketType::PBMoniData ,
111 102 => PacketType::LTBMoniData ,
112 103 => PacketType::PAMoniData ,
113 120 => PacketType::RBEventMemoryView,
114 130 => PacketType::RBCalibration,
115 140 => PacketType::TofCommand,
116 141 => PacketType::TofCommandV2,
117 142 => PacketType::TofResponse,
118 150 => PacketType::RBCommand,
119 160 => PacketType::RBPing,
120 161 => PacketType::PreampBiasConfig,
121 162 => PacketType::RunConfig,
122 163 => PacketType::LTBThresholdConfig,
123 171 => PacketType::TofDetectorStatus,
124 201 => PacketType::ConfigBinary,
125 202 => PacketType::LiftofRBBinary,
126 203 => PacketType::LiftofBinaryService,
127 204 => PacketType::LiftofCCBinary,
128 210 => PacketType::RBCalibrationFlightV,
129 211 => PacketType::RBCalibrationFlightT,
130 212 => PacketType::BfswAckPacket,
131 255 => PacketType::MultiPacket,
132 _ => PacketType::Unknown
133 }
134 }
135}
136
137#[cfg(feature = "pybindings")]
138#[pymethods]
139impl PacketType {
140
141 #[getter]
142 fn __eq__(&self, b: &PacketType) -> bool {
143 self == b
144 }
145
146 #[getter]
147 fn __hash__(&self) -> usize {
148 (*self as u8) as usize
149 }
194}
195
196
197#[cfg(feature = "random")]
198impl FromRandom for PacketType {
199
200 fn from_random() -> Self {
201 let choices = [
202 PacketType::Unknown,
203 PacketType::TofEvent,
204 PacketType::RBWaveform,
205 PacketType::TofEventSummary,
206 PacketType::MasterTrigger,
207 PacketType::TriggerConfig,
208 PacketType::HeartBeatDataSink,
209 PacketType::MTBHeartbeat,
210 PacketType::EVTBLDRHeartbeat,
211 PacketType::RBEventHeader,
212 PacketType::RBEvent,
213 PacketType::RBEventMemoryView,
214 PacketType::TofCommand,
215 PacketType::TofCommandV2,
216 PacketType::TofResponse,
217 PacketType::TofRBConfig,
218 PacketType::RBChannelMaskConfig,
219 PacketType::DataPublisherConfig,
220 PacketType::TofRunConfig,
221 PacketType::AnalysisEngineConfig,
222 PacketType::RBCommand,
223 PacketType::RBPing,
224 PacketType::PreampBiasConfig,
225 PacketType::RunConfig,
226 PacketType::LTBThresholdConfig,
227 PacketType::RBMoniData,
228 PacketType::PBMoniData,
229 PacketType::LTBMoniData,
230 PacketType::PAMoniData,
231 PacketType::CPUMoniData,
232 PacketType::MonitorMtb,
233 PacketType::RBCalibration,
234 PacketType::TofDetectorStatus,
235 PacketType::ConfigBinary,
236 PacketType::LiftofRBBinary,
237 PacketType::LiftofBinaryService,
238 PacketType::LiftofCCBinary,
239 PacketType::BfswAckPacket,
240 PacketType::RBCalibrationFlightV,
241 PacketType::RBCalibrationFlightT,
242 ];
243 let mut rng = rand::thread_rng();
244 let idx = rng.gen_range(0..choices.len());
245 choices[idx]
246 }
247}
248
249#[test]
250fn test_packet_types() {
251 let mut type_codes = Vec::<u8>::new();
252 type_codes.push(PacketType::Unknown as u8);
253 type_codes.push(PacketType::TofEvent as u8);
254 type_codes.push(PacketType::RBWaveform as u8);
255 type_codes.push(PacketType::TofEventSummary as u8);
256 type_codes.push(PacketType::TriggerConfig as u8);
257 type_codes.push(PacketType::MasterTrigger as u8);
258 type_codes.push(PacketType::HeartBeatDataSink as u8);
259 type_codes.push(PacketType::MTBHeartbeat as u8);
260 type_codes.push(PacketType::EVTBLDRHeartbeat as u8);
261 type_codes.push(PacketType::RBEventHeader as u8);
262 type_codes.push(PacketType::RBEvent as u8);
263 type_codes.push(PacketType::TofRBConfig as u8);
264 type_codes.push(PacketType::RBEventMemoryView as u8);
265 type_codes.push(PacketType::TofCommand as u8);
266 type_codes.push(PacketType::TofCommandV2 as u8);
267 type_codes.push(PacketType::TofResponse as u8);
268 type_codes.push(PacketType::RBCommand as u8);
269 type_codes.push(PacketType::RBMoniData as u8);
270 type_codes.push(PacketType::PBMoniData as u8);
271 type_codes.push(PacketType::LTBMoniData as u8);
272 type_codes.push(PacketType::PAMoniData as u8);
273 type_codes.push(PacketType::CPUMoniData as u8);
274 type_codes.push(PacketType::RBPing as u8);
275 type_codes.push(PacketType::PreampBiasConfig as u8);
276 type_codes.push(PacketType::RunConfig as u8);
277 type_codes.push(PacketType::TofRunConfig as u8);
278 type_codes.push(PacketType::LTBThresholdConfig as u8);
279 type_codes.push(PacketType::AnalysisEngineConfig as u8);
280 type_codes.push(PacketType::DataPublisherConfig as u8);
281 type_codes.push(PacketType::RBChannelMaskConfig as u8);
282 type_codes.push(PacketType::MonitorMtb as u8);
283 type_codes.push(PacketType::RBCalibration as u8);
284 type_codes.push(PacketType::TofDetectorStatus as u8);
285 type_codes.push(PacketType::ConfigBinary as u8);
286 type_codes.push(PacketType::LiftofCCBinary as u8);
287 type_codes.push(PacketType::LiftofRBBinary as u8);
288 type_codes.push(PacketType::LiftofBinaryService as u8);
289 type_codes.push(PacketType::BfswAckPacket as u8);
290 type_codes.push(PacketType::RBCalibrationFlightV as u8);
291 type_codes.push(PacketType::RBCalibrationFlightT as u8);
292 for tc in type_codes.iter() {
293 assert_eq!(*tc,PacketType::try_from(*tc).unwrap() as u8);
294 }
295}
296