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
//
// 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.
//

pub mod commands;
pub mod logs;

pub use self::commands::*;
pub use self::logs::*;
use byteorder::{LittleEndian, WriteBytesExt};
use nom::*;

pub const SYNC: [u8; 3] = [0xAA, 0x44, 0x12];
pub const HDR_LEN: u8 = 28;

/// Supported message types
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum MessageID {
    /// Log command message
    Log = 1,
    /// Unlog command message
    Unlog = 36,
    /// Unlog all command message
    UnlogAll = 38,
    /// Version data log
    Version = 37,
    /// RX status event data log
    RxStatusEvent = 94,
    /// Best XYZ position/velocity data log
    BestXYZ = 241,
    /// Catch-all value for received messages with an unknown ID
    Unknown,
}

impl Default for MessageID {
    fn default() -> MessageID {
        MessageID::Unknown
    }
}

impl From<u16> for MessageID {
    fn from(t: u16) -> MessageID {
        match t {
            1 => MessageID::Log,
            36 => MessageID::Unlog,
            37 => MessageID::Version,
            38 => MessageID::UnlogAll,
            94 => MessageID::RxStatusEvent,
            241 => MessageID::BestXYZ,
            _ => MessageID::Unknown,
        }
    }
}

/// Common header structure for all messages
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Header {
    pub sync: [u8; 3],
    pub hdr_len: u8,
    pub msg_id: MessageID,
    pub msg_type: u8,
    pub port_addr: u8,
    pub msg_len: u16,
    pub seq: u16,
    pub idle_time: u8,
    pub time_status: u8,
    pub week: u16,
    pub ms: i32,
    pub recv_status: ReceiverStatusFlags,
    pub recv_ver: u16,
}

impl Header {
    fn new(msg_id: MessageID, msg_len: u16) -> Self {
        Header {
            sync: SYNC,
            hdr_len: HDR_LEN,
            msg_id,
            msg_type: 0, // Measurement source = Primary antenna, Format = Binary, Response bit = Original message.
            port_addr: Port::ThisPort as u8,
            msg_len,
            seq: 0,         // Always zero. We're only sending the one message
            idle_time: 0,   // Ignored for TX
            time_status: 0, // Ignored for TX
            week: 0,        // Ignored for TX
            ms: 0,          // Ignored for TX
            recv_status: ReceiverStatusFlags::empty(), // Ignored for TX
            recv_ver: 0,    // Ignored for TX
        }
    }

    pub fn parse(raw: &Vec<u8>) -> Option<Self> {
        match parse_header(raw) {
            Ok(conv) => Some(conv.1),
            _ => None,
        }
    }
}

impl Message for Header {
    fn serialize(&self) -> Vec<u8> {
        let mut vec = SYNC.to_vec();

        vec.push(self.hdr_len);
        vec.write_u16::<LittleEndian>(self.msg_id as u16).unwrap();
        vec.push(self.msg_type);
        vec.push(self.port_addr);
        vec.write_u16::<LittleEndian>(self.msg_len).unwrap();
        vec.write_u16::<LittleEndian>(self.seq).unwrap();
        vec.push(self.idle_time);
        vec.push(self.time_status);
        vec.write_u16::<LittleEndian>(self.week).unwrap();
        vec.write_i32::<LittleEndian>(self.ms).unwrap();
        vec.write_u32::<LittleEndian>(self.recv_status.bits())
            .unwrap();
        vec.push(0);
        vec.push(0);
        vec.write_u16::<LittleEndian>(self.recv_ver).unwrap();

        vec
    }
}

/// Device communication ports
pub enum Port {
    COM1 = 32,
    ThisPort = 192,
}

named!(parse_header(&[u8]) -> Header,
    do_parse!(
        sync1: le_u8 >>
        sync2: le_u8 >>
        sync3: le_u8 >>
        hdr_len: le_u8 >>
        msg_id: le_u16 >>
        msg_type: le_u8 >>
        port_addr: le_u8 >>
        msg_len: le_u16 >>
        seq: le_u16 >>
        idle_time: le_u8 >>
        time_status: le_u8 >>
        week: le_u16 >>
        ms: le_i32 >>
        recv_status: le_u32 >>
        le_u16 >>
        recv_ver: le_u16 >>
        (Header {
                sync: [sync1, sync2, sync3],
                hdr_len,
                msg_id: msg_id.into(),
                msg_type,
                port_addr,
                msg_len,
                seq,
                idle_time,
                time_status,
                week,
                ms,
                recv_status: ReceiverStatusFlags::from_bits_truncate(recv_status),
                recv_ver
        })
    )
);

bitflags! {
    /// Receiver status flags
    #[derive(Default)]
    pub struct ReceiverStatusFlags: u32 {
        /// System has encountered an error
        const ERROR_PRESENT = 0x00000001;
        /// Temperature warning
        const TEMPERATURE_WARNING = 0x00000002;
        /// Voltage supply warning
        const VOLTAGE_SUPPLY_WARNING = 0x00000004;
        /// Antenna not powered
        const ANTENNA_NOT_POWERED = 0x00000008;
        /// LNA failure encountered
        const LNA_FAILURE = 0x00000010;
        /// Antenna open
        const ANTENNA_OPEN = 0x00000020;
        /// Antenna shortened
        const ANTENNA_SHORTENED = 0x00000040;
        /// CPU overloaded
        const CPU_OVERLOAD = 0x00000080;
        /// The COM1 buffer has been overrun
        const COM1_BUFFER_OVERRUN = 0x00000100;
        /// The COM2 buffer has been overrun
        const COM2_BUFFER_OVERRUN = 0x00000200;
        /// The COM3 buffer has been overrun
        const COM3_BUFFER_OVERRUN = 0x00000400;
        /// Link overrun
        const LINK_OVERRUN = 0x00000800;
        /// Auxilliary transmit overrun
        const AUX_TRANSMIT_OVERRUN = 0x00002000;
        /// AGC out of range
        const AGC_OUT_OF_RANGE = 0x00004000;
        /// INS has been reset
        const INS_RESET = 0x00010000;
        /// GPS almanac invalid and/or UTC unknown
        const GPS_ALMANAC_INVALID = 0x00040000;
        /// Position solution is invalid
        const POSITION_SOLUTION_INVALID = 0x00080000;
        /// A fixed position is in place
        const POSITION_FIXED = 0x00100000;
        /// Clock steering is disabled
        const CLOCK_STEERING_DISABLED = 0x00200000;
        /// The clock model is invalid
        const CLOCK_MODEL_INVALID = 0x00400000;
        /// The external oscillator is locked
        const EXTERNAL_OSCILLATOR_LOCKED = 0x00800000;
        /// Software resource warning
        const SOFTWARE_RESOURCE_WARNING = 0x01000000;
        /// An auxilliary 3 status event has occurred
        const AUX3_STATUS_EVENT = 0x20000000;
        /// An auxilliary 2 status event has occurred
        const AUX2_STATUS_EVENT = 0x40000000;
        /// An auxilliary 1 status event has occurred
        const AUX1_STATUS_EVENT = 0x80000000;
    }
}

impl ReceiverStatusFlags {
    /// Convert the flags byte into a vector containing the string representations
    /// of all flags present.
    ///
    /// # Examples
    ///
    /// ```
    /// use novatel_oem6_api::*;
    ///
    /// # fn func() -> OEMResult<()> {
    /// let flags = ReceiverStatusFlags::CLOCK_MODEL_INVALID |
    ///     ReceiverStatusFlags::POSITION_SOLUTION_INVALID;
    ///
    /// let conv = flags.to_vec();
    ///
    /// assert_eq!(conv, vec!["CLOCK_MODEL_INVALID", "POSITION_SOLUTION_INVALID"]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    pub fn to_vec(&self) -> Vec<String> {
        format!("{:?}", self)
            .to_owned()
            .split(" | ")
            .map(|x| x.to_string())
            .collect()
    }
}