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
//
// Copyright (C) 2019 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

//! Main module used for interacting with the underlying EPS API

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;

/// Enum for tracking the last mutation executed
#[derive(Copy, Clone, Debug, Eq, Hash, GraphQLEnum, PartialEq)]
pub enum Mutations {
    /// No mutation has been run since the service was started
    None,
    /// No-op
    Noop,
    /// Manual reset
    ManualReset,
    /// Raw passthrough command
    RawCommand,
    /// Watchdog reset
    ResetWatchdog,
    /// Set watchdog period
    SetWatchdogPeriod,
    /// Hardware test
    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();
    }
}

/// Main structure for controlling and accessing system resources
#[derive(Clone)]
pub struct Subsystem {
    /// Underlying EPS object
    pub eps: Arc<Mutex<Box<dyn Clyde3gEps + Send>>>,
    /// Last mutation executed
    pub last_mutation: Arc<RwLock<Mutations>>,
    /// Errors accumulated over all queries and mutations
    pub errors: Arc<RwLock<Vec<String>>>,
    /// Watchdog kicking thread handle
    pub watchdog_handle: Arc<Mutex<thread::JoinHandle<()>>>,
    /// Last known checksum of EPS ROM
    pub checksum: Arc<Mutex<Checksum>>,
}

impl Subsystem {
    /// Create a new subsystem instance for the service to use
    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())),
        })
    }

    /// Create the underlying EPS object and then create a new subsystem which will use it
    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)
    }

    /// Get the requested telemetry item from the motherboard
    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)
    }

    /// Get the requested telemetry item from the daughterboard
    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
        )?)
    }

    /// Get the specific type of reset counts
    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())
    }

    /// Get the current watchdog period setting
    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)?)
    }

    /// Get the system version information
    pub fn get_version(&self) -> Result<version::VersionData, String> {
        let eps = self.eps.lock().unwrap();
        Ok(run!(eps.get_version_info(), self.errors)?.into())
    }

    /// Get the current board status
    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())
    }

    /// Get the last error the EPS encountered
    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())
    }

    /// Get the current power state of the EPS
    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,
            })
        }
    }

    /// Trigger a manual reset of the EPS
    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,
            }),
        }
    }

    /// Kick the I2C watchdog
    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,
            }),
        }
    }

    /// Set the I2C watchdog timeout period
    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,
            }),
        }
    }

    /// Pass raw command values through to the EPS
    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,
            }),
        }
    }

    /// Run hardware tests to check system health
    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();

                // Compare the new checksum values to the previously fetched ones
                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
                        ));
                    }
                }

                // Update the stored values
                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,
            }),
        }
    }

    /// Record the last mutation executed by the service
    pub fn set_last_mutation(&self, mutation: Mutations) {
        if let Ok(mut last_cmd) = self.last_mutation.write() {
            *last_cmd = mutation;
        }
    }

    /// Fetch all errors since the last time this function was called, then clear the errors storage
    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()
            ]),
        }
    }
}