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
//
// 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::ants::*;
use crate::ffi;
use nom::{bits, bits_impl, do_parse, error_position, named, take_bits, tuple, tuple_parser};
use std::mem::transmute;

/// I<sup>2</sup>C bus which will be used for communication
///
/// *Note: Not all OBCs will have all of these buses avaialable*
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum KI2CNum {
    /// I<sup>2</sup>C Bus 1
    KI2C1,
    /// I<sup>2</sup>C Bus 2
    KI2C2,
    /// I<sup>2</sup>C Bus 3
    KI2C3,
}

/// Specific antenna to control
///
/// *Note: Not all antenna systems have four antennas*
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum KANTSAnt {
    /// Antenna 1
    Ant1,
    /// Antenna 2
    Ant2,
    /// Antenna 3
    Ant3,
    /// Antenna 4
    Ant4,
}

/// Antenna microcontroller which any commands should be run against
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum KANTSController {
    /// Primary microcontroller
    Primary,
    /// Secondary/redundant microcontroller
    Secondary,
}

/// System telemetry fields returned from [`get_system_telemetry`]
///
/// [`get_system_telemetry`]: struct.AntS.html#method.get_system_telemetry
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct AntsTelemetry {
    /// Current system temperature (raw value)
    pub raw_temp: u16,
    /// Current deployment status flags
    pub deploy_status: DeployStatus,
    /// System uptime (in seconds)
    pub uptime: u32,
}

/// Current deployment status returned from [`get_deploy`]
///
/// [`get_deploy`]: struct.AntS.html#method.get_deploy
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct DeployStatus {
    /// Whether antenna system independent burn is active
    pub sys_burn_active: bool,
    /// Whether the antenna system is ignoring the deployment switches
    pub sys_ignore_deploy: bool,
    /// Whether the antenna system is armed (ready for deployment)
    pub sys_armed: bool,
    /// Whether antenna 1 is *not* deployed
    pub ant_1_not_deployed: bool,
    /// Whether antenna 1's deployment time limit was reached before achieving full deployment
    pub ant_1_stopped_time: bool,
    /// Whether antenna 1's deployment system is currently active
    pub ant_1_active: bool,
    /// Whether antenna 2 is *not* deployed
    pub ant_2_not_deployed: bool,
    /// Whether antenna 2's deployment time limit was reached before achieving full deployment
    pub ant_2_stopped_time: bool,
    /// Whether antenna 2's deployment system is currently active
    pub ant_2_active: bool,
    /// Whether antenna 3 is *not* deployed
    pub ant_3_not_deployed: bool,
    /// Whether antenna 3's deployment time limit was reached before achieving full deployment
    pub ant_3_stopped_time: bool,
    /// Whether antenna 3's deployment system is currently active
    pub ant_3_active: bool,
    /// Whether antenna 4 is *not* deployed
    pub ant_4_not_deployed: bool,
    /// Whether antenna 4's deployment time limit was reached before achieving full deployment
    pub ant_4_stopped_time: bool,
    /// Whether antenna 4's deployment system is currently active
    pub ant_4_active: bool,
}

named!(parse_status<&[u8], DeployStatus>,
	do_parse!(
		bits: bits!(
				tuple!(
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1),
				    take_bits!(u8, 1)
				)
		) >>
	    (DeployStatus {
	         //Note: Bit 11 is unused
	         sys_burn_active: bits.3 == 1,
	         sys_ignore_deploy: bits.15 == 1,
	         sys_armed: bits.7 == 1,
	         ant_1_not_deployed: bits.8 == 1,
	         ant_1_stopped_time: bits.9 == 1,
	         ant_1_active: bits.10 == 1,
	         ant_2_not_deployed: bits.12 == 1,
	         ant_2_stopped_time: bits.13 == 1,
	         ant_2_active: bits.14 == 1,
	         ant_3_not_deployed: bits.0 == 1,
	         ant_3_stopped_time: bits.1 == 1,
	         ant_3_active: bits.2 == 1,
	         ant_4_not_deployed: bits.4 == 1,
	         ant_4_stopped_time: bits.5 == 1,
	         ant_4_active: bits.6 == 1,
	     })
	)
);

impl AntsTelemetry {
    #[doc(hidden)]
    pub fn new(c_telem: &ffi::AntsTelemetry) -> Result<AntsTelemetry, AntsError> {
        let raw_status: [u8; 2] = unsafe { transmute(c_telem.deploy_status) };

        let status = DeployStatus::new(&raw_status)?;

        let telem = AntsTelemetry {
            raw_temp: c_telem.raw_temp,
            deploy_status: status,
            uptime: c_telem.uptime,
        };
        Ok(telem)
    }
}

impl DeployStatus {
    #[doc(hidden)]
    pub fn new(input: &[u8]) -> Result<DeployStatus, AntsError> {
        match parse_status(input) {
            Ok(v) => {
                let (_input, status) = v;
                Ok(status)
            }
            _ => Err(AntsError::GenericError),
        }
    }
}

pub fn convert_bus(bus: &str) -> ffi::KI2CNum {
    match bus {
        "KI2C1" => ffi::KI2CNum::KI2C1,
        "KI2C2" => ffi::KI2CNum::KI2C2,
        "KI2C3" => ffi::KI2CNum::KI2C3,
        _ => ffi::KI2CNum::KI2CNoBus,
    }
}

pub fn convert_controller(controller: &KANTSController) -> ffi::KANTSController {
    match *controller {
        self::KANTSController::Primary => ffi::KANTSController::Primary,
        self::KANTSController::Secondary => ffi::KANTSController::Secondary,
    }
}

pub fn convert_antenna(antenna: &KANTSAnt) -> ffi::KANTSAnt {
    match *antenna {
        self::KANTSAnt::Ant1 => ffi::KANTSAnt::Ant1,
        self::KANTSAnt::Ant2 => ffi::KANTSAnt::Ant2,
        self::KANTSAnt::Ant3 => ffi::KANTSAnt::Ant3,
        self::KANTSAnt::Ant4 => ffi::KANTSAnt::Ant4,
    }
}