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
/*
 * Copyright (C) 2018 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.
 */

use crate::commands::*;
use crate::telemetry;
use eps_api::EpsResult;
use rust_i2c::Connection;
use std::time::Duration;

/// EPS structure containing low level connection and functionality
/// required for commanding and requesting telemetry from EPS device.
pub struct Eps {
    connection: Connection,
}

impl Eps {
    /// Constructor
    ///
    /// Creates new instance of Eps structure.
    ///
    /// # Arguments
    /// `connection` - A [`Connection`] used as low-level connection to EPS hardware
    ///
    /// [`Connection`]: ../rust_i2c/struct.Connection.html
    pub fn new(connection: Connection) -> Self {
        Eps { connection }
    }

    /// Get Board Status
    ///
    /// The status bytes are designed to supply operational data about the I2C Node.
    pub fn get_board_status(&self) -> EpsResult<board_status::BoardStatus> {
        board_status::parse(
            &self
                .connection
                .transfer(board_status::command(), Duration::from_millis(2))?,
        )
    }

    /// Get Checksum
    ///
    /// This command instructs the node to self-inspect its ROM contents in order
    /// to generate a checksum. The value retrieved can be used to determine whether
    /// the contents of the ROM have changed during the operation of the device.
    pub fn get_checksum(&self) -> EpsResult<checksum::Checksum> {
        checksum::parse(
            &self
                .connection
                .transfer(checksum::command(), Duration::from_millis(50))?,
        )
    }

    /// Get Version
    ///
    /// The version number of the firmware will be returned on this command.
    /// The revision number returns the current revision of the firmware that is
    /// present on the board. The firmware number returns the current firmware on the board.
    pub fn get_version_info(&self) -> EpsResult<version::VersionInfo> {
        version::parse(
            &self
                .connection
                .transfer(version::command(), Duration::from_millis(2))?,
        )
    }

    /// Get Last Error
    ///
    /// If an error has been generated after attempting to execute a user's command,
    /// this command can be used to retrieve details about the error.
    pub fn get_last_error(&self) -> EpsResult<last_error::LastError> {
        last_error::parse(
            &self
                .connection
                .transfer(last_error::command(), Duration::from_millis(2))?,
        )
    }

    /// Manual Reset
    ///
    /// If required the user can reset the TTC node using this command. When issued,
    /// the board will reset within 1 second. This command will result in the board
    /// being brought up in its defined initial condition. Resetting the board in
    /// this fashion will increment the Manual Reset Counter.
    pub fn manual_reset(&self) -> EpsResult<()> {
        self.connection.write(manual_reset::command())?;
        Ok(())
    }

    /// Reset Communications Watchdog
    ///
    /// Any valid command will reset the communications watchdog timer. If the user
    /// does not require any telemetry from the board, this command can be sent
    /// to reset the communications watchdog.
    pub fn reset_comms_watchdog(&self) -> EpsResult<()> {
        self.connection.write(reset_comms_watchdog::command())?;
        Ok(())
    }

    /// Get Motherboard Telemetry
    ///
    /// This command is used to request telemetry items from the motherboard's
    /// telemetry node.
    ///
    /// # Arguments
    /// `telem_type` - Variant of [`MotherboardTelemetry::Type`] to request
    ///
    /// [`MotherboardTelemetry::Type`]: ./MotherboardTelemetry/enum.Type.html
    pub fn get_motherboard_telemetry(
        &self,
        telem_type: telemetry::motherboard::Type,
    ) -> EpsResult<f64> {
        telemetry::motherboard::parse(
            &self.connection.transfer(
                telemetry::motherboard::command(telem_type),
                Duration::from_millis(20),
            )?,
            telem_type,
        )
    }

    /// Get Daughterboard Telemetry
    ///
    /// This command is used to request telemetry items from the daughterboard's
    /// telemetry node.
    ///
    /// # Arguments
    /// `telem_type` - Variant of [`DaughterboardTelemetry::Type`] to request
    ///
    /// [`DaughterboardTelemetry::Type`]: ./DaughterboardTelemetry/enum.Type.html
    pub fn get_daughterboard_telemetry(
        &self,
        telem_type: telemetry::daughterboard::Type,
    ) -> EpsResult<f64> {
        telemetry::daughterboard::parse(
            &self.connection.transfer(
                telemetry::daughterboard::command(telem_type),
                Duration::from_millis(20),
            )?,
            telem_type,
        )
    }

    /// Get Reset Telemetry
    ///
    /// This command is used to request telemetry items regarding various
    /// reset conditions on both the motherboard and daughterboard.
    ///
    /// # Arguments
    /// `telem_type` - Variant of [`ResetTelemetry::Type`] to request
    ///
    /// [`ResetTelemetry::Type`]: ./ResetTelemetry/enum.Type.html
    pub fn get_reset_telemetry(
        &self,
        telem_type: telemetry::reset::Type,
    ) -> EpsResult<telemetry::reset::Data> {
        telemetry::reset::parse(&self.connection.transfer(
            telemetry::reset::command(telem_type),
            Duration::from_millis(20),
        )?)
    }

    /// Set Communications Watchdog Period
    ///
    /// The Communications Watchdog by default has a value of 4 minutes set as
    /// its timeout period. If 4 minutes pass without a command being received
    /// then the device will reboot into its pre-defined initial state. This
    /// value of 4 minutes can be changed using the Set Communications Watchdog
    /// Period command, 0x21. The data byte specifies the number of minutes the
    /// communications watchdog will wait before timing out.
    ///
    /// # Arguments
    /// `period` - Watchdog period to set in minutes
    pub fn set_comms_watchdog_period(&self, period: u8) -> EpsResult<()> {
        self.connection
            .write(set_comms_watchdog_period::command(period))?;
        Ok(())
    }

    /// Get Communications Watchdog Period
    ///
    /// This command provides the user with the current communications watchdog
    /// timeout that has been set. The returned value is indicated in minutes.
    pub fn get_comms_watchdog_period(&self) -> EpsResult<u8> {
        get_comms_watchdog_period::parse(&self.connection.transfer(
            get_comms_watchdog_period::command(),
            Duration::from_millis(2),
        )?)
    }
}