tof_dataclasses/packets/
packet_type.rs

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/// PacketType identifies the payload in TofPackets
///
/// This needs to be kept in sync with the C++ API
use std::fmt;

cfg_if::cfg_if! {
  if #[cfg(feature = "random")]  {
    use crate::FromRandom;
    extern crate rand;
    use rand::Rng;
  }
}

#[cfg(feature = "pybindings")]
use pyo3::pyclass;
#[cfg(feature = "pybindings")]
use pyo3::pymethods;

/// Types of serializable data structures used
/// throughout the tof system
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "pybindings", pyclass)]
#[repr(u8)]
pub enum PacketType {
  Unknown               = 0u8, 
  RBEvent               = 20u8,
  TofEvent              = 21u8,
  RBWaveform            = 22u8,
  TofEventSummary       = 23u8,
  HeartBeatDataSink     = 40u8,    
  MasterTrigger         = 60u8,    // needs to be renamed to either MasterTriggerEvent or MTEvent
  TriggerConfig         = 61u8,
  MTBHeartbeat          = 62u8, 
  EVTBLDRHeartbeat      = 63u8,
  RBChannelMaskConfig   = 64u8,
  TofRBConfig           = 68u8,
  AnalysisEngineConfig  = 69u8,
  RBEventHeader         = 70u8,    // needs to go away
  TOFEventBuilderConfig = 71u8,
  DataPublisherConfig   = 72u8,
  TofRunConfig          = 73u8,
  CPUMoniData           = 80u8,
  MonitorMtb            = 90u8,
  RBMoniData            = 100u8,
  PBMoniData            = 101u8,
  LTBMoniData           = 102u8,
  PAMoniData            = 103u8,
  RBEventMemoryView     = 120u8, // We'll keep it for now - indicates that the event
                                 // still needs to be processed.
  RBCalibration         = 130u8,
  TofCommand            = 140u8,
  TofCommandV2          = 141u8,
  TofResponse           = 142u8,
  // needs to go away
  RBCommand             = 150u8,
  // > 160 configuration packets
  RBPing                = 160u8,
  PreampBiasConfig      = 161u8,
  RunConfig             = 162u8,
  LTBThresholdConfig    = 163u8,
  // avoid 170 since it is our 
  // delimiter
  // >= 171 detector status
  TofDetectorStatus     = 171u8,
  // use the > 200 values for transmitting
  // various binary files
  ConfigBinary          = 201u8,
  LiftofRBBinary        = 202u8,
  LiftofBinaryService   = 203u8,
  LiftofCCBinary        = 204u8,
  RBCalibrationFlightV  = 210u8,
  RBCalibrationFlightT  = 211u8,
  /// A klude which allows us to send bfsw ack packets
  /// through the TOF system
  BfswAckPacket         = 212u8,
  /// a MultiPacket consists of other TofPackets
  MultiPacket           = 255u8,
}

impl fmt::Display for PacketType {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let r = serde_json::to_string(self).unwrap_or(
      String::from("Error - Don't understand packet type!"));
    write!(f, "<PacketType: {}>", r)
  }
}

impl From<u8> for PacketType {
  fn from(value: u8) -> Self {
    match value {
      0   => PacketType::Unknown,
      20  => PacketType::RBEvent,
      21  => PacketType::TofEvent,
      22  => PacketType::RBWaveform,
      23  => PacketType::TofEventSummary,
      40  => PacketType::HeartBeatDataSink,
      60  => PacketType::MasterTrigger,
      61  => PacketType::TriggerConfig,
      62  => PacketType::MTBHeartbeat,
      63  => PacketType::EVTBLDRHeartbeat,
      64  => PacketType::RBChannelMaskConfig,
      68  => PacketType::TofRBConfig,
      69  => PacketType::AnalysisEngineConfig,
      70  => PacketType::RBEventHeader,
      72  => PacketType::DataPublisherConfig,
      73  => PacketType::TofRunConfig,
      80  => PacketType::CPUMoniData,
      90  => PacketType::MonitorMtb,
      100 => PacketType::RBMoniData,
      101 => PacketType::PBMoniData   ,
      102 => PacketType::LTBMoniData  ,
      103 => PacketType::PAMoniData   ,
      120 => PacketType::RBEventMemoryView,
      130 => PacketType::RBCalibration,
      140 => PacketType::TofCommand,
      141 => PacketType::TofCommandV2,
      142 => PacketType::TofResponse,
      150 => PacketType::RBCommand,
      160 => PacketType::RBPing,
      161 => PacketType::PreampBiasConfig,
      162 => PacketType::RunConfig,
      163 => PacketType::LTBThresholdConfig,
      171 => PacketType::TofDetectorStatus,
      201 => PacketType::ConfigBinary,
      202 => PacketType::LiftofRBBinary,
      203 => PacketType::LiftofBinaryService,
      204 => PacketType::LiftofCCBinary,
      210 => PacketType::RBCalibrationFlightV,
      211 => PacketType::RBCalibrationFlightT,
      212 => PacketType::BfswAckPacket,
      255 => PacketType::MultiPacket,
      _     => PacketType::Unknown
    }
  }
}

#[cfg(feature = "pybindings")]
#[pymethods]
impl PacketType {

  #[getter]
  fn __eq__(&self, b: &PacketType) -> bool {
      self == b
  }

  #[getter]
  fn __hash__(&self) -> usize {
    (*self as u8) as usize
    //match self {
    //  PacketType::Unknown               => 0 , 
    //  PacketType::RBEvent               => 20,
    //  PacketType::TofEvent              => 21,
    //  PacketType::RBWaveform            => 22,
    //  PacketType::TofEventSummary       => 23,
    //  PacketType::HeartBeatDataSink     => 40,    
    //  PacketType::MasterTrigger         => 60,    // needs to be renamed to either MasterTriggerEvent or MTEvent
    //  PacketType::TriggerConfig         => 61,
    //  PacketType::MTBHeartbeat          => 62, 
    //  PacketType::EVTBLDRHeartbeat      => 63,
    //  PacketType::RBChannelMaskConfig   => 64,
    //  PacketType::TofRBConfig           => 68,
    //  PacketType::AnalysisEngineConfig  => 69,
    //  PacketType::RBEventHeader         => 70,    // needs to go away
    //  PacketType::TOFEventBuilderConfig => 71,
    //  PacketType::DataPublisherConfig   => 72,
    //  PacketType::TofRunConfig          => 73,
    //  PacketType::CPUMoniData           => 80,
    //  PacketType::MonitorMtb            => 90,
    //  PacketType::RBMoniData            => 100,
    //  PacketType::PBMoniData            => 101,
    //  PacketType::LTBMoniData           => 102,
    //  PacketType::PAMoniData            => 103,
    //  PacketType::RBEventMemoryView     => 120, // We'll keep it for now - indicates that the event
    //  PacketType::RBCalibration         => 130,
    //  PacketType::TofCommand            => 140,
    //  PacketType::TofCommandV2          => 141,
    //  PacketType::TofResponse           => 142,
    //  PacketType::RBCommand             => 150,
    //  PacketType::RBPing                => 160,
    //  PacketType::PreampBiasConfig      => 161,
    //  PacketType::RunConfig             => 162,
    //  PacketType::LTBThresholdConfig    => 163,
    //  PacketType::TofDetectorStatus     => 171,
    //  PacketType::ConfigBinary          => 201,
    //  PacketType::LiftofRBBinary        => 202,
    //  PacketType::LiftofBinaryService   => 203,
    //  PacketType::LiftofCCBinary        => 204,
    //  PacketType::RBCalibrationFlightV  => 210,
    //  PacketType::RBCalibrationFlightT  => 211,
    //  PacketType::BfswAckPacket         => 212,
    //  PacketType::MultiPacket           => 255,
    //}
  } 
}


#[cfg(feature = "random")]
impl FromRandom for PacketType {
  
  fn from_random() -> Self {
    let choices = [
      PacketType::Unknown,
      PacketType::TofEvent,
      PacketType::RBWaveform,
      PacketType::TofEventSummary,
      PacketType::MasterTrigger,
      PacketType::TriggerConfig, 
      PacketType::HeartBeatDataSink,
      PacketType::MTBHeartbeat,
      PacketType::EVTBLDRHeartbeat,
      PacketType::RBEventHeader,
      PacketType::RBEvent,
      PacketType::RBEventMemoryView,
      PacketType::TofCommand,
      PacketType::TofCommandV2,
      PacketType::TofResponse,
      PacketType::TofRBConfig,
      PacketType::RBChannelMaskConfig,
      PacketType::DataPublisherConfig,
      PacketType::TofRunConfig,
      PacketType::AnalysisEngineConfig,
      PacketType::RBCommand,
      PacketType::RBPing,
      PacketType::PreampBiasConfig,
      PacketType::RunConfig,
      PacketType::LTBThresholdConfig,
      PacketType::RBMoniData,
      PacketType::PBMoniData,
      PacketType::LTBMoniData,
      PacketType::PAMoniData,
      PacketType::CPUMoniData,
      PacketType::MonitorMtb,
      PacketType::RBCalibration,
      PacketType::TofDetectorStatus,
      PacketType::ConfigBinary,
      PacketType::LiftofRBBinary,
      PacketType::LiftofBinaryService,
      PacketType::LiftofCCBinary,
      PacketType::BfswAckPacket,
      PacketType::RBCalibrationFlightV,
      PacketType::RBCalibrationFlightT,
    ];
    let mut rng  = rand::thread_rng();
    let idx = rng.gen_range(0..choices.len());
    choices[idx]
  }
}

#[test]
fn test_packet_types() {
  let mut type_codes = Vec::<u8>::new();
  type_codes.push(PacketType::Unknown as u8);
  type_codes.push(PacketType::TofEvent as u8);
  type_codes.push(PacketType::RBWaveform as u8);
  type_codes.push(PacketType::TofEventSummary as u8);
  type_codes.push(PacketType::TriggerConfig as u8);
  type_codes.push(PacketType::MasterTrigger as u8);
  type_codes.push(PacketType::HeartBeatDataSink as u8);
  type_codes.push(PacketType::MTBHeartbeat as u8);
  type_codes.push(PacketType::EVTBLDRHeartbeat as u8);
  type_codes.push(PacketType::RBEventHeader as u8);
  type_codes.push(PacketType::RBEvent as u8);
  type_codes.push(PacketType::TofRBConfig as u8);
  type_codes.push(PacketType::RBEventMemoryView as u8);
  type_codes.push(PacketType::TofCommand as u8);
  type_codes.push(PacketType::TofCommandV2 as u8);
  type_codes.push(PacketType::TofResponse as u8);
  type_codes.push(PacketType::RBCommand as u8);
  type_codes.push(PacketType::RBMoniData as u8);
  type_codes.push(PacketType::PBMoniData as u8);
  type_codes.push(PacketType::LTBMoniData as u8);
  type_codes.push(PacketType::PAMoniData as u8);
  type_codes.push(PacketType::CPUMoniData as u8);
  type_codes.push(PacketType::RBPing as u8);
  type_codes.push(PacketType::PreampBiasConfig as u8);
  type_codes.push(PacketType::RunConfig as u8);
  type_codes.push(PacketType::TofRunConfig as u8);
  type_codes.push(PacketType::LTBThresholdConfig as u8);
  type_codes.push(PacketType::AnalysisEngineConfig as u8);
  type_codes.push(PacketType::DataPublisherConfig as u8);
  type_codes.push(PacketType::RBChannelMaskConfig as u8);
  type_codes.push(PacketType::MonitorMtb as u8);
  type_codes.push(PacketType::RBCalibration as u8);
  type_codes.push(PacketType::TofDetectorStatus as u8);
  type_codes.push(PacketType::ConfigBinary as u8);
  type_codes.push(PacketType::LiftofCCBinary as u8);
  type_codes.push(PacketType::LiftofRBBinary as u8);
  type_codes.push(PacketType::LiftofBinaryService as u8);
  type_codes.push(PacketType::BfswAckPacket as u8);
  type_codes.push(PacketType::RBCalibrationFlightV as u8);
  type_codes.push(PacketType::RBCalibrationFlightT as u8);
  for tc in type_codes.iter() {
    assert_eq!(*tc,PacketType::try_from(*tc).unwrap() as u8);  
  }
}