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
#[cfg(feature = "log")]
use log::debug;
use crate::gps::{GpsError, GpsQzssFrame, State, GPS_MIN_SIZE, GPS_PREAMBLE_MASK};
impl GpsQzssFrame {
/// Encodes this [GpsQzssFrame] into preallocated buffer in which
/// the frame must fit completely. We consider the minimal
/// allocation should be [GPS_MIN_SIZE].
///
/// GPS is MSB first,
/// least significant bit first inside each bytes,
/// and uses 30 bit data words, NB: the output is not aligned
/// to 32 bits.
///
/// ## Input
/// - buf: preallocated buffer.
/// - size: available size in buffer. If this value
/// is not up to date, the program may panic.
/// If size available is greater than [GPS_MIN_SIZE], this will never happen.
///
/// ## Output
/// - Ok(size) : encoded size, when everything went fine
pub fn encode(&self, buf: &mut [u8], size: usize) -> Result<usize, GpsError> {
if size < GPS_MIN_SIZE {
return Err(GpsError::WouldNotFit);
}
let mut ptr = 0;
let mut binoffset = 0;
let mut state = State::default();
let mut prev_state = state;
loop {
if ptr == size {
// would panic
unreachable!("buffer to small");
}
#[cfg(feature = "log")]
debug!("state={:?} | ptr={}", state, ptr);
}
Ok(size)
}
}
#[cfg(test)]
mod test {
use crate::{
gps::{
GpsQzssFrame, GpsQzssFrameId, GpsQzssHow, GpsQzssSubframe, GpsQzssTelemetry,
GPS_MIN_SIZE,
},
GpsQzssDecoder, GPS_PREAMBLE_MASK,
};
#[cfg(feature = "log")]
use crate::tests::init_logger;
#[cfg(feature = "log")]
use log::info;
#[test]
fn gps_qzss_frame1_encoding() {
#[cfg(feature = "log")]
init_logger();
let frame = GpsQzssFrame {
how: crate::GpsQzssHow {
tow: 1,
alert: true,
anti_spoofing: false,
frame_id: GpsQzssFrameId::Ephemeris1,
},
telemetry: GpsQzssTelemetry {
message: 10,
integrity: true,
reserved_bits: false,
},
subframe: GpsQzssSubframe::Eph1(crate::GpsQzssFrame1 {
week: 11,
ca_or_p_l2: 4,
ura: 5,
health: 6,
iodc: 7,
toc: 8,
tgd: 9.0,
af2: 10.0,
af1: 11.0,
af0: 12.0,
reserved_word4: 13,
l2_p_data_flag: false,
reserved_word5: 14,
reserved_word6: 15,
reserved_word7: 16,
}),
};
let mut buffer = [0u8; GPS_MIN_SIZE];
let encoded = frame.encode(&mut buffer, GPS_MIN_SIZE).unwrap();
assert_eq!(
buffer[0], GPS_PREAMBLE_MASK,
"encoded first byte is not preamble!"
);
assert_eq!(buffer[1], 0x00);
#[cfg(feature = "log")]
info!("decoding!");
let mut decoder = GpsQzssDecoder::default().without_parity_verification();
// for byte in buffer.iter() {
// let decoded = decoder.parse(Byte::byte(*byte));
// }
}
}