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
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
//
// 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 std::mem;

mod ffi;

/// Structure returned by supervisor_version
#[derive(Debug)]
pub struct SupervisorVersion {
    pub dummy: u8,
    pub spi_command_status: u8,
    pub index_of_subsystem: u8,
    pub major_version: u8,
    pub minor_version: u8,
    pub patch_version: u8,
    pub git_head_version: u32,
    pub serial_number: u16,
    pub compile_information: [i8; ffi::LENGTH_COMPILE_INFORMATION],
    pub clock_speed: u8,
    pub code_type: i8,
    pub crc: u8,
}

/// Structure for individual enable statuses
/// Used in SupervisorHousekeeping
#[derive(Clone, Copy, Debug)]
pub struct SupervisorEnableStatus {
    pub power_obc: u8,
    pub power_rtc: u8,
    pub supervisor_mode: u8,
    pub busy_rtc: u8,
    pub power_off_rtc: u8,
}

/// Structure returned by supervisor_housekeeping
#[derive(Debug)]
pub struct SupervisorHousekeeping {
    pub dummy: u8,
    pub spi_command_status: u8,
    pub enable_status: SupervisorEnableStatus,
    pub supervisor_uptime: u32,
    pub iobc_uptime: u32,
    pub iobc_reset_count: u32,
    pub adc_data: [u16; ffi::SUPERVISOR_NUMBER_OF_ADC_CHANNELS],
    pub adc_update_flag: u8,
    pub crc8: u8,
}

/// Supervisor emergency reset interface
pub fn supervisor_emergency_reset() -> Result<(), String> {
    if unsafe { ffi::supervisor_emergency_reset() } {
        Ok(())
    } else {
        Err(String::from("Problem with supervisor emergency reset"))
    }
}

/// Supervisor reset interface
pub fn supervisor_reset() -> Result<(), String> {
    if unsafe { ffi::supervisor_reset() } {
        Ok(())
    } else {
        Err(String::from("Problem with supervisor reset"))
    }
}

/// Supervisor powercycle interface
pub fn supervisor_powercycle() -> Result<(), String> {
    if unsafe { ffi::supervisor_powercycle() } {
        Ok(())
    } else {
        Err(String::from("Problem with supervisor powercycle"))
    }
}

/// Converts raw bytes from iOBC into SupervisorVersion
fn convert_raw_version(raw: &ffi::supervisor_version) -> SupervisorVersion {
    SupervisorVersion {
        dummy: raw.0[0] as u8,
        spi_command_status: raw.0[1] as u8,
        index_of_subsystem: raw.0[2] as u8,
        major_version: raw.0[3] as u8,
        minor_version: raw.0[4] as u8,
        patch_version: raw.0[5] as u8,
        git_head_version: {
            u32::from(raw.0[6])
                | u32::from(raw.0[7]) << 8
                | u32::from(raw.0[8]) << 16
                | u32::from(raw.0[9]) << 24
        },
        serial_number: { u16::from(raw.0[10]) | u16::from(raw.0[11]) << 8 },
        compile_information: {
            let mut a = [0; ffi::LENGTH_COMPILE_INFORMATION];
            for (i, element) in a.iter_mut().enumerate() {
                // 12 is the offset to locate compile_information
                // in the version data
                *element = raw.0[i + 12] as i8;
            }
            a
        },
        clock_speed: raw.0[31] as u8,
        code_type: raw.0[32] as i8,
        crc: raw.0[33] as u8,
    }
}

/// Interface for retrieving iOBC supervisor version data
pub fn supervisor_version() -> Result<SupervisorVersion, String> {
    let mut version: ffi::supervisor_version =
        unsafe { mem::MaybeUninit::<ffi::supervisor_version>::uninit().assume_init() };
    let version_result = unsafe { ffi::supervisor_get_version(&mut version) };
    if !version_result {
        Err(String::from("Problem retrieving supervisor version"))
    } else {
        Ok(convert_raw_version(&version))
    }
}

/// Converts raw bytes from iOBC into SupervisorHousekeeping
fn convert_raw_housekeeping(raw: &ffi::supervisor_housekeeping) -> SupervisorHousekeeping {
    SupervisorHousekeeping {
        dummy: raw.0[0] as u8,
        spi_command_status: raw.0[1] as u8,
        enable_status: SupervisorEnableStatus {
            // We bitmask rather than split the int
            // across bitfields
            power_obc: (raw.0[2] as u8) & 0x1,
            power_rtc: ((raw.0[2] as u8) & 0x2) >> 1,
            supervisor_mode: ((raw.0[2] as u8) & 0x4) >> 2,
            busy_rtc: ((raw.0[2] as u8) & 0x20) >> 5,
            power_off_rtc: ((raw.0[2] as u8) & 0x40) >> 6,
        },
        supervisor_uptime: {
            u32::from(raw.0[3])
                | u32::from(raw.0[4]) << 8
                | u32::from(raw.0[5]) << 16
                | u32::from(raw.0[6]) << 24
        },
        iobc_uptime: {
            u32::from(raw.0[7])
                | u32::from(raw.0[8]) << 8
                | u32::from(raw.0[9]) << 16
                | u32::from(raw.0[10]) << 24
        },
        iobc_reset_count: {
            u32::from(raw.0[11])
                | u32::from(raw.0[12]) << 8
                | u32::from(raw.0[13]) << 16
                | u32::from(raw.0[14]) << 24
        },
        adc_data: {
            // combining bytes into 16-bit uints
            let mut a = [0; ffi::SUPERVISOR_NUMBER_OF_ADC_CHANNELS];
            for (i, element) in a.iter_mut().enumerate() {
                // 15 is the offset to locate the adc data
                // in the housekeeping data
                *element = u16::from(raw.0[15 + 2 * i]) | u16::from(raw.0[15 + 2 * i + 1]) << 8;
            }
            a
        },
        adc_update_flag: raw.0[35] as u8,
        crc8: raw.0[36] as u8,
    }
}

/// Interface for fetching iOBC supervisor housekeeping data
pub fn supervisor_housekeeping() -> Result<SupervisorHousekeeping, String> {
    let mut raw: ffi::supervisor_housekeeping =
        unsafe { mem::MaybeUninit::<ffi::supervisor_housekeeping>::uninit().assume_init() };
    let result = unsafe { ffi::supervisor_get_housekeeping(&mut raw) };

    if !result {
        Err(String::from("Problem retrieving supervisor housekeeping"))
    } else {
        Ok(convert_raw_housekeeping(&raw))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Accuracy testing conversion of raw bytes into non-C
    /// SupervisorVersion structure
    #[test]
    fn test_convert_version() {
        let raw: ffi::supervisor_version = ffi::supervisor_version([
            0, // dummy (u8)
            1, // spi_command_status (u8)
            2, // index_of_subsystem (u8)
            3, // major_version (u8)
            4, // minor version (u8)
            5, // patch version (u8)
            6, 7, 8, 9, // git_head_version (u32)
            10, 11, // serial_number (u16)
            // compile_information (i8 * 19)
            12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
            31, // clock_speed (u8)
            32, // code_type (i8)
            33, // crc8 (u8)
        ]);

        let version = convert_raw_version(&raw);

        assert_eq!(version.dummy, 0);
        assert_eq!(version.spi_command_status, 1);
        assert_eq!(version.major_version, 3);
        assert_eq!(version.minor_version, 4);
        assert_eq!(version.patch_version, 5);
        assert_eq!(version.git_head_version, 151_521_030);
        assert_eq!(version.serial_number, 2826);
        for i in 12..31 {
            assert_eq!(version.compile_information[i - 12], i as i8);
        }
        assert_eq!(version.clock_speed, 31);
        assert_eq!(version.code_type, 32);
        assert_eq!(version.crc, 33);
    }

    /// Accuracy testing conversion of raw bytes into non-C
    /// SupervisorHousekeeping structure
    #[test]
    fn test_convert_housekeeping() {
        let raw: ffi::supervisor_housekeeping = ffi::supervisor_housekeeping([
            0,  // dummy (u8)
            1,  // spi_command_status (u8)
            34, // enable_status (u8) is a bitfield in the C structure
            // power_obc : 1
            // power_rtc : 1
            // supervisor_mode : 1
            // padding : 2
            // busy_rtc : 1
            // power_off_rtc : 1
            // padding: 1
            // Using 34 gives us -
            // 0 0 1 0 0 0 1 0
            // which results in alternating 1/0 field values
            3, 2, 1, 0, // super_uptime (u32)
            4, 3, 2, 1, // iobc_uptime (u32)
            5, 4, 3, 2, // iobc_reset_count (u32)
            // adc_data (u16 * 10)
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            12, // adc_update_flag (u8)
            13, // crc8
        ]);

        let housekeeping = convert_raw_housekeeping(&raw);

        assert_eq!(housekeeping.dummy, 0);
        assert_eq!(housekeeping.spi_command_status, 1);
        assert_eq!(housekeeping.enable_status.power_obc, 0);
        assert_eq!(housekeeping.enable_status.power_rtc, 1);
        assert_eq!(housekeeping.enable_status.supervisor_mode, 0);
        assert_eq!(housekeeping.enable_status.busy_rtc, 1);
        assert_eq!(housekeeping.enable_status.power_off_rtc, 0);
        assert_eq!(housekeeping.supervisor_uptime, 66051);
        assert_eq!(housekeeping.iobc_uptime, 16_909_060);
        assert_eq!(housekeeping.iobc_reset_count, 33_752_069);
        assert_eq!(housekeeping.adc_data[0], 256);
        assert_eq!(housekeeping.adc_data[1], 770);
        assert_eq!(housekeeping.adc_data[2], 1284);
        assert_eq!(housekeeping.adc_data[3], 1798);
        assert_eq!(housekeeping.adc_data[4], 2312);
        assert_eq!(housekeeping.adc_data[5], 2826);
        assert_eq!(housekeeping.adc_data[6], 3340);
        assert_eq!(housekeeping.adc_data[7], 3854);
        assert_eq!(housekeeping.adc_data[8], 4368);
        assert_eq!(housekeeping.adc_data[9], 4882);
        assert_eq!(housekeeping.adc_update_flag, 12);
        assert_eq!(housekeeping.crc8, 13);
    }
}