Files
adcs_api
cbor_protocol
channel_protocol
clyde_3g_eps_api
clyde_3g_eps_service
comms_service
db_test
eps_api
example_rust_c_service
example_rust_service
extern_lib
file_protocol
file_service
iobc_supervisor_service
isis_ants
isis_ants_api
isis_ants_service
isis_imtq_api
isis_iobc_supervisor
kubos_app
kubos_app_service
kubos_build_helper
kubos_file_client
kubos_service
kubos_shell_client
kubos_system
kubos_telemetry_db
large_download
large_upload
local_comms_service
mai400
mai400_api
mai400_service
monitor_service
novatel_oem6_api
novatel_oem6_service
nsl_duplex_d2
nsl_duplex_d2_comms_service
obc_hs
radio_api
rust_i2c
rust_mission_app
rust_uart
scheduler_service
serial_comms_service
shell_protocol
shell_service
telemetry_service
uart_comms_client
udp_client
utils
  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
//
// 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, do_parse, named, take_bits, tuple};
use std::mem::transmute;

/// 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_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,
    }
}