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
use crate::models::*;
use clyde_3g_eps_api::{Checksum, Clyde3gEps, Eps};
use eps_api::EpsResult;
use failure::Error;
use rust_i2c::*;
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::Duration;
#[derive(Copy, Clone, Debug, Eq, Hash, GraphQLEnum, PartialEq)]
pub enum Mutations {
None,
Noop,
ManualReset,
RawCommand,
ResetWatchdog,
SetWatchdogPeriod,
TestHardware,
}
fn watchdog_thread(eps: Arc<Mutex<Box<dyn Clyde3gEps + Send>>>) {
loop {
thread::sleep(Duration::from_secs(60));
let _res_ = eps.lock().unwrap().reset_comms_watchdog();
}
}
#[derive(Clone)]
pub struct Subsystem {
pub eps: Arc<Mutex<Box<dyn Clyde3gEps + Send>>>,
pub last_mutation: Arc<RwLock<Mutations>>,
pub errors: Arc<RwLock<Vec<String>>>,
pub watchdog_handle: Arc<Mutex<thread::JoinHandle<()>>>,
pub checksum: Arc<Mutex<Checksum>>,
}
impl Subsystem {
pub fn new(eps: Box<dyn Clyde3gEps + Send>) -> EpsResult<Self> {
let eps = Arc::new(Mutex::new(eps));
let thread_eps = eps.clone();
let watchdog = thread::spawn(move || watchdog_thread(thread_eps));
Ok(Self {
eps,
last_mutation: Arc::new(RwLock::new(Mutations::None)),
errors: Arc::new(RwLock::new(vec![])),
watchdog_handle: Arc::new(Mutex::new(watchdog)),
checksum: Arc::new(Mutex::new(Checksum::default())),
})
}
pub fn from_path(bus: &str) -> EpsResult<Self> {
let clyde_eps: Box<dyn Clyde3gEps + Send> =
Box::new(Eps::new(Connection::from_path(bus, 0x2B)));
Subsystem::new(clyde_eps)
}
pub fn get_motherboard_telemetry(
&self,
telem_type: motherboard_telemetry::Type,
) -> Result<f64, String> {
let result = run!(
self.eps
.lock()
.unwrap()
.get_motherboard_telemetry(telem_type.into()),
self.errors
)?;
Ok(result)
}
pub fn get_daughterboard_telemetry(
&self,
telem_type: daughterboard_telemetry::Type,
) -> Result<f64, String> {
let eps = self.eps.lock().unwrap();
Ok(run!(
eps.get_daughterboard_telemetry(telem_type.into()),
self.errors
)?)
}
pub fn get_reset_telemetry(
&self,
telem_type: reset_telemetry::Type,
) -> Result<reset_telemetry::Data, String> {
let eps = self.eps.lock().unwrap();
Ok(run!(eps.get_reset_telemetry(telem_type.into()), self.errors)?.into())
}
pub fn get_comms_watchdog_period(&self) -> Result<u8, String> {
let eps = self.eps.lock().unwrap();
Ok(run!(eps.get_comms_watchdog_period(), self.errors)?)
}
pub fn get_version(&self) -> Result<version::VersionData, String> {
let eps = self.eps.lock().unwrap();
Ok(run!(eps.get_version_info(), self.errors)?.into())
}
pub fn get_board_status(&self) -> Result<board_status::BoardData, String> {
let eps = self.eps.lock().unwrap();
Ok(run!(eps.get_board_status(), self.errors)?.into())
}
pub fn get_last_eps_error(&self) -> Result<last_error::ErrorData, String> {
let eps = self.eps.lock().unwrap();
Ok(run!(eps.get_last_error(), self.errors)?.into())
}
pub fn get_power(&self) -> Result<GetPowerResponse, String> {
let eps = self.eps.lock().unwrap();
if let Ok(data) = eps.get_version_info() {
let daughterboard = if data.daughterboard.is_some() {
PowerState::On
} else {
PowerState::Off
};
Ok(GetPowerResponse {
motherboard: PowerState::On,
daughterboard,
})
} else {
Ok(GetPowerResponse {
motherboard: PowerState::Off,
daughterboard: PowerState::Off,
})
}
}
pub fn manual_reset(&self) -> Result<MutationResponse, String> {
let eps = self.eps.lock().unwrap();
match run!(eps.manual_reset(), self.errors) {
Ok(_v) => Ok(MutationResponse {
success: true,
errors: "".to_string(),
}),
Err(e) => Ok(MutationResponse {
success: false,
errors: e,
}),
}
}
pub fn reset_watchdog(&self) -> Result<MutationResponse, String> {
let eps = self.eps.lock().unwrap();
match run!(eps.reset_comms_watchdog(), self.errors) {
Ok(_v) => Ok(MutationResponse {
success: true,
errors: "".to_string(),
}),
Err(e) => Ok(MutationResponse {
success: false,
errors: e,
}),
}
}
pub fn set_watchdog_period(&self, period: u8) -> Result<MutationResponse, String> {
let eps = self.eps.lock().unwrap();
match run!(eps.set_comms_watchdog_period(period), self.errors) {
Ok(_v) => Ok(MutationResponse {
success: true,
errors: "".to_string(),
}),
Err(e) => Ok(MutationResponse {
success: false,
errors: e,
}),
}
}
pub fn raw_command(&self, command: u8, data: Vec<u8>) -> Result<MutationResponse, String> {
let eps = self.eps.lock().unwrap();
match run!(eps.raw_command(command, data), self.errors) {
Ok(_v) => Ok(MutationResponse {
success: true,
errors: "".to_string(),
}),
Err(e) => Ok(MutationResponse {
success: false,
errors: e,
}),
}
}
pub fn test_hardware(&self) -> Result<MutationResponse, String> {
let eps = self.eps.lock().unwrap();
match run!(eps.get_checksum(), self.errors) {
Ok(new_data) => {
let mut errors = vec![];
let mut success = true;
let mut old_data = self.checksum.lock().unwrap();
if old_data.motherboard != 0 {
if old_data.motherboard != new_data.motherboard {
success = false;
errors.push(format!(
"Motherboard checksum changed: {} -> {}",
old_data.motherboard, new_data.motherboard
));
}
if old_data.daughterboard != new_data.daughterboard {
success = false;
errors.push(format!(
"Daughterboard checksum changed: {:?} -> {:?}",
old_data.daughterboard, new_data.daughterboard
));
}
}
old_data.motherboard = new_data.motherboard;
old_data.daughterboard = new_data.daughterboard;
eprintln!(
"EPS checksums: {}, {:?}",
old_data.motherboard, old_data.daughterboard
);
Ok(MutationResponse {
success,
errors: errors.join(". "),
})
}
Err(e) => Ok(MutationResponse {
success: false,
errors: e,
}),
}
}
pub fn set_last_mutation(&self, mutation: Mutations) {
if let Ok(mut last_cmd) = self.last_mutation.write() {
*last_cmd = mutation;
}
}
pub fn get_errors(&self) -> EpsResult<Vec<String>> {
match self.errors.write() {
Ok(mut master_vec) => {
let current = master_vec.clone();
master_vec.clear();
master_vec.shrink_to_fit();
Ok(current)
}
_ => Ok(vec![
"Error: Failed to borrow master errors vector".to_string()
]),
}
}
}