tof_dataclasses/commands/
factory.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! The actual command list - one function per ocmmand
//!
//! These are factory functions and each of them will 
//! return a single command

use std::collections::HashMap;

use crate::commands::{
  TofCommandV2,
  TofCommandCode
};

use crate::commands::config::{
  TriggerConfig,
  TOFEventBuilderConfig,
  DataPublisherConfig,
  TofRunConfig,
  TofRBConfig,
};

use crate::serialization::Serialization;

/// A hardwired map of RB -> RAT
pub fn get_rbratmap_hardcoded() ->  HashMap<u8,u8> {
  warn!("Using hardcoded rbratmap!");
  let mapping = HashMap::<u8,u8>::from(
      [(1, 10), 
       (2, 15), 
       (3,  1),  
       (4, 15), 
       (5, 20), 
       (6, 19), 
       (7, 17), 
       (8,  9),
       (9, 13),  
       (11,10),
       (13, 4), 
       (14, 2), 
       (15, 1), 
       (16, 8), 
       (17,17),
       (18,13),
       (19, 7), 
       (20, 7), 
       (21, 5), 
       (22,11),
       (23, 5), 
       (24, 6), 
       (25, 8), 
       (26,11),
       (27, 6), 
       (28,20),
       (29, 3), 
       (30, 9), 
       (31, 3), 
       (32, 2), 
       (33,18),
       (34,18),
       (35, 4), 
       (36,19),
       (39,12),
       (40,12),
       (41,14),
       (42,14),
       (44,16),
       (46,16)]);
  mapping
}

/// A hardwired map of RAT -> (RB1, RB2)
pub fn get_ratrbmap_hardcoded() ->  HashMap<u8,(u8,u8)> {
  warn!("Using hardcoded ratrb map!");
  let mapping = HashMap::<u8,(u8,u8)>::from(
      [(1, (3,15)), 
       (2, (32,14)), 
       (3, (31,29)),  
       (4, (35,13)), 
       (5, (23,21)), 
       (6, (27,24)), 
       (7, (20,19)), 
       (8, (16,25)),  
       (9, (8,30)),
       (10,(1,11)), 
       (11,(26,22)), 
       (12,(39,40)),
       (13,(9,18)), 
       (14,(41,42)),
       (15,(2,4)),
       (16,(46,44)), 
       (17,(7,17)), 
       (18,(33,34)), 
       (19,(36,6)), 
       (20,(28,5))]); 
  mapping
}

/// A hardwired map of PDU #id PDUCHANNEL #id to (RAT,RAT)
///
/// Can be used to synchronize powering down proces for 
/// RATs
pub fn get_ratpdumap_hardcoded() ->  HashMap<u8,HashMap::<u8, (u8,u8)>> {
  warn!("Using hardcoded rat-pdu map!");
  let mut mapping = HashMap::<u8,HashMap::<u8,(u8,u8)>>::new();
  let mut ch_map = HashMap::<u8, (u8,u8)>::from([(3, (15,16)), (7, (8,9))]);
  mapping.insert(0u8, ch_map.clone());
  ch_map = HashMap::<u8, (u8, u8)>::from([(2, (2,17)), (3, (4,5)), (5, (13,14))]);
  mapping.insert(1u8, ch_map.clone());
  ch_map = HashMap::<u8, (u8, u8)>::from([(3, (12,20)), (4, (10,11)), (5, (8,9))]);
  mapping.insert(2u8, ch_map.clone());
  ch_map = HashMap::<u8, (u8, u8)>::from([(2, (6,7)), (3, (1,3))]);
  mapping.insert(3u8, ch_map.clone());
  mapping
}

/// Send the 'sudo shutdown now' command to a single RB
///
/// # Arguements:
///   * rb :  The RB id of the RB to be shutdown 
///           (NOT RAT)
pub fn shutdown_rb(rb : u8) -> Option<TofCommandV2> {
  let code = TofCommandCode::ShutdownRB;
  let mut cmd  = TofCommandV2::new();
  cmd.command_code = code;
  cmd.payload = vec![rb];
  Some(cmd)
}

/// Send the 'sudo shutdown now' command to all RBs in a RAT
///
/// # Arguments:
///   * rat : The RAT id for the rat the RBs to be 
///           shutdown live in 
pub fn shutdown_rat(rat : u8) -> Option<TofCommandV2> {
  let code = TofCommandCode::ShutdownRAT;
  let mut cmd  = TofCommandV2::new();
  cmd.command_code = code;
  cmd.payload = Vec::<u8>::new();
  let ratmap = get_ratrbmap_hardcoded();
  match ratmap.get(&rat) {
    None => {
      error!("Don't know RBs in RAT {}", rat);
      return None
    }
    Some(pair) => {
      cmd.payload.push(pair.0);
      cmd.payload.push(pair.1);
    }
  }
  Some(cmd)
}

/// Send the 'sudo shutdown now' command to all RBs in a RAT
/// 
/// This will prepare the shutdown command for the RBs in the 
/// RATs which are connected to a specific pdu channel
///
/// # Arguments:
///   * pdu        : PDU ID (0-3)
///   * pduchannel : PDU Channel (0-7)
pub fn shutdown_ratpair(pdu : u8, pduchannel : u8) -> Option<TofCommandV2> {
  let code     = TofCommandCode::ShutdownRATPair;
  let mut cmd  = TofCommandV2::new();
  cmd.command_code = code;
  cmd.payload = Vec::<u8>::new();
  let ratmap    = get_ratrbmap_hardcoded();
  let ratpdumap = get_ratpdumap_hardcoded();
  match ratpdumap.get(&pdu) {
    None => {
      error!("Don't know that there is a RAT connected to PDU {}!", pdu);
      return None;
    }
    Some(select_pdu) => {
      match select_pdu.get(&pduchannel) {
        None => {
          error!("Don't know that there is a RAT connected to PDU {}, channel {}!", pdu, pduchannel);
          return None;
        }
        Some(rats) => {
          match ratmap.get(&rats.0) {
            Some(rbs) => {
              cmd.payload.push(rbs.0);
              cmd.payload.push(rbs.1);
            }
            None => {
              error!("RAT mapping incorrect!");
              return None;
            }
          }
          match ratmap.get(&rats.1) {
            Some(rbs) => {
              cmd.payload.push(rbs.0);
              cmd.payload.push(rbs.1);
            },
            None => {
              error!("RAT mapping incorrect!");
              return None;
            }
          }
        }
      }
    }
  }
  Some(cmd)
}

/// Send the 'sudo shutdown now' command to
/// ALL RBs
pub fn shutdown_all_rbs() -> Option<TofCommandV2> {
  Some(TofCommandV2 {
    command_code : TofCommandCode::ShutdownRB,
    payload      : Vec::<u8>::new()
  })
}

/// Send the 'sudo shutdown now command to
/// the TOF main computer ("TOFCPU")
pub fn shutdown_tofcpu() -> Option<TofCommandV2> {
  Some(TofCommandV2 {
    command_code : TofCommandCode::ShutdownCPU,
    payload      : Vec::<u8>::new()
  })
}

/// Restart the liftof-rb clients on the given boards
///
/// # Arguments
///   * rbs: restart the client on the given rb ids, 
///          if empty, restart on all of them
pub fn restart_liftofrb(rbs : &Vec<u8>) -> Option<TofCommandV2> {
  Some(TofCommandV2 {
    command_code : TofCommandCode::RestartLiftofRBClients,
    payload      : rbs.clone()
  })
}

/// Trigger the start of a new data run with 
/// the next active config
pub fn start_run() -> Option<TofCommandV2> {
  Some(TofCommandV2 {
    command_code : TofCommandCode::DataRunStart,
    payload      : Vec::<u8>::new(),
  })
}

/// Stop the current active run and idle
pub fn stop_run() -> Option<TofCommandV2> {
  Some(TofCommandV2 {
    command_code : TofCommandCode::DataRunStop,
    payload      : Vec::<u8>::new(),
  })
}

/// Run a calibration of all RBs
///
/// # Arguments:
///   * pre_run_calibration : Run the RBCalibration routine before 
///                           every run start
///   * send_packetes       : Send the RBCalibration packets
///   * save_events         : Save the events to the RBCalibration
///                           packets
pub fn rb_calibration(pre_run_calibration : bool, send_packets : bool, save_events : bool) -> Option<TofCommandV2> {
  let payload = vec![pre_run_calibration as u8, send_packets as u8, save_events as u8];
  Some(TofCommandV2 {
    command_code : TofCommandCode::RBCalibration,
    payload      : payload,
  })
}

/// Change the MTBSettings in the config file with relevant trigger settings
pub fn change_triggerconfig(cfg : &TriggerConfig) -> Option<TofCommandV2> {
  let payload = cfg.to_bytestream();
  Some(TofCommandV2 {
    command_code : TofCommandCode::SetMTConfig,
    payload      : payload,
  })
}

/// Change the EventBuilderSettings in the config file
pub fn change_tofeventbuilderconfig(cfg : &TOFEventBuilderConfig) -> Option<TofCommandV2> {
  let payload = cfg.to_bytestream();
  Some(TofCommandV2 {
    command_code : TofCommandCode::SetTOFEventBuilderConfig,
    payload      : payload,
  })
}

/// Change the data publisher config part of the config file
pub fn change_datapublisherconfig(cfg : &DataPublisherConfig) -> Option<TofCommandV2> {
  let payload = cfg.to_bytestream();
  Some(TofCommandV2 {
    command_code : TofCommandCode::SetDataPublisherConfig,
    payload      : payload,
  })
}

/// Change the data publisher config part of the config file
pub fn change_tofrunconfig(cfg : &TofRunConfig) -> Option<TofCommandV2> {
  let payload = cfg.to_bytestream();
  Some(TofCommandV2 {
    command_code : TofCommandCode::SetTofRunConfig,
    payload      : payload,
  })
}

pub fn change_tofrbconfig(cfg : &TofRBConfig) -> Option<TofCommandV2> {
  let payload = cfg.to_bytestream();
  Some(TofCommandV2 {
    command_code : TofCommandCode::SetTofRBConfig,
    payload      : payload,
  })
}