Skip to content

Commit 4e25232

Browse files
authored
Add wrapper around error detection functions (#23)
1 parent e2b650a commit 4e25232

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn main() {
2626
.header(format!("{}/include/swiftnav/ionosphere.h", dst.display()))
2727
.header(format!("{}/include/swiftnav/troposphere.h", dst.display()))
2828
.header(format!("{}/include/swiftnav/ephemeris.h", dst.display()))
29+
.header(format!("{}/include/swiftnav/edc.h", dst.display()))
2930
// Tell cargo to invalidate the built crate whenever any of the
3031
// included header files changed.
3132
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
@@ -80,6 +81,7 @@ fn main() {
8081
.whitelist_function("decode_ephemeris")
8182
.whitelist_function("decode_bds_d1_ephemeris")
8283
.whitelist_function("decode_gal_ephemeris")
84+
.whitelist_function("crc24q")
8385
// Finish the builder and generate the bindings.
8486
.generate()
8587
// Unwrap the Result and panic on failure.

src/edc.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::c_bindings;
2+
3+
pub fn compute_crc24q(buf: &[u8], initial_value: u32) -> u32 {
4+
unsafe { c_bindings::crc24q(buf.as_ptr(), buf.len() as u32, initial_value) }
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
const TEST_DATA: &[u8] = "123456789".as_bytes();
10+
11+
#[test]
12+
fn crc24q() {
13+
let crc = super::compute_crc24q(&TEST_DATA[0..0], 0);
14+
assert!(
15+
crc == 0,
16+
"CRC of empty buffer with starting value 0 should be 0, not {}",
17+
crc
18+
);
19+
20+
let crc = super::compute_crc24q(&TEST_DATA[0..0], 22);
21+
assert!(
22+
crc == 22,
23+
"CRC of empty buffer with starting value 22 should be 22, not {}",
24+
crc
25+
);
26+
27+
/* Test value taken from python crcmod package tests, see:
28+
* http://crcmod.sourceforge.net/crcmod.predefined.html */
29+
let crc = super::compute_crc24q(TEST_DATA, 0xB704CE);
30+
assert!(
31+
crc == 0x21CF02,
32+
"CRC of \"123456789\" with init value 0xB704CE should be {}, not 0x%06X",
33+
crc
34+
);
35+
}
36+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
mod c_bindings;
1818
pub mod coords;
19+
pub mod edc;
1920
pub mod ephemeris;
2021
pub mod ionosphere;
2122
pub mod signal;

0 commit comments

Comments
 (0)