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
pub mod commands;
pub mod logs;
pub use self::commands::*;
pub use self::logs::*;
use bitflags::bitflags;
use byteorder::{LittleEndian, WriteBytesExt};
use nom::*;
pub const SYNC: [u8; 3] = [0xAA, 0x44, 0x12];
pub const HDR_LEN: u8 = 28;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum MessageID {
Log = 1,
Unlog = 36,
UnlogAll = 38,
Version = 37,
RxStatusEvent = 94,
BestXYZ = 241,
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,
}
}
}
#[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,
port_addr: Port::ThisPort as u8,
msg_len,
seq: 0,
idle_time: 0,
time_status: 0,
week: 0,
ms: 0,
recv_status: ReceiverStatusFlags::empty(),
recv_ver: 0,
}
}
pub fn parse(raw: &[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
}
}
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! {
#[derive(Default)]
pub struct ReceiverStatusFlags: u32 {
const ERROR_PRESENT = 0x0000_0001;
const TEMPERATURE_WARNING = 0x0000_0002;
const VOLTAGE_SUPPLY_WARNING = 0x0000_0004;
const ANTENNA_NOT_POWERED = 0x0000_0008;
const LNA_FAILURE = 0x0000_0010;
const ANTENNA_OPEN = 0x0000_0020;
const ANTENNA_SHORTENED = 0x0000_0040;
const CPU_OVERLOAD = 0x0000_0080;
const COM1_BUFFER_OVERRUN = 0x0000_0100;
const COM2_BUFFER_OVERRUN = 0x0000_0200;
const COM3_BUFFER_OVERRUN = 0x0000_0400;
const LINK_OVERRUN = 0x0000_0800;
const AUX_TRANSMIT_OVERRUN = 0x0000_2000;
const AGC_OUT_OF_RANGE = 0x0000_4000;
const INS_RESET = 0x0001_0000;
const GPS_ALMANAC_INVALID = 0x0004_0000;
const POSITION_SOLUTION_INVALID = 0x0008_0000;
const POSITION_FIXED = 0x0010_0000;
const CLOCK_STEERING_DISABLED = 0x0020_0000;
const CLOCK_MODEL_INVALID = 0x0040_0000;
const EXTERNAL_OSCILLATOR_LOCKED = 0x0080_0000;
const SOFTWARE_RESOURCE_WARNING = 0x0100_0000;
const AUX3_STATUS_EVENT = 0x2000_0000;
const AUX2_STATUS_EVENT = 0x4000_0000;
const AUX1_STATUS_EVENT = 0x8000_0000;
}
}
impl ReceiverStatusFlags {
pub fn to_vec(self) -> Vec<String> {
format!("{:?}", self)
.to_owned()
.split(" | ")
.map(|x| x.to_string())
.collect()
}
}