diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index 460321427c..9df5206c83 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -63,7 +63,7 @@ jobs: matrix: os: - ubuntu-18.04 - - macos-10.15 + - macos-12 - windows-2022 runs-on: ${{ matrix.os }} steps: diff --git a/generator/sbpg/targets/resources/rust/test/sbp_tests_template.rs b/generator/sbpg/targets/resources/rust/test/sbp_tests_template.rs index 1a4b9ee259..9e0ba38c97 100644 --- a/generator/sbpg/targets/resources/rust/test/sbp_tests_template.rs +++ b/generator/sbpg/targets/resources/rust/test/sbp_tests_template.rs @@ -29,6 +29,11 @@ assert_eq!(msg.(((prefix|snake_case))), (((value))), "incorrect value for (((pre ((*- endif *)) ((*- endmacro *)) +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_(((s.suite_name|snake_case)))() { @@ -58,6 +63,13 @@ fn test_(((s.suite_name|snake_case)))() ((*- endfor *)) } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_(((s.suite_name|snake_case)))() @@ -67,13 +79,13 @@ fn test_json2sbp_(((s.suite_name|snake_case)))() let json_input = r#"((( t.raw_json )))"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter.next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter.next() .expect("no message found") @@ -95,3 +107,50 @@ fn test_json2sbp_(((s.suite_name|snake_case)))() ((*- endfor *)) } +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_(((s.suite_name|snake_case)))`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_(((s.suite_name|snake_case)))() +{ + ((*- for t in s.tests *)) + { + let mut payload = Cursor::new(vec![ ((*- for p in t.packet_as_byte_array *))(((p))),((*- endfor *)) ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}).send(&sbp_msg).unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::(((t.msg.name|lower_acronyms)))(serde_json::from_str(std::str::from_utf8(json_buffer.as_slice()).unwrap().to_string().as_str()).unwrap()); + match &sbp_msg { + sbp::messages::Sbp::(((t.msg.name|lower_acronyms)))(msg) => { + assert_eq!( msg.message_type(), (((t.msg_type))), "Incorrect message type, expected (((t.msg_type))), is {}", msg.message_type()); + let sender_id = msg.sender_id().unwrap(); + assert_eq!(sender_id, (((t.sbp.sender))), "incorrect sender id, expected (((t.sbp.sender))), is {}", sender_id); + ((*- for f in t.fieldskeys *))(((compare_value( (((f))), (((t.fields[f]))) ))))((*- endfor *)) + }, + _ => panic!("Invalid message type! Expected a (((t.msg.name)))"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + ((*- endfor *)) +} + diff --git a/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_a.rs index fb75fca61a..65b20e78db 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_acquisition_msg_acq_result_dep_a() { { @@ -330,6 +335,13 @@ fn test_auto_check_sbp_acquisition_msg_acq_result_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { @@ -337,14 +349,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 21, "prn": 8, "cf": 8241.943359375, "crc": 17410, "length": 13, "snr": 14.5, "cp": 727.0, "preamble": 85, "payload": "AABoQQDANUTGxwBGCA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -396,14 +408,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 21, "prn": 9, "cf": 749.2676391601562, "crc": 7131, "length": 13, "snr": 15.300000190734863, "cp": 359.5, "preamble": 85, "payload": "zcx0QQDAs0MhUTtECQ=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -455,14 +467,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 21, "prn": 11, "cf": -6493.65283203125, "crc": 9110, "length": 13, "snr": 18.100000381469727, "cp": 40.5, "preamble": 85, "payload": "zcyQQQAAIkI57crFCw=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -514,14 +526,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 21, "prn": 12, "cf": -999.0234985351562, "crc": 30354, "length": 13, "snr": 15.300000190734863, "cp": 548.5, "preamble": 85, "payload": "zcx0QQAgCUSBwXnEDA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -573,14 +585,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 21, "prn": 14, "cf": 4745.361328125, "crc": 19223, "length": 13, "snr": 15.300000190734863, "cp": 780.5, "preamble": 85, "payload": "zcx0QQAgQ0TkSpRFDg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -632,14 +644,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 21, "prn": 0, "cf": -499.5117492675781, "crc": 53196, "length": 13, "snr": 163.22222900390625, "cp": 584.5, "preamble": 85, "payload": "5DgjQwAgEkSBwfnDAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -688,3 +700,443 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_acquisition_msg_acq_result_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_acquisition_msg_acq_result_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 21, 0, 195, 4, 13, 0, 0, 104, 65, 0, 192, 53, 68, 198, 199, 0, 70, 8, 2, 68, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x15, + "Incorrect message type, expected 0x15, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(8.24194335937500000e+03), + "incorrect value for cf, expected 8.24194335937500000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(7.27000000000000000e+02), + "incorrect value for cp, expected 7.27000000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.prn, 8, + "incorrect value for prn, expected 8, is {}", + msg.prn + ); + assert!( + msg.snr.almost_eq(1.45000000000000000e+01), + "incorrect value for snr, expected 1.45000000000000000e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 21, 0, 195, 4, 13, 205, 204, 116, 65, 0, 192, 179, 67, 33, 81, 59, 68, 9, 219, 27, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x15, + "Incorrect message type, expected 0x15, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(7.49267639160156250e+02), + "incorrect value for cf, expected 7.49267639160156250e+02, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(3.59500000000000000e+02), + "incorrect value for cp, expected 3.59500000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.prn, 9, + "incorrect value for prn, expected 9, is {}", + msg.prn + ); + assert!( + msg.snr.almost_eq(1.53000001907348633e+01), + "incorrect value for snr, expected 1.53000001907348633e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 21, 0, 195, 4, 13, 205, 204, 144, 65, 0, 0, 34, 66, 57, 237, 202, 197, 11, 150, 35, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x15, + "Incorrect message type, expected 0x15, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-6.49365283203125000e+03), + "incorrect value for cf, expected -6.49365283203125000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(4.05000000000000000e+01), + "incorrect value for cp, expected 4.05000000000000000e+01, is {:e}", + msg.cp + ); + assert_eq!( + msg.prn, 11, + "incorrect value for prn, expected 11, is {}", + msg.prn + ); + assert!( + msg.snr.almost_eq(1.81000003814697266e+01), + "incorrect value for snr, expected 1.81000003814697266e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 21, 0, 195, 4, 13, 205, 204, 116, 65, 0, 32, 9, 68, 129, 193, 121, 196, 12, 146, + 118, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x15, + "Incorrect message type, expected 0x15, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-9.99023498535156250e+02), + "incorrect value for cf, expected -9.99023498535156250e+02, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(5.48500000000000000e+02), + "incorrect value for cp, expected 5.48500000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.prn, 12, + "incorrect value for prn, expected 12, is {}", + msg.prn + ); + assert!( + msg.snr.almost_eq(1.53000001907348633e+01), + "incorrect value for snr, expected 1.53000001907348633e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 21, 0, 195, 4, 13, 205, 204, 116, 65, 0, 32, 67, 68, 228, 74, 148, 69, 14, 23, 75, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x15, + "Incorrect message type, expected 0x15, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(4.74536132812500000e+03), + "incorrect value for cf, expected 4.74536132812500000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(7.80500000000000000e+02), + "incorrect value for cp, expected 7.80500000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.prn, 14, + "incorrect value for prn, expected 14, is {}", + msg.prn + ); + assert!( + msg.snr.almost_eq(1.53000001907348633e+01), + "incorrect value for snr, expected 1.53000001907348633e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 21, 0, 195, 4, 13, 228, 56, 35, 67, 0, 32, 18, 68, 129, 193, 249, 195, 0, 204, 207, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x15, + "Incorrect message type, expected 0x15, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-4.99511749267578125e+02), + "incorrect value for cf, expected -4.99511749267578125e+02, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(5.84500000000000000e+02), + "incorrect value for cp, expected 5.84500000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.prn, 0, + "incorrect value for prn, expected 0, is {}", + msg.prn + ); + assert!( + msg.snr.almost_eq(1.63222229003906250e+02), + "incorrect value for snr, expected 1.63222229003906250e+02, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_b.rs b/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_b.rs index d1be4bf73f..7ea49d5ac8 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_b.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_b.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_acquisition_msg_acq_result_dep_b() { { @@ -332,6 +337,13 @@ fn test_auto_check_sbp_acquisition_msg_acq_result_dep_b() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { @@ -339,14 +351,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 20, "cf": 4995.1171875, "crc": 50000, "length": 16, "snr": 36.66360855102539, "sid": {"code": 0, "reserved": 0, "sat": 9}, "cp": 322.0, "preamble": 85, "payload": "iacSQgAAoUPwGJxFCQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -408,14 +420,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 20, "cf": -8241.943359375, "crc": 36757, "length": 16, "snr": 36.16875457763672, "sid": {"code": 0, "reserved": 0, "sat": 3}, "cp": 843.0, "preamble": 85, "payload": "zqwQQgDAUkTGxwDGAwAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -477,14 +489,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 20, "cf": 4745.361328125, "crc": 39859, "length": 16, "snr": 35.77723693847656, "sid": {"code": 0, "reserved": 0, "sat": 18}, "cp": 794.0, "preamble": 85, "payload": "5BsPQgCARkTkSpRFEgAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -546,14 +558,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 20, "cf": 2497.55859375, "crc": 46354, "length": 16, "snr": 35.69451141357422, "sid": {"code": 0, "reserved": 0, "sat": 17}, "cp": 258.5, "preamble": 85, "payload": "LscOQgBAgUPwGBxFEQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -615,14 +627,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 20, "cf": -499.5117492675781, "crc": 52003, "length": 16, "snr": 35.52417755126953, "sid": {"code": 0, "reserved": 0, "sat": 5}, "cp": 522.0, "preamble": 85, "payload": "whgOQgCAAkSBwfnDBQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -681,3 +693,426 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_b() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_acquisition_msg_acq_result_dep_b`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_acquisition_msg_acq_result_dep_b() { + { + let mut payload = Cursor::new(vec![ + 85, 20, 0, 246, 215, 16, 137, 167, 18, 66, 0, 0, 161, 67, 240, 24, 156, 69, 9, 0, 0, 0, + 80, 195, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x14, + "Incorrect message type, expected 0x14, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(4.99511718750000000e+03), + "incorrect value for cf, expected 4.99511718750000000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(3.22000000000000000e+02), + "incorrect value for cp, expected 3.22000000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 9, + "incorrect value for sid.sat, expected 9, is {}", + msg.sid.sat + ); + assert!( + msg.snr.almost_eq(3.66636085510253906e+01), + "incorrect value for snr, expected 3.66636085510253906e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 20, 0, 246, 215, 16, 206, 172, 16, 66, 0, 192, 82, 68, 198, 199, 0, 198, 3, 0, 0, + 0, 149, 143, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x14, + "Incorrect message type, expected 0x14, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-8.24194335937500000e+03), + "incorrect value for cf, expected -8.24194335937500000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(8.43000000000000000e+02), + "incorrect value for cp, expected 8.43000000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 3, + "incorrect value for sid.sat, expected 3, is {}", + msg.sid.sat + ); + assert!( + msg.snr.almost_eq(3.61687545776367188e+01), + "incorrect value for snr, expected 3.61687545776367188e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 20, 0, 246, 215, 16, 228, 27, 15, 66, 0, 128, 70, 68, 228, 74, 148, 69, 18, 0, 0, + 0, 179, 155, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x14, + "Incorrect message type, expected 0x14, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(4.74536132812500000e+03), + "incorrect value for cf, expected 4.74536132812500000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(7.94000000000000000e+02), + "incorrect value for cp, expected 7.94000000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 18, + "incorrect value for sid.sat, expected 18, is {}", + msg.sid.sat + ); + assert!( + msg.snr.almost_eq(3.57772369384765625e+01), + "incorrect value for snr, expected 3.57772369384765625e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 20, 0, 246, 215, 16, 46, 199, 14, 66, 0, 64, 129, 67, 240, 24, 28, 69, 17, 0, 0, 0, + 18, 181, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x14, + "Incorrect message type, expected 0x14, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(2.49755859375000000e+03), + "incorrect value for cf, expected 2.49755859375000000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(2.58500000000000000e+02), + "incorrect value for cp, expected 2.58500000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 17, + "incorrect value for sid.sat, expected 17, is {}", + msg.sid.sat + ); + assert!( + msg.snr.almost_eq(3.56945114135742188e+01), + "incorrect value for snr, expected 3.56945114135742188e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 20, 0, 246, 215, 16, 194, 24, 14, 66, 0, 128, 2, 68, 129, 193, 249, 195, 5, 0, 0, + 0, 35, 203, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x14, + "Incorrect message type, expected 0x14, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-4.99511749267578125e+02), + "incorrect value for cf, expected -4.99511749267578125e+02, is {:e}", + msg.cf + ); + assert!( + msg.cp.almost_eq(5.22000000000000000e+02), + "incorrect value for cp, expected 5.22000000000000000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 5, + "incorrect value for sid.sat, expected 5, is {}", + msg.sid.sat + ); + assert!( + msg.snr.almost_eq(3.55241775512695312e+01), + "incorrect value for snr, expected 3.55241775512695312e+01, is {:e}", + msg.snr + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_c.rs b/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_c.rs index d2d2953120..b07c860ca4 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_c.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_acquisition_msg_acq_result_dep_c.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_acquisition_msg_acq_result_dep_c() { { @@ -332,6 +337,13 @@ fn test_auto_check_sbp_acquisition_msg_acq_result_dep_c() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { @@ -339,14 +351,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { let json_input = r#"{"sender": 3112, "msg_type": 31, "cf": 1769.06591796875, "cn0": 40.509063720703125, "crc": 48393, "length": 16, "sid": {"code": 0, "reserved": 0, "sat": 10}, "cp": 457.1922302246094, "preamble": 85, "payload": "SAkiQpuY5EMcIt1ECgAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -408,14 +420,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { let json_input = r#"{"sender": 3112, "msg_type": 31, "cf": -505.4473876953125, "cn0": 43.49464416503906, "crc": 47496, "length": 16, "sid": {"code": 0, "reserved": 0, "sat": 6}, "cp": 865.4657592773438, "preamble": 85, "payload": "hPotQs9dWEREufzDBgAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -477,14 +489,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { let json_input = r#"{"sender": 3112, "msg_type": 31, "cf": -1263.618408203125, "cn0": 38.2183952331543, "crc": 41366, "length": 16, "sid": {"code": 0, "reserved": 0, "sat": 13}, "cp": 230.3564453125, "preamble": 85, "payload": "o98YQkBbZkPK853EDQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -546,14 +558,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { let json_input = r#"{"sender": 3112, "msg_type": 31, "cf": 3032.684326171875, "cn0": 37.313968658447266, "crc": 53613, "length": 16, "sid": {"code": 0, "reserved": 0, "sat": 1}, "cp": 252.83935546875, "preamble": 85, "payload": "gUEVQuDWfEPzij1FAQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -615,14 +627,14 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { let json_input = r#"{"sender": 3112, "msg_type": 31, "cf": 2527.23681640625, "cn0": 47.53466033935547, "crc": 17243, "length": 16, "sid": {"code": 0, "reserved": 0, "sat": 27}, "cp": 920.5919189453125, "preamble": 85, "payload": "fiM+QuIlZkTK8x1FGwAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -681,3 +693,426 @@ fn test_json2sbp_auto_check_sbp_acquisition_msg_acq_result_dep_c() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_acquisition_msg_acq_result_dep_c`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_acquisition_msg_acq_result_dep_c() { + { + let mut payload = Cursor::new(vec![ + 85, 31, 0, 40, 12, 16, 72, 9, 34, 66, 155, 152, 228, 67, 28, 34, 221, 68, 10, 0, 0, 0, + 9, 189, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x1f, + "Incorrect message type, expected 0x1f, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xc28, + "incorrect sender id, expected 0xc28, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(1.76906591796875000e+03), + "incorrect value for cf, expected 1.76906591796875000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cn0.almost_eq(4.05090637207031250e+01), + "incorrect value for cn0, expected 4.05090637207031250e+01, is {:e}", + msg.cn0 + ); + assert!( + msg.cp.almost_eq(4.57192230224609375e+02), + "incorrect value for cp, expected 4.57192230224609375e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 10, + "incorrect value for sid.sat, expected 10, is {}", + msg.sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 31, 0, 40, 12, 16, 132, 250, 45, 66, 207, 93, 88, 68, 68, 185, 252, 195, 6, 0, 0, + 0, 136, 185, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x1f, + "Incorrect message type, expected 0x1f, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xc28, + "incorrect sender id, expected 0xc28, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-5.05447387695312500e+02), + "incorrect value for cf, expected -5.05447387695312500e+02, is {:e}", + msg.cf + ); + assert!( + msg.cn0.almost_eq(4.34946441650390625e+01), + "incorrect value for cn0, expected 4.34946441650390625e+01, is {:e}", + msg.cn0 + ); + assert!( + msg.cp.almost_eq(8.65465759277343750e+02), + "incorrect value for cp, expected 8.65465759277343750e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 6, + "incorrect value for sid.sat, expected 6, is {}", + msg.sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 31, 0, 40, 12, 16, 163, 223, 24, 66, 64, 91, 102, 67, 202, 243, 157, 196, 13, 0, 0, + 0, 150, 161, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x1f, + "Incorrect message type, expected 0x1f, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xc28, + "incorrect sender id, expected 0xc28, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(-1.26361840820312500e+03), + "incorrect value for cf, expected -1.26361840820312500e+03, is {:e}", + msg.cf + ); + assert!( + msg.cn0.almost_eq(3.82183952331542969e+01), + "incorrect value for cn0, expected 3.82183952331542969e+01, is {:e}", + msg.cn0 + ); + assert!( + msg.cp.almost_eq(2.30356445312500000e+02), + "incorrect value for cp, expected 2.30356445312500000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 13, + "incorrect value for sid.sat, expected 13, is {}", + msg.sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 31, 0, 40, 12, 16, 129, 65, 21, 66, 224, 214, 124, 67, 243, 138, 61, 69, 1, 0, 0, + 0, 109, 209, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x1f, + "Incorrect message type, expected 0x1f, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xc28, + "incorrect sender id, expected 0xc28, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(3.03268432617187500e+03), + "incorrect value for cf, expected 3.03268432617187500e+03, is {:e}", + msg.cf + ); + assert!( + msg.cn0.almost_eq(3.73139686584472656e+01), + "incorrect value for cn0, expected 3.73139686584472656e+01, is {:e}", + msg.cn0 + ); + assert!( + msg.cp.almost_eq(2.52839355468750000e+02), + "incorrect value for cp, expected 2.52839355468750000e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 1, + "incorrect value for sid.sat, expected 1, is {}", + msg.sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 31, 0, 40, 12, 16, 126, 35, 62, 66, 226, 37, 102, 68, 202, 243, 29, 69, 27, 0, 0, + 0, 91, 67, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAcqResultDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAcqResultDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x1f, + "Incorrect message type, expected 0x1f, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xc28, + "incorrect sender id, expected 0xc28, is {}", + sender_id + ); + assert!( + msg.cf.almost_eq(2.52723681640625000e+03), + "incorrect value for cf, expected 2.52723681640625000e+03, is {:e}", + msg.cf + ); + assert!( + msg.cn0.almost_eq(4.75346603393554688e+01), + "incorrect value for cn0, expected 4.75346603393554688e+01, is {:e}", + msg.cn0 + ); + assert!( + msg.cp.almost_eq(9.20591918945312500e+02), + "incorrect value for cp, expected 9.20591918945312500e+02, is {:e}", + msg.cp + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 27, + "incorrect value for sid.sat, expected 27, is {}", + msg.sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgAcqResultDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_bootload_msg_bootloader_handshake_resp.rs b/rust/sbp/tests/integration/auto_check_sbp_bootload_msg_bootloader_handshake_resp.rs index b7c901a675..b2781096b8 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_bootload_msg_bootloader_handshake_resp.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_bootload_msg_bootloader_handshake_resp.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { { @@ -115,6 +120,13 @@ fn test_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { @@ -122,14 +134,14 @@ fn test_json2sbp_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { let json_input = r#"{"sender": 0, "msg_type": 180, "crc": 457, "length": 9, "version": "v1.2\n", "flags": 0, "preamble": 85, "payload": "AAAAAHYxLjIK"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -177,14 +189,14 @@ fn test_json2sbp_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { let json_input = r#"{ "handshake": [118, 49, 46, 50], "crc": 52737, "length": 4, "msg_type": 176, "payload": "djEuMg==", "preamble": 85, "sender": 1219 }"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -233,3 +245,152 @@ fn test_json2sbp_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_bootload_msg_bootloader_handshake_resp`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_bootload_msg_bootloader_handshake_resp() { + { + let mut payload = Cursor::new(vec![ + 85, 180, 0, 0, 0, 9, 0, 0, 0, 0, 118, 49, 46, 50, 10, 201, 1, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBootloaderHandshakeResp( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBootloaderHandshakeResp(msg) => { + assert_eq!( + msg.message_type(), + 0xb4, + "Incorrect message type, expected 0xb4, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0, + "incorrect sender id, expected 0x0, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.version.to_string(), + "v1.2 +" + .to_string(), + "incorrect value for msg.version, expected string '{}', is '{}'", + "v1.2 +" + .to_string(), + msg.version + ); + } + _ => panic!("Invalid message type! Expected a MsgBootloaderHandshakeResp"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 176, 0, 195, 4, 4, 118, 49, 46, 50, 1, 206]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBootloaderHandshakeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBootloaderHandshakeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0xb0, + "Incorrect message type, expected 0xb0, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.handshake[0], 118, + "incorrect value for handshake[0], expected 118, is {}", + msg.handshake[0] + ); + assert_eq!( + msg.handshake[1], 49, + "incorrect value for handshake[1], expected 49, is {}", + msg.handshake[1] + ); + assert_eq!( + msg.handshake[2], 46, + "incorrect value for handshake[2], expected 46, is {}", + msg.handshake[2] + ); + assert_eq!( + msg.handshake[3], 50, + "incorrect value for handshake[3], expected 50, is {}", + msg.handshake[3] + ); + } + _ => panic!("Invalid message type! Expected a MsgBootloaderHandshakeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_ext_events_msg_ext_event.rs b/rust/sbp/tests/integration/auto_check_sbp_ext_events_msg_ext_event.rs index a02322d7f5..fb7fb57643 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ext_events_msg_ext_event.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ext_events_msg_ext_event.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ext_events_msg_ext_event() { { @@ -74,6 +79,13 @@ fn test_auto_check_sbp_ext_events_msg_ext_event() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ext_events_msg_ext_event() { @@ -81,14 +93,14 @@ fn test_json2sbp_auto_check_sbp_ext_events_msg_ext_event() { let json_input = r#"{"sender": 1781, "msg_type": 257, "wn": 1840, "tow": 254924999, "crc": 52286, "length": 12, "flags": 3, "pin": 0, "ns_residual": 999882, "preamble": 85, "payload": "MAfH2DEPykEPAAMA"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -142,3 +154,92 @@ fn test_json2sbp_auto_check_sbp_ext_events_msg_ext_event() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ext_events_msg_ext_event`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ext_events_msg_ext_event() { + { + let mut payload = Cursor::new(vec![ + 85, 1, 1, 245, 6, 12, 48, 7, 199, 216, 49, 15, 202, 65, 15, 0, 3, 0, 62, 204, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgExtEvent( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgExtEvent(msg) => { + assert_eq!( + msg.message_type(), + 0x101, + "Incorrect message type, expected 0x101, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x6f5, + "incorrect sender id, expected 0x6f5, is {}", + sender_id + ); + assert_eq!( + msg.flags, 3, + "incorrect value for flags, expected 3, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 999882, + "incorrect value for ns_residual, expected 999882, is {}", + msg.ns_residual + ); + assert_eq!( + msg.pin, 0, + "incorrect value for pin, expected 0, is {}", + msg.pin + ); + assert_eq!( + msg.tow, 254924999, + "incorrect value for tow, expected 254924999, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1840, + "incorrect value for wn, expected 1840, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgExtEvent"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_file_io_msg_fileio_write_resp.rs b/rust/sbp/tests/integration/auto_check_sbp_file_io_msg_fileio_write_resp.rs index a5a5f6ccde..d514994cf7 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_file_io_msg_fileio_write_resp.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_file_io_msg_fileio_write_resp.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_file_io_msg_fileio_write_resp() { { @@ -52,6 +57,13 @@ fn test_auto_check_sbp_file_io_msg_fileio_write_resp() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_file_io_msg_fileio_write_resp() { @@ -59,14 +71,14 @@ fn test_json2sbp_auto_check_sbp_file_io_msg_fileio_write_resp() { let json_input = r#"{"sender": 66, "msg_type": 171, "sequence": 202, "crc": 62451, "length": 4, "preamble": 85, "payload": "ygAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -100,3 +112,70 @@ fn test_json2sbp_auto_check_sbp_file_io_msg_fileio_write_resp() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_file_io_msg_fileio_write_resp`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_file_io_msg_fileio_write_resp() { + { + let mut payload = Cursor::new(vec![85, 171, 0, 66, 0, 4, 202, 0, 0, 0, 243, 243]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgFileioWriteResp( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgFileioWriteResp(msg) => { + assert_eq!( + msg.message_type(), + 0xab, + "Incorrect message type, expected 0xab, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.sequence, 202, + "incorrect value for sequence, expected 202, is {}", + msg.sequence + ); + } + _ => panic!("Invalid message type! Expected a MsgFileioWriteResp"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_aux.rs b/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_aux.rs index 6fc4cd2e77..6431fb8ce9 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_aux.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_aux.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_imu_msg_imu_aux() { { @@ -62,6 +67,13 @@ fn test_auto_check_sbp_imu_msg_imu_aux() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_imu_msg_imu_aux() { @@ -69,14 +81,14 @@ fn test_json2sbp_auto_check_sbp_imu_msg_imu_aux() { let json_input = r#"{"imu_type":1,"temp":2804,"imu_conf":66,"preamble":85,"msg_type":2305,"sender":4660,"payload":"AfQKQg==","crc":64712,"length":4}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -120,3 +132,80 @@ fn test_json2sbp_auto_check_sbp_imu_msg_imu_aux() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_imu_msg_imu_aux`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_imu_msg_imu_aux() { + { + let mut payload = Cursor::new(vec![85, 1, 9, 52, 18, 4, 1, 244, 10, 66, 200, 252]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgImuAux( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgImuAux(msg) => { + assert_eq!( + msg.message_type(), + 0x901, + "Incorrect message type, expected 0x901, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1234, + "incorrect sender id, expected 0x1234, is {}", + sender_id + ); + assert_eq!( + msg.imu_conf, 66, + "incorrect value for imu_conf, expected 66, is {}", + msg.imu_conf + ); + assert_eq!( + msg.imu_type, 1, + "incorrect value for imu_type, expected 1, is {}", + msg.imu_type + ); + assert_eq!( + msg.temp, 2804, + "incorrect value for temp, expected 2804, is {}", + msg.temp + ); + } + _ => panic!("Invalid message type! Expected a MsgImuAux"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_raw.rs b/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_raw.rs index d2686608c0..c39c4700b7 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_raw.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_imu_msg_imu_raw.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_imu_msg_imu_raw() { { @@ -90,6 +95,13 @@ fn test_auto_check_sbp_imu_msg_imu_raw() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_imu_msg_imu_raw() { @@ -97,14 +109,14 @@ fn test_json2sbp_auto_check_sbp_imu_msg_imu_raw() { let json_input = r#"{"tow":3221225754,"tow_f":206,"acc_x":96,"acc_y":-33,"acc_z":4140,"gyr_x":60,"gyr_y":-304,"gyr_z":-18,"preamble":85,"msg_type":2304,"sender":4660,"payload":"GgEAwM5gAN//LBA8AND+7v8=","crc":34630,"length":17}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -173,3 +185,108 @@ fn test_json2sbp_auto_check_sbp_imu_msg_imu_raw() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_imu_msg_imu_raw`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_imu_msg_imu_raw() { + { + let mut payload = Cursor::new(vec![ + 85, 0, 9, 52, 18, 17, 26, 1, 0, 192, 206, 96, 0, 223, 255, 44, 16, 60, 0, 208, 254, + 238, 255, 70, 135, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgImuRaw( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgImuRaw(msg) => { + assert_eq!( + msg.message_type(), + 0x900, + "Incorrect message type, expected 0x900, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1234, + "incorrect sender id, expected 0x1234, is {}", + sender_id + ); + assert_eq!( + msg.acc_x, 96, + "incorrect value for acc_x, expected 96, is {}", + msg.acc_x + ); + assert_eq!( + msg.acc_y, -33, + "incorrect value for acc_y, expected -33, is {}", + msg.acc_y + ); + assert_eq!( + msg.acc_z, 4140, + "incorrect value for acc_z, expected 4140, is {}", + msg.acc_z + ); + assert_eq!( + msg.gyr_x, 60, + "incorrect value for gyr_x, expected 60, is {}", + msg.gyr_x + ); + assert_eq!( + msg.gyr_y, -304, + "incorrect value for gyr_y, expected -304, is {}", + msg.gyr_y + ); + assert_eq!( + msg.gyr_z, -18, + "incorrect value for gyr_z, expected -18, is {}", + msg.gyr_z + ); + assert_eq!( + msg.tow, 3221225754, + "incorrect value for tow, expected 3221225754, is {}", + msg.tow + ); + assert_eq!( + msg.tow_f, 206, + "incorrect value for tow_f, expected 206, is {}", + msg.tow_f + ); + } + _ => panic!("Invalid message type! Expected a MsgImuRaw"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_high_level.rs b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_high_level.rs index 195ad726f0..367905f361 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_high_level.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_high_level.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_integrity_msg_ssr_flag_high_level() { { @@ -155,6 +160,13 @@ fn test_auto_check_sbp_integrity_msg_ssr_flag_high_level() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_high_level() { @@ -162,14 +174,14 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_high_level() { let json_input = r#"{"obs_time": {"tow": 180, "wn": 3}, "corr_time": {"tow": 360, "wn": 6}, "ssr_sol_id": 10, "tile_set_id": 20, "tile_id": 30, "chain_id": 40, "use_gps_sat": 1, "use_gal_sat": 2, "use_bds_sat": 3, "reserved": [0,0,0,0,0,0], "use_tropo_grid_points": 4, "use_iono_grid_points": 5, "use_iono_tile_sat_los": 6, "use_iono_grid_point_sat_los": 7, "preamble": 85, "msg_type": 3001, "sender": 66, "length": 31, "payload": "tAAAAAMAaAEAAAYAChQAHgAoAQIDAAAAAAAABAUGBw==", "crc": 17254}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -303,3 +315,173 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_high_level() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_integrity_msg_ssr_flag_high_level`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_integrity_msg_ssr_flag_high_level() { + { + let mut payload = Cursor::new(vec![ + 85, 185, 11, 66, 0, 31, 180, 0, 0, 0, 3, 0, 104, 1, 0, 0, 6, 0, 10, 20, 0, 30, 0, 40, + 1, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 102, 67, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrFlagHighLevel( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrFlagHighLevel(msg) => { + assert_eq!( + msg.message_type(), + 3001, + "Incorrect message type, expected 3001, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.chain_id, 40, + "incorrect value for chain_id, expected 40, is {}", + msg.chain_id + ); + assert_eq!( + msg.corr_time.tow, 360, + "incorrect value for corr_time.tow, expected 360, is {}", + msg.corr_time.tow + ); + assert_eq!( + msg.corr_time.wn, 6, + "incorrect value for corr_time.wn, expected 6, is {}", + msg.corr_time.wn + ); + assert_eq!( + msg.obs_time.tow, 180, + "incorrect value for obs_time.tow, expected 180, is {}", + msg.obs_time.tow + ); + assert_eq!( + msg.obs_time.wn, 3, + "incorrect value for obs_time.wn, expected 3, is {}", + msg.obs_time.wn + ); + assert_eq!( + msg.reserved[0], 0, + "incorrect value for reserved[0], expected 0, is {}", + msg.reserved[0] + ); + assert_eq!( + msg.reserved[1], 0, + "incorrect value for reserved[1], expected 0, is {}", + msg.reserved[1] + ); + assert_eq!( + msg.reserved[2], 0, + "incorrect value for reserved[2], expected 0, is {}", + msg.reserved[2] + ); + assert_eq!( + msg.reserved[3], 0, + "incorrect value for reserved[3], expected 0, is {}", + msg.reserved[3] + ); + assert_eq!( + msg.reserved[4], 0, + "incorrect value for reserved[4], expected 0, is {}", + msg.reserved[4] + ); + assert_eq!( + msg.reserved[5], 0, + "incorrect value for reserved[5], expected 0, is {}", + msg.reserved[5] + ); + assert_eq!( + msg.ssr_sol_id, 10, + "incorrect value for ssr_sol_id, expected 10, is {}", + msg.ssr_sol_id + ); + assert_eq!( + msg.tile_id, 30, + "incorrect value for tile_id, expected 30, is {}", + msg.tile_id + ); + assert_eq!( + msg.tile_set_id, 20, + "incorrect value for tile_set_id, expected 20, is {}", + msg.tile_set_id + ); + assert_eq!( + msg.use_bds_sat, 3, + "incorrect value for use_bds_sat, expected 3, is {}", + msg.use_bds_sat + ); + assert_eq!( + msg.use_gal_sat, 2, + "incorrect value for use_gal_sat, expected 2, is {}", + msg.use_gal_sat + ); + assert_eq!( + msg.use_gps_sat, 1, + "incorrect value for use_gps_sat, expected 1, is {}", + msg.use_gps_sat + ); + assert_eq!( + msg.use_iono_grid_point_sat_los, 7, + "incorrect value for use_iono_grid_point_sat_los, expected 7, is {}", + msg.use_iono_grid_point_sat_los + ); + assert_eq!( + msg.use_iono_grid_points, 5, + "incorrect value for use_iono_grid_points, expected 5, is {}", + msg.use_iono_grid_points + ); + assert_eq!( + msg.use_iono_tile_sat_los, 6, + "incorrect value for use_iono_tile_sat_los, expected 6, is {}", + msg.use_iono_tile_sat_los + ); + assert_eq!( + msg.use_tropo_grid_points, 4, + "incorrect value for use_tropo_grid_points, expected 4, is {}", + msg.use_tropo_grid_points + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrFlagHighLevel"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los.rs b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los.rs index d4da95b990..98efc60f53 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los() { { @@ -120,6 +125,13 @@ fn test_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los() { @@ -127,14 +139,14 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los() let json_input = r#"{"header": {"obs_time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "ssr_sol_id": 3, "tile_set_id": 4, "tile_id": 5, "chain_id": 6}, "grid_point_id": 30, "n_faulty_los": 2, "faulty_los": [{"satId": 10, "constellation": 11}, {"satId": 15, "constellation": 14}], "preamble": 85, "msg_type": 3025, "sender": 66, "length": 21, "payload": "tAAAAAMAAQIDBAAFAAYeAAIKCw8O", "crc": 37986}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -233,3 +245,138 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los() }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_point_sat_los() { + { + let mut payload = Cursor::new(vec![ + 85, 209, 11, 66, 0, 21, 180, 0, 0, 0, 3, 0, 1, 2, 3, 4, 0, 5, 0, 6, 30, 0, 2, 10, 11, + 15, 14, 98, 148, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrFlagIonoGridPointSatLos( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrFlagIonoGridPointSatLos(msg) => { + assert_eq!( + msg.message_type(), + 3025, + "Incorrect message type, expected 3025, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.faulty_los[0].constellation, 11, + "incorrect value for faulty_los[0].constellation, expected 11, is {}", + msg.faulty_los[0].constellation + ); + assert_eq!( + msg.faulty_los[0].sat_id, 10, + "incorrect value for faulty_los[0].sat_id, expected 10, is {}", + msg.faulty_los[0].sat_id + ); + assert_eq!( + msg.faulty_los[1].constellation, 14, + "incorrect value for faulty_los[1].constellation, expected 14, is {}", + msg.faulty_los[1].constellation + ); + assert_eq!( + msg.faulty_los[1].sat_id, 15, + "incorrect value for faulty_los[1].sat_id, expected 15, is {}", + msg.faulty_los[1].sat_id + ); + assert_eq!( + msg.grid_point_id, 30, + "incorrect value for grid_point_id, expected 30, is {}", + msg.grid_point_id + ); + assert_eq!( + msg.header.chain_id, 6, + "incorrect value for header.chain_id, expected 6, is {}", + msg.header.chain_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.obs_time.tow, 180, + "incorrect value for header.obs_time.tow, expected 180, is {}", + msg.header.obs_time.tow + ); + assert_eq!( + msg.header.obs_time.wn, 3, + "incorrect value for header.obs_time.wn, expected 3, is {}", + msg.header.obs_time.wn + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.ssr_sol_id, 3, + "incorrect value for header.ssr_sol_id, expected 3, is {}", + msg.header.ssr_sol_id + ); + assert_eq!( + msg.header.tile_id, 5, + "incorrect value for header.tile_id, expected 5, is {}", + msg.header.tile_id + ); + assert_eq!( + msg.header.tile_set_id, 4, + "incorrect value for header.tile_set_id, expected 4, is {}", + msg.header.tile_set_id + ); + assert_eq!( + msg.n_faulty_los, 2, + "incorrect value for n_faulty_los, expected 2, is {}", + msg.n_faulty_los + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrFlagIonoGridPointSatLos"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points.rs b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points.rs index 841f8773fe..ae227cfae1 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points() { { @@ -110,6 +115,13 @@ fn test_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points() { @@ -117,14 +129,14 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points() { let json_input = r#"{"header": {"obs_time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "ssr_sol_id": 3, "tile_set_id": 4, "tile_id": 5, "chain_id": 6}, "n_faulty_points": 3, "faulty_points": [10, 11, 12], "preamble": 85, "msg_type": 3015, "sender": 66, "length": 21, "payload": "tAAAAAMAAQIDBAAFAAYDCgALAAwA", "crc": 1845}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -213,3 +225,128 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_integrity_msg_ssr_flag_iono_grid_points() { + { + let mut payload = Cursor::new(vec![ + 85, 199, 11, 66, 0, 21, 180, 0, 0, 0, 3, 0, 1, 2, 3, 4, 0, 5, 0, 6, 3, 10, 0, 11, 0, + 12, 0, 53, 7, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrFlagIonoGridPoints( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrFlagIonoGridPoints(msg) => { + assert_eq!( + msg.message_type(), + 3015, + "Incorrect message type, expected 3015, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.faulty_points[0], 10, + "incorrect value for faulty_points[0], expected 10, is {}", + msg.faulty_points[0] + ); + assert_eq!( + msg.faulty_points[1], 11, + "incorrect value for faulty_points[1], expected 11, is {}", + msg.faulty_points[1] + ); + assert_eq!( + msg.faulty_points[2], 12, + "incorrect value for faulty_points[2], expected 12, is {}", + msg.faulty_points[2] + ); + assert_eq!( + msg.header.chain_id, 6, + "incorrect value for header.chain_id, expected 6, is {}", + msg.header.chain_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.obs_time.tow, 180, + "incorrect value for header.obs_time.tow, expected 180, is {}", + msg.header.obs_time.tow + ); + assert_eq!( + msg.header.obs_time.wn, 3, + "incorrect value for header.obs_time.wn, expected 3, is {}", + msg.header.obs_time.wn + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.ssr_sol_id, 3, + "incorrect value for header.ssr_sol_id, expected 3, is {}", + msg.header.ssr_sol_id + ); + assert_eq!( + msg.header.tile_id, 5, + "incorrect value for header.tile_id, expected 5, is {}", + msg.header.tile_id + ); + assert_eq!( + msg.header.tile_set_id, 4, + "incorrect value for header.tile_set_id, expected 4, is {}", + msg.header.tile_set_id + ); + assert_eq!( + msg.n_faulty_points, 3, + "incorrect value for n_faulty_points, expected 3, is {}", + msg.n_faulty_points + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrFlagIonoGridPoints"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los.rs b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los.rs index b634dba401..aa62ed9ab0 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los() { { @@ -115,6 +120,13 @@ fn test_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los() { @@ -122,14 +134,14 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los() { let json_input = r#"{"header": {"obs_time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "ssr_sol_id": 3, "tile_set_id": 4, "tile_id": 5, "chain_id": 6}, "n_faulty_los": 2, "faulty_los": [{"satId": 10, "constellation": 11}, {"satId": 15, "constellation": 14}], "preamble": 85, "msg_type": 3021, "sender": 66, "length": 19, "payload": "tAAAAAMAAQIDBAAFAAYCCgsPDg==", "crc": 60399}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -223,3 +235,133 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_integrity_msg_ssr_flag_iono_tile_sat_los() { + { + let mut payload = Cursor::new(vec![ + 85, 205, 11, 66, 0, 19, 180, 0, 0, 0, 3, 0, 1, 2, 3, 4, 0, 5, 0, 6, 2, 10, 11, 15, 14, + 239, 235, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrFlagIonoTileSatLos( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrFlagIonoTileSatLos(msg) => { + assert_eq!( + msg.message_type(), + 3021, + "Incorrect message type, expected 3021, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.faulty_los[0].constellation, 11, + "incorrect value for faulty_los[0].constellation, expected 11, is {}", + msg.faulty_los[0].constellation + ); + assert_eq!( + msg.faulty_los[0].sat_id, 10, + "incorrect value for faulty_los[0].sat_id, expected 10, is {}", + msg.faulty_los[0].sat_id + ); + assert_eq!( + msg.faulty_los[1].constellation, 14, + "incorrect value for faulty_los[1].constellation, expected 14, is {}", + msg.faulty_los[1].constellation + ); + assert_eq!( + msg.faulty_los[1].sat_id, 15, + "incorrect value for faulty_los[1].sat_id, expected 15, is {}", + msg.faulty_los[1].sat_id + ); + assert_eq!( + msg.header.chain_id, 6, + "incorrect value for header.chain_id, expected 6, is {}", + msg.header.chain_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.obs_time.tow, 180, + "incorrect value for header.obs_time.tow, expected 180, is {}", + msg.header.obs_time.tow + ); + assert_eq!( + msg.header.obs_time.wn, 3, + "incorrect value for header.obs_time.wn, expected 3, is {}", + msg.header.obs_time.wn + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.ssr_sol_id, 3, + "incorrect value for header.ssr_sol_id, expected 3, is {}", + msg.header.ssr_sol_id + ); + assert_eq!( + msg.header.tile_id, 5, + "incorrect value for header.tile_id, expected 5, is {}", + msg.header.tile_id + ); + assert_eq!( + msg.header.tile_set_id, 4, + "incorrect value for header.tile_set_id, expected 4, is {}", + msg.header.tile_set_id + ); + assert_eq!( + msg.n_faulty_los, 2, + "incorrect value for n_faulty_los, expected 2, is {}", + msg.n_faulty_los + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrFlagIonoTileSatLos"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_satellites.rs b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_satellites.rs index 0779fb2a53..ce97004db6 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_satellites.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_satellites.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_integrity_msg_ssr_flag_satellites() { { @@ -104,6 +109,13 @@ fn test_auto_check_sbp_integrity_msg_ssr_flag_satellites() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_satellites() { @@ -111,14 +123,14 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_satellites() { let json_input = r#"{"obs_time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "ssr_sol_id": 3, "chain_id": 4, "const_id": 5, "n_faulty_sats": 3, "faulty_sats": [10, 11, 12], "preamble": 85, "msg_type": 3005, "sender": 66, "length": 15, "payload": "tAAAAAMAAQIDBAUDCgsM", "crc": 42350}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -202,3 +214,122 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_satellites() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_integrity_msg_ssr_flag_satellites`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_integrity_msg_ssr_flag_satellites() { + { + let mut payload = Cursor::new(vec![ + 85, 189, 11, 66, 0, 15, 180, 0, 0, 0, 3, 0, 1, 2, 3, 4, 5, 3, 10, 11, 12, 110, 165, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrFlagSatellites( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrFlagSatellites(msg) => { + assert_eq!( + msg.message_type(), + 3005, + "Incorrect message type, expected 3005, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.chain_id, 4, + "incorrect value for chain_id, expected 4, is {}", + msg.chain_id + ); + assert_eq!( + msg.const_id, 5, + "incorrect value for const_id, expected 5, is {}", + msg.const_id + ); + assert_eq!( + msg.faulty_sats[0], 10, + "incorrect value for faulty_sats[0], expected 10, is {}", + msg.faulty_sats[0] + ); + assert_eq!( + msg.faulty_sats[1], 11, + "incorrect value for faulty_sats[1], expected 11, is {}", + msg.faulty_sats[1] + ); + assert_eq!( + msg.faulty_sats[2], 12, + "incorrect value for faulty_sats[2], expected 12, is {}", + msg.faulty_sats[2] + ); + assert_eq!( + msg.n_faulty_sats, 3, + "incorrect value for n_faulty_sats, expected 3, is {}", + msg.n_faulty_sats + ); + assert_eq!( + msg.num_msgs, 1, + "incorrect value for num_msgs, expected 1, is {}", + msg.num_msgs + ); + assert_eq!( + msg.obs_time.tow, 180, + "incorrect value for obs_time.tow, expected 180, is {}", + msg.obs_time.tow + ); + assert_eq!( + msg.obs_time.wn, 3, + "incorrect value for obs_time.wn, expected 3, is {}", + msg.obs_time.wn + ); + assert_eq!( + msg.seq_num, 2, + "incorrect value for seq_num, expected 2, is {}", + msg.seq_num + ); + assert_eq!( + msg.ssr_sol_id, 3, + "incorrect value for ssr_sol_id, expected 3, is {}", + msg.ssr_sol_id + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrFlagSatellites"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points.rs b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points.rs index b4b17938d6..23a61f6657 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points() { { @@ -110,6 +115,13 @@ fn test_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points() { @@ -117,14 +129,14 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points() { let json_input = r#"{"header": {"obs_time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "ssr_sol_id": 3, "tile_set_id": 4, "tile_id": 5, "chain_id": 6}, "n_faulty_points": 3, "faulty_points": [10, 11, 12], "preamble": 85, "msg_type": 3011, "sender": 66, "length": 21, "payload": "tAAAAAMAAQIDBAAFAAYDCgALAAwA", "crc": 38643}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -213,3 +225,128 @@ fn test_json2sbp_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_integrity_msg_ssr_flag_tropo_grid_points() { + { + let mut payload = Cursor::new(vec![ + 85, 195, 11, 66, 0, 21, 180, 0, 0, 0, 3, 0, 1, 2, 3, 4, 0, 5, 0, 6, 3, 10, 0, 11, 0, + 12, 0, 243, 150, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrFlagTropoGridPoints( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrFlagTropoGridPoints(msg) => { + assert_eq!( + msg.message_type(), + 3011, + "Incorrect message type, expected 3011, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.faulty_points[0], 10, + "incorrect value for faulty_points[0], expected 10, is {}", + msg.faulty_points[0] + ); + assert_eq!( + msg.faulty_points[1], 11, + "incorrect value for faulty_points[1], expected 11, is {}", + msg.faulty_points[1] + ); + assert_eq!( + msg.faulty_points[2], 12, + "incorrect value for faulty_points[2], expected 12, is {}", + msg.faulty_points[2] + ); + assert_eq!( + msg.header.chain_id, 6, + "incorrect value for header.chain_id, expected 6, is {}", + msg.header.chain_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.obs_time.tow, 180, + "incorrect value for header.obs_time.tow, expected 180, is {}", + msg.header.obs_time.tow + ); + assert_eq!( + msg.header.obs_time.wn, 3, + "incorrect value for header.obs_time.wn, expected 3, is {}", + msg.header.obs_time.wn + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.ssr_sol_id, 3, + "incorrect value for header.ssr_sol_id, expected 3, is {}", + msg.header.ssr_sol_id + ); + assert_eq!( + msg.header.tile_id, 5, + "incorrect value for header.tile_id, expected 5, is {}", + msg.header.tile_id + ); + assert_eq!( + msg.header.tile_set_id, 4, + "incorrect value for header.tile_set_id, expected 4, is {}", + msg.header.tile_set_id + ); + assert_eq!( + msg.n_faulty_points, 3, + "incorrect value for n_faulty_points, expected 3, is {}", + msg.n_faulty_points + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrFlagTropoGridPoints"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_logging_msg_fwd.rs b/rust/sbp/tests/integration/auto_check_sbp_logging_msg_fwd.rs index e3da9dace5..2186a295ea 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_logging_msg_fwd.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_logging_msg_fwd.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_logging_msg_fwd() { { @@ -140,6 +145,13 @@ fn test_auto_check_sbp_logging_msg_fwd() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_logging_msg_fwd() { @@ -147,14 +159,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_fwd() { let json_input = r#"{"protocol": 0, "sender": 66, "msg_type": 1026, "source": 0, "fwd_payload": [ 86, 81, 68, 47, 81, 103, 65, 69, 65, 65, 65, 65, 65, 69, 97, 103 ], "crc": 24445, "length": 18, "preamble": 85, "payload": "AABWUUQvUWdBRUFBQUFBRWFn"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -273,3 +285,158 @@ fn test_json2sbp_auto_check_sbp_logging_msg_fwd() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_logging_msg_fwd`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_logging_msg_fwd() { + { + let mut payload = Cursor::new(vec![ + 85, 2, 4, 66, 0, 18, 0, 0, 86, 81, 68, 47, 81, 103, 65, 69, 65, 65, 65, 65, 65, 69, 97, + 103, 125, 95, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgFwd( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgFwd(msg) => { + assert_eq!( + msg.message_type(), + 0x402, + "Incorrect message type, expected 0x402, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.fwd_payload[0], 86, + "incorrect value for fwd_payload[0], expected 86, is {}", + msg.fwd_payload[0] + ); + assert_eq!( + msg.fwd_payload[1], 81, + "incorrect value for fwd_payload[1], expected 81, is {}", + msg.fwd_payload[1] + ); + assert_eq!( + msg.fwd_payload[2], 68, + "incorrect value for fwd_payload[2], expected 68, is {}", + msg.fwd_payload[2] + ); + assert_eq!( + msg.fwd_payload[3], 47, + "incorrect value for fwd_payload[3], expected 47, is {}", + msg.fwd_payload[3] + ); + assert_eq!( + msg.fwd_payload[4], 81, + "incorrect value for fwd_payload[4], expected 81, is {}", + msg.fwd_payload[4] + ); + assert_eq!( + msg.fwd_payload[5], 103, + "incorrect value for fwd_payload[5], expected 103, is {}", + msg.fwd_payload[5] + ); + assert_eq!( + msg.fwd_payload[6], 65, + "incorrect value for fwd_payload[6], expected 65, is {}", + msg.fwd_payload[6] + ); + assert_eq!( + msg.fwd_payload[7], 69, + "incorrect value for fwd_payload[7], expected 69, is {}", + msg.fwd_payload[7] + ); + assert_eq!( + msg.fwd_payload[8], 65, + "incorrect value for fwd_payload[8], expected 65, is {}", + msg.fwd_payload[8] + ); + assert_eq!( + msg.fwd_payload[9], 65, + "incorrect value for fwd_payload[9], expected 65, is {}", + msg.fwd_payload[9] + ); + assert_eq!( + msg.fwd_payload[10], 65, + "incorrect value for fwd_payload[10], expected 65, is {}", + msg.fwd_payload[10] + ); + assert_eq!( + msg.fwd_payload[11], 65, + "incorrect value for fwd_payload[11], expected 65, is {}", + msg.fwd_payload[11] + ); + assert_eq!( + msg.fwd_payload[12], 65, + "incorrect value for fwd_payload[12], expected 65, is {}", + msg.fwd_payload[12] + ); + assert_eq!( + msg.fwd_payload[13], 69, + "incorrect value for fwd_payload[13], expected 69, is {}", + msg.fwd_payload[13] + ); + assert_eq!( + msg.fwd_payload[14], 97, + "incorrect value for fwd_payload[14], expected 97, is {}", + msg.fwd_payload[14] + ); + assert_eq!( + msg.fwd_payload[15], 103, + "incorrect value for fwd_payload[15], expected 103, is {}", + msg.fwd_payload[15] + ); + assert_eq!( + msg.protocol, 0, + "incorrect value for protocol, expected 0, is {}", + msg.protocol + ); + assert_eq!( + msg.source, 0, + "incorrect value for source, expected 0, is {}", + msg.source + ); + } + _ => panic!("Invalid message type! Expected a MsgFwd"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_logging_msg_log.rs b/rust/sbp/tests/integration/auto_check_sbp_logging_msg_log.rs index abf815017e..88ffb00f56 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_logging_msg_log.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_logging_msg_log.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_logging_msg_log() { { @@ -63,6 +68,13 @@ fn test_auto_check_sbp_logging_msg_log() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_logging_msg_log() { @@ -70,14 +82,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_log() { let json_input = r#"{"level":6,"text":"Filtered all obs from 2314 at tow 83.539019","preamble":85,"msg_type":1025,"sender":2314,"payload":"BkZpbHRlcmVkIGFsbCBvYnMgZnJvbSAyMzE0IGF0IHRvdyA4My41MzkwMTk=","crc":41905,"length":44}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -118,3 +130,81 @@ fn test_json2sbp_auto_check_sbp_logging_msg_log() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_logging_msg_log`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_logging_msg_log() { + { + let mut payload = Cursor::new(vec![ + 85, 1, 4, 10, 9, 44, 6, 70, 105, 108, 116, 101, 114, 101, 100, 32, 97, 108, 108, 32, + 111, 98, 115, 32, 102, 114, 111, 109, 32, 50, 51, 49, 52, 32, 97, 116, 32, 116, 111, + 119, 32, 56, 51, 46, 53, 51, 57, 48, 49, 57, 177, 163, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgLog( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgLog(msg) => { + assert_eq!( + msg.message_type(), + 0x0401, + "Incorrect message type, expected 0x0401, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x90a, + "incorrect sender id, expected 0x90a, is {}", + sender_id + ); + assert_eq!( + msg.level, 6, + "incorrect value for level, expected 6, is {}", + msg.level + ); + assert_eq!( + msg.text.to_string(), + "Filtered all obs from 2314 at tow 83.539019".to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "Filtered all obs from 2314 at tow 83.539019".to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgLog"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_logging_msg_print_dep.rs b/rust/sbp/tests/integration/auto_check_sbp_logging_msg_print_dep.rs index 3d1ef85526..1d76cea9e9 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_logging_msg_print_dep.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_logging_msg_print_dep.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_logging_msg_print_dep() { { @@ -287,6 +292,13 @@ fn test_auto_check_sbp_logging_msg_print_dep() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { @@ -294,14 +306,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { let json_input = r#"{"sender": 8738, "msg_type": 16, "text": "INFO: acq: PRN 15 found @ -2497 Hz, 20 SNR\n", "crc": 26484, "length": 43, "preamble": 85, "payload": "SU5GTzogYWNxOiBQUk4gMTUgZm91bmQgQCAtMjQ5NyBIeiwgMjAgU05SCg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -344,14 +356,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { let json_input = r#"{"sender": 8738, "msg_type": 16, "text": "INFO: acq: PRN 31 found @ 4245 Hz, 21 SNR\n", "crc": 11148, "length": 42, "preamble": 85, "payload": "SU5GTzogYWNxOiBQUk4gMzEgZm91bmQgQCA0MjQ1IEh6LCAyMSBTTlIK"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -394,14 +406,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { let json_input = r#"{"sender": 8738, "msg_type": 16, "text": "INFO: Disabling channel 0 (PRN 11)\n", "crc": 36631, "length": 35, "preamble": 85, "payload": "SU5GTzogRGlzYWJsaW5nIGNoYW5uZWwgMCAoUFJOIDExKQo="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -444,14 +456,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { let json_input = r#"{"sender": 8738, "msg_type": 16, "text": "INFO: acq: PRN 2 found @ 3996 Hz, 20 SNR\n", "crc": 12527, "length": 41, "preamble": 85, "payload": "SU5GTzogYWNxOiBQUk4gMiBmb3VuZCBAIDM5OTYgSHosIDIwIFNOUgo="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -494,14 +506,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { let json_input = r#"{"sender": 8738, "msg_type": 16, "text": "INFO: acq: PRN 4 found @ -7492 Hz, 20 SNR\n", "crc": 63535, "length": 42, "preamble": 85, "payload": "SU5GTzogYWNxOiBQUk4gNCBmb3VuZCBAIC03NDkyIEh6LCAyMCBTTlIK"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -544,14 +556,14 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { let json_input = r#"{"sender": 8738, "msg_type": 16, "text": "INFO: Disabling channel 1 (PRN 15)\n", "crc": 35742, "length": 35, "preamble": 85, "payload": "SU5GTzogRGlzYWJsaW5nIGNoYW5uZWwgMSAoUFJOIDE1KQo="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -591,3 +603,400 @@ fn test_json2sbp_auto_check_sbp_logging_msg_print_dep() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_logging_msg_print_dep`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_logging_msg_print_dep() { + { + let mut payload = Cursor::new(vec![ + 85, 16, 0, 34, 34, 43, 73, 78, 70, 79, 58, 32, 97, 99, 113, 58, 32, 80, 82, 78, 32, 49, + 53, 32, 102, 111, 117, 110, 100, 32, 64, 32, 45, 50, 52, 57, 55, 32, 72, 122, 44, 32, + 50, 48, 32, 83, 78, 82, 10, 116, 103, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPrintDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPrintDep(msg) => { + assert_eq!( + msg.message_type(), + 0x10, + "Incorrect message type, expected 0x10, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x2222, + "incorrect sender id, expected 0x2222, is {}", + sender_id + ); + assert_eq!( + msg.text.to_string(), + "INFO: acq: PRN 15 found @ -2497 Hz, 20 SNR +" + .to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "INFO: acq: PRN 15 found @ -2497 Hz, 20 SNR +" + .to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgPrintDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 16, 0, 34, 34, 42, 73, 78, 70, 79, 58, 32, 97, 99, 113, 58, 32, 80, 82, 78, 32, 51, + 49, 32, 102, 111, 117, 110, 100, 32, 64, 32, 52, 50, 52, 53, 32, 72, 122, 44, 32, 50, + 49, 32, 83, 78, 82, 10, 140, 43, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPrintDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPrintDep(msg) => { + assert_eq!( + msg.message_type(), + 0x10, + "Incorrect message type, expected 0x10, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x2222, + "incorrect sender id, expected 0x2222, is {}", + sender_id + ); + assert_eq!( + msg.text.to_string(), + "INFO: acq: PRN 31 found @ 4245 Hz, 21 SNR +" + .to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "INFO: acq: PRN 31 found @ 4245 Hz, 21 SNR +" + .to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgPrintDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 16, 0, 34, 34, 35, 73, 78, 70, 79, 58, 32, 68, 105, 115, 97, 98, 108, 105, 110, + 103, 32, 99, 104, 97, 110, 110, 101, 108, 32, 48, 32, 40, 80, 82, 78, 32, 49, 49, 41, + 10, 23, 143, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPrintDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPrintDep(msg) => { + assert_eq!( + msg.message_type(), + 0x10, + "Incorrect message type, expected 0x10, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x2222, + "incorrect sender id, expected 0x2222, is {}", + sender_id + ); + assert_eq!( + msg.text.to_string(), + "INFO: Disabling channel 0 (PRN 11) +" + .to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "INFO: Disabling channel 0 (PRN 11) +" + .to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgPrintDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 16, 0, 34, 34, 41, 73, 78, 70, 79, 58, 32, 97, 99, 113, 58, 32, 80, 82, 78, 32, 50, + 32, 102, 111, 117, 110, 100, 32, 64, 32, 51, 57, 57, 54, 32, 72, 122, 44, 32, 50, 48, + 32, 83, 78, 82, 10, 239, 48, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPrintDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPrintDep(msg) => { + assert_eq!( + msg.message_type(), + 0x10, + "Incorrect message type, expected 0x10, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x2222, + "incorrect sender id, expected 0x2222, is {}", + sender_id + ); + assert_eq!( + msg.text.to_string(), + "INFO: acq: PRN 2 found @ 3996 Hz, 20 SNR +" + .to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "INFO: acq: PRN 2 found @ 3996 Hz, 20 SNR +" + .to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgPrintDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 16, 0, 34, 34, 42, 73, 78, 70, 79, 58, 32, 97, 99, 113, 58, 32, 80, 82, 78, 32, 52, + 32, 102, 111, 117, 110, 100, 32, 64, 32, 45, 55, 52, 57, 50, 32, 72, 122, 44, 32, 50, + 48, 32, 83, 78, 82, 10, 47, 248, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPrintDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPrintDep(msg) => { + assert_eq!( + msg.message_type(), + 0x10, + "Incorrect message type, expected 0x10, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x2222, + "incorrect sender id, expected 0x2222, is {}", + sender_id + ); + assert_eq!( + msg.text.to_string(), + "INFO: acq: PRN 4 found @ -7492 Hz, 20 SNR +" + .to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "INFO: acq: PRN 4 found @ -7492 Hz, 20 SNR +" + .to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgPrintDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 16, 0, 34, 34, 35, 73, 78, 70, 79, 58, 32, 68, 105, 115, 97, 98, 108, 105, 110, + 103, 32, 99, 104, 97, 110, 110, 101, 108, 32, 49, 32, 40, 80, 82, 78, 32, 49, 53, 41, + 10, 158, 139, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPrintDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPrintDep(msg) => { + assert_eq!( + msg.message_type(), + 0x10, + "Incorrect message type, expected 0x10, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x2222, + "incorrect sender id, expected 0x2222, is {}", + sender_id + ); + assert_eq!( + msg.text.to_string(), + "INFO: Disabling channel 1 (PRN 15) +" + .to_string(), + "incorrect value for msg.text, expected string '{}', is '{}'", + "INFO: Disabling channel 1 (PRN 15) +" + .to_string(), + msg.text + ); + } + _ => panic!("Invalid message type! Expected a MsgPrintDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_age_corrections.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_age_corrections.rs index 2bd4f47c43..d48c0bd675 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_age_corrections.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_age_corrections.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_age_corrections() { { @@ -57,6 +62,13 @@ fn test_auto_check_sbp_navigation_msg_age_corrections() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_age_corrections() { @@ -64,14 +76,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_age_corrections() { let json_input = r#"{"sender": 66, "msg_type": 528, "age": 30, "tow": 100, "crc": 51945, "length": 6, "preamble": 85, "payload": "ZAAAAB4A"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -110,3 +122,75 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_age_corrections() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_age_corrections`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_age_corrections() { + { + let mut payload = Cursor::new(vec![85, 16, 2, 66, 0, 6, 100, 0, 0, 0, 30, 0, 233, 202]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAgeCorrections( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAgeCorrections(msg) => { + assert_eq!( + msg.message_type(), + 0x210, + "Incorrect message type, expected 0x210, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.age, 30, + "incorrect value for age, expected 30, is {}", + msg.age + ); + assert_eq!( + msg.tow, 100, + "incorrect value for tow, expected 100, is {}", + msg.tow + ); + } + _ => panic!("Invalid message type! Expected a MsgAgeCorrections"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef.rs index bfaac917be..cf5e6cdc46 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_baseline_ecef() { { @@ -357,6 +362,13 @@ fn test_auto_check_sbp_navigation_msg_baseline_ecef() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { @@ -364,14 +376,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { let json_input = r#"{"n_sats": 14, "sender": 35027, "msg_type": 523, "tow": 326825000, "crc": 49115, "length": 20, "flags": 0, "y": 1327294, "x": -1154410, "z": 631798, "preamble": 85, "payload": "KPR6E5Zi7v++QBQA9qMJAAAADgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -438,14 +450,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 523, "tow": 326826000, "crc": 20208, "length": 20, "flags": 0, "y": 1327551, "x": -1154232, "z": 631434, "preamble": 85, "payload": "EPh6E0hj7v+/QRQAiqIJAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -512,14 +524,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 523, "tow": 326827000, "crc": 33796, "length": 20, "flags": 0, "y": 1327541, "x": -1154263, "z": 631188, "preamble": 85, "payload": "+Pt6Eylj7v+1QRQAlKEJAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -586,14 +598,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 523, "tow": 326828000, "crc": 24131, "length": 20, "flags": 0, "y": 1327185, "x": -1154628, "z": 630849, "preamble": 85, "payload": "4P96E7xh7v9RQBQAQaAJAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -660,14 +672,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 523, "tow": 326829000, "crc": 24170, "length": 20, "flags": 0, "y": 1326941, "x": -1154883, "z": 630626, "preamble": 85, "payload": "yAN7E71g7v9dPxQAYp8JAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -731,3 +743,451 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_baseline_ecef`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_baseline_ecef() { + { + let mut payload = Cursor::new(vec![ + 85, 11, 2, 211, 136, 20, 40, 244, 122, 19, 150, 98, 238, 255, 190, 64, 20, 0, 246, 163, + 9, 0, 0, 0, 14, 0, 219, 191, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20b, + "Incorrect message type, expected 0x20b, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 14, + "incorrect value for n_sats, expected 14, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -1154410, + "incorrect value for x, expected -1154410, is {}", + msg.x + ); + assert_eq!( + msg.y, 1327294, + "incorrect value for y, expected 1327294, is {}", + msg.y + ); + assert_eq!( + msg.z, 631798, + "incorrect value for z, expected 631798, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 11, 2, 211, 136, 20, 16, 248, 122, 19, 72, 99, 238, 255, 191, 65, 20, 0, 138, 162, + 9, 0, 0, 0, 15, 0, 240, 78, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20b, + "Incorrect message type, expected 0x20b, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -1154232, + "incorrect value for x, expected -1154232, is {}", + msg.x + ); + assert_eq!( + msg.y, 1327551, + "incorrect value for y, expected 1327551, is {}", + msg.y + ); + assert_eq!( + msg.z, 631434, + "incorrect value for z, expected 631434, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 11, 2, 211, 136, 20, 248, 251, 122, 19, 41, 99, 238, 255, 181, 65, 20, 0, 148, 161, + 9, 0, 0, 0, 15, 0, 4, 132, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20b, + "Incorrect message type, expected 0x20b, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -1154263, + "incorrect value for x, expected -1154263, is {}", + msg.x + ); + assert_eq!( + msg.y, 1327541, + "incorrect value for y, expected 1327541, is {}", + msg.y + ); + assert_eq!( + msg.z, 631188, + "incorrect value for z, expected 631188, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 11, 2, 211, 136, 20, 224, 255, 122, 19, 188, 97, 238, 255, 81, 64, 20, 0, 65, 160, + 9, 0, 0, 0, 15, 0, 67, 94, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20b, + "Incorrect message type, expected 0x20b, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326828000, + "incorrect value for tow, expected 326828000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -1154628, + "incorrect value for x, expected -1154628, is {}", + msg.x + ); + assert_eq!( + msg.y, 1327185, + "incorrect value for y, expected 1327185, is {}", + msg.y + ); + assert_eq!( + msg.z, 630849, + "incorrect value for z, expected 630849, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 11, 2, 211, 136, 20, 200, 3, 123, 19, 189, 96, 238, 255, 93, 63, 20, 0, 98, 159, 9, + 0, 0, 0, 15, 0, 106, 94, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20b, + "Incorrect message type, expected 0x20b, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326829000, + "incorrect value for tow, expected 326829000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -1154883, + "incorrect value for x, expected -1154883, is {}", + msg.x + ); + assert_eq!( + msg.y, 1326941, + "incorrect value for y, expected 1326941, is {}", + msg.y + ); + assert_eq!( + msg.z, 630626, + "incorrect value for z, expected 630626, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef_dep_a.rs index 7844118b90..9133266793 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ecef_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { { @@ -765,6 +770,13 @@ fn test_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { @@ -772,14 +784,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 514, "tow": 2567700, "crc": 35122, "length": 20, "flags": 1, "y": -35532, "x": -53227, "z": -76840, "preamble": 85, "payload": "FC4nABUw//80df//2NP+/wAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -846,14 +858,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 514, "tow": 2567800, "crc": 39907, "length": 20, "flags": 1, "y": -35791, "x": -52934, "z": -76922, "preamble": 85, "payload": "eC4nADox//8xdP//htP+/wAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -920,14 +932,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 514, "tow": 2567900, "crc": 32317, "length": 20, "flags": 1, "y": -36049, "x": -52639, "z": -77004, "preamble": 85, "payload": "3C4nAGEy//8vc///NNP+/wAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -994,14 +1006,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 514, "tow": 2568000, "crc": 20424, "length": 20, "flags": 1, "y": -36307, "x": -52344, "z": -77084, "preamble": 85, "payload": "QC8nAIgz//8tcv//5NL+/wAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1068,14 +1080,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 514, "tow": 2568100, "crc": 6248, "length": 20, "flags": 1, "y": -36564, "x": -52048, "z": -77163, "preamble": 85, "payload": "pC8nALA0//8scf//ldL+/wAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1142,14 +1154,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 6, "sender": 1219, "msg_type": 514, "tow": 407180700, "crc": 43154, "length": 20, "flags": 0, "y": -12186, "x": -6231, "z": 7419, "preamble": 85, "payload": "nBVFGKnn//9m0P//+xwAAAAABgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1216,14 +1228,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 6, "sender": 1219, "msg_type": 514, "tow": 407180800, "crc": 29730, "length": 20, "flags": 0, "y": -12185, "x": -6231, "z": 7420, "preamble": 85, "payload": "ABZFGKnn//9n0P///BwAAAAABgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1290,14 +1302,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 6, "sender": 1219, "msg_type": 514, "tow": 407180900, "crc": 4065, "length": 20, "flags": 0, "y": -18496, "x": -8162, "z": 13807, "preamble": 85, "payload": "ZBZFGB7g///At///7zUAAAAABgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1364,14 +1376,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 6, "sender": 1219, "msg_type": 514, "tow": 407181000, "crc": 25635, "length": 20, "flags": 0, "y": -18497, "x": -8164, "z": 13810, "preamble": 85, "payload": "yBZFGBzg//+/t///8jUAAAAABgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1438,14 +1450,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 6, "sender": 1219, "msg_type": 514, "tow": 407181100, "crc": 16962, "length": 20, "flags": 0, "y": -15591, "x": -7400, "z": 15257, "preamble": 85, "payload": "LBdFGBjj//8Zw///mTsAAAAABgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1512,14 +1524,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { let json_input = r#"{"n_sats": 6, "sender": 1219, "msg_type": 514, "tow": 407181200, "crc": 34595, "length": 20, "flags": 0, "y": -15591, "x": -7401, "z": 15257, "preamble": 85, "payload": "kBdFGBfj//8Zw///mTsAAAAABgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1583,3 +1595,973 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_baseline_ecef_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_baseline_ecef_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 246, 215, 20, 20, 46, 39, 0, 21, 48, 255, 255, 52, 117, 255, 255, 216, 211, + 254, 255, 0, 0, 9, 1, 50, 137, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert_eq!( + msg.x, -53227, + "incorrect value for x, expected -53227, is {}", + msg.x + ); + assert_eq!( + msg.y, -35532, + "incorrect value for y, expected -35532, is {}", + msg.y + ); + assert_eq!( + msg.z, -76840, + "incorrect value for z, expected -76840, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 246, 215, 20, 120, 46, 39, 0, 58, 49, 255, 255, 49, 116, 255, 255, 134, 211, + 254, 255, 0, 0, 9, 1, 227, 155, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.x, -52934, + "incorrect value for x, expected -52934, is {}", + msg.x + ); + assert_eq!( + msg.y, -35791, + "incorrect value for y, expected -35791, is {}", + msg.y + ); + assert_eq!( + msg.z, -76922, + "incorrect value for z, expected -76922, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 246, 215, 20, 220, 46, 39, 0, 97, 50, 255, 255, 47, 115, 255, 255, 52, 211, + 254, 255, 0, 0, 9, 1, 61, 126, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert_eq!( + msg.x, -52639, + "incorrect value for x, expected -52639, is {}", + msg.x + ); + assert_eq!( + msg.y, -36049, + "incorrect value for y, expected -36049, is {}", + msg.y + ); + assert_eq!( + msg.z, -77004, + "incorrect value for z, expected -77004, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 246, 215, 20, 64, 47, 39, 0, 136, 51, 255, 255, 45, 114, 255, 255, 228, 210, + 254, 255, 0, 0, 9, 1, 200, 79, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568000, + "incorrect value for tow, expected 2568000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -52344, + "incorrect value for x, expected -52344, is {}", + msg.x + ); + assert_eq!( + msg.y, -36307, + "incorrect value for y, expected -36307, is {}", + msg.y + ); + assert_eq!( + msg.z, -77084, + "incorrect value for z, expected -77084, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 246, 215, 20, 164, 47, 39, 0, 176, 52, 255, 255, 44, 113, 255, 255, 149, 210, + 254, 255, 0, 0, 9, 1, 104, 24, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568100, + "incorrect value for tow, expected 2568100, is {}", + msg.tow + ); + assert_eq!( + msg.x, -52048, + "incorrect value for x, expected -52048, is {}", + msg.x + ); + assert_eq!( + msg.y, -36564, + "incorrect value for y, expected -36564, is {}", + msg.y + ); + assert_eq!( + msg.z, -77163, + "incorrect value for z, expected -77163, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 195, 4, 20, 156, 21, 69, 24, 169, 231, 255, 255, 102, 208, 255, 255, 251, 28, + 0, 0, 0, 0, 6, 0, 146, 168, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407180700, + "incorrect value for tow, expected 407180700, is {}", + msg.tow + ); + assert_eq!( + msg.x, -6231, + "incorrect value for x, expected -6231, is {}", + msg.x + ); + assert_eq!( + msg.y, -12186, + "incorrect value for y, expected -12186, is {}", + msg.y + ); + assert_eq!( + msg.z, 7419, + "incorrect value for z, expected 7419, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 195, 4, 20, 0, 22, 69, 24, 169, 231, 255, 255, 103, 208, 255, 255, 252, 28, + 0, 0, 0, 0, 6, 0, 34, 116, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407180800, + "incorrect value for tow, expected 407180800, is {}", + msg.tow + ); + assert_eq!( + msg.x, -6231, + "incorrect value for x, expected -6231, is {}", + msg.x + ); + assert_eq!( + msg.y, -12185, + "incorrect value for y, expected -12185, is {}", + msg.y + ); + assert_eq!( + msg.z, 7420, + "incorrect value for z, expected 7420, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 195, 4, 20, 100, 22, 69, 24, 30, 224, 255, 255, 192, 183, 255, 255, 239, 53, + 0, 0, 0, 0, 6, 0, 225, 15, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407180900, + "incorrect value for tow, expected 407180900, is {}", + msg.tow + ); + assert_eq!( + msg.x, -8162, + "incorrect value for x, expected -8162, is {}", + msg.x + ); + assert_eq!( + msg.y, -18496, + "incorrect value for y, expected -18496, is {}", + msg.y + ); + assert_eq!( + msg.z, 13807, + "incorrect value for z, expected 13807, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 195, 4, 20, 200, 22, 69, 24, 28, 224, 255, 255, 191, 183, 255, 255, 242, 53, + 0, 0, 0, 0, 6, 0, 35, 100, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407181000, + "incorrect value for tow, expected 407181000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -8164, + "incorrect value for x, expected -8164, is {}", + msg.x + ); + assert_eq!( + msg.y, -18497, + "incorrect value for y, expected -18497, is {}", + msg.y + ); + assert_eq!( + msg.z, 13810, + "incorrect value for z, expected 13810, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 195, 4, 20, 44, 23, 69, 24, 24, 227, 255, 255, 25, 195, 255, 255, 153, 59, 0, + 0, 0, 0, 6, 0, 66, 66, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407181100, + "incorrect value for tow, expected 407181100, is {}", + msg.tow + ); + assert_eq!( + msg.x, -7400, + "incorrect value for x, expected -7400, is {}", + msg.x + ); + assert_eq!( + msg.y, -15591, + "incorrect value for y, expected -15591, is {}", + msg.y + ); + assert_eq!( + msg.z, 15257, + "incorrect value for z, expected 15257, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 2, 195, 4, 20, 144, 23, 69, 24, 23, 227, 255, 255, 25, 195, 255, 255, 153, 59, + 0, 0, 0, 0, 6, 0, 35, 135, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x202, + "Incorrect message type, expected 0x202, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407181200, + "incorrect value for tow, expected 407181200, is {}", + msg.tow + ); + assert_eq!( + msg.x, -7401, + "incorrect value for x, expected -7401, is {}", + msg.x + ); + assert_eq!( + msg.y, -15591, + "incorrect value for y, expected -15591, is {}", + msg.y + ); + assert_eq!( + msg.z, 15257, + "incorrect value for z, expected 15257, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned.rs index d62113885e..18ee959ca3 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_baseline_ned() { { @@ -382,6 +387,13 @@ fn test_auto_check_sbp_navigation_msg_baseline_ned() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { @@ -389,14 +401,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { let json_input = r#"{"v_accuracy": 0, "e": -1681229, "sender": 35027, "msg_type": 524, "tow": 326825000, "n": 816073, "crc": 18146, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 14, "preamble": 85, "payload": "KPR6E8lzDACzWOb/mX0AAAAAAAAOAA==", "d": 32153}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -468,14 +480,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { let json_input = r#"{"v_accuracy": 0, "e": -1681214, "sender": 35027, "msg_type": 524, "tow": 326826000, "n": 815970, "crc": 23877, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "EPh6E2JzDADCWOb/bn8AAAAAAAAPAA==", "d": 32622}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -547,14 +559,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { let json_input = r#"{"v_accuracy": 0, "e": -1681235, "sender": 35027, "msg_type": 524, "tow": 326827000, "n": 815759, "crc": 43474, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "+Pt6E49yDACtWOb/7n8AAAAAAAAPAA==", "d": 32750}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -626,14 +638,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { let json_input = r#"{"v_accuracy": 0, "e": -1681357, "sender": 35027, "msg_type": 524, "tow": 326828000, "n": 815190, "crc": 27527, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "4P96E1ZwDAAzWOb/L38AAAAAAAAPAA==", "d": 32559}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -705,14 +717,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { let json_input = r#"{"v_accuracy": 0, "e": -1681444, "sender": 35027, "msg_type": 524, "tow": 326829000, "n": 814806, "crc": 20670, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "yAN7E9ZuDADcV+b/pX4AAAAAAAAPAA==", "d": 32421}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -781,3 +793,476 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_baseline_ned`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_baseline_ned() { + { + let mut payload = Cursor::new(vec![ + 85, 12, 2, 211, 136, 22, 40, 244, 122, 19, 201, 115, 12, 0, 179, 88, 230, 255, 153, + 125, 0, 0, 0, 0, 0, 0, 14, 0, 226, 70, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20c, + "Incorrect message type, expected 0x20c, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, 32153, + "incorrect value for d, expected 32153, is {}", + msg.d + ); + assert_eq!( + msg.e, -1681229, + "incorrect value for e, expected -1681229, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 816073, + "incorrect value for n, expected 816073, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 14, + "incorrect value for n_sats, expected 14, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 12, 2, 211, 136, 22, 16, 248, 122, 19, 98, 115, 12, 0, 194, 88, 230, 255, 110, 127, + 0, 0, 0, 0, 0, 0, 15, 0, 69, 93, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20c, + "Incorrect message type, expected 0x20c, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, 32622, + "incorrect value for d, expected 32622, is {}", + msg.d + ); + assert_eq!( + msg.e, -1681214, + "incorrect value for e, expected -1681214, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 815970, + "incorrect value for n, expected 815970, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 12, 2, 211, 136, 22, 248, 251, 122, 19, 143, 114, 12, 0, 173, 88, 230, 255, 238, + 127, 0, 0, 0, 0, 0, 0, 15, 0, 210, 169, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20c, + "Incorrect message type, expected 0x20c, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, 32750, + "incorrect value for d, expected 32750, is {}", + msg.d + ); + assert_eq!( + msg.e, -1681235, + "incorrect value for e, expected -1681235, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 815759, + "incorrect value for n, expected 815759, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 12, 2, 211, 136, 22, 224, 255, 122, 19, 86, 112, 12, 0, 51, 88, 230, 255, 47, 127, + 0, 0, 0, 0, 0, 0, 15, 0, 135, 107, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20c, + "Incorrect message type, expected 0x20c, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, 32559, + "incorrect value for d, expected 32559, is {}", + msg.d + ); + assert_eq!( + msg.e, -1681357, + "incorrect value for e, expected -1681357, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 815190, + "incorrect value for n, expected 815190, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326828000, + "incorrect value for tow, expected 326828000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 12, 2, 211, 136, 22, 200, 3, 123, 19, 214, 110, 12, 0, 220, 87, 230, 255, 165, 126, + 0, 0, 0, 0, 0, 0, 15, 0, 190, 80, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20c, + "Incorrect message type, expected 0x20c, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, 32421, + "incorrect value for d, expected 32421, is {}", + msg.d + ); + assert_eq!( + msg.e, -1681444, + "incorrect value for e, expected -1681444, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 814806, + "incorrect value for n, expected 814806, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326829000, + "incorrect value for tow, expected 326829000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned_dep_a.rs index 8943df5274..49d5631b79 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_baseline_ned_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { { @@ -800,6 +805,13 @@ fn test_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { @@ -807,14 +819,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -26134, "sender": 55286, "msg_type": 515, "tow": 2567700, "n": -96525, "crc": 49501, "length": 22, "flags": 1, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "FC4nAPOG/v/qmf//AAAAAAAAAAAJAQ==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -882,14 +894,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -25747, "sender": 55286, "msg_type": 515, "tow": 2567800, "n": -96629, "crc": 10022, "length": 22, "flags": 1, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "eC4nAIuG/v9tm///AAAAAAAAAAAJAQ==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -957,14 +969,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -25360, "sender": 55286, "msg_type": 515, "tow": 2567900, "n": -96731, "crc": 34106, "length": 22, "flags": 1, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "3C4nACWG/v/wnP//AAAAAAAAAAAJAQ==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1032,14 +1044,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -24973, "sender": 55286, "msg_type": 515, "tow": 2568000, "n": -96831, "crc": 54840, "length": 22, "flags": 1, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "QC8nAMGF/v9znv//AAAAAAAAAAAJAQ==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1107,14 +1119,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -24586, "sender": 55286, "msg_type": 515, "tow": 2568100, "n": -96931, "crc": 62698, "length": 22, "flags": 1, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "pC8nAF2F/v/2n///AAAAAAAAAAAJAQ==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1182,14 +1194,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 1265, "sender": 1219, "msg_type": 515, "tow": 407180700, "n": -2430, "crc": 5626, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 6, "preamble": 85, "payload": "nBVFGIL2///xBAAAI8T//wAAAAAGAA==", "d": -15325}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1261,14 +1273,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 1265, "sender": 1219, "msg_type": 515, "tow": 407180800, "n": -2430, "crc": 34288, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 6, "preamble": 85, "payload": "ABZFGIL2///xBAAAI8T//wAAAAAGAA==", "d": -15325}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1340,14 +1352,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3015, "sender": 1219, "msg_type": 515, "tow": 407180900, "n": -1248, "crc": 46348, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 6, "preamble": 85, "payload": "ZBZFGCD7///HCwAAOaH//wAAAAAGAA==", "d": -24263}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1419,14 +1431,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3015, "sender": 1219, "msg_type": 515, "tow": 407181000, "n": -1247, "crc": 14934, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 6, "preamble": 85, "payload": "yBZFGCH7///HCwAANqH//wAAAAAGAA==", "d": -24266}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1498,14 +1510,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 2103, "sender": 1219, "msg_type": 515, "tow": 407181100, "n": 1646, "crc": 63795, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 6, "preamble": 85, "payload": "LBdFGG4GAAA3CAAAoKb//wAAAAAGAA==", "d": -22880}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1577,14 +1589,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 2102, "sender": 1219, "msg_type": 515, "tow": 407181200, "n": 1646, "crc": 5838, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 6, "preamble": 85, "payload": "kBdFGG4GAAA2CAAAoKb//wAAAAAGAA==", "d": -22880}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1653,3 +1665,1008 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_baseline_ned_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_baseline_ned_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 246, 215, 22, 20, 46, 39, 0, 243, 134, 254, 255, 234, 153, 255, 255, 0, 0, 0, + 0, 0, 0, 0, 0, 9, 1, 93, 193, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, -26134, + "incorrect value for e, expected -26134, is {}", + msg.e + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -96525, + "incorrect value for n, expected -96525, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 246, 215, 22, 120, 46, 39, 0, 139, 134, 254, 255, 109, 155, 255, 255, 0, 0, + 0, 0, 0, 0, 0, 0, 9, 1, 38, 39, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, -25747, + "incorrect value for e, expected -25747, is {}", + msg.e + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -96629, + "incorrect value for n, expected -96629, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 246, 215, 22, 220, 46, 39, 0, 37, 134, 254, 255, 240, 156, 255, 255, 0, 0, 0, + 0, 0, 0, 0, 0, 9, 1, 58, 133, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, -25360, + "incorrect value for e, expected -25360, is {}", + msg.e + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -96731, + "incorrect value for n, expected -96731, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 246, 215, 22, 64, 47, 39, 0, 193, 133, 254, 255, 115, 158, 255, 255, 0, 0, 0, + 0, 0, 0, 0, 0, 9, 1, 56, 214, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, -24973, + "incorrect value for e, expected -24973, is {}", + msg.e + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -96831, + "incorrect value for n, expected -96831, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568000, + "incorrect value for tow, expected 2568000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 246, 215, 22, 164, 47, 39, 0, 93, 133, 254, 255, 246, 159, 255, 255, 0, 0, 0, + 0, 0, 0, 0, 0, 9, 1, 234, 244, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, -24586, + "incorrect value for e, expected -24586, is {}", + msg.e + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -96931, + "incorrect value for n, expected -96931, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568100, + "incorrect value for tow, expected 2568100, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 195, 4, 22, 156, 21, 69, 24, 130, 246, 255, 255, 241, 4, 0, 0, 35, 196, 255, + 255, 0, 0, 0, 0, 6, 0, 250, 21, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -15325, + "incorrect value for d, expected -15325, is {}", + msg.d + ); + assert_eq!( + msg.e, 1265, + "incorrect value for e, expected 1265, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -2430, + "incorrect value for n, expected -2430, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407180700, + "incorrect value for tow, expected 407180700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 195, 4, 22, 0, 22, 69, 24, 130, 246, 255, 255, 241, 4, 0, 0, 35, 196, 255, + 255, 0, 0, 0, 0, 6, 0, 240, 133, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -15325, + "incorrect value for d, expected -15325, is {}", + msg.d + ); + assert_eq!( + msg.e, 1265, + "incorrect value for e, expected 1265, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -2430, + "incorrect value for n, expected -2430, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407180800, + "incorrect value for tow, expected 407180800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 195, 4, 22, 100, 22, 69, 24, 32, 251, 255, 255, 199, 11, 0, 0, 57, 161, 255, + 255, 0, 0, 0, 0, 6, 0, 12, 181, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -24263, + "incorrect value for d, expected -24263, is {}", + msg.d + ); + assert_eq!( + msg.e, 3015, + "incorrect value for e, expected 3015, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -1248, + "incorrect value for n, expected -1248, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407180900, + "incorrect value for tow, expected 407180900, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 195, 4, 22, 200, 22, 69, 24, 33, 251, 255, 255, 199, 11, 0, 0, 54, 161, 255, + 255, 0, 0, 0, 0, 6, 0, 86, 58, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -24266, + "incorrect value for d, expected -24266, is {}", + msg.d + ); + assert_eq!( + msg.e, 3015, + "incorrect value for e, expected 3015, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -1247, + "incorrect value for n, expected -1247, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407181000, + "incorrect value for tow, expected 407181000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 195, 4, 22, 44, 23, 69, 24, 110, 6, 0, 0, 55, 8, 0, 0, 160, 166, 255, 255, 0, + 0, 0, 0, 6, 0, 51, 249, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -22880, + "incorrect value for d, expected -22880, is {}", + msg.d + ); + assert_eq!( + msg.e, 2103, + "incorrect value for e, expected 2103, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 1646, + "incorrect value for n, expected 1646, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407181100, + "incorrect value for tow, expected 407181100, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 3, 2, 195, 4, 22, 144, 23, 69, 24, 110, 6, 0, 0, 54, 8, 0, 0, 160, 166, 255, 255, + 0, 0, 0, 0, 6, 0, 206, 22, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBaselineNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBaselineNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x203, + "Incorrect message type, expected 0x203, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -22880, + "incorrect value for d, expected -22880, is {}", + msg.d + ); + assert_eq!( + msg.e, 2102, + "incorrect value for e, expected 2102, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 1646, + "incorrect value for n, expected 1646, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 6, + "incorrect value for n_sats, expected 6, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407181200, + "incorrect value for tow, expected 407181200, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgBaselineNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops.rs index 7017a36ad7..e583353f5d 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_dops() { { @@ -84,6 +89,13 @@ fn test_auto_check_sbp_navigation_msg_dops() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_dops() { @@ -91,14 +103,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops() { let json_input = r#"{"gdop": 2, "tdop": 5, "vdop": 5, "sender": 66, "msg_type": 520, "pdop": 6, "tow": 100, "crc": 1268, "length": 15, "flags": 0, "preamble": 85, "payload": "ZAAAAAIABgAFAAUABQAA", "hdop": 5}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -162,3 +174,102 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_dops`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_dops() { + { + let mut payload = Cursor::new(vec![ + 85, 8, 2, 66, 0, 15, 100, 0, 0, 0, 2, 0, 6, 0, 5, 0, 5, 0, 5, 0, 0, 244, 4, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDops( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDops(msg) => { + assert_eq!( + msg.message_type(), + 0x208, + "Incorrect message type, expected 0x208, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.gdop, 2, + "incorrect value for gdop, expected 2, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 5, + "incorrect value for hdop, expected 5, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 6, + "incorrect value for pdop, expected 6, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 5, + "incorrect value for tdop, expected 5, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 100, + "incorrect value for tow, expected 100, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 5, + "incorrect value for vdop, expected 5, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDops"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops_dep_a.rs index d6b2aaed9f..2fad058d46 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_dops_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_dops_dep_a() { { @@ -575,6 +580,13 @@ fn test_auto_check_sbp_navigation_msg_dops_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { @@ -582,14 +594,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 180, "tdop": 170, "vdop": 150, "sender": 55286, "msg_type": 518, "pdop": 190, "tow": 2568200, "crc": 43641, "length": 14, "preamble": 85, "payload": "CDAnALQAvgCqAKAAlgA=", "hdop": 160}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -651,14 +663,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 180, "tdop": 170, "vdop": 150, "sender": 55286, "msg_type": 518, "pdop": 190, "tow": 2569200, "crc": 43342, "length": 14, "preamble": 85, "payload": "8DMnALQAvgCqAKAAlgA=", "hdop": 160}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -720,14 +732,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 180, "tdop": 170, "vdop": 150, "sender": 55286, "msg_type": 518, "pdop": 190, "tow": 2570200, "crc": 55879, "length": 14, "preamble": 85, "payload": "2DcnALQAvgCqAKAAlgA=", "hdop": 160}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -789,14 +801,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 247, "tdop": 123, "vdop": 44, "sender": 1219, "msg_type": 518, "tow": 407084500, "pdop": 215, "crc": 5582, "length": 14, "preamble": 85, "payload": "1J1DGPcA1wB7ABEBLAA=", "hdop": 273}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -858,14 +870,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 65535, "tdop": 0, "vdop": 0, "sender": 1219, "msg_type": 518, "tow": 0, "pdop": 65535, "crc": 3218, "length": 14, "preamble": 85, "payload": "AAAAAP////8AAAAAAAA=", "hdop": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -927,14 +939,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 348, "tdop": 155, "vdop": 113, "sender": 1219, "msg_type": 518, "tow": 407152000, "pdop": 312, "crc": 23937, "length": 14, "preamble": 85, "payload": "gKVEGFwBOAGbAH0CcQA=", "hdop": 637}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -996,14 +1008,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 348, "tdop": 155, "vdop": 113, "sender": 1219, "msg_type": 518, "tow": 407153000, "pdop": 311, "crc": 32977, "length": 14, "preamble": 85, "payload": "aKlEGFwBNwGbAH0CcQA=", "hdop": 637}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1065,14 +1077,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 348, "tdop": 155, "vdop": 112, "sender": 1219, "msg_type": 518, "tow": 407154000, "pdop": 311, "crc": 1566, "length": 14, "preamble": 85, "payload": "UK1EGFwBNwGbAH0CcAA=", "hdop": 637}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1134,14 +1146,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { let json_input = r#"{"gdop": 348, "tdop": 155, "vdop": 112, "sender": 1219, "msg_type": 518, "tow": 407155000, "pdop": 311, "crc": 17222, "length": 14, "preamble": 85, "payload": "OLFEGFwBNwGbAH0CcAA=", "hdop": 637}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1200,3 +1212,745 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_dops_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_dops_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_dops_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 246, 215, 14, 8, 48, 39, 0, 180, 0, 190, 0, 170, 0, 160, 0, 150, 0, 121, 170, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 180, + "incorrect value for gdop, expected 180, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 160, + "incorrect value for hdop, expected 160, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 190, + "incorrect value for pdop, expected 190, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 170, + "incorrect value for tdop, expected 170, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 2568200, + "incorrect value for tow, expected 2568200, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 150, + "incorrect value for vdop, expected 150, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 246, 215, 14, 240, 51, 39, 0, 180, 0, 190, 0, 170, 0, 160, 0, 150, 0, 78, 169, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 180, + "incorrect value for gdop, expected 180, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 160, + "incorrect value for hdop, expected 160, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 190, + "incorrect value for pdop, expected 190, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 170, + "incorrect value for tdop, expected 170, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 2569200, + "incorrect value for tow, expected 2569200, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 150, + "incorrect value for vdop, expected 150, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 246, 215, 14, 216, 55, 39, 0, 180, 0, 190, 0, 170, 0, 160, 0, 150, 0, 71, 218, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 180, + "incorrect value for gdop, expected 180, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 160, + "incorrect value for hdop, expected 160, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 190, + "incorrect value for pdop, expected 190, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 170, + "incorrect value for tdop, expected 170, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 2570200, + "incorrect value for tow, expected 2570200, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 150, + "incorrect value for vdop, expected 150, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 195, 4, 14, 212, 157, 67, 24, 247, 0, 215, 0, 123, 0, 17, 1, 44, 0, 206, 21, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 247, + "incorrect value for gdop, expected 247, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 273, + "incorrect value for hdop, expected 273, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 215, + "incorrect value for pdop, expected 215, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 123, + "incorrect value for tdop, expected 123, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 407084500, + "incorrect value for tow, expected 407084500, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 44, + "incorrect value for vdop, expected 44, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 195, 4, 14, 0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 146, 12, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 65535, + "incorrect value for gdop, expected 65535, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 0, + "incorrect value for hdop, expected 0, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 65535, + "incorrect value for pdop, expected 65535, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 0, + "incorrect value for tdop, expected 0, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 0, + "incorrect value for tow, expected 0, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 0, + "incorrect value for vdop, expected 0, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 195, 4, 14, 128, 165, 68, 24, 92, 1, 56, 1, 155, 0, 125, 2, 113, 0, 129, 93, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 348, + "incorrect value for gdop, expected 348, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 637, + "incorrect value for hdop, expected 637, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 312, + "incorrect value for pdop, expected 312, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 155, + "incorrect value for tdop, expected 155, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 407152000, + "incorrect value for tow, expected 407152000, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 113, + "incorrect value for vdop, expected 113, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 195, 4, 14, 104, 169, 68, 24, 92, 1, 55, 1, 155, 0, 125, 2, 113, 0, 209, 128, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 348, + "incorrect value for gdop, expected 348, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 637, + "incorrect value for hdop, expected 637, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 311, + "incorrect value for pdop, expected 311, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 155, + "incorrect value for tdop, expected 155, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 407153000, + "incorrect value for tow, expected 407153000, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 113, + "incorrect value for vdop, expected 113, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 195, 4, 14, 80, 173, 68, 24, 92, 1, 55, 1, 155, 0, 125, 2, 112, 0, 30, 6, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 348, + "incorrect value for gdop, expected 348, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 637, + "incorrect value for hdop, expected 637, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 311, + "incorrect value for pdop, expected 311, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 155, + "incorrect value for tdop, expected 155, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 407154000, + "incorrect value for tow, expected 407154000, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 112, + "incorrect value for vdop, expected 112, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 6, 2, 195, 4, 14, 56, 177, 68, 24, 92, 1, 55, 1, 155, 0, 125, 2, 112, 0, 70, 67, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDopsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDopsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x206, + "Incorrect message type, expected 0x206, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.gdop, 348, + "incorrect value for gdop, expected 348, is {}", + msg.gdop + ); + assert_eq!( + msg.hdop, 637, + "incorrect value for hdop, expected 637, is {}", + msg.hdop + ); + assert_eq!( + msg.pdop, 311, + "incorrect value for pdop, expected 311, is {}", + msg.pdop + ); + assert_eq!( + msg.tdop, 155, + "incorrect value for tdop, expected 155, is {}", + msg.tdop + ); + assert_eq!( + msg.tow, 407155000, + "incorrect value for tow, expected 407155000, is {}", + msg.tow + ); + assert_eq!( + msg.vdop, 112, + "incorrect value for vdop, expected 112, is {}", + msg.vdop + ); + } + _ => panic!("Invalid message type! Expected a MsgDopsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time.rs index d51db6f044..ce88929105 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_gps_time() { { @@ -277,6 +282,13 @@ fn test_auto_check_sbp_navigation_msg_gps_time() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { @@ -284,14 +296,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { let json_input = r#"{"sender": 35027, "msg_type": 258, "wn": 1920, "tow": 326825000, "crc": 38946, "length": 11, "flags": 0, "ns_residual": 166900, "preamble": 85, "payload": "gAco9HoT9IsCAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -343,14 +355,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { let json_input = r#"{"sender": 35027, "msg_type": 258, "wn": 1920, "tow": 326825500, "crc": 833, "length": 11, "flags": 0, "ns_residual": 256638, "preamble": 85, "payload": "gAcc9noTfuoDAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -402,14 +414,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { let json_input = r#"{"sender": 35027, "msg_type": 258, "wn": 1920, "tow": 326826000, "crc": 21516, "length": 11, "flags": 0, "ns_residual": 265345, "preamble": 85, "payload": "gAcQ+HoTgQwEAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -461,14 +473,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { let json_input = r#"{"sender": 35027, "msg_type": 258, "wn": 1920, "tow": 326826500, "crc": 42290, "length": 11, "flags": 0, "ns_residual": 314505, "preamble": 85, "payload": "gAcE+noTicwEAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -520,14 +532,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { let json_input = r#"{"sender": 35027, "msg_type": 258, "wn": 1920, "tow": 326827000, "crc": 8628, "length": 11, "flags": 0, "ns_residual": 362933, "preamble": 85, "payload": "gAf4+3oTtYkFAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -576,3 +588,371 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_gps_time`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_gps_time() { + { + let mut payload = Cursor::new(vec![ + 85, 2, 1, 211, 136, 11, 128, 7, 40, 244, 122, 19, 244, 139, 2, 0, 0, 34, 152, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTime( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTime(msg) => { + assert_eq!( + msg.message_type(), + 0x102, + "Incorrect message type, expected 0x102, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 166900, + "incorrect value for ns_residual, expected 166900, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTime"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 1, 211, 136, 11, 128, 7, 28, 246, 122, 19, 126, 234, 3, 0, 0, 65, 3, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTime( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTime(msg) => { + assert_eq!( + msg.message_type(), + 0x102, + "Incorrect message type, expected 0x102, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 256638, + "incorrect value for ns_residual, expected 256638, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326825500, + "incorrect value for tow, expected 326825500, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTime"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 1, 211, 136, 11, 128, 7, 16, 248, 122, 19, 129, 12, 4, 0, 0, 12, 84, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTime( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTime(msg) => { + assert_eq!( + msg.message_type(), + 0x102, + "Incorrect message type, expected 0x102, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 265345, + "incorrect value for ns_residual, expected 265345, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTime"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 1, 211, 136, 11, 128, 7, 4, 250, 122, 19, 137, 204, 4, 0, 0, 50, 165, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTime( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTime(msg) => { + assert_eq!( + msg.message_type(), + 0x102, + "Incorrect message type, expected 0x102, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 314505, + "incorrect value for ns_residual, expected 314505, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326826500, + "incorrect value for tow, expected 326826500, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTime"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 2, 1, 211, 136, 11, 128, 7, 248, 251, 122, 19, 181, 137, 5, 0, 0, 180, 33, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTime( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTime(msg) => { + assert_eq!( + msg.message_type(), + 0x102, + "Incorrect message type, expected 0x102, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 362933, + "incorrect value for ns_residual, expected 362933, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTime"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_dep_a.rs index 73d88b1156..9c3ca8ead7 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_gps_time_dep_a() { { @@ -589,6 +594,13 @@ fn test_auto_check_sbp_navigation_msg_gps_time_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { @@ -596,14 +608,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 55286, "msg_type": 256, "wn": 1787, "tow": 2567800, "crc": 9349, "length": 11, "flags": 0, "ns_residual": 0, "preamble": 85, "payload": "+wZ4LicAAAAAAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -655,14 +667,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 55286, "msg_type": 256, "wn": 1787, "tow": 2567900, "crc": 40996, "length": 11, "flags": 0, "ns_residual": 0, "preamble": 85, "payload": "+wbcLicAAAAAAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -714,14 +726,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 55286, "msg_type": 256, "wn": 1787, "tow": 2568000, "crc": 48811, "length": 11, "flags": 0, "ns_residual": 0, "preamble": 85, "payload": "+wZALycAAAAAAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -773,14 +785,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 55286, "msg_type": 256, "wn": 1787, "tow": 2568100, "crc": 26067, "length": 11, "flags": 0, "ns_residual": 0, "preamble": 85, "payload": "+wakLycAAAAAAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -832,14 +844,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 55286, "msg_type": 256, "wn": 1787, "tow": 2568200, "crc": 11515, "length": 11, "flags": 0, "ns_residual": 0, "preamble": 85, "payload": "+wYIMCcAAAAAAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -891,14 +903,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 256, "wn": 1838, "tow": 407084500, "crc": 48855, "length": 11, "flags": 0, "ns_residual": -224401, "preamble": 85, "payload": "LgfUnUMYb5P8/wA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -950,14 +962,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 256, "wn": 1838, "tow": 407084600, "crc": 22918, "length": 11, "flags": 0, "ns_residual": 223085, "preamble": 85, "payload": "Lgc4nkMYbWcDAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1009,14 +1021,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 256, "wn": 1838, "tow": 407084700, "crc": 61902, "length": 11, "flags": 0, "ns_residual": -222999, "preamble": 85, "payload": "LgecnkMY6Zj8/wA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1068,14 +1080,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 256, "wn": 1838, "tow": 407084800, "crc": 25235, "length": 11, "flags": 0, "ns_residual": 236272, "preamble": 85, "payload": "LgcAn0MY8JoDAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1127,14 +1139,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 256, "wn": 1838, "tow": 407084900, "crc": 39098, "length": 11, "flags": 0, "ns_residual": -236144, "preamble": 85, "payload": "Lgdkn0MYkGX8/wA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1186,14 +1198,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 256, "wn": 1838, "tow": 407151150, "crc": 57611, "length": 11, "flags": 0, "ns_residual": -334131, "preamble": 85, "payload": "LgcuokQYzeb6/wA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1242,3 +1254,797 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_gps_time_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_gps_time_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 246, 215, 11, 251, 6, 120, 46, 39, 0, 0, 0, 0, 0, 0, 133, 36, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 0, + "incorrect value for ns_residual, expected 0, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1787, + "incorrect value for wn, expected 1787, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 246, 215, 11, 251, 6, 220, 46, 39, 0, 0, 0, 0, 0, 0, 36, 160, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 0, + "incorrect value for ns_residual, expected 0, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1787, + "incorrect value for wn, expected 1787, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 246, 215, 11, 251, 6, 64, 47, 39, 0, 0, 0, 0, 0, 0, 171, 190, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 0, + "incorrect value for ns_residual, expected 0, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 2568000, + "incorrect value for tow, expected 2568000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1787, + "incorrect value for wn, expected 1787, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 246, 215, 11, 251, 6, 164, 47, 39, 0, 0, 0, 0, 0, 0, 211, 101, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 0, + "incorrect value for ns_residual, expected 0, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 2568100, + "incorrect value for tow, expected 2568100, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1787, + "incorrect value for wn, expected 1787, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 246, 215, 11, 251, 6, 8, 48, 39, 0, 0, 0, 0, 0, 0, 251, 44, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 0, + "incorrect value for ns_residual, expected 0, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 2568200, + "incorrect value for tow, expected 2568200, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1787, + "incorrect value for wn, expected 1787, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 195, 4, 11, 46, 7, 212, 157, 67, 24, 111, 147, 252, 255, 0, 215, 190, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, -224401, + "incorrect value for ns_residual, expected -224401, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 407084500, + "incorrect value for tow, expected 407084500, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1838, + "incorrect value for wn, expected 1838, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 195, 4, 11, 46, 7, 56, 158, 67, 24, 109, 103, 3, 0, 0, 134, 89, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 223085, + "incorrect value for ns_residual, expected 223085, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 407084600, + "incorrect value for tow, expected 407084600, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1838, + "incorrect value for wn, expected 1838, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 195, 4, 11, 46, 7, 156, 158, 67, 24, 233, 152, 252, 255, 0, 206, 241, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, -222999, + "incorrect value for ns_residual, expected -222999, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 407084700, + "incorrect value for tow, expected 407084700, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1838, + "incorrect value for wn, expected 1838, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 195, 4, 11, 46, 7, 0, 159, 67, 24, 240, 154, 3, 0, 0, 147, 98, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 236272, + "incorrect value for ns_residual, expected 236272, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 407084800, + "incorrect value for tow, expected 407084800, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1838, + "incorrect value for wn, expected 1838, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 195, 4, 11, 46, 7, 100, 159, 67, 24, 144, 101, 252, 255, 0, 186, 152, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, -236144, + "incorrect value for ns_residual, expected -236144, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 407084900, + "incorrect value for tow, expected 407084900, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1838, + "incorrect value for wn, expected 1838, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 1, 195, 4, 11, 46, 7, 46, 162, 68, 24, 205, 230, 250, 255, 0, 11, 225, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x100, + "Incorrect message type, expected 0x100, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, -334131, + "incorrect value for ns_residual, expected -334131, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 407151150, + "incorrect value for tow, expected 407151150, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1838, + "incorrect value for wn, expected 1838, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_gnss.rs index 9a184fc50c..0089a10bd3 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_gps_time_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_gps_time_gnss() { { @@ -277,6 +282,13 @@ fn test_auto_check_sbp_navigation_msg_gps_time_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { @@ -284,14 +296,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { let json_input = r#"{"sender": 35027, "msg_type": 260, "wn": 1920, "tow": 326825000, "crc": 22681, "length": 11, "flags": 0, "ns_residual": 166900, "preamble": 85, "payload": "gAco9HoT9IsCAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -343,14 +355,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { let json_input = r#"{"sender": 35027, "msg_type": 260, "wn": 1920, "tow": 326825500, "crc": 50170, "length": 11, "flags": 0, "ns_residual": 256638, "preamble": 85, "payload": "gAcc9noTfuoDAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -402,14 +414,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { let json_input = r#"{"sender": 35027, "msg_type": 260, "wn": 1920, "tow": 326826000, "crc": 38071, "length": 11, "flags": 0, "ns_residual": 265345, "preamble": 85, "payload": "gAcQ+HoTgQwEAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -461,14 +473,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { let json_input = r#"{"sender": 35027, "msg_type": 260, "wn": 1920, "tow": 326826500, "crc": 25993, "length": 11, "flags": 0, "ns_residual": 314505, "preamble": 85, "payload": "gAcE+noTicwEAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -520,14 +532,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { let json_input = r#"{"sender": 35027, "msg_type": 260, "wn": 1920, "tow": 326827000, "crc": 57615, "length": 11, "flags": 0, "ns_residual": 362933, "preamble": 85, "payload": "gAf4+3oTtYkFAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -576,3 +588,371 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_gps_time_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_gps_time_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_gps_time_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 4, 1, 211, 136, 11, 128, 7, 40, 244, 122, 19, 244, 139, 2, 0, 0, 153, 88, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x104, + "Incorrect message type, expected 0x104, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 166900, + "incorrect value for ns_residual, expected 166900, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 1, 211, 136, 11, 128, 7, 28, 246, 122, 19, 126, 234, 3, 0, 0, 250, 195, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x104, + "Incorrect message type, expected 0x104, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 256638, + "incorrect value for ns_residual, expected 256638, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326825500, + "incorrect value for tow, expected 326825500, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 1, 211, 136, 11, 128, 7, 16, 248, 122, 19, 129, 12, 4, 0, 0, 183, 148, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x104, + "Incorrect message type, expected 0x104, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 265345, + "incorrect value for ns_residual, expected 265345, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 1, 211, 136, 11, 128, 7, 4, 250, 122, 19, 137, 204, 4, 0, 0, 137, 101, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x104, + "Incorrect message type, expected 0x104, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 314505, + "incorrect value for ns_residual, expected 314505, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326826500, + "incorrect value for tow, expected 326826500, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 1, 211, 136, 11, 128, 7, 248, 251, 122, 19, 181, 137, 5, 0, 0, 15, 225, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGpsTimeGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGpsTimeGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x104, + "Incorrect message type, expected 0x104, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.ns_residual, 362933, + "incorrect value for ns_residual, expected 362933, is {}", + msg.ns_residual + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.wn, 1920, + "incorrect value for wn, expected 1920, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgGPSTimeGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef.rs index 9f5173b233..b9979b4957 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_ecef() { { @@ -291,6 +296,13 @@ fn test_auto_check_sbp_navigation_msg_pos_ecef() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef() { @@ -298,14 +310,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 521, "tow": 326826000, "crc": 1620, "length": 32, "flags": 2, "y": -4316646.751816, "x": -2684269.0326572997, "z": 3839646.7095350414, "preamble": 85, "payload": "EPh6E0kdLoS2ekTB28AdsHl3UMFTC9JaT0tNQQAADwI=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -372,14 +384,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 521, "tow": 326827000, "crc": 55443, "length": 32, "flags": 2, "y": -4316646.762264892, "x": -2684269.064252186, "z": 3839646.463913912, "preamble": 85, "payload": "+Pt6E2dqOYi2ekTBsPLIsHl3UMH0h2E7T0tNQQAADwI=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -446,14 +458,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 521, "tow": 326828000, "crc": 51496, "length": 32, "flags": 2, "y": -4316647.118271949, "x": -2684269.4292816394, "z": 3839646.124909738, "preamble": 85, "payload": "4P96E2Wz8ra2ekTBgsSRx3l3UMHUCv0PT0tNQQAADwI=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -520,14 +532,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 521, "tow": 326829000, "crc": 22203, "length": 32, "flags": 2, "y": -4316647.3623821335, "x": -2684269.683741399, "z": 3839645.90179852, "preamble": 85, "payload": "yAN7E5LWhNe2ekTB1UQx13l3UMFHIm7zTktNQQAADwI=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -591,3 +603,366 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_ecef`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_ecef() { + { + let mut payload = Cursor::new(vec![ + 85, 9, 2, 211, 136, 32, 16, 248, 122, 19, 73, 29, 46, 132, 182, 122, 68, 193, 219, 192, + 29, 176, 121, 119, 80, 193, 83, 11, 210, 90, 79, 75, 77, 65, 0, 0, 15, 2, 84, 6, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x209, + "Incorrect message type, expected 0x209, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.68426903265729966e+06), + "incorrect value for x, expected -2.68426903265729966e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.31664675181599986e+06), + "incorrect value for y, expected -4.31664675181599986e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.83964670953504136e+06), + "incorrect value for z, expected 3.83964670953504136e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 9, 2, 211, 136, 32, 248, 251, 122, 19, 103, 106, 57, 136, 182, 122, 68, 193, 176, + 242, 200, 176, 121, 119, 80, 193, 244, 135, 97, 59, 79, 75, 77, 65, 0, 0, 15, 2, 147, + 216, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x209, + "Incorrect message type, expected 0x209, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.68426906425218610e+06), + "incorrect value for x, expected -2.68426906425218610e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.31664676226489246e+06), + "incorrect value for y, expected -4.31664676226489246e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.83964646391391195e+06), + "incorrect value for z, expected 3.83964646391391195e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 9, 2, 211, 136, 32, 224, 255, 122, 19, 101, 179, 242, 182, 182, 122, 68, 193, 130, + 196, 145, 199, 121, 119, 80, 193, 212, 10, 253, 15, 79, 75, 77, 65, 0, 0, 15, 2, 40, + 201, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x209, + "Incorrect message type, expected 0x209, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326828000, + "incorrect value for tow, expected 326828000, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.68426942928163940e+06), + "incorrect value for x, expected -2.68426942928163940e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.31664711827194877e+06), + "incorrect value for y, expected -4.31664711827194877e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.83964612490973808e+06), + "incorrect value for z, expected 3.83964612490973808e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 9, 2, 211, 136, 32, 200, 3, 123, 19, 146, 214, 132, 215, 182, 122, 68, 193, 213, + 68, 49, 215, 121, 119, 80, 193, 71, 34, 110, 243, 78, 75, 77, 65, 0, 0, 15, 2, 187, 86, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x209, + "Incorrect message type, expected 0x209, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326829000, + "incorrect value for tow, expected 326829000, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.68426968374139909e+06), + "incorrect value for x, expected -2.68426968374139909e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.31664736238213349e+06), + "incorrect value for y, expected -4.31664736238213349e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.83964590179851977e+06), + "incorrect value for z, expected 3.83964590179851977e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov.rs index 4090b0cbe1..851add580e 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_ecef_cov() { { @@ -111,6 +116,13 @@ fn test_auto_check_sbp_navigation_msg_pos_ecef_cov() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_cov() { @@ -118,14 +130,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_cov() { let json_input = r#"{"cov_y_z": 8, "cov_y_y": 6, "n_sats": 4, "sender": 66, "msg_type": 532, "cov_x_z": 2, "cov_x_y": 7, "cov_x_x": 8, "tow": 7, "crc": 43001, "length": 54, "flags": 5, "y": 1, "x": 6, "z": 4, "preamble": 85, "payload": "BwAAAAAAAAAAABhAAAAAAAAA8D8AAAAAAAAQQAAAAEEAAOBAAAAAQAAAwEAAAABBAACgQAQF", "cov_z_z": 5}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -214,3 +226,129 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_cov() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_ecef_cov`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_ecef_cov() { + { + let mut payload = Cursor::new(vec![ + 85, 20, 2, 66, 0, 54, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 64, 0, 0, 0, 0, 0, 0, 240, 63, + 0, 0, 0, 0, 0, 0, 16, 64, 0, 0, 0, 65, 0, 0, 224, 64, 0, 0, 0, 64, 0, 0, 192, 64, 0, 0, + 0, 65, 0, 0, 160, 64, 4, 5, 249, 167, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefCov( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefCov(msg) => { + assert_eq!( + msg.message_type(), + 0x214, + "Incorrect message type, expected 0x214, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert!( + msg.cov_x_x.almost_eq(8.00000000000000000e+00), + "incorrect value for cov_x_x, expected 8.00000000000000000e+00, is {:e}", + msg.cov_x_x + ); + assert!( + msg.cov_x_y.almost_eq(7.00000000000000000e+00), + "incorrect value for cov_x_y, expected 7.00000000000000000e+00, is {:e}", + msg.cov_x_y + ); + assert!( + msg.cov_x_z.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_x_z, expected 2.00000000000000000e+00, is {:e}", + msg.cov_x_z + ); + assert!( + msg.cov_y_y.almost_eq(6.00000000000000000e+00), + "incorrect value for cov_y_y, expected 6.00000000000000000e+00, is {:e}", + msg.cov_y_y + ); + assert!( + msg.cov_y_z.almost_eq(8.00000000000000000e+00), + "incorrect value for cov_y_z, expected 8.00000000000000000e+00, is {:e}", + msg.cov_y_z + ); + assert!( + msg.cov_z_z.almost_eq(5.00000000000000000e+00), + "incorrect value for cov_z_z, expected 5.00000000000000000e+00, is {:e}", + msg.cov_z_z + ); + assert_eq!( + msg.flags, 5, + "incorrect value for flags, expected 5, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 4, + "incorrect value for n_sats, expected 4, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 7, + "incorrect value for tow, expected 7, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(6.00000000000000000e+00), + "incorrect value for x, expected 6.00000000000000000e+00, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(1.00000000000000000e+00), + "incorrect value for y, expected 1.00000000000000000e+00, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(4.00000000000000000e+00), + "incorrect value for z, expected 4.00000000000000000e+00, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFCov"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov_gnss.rs index c2127c90ba..41f67ca2aa 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_cov_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss() { { @@ -112,6 +117,13 @@ fn test_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss() { @@ -119,14 +131,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss() { let json_input = r#"{"tow":501867800,"x":-2694229.7079770807,"y":-4264073.427345817,"z":3890655.013186158,"cov_x_x":0.009699014946818352,"cov_x_y":0.009086096659302711,"cov_x_z":-0.006058753002434969,"cov_y_y":0.020321274176239967,"cov_y_z":-0.009988312609493732,"cov_z_z":0.01487385667860508,"n_sats":18,"flags":4,"preamble":85,"msg_type":564,"sender":4096,"payload":"GOXpHTT+ntoqjkTBRaJZWyJEUMGDFbCB765NQZ7oHjza3RQ8gYjGu814pjwFpiO8erFzPBIE","crc":26271,"length":54}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -215,3 +227,130 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_ecef_cov_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 52, 2, 0, 16, 54, 24, 229, 233, 29, 52, 254, 158, 218, 42, 142, 68, 193, 69, 162, + 89, 91, 34, 68, 80, 193, 131, 21, 176, 129, 239, 174, 77, 65, 158, 232, 30, 60, 218, + 221, 20, 60, 129, 136, 198, 187, 205, 120, 166, 60, 5, 166, 35, 188, 122, 177, 115, 60, + 18, 4, 159, 102, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefCovGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefCovGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x234, + "Incorrect message type, expected 0x234, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert!( + msg.cov_x_x.almost_eq(9.69901494681835175e-03), + "incorrect value for cov_x_x, expected 9.69901494681835175e-03, is {:e}", + msg.cov_x_x + ); + assert!( + msg.cov_x_y.almost_eq(9.08609665930271149e-03), + "incorrect value for cov_x_y, expected 9.08609665930271149e-03, is {:e}", + msg.cov_x_y + ); + assert!( + msg.cov_x_z.almost_eq(-6.05875300243496895e-03), + "incorrect value for cov_x_z, expected -6.05875300243496895e-03, is {:e}", + msg.cov_x_z + ); + assert!( + msg.cov_y_y.almost_eq(2.03212741762399673e-02), + "incorrect value for cov_y_y, expected 2.03212741762399673e-02, is {:e}", + msg.cov_y_y + ); + assert!( + msg.cov_y_z.almost_eq(-9.98831260949373245e-03), + "incorrect value for cov_y_z, expected -9.98831260949373245e-03, is {:e}", + msg.cov_y_z + ); + assert!( + msg.cov_z_z.almost_eq(1.48738566786050797e-02), + "incorrect value for cov_z_z, expected 1.48738566786050797e-02, is {:e}", + msg.cov_z_z + ); + assert_eq!( + msg.flags, 4, + "incorrect value for flags, expected 4, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 18, + "incorrect value for n_sats, expected 18, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501867800, + "incorrect value for tow, expected 501867800, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.69422970797708072e+06), + "incorrect value for x, expected -2.69422970797708072e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26407342734581698e+06), + "incorrect value for y, expected -4.26407342734581698e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.89065501318615815e+06), + "incorrect value for z, expected 3.89065501318615815e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFCovGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_dep_a.rs index c18b7a4d7c..360c911dee 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecef_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { { @@ -765,6 +770,13 @@ fn test_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { @@ -772,14 +784,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 512, "tow": 2567700, "crc": 22029, "length": 32, "flags": 0, "y": -4292510.764041577, "x": -2700354.5912927105, "z": 3855357.977260149, "preamble": 85, "payload": "FC4nAMN6r0shmkTBpA7msOdfUMFO3Bb9/mlNQQAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -846,14 +858,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 512, "tow": 2567700, "crc": 36683, "length": 32, "flags": 1, "y": -4292509.928737887, "x": -2700356.3285146747, "z": 3855357.5011712564, "preamble": 85, "payload": "FC4nANTEDCoimkTBCXFwe+dfUME2YSbA/mlNQQAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -920,14 +932,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 512, "tow": 2567800, "crc": 29132, "length": 32, "flags": 0, "y": -4292509.80414865, "x": -2700357.485576801, "z": 3855356.517968082, "preamble": 85, "payload": "eC4nAHBhJ74imkTB5it3c+dfUMEyx0xC/mlNQQAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -994,14 +1006,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 512, "tow": 2567800, "crc": 18273, "length": 32, "flags": 1, "y": -4292510.187605589, "x": -2700356.0349524925, "z": 3855357.4185667858, "preamble": 85, "payload": "eC4nAMJSeQQimkTB37oBjOdfUMGwmJO1/mlNQQAACQE=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1068,14 +1080,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 512, "tow": 2567900, "crc": 25095, "length": 32, "flags": 0, "y": -4292509.946935424, "x": -2700355.9913074784, "z": 3855359.0924900775, "preamble": 85, "payload": "3C4nANgp4/4hmkTBCZeafOdfUMEBt9aL/2lNQQAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1142,14 +1154,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 512, "tow": 407084500, "crc": 1169, "length": 32, "flags": 0, "y": -4263209.753232954, "x": -2704376.0110433814, "z": 3884633.142084079, "preamble": 85, "payload": "1J1DGJneaQH8oUTB/vc0cEpDUMGkzy+SLKNNQQAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1216,14 +1228,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 512, "tow": 407084600, "crc": 17141, "length": 32, "flags": 0, "y": -4263208.610442672, "x": -2704375.9287024545, "z": 3884632.627157578, "preamble": 85, "payload": "OJ5DGNe43/b7oUTBJH4RJ0pDUMETs0ZQLKNNQQAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1290,14 +1302,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 512, "tow": 407084700, "crc": 57093, "length": 32, "flags": 0, "y": -4263207.370641668, "x": -2704375.162789617, "z": 3884631.282421521, "preamble": 85, "payload": "nJ5DGElK1pT7oUTB1Ze410lDUMFuYyakK6NNQQAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1364,14 +1376,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 512, "tow": 407084800, "crc": 54415, "length": 32, "flags": 0, "y": -4263207.965250214, "x": -2704376.3549937834, "z": 3884632.1007095524, "preamble": 85, "payload": "AJ9DGLFvcC38oUTB1ajG/UlDUMH1DOQMLKNNQQAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1438,14 +1450,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 512, "tow": 407084900, "crc": 56646, "length": 32, "flags": 0, "y": -4263207.314747473, "x": -2704375.291287334, "z": 3884631.4773294823, "preamble": 85, "payload": "ZJ9DGEPnSKX7oUTBltIk1ElDUMHqIRm9K6NNQQAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1512,14 +1524,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { let json_input = r#"{"n_sats": 5, "sender": 1219, "msg_type": 512, "tow": 407151150, "crc": 56593, "length": 32, "flags": 0, "y": -4263209.482329298, "x": -2704375.68369399, "z": 3884635.5118107493, "preamble": 85, "payload": "LqJEGOBIg9f7oUTBtHveXkpDUMG/A4PBLaNNQQAABQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1583,3 +1595,973 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_ecef_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_ecef_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 246, 215, 32, 20, 46, 39, 0, 195, 122, 175, 75, 33, 154, 68, 193, 164, 14, + 230, 176, 231, 95, 80, 193, 78, 220, 22, 253, 254, 105, 77, 65, 0, 0, 9, 0, 13, 86, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70035459129271051e+06), + "incorrect value for x, expected -2.70035459129271051e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.29251076404157653e+06), + "incorrect value for y, expected -4.29251076404157653e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.85535797726014908e+06), + "incorrect value for z, expected 3.85535797726014908e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 246, 215, 32, 20, 46, 39, 0, 212, 196, 12, 42, 34, 154, 68, 193, 9, 113, 112, + 123, 231, 95, 80, 193, 54, 97, 38, 192, 254, 105, 77, 65, 0, 0, 9, 1, 75, 143, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70035632851467468e+06), + "incorrect value for x, expected -2.70035632851467468e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.29250992873788718e+06), + "incorrect value for y, expected -4.29250992873788718e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.85535750117125642e+06), + "incorrect value for z, expected 3.85535750117125642e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 246, 215, 32, 120, 46, 39, 0, 112, 97, 39, 190, 34, 154, 68, 193, 230, 43, + 119, 115, 231, 95, 80, 193, 50, 199, 76, 66, 254, 105, 77, 65, 0, 0, 9, 0, 204, 113, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70035748557680100e+06), + "incorrect value for x, expected -2.70035748557680100e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.29250980414864980e+06), + "incorrect value for y, expected -4.29250980414864980e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.85535651796808187e+06), + "incorrect value for z, expected 3.85535651796808187e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 246, 215, 32, 120, 46, 39, 0, 194, 82, 121, 4, 34, 154, 68, 193, 223, 186, 1, + 140, 231, 95, 80, 193, 176, 152, 147, 181, 254, 105, 77, 65, 0, 0, 9, 1, 97, 71, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70035603495249245e+06), + "incorrect value for x, expected -2.70035603495249245e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.29251018760558870e+06), + "incorrect value for y, expected -4.29251018760558870e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.85535741856678575e+06), + "incorrect value for z, expected 3.85535741856678575e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 246, 215, 32, 220, 46, 39, 0, 216, 41, 227, 254, 33, 154, 68, 193, 9, 151, + 154, 124, 231, 95, 80, 193, 1, 183, 214, 139, 255, 105, 77, 65, 0, 0, 9, 0, 7, 98, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70035599130747840e+06), + "incorrect value for x, expected -2.70035599130747840e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.29250994693542365e+06), + "incorrect value for y, expected -4.29250994693542365e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.85535909249007748e+06), + "incorrect value for z, expected 3.85535909249007748e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 195, 4, 32, 212, 157, 67, 24, 153, 222, 105, 1, 252, 161, 68, 193, 254, 247, + 52, 112, 74, 67, 80, 193, 164, 207, 47, 146, 44, 163, 77, 65, 0, 0, 8, 0, 145, 4, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084500, + "incorrect value for tow, expected 407084500, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70437601104338141e+06), + "incorrect value for x, expected -2.70437601104338141e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26320975323295407e+06), + "incorrect value for y, expected -4.26320975323295407e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.88463314208407886e+06), + "incorrect value for z, expected 3.88463314208407886e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 195, 4, 32, 56, 158, 67, 24, 215, 184, 223, 246, 251, 161, 68, 193, 36, 126, + 17, 39, 74, 67, 80, 193, 19, 179, 70, 80, 44, 163, 77, 65, 0, 0, 8, 0, 245, 66, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084600, + "incorrect value for tow, expected 407084600, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70437592870245455e+06), + "incorrect value for x, expected -2.70437592870245455e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26320861044267192e+06), + "incorrect value for y, expected -4.26320861044267192e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.88463262715757778e+06), + "incorrect value for z, expected 3.88463262715757778e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 195, 4, 32, 156, 158, 67, 24, 73, 74, 214, 148, 251, 161, 68, 193, 213, 151, + 184, 215, 73, 67, 80, 193, 110, 99, 38, 164, 43, 163, 77, 65, 0, 0, 8, 0, 5, 223, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084700, + "incorrect value for tow, expected 407084700, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70437516278961720e+06), + "incorrect value for x, expected -2.70437516278961720e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26320737064166833e+06), + "incorrect value for y, expected -4.26320737064166833e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.88463128242152091e+06), + "incorrect value for z, expected 3.88463128242152091e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 195, 4, 32, 0, 159, 67, 24, 177, 111, 112, 45, 252, 161, 68, 193, 213, 168, + 198, 253, 73, 67, 80, 193, 245, 12, 228, 12, 44, 163, 77, 65, 0, 0, 8, 0, 143, 212, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084800, + "incorrect value for tow, expected 407084800, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70437635499378340e+06), + "incorrect value for x, expected -2.70437635499378340e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26320796525021363e+06), + "incorrect value for y, expected -4.26320796525021363e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.88463210070955241e+06), + "incorrect value for z, expected 3.88463210070955241e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 195, 4, 32, 100, 159, 67, 24, 67, 231, 72, 165, 251, 161, 68, 193, 150, 210, + 36, 212, 73, 67, 80, 193, 234, 33, 25, 189, 43, 163, 77, 65, 0, 0, 8, 0, 70, 221, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084900, + "incorrect value for tow, expected 407084900, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70437529128733417e+06), + "incorrect value for x, expected -2.70437529128733417e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26320731474747322e+06), + "incorrect value for y, expected -4.26320731474747322e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.88463147732948232e+06), + "incorrect value for z, expected 3.88463147732948232e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 0, 2, 195, 4, 32, 46, 162, 68, 24, 224, 72, 131, 215, 251, 161, 68, 193, 180, 123, + 222, 94, 74, 67, 80, 193, 191, 3, 131, 193, 45, 163, 77, 65, 0, 0, 5, 0, 17, 221, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x200, + "Incorrect message type, expected 0x200, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 5, + "incorrect value for n_sats, expected 5, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407151150, + "incorrect value for tow, expected 407151150, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.70437568369399011e+06), + "incorrect value for x, expected -2.70437568369399011e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26320948232929781e+06), + "incorrect value for y, expected -4.26320948232929781e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.88463551181074930e+06), + "incorrect value for z, expected 3.88463551181074930e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecefgnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecefgnss.rs index 6617f99810..40e75463ab 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecefgnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_ecefgnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_ecefgnss() { { @@ -85,6 +90,13 @@ fn test_auto_check_sbp_navigation_msg_pos_ecefgnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecefgnss() { @@ -92,14 +104,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecefgnss() { let json_input = r#"{"tow":501867800,"x":-2694229.7079770807,"y":-4264073.427345817,"z":3890655.013186158,"accuracy":182,"n_sats":18,"flags":4,"preamble":85,"msg_type":553,"sender":4096,"payload":"GOXpHTT+ntoqjkTBRaJZWyJEUMGDFbCB765NQbYAEgQ=","crc":647,"length":32}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -163,3 +175,103 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_ecefgnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_ecefgnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_ecefgnss() { + { + let mut payload = Cursor::new(vec![ + 85, 41, 2, 0, 16, 32, 24, 229, 233, 29, 52, 254, 158, 218, 42, 142, 68, 193, 69, 162, + 89, 91, 34, 68, 80, 193, 131, 21, 176, 129, 239, 174, 77, 65, 182, 0, 18, 4, 135, 2, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosEcefGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosEcefGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x229, + "Incorrect message type, expected 0x229, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 182, + "incorrect value for accuracy, expected 182, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 4, + "incorrect value for flags, expected 4, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 18, + "incorrect value for n_sats, expected 18, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501867800, + "incorrect value for tow, expected 501867800, is {}", + msg.tow + ); + assert!( + msg.x.almost_eq(-2.69422970797708072e+06), + "incorrect value for x, expected -2.69422970797708072e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.26407342734581698e+06), + "incorrect value for y, expected -4.26407342734581698e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.89065501318615815e+06), + "incorrect value for z, expected 3.89065501318615815e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgPosECEFGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh.rs index 7ea4483cd5..5aab5d6d95 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_llh() { { @@ -387,6 +392,13 @@ fn test_auto_check_sbp_navigation_msg_pos_llh() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { @@ -394,14 +406,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { let json_input = r#"{"v_accuracy": 0, "n_sats": 14, "sender": 35027, "msg_type": 522, "lon": -121.87505366879361, "tow": 326825000, "height": 28.21160739227208, "crc": 41647, "length": 34, "flags": 2, "h_accuracy": 0, "lat": 37.25130398358085, "preamble": 85, "payload": "KPR6E8lqm7oqoEJAqG0a4QB4XsCCZu3mKzY8QAAAAAAOAg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -473,14 +485,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { let json_input = r#"{"v_accuracy": 0, "n_sats": 15, "sender": 35027, "msg_type": 522, "lon": -121.87505349618341, "tow": 326826000, "height": 27.742055560866373, "crc": 45350, "length": 34, "flags": 2, "h_accuracy": 0, "lat": 37.251303074738104, "preamble": 85, "payload": "EPh6EzSx+7IqoEJA7RZh4AB4XsBrvG1a9707QAAAAAAPAg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -552,14 +564,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { let json_input = r#"{"v_accuracy": 0, "n_sats": 15, "sender": 35027, "msg_type": 522, "lon": -121.87505373641241, "tow": 326827000, "height": 27.613721582970516, "crc": 10291, "length": 34, "flags": 2, "h_accuracy": 0, "lat": 37.25130117370741, "preamble": 85, "payload": "+Pt6E4dCCaMqoEJAkghj4QB4XsAttY/bHJ07QAAAAAAPAg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -631,14 +643,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { let json_input = r#"{"v_accuracy": 0, "n_sats": 15, "sender": 35027, "msg_type": 522, "lon": -121.87505511141057, "tow": 326828000, "height": 27.80259807042305, "crc": 49676, "length": 34, "flags": 2, "h_accuracy": 0, "lat": 37.251296042079176, "preamble": 85, "payload": "4P96ExIs/XcqoEJAMG0n5wB4XsC5TDARd807QAAAAAAPAg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -710,14 +722,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { let json_input = r#"{"v_accuracy": 0, "n_sats": 15, "sender": 35027, "msg_type": 522, "lon": -121.87505609407974, "tow": 326829000, "height": 27.939512310879213, "crc": 26402, "length": 34, "flags": 2, "h_accuracy": 0, "lat": 37.251292578377395, "preamble": 85, "payload": "yAN7E+Ht7loqoEJAO49G6wB4XsBlavngg/A7QAAAAAAPAg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -786,3 +798,481 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_llh`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_llh() { + { + let mut payload = Cursor::new(vec![ + 85, 10, 2, 211, 136, 34, 40, 244, 122, 19, 201, 106, 155, 186, 42, 160, 66, 64, 168, + 109, 26, 225, 0, 120, 94, 192, 130, 102, 237, 230, 43, 54, 60, 64, 0, 0, 0, 0, 14, 2, + 175, 162, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlh( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlh(msg) => { + assert_eq!( + msg.message_type(), + 0x20a, + "Incorrect message type, expected 0x20a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.82116073922720787e+01), + "incorrect value for height, expected 2.82116073922720787e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.72513039835808470e+01), + "incorrect value for lat, expected 3.72513039835808470e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.21875053668793612e+02), + "incorrect value for lon, expected -1.21875053668793612e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 14, + "incorrect value for n_sats, expected 14, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLH"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 10, 2, 211, 136, 34, 16, 248, 122, 19, 52, 177, 251, 178, 42, 160, 66, 64, 237, 22, + 97, 224, 0, 120, 94, 192, 107, 188, 109, 90, 247, 189, 59, 64, 0, 0, 0, 0, 15, 2, 38, + 177, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlh( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlh(msg) => { + assert_eq!( + msg.message_type(), + 0x20a, + "Incorrect message type, expected 0x20a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.77420555608663726e+01), + "incorrect value for height, expected 2.77420555608663726e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.72513030747381038e+01), + "incorrect value for lat, expected 3.72513030747381038e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.21875053496183412e+02), + "incorrect value for lon, expected -1.21875053496183412e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLH"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 10, 2, 211, 136, 34, 248, 251, 122, 19, 135, 66, 9, 163, 42, 160, 66, 64, 146, 8, + 99, 225, 0, 120, 94, 192, 45, 181, 143, 219, 28, 157, 59, 64, 0, 0, 0, 0, 15, 2, 51, + 40, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlh( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlh(msg) => { + assert_eq!( + msg.message_type(), + 0x20a, + "Incorrect message type, expected 0x20a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.76137215829705163e+01), + "incorrect value for height, expected 2.76137215829705163e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.72513011737074109e+01), + "incorrect value for lat, expected 3.72513011737074109e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.21875053736412411e+02), + "incorrect value for lon, expected -1.21875053736412411e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLH"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 10, 2, 211, 136, 34, 224, 255, 122, 19, 18, 44, 253, 119, 42, 160, 66, 64, 48, 109, + 39, 231, 0, 120, 94, 192, 185, 76, 48, 17, 119, 205, 59, 64, 0, 0, 0, 0, 15, 2, 12, + 194, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlh( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlh(msg) => { + assert_eq!( + msg.message_type(), + 0x20a, + "Incorrect message type, expected 0x20a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.78025980704230484e+01), + "incorrect value for height, expected 2.78025980704230484e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.72512960420791757e+01), + "incorrect value for lat, expected 3.72512960420791757e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.21875055111410575e+02), + "incorrect value for lon, expected -1.21875055111410575e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326828000, + "incorrect value for tow, expected 326828000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLH"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 10, 2, 211, 136, 34, 200, 3, 123, 19, 225, 237, 238, 90, 42, 160, 66, 64, 59, 143, + 70, 235, 0, 120, 94, 192, 101, 106, 249, 224, 131, 240, 59, 64, 0, 0, 0, 0, 15, 2, 34, + 103, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlh( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlh(msg) => { + assert_eq!( + msg.message_type(), + 0x20a, + "Incorrect message type, expected 0x20a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.79395123108792127e+01), + "incorrect value for height, expected 2.79395123108792127e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.72512925783773952e+01), + "incorrect value for lat, expected 3.72512925783773952e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.21875056094079739e+02), + "incorrect value for lon, expected -1.21875056094079739e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326829000, + "incorrect value for tow, expected 326829000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLH"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov.rs index 1e7d2fbd95..a5c9d84ba4 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_llh_cov() { { @@ -111,6 +116,13 @@ fn test_auto_check_sbp_navigation_msg_pos_llh_cov() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_cov() { @@ -118,14 +130,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_cov() { let json_input = r#"{"cov_e_d": 1, "cov_e_e": 6, "n_sats": 5, "sender": 66, "msg_type": 529, "cov_n_n": 7, "lon": 7, "cov_n_e": 5, "tow": 7, "height": 0, "crc": 25239, "length": 54, "cov_n_d": 8, "lat": 0, "flags": 5, "cov_d_d": 2, "preamble": 85, "payload": "BwAAAAAAAAAAAAAAAAAAAAAAHEAAAAAAAAAAAAAA4EAAAKBAAAAAQQAAwEAAAIA/AAAAQAUF"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -214,3 +226,129 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_cov() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_llh_cov`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_llh_cov() { + { + let mut payload = Cursor::new(vec![ + 85, 17, 2, 66, 0, 54, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 64, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 224, 64, 0, 0, 160, 64, 0, 0, 0, 65, 0, 0, 192, 64, 0, 0, + 128, 63, 0, 0, 0, 64, 5, 5, 151, 98, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhCov( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhCov(msg) => { + assert_eq!( + msg.message_type(), + 0x211, + "Incorrect message type, expected 0x211, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert!( + msg.cov_d_d.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_d_d, expected 2.00000000000000000e+00, is {:e}", + msg.cov_d_d + ); + assert!( + msg.cov_e_d.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_e_d, expected 1.00000000000000000e+00, is {:e}", + msg.cov_e_d + ); + assert!( + msg.cov_e_e.almost_eq(6.00000000000000000e+00), + "incorrect value for cov_e_e, expected 6.00000000000000000e+00, is {:e}", + msg.cov_e_e + ); + assert!( + msg.cov_n_d.almost_eq(8.00000000000000000e+00), + "incorrect value for cov_n_d, expected 8.00000000000000000e+00, is {:e}", + msg.cov_n_d + ); + assert!( + msg.cov_n_e.almost_eq(5.00000000000000000e+00), + "incorrect value for cov_n_e, expected 5.00000000000000000e+00, is {:e}", + msg.cov_n_e + ); + assert!( + msg.cov_n_n.almost_eq(7.00000000000000000e+00), + "incorrect value for cov_n_n, expected 7.00000000000000000e+00, is {:e}", + msg.cov_n_n + ); + assert_eq!( + msg.flags, 5, + "incorrect value for flags, expected 5, is {}", + msg.flags + ); + assert!( + msg.height.almost_eq(0.00000000000000000e+00), + "incorrect value for height, expected 0.00000000000000000e+00, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(0.00000000000000000e+00), + "incorrect value for lat, expected 0.00000000000000000e+00, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(7.00000000000000000e+00), + "incorrect value for lon, expected 7.00000000000000000e+00, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 5, + "incorrect value for n_sats, expected 5, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 7, + "incorrect value for tow, expected 7, is {}", + msg.tow + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHCov"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov_gnss.rs index c3bccecc7c..fd200edf1e 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_cov_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_llh_cov_gnss() { { @@ -112,6 +117,13 @@ fn test_auto_check_sbp_navigation_msg_pos_llh_cov_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_cov_gnss() { @@ -119,14 +131,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_cov_gnss() { let json_input = r#"{"tow":501867800,"lat":37.83123196497633,"lon":-122.28650381011681,"height":-17.39382124780135,"cov_n_n":0.007488971576094627,"cov_n_e":-0.00036755966721102595,"cov_n_d":0.0018563168123364449,"cov_e_e":0.004523798823356628,"cov_e_d":-0.0008439270895905793,"cov_d_d":0.03288137540221214,"n_sats":18,"flags":4,"preamble":85,"msg_type":561,"sender":4096,"payload":"GOXpHUl7HM9l6kJAZKgTFFaSXsDWxiN40WQxwAxm9TsGtcC5qE/zOmA8lDv9Ol26n64GPRIE","crc":50186,"length":54}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -215,3 +227,130 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_cov_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_llh_cov_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_llh_cov_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 49, 2, 0, 16, 54, 24, 229, 233, 29, 73, 123, 28, 207, 101, 234, 66, 64, 100, 168, + 19, 20, 86, 146, 94, 192, 214, 198, 35, 120, 209, 100, 49, 192, 12, 102, 245, 59, 6, + 181, 192, 185, 168, 79, 243, 58, 96, 60, 148, 59, 253, 58, 93, 186, 159, 174, 6, 61, + 18, 4, 10, 196, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhCovGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhCovGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x231, + "Incorrect message type, expected 0x231, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert!( + msg.cov_d_d.almost_eq(3.28813754022121429e-02), + "incorrect value for cov_d_d, expected 3.28813754022121429e-02, is {:e}", + msg.cov_d_d + ); + assert!( + msg.cov_e_d.almost_eq(-8.43927089590579271e-04), + "incorrect value for cov_e_d, expected -8.43927089590579271e-04, is {:e}", + msg.cov_e_d + ); + assert!( + msg.cov_e_e.almost_eq(4.52379882335662842e-03), + "incorrect value for cov_e_e, expected 4.52379882335662842e-03, is {:e}", + msg.cov_e_e + ); + assert!( + msg.cov_n_d.almost_eq(1.85631681233644485e-03), + "incorrect value for cov_n_d, expected 1.85631681233644485e-03, is {:e}", + msg.cov_n_d + ); + assert!( + msg.cov_n_e.almost_eq(-3.67559667211025953e-04), + "incorrect value for cov_n_e, expected -3.67559667211025953e-04, is {:e}", + msg.cov_n_e + ); + assert!( + msg.cov_n_n.almost_eq(7.48897157609462738e-03), + "incorrect value for cov_n_n, expected 7.48897157609462738e-03, is {:e}", + msg.cov_n_n + ); + assert_eq!( + msg.flags, 4, + "incorrect value for flags, expected 4, is {}", + msg.flags + ); + assert!( + msg.height.almost_eq(-1.73938212478013483e+01), + "incorrect value for height, expected -1.73938212478013483e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.78312319649763324e+01), + "incorrect value for lat, expected 3.78312319649763324e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22286503810116812e+02), + "incorrect value for lon, expected -1.22286503810116812e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 18, + "incorrect value for n_sats, expected 18, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501867800, + "incorrect value for tow, expected 501867800, is {}", + msg.tow + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHCovGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_dep_a.rs index b4156592b9..2775bcd4fc 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_llh_dep_a() { { @@ -828,6 +833,13 @@ fn test_auto_check_sbp_navigation_msg_pos_llh_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { @@ -835,14 +847,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 9, "sender": 55286, "msg_type": 513, "lon": -122.17338662202773, "tow": 2567700, "height": 69.80437675175607, "crc": 35820, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.42906890908121, "preamble": 85, "payload": "FC4nAPod4rrrtkJAE8szxBiLXsAfnaDoenNRQAAAAAAJAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -914,14 +926,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 9, "sender": 55286, "msg_type": 513, "lon": -122.17340826071865, "tow": 2567700, "height": 69.68814067715354, "crc": 29977, "length": 34, "flags": 1, "h_accuracy": 0, "lat": 37.42906430885274, "preamble": 85, "payload": "FC4nAKEzS5TrtkJAJCn2HhmLXsD+2jF/CmxRQAAAAAAJAQ=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -993,14 +1005,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 9, "sender": 55286, "msg_type": 513, "lon": -122.17342007549469, "tow": 2567800, "height": 69.49608854815264, "crc": 27054, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.42905447764173, "preamble": 85, "payload": "eC4nADjW0kHrtkJADS6EUBmLXsAWjy7qv19RQAAAAAAJAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1072,14 +1084,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 9, "sender": 55286, "msg_type": 513, "lon": -122.17340389594972, "tow": 2567800, "height": 69.68780458819901, "crc": 32634, "length": 34, "flags": 1, "h_accuracy": 0, "lat": 37.429063373925565, "preamble": 85, "payload": "eC4nAPt1c4zrtkJAmIanDBmLXsCgFon9BGxRQAAAAAAJAQ=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1151,14 +1163,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 9, "sender": 55286, "msg_type": 513, "lon": -122.17340492645452, "tow": 2567900, "height": 70.5249547317965, "crc": 40642, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.42907659359516, "preamble": 85, "payload": "3C4nADN8WPvrtkJAmQX6EBmLXsCSPLvbmKFRQAAAAAAJAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1230,14 +1242,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 8, "sender": 1219, "msg_type": 513, "lon": -122.38908437889262, "tow": 407084500, "height": 4.039810885214956, "crc": 43501, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.76242171418386, "preamble": 85, "payload": "1J1DGAgX5AiX4UJAnK4qwuaYXsCZF0gvxCgQQAAAAAAIAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1309,14 +1321,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 8, "sender": 1219, "msg_type": 513, "lon": -122.38909053700489, "tow": 407084600, "height": 2.926714087009657, "crc": 2968, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.76242361423985, "preamble": 85, "payload": "OJ5DGNxt1BiX4UJAn+f+2+aYXsCAl0MT6WkHQAAAAAAIAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1388,14 +1400,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 8, "sender": 1219, "msg_type": 513, "lon": -122.3890907340148, "tow": 407084700, "height": 0.9512146647395566, "crc": 39901, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.762422076126406, "preamble": 85, "payload": "nJ5DGA1b7QuX4UJAS3HS3OaYXsAlBpG8WXDuPwAAAAAIAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1467,14 +1479,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 8, "sender": 1219, "msg_type": 513, "lon": -122.38909854449612, "tow": 407084800, "height": 2.354135752047538, "crc": 24146, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.762421610632735, "preamble": 85, "payload": "AJ9DGDO3BQiX4UJADeKU/eaYXsC7GwsgRdUCQAAAAAAIAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1546,14 +1558,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 8, "sender": 1219, "msg_type": 513, "lon": -122.38909230523223, "tow": 407084900, "height": 1.0876763181642641, "crc": 15430, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.76242334502801, "preamble": 85, "payload": "ZJ9DGBZNkhaX4UJAQIZp4+aYXsAlY3JIH2fxPwAAAAAIAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1625,14 +1637,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { let json_input = r#"{"v_accuracy": 0, "n_sats": 5, "sender": 1219, "msg_type": 513, "lon": -122.38908288868525, "tow": 407151150, "height": 5.171533844654222, "crc": 31188, "length": 34, "flags": 0, "h_accuracy": 0, "lat": 37.76244082253376, "preamble": 85, "payload": "LqJEGHz1LqmX4UJAh5Xqu+aYXsDCyXORpq8UQAAAAAAFAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1701,3 +1713,1036 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_llh_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_llh_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 246, 215, 34, 20, 46, 39, 0, 250, 29, 226, 186, 235, 182, 66, 64, 19, 203, + 51, 196, 24, 139, 94, 192, 31, 157, 160, 232, 122, 115, 81, 64, 0, 0, 0, 0, 9, 0, 236, + 139, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(6.98043767517560667e+01), + "incorrect value for height, expected 6.98043767517560667e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.74290689090812094e+01), + "incorrect value for lat, expected 3.74290689090812094e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22173386622027735e+02), + "incorrect value for lon, expected -1.22173386622027735e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 246, 215, 34, 20, 46, 39, 0, 161, 51, 75, 148, 235, 182, 66, 64, 36, 41, 246, + 30, 25, 139, 94, 192, 254, 218, 49, 127, 10, 108, 81, 64, 0, 0, 0, 0, 9, 1, 25, 117, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(6.96881406771535410e+01), + "incorrect value for height, expected 6.96881406771535410e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.74290643088527375e+01), + "incorrect value for lat, expected 3.74290643088527375e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22173408260718645e+02), + "incorrect value for lon, expected -1.22173408260718645e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 246, 215, 34, 120, 46, 39, 0, 56, 214, 210, 65, 235, 182, 66, 64, 13, 46, + 132, 80, 25, 139, 94, 192, 22, 143, 46, 234, 191, 95, 81, 64, 0, 0, 0, 0, 9, 0, 174, + 105, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(6.94960885481526418e+01), + "incorrect value for height, expected 6.94960885481526418e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.74290544776417278e+01), + "incorrect value for lat, expected 3.74290544776417278e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22173420075494690e+02), + "incorrect value for lon, expected -1.22173420075494690e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 246, 215, 34, 120, 46, 39, 0, 251, 117, 115, 140, 235, 182, 66, 64, 152, 134, + 167, 12, 25, 139, 94, 192, 160, 22, 137, 253, 4, 108, 81, 64, 0, 0, 0, 0, 9, 1, 122, + 127, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(6.96878045881990147e+01), + "incorrect value for height, expected 6.96878045881990147e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.74290633739255654e+01), + "incorrect value for lat, expected 3.74290633739255654e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22173403895949718e+02), + "incorrect value for lon, expected -1.22173403895949718e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 246, 215, 34, 220, 46, 39, 0, 51, 124, 88, 251, 235, 182, 66, 64, 153, 5, + 250, 16, 25, 139, 94, 192, 146, 60, 187, 219, 152, 161, 81, 64, 0, 0, 0, 0, 9, 0, 194, + 158, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(7.05249547317964982e+01), + "incorrect value for height, expected 7.05249547317964982e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.74290765935951626e+01), + "incorrect value for lat, expected 3.74290765935951626e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22173404926454523e+02), + "incorrect value for lon, expected -1.22173404926454523e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 195, 4, 34, 212, 157, 67, 24, 8, 23, 228, 8, 151, 225, 66, 64, 156, 174, 42, + 194, 230, 152, 94, 192, 153, 23, 72, 47, 196, 40, 16, 64, 0, 0, 0, 0, 8, 0, 237, 169, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(4.03981088521495568e+00), + "incorrect value for height, expected 4.03981088521495568e+00, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.77624217141838585e+01), + "incorrect value for lat, expected 3.77624217141838585e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22389084378892619e+02), + "incorrect value for lon, expected -1.22389084378892619e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084500, + "incorrect value for tow, expected 407084500, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 195, 4, 34, 56, 158, 67, 24, 220, 109, 212, 24, 151, 225, 66, 64, 159, 231, + 254, 219, 230, 152, 94, 192, 128, 151, 67, 19, 233, 105, 7, 64, 0, 0, 0, 0, 8, 0, 152, + 11, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.92671408700965685e+00), + "incorrect value for height, expected 2.92671408700965685e+00, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.77624236142398502e+01), + "incorrect value for lat, expected 3.77624236142398502e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22389090537004890e+02), + "incorrect value for lon, expected -1.22389090537004890e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084600, + "incorrect value for tow, expected 407084600, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 195, 4, 34, 156, 158, 67, 24, 13, 91, 237, 11, 151, 225, 66, 64, 75, 113, + 210, 220, 230, 152, 94, 192, 37, 6, 145, 188, 89, 112, 238, 63, 0, 0, 0, 0, 8, 0, 221, + 155, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(9.51214664739556626e-01), + "incorrect value for height, expected 9.51214664739556626e-01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.77624220761264056e+01), + "incorrect value for lat, expected 3.77624220761264056e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22389090734014800e+02), + "incorrect value for lon, expected -1.22389090734014800e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084700, + "incorrect value for tow, expected 407084700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 195, 4, 34, 0, 159, 67, 24, 51, 183, 5, 8, 151, 225, 66, 64, 13, 226, 148, + 253, 230, 152, 94, 192, 187, 27, 11, 32, 69, 213, 2, 64, 0, 0, 0, 0, 8, 0, 82, 94, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(2.35413575204753789e+00), + "incorrect value for height, expected 2.35413575204753789e+00, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.77624216106327353e+01), + "incorrect value for lat, expected 3.77624216106327353e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22389098544496122e+02), + "incorrect value for lon, expected -1.22389098544496122e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084800, + "incorrect value for tow, expected 407084800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 195, 4, 34, 100, 159, 67, 24, 22, 77, 146, 22, 151, 225, 66, 64, 64, 134, + 105, 227, 230, 152, 94, 192, 37, 99, 114, 72, 31, 103, 241, 63, 0, 0, 0, 0, 8, 0, 70, + 60, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(1.08767631816426413e+00), + "incorrect value for height, expected 1.08767631816426413e+00, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.77624233450280116e+01), + "incorrect value for lat, expected 3.77624233450280116e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22389092305232225e+02), + "incorrect value for lon, expected -1.22389092305232225e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084900, + "incorrect value for tow, expected 407084900, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 1, 2, 195, 4, 34, 46, 162, 68, 24, 124, 245, 46, 169, 151, 225, 66, 64, 135, 149, + 234, 187, 230, 152, 94, 192, 194, 201, 115, 145, 166, 175, 20, 64, 0, 0, 0, 0, 5, 0, + 212, 121, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x201, + "Incorrect message type, expected 0x201, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(5.17153384465422228e+00), + "incorrect value for height, expected 5.17153384465422228e+00, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.77624408225337618e+01), + "incorrect value for lat, expected 3.77624408225337618e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22389082888685252e+02), + "incorrect value for lon, expected -1.22389082888685252e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 5, + "incorrect value for n_sats, expected 5, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407151150, + "incorrect value for tow, expected 407151150, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_gnss.rs index a38b20e421..27408e3268 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_pos_llh_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_pos_llh_gnss() { { @@ -91,6 +96,13 @@ fn test_auto_check_sbp_navigation_msg_pos_llh_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_gnss() { @@ -98,14 +110,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_gnss() { let json_input = r#"{"tow":501867800,"lat":37.83123196497633,"lon":-122.28650381011681,"height":-17.39382124780135,"h_accuracy":87,"v_accuracy":181,"n_sats":18,"flags":4,"preamble":85,"msg_type":554,"sender":4096,"payload":"GOXpHUl7HM9l6kJAZKgTFFaSXsDWxiN40WQxwFcAtQASBA==","crc":14185,"length":34}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -174,3 +186,109 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_pos_llh_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_pos_llh_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_pos_llh_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 42, 2, 0, 16, 34, 24, 229, 233, 29, 73, 123, 28, 207, 101, 234, 66, 64, 100, 168, + 19, 20, 86, 146, 94, 192, 214, 198, 35, 120, 209, 100, 49, 192, 87, 0, 181, 0, 18, 4, + 105, 55, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgPosLlhGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgPosLlhGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x22a, + "Incorrect message type, expected 0x22a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert_eq!( + msg.flags, 4, + "incorrect value for flags, expected 4, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 87, + "incorrect value for h_accuracy, expected 87, is {}", + msg.h_accuracy + ); + assert!( + msg.height.almost_eq(-1.73938212478013483e+01), + "incorrect value for height, expected -1.73938212478013483e+01, is {:e}", + msg.height + ); + assert!( + msg.lat.almost_eq(3.78312319649763324e+01), + "incorrect value for lat, expected 3.78312319649763324e+01, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(-1.22286503810116812e+02), + "incorrect value for lon, expected -1.22286503810116812e+02, is {:e}", + msg.lon + ); + assert_eq!( + msg.n_sats, 18, + "incorrect value for n_sats, expected 18, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501867800, + "incorrect value for tow, expected 501867800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 181, + "incorrect value for v_accuracy, expected 181, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgPosLLHGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_protection_level.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_protection_level.rs index 5b671f2138..44f792a58f 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_protection_level.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_protection_level.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_protection_level() { { @@ -85,6 +90,13 @@ fn test_auto_check_sbp_navigation_msg_protection_level() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_protection_level() { @@ -92,14 +104,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_protection_level() { let json_input = r#"{"tow":501867400,"vpl":0,"hpl":0,"lat":0.0,"lon":0.0,"height":0.0,"flags":0,"preamble":85,"msg_type":534,"sender":4096,"payload":"iOPpHQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","crc":50002,"length":33}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -163,3 +175,103 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_protection_level() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_protection_level`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_protection_level() { + { + let mut payload = Cursor::new(vec![ + 85, 22, 2, 0, 16, 33, 136, 227, 233, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 195, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgProtectionLevelDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgProtectionLevelDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x216, + "Incorrect message type, expected 0x216, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert!( + msg.height.almost_eq(0.00000000000000000e+00), + "incorrect value for height, expected 0.00000000000000000e+00, is {:e}", + msg.height + ); + assert_eq!( + msg.hpl, 0, + "incorrect value for hpl, expected 0, is {}", + msg.hpl + ); + assert!( + msg.lat.almost_eq(0.00000000000000000e+00), + "incorrect value for lat, expected 0.00000000000000000e+00, is {:e}", + msg.lat + ); + assert!( + msg.lon.almost_eq(0.00000000000000000e+00), + "incorrect value for lon, expected 0.00000000000000000e+00, is {:e}", + msg.lon + ); + assert_eq!( + msg.tow, 501867400, + "incorrect value for tow, expected 501867400, is {}", + msg.tow + ); + assert_eq!( + msg.vpl, 0, + "incorrect value for vpl, expected 0, is {}", + msg.vpl + ); + } + _ => panic!("Invalid message type! Expected a MsgProtectionLevelDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_reference_frame_param.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_reference_frame_param.rs index 9307537993..1b9d62f659 100644 Binary files a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_reference_frame_param.rs and b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_reference_frame_param.rs differ diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_leap_second.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_leap_second.rs index 66e696661d..14ae084c43 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_leap_second.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_leap_second.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_utc_leap_second() { { @@ -94,6 +99,13 @@ fn test_auto_check_sbp_navigation_msg_utc_leap_second() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_utc_leap_second() { @@ -101,14 +113,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_utc_leap_second() { let json_input = r#"{"bias_coeff": 1, "drift_coeff": 2, "drift_rate_coeff": 3, "count_before": 4, "tow_s": 5, "wn": 6, "ref_wn": 7, "ref_dn": 8, "count_after": 9, "preamble": 85, "msg_type": 570, "sender": 66, "length": 14, "payload": "AQACAAMEBQAGAAcACAk=", "crc": 59442}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -182,3 +194,112 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_utc_leap_second() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_utc_leap_second`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_utc_leap_second() { + { + let mut payload = Cursor::new(vec![ + 85, 58, 2, 66, 0, 14, 1, 0, 2, 0, 3, 4, 5, 0, 6, 0, 7, 0, 8, 9, 50, 232, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUtcLeapSecond( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUtcLeapSecond(msg) => { + assert_eq!( + msg.message_type(), + 570, + "Incorrect message type, expected 570, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.bias_coeff, 1, + "incorrect value for bias_coeff, expected 1, is {}", + msg.bias_coeff + ); + assert_eq!( + msg.count_after, 9, + "incorrect value for count_after, expected 9, is {}", + msg.count_after + ); + assert_eq!( + msg.count_before, 4, + "incorrect value for count_before, expected 4, is {}", + msg.count_before + ); + assert_eq!( + msg.drift_coeff, 2, + "incorrect value for drift_coeff, expected 2, is {}", + msg.drift_coeff + ); + assert_eq!( + msg.drift_rate_coeff, 3, + "incorrect value for drift_rate_coeff, expected 3, is {}", + msg.drift_rate_coeff + ); + assert_eq!( + msg.ref_dn, 8, + "incorrect value for ref_dn, expected 8, is {}", + msg.ref_dn + ); + assert_eq!( + msg.ref_wn, 7, + "incorrect value for ref_wn, expected 7, is {}", + msg.ref_wn + ); + assert_eq!( + msg.tow_s, 5, + "incorrect value for tow_s, expected 5, is {}", + msg.tow_s + ); + assert_eq!( + msg.wn, 6, + "incorrect value for wn, expected 6, is {}", + msg.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgUtcLeapSecond"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time.rs index 3ce8e2df5a..61dfa99f6c 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_utc_time() { { @@ -95,6 +100,13 @@ fn test_auto_check_sbp_navigation_msg_utc_time() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_utc_time() { @@ -102,14 +114,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_utc_time() { let json_input = r#"{"flags":1,"tow":501867800,"year":2021,"month":4,"day":9,"hours":19,"minutes":24,"seconds":9,"ns":800000000,"preamble":85,"msg_type":259,"sender":789,"payload":"ARjl6R3lBwQJExgJAAivLw==","crc":64967,"length":16}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -183,3 +195,113 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_utc_time() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_utc_time`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_utc_time() { + { + let mut payload = Cursor::new(vec![ + 85, 3, 1, 21, 3, 16, 1, 24, 229, 233, 29, 229, 7, 4, 9, 19, 24, 9, 0, 8, 175, 47, 199, + 253, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUtcTime( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUtcTime(msg) => { + assert_eq!( + msg.message_type(), + 0x103, + "Incorrect message type, expected 0x103, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x315, + "incorrect sender id, expected 0x315, is {}", + sender_id + ); + assert_eq!( + msg.day, 9, + "incorrect value for day, expected 9, is {}", + msg.day + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.hours, 19, + "incorrect value for hours, expected 19, is {}", + msg.hours + ); + assert_eq!( + msg.minutes, 24, + "incorrect value for minutes, expected 24, is {}", + msg.minutes + ); + assert_eq!( + msg.month, 4, + "incorrect value for month, expected 4, is {}", + msg.month + ); + assert_eq!( + msg.ns, 800000000, + "incorrect value for ns, expected 800000000, is {}", + msg.ns + ); + assert_eq!( + msg.seconds, 9, + "incorrect value for seconds, expected 9, is {}", + msg.seconds + ); + assert_eq!( + msg.tow, 501867800, + "incorrect value for tow, expected 501867800, is {}", + msg.tow + ); + assert_eq!( + msg.year, 2021, + "incorrect value for year, expected 2021, is {}", + msg.year + ); + } + _ => panic!("Invalid message type! Expected a MsgUtcTime"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time_gnss.rs index 5a189bcdeb..ccf70c1366 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_utc_time_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_utc_time_gnss() { { @@ -95,6 +100,13 @@ fn test_auto_check_sbp_navigation_msg_utc_time_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_utc_time_gnss() { @@ -102,14 +114,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_utc_time_gnss() { let json_input = r#"{"flags":1,"tow":501867800,"year":2021,"month":4,"day":9,"hours":19,"minutes":24,"seconds":9,"ns":800000000,"preamble":85,"msg_type":261,"sender":789,"payload":"ARjl6R3lBwQJExgJAAivLw==","crc":8625,"length":16}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -183,3 +195,113 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_utc_time_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_utc_time_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_utc_time_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 5, 1, 21, 3, 16, 1, 24, 229, 233, 29, 229, 7, 4, 9, 19, 24, 9, 0, 8, 175, 47, 177, + 33, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUtcTimeGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUtcTimeGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x105, + "Incorrect message type, expected 0x105, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x315, + "incorrect sender id, expected 0x315, is {}", + sender_id + ); + assert_eq!( + msg.day, 9, + "incorrect value for day, expected 9, is {}", + msg.day + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.hours, 19, + "incorrect value for hours, expected 19, is {}", + msg.hours + ); + assert_eq!( + msg.minutes, 24, + "incorrect value for minutes, expected 24, is {}", + msg.minutes + ); + assert_eq!( + msg.month, 4, + "incorrect value for month, expected 4, is {}", + msg.month + ); + assert_eq!( + msg.ns, 800000000, + "incorrect value for ns, expected 800000000, is {}", + msg.ns + ); + assert_eq!( + msg.seconds, 9, + "incorrect value for seconds, expected 9, is {}", + msg.seconds + ); + assert_eq!( + msg.tow, 501867800, + "incorrect value for tow, expected 501867800, is {}", + msg.tow + ); + assert_eq!( + msg.year, 2021, + "incorrect value for year, expected 2021, is {}", + msg.year + ); + } + _ => panic!("Invalid message type! Expected a MsgUtcTimeGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_body.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_body.rs index 150a6d6d8e..a58055b480 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_body.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_body.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_body() { { @@ -98,6 +103,13 @@ fn test_auto_check_sbp_navigation_msg_vel_body() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_body() { @@ -105,14 +117,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_body() { let json_input = r#"{"cov_y_z": 3, "cov_y_y": 7, "n_sats": 3, "sender": 66, "msg_type": 531, "cov_x_z": 7, "cov_x_y": 5, "cov_x_x": 0, "tow": 1, "crc": 36984, "length": 42, "flags": 8, "y": 2, "x": 4, "z": 1, "preamble": 85, "payload": "AQAAAAQAAAACAAAAAQAAAAAAAAAAAKBAAADgQAAA4EAAAEBAAAAAQAMI", "cov_z_z": 2}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -189,3 +201,116 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_body() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_body`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_body() { + { + let mut payload = Cursor::new(vec![ + 85, 19, 2, 66, 0, 42, 1, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 160, 64, 0, 0, 224, 64, 0, 0, 224, 64, 0, 0, 64, 64, 0, 0, 0, 64, 3, 8, 120, 144, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelBody( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelBody(msg) => { + assert_eq!( + msg.message_type(), + 0x213, + "Incorrect message type, expected 0x213, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert!( + msg.cov_x_x.almost_eq(0.00000000000000000e+00), + "incorrect value for cov_x_x, expected 0.00000000000000000e+00, is {:e}", + msg.cov_x_x + ); + assert!( + msg.cov_x_y.almost_eq(5.00000000000000000e+00), + "incorrect value for cov_x_y, expected 5.00000000000000000e+00, is {:e}", + msg.cov_x_y + ); + assert!( + msg.cov_x_z.almost_eq(7.00000000000000000e+00), + "incorrect value for cov_x_z, expected 7.00000000000000000e+00, is {:e}", + msg.cov_x_z + ); + assert!( + msg.cov_y_y.almost_eq(7.00000000000000000e+00), + "incorrect value for cov_y_y, expected 7.00000000000000000e+00, is {:e}", + msg.cov_y_y + ); + assert!( + msg.cov_y_z.almost_eq(3.00000000000000000e+00), + "incorrect value for cov_y_z, expected 3.00000000000000000e+00, is {:e}", + msg.cov_y_z + ); + assert!( + msg.cov_z_z.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_z_z, expected 2.00000000000000000e+00, is {:e}", + msg.cov_z_z + ); + assert_eq!( + msg.flags, 8, + "incorrect value for flags, expected 8, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 3, + "incorrect value for n_sats, expected 3, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 1, + "incorrect value for tow, expected 1, is {}", + msg.tow + ); + assert_eq!(msg.x, 4, "incorrect value for x, expected 4, is {}", msg.x); + assert_eq!(msg.y, 2, "incorrect value for y, expected 2, is {}", msg.y); + assert_eq!(msg.z, 1, "incorrect value for z, expected 1, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgVelBody"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_cog.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_cog.rs index e579b85570..b7121c6e91 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_cog.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_cog.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_cog() { { @@ -236,6 +241,13 @@ fn test_auto_check_sbp_navigation_msg_vel_cog() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_cog() { @@ -243,14 +255,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_cog() { let json_input = r#"{"crc":49620,"length":30,"msg_type":540,"payload": "MPZ6E+gDAADQBwAAuAsAAKAPAACIEwAAcBcAAD4A","preamble":85,"sender":35027, "tow":326825520,"cog":1000,"sog":2000,"v_up":3000,"cog_accuracy":4000, "sog_accuracy":5000,"v_up_accuracy":6000,"flags":62}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -322,14 +334,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_cog() { let json_input = r#"{"crc":29274,"length":30,"msg_type":540,"payload": "MPZ6E3sAAADIAQAAGPz//wCVugpkAAAAZAAAAAAA","preamble":85,"sender":35027, "tow":326825520,"cog":123,"sog":456,"v_up":-1000,"cog_accuracy":180000000, "sog_accuracy":100,"v_up_accuracy":100,"flags":0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -401,14 +413,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_cog() { let json_input = r#"{"crc":53802,"length":30,"msg_type":540,"payload": "MPZ6EwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","preamble":85,"sender":35027, "tow":326825520,"cog":0,"sog":0,"v_up":0,"cog_accuracy":0,"sog_accuracy":0, "v_up_accuracy":0,"flags":0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -477,3 +489,292 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_cog() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_cog`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_cog() { + { + let mut payload = Cursor::new(vec![ + 85, 28, 2, 211, 136, 30, 48, 246, 122, 19, 232, 3, 0, 0, 208, 7, 0, 0, 184, 11, 0, 0, + 160, 15, 0, 0, 136, 19, 0, 0, 112, 23, 0, 0, 62, 0, 212, 193, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelCog( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelCog(msg) => { + assert_eq!( + msg.message_type(), + 0x21C, + "Incorrect message type, expected 0x21C, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88D3, + "incorrect sender id, expected 0x88D3, is {}", + sender_id + ); + assert_eq!( + msg.cog, 1000, + "incorrect value for cog, expected 1000, is {}", + msg.cog + ); + assert_eq!( + msg.cog_accuracy, 4000, + "incorrect value for cog_accuracy, expected 4000, is {}", + msg.cog_accuracy + ); + assert_eq!( + msg.flags, 62, + "incorrect value for flags, expected 62, is {}", + msg.flags + ); + assert_eq!( + msg.sog, 2000, + "incorrect value for sog, expected 2000, is {}", + msg.sog + ); + assert_eq!( + msg.sog_accuracy, 5000, + "incorrect value for sog_accuracy, expected 5000, is {}", + msg.sog_accuracy + ); + assert_eq!( + msg.tow, 326825520, + "incorrect value for tow, expected 326825520, is {}", + msg.tow + ); + assert_eq!( + msg.v_up, 3000, + "incorrect value for v_up, expected 3000, is {}", + msg.v_up + ); + assert_eq!( + msg.v_up_accuracy, 6000, + "incorrect value for v_up_accuracy, expected 6000, is {}", + msg.v_up_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelCog"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 28, 2, 211, 136, 30, 48, 246, 122, 19, 123, 0, 0, 0, 200, 1, 0, 0, 24, 252, 255, + 255, 0, 149, 186, 10, 100, 0, 0, 0, 100, 0, 0, 0, 0, 0, 90, 114, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelCog( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelCog(msg) => { + assert_eq!( + msg.message_type(), + 0x21C, + "Incorrect message type, expected 0x21C, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88D3, + "incorrect sender id, expected 0x88D3, is {}", + sender_id + ); + assert_eq!( + msg.cog, 123, + "incorrect value for cog, expected 123, is {}", + msg.cog + ); + assert_eq!( + msg.cog_accuracy, 180000000, + "incorrect value for cog_accuracy, expected 180000000, is {}", + msg.cog_accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.sog, 456, + "incorrect value for sog, expected 456, is {}", + msg.sog + ); + assert_eq!( + msg.sog_accuracy, 100, + "incorrect value for sog_accuracy, expected 100, is {}", + msg.sog_accuracy + ); + assert_eq!( + msg.tow, 326825520, + "incorrect value for tow, expected 326825520, is {}", + msg.tow + ); + assert_eq!( + msg.v_up, -1000, + "incorrect value for v_up, expected -1000, is {}", + msg.v_up + ); + assert_eq!( + msg.v_up_accuracy, 100, + "incorrect value for v_up_accuracy, expected 100, is {}", + msg.v_up_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelCog"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 28, 2, 211, 136, 30, 48, 246, 122, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 210, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelCog( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelCog(msg) => { + assert_eq!( + msg.message_type(), + 0x21C, + "Incorrect message type, expected 0x21C, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88D3, + "incorrect sender id, expected 0x88D3, is {}", + sender_id + ); + assert_eq!( + msg.cog, 0, + "incorrect value for cog, expected 0, is {}", + msg.cog + ); + assert_eq!( + msg.cog_accuracy, 0, + "incorrect value for cog_accuracy, expected 0, is {}", + msg.cog_accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.sog, 0, + "incorrect value for sog, expected 0, is {}", + msg.sog + ); + assert_eq!( + msg.sog_accuracy, 0, + "incorrect value for sog_accuracy, expected 0, is {}", + msg.sog_accuracy + ); + assert_eq!( + msg.tow, 326825520, + "incorrect value for tow, expected 326825520, is {}", + msg.tow + ); + assert_eq!( + msg.v_up, 0, + "incorrect value for v_up, expected 0, is {}", + msg.v_up + ); + assert_eq!( + msg.v_up_accuracy, 0, + "incorrect value for v_up_accuracy, expected 0, is {}", + msg.v_up_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelCog"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef.rs index 586de62db1..d187bebe21 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ecef() { { @@ -353,6 +358,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ecef() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { @@ -360,14 +372,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { let json_input = r#"{"n_sats": 14, "sender": 35027, "msg_type": 525, "tow": 326825000, "crc": 25525, "length": 20, "flags": 0, "y": -5, "x": -8, "z": 10, "preamble": 85, "payload": "KPR6E/j////7////CgAAAAAADgA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -434,14 +446,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 525, "tow": 326825500, "crc": 30935, "length": 20, "flags": 0, "y": -18, "x": -12, "z": 11, "preamble": 85, "payload": "HPZ6E/T////u////CwAAAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -508,14 +520,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 525, "tow": 326826000, "crc": 56824, "length": 20, "flags": 0, "y": -6, "x": -8, "z": 7, "preamble": 85, "payload": "EPh6E/j////6////BwAAAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -578,14 +590,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 525, "tow": 326826500, "crc": 42753, "length": 20, "flags": 0, "y": -17, "x": -7, "z": 16, "preamble": 85, "payload": "BPp6E/n////v////EAAAAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -652,14 +664,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { let json_input = r#"{"n_sats": 15, "sender": 35027, "msg_type": 525, "tow": 326827000, "crc": 11199, "length": 20, "flags": 0, "y": -13, "x": -9, "z": 14, "preamble": 85, "payload": "+Pt6E/f////z////DgAAAAAADwA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -723,3 +735,447 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ecef`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ecef() { + { + let mut payload = Cursor::new(vec![ + 85, 13, 2, 211, 136, 20, 40, 244, 122, 19, 248, 255, 255, 255, 251, 255, 255, 255, 10, + 0, 0, 0, 0, 0, 14, 0, 181, 99, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20d, + "Incorrect message type, expected 0x20d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 14, + "incorrect value for n_sats, expected 14, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -8, + "incorrect value for x, expected -8, is {}", + msg.x + ); + assert_eq!( + msg.y, -5, + "incorrect value for y, expected -5, is {}", + msg.y + ); + assert_eq!( + msg.z, 10, + "incorrect value for z, expected 10, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 13, 2, 211, 136, 20, 28, 246, 122, 19, 244, 255, 255, 255, 238, 255, 255, 255, 11, + 0, 0, 0, 0, 0, 15, 0, 215, 120, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20d, + "Incorrect message type, expected 0x20d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825500, + "incorrect value for tow, expected 326825500, is {}", + msg.tow + ); + assert_eq!( + msg.x, -12, + "incorrect value for x, expected -12, is {}", + msg.x + ); + assert_eq!( + msg.y, -18, + "incorrect value for y, expected -18, is {}", + msg.y + ); + assert_eq!( + msg.z, 11, + "incorrect value for z, expected 11, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 13, 2, 211, 136, 20, 16, 248, 122, 19, 248, 255, 255, 255, 250, 255, 255, 255, 7, + 0, 0, 0, 0, 0, 15, 0, 248, 221, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20d, + "Incorrect message type, expected 0x20d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -8, + "incorrect value for x, expected -8, is {}", + msg.x + ); + assert_eq!( + msg.y, -6, + "incorrect value for y, expected -6, is {}", + msg.y + ); + assert_eq!(msg.z, 7, "incorrect value for z, expected 7, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgVelECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 13, 2, 211, 136, 20, 4, 250, 122, 19, 249, 255, 255, 255, 239, 255, 255, 255, 16, + 0, 0, 0, 0, 0, 15, 0, 1, 167, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20d, + "Incorrect message type, expected 0x20d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826500, + "incorrect value for tow, expected 326826500, is {}", + msg.tow + ); + assert_eq!( + msg.x, -7, + "incorrect value for x, expected -7, is {}", + msg.x + ); + assert_eq!( + msg.y, -17, + "incorrect value for y, expected -17, is {}", + msg.y + ); + assert_eq!( + msg.z, 16, + "incorrect value for z, expected 16, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 13, 2, 211, 136, 20, 248, 251, 122, 19, 247, 255, 255, 255, 243, 255, 255, 255, 14, + 0, 0, 0, 0, 0, 15, 0, 191, 43, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x20d, + "Incorrect message type, expected 0x20d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -9, + "incorrect value for x, expected -9, is {}", + msg.x + ); + assert_eq!( + msg.y, -13, + "incorrect value for y, expected -13, is {}", + msg.y + ); + assert_eq!( + msg.z, 14, + "incorrect value for z, expected 14, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov.rs index 13dfea7978..cf6a8e5185 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ecef_cov() { { @@ -98,6 +103,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ecef_cov() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_cov() { @@ -105,14 +117,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_cov() { let json_input = r#"{"cov_y_z": 1, "cov_y_y": 2, "n_sats": 3, "sender": 66, "msg_type": 533, "cov_x_z": 2, "cov_x_y": 2, "cov_x_x": 2, "tow": 2, "crc": 65115, "length": 42, "flags": 4, "y": 0, "x": 0, "z": 6, "preamble": 85, "payload": "AgAAAAAAAAAAAAAABgAAAAAAAEAAAABAAAAAQAAAAEAAAIA/AABAQAME", "cov_z_z": 3}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -189,3 +201,116 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_cov() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ecef_cov`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ecef_cov() { + { + let mut payload = Cursor::new(vec![ + 85, 21, 2, 66, 0, 42, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 64, 0, + 0, 0, 64, 0, 0, 0, 64, 0, 0, 0, 64, 0, 0, 128, 63, 0, 0, 64, 64, 3, 4, 91, 254, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefCov( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefCov(msg) => { + assert_eq!( + msg.message_type(), + 0x215, + "Incorrect message type, expected 0x215, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert!( + msg.cov_x_x.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_x_x, expected 2.00000000000000000e+00, is {:e}", + msg.cov_x_x + ); + assert!( + msg.cov_x_y.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_x_y, expected 2.00000000000000000e+00, is {:e}", + msg.cov_x_y + ); + assert!( + msg.cov_x_z.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_x_z, expected 2.00000000000000000e+00, is {:e}", + msg.cov_x_z + ); + assert!( + msg.cov_y_y.almost_eq(2.00000000000000000e+00), + "incorrect value for cov_y_y, expected 2.00000000000000000e+00, is {:e}", + msg.cov_y_y + ); + assert!( + msg.cov_y_z.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_y_z, expected 1.00000000000000000e+00, is {:e}", + msg.cov_y_z + ); + assert!( + msg.cov_z_z.almost_eq(3.00000000000000000e+00), + "incorrect value for cov_z_z, expected 3.00000000000000000e+00, is {:e}", + msg.cov_z_z + ); + assert_eq!( + msg.flags, 4, + "incorrect value for flags, expected 4, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 3, + "incorrect value for n_sats, expected 3, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2, + "incorrect value for tow, expected 2, is {}", + msg.tow + ); + assert_eq!(msg.x, 0, "incorrect value for x, expected 0, is {}", msg.x); + assert_eq!(msg.y, 0, "incorrect value for y, expected 0, is {}", msg.y); + assert_eq!(msg.z, 6, "incorrect value for z, expected 6, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFCov"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov_gnss.rs index 8f78419471..fcff23f687 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_cov_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss() { { @@ -103,6 +108,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss() { @@ -110,14 +122,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss() { let json_input = r#"{"tow":501868000,"x":-3,"y":1,"z":4,"cov_x_x":0.0024547684006392956,"cov_x_y":0.0021795108914375305,"cov_x_z":-0.0016828652005642653,"cov_y_y":0.004218944814056158,"cov_y_z":-0.0024961293675005436,"cov_z_z":0.0037804271560162306,"n_sats":21,"flags":2,"preamble":85,"msg_type":565,"sender":4096,"payload":"4OXpHf3///8BAAAABAAAAC7gIDsg1g47lpPcuhM/ijsaliO7C8F3OxUC","crc":58902,"length":42}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -198,3 +210,121 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ecef_cov_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 53, 2, 0, 16, 42, 224, 229, 233, 29, 253, 255, 255, 255, 1, 0, 0, 0, 4, 0, 0, 0, + 46, 224, 32, 59, 32, 214, 14, 59, 150, 147, 220, 186, 19, 63, 138, 59, 26, 150, 35, + 187, 11, 193, 119, 59, 21, 2, 22, 230, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefCovGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefCovGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x235, + "Incorrect message type, expected 0x235, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert!( + msg.cov_x_x.almost_eq(2.45476840063929558e-03), + "incorrect value for cov_x_x, expected 2.45476840063929558e-03, is {:e}", + msg.cov_x_x + ); + assert!( + msg.cov_x_y.almost_eq(2.17951089143753052e-03), + "incorrect value for cov_x_y, expected 2.17951089143753052e-03, is {:e}", + msg.cov_x_y + ); + assert!( + msg.cov_x_z.almost_eq(-1.68286520056426525e-03), + "incorrect value for cov_x_z, expected -1.68286520056426525e-03, is {:e}", + msg.cov_x_z + ); + assert!( + msg.cov_y_y.almost_eq(4.21894481405615807e-03), + "incorrect value for cov_y_y, expected 4.21894481405615807e-03, is {:e}", + msg.cov_y_y + ); + assert!( + msg.cov_y_z.almost_eq(-2.49612936750054359e-03), + "incorrect value for cov_y_z, expected -2.49612936750054359e-03, is {:e}", + msg.cov_y_z + ); + assert!( + msg.cov_z_z.almost_eq(3.78042715601623058e-03), + "incorrect value for cov_z_z, expected 3.78042715601623058e-03, is {:e}", + msg.cov_z_z + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 21, + "incorrect value for n_sats, expected 21, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501868000, + "incorrect value for tow, expected 501868000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -3, + "incorrect value for x, expected -3, is {}", + msg.x + ); + assert_eq!(msg.y, 1, "incorrect value for y, expected 1, is {}", msg.y); + assert_eq!(msg.z, 4, "incorrect value for z, expected 4, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFCovGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_dep_a.rs index fd7fb1f858..ac6d7d08f7 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { { @@ -749,6 +754,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { @@ -756,14 +768,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 516, "tow": 2567700, "crc": 60496, "length": 20, "flags": 0, "y": -2682, "x": 3034, "z": -861, "preamble": 85, "payload": "FC4nANoLAACG9f//o/z//wAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -830,14 +842,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 516, "tow": 2567800, "crc": 35576, "length": 20, "flags": 0, "y": -2536, "x": 2884, "z": -804, "preamble": 85, "payload": "eC4nAEQLAAAY9v//3Pz//wAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -904,14 +916,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 516, "tow": 2567900, "crc": 44569, "length": 20, "flags": 0, "y": -2483, "x": 2837, "z": -777, "preamble": 85, "payload": "3C4nABULAABN9v//9/z//wAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -978,14 +990,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 516, "tow": 2568000, "crc": 58563, "length": 20, "flags": 0, "y": -2558, "x": 2937, "z": -790, "preamble": 85, "payload": "QC8nAHkLAAAC9v//6vz//wAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1052,14 +1064,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 9, "sender": 55286, "msg_type": 516, "tow": 2568100, "crc": 42203, "length": 20, "flags": 0, "y": -2467, "x": 2847, "z": -752, "preamble": 85, "payload": "pC8nAB8LAABd9v//EP3//wAACQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1126,14 +1138,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 516, "tow": 407084500, "crc": 65348, "length": 20, "flags": 0, "y": -11, "x": 24, "z": -37, "preamble": 85, "payload": "1J1DGBgAAAD1////2////wAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1200,14 +1212,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 516, "tow": 407084600, "crc": 35030, "length": 20, "flags": 0, "y": -22, "x": 4, "z": 18, "preamble": 85, "payload": "OJ5DGAQAAADq////EgAAAAAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1270,14 +1282,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 516, "tow": 407084700, "crc": 40826, "length": 20, "flags": 0, "y": 4, "x": -26, "z": 1, "preamble": 85, "payload": "nJ5DGOb///8EAAAAAQAAAAAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1336,14 +1348,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 516, "tow": 407084800, "crc": 37608, "length": 20, "flags": 0, "y": -19, "x": -9, "z": 28, "preamble": 85, "payload": "AJ9DGPf////t////HAAAAAAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1410,14 +1422,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 8, "sender": 1219, "msg_type": 516, "tow": 407084900, "crc": 61099, "length": 20, "flags": 0, "y": 2, "x": -1, "z": -11, "preamble": 85, "payload": "ZJ9DGP////8CAAAA9f///wAACAA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1480,14 +1492,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { let json_input = r#"{"n_sats": 5, "sender": 1219, "msg_type": 516, "tow": 407151150, "crc": 39506, "length": 20, "flags": 0, "y": -71, "x": -49, "z": 65, "preamble": 85, "payload": "LqJEGM////+5////QQAAAAAABQA=", "accuracy": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1551,3 +1563,957 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ecef_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ecef_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 246, 215, 20, 20, 46, 39, 0, 218, 11, 0, 0, 134, 245, 255, 255, 163, 252, + 255, 255, 0, 0, 9, 0, 80, 236, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert_eq!( + msg.x, 3034, + "incorrect value for x, expected 3034, is {}", + msg.x + ); + assert_eq!( + msg.y, -2682, + "incorrect value for y, expected -2682, is {}", + msg.y + ); + assert_eq!( + msg.z, -861, + "incorrect value for z, expected -861, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 246, 215, 20, 120, 46, 39, 0, 68, 11, 0, 0, 24, 246, 255, 255, 220, 252, 255, + 255, 0, 0, 9, 0, 248, 138, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.x, 2884, + "incorrect value for x, expected 2884, is {}", + msg.x + ); + assert_eq!( + msg.y, -2536, + "incorrect value for y, expected -2536, is {}", + msg.y + ); + assert_eq!( + msg.z, -804, + "incorrect value for z, expected -804, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 246, 215, 20, 220, 46, 39, 0, 21, 11, 0, 0, 77, 246, 255, 255, 247, 252, 255, + 255, 0, 0, 9, 0, 25, 174, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert_eq!( + msg.x, 2837, + "incorrect value for x, expected 2837, is {}", + msg.x + ); + assert_eq!( + msg.y, -2483, + "incorrect value for y, expected -2483, is {}", + msg.y + ); + assert_eq!( + msg.z, -777, + "incorrect value for z, expected -777, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 246, 215, 20, 64, 47, 39, 0, 121, 11, 0, 0, 2, 246, 255, 255, 234, 252, 255, + 255, 0, 0, 9, 0, 195, 228, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568000, + "incorrect value for tow, expected 2568000, is {}", + msg.tow + ); + assert_eq!( + msg.x, 2937, + "incorrect value for x, expected 2937, is {}", + msg.x + ); + assert_eq!( + msg.y, -2558, + "incorrect value for y, expected -2558, is {}", + msg.y + ); + assert_eq!( + msg.z, -790, + "incorrect value for z, expected -790, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 246, 215, 20, 164, 47, 39, 0, 31, 11, 0, 0, 93, 246, 255, 255, 16, 253, 255, + 255, 0, 0, 9, 0, 219, 164, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568100, + "incorrect value for tow, expected 2568100, is {}", + msg.tow + ); + assert_eq!( + msg.x, 2847, + "incorrect value for x, expected 2847, is {}", + msg.x + ); + assert_eq!( + msg.y, -2467, + "incorrect value for y, expected -2467, is {}", + msg.y + ); + assert_eq!( + msg.z, -752, + "incorrect value for z, expected -752, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 195, 4, 20, 212, 157, 67, 24, 24, 0, 0, 0, 245, 255, 255, 255, 219, 255, 255, + 255, 0, 0, 8, 0, 68, 255, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084500, + "incorrect value for tow, expected 407084500, is {}", + msg.tow + ); + assert_eq!( + msg.x, 24, + "incorrect value for x, expected 24, is {}", + msg.x + ); + assert_eq!( + msg.y, -11, + "incorrect value for y, expected -11, is {}", + msg.y + ); + assert_eq!( + msg.z, -37, + "incorrect value for z, expected -37, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 195, 4, 20, 56, 158, 67, 24, 4, 0, 0, 0, 234, 255, 255, 255, 18, 0, 0, 0, 0, + 0, 8, 0, 214, 136, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084600, + "incorrect value for tow, expected 407084600, is {}", + msg.tow + ); + assert_eq!(msg.x, 4, "incorrect value for x, expected 4, is {}", msg.x); + assert_eq!( + msg.y, -22, + "incorrect value for y, expected -22, is {}", + msg.y + ); + assert_eq!( + msg.z, 18, + "incorrect value for z, expected 18, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 195, 4, 20, 156, 158, 67, 24, 230, 255, 255, 255, 4, 0, 0, 0, 1, 0, 0, 0, 0, + 0, 8, 0, 122, 159, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084700, + "incorrect value for tow, expected 407084700, is {}", + msg.tow + ); + assert_eq!( + msg.x, -26, + "incorrect value for x, expected -26, is {}", + msg.x + ); + assert_eq!(msg.y, 4, "incorrect value for y, expected 4, is {}", msg.y); + assert_eq!(msg.z, 1, "incorrect value for z, expected 1, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 195, 4, 20, 0, 159, 67, 24, 247, 255, 255, 255, 237, 255, 255, 255, 28, 0, 0, + 0, 0, 0, 8, 0, 232, 146, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084800, + "incorrect value for tow, expected 407084800, is {}", + msg.tow + ); + assert_eq!( + msg.x, -9, + "incorrect value for x, expected -9, is {}", + msg.x + ); + assert_eq!( + msg.y, -19, + "incorrect value for y, expected -19, is {}", + msg.y + ); + assert_eq!( + msg.z, 28, + "incorrect value for z, expected 28, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 195, 4, 20, 100, 159, 67, 24, 255, 255, 255, 255, 2, 0, 0, 0, 245, 255, 255, + 255, 0, 0, 8, 0, 171, 238, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084900, + "incorrect value for tow, expected 407084900, is {}", + msg.tow + ); + assert_eq!( + msg.x, -1, + "incorrect value for x, expected -1, is {}", + msg.x + ); + assert_eq!(msg.y, 2, "incorrect value for y, expected 2, is {}", msg.y); + assert_eq!( + msg.z, -11, + "incorrect value for z, expected -11, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 4, 2, 195, 4, 20, 46, 162, 68, 24, 207, 255, 255, 255, 185, 255, 255, 255, 65, 0, + 0, 0, 0, 0, 5, 0, 82, 154, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x204, + "Incorrect message type, expected 0x204, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 0, + "incorrect value for accuracy, expected 0, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 5, + "incorrect value for n_sats, expected 5, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407151150, + "incorrect value for tow, expected 407151150, is {}", + msg.tow + ); + assert_eq!( + msg.x, -49, + "incorrect value for x, expected -49, is {}", + msg.x + ); + assert_eq!( + msg.y, -71, + "incorrect value for y, expected -71, is {}", + msg.y + ); + assert_eq!( + msg.z, 65, + "incorrect value for z, expected 65, is {}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_gnss.rs index f1d7ccd132..871326cd5f 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ecef_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ecef_gnss() { { @@ -77,6 +82,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ecef_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_gnss() { @@ -84,14 +96,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_gnss() { let json_input = r#"{"tow":501868000,"x":-3,"y":1,"z":4,"accuracy":89,"n_sats":21,"flags":2,"preamble":85,"msg_type":557,"sender":4096,"payload":"4OXpHf3///8BAAAABAAAAFkAFQI=","crc":4301,"length":20}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -147,3 +159,95 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ecef_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ecef_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ecef_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 45, 2, 0, 16, 20, 224, 229, 233, 29, 253, 255, 255, 255, 1, 0, 0, 0, 4, 0, 0, 0, + 89, 0, 21, 2, 205, 16, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelEcefGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelEcefGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x22d, + "Incorrect message type, expected 0x22d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert_eq!( + msg.accuracy, 89, + "incorrect value for accuracy, expected 89, is {}", + msg.accuracy + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n_sats, 21, + "incorrect value for n_sats, expected 21, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501868000, + "incorrect value for tow, expected 501868000, is {}", + msg.tow + ); + assert_eq!( + msg.x, -3, + "incorrect value for x, expected -3, is {}", + msg.x + ); + assert_eq!(msg.y, 1, "incorrect value for y, expected 1, is {}", msg.y); + assert_eq!(msg.z, 4, "incorrect value for z, expected 4, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgVelECEFGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned.rs index 7cfde1b4d5..ffa046f398 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ned() { { @@ -358,6 +363,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ned() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { @@ -365,14 +377,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { let json_input = r#"{"v_accuracy": 0, "e": -4, "sender": 35027, "msg_type": 526, "tow": 326825000, "n": 3, "crc": 53590, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 14, "preamble": 85, "payload": "KPR6EwMAAAD8////8////wAAAAAOAA==", "d": -13}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -440,14 +452,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { let json_input = r#"{"v_accuracy": 0, "e": -1, "sender": 35027, "msg_type": 526, "tow": 326825500, "n": -4, "crc": 58384, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "HPZ6E/z/////////6P///wAAAAAPAA==", "d": -24}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -519,14 +531,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { let json_input = r#"{"v_accuracy": 0, "e": -3, "sender": 35027, "msg_type": 526, "tow": 326826000, "n": 0, "crc": 41995, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "EPh6EwAAAAD9////9P///wAAAAAPAA==", "d": -12}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -594,14 +606,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { let json_input = r#"{"v_accuracy": 0, "e": 3, "sender": 35027, "msg_type": 526, "tow": 326826500, "n": 2, "crc": 53400, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "BPp6EwIAAAADAAAA6P///wAAAAAPAA==", "d": -24}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -665,14 +677,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { let json_input = r#"{"v_accuracy": 0, "e": 0, "sender": 35027, "msg_type": 526, "tow": 326827000, "n": 1, "crc": 30902, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 15, "preamble": 85, "payload": "+Pt6EwEAAAAAAAAA6////wAAAAAPAA==", "d": -21}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -733,3 +745,452 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ned`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ned() { + { + let mut payload = Cursor::new(vec![ + 85, 14, 2, 211, 136, 22, 40, 244, 122, 19, 3, 0, 0, 0, 252, 255, 255, 255, 243, 255, + 255, 255, 0, 0, 0, 0, 14, 0, 86, 209, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20e, + "Incorrect message type, expected 0x20e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, -13, + "incorrect value for d, expected -13, is {}", + msg.d + ); + assert_eq!( + msg.e, -4, + "incorrect value for e, expected -4, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!(msg.n, 3, "incorrect value for n, expected 3, is {}", msg.n); + assert_eq!( + msg.n_sats, 14, + "incorrect value for n_sats, expected 14, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825000, + "incorrect value for tow, expected 326825000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 14, 2, 211, 136, 22, 28, 246, 122, 19, 252, 255, 255, 255, 255, 255, 255, 255, 232, + 255, 255, 255, 0, 0, 0, 0, 15, 0, 16, 228, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20e, + "Incorrect message type, expected 0x20e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, -24, + "incorrect value for d, expected -24, is {}", + msg.d + ); + assert_eq!( + msg.e, -1, + "incorrect value for e, expected -1, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -4, + "incorrect value for n, expected -4, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326825500, + "incorrect value for tow, expected 326825500, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 14, 2, 211, 136, 22, 16, 248, 122, 19, 0, 0, 0, 0, 253, 255, 255, 255, 244, 255, + 255, 255, 0, 0, 0, 0, 15, 0, 11, 164, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20e, + "Incorrect message type, expected 0x20e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, -12, + "incorrect value for d, expected -12, is {}", + msg.d + ); + assert_eq!( + msg.e, -3, + "incorrect value for e, expected -3, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!(msg.n, 0, "incorrect value for n, expected 0, is {}", msg.n); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826000, + "incorrect value for tow, expected 326826000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 14, 2, 211, 136, 22, 4, 250, 122, 19, 2, 0, 0, 0, 3, 0, 0, 0, 232, 255, 255, 255, + 0, 0, 0, 0, 15, 0, 152, 208, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20e, + "Incorrect message type, expected 0x20e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, -24, + "incorrect value for d, expected -24, is {}", + msg.d + ); + assert_eq!(msg.e, 3, "incorrect value for e, expected 3, is {}", msg.e); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!(msg.n, 2, "incorrect value for n, expected 2, is {}", msg.n); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326826500, + "incorrect value for tow, expected 326826500, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 14, 2, 211, 136, 22, 248, 251, 122, 19, 1, 0, 0, 0, 0, 0, 0, 0, 235, 255, 255, 255, + 0, 0, 0, 0, 15, 0, 182, 120, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNed( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNed(msg) => { + assert_eq!( + msg.message_type(), + 0x20e, + "Incorrect message type, expected 0x20e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88d3, + "incorrect sender id, expected 0x88d3, is {}", + sender_id + ); + assert_eq!( + msg.d, -21, + "incorrect value for d, expected -21, is {}", + msg.d + ); + assert_eq!(msg.e, 0, "incorrect value for e, expected 0, is {}", msg.e); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!(msg.n, 1, "incorrect value for n, expected 1, is {}", msg.n); + assert_eq!( + msg.n_sats, 15, + "incorrect value for n_sats, expected 15, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 326827000, + "incorrect value for tow, expected 326827000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNED"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_cov_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_cov_gnss.rs index 3a85783580..41cff1925e 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_cov_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_cov_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ned_cov_gnss() { { @@ -107,6 +112,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ned_cov_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_cov_gnss() { @@ -114,14 +126,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_cov_gnss() { let json_input = r#"{"tow":501868200,"n":-5,"e":0,"d":-10,"cov_n_n":0.0015810149488970637,"cov_n_e":1.457612233934924e-05,"cov_n_d":0.00017716512957122177,"cov_e_e":0.0009897587588056922,"cov_e_d":0.00016467059322167188,"cov_d_d":0.007882959209382534,"n_sats":21,"flags":2,"preamble":85,"msg_type":562,"sender":4096,"payload":"qObpHfv///8AAAAA9v///w86zzr4i3Q3Z8U5Ocu6gTptqyw5hycBPBUC","crc":923,"length":42}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -206,3 +218,125 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_cov_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ned_cov_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ned_cov_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 50, 2, 0, 16, 42, 168, 230, 233, 29, 251, 255, 255, 255, 0, 0, 0, 0, 246, 255, 255, + 255, 15, 58, 207, 58, 248, 139, 116, 55, 103, 197, 57, 57, 203, 186, 129, 58, 109, 171, + 44, 57, 135, 39, 1, 60, 21, 2, 155, 3, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedCovGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedCovGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x232, + "Incorrect message type, expected 0x232, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert!( + msg.cov_d_d.almost_eq(7.88295920938253403e-03), + "incorrect value for cov_d_d, expected 7.88295920938253403e-03, is {:e}", + msg.cov_d_d + ); + assert!( + msg.cov_e_d.almost_eq(1.64670593221671879e-04), + "incorrect value for cov_e_d, expected 1.64670593221671879e-04, is {:e}", + msg.cov_e_d + ); + assert!( + msg.cov_e_e.almost_eq(9.89758758805692196e-04), + "incorrect value for cov_e_e, expected 9.89758758805692196e-04, is {:e}", + msg.cov_e_e + ); + assert!( + msg.cov_n_d.almost_eq(1.77165129571221769e-04), + "incorrect value for cov_n_d, expected 1.77165129571221769e-04, is {:e}", + msg.cov_n_d + ); + assert!( + msg.cov_n_e.almost_eq(1.45761223393492401e-05), + "incorrect value for cov_n_e, expected 1.45761223393492401e-05, is {:e}", + msg.cov_n_e + ); + assert!( + msg.cov_n_n.almost_eq(1.58101494889706373e-03), + "incorrect value for cov_n_n, expected 1.58101494889706373e-03, is {:e}", + msg.cov_n_n + ); + assert_eq!( + msg.d, -10, + "incorrect value for d, expected -10, is {}", + msg.d + ); + assert_eq!(msg.e, 0, "incorrect value for e, expected 0, is {}", msg.e); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.n, -5, + "incorrect value for n, expected -5, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 21, + "incorrect value for n_sats, expected 21, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501868200, + "incorrect value for tow, expected 501868200, is {}", + msg.tow + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDCovGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_dep_a.rs index d00a39a4a7..7b76947bb6 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ned_dep_a() { { @@ -788,6 +793,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ned_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { @@ -795,14 +807,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3996, "sender": 55286, "msg_type": 517, "tow": 2567700, "n": -1082, "crc": 23713, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "FC4nAMb7//+cDwAAAAAAAAAAAAAJAA==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -870,14 +882,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3791, "sender": 55286, "msg_type": 517, "tow": 2567800, "n": -1010, "crc": 41085, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "eC4nAA78///PDgAAAAAAAAAAAAAJAA==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -945,14 +957,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3724, "sender": 55286, "msg_type": 517, "tow": 2567900, "n": -976, "crc": 34739, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "3C4nADD8//+MDgAAAAAAAAAAAAAJAA==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1020,14 +1032,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3848, "sender": 55286, "msg_type": 517, "tow": 2568000, "n": -992, "crc": 45363, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "QC8nACD8//8IDwAAAAAAAAAAAAAJAA==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1095,14 +1107,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 3724, "sender": 55286, "msg_type": 517, "tow": 2568100, "n": -944, "crc": 23, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 9, "preamble": 85, "payload": "pC8nAFD8//+MDgAAAAAAAAAAAAAJAA==", "d": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1170,14 +1182,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 26, "sender": 1219, "msg_type": 517, "tow": 407084500, "n": -27, "crc": 6532, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 8, "preamble": 85, "payload": "1J1DGOX///8aAAAAGQAAAAAAAAAIAA==", "d": 25}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1249,14 +1261,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 15, "sender": 1219, "msg_type": 517, "tow": 407084600, "n": 4, "crc": 3626, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 8, "preamble": 85, "payload": "OJ5DGAQAAAAPAAAA6P///wAAAAAIAA==", "d": -24}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1324,14 +1336,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -24, "sender": 1219, "msg_type": 517, "tow": 407084700, "n": -5, "crc": 38106, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 8, "preamble": 85, "payload": "nJ5DGPv////o////9////wAAAAAIAA==", "d": -9}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1403,14 +1415,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": 2, "sender": 1219, "msg_type": 517, "tow": 407084800, "n": 10, "crc": 4244, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 8, "preamble": 85, "payload": "AJ9DGAoAAAACAAAA3v///wAAAAAIAA==", "d": -34}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1478,14 +1490,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -2, "sender": 1219, "msg_type": 517, "tow": 407084900, "n": -8, "crc": 60671, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 8, "preamble": 85, "payload": "ZJ9DGPj////+////BwAAAAAAAAAIAA==", "d": 7}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1553,14 +1565,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { let json_input = r#"{"v_accuracy": 0, "e": -3, "sender": 1219, "msg_type": 517, "tow": 407151150, "n": -1, "crc": 48550, "length": 22, "flags": 0, "h_accuracy": 0, "n_sats": 5, "preamble": 85, "payload": "LqJEGP/////9////lP///wAAAAAFAA==", "d": -108}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1629,3 +1641,996 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ned_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ned_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 246, 215, 22, 20, 46, 39, 0, 198, 251, 255, 255, 156, 15, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 9, 0, 161, 92, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, 3996, + "incorrect value for e, expected 3996, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -1082, + "incorrect value for n, expected -1082, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567700, + "incorrect value for tow, expected 2567700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 246, 215, 22, 120, 46, 39, 0, 14, 252, 255, 255, 207, 14, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 9, 0, 125, 160, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, 3791, + "incorrect value for e, expected 3791, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -1010, + "incorrect value for n, expected -1010, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567800, + "incorrect value for tow, expected 2567800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 246, 215, 22, 220, 46, 39, 0, 48, 252, 255, 255, 140, 14, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 9, 0, 179, 135, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, 3724, + "incorrect value for e, expected 3724, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -976, + "incorrect value for n, expected -976, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2567900, + "incorrect value for tow, expected 2567900, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 246, 215, 22, 64, 47, 39, 0, 32, 252, 255, 255, 8, 15, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 9, 0, 51, 177, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, 3848, + "incorrect value for e, expected 3848, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -992, + "incorrect value for n, expected -992, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568000, + "incorrect value for tow, expected 2568000, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 246, 215, 22, 164, 47, 39, 0, 80, 252, 255, 255, 140, 14, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 9, 0, 23, 0, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!(msg.d, 0, "incorrect value for d, expected 0, is {}", msg.d); + assert_eq!( + msg.e, 3724, + "incorrect value for e, expected 3724, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -944, + "incorrect value for n, expected -944, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 9, + "incorrect value for n_sats, expected 9, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 2568100, + "incorrect value for tow, expected 2568100, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 195, 4, 22, 212, 157, 67, 24, 229, 255, 255, 255, 26, 0, 0, 0, 25, 0, 0, 0, + 0, 0, 0, 0, 8, 0, 132, 25, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, 25, + "incorrect value for d, expected 25, is {}", + msg.d + ); + assert_eq!( + msg.e, 26, + "incorrect value for e, expected 26, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -27, + "incorrect value for n, expected -27, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084500, + "incorrect value for tow, expected 407084500, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 195, 4, 22, 56, 158, 67, 24, 4, 0, 0, 0, 15, 0, 0, 0, 232, 255, 255, 255, 0, + 0, 0, 0, 8, 0, 42, 14, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -24, + "incorrect value for d, expected -24, is {}", + msg.d + ); + assert_eq!( + msg.e, 15, + "incorrect value for e, expected 15, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!(msg.n, 4, "incorrect value for n, expected 4, is {}", msg.n); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084600, + "incorrect value for tow, expected 407084600, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 195, 4, 22, 156, 158, 67, 24, 251, 255, 255, 255, 232, 255, 255, 255, 247, + 255, 255, 255, 0, 0, 0, 0, 8, 0, 218, 148, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -9, + "incorrect value for d, expected -9, is {}", + msg.d + ); + assert_eq!( + msg.e, -24, + "incorrect value for e, expected -24, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -5, + "incorrect value for n, expected -5, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084700, + "incorrect value for tow, expected 407084700, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 195, 4, 22, 0, 159, 67, 24, 10, 0, 0, 0, 2, 0, 0, 0, 222, 255, 255, 255, 0, + 0, 0, 0, 8, 0, 148, 16, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -34, + "incorrect value for d, expected -34, is {}", + msg.d + ); + assert_eq!(msg.e, 2, "incorrect value for e, expected 2, is {}", msg.e); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, 10, + "incorrect value for n, expected 10, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084800, + "incorrect value for tow, expected 407084800, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 195, 4, 22, 100, 159, 67, 24, 248, 255, 255, 255, 254, 255, 255, 255, 7, 0, + 0, 0, 0, 0, 0, 0, 8, 0, 255, 236, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!(msg.d, 7, "incorrect value for d, expected 7, is {}", msg.d); + assert_eq!( + msg.e, -2, + "incorrect value for e, expected -2, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -8, + "incorrect value for n, expected -8, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 8, + "incorrect value for n_sats, expected 8, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407084900, + "incorrect value for tow, expected 407084900, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 5, 2, 195, 4, 22, 46, 162, 68, 24, 255, 255, 255, 255, 253, 255, 255, 255, 148, + 255, 255, 255, 0, 0, 0, 0, 5, 0, 166, 189, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x205, + "Incorrect message type, expected 0x205, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.d, -108, + "incorrect value for d, expected -108, is {}", + msg.d + ); + assert_eq!( + msg.e, -3, + "incorrect value for e, expected -3, is {}", + msg.e + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 0, + "incorrect value for h_accuracy, expected 0, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -1, + "incorrect value for n, expected -1, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 5, + "incorrect value for n_sats, expected 5, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 407151150, + "incorrect value for tow, expected 407151150, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 0, + "incorrect value for v_accuracy, expected 0, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_gnss.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_gnss.rs index b57a6bdc47..c77c7bb563 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_gnss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_ned_gnss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_ned_gnss() { { @@ -86,6 +91,13 @@ fn test_auto_check_sbp_navigation_msg_vel_ned_gnss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_gnss() { @@ -93,14 +105,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_gnss() { let json_input = r#"{"tow":501868200,"n":-5,"e":0,"d":-10,"h_accuracy":40,"v_accuracy":89,"n_sats":21,"flags":2,"preamble":85,"msg_type":558,"sender":4096,"payload":"qObpHfv///8AAAAA9v///ygAWQAVAg==","crc":43875,"length":22}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -165,3 +177,104 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_ned_gnss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_ned_gnss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_ned_gnss() { + { + let mut payload = Cursor::new(vec![ + 85, 46, 2, 0, 16, 22, 168, 230, 233, 29, 251, 255, 255, 255, 0, 0, 0, 0, 246, 255, 255, + 255, 40, 0, 89, 0, 21, 2, 99, 171, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedGnss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedGnss(msg) => { + assert_eq!( + msg.message_type(), + 0x22e, + "Incorrect message type, expected 0x22e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x1000, + "incorrect sender id, expected 0x1000, is {}", + sender_id + ); + assert_eq!( + msg.d, -10, + "incorrect value for d, expected -10, is {}", + msg.d + ); + assert_eq!(msg.e, 0, "incorrect value for e, expected 0, is {}", msg.e); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.h_accuracy, 40, + "incorrect value for h_accuracy, expected 40, is {}", + msg.h_accuracy + ); + assert_eq!( + msg.n, -5, + "incorrect value for n, expected -5, is {}", + msg.n + ); + assert_eq!( + msg.n_sats, 21, + "incorrect value for n_sats, expected 21, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 501868200, + "incorrect value for tow, expected 501868200, is {}", + msg.tow + ); + assert_eq!( + msg.v_accuracy, 89, + "incorrect value for v_accuracy, expected 89, is {}", + msg.v_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDGnss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_nedcov.rs b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_nedcov.rs index b3c8e29f46..5db3b4b55f 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_nedcov.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_navigation_msg_vel_nedcov.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_navigation_msg_vel_nedcov() { { @@ -99,6 +104,13 @@ fn test_auto_check_sbp_navigation_msg_vel_nedcov() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_navigation_msg_vel_nedcov() { @@ -106,14 +118,14 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_nedcov() { let json_input = r#"{"cov_e_d": 1, "cov_e_e": 1, "n_sats": 10, "sender": 66, "msg_type": 530, "cov_n_n": 1, "cov_n_e": 1, "tow": 100, "n": 1, "crc": 52568, "length": 42, "cov_n_d": 1, "flags": 0, "e": 1, "cov_d_d": 1, "preamble": 85, "payload": "ZAAAAAEAAAABAAAAAQAAAAAAgD8AAIA/AACAPwAAgD8AAIA/AACAPwoA", "d": 1}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -190,3 +202,117 @@ fn test_json2sbp_auto_check_sbp_navigation_msg_vel_nedcov() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_navigation_msg_vel_nedcov`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_navigation_msg_vel_nedcov() { + { + let mut payload = Cursor::new(vec![ + 85, 18, 2, 66, 0, 42, 100, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 128, 63, + 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63, 10, 0, 88, + 205, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgVelNedCov( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgVelNedCov(msg) => { + assert_eq!( + msg.message_type(), + 0x212, + "Incorrect message type, expected 0x212, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert!( + msg.cov_d_d.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_d_d, expected 1.00000000000000000e+00, is {:e}", + msg.cov_d_d + ); + assert!( + msg.cov_e_d.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_e_d, expected 1.00000000000000000e+00, is {:e}", + msg.cov_e_d + ); + assert!( + msg.cov_e_e.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_e_e, expected 1.00000000000000000e+00, is {:e}", + msg.cov_e_e + ); + assert!( + msg.cov_n_d.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_n_d, expected 1.00000000000000000e+00, is {:e}", + msg.cov_n_d + ); + assert!( + msg.cov_n_e.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_n_e, expected 1.00000000000000000e+00, is {:e}", + msg.cov_n_e + ); + assert!( + msg.cov_n_n.almost_eq(1.00000000000000000e+00), + "incorrect value for cov_n_n, expected 1.00000000000000000e+00, is {:e}", + msg.cov_n_n + ); + assert_eq!(msg.d, 1, "incorrect value for d, expected 1, is {}", msg.d); + assert_eq!(msg.e, 1, "incorrect value for e, expected 1, is {}", msg.e); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!(msg.n, 1, "incorrect value for n, expected 1, is {}", msg.n); + assert_eq!( + msg.n_sats, 10, + "incorrect value for n_sats, expected 10, is {}", + msg.n_sats + ); + assert_eq!( + msg.tow, 100, + "incorrect value for tow, expected 100, is {}", + msg.tow + ); + } + _ => panic!("Invalid message type! Expected a MsgVelNEDCov"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_base_pos_ecef.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_base_pos_ecef.rs index 46c08f368d..ce43c0b16f 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_base_pos_ecef.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_base_pos_ecef.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_base_pos_ecef() { { @@ -65,6 +70,13 @@ fn test_auto_check_sbp_observation_msg_base_pos_ecef() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_base_pos_ecef() { @@ -72,14 +84,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_base_pos_ecef() { let json_input = r#"{"x":-2726575.9189,"y":-4315267.2798,"z":3811455.9642,"preamble":85,"msg_type":72,"sender":0,"payload":"5IOe9VfNRMFCPujRIHZQwdXnavs/FE1B","crc":32194,"length":24}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -123,3 +135,83 @@ fn test_json2sbp_auto_check_sbp_observation_msg_base_pos_ecef() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_base_pos_ecef`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_base_pos_ecef() { + { + let mut payload = Cursor::new(vec![ + 85, 72, 0, 0, 0, 24, 228, 131, 158, 245, 87, 205, 68, 193, 66, 62, 232, 209, 32, 118, + 80, 193, 213, 231, 106, 251, 63, 20, 77, 65, 194, 125, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgBasePosEcef( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgBasePosEcef(msg) => { + assert_eq!( + msg.message_type(), + 0x48, + "Incorrect message type, expected 0x48, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0, + "incorrect sender id, expected 0, is {}", + sender_id + ); + assert!( + msg.x.almost_eq(-2.72657591889999993e+06), + "incorrect value for x, expected -2.72657591889999993e+06, is {:e}", + msg.x + ); + assert!( + msg.y.almost_eq(-4.31526727979999967e+06), + "incorrect value for y, expected -4.31526727979999967e+06, is {:e}", + msg.y + ); + assert!( + msg.z.almost_eq(3.81145596419999981e+06), + "incorrect value for z, expected 3.81145596419999981e+06, is {:e}", + msg.z + ); + } + _ => panic!("Invalid message type! Expected a MsgBasePosECEF"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_bds.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_bds.rs index c90a8e6d53..485667da49 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_bds.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_bds.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_ephemeris_bds() { { @@ -216,6 +221,13 @@ fn test_auto_check_sbp_observation_msg_ephemeris_bds() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_bds() { @@ -223,14 +235,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_bds() { let json_input = r#"{"common":{"sid":{"sat":8,"code":12},"toe":{"tow":439214,"wn":2154},"ura":2.0,"fit_interval":10800,"valid":1,"health_bits":0},"tgd1":1.0499999980595476e-08,"tgd2":-1.0999999799921056e-09,"c_rs":224.4375,"c_rc":234.640625,"c_uc":7.606577128171921e-06,"c_us":6.551854312419891e-07,"c_ic":-6.658956408500671e-08,"c_is":3.5529956221580505e-07,"dn":1.1296899132622133e-09,"m0":1.6943958190727237,"ecc":0.005184737499803305,"sqrta":6493.49845123291,"omega0":-2.581073762870982,"omegadot":-2.303310227830545e-09,"w":-2.698603205735458,"inc":1.0421769543504915,"inc_dot":7.507455572801683e-10,"af0":-0.0008911322802305222,"af1":1.2398970739013748e-12,"af2":-7.318364664277155e-19,"toc":{"tow":439214,"wn":2154},"iode":6,"iodc":5,"preamble":85,"msg_type":137,"sender":61568,"payload":"CAyuswYAaggAAABAMCoAAAEAfWM0Ms8ul7AAcGBDAKRqQwA8/zYA4C81AACPswDAvjSSZaLEbWgTPv1XVso+HPs/AAAAYJc8dT8AAICaf125QJfBQAAKpgTAoEuuYgjJI77NHQxHvZYFwLBI+b3BrPA/SPm8tKDLCT4AAAAAXDNNvwCArisAAFihrrMGAGoIBgUA","crc":63901,"length":147}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -419,3 +431,234 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_bds() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_ephemeris_bds`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_ephemeris_bds() { + { + let mut payload = Cursor::new(vec![ + 85, 137, 0, 128, 240, 147, 8, 12, 174, 179, 6, 0, 106, 8, 0, 0, 0, 64, 48, 42, 0, 0, 1, + 0, 125, 99, 52, 50, 207, 46, 151, 176, 0, 112, 96, 67, 0, 164, 106, 67, 0, 60, 255, 54, + 0, 224, 47, 53, 0, 0, 143, 179, 0, 192, 190, 52, 146, 101, 162, 196, 109, 104, 19, 62, + 253, 87, 86, 202, 62, 28, 251, 63, 0, 0, 0, 96, 151, 60, 117, 63, 0, 0, 128, 154, 127, + 93, 185, 64, 151, 193, 64, 0, 10, 166, 4, 192, 160, 75, 174, 98, 8, 201, 35, 190, 205, + 29, 12, 71, 189, 150, 5, 192, 176, 72, 249, 189, 193, 172, 240, 63, 72, 249, 188, 180, + 160, 203, 9, 62, 0, 0, 0, 0, 92, 51, 77, 191, 0, 128, 174, 43, 0, 0, 88, 161, 174, 179, + 6, 0, 106, 8, 6, 5, 0, 157, 249, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisBds( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisBds(msg) => { + assert_eq!( + msg.message_type(), + 0x89, + "Incorrect message type, expected 0x89, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xf080, + "incorrect sender id, expected 0xf080, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-8.91132280230522156e-04), + "incorrect value for af0, expected -8.91132280230522156e-04, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(1.23989707390137482e-12), + "incorrect value for af1, expected 1.23989707390137482e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(-7.31836466427715493e-19), + "incorrect value for af2, expected -7.31836466427715493e-19, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(-6.65895640850067139e-08), + "incorrect value for c_ic, expected -6.65895640850067139e-08, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(3.55299562215805054e-07), + "incorrect value for c_is, expected 3.55299562215805054e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(2.34640625000000000e+02), + "incorrect value for c_rc, expected 2.34640625000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(2.24437500000000000e+02), + "incorrect value for c_rs, expected 2.24437500000000000e+02, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(7.60657712817192078e-06), + "incorrect value for c_uc, expected 7.60657712817192078e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(6.55185431241989136e-07), + "incorrect value for c_us, expected 6.55185431241989136e-07, is {:e}", + msg.c_us + ); + assert_eq!( + msg.common.fit_interval, 10800, + "incorrect value for common.fit_interval, expected 10800, is {}", + msg.common.fit_interval + ); + assert_eq!( + msg.common.health_bits, 0, + "incorrect value for common.health_bits, expected 0, is {}", + msg.common.health_bits + ); + assert_eq!( + msg.common.sid.code, 12, + "incorrect value for common.sid.code, expected 12, is {}", + msg.common.sid.code + ); + assert_eq!( + msg.common.sid.sat, 8, + "incorrect value for common.sid.sat, expected 8, is {}", + msg.common.sid.sat + ); + assert_eq!( + msg.common.toe.tow, 439214, + "incorrect value for common.toe.tow, expected 439214, is {}", + msg.common.toe.tow + ); + assert_eq!( + msg.common.toe.wn, 2154, + "incorrect value for common.toe.wn, expected 2154, is {}", + msg.common.toe.wn + ); + assert!( + msg.common.ura.almost_eq(2.00000000000000000e+00), + "incorrect value for common.ura, expected 2.00000000000000000e+00, is {:e}", + msg.common.ura + ); + assert_eq!( + msg.common.valid, 1, + "incorrect value for common.valid, expected 1, is {}", + msg.common.valid + ); + assert!( + msg.dn.almost_eq(1.12968991326221325e-09), + "incorrect value for dn, expected 1.12968991326221325e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(5.18473749980330467e-03), + "incorrect value for ecc, expected 5.18473749980330467e-03, is {:e}", + msg.ecc + ); + assert!( + msg.inc.almost_eq(1.04217695435049151e+00), + "incorrect value for inc, expected 1.04217695435049151e+00, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(7.50745557280168303e-10), + "incorrect value for inc_dot, expected 7.50745557280168303e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iodc, 5, + "incorrect value for iodc, expected 5, is {}", + msg.iodc + ); + assert_eq!( + msg.iode, 6, + "incorrect value for iode, expected 6, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(1.69439581907272374e+00), + "incorrect value for m0, expected 1.69439581907272374e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(-2.58107376287098189e+00), + "incorrect value for omega0, expected -2.58107376287098189e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-2.30331022783054511e-09), + "incorrect value for omegadot, expected -2.30331022783054511e-09, is {:e}", + msg.omegadot + ); + assert!( + msg.sqrta.almost_eq(6.49349845123291016e+03), + "incorrect value for sqrta, expected 6.49349845123291016e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd1.almost_eq(1.04999999805954758e-08), + "incorrect value for tgd1, expected 1.04999999805954758e-08, is {:e}", + msg.tgd1 + ); + assert!( + msg.tgd2.almost_eq(-1.09999997999210564e-09), + "incorrect value for tgd2, expected -1.09999997999210564e-09, is {:e}", + msg.tgd2 + ); + assert_eq!( + msg.toc.tow, 439214, + "incorrect value for toc.tow, expected 439214, is {}", + msg.toc.tow + ); + assert_eq!( + msg.toc.wn, 2154, + "incorrect value for toc.wn, expected 2154, is {}", + msg.toc.wn + ); + assert!( + msg.w.almost_eq(-2.69860320573545787e+00), + "incorrect value for w, expected -2.69860320573545787e+00, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisBds"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_dep_b.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_dep_b.rs index 75afc905c1..485c9447fd 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_dep_b.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_dep_b.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_ephemeris_dep_b() { { @@ -1067,6 +1072,13 @@ fn test_auto_check_sbp_observation_msg_ephemeris_dep_b() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { @@ -1074,14 +1086,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { let json_input = r#"{"msg_type": 70, "c_rs": -74.90625, "toe_wn": 1838, "prn": 3, "inc_dot": -3.296565886629854e-10, "tgd": -6.51925802230835e-09, "c_rc": 234.375, "ecc": 0.011132609914056957, "sqrta": 5153.714303970337, "payload": "AAAAAAAAPL4AAAAAALpSwAAAAAAATG1AAAAAAACE0L4AAAAAAP7cPgAAAAAAAHE+AAAAAAAAhb4cJBlR3/40Ptx02CchvQNAAAAAnLHMhj8AAKDctiG0QJjhwCz+TO6/KZYYApScQb78WncwD9fwP3x/c17QEO4/pXM0SmGn9r0AAAAAwLTlvgAAAAAAAHC9AAAAAAAAAAAAAAAAgAwZQS4HAAAAAIAMGUEuBwEBAwA=", "inc": 0.9395524830579087, "toe_tow": 410400.0, "c_uc": -3.937631845474243e-06, "c_us": 6.9122761487960815e-06, "valid": 1, "m0": 2.467348395627239, "toc_tow": 410400.0, "dn": 4.8884179079418005e-09, "omegadot": -8.201055892610478e-09, "toc_wn": 1838, "c_ic": 6.332993507385254e-08, "c_is": -1.564621925354004e-07, "preamble": 85, "sender": 1219, "healthy": 1, "iode": 0, "af1": -9.094947017729282e-13, "crc": 40161, "length": 176, "w": 1.0525047200405302, "af0": -1.035025343298912e-05, "omega0": -0.9468985437479658, "af2": 0.0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1248,14 +1260,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { let json_input = r#"{"msg_type": 70, "c_rs": 36.5625, "toe_wn": 1838, "prn": 13, "inc_dot": 2.6251093463412166e-10, "tgd": -9.313225746154785e-09, "c_rc": 315.78125, "ecc": 0.007922741584479809, "sqrta": 5153.57085609436, "payload": "AAAAAAAARL4AAAAAAEhCQAAAAACAvHNAAAAAAABQwT4AAAAAAKTMPgAAAAAAAII+AAAAAAAAgD5ItX8G0OE0Pp6ugVsbafm/AAAAYMw5gD8AAKAjkiG0QPepASSFzvM/TwttXJzQQb5nTgP935P/v6TWWvra8O4/Xu+7JSQK8j0AAAAAsFsTPwAAAAAAAIm9AAAAAAAAAAAAAAAAgAwZQS4HAAAAAIAMGUEuBwEBDQA=", "inc": 0.9669012918227122, "toe_tow": 410400.0, "c_uc": 2.0638108253479004e-06, "c_us": 3.4142285585403442e-06, "valid": 1, "m0": -1.588160855720083, "toc_tow": 410400.0, "dn": 4.86198823561129e-09, "omegadot": -8.295702692172441e-09, "toc_wn": 1838, "c_ic": 1.341104507446289e-07, "c_is": 1.1920928955078125e-07, "preamble": 85, "sender": 1219, "healthy": 1, "iode": 0, "af1": -2.8421709430404007e-12, "crc": 5556, "length": 176, "w": -1.9736022837941165, "af0": 7.384549826383591e-05, "omega0": 1.237919941568746, "af2": 0.0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1422,14 +1434,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { let json_input = r#"{"msg_type": 70, "c_rs": -68.625, "toe_wn": 1838, "prn": 0, "inc_dot": -3.3644258561271105e-10, "tgd": 5.587935447692871e-09, "c_rc": 236.03125, "ecc": 0.004040417145006359, "sqrta": 5153.669353485107, "payload": "AAAAAAAAOD4AAAAAAChRwAAAAAAAgW1AAAAAAAAczb4AAAAAAEzfPgAAAAAAAGm+AAAAAAAAXL6God//8yszPpKwcY7qpAVAAAAAOK+McD8AAMBaqyG0QCQm7f/IoO2/zFw/mjFbQb59XvuEND3YPwKLHBvnx+4/fLcEtMIe970AAAAAAGjevgAAAAAAAGA9AAAAAAAAAAAAAAAAgAwZQS4HAAAAAIAMGUEuBwEBAAA=", "inc": 0.9619021920701416, "toe_tow": 410400.0, "c_uc": -3.470107913017273e-06, "c_us": 7.461756467819214e-06, "valid": 1, "m0": 2.7055255058713295, "toc_tow": 410400.0, "dn": 4.4637573619826565e-09, "omegadot": -8.082122367123182e-09, "toc_wn": 1838, "c_ic": -4.6566128730773926e-08, "c_is": -2.60770320892334e-08, "preamble": 85, "sender": 1219, "healthy": 1, "iode": 0, "af1": 4.547473508864641e-13, "crc": 39134, "length": 176, "w": 0.37873566614641857, "af0": -7.249414920806885e-06, "omega0": -0.9258770941316397, "af2": 0.0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1596,14 +1608,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { let json_input = r#"{"msg_type": 70, "c_rs": 36.5625, "toe_wn": 1838, "prn": 13, "inc_dot": 2.6251093463412166e-10, "tgd": -9.313225746154785e-09, "c_rc": 315.78125, "ecc": 0.007922741584479809, "sqrta": 5153.57085609436, "payload": "AAAAAAAARL4AAAAAAEhCQAAAAACAvHNAAAAAAABQwT4AAAAAAKTMPgAAAAAAAII+AAAAAAAAgD5ItX8G0OE0Pp6ugVsbafm/AAAAYMw5gD8AAKAjkiG0QPepASSFzvM/TwttXJzQQb5nTgP935P/v6TWWvra8O4/Xu+7JSQK8j0AAAAAsFsTPwAAAAAAAIm9AAAAAAAAAAAAAAAAgAwZQS4HAAAAAIAMGUEuBwEBDQA=", "inc": 0.9669012918227122, "toe_tow": 410400.0, "c_uc": 2.0638108253479004e-06, "c_us": 3.4142285585403442e-06, "valid": 1, "m0": -1.588160855720083, "toc_tow": 410400.0, "dn": 4.86198823561129e-09, "omegadot": -8.295702692172441e-09, "toc_wn": 1838, "c_ic": 1.341104507446289e-07, "c_is": 1.1920928955078125e-07, "preamble": 85, "sender": 1219, "healthy": 1, "iode": 0, "af1": -2.8421709430404007e-12, "crc": 5556, "length": 176, "w": -1.9736022837941165, "af0": 7.384549826383591e-05, "omega0": 1.237919941568746, "af2": 0.0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1770,14 +1782,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { let json_input = r#"{"msg_type": 70, "c_rs": 43.21875, "toe_wn": 1838, "prn": 22, "inc_dot": 3.000124967247105e-10, "tgd": -2.0023435354232788e-08, "c_rc": 305.21875, "ecc": 0.009923744946718216, "sqrta": 5153.636667251587, "payload": "AAAAAACAVb4AAAAAAJxFQAAAAACAE3NAAAAAAACgwT4AAAAAAJjPPgAAAAAAAGG+AAAAAADAi74aGg2VEJg2PmgHLtZLVAXAAAAAgOZShD8AAKD8oiG0QEkGgjbZq/I/UeCje+4qQr7OK41D850FwHGzmbsrXO4//uwfK+Cd9D0AAAAA6AQavwAAAAAAAIa9AAAAAAAAAAAAAAAAgAwZQS4HAAAAAIAMGUEuBwEBFgA=", "inc": 0.9487513221807672, "toe_tow": 410400.0, "c_uc": 2.1010637283325195e-06, "c_us": 3.766268491744995e-06, "valid": 1, "m0": -2.666160271911327, "toc_tow": 410400.0, "dn": 5.26057626697412e-09, "omegadot": -8.45999524990264e-09, "toc_wn": 1838, "c_ic": -3.166496753692627e-08, "c_is": -2.0675361156463623e-07, "preamble": 85, "sender": 1219, "healthy": 1, "iode": 0, "af1": -2.5011104298755527e-12, "crc": 15715, "length": 176, "w": -2.7021241452652935, "af0": -9.925523772835732e-05, "omega0": 1.1669551972594425, "af2": 0.0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1944,14 +1956,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { let json_input = r#"{"msg_type": 70, "c_rs": -77.90625, "toe_wn": 1838, "prn": 30, "inc_dot": -5.882387882209502e-10, "tgd": -1.3504177331924438e-08, "c_rc": 247.28125, "ecc": 0.008178644930012524, "sqrta": 5153.7539920806885, "payload": "AAAAAAAATb4AAAAAAHpTwAAAAAAA6W5AAAAAAAA8z74AAAAAABzePgAAAAAAgHg+AAAAAAAAbD4K5reM1uYyPjZWxKT8Cv8/AAAAJPe/gD8AAKAFwSG0QLqKUYFY7wFAXtJ4qmoZQb5n1SCb48Lgv5wvaF1lN+8/xFNk/jM2BL4AAAAAMvI0PwAAAAAAAHK9AAAAAAAAAAAAAAAAgAwZQS4HAAAAAIAMGUEuBwEBHgA=", "inc": 0.9755122017245301, "toe_tow": 410400.0, "c_uc": -3.723427653312683e-06, "c_us": 7.178634405136108e-06, "valid": 1, "m0": 1.9401823459824192, "toc_tow": 410400.0, "dn": 4.400897600764146e-09, "omegadot": -7.962474526167494e-09, "toc_wn": 1838, "c_ic": 9.12696123123169e-08, "c_is": 5.21540641784668e-08, "preamble": 85, "sender": 1219, "healthy": 1, "iode": 0, "af1": -1.0231815394945443e-12, "crc": 8618, "length": 176, "w": -0.5237901716088061, "af0": 0.0003196117468178272, "omega0": 2.241868028927766, "af2": 0.0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2115,3 +2127,1180 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_dep_b() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_ephemeris_dep_b`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_ephemeris_dep_b() { + { + let mut payload = Cursor::new(vec![ + 85, 70, 0, 195, 4, 176, 0, 0, 0, 0, 0, 0, 60, 190, 0, 0, 0, 0, 0, 186, 82, 192, 0, 0, + 0, 0, 0, 76, 109, 64, 0, 0, 0, 0, 0, 132, 208, 190, 0, 0, 0, 0, 0, 254, 220, 62, 0, 0, + 0, 0, 0, 0, 113, 62, 0, 0, 0, 0, 0, 0, 133, 190, 28, 36, 25, 81, 223, 254, 52, 62, 220, + 116, 216, 39, 33, 189, 3, 64, 0, 0, 0, 156, 177, 204, 134, 63, 0, 0, 160, 220, 182, 33, + 180, 64, 152, 225, 192, 44, 254, 76, 238, 191, 41, 150, 24, 2, 148, 156, 65, 190, 252, + 90, 119, 48, 15, 215, 240, 63, 124, 127, 115, 94, 208, 16, 238, 63, 165, 115, 52, 74, + 97, 167, 246, 189, 0, 0, 0, 0, 192, 180, 229, 190, 0, 0, 0, 0, 0, 0, 112, 189, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, + 1, 1, 3, 0, 225, 156, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x46, + "Incorrect message type, expected 0x46, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-1.03502534329891205e-05), + "incorrect value for af0, expected -1.03502534329891205e-05, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-9.09494701772928238e-13), + "incorrect value for af1, expected -9.09494701772928238e-13, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(6.33299350738525391e-08), + "incorrect value for c_ic, expected 6.33299350738525391e-08, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(-1.56462192535400391e-07), + "incorrect value for c_is, expected -1.56462192535400391e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(2.34375000000000000e+02), + "incorrect value for c_rc, expected 2.34375000000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(-7.49062500000000000e+01), + "incorrect value for c_rs, expected -7.49062500000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(-3.93763184547424316e-06), + "incorrect value for c_uc, expected -3.93763184547424316e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(6.91227614879608154e-06), + "incorrect value for c_us, expected 6.91227614879608154e-06, is {:e}", + msg.c_us + ); + assert!( + msg.dn.almost_eq(4.88841790794180047e-09), + "incorrect value for dn, expected 4.88841790794180047e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(1.11326099140569568e-02), + "incorrect value for ecc, expected 1.11326099140569568e-02, is {:e}", + msg.ecc + ); + assert_eq!( + msg.healthy, 1, + "incorrect value for healthy, expected 1, is {}", + msg.healthy + ); + assert!( + msg.inc.almost_eq(9.39552483057908727e-01), + "incorrect value for inc, expected 9.39552483057908727e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(-3.29656588662985413e-10), + "incorrect value for inc_dot, expected -3.29656588662985413e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iode, 0, + "incorrect value for iode, expected 0, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(2.46734839562723884e+00), + "incorrect value for m0, expected 2.46734839562723884e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(-9.46898543747965782e-01), + "incorrect value for omega0, expected -9.46898543747965782e-01, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-8.20105589261047769e-09), + "incorrect value for omegadot, expected -8.20105589261047769e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.prn, 3, + "incorrect value for prn, expected 3, is {}", + msg.prn + ); + assert!( + msg.sqrta.almost_eq(5.15371430397033691e+03), + "incorrect value for sqrta, expected 5.15371430397033691e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-6.51925802230834961e-09), + "incorrect value for tgd, expected -6.51925802230834961e-09, is {:e}", + msg.tgd + ); + assert!( + msg.toc_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toc_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toc_tow + ); + assert_eq!( + msg.toc_wn, 1838, + "incorrect value for toc_wn, expected 1838, is {}", + msg.toc_wn + ); + assert!( + msg.toe_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toe_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toe_tow + ); + assert_eq!( + msg.toe_wn, 1838, + "incorrect value for toe_wn, expected 1838, is {}", + msg.toe_wn + ); + assert_eq!( + msg.valid, 1, + "incorrect value for valid, expected 1, is {}", + msg.valid + ); + assert!( + msg.w.almost_eq(1.05250472004053020e+00), + "incorrect value for w, expected 1.05250472004053020e+00, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 70, 0, 195, 4, 176, 0, 0, 0, 0, 0, 0, 68, 190, 0, 0, 0, 0, 0, 72, 66, 64, 0, 0, 0, + 0, 128, 188, 115, 64, 0, 0, 0, 0, 0, 80, 193, 62, 0, 0, 0, 0, 0, 164, 204, 62, 0, 0, 0, + 0, 0, 0, 130, 62, 0, 0, 0, 0, 0, 0, 128, 62, 72, 181, 127, 6, 208, 225, 52, 62, 158, + 174, 129, 91, 27, 105, 249, 191, 0, 0, 0, 96, 204, 57, 128, 63, 0, 0, 160, 35, 146, 33, + 180, 64, 247, 169, 1, 36, 133, 206, 243, 63, 79, 11, 109, 92, 156, 208, 65, 190, 103, + 78, 3, 253, 223, 147, 255, 191, 164, 214, 90, 250, 218, 240, 238, 63, 94, 239, 187, 37, + 36, 10, 242, 61, 0, 0, 0, 0, 176, 91, 19, 63, 0, 0, 0, 0, 0, 0, 137, 189, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 1, + 1, 13, 0, 180, 21, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x46, + "Incorrect message type, expected 0x46, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(7.38454982638359070e-05), + "incorrect value for af0, expected 7.38454982638359070e-05, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-2.84217094304040074e-12), + "incorrect value for af1, expected -2.84217094304040074e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(1.34110450744628906e-07), + "incorrect value for c_ic, expected 1.34110450744628906e-07, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(1.19209289550781250e-07), + "incorrect value for c_is, expected 1.19209289550781250e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(3.15781250000000000e+02), + "incorrect value for c_rc, expected 3.15781250000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(3.65625000000000000e+01), + "incorrect value for c_rs, expected 3.65625000000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(2.06381082534790039e-06), + "incorrect value for c_uc, expected 2.06381082534790039e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(3.41422855854034424e-06), + "incorrect value for c_us, expected 3.41422855854034424e-06, is {:e}", + msg.c_us + ); + assert!( + msg.dn.almost_eq(4.86198823561129038e-09), + "incorrect value for dn, expected 4.86198823561129038e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(7.92274158447980881e-03), + "incorrect value for ecc, expected 7.92274158447980881e-03, is {:e}", + msg.ecc + ); + assert_eq!( + msg.healthy, 1, + "incorrect value for healthy, expected 1, is {}", + msg.healthy + ); + assert!( + msg.inc.almost_eq(9.66901291822712228e-01), + "incorrect value for inc, expected 9.66901291822712228e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(2.62510934634121662e-10), + "incorrect value for inc_dot, expected 2.62510934634121662e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iode, 0, + "incorrect value for iode, expected 0, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(-1.58816085572008303e+00), + "incorrect value for m0, expected -1.58816085572008303e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(1.23791994156874607e+00), + "incorrect value for omega0, expected 1.23791994156874607e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-8.29570269217244098e-09), + "incorrect value for omegadot, expected -8.29570269217244098e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.prn, 13, + "incorrect value for prn, expected 13, is {}", + msg.prn + ); + assert!( + msg.sqrta.almost_eq(5.15357085609436035e+03), + "incorrect value for sqrta, expected 5.15357085609436035e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-9.31322574615478516e-09), + "incorrect value for tgd, expected -9.31322574615478516e-09, is {:e}", + msg.tgd + ); + assert!( + msg.toc_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toc_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toc_tow + ); + assert_eq!( + msg.toc_wn, 1838, + "incorrect value for toc_wn, expected 1838, is {}", + msg.toc_wn + ); + assert!( + msg.toe_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toe_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toe_tow + ); + assert_eq!( + msg.toe_wn, 1838, + "incorrect value for toe_wn, expected 1838, is {}", + msg.toe_wn + ); + assert_eq!( + msg.valid, 1, + "incorrect value for valid, expected 1, is {}", + msg.valid + ); + assert!( + msg.w.almost_eq(-1.97360228379411651e+00), + "incorrect value for w, expected -1.97360228379411651e+00, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 70, 0, 195, 4, 176, 0, 0, 0, 0, 0, 0, 56, 62, 0, 0, 0, 0, 0, 40, 81, 192, 0, 0, 0, + 0, 0, 129, 109, 64, 0, 0, 0, 0, 0, 28, 205, 190, 0, 0, 0, 0, 0, 76, 223, 62, 0, 0, 0, + 0, 0, 0, 105, 190, 0, 0, 0, 0, 0, 0, 92, 190, 134, 161, 223, 255, 243, 43, 51, 62, 146, + 176, 113, 142, 234, 164, 5, 64, 0, 0, 0, 56, 175, 140, 112, 63, 0, 0, 192, 90, 171, 33, + 180, 64, 36, 38, 237, 255, 200, 160, 237, 191, 204, 92, 63, 154, 49, 91, 65, 190, 125, + 94, 251, 132, 52, 61, 216, 63, 2, 139, 28, 27, 231, 199, 238, 63, 124, 183, 4, 180, + 194, 30, 247, 189, 0, 0, 0, 0, 0, 104, 222, 190, 0, 0, 0, 0, 0, 0, 96, 61, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 1, + 1, 0, 0, 222, 152, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x46, + "Incorrect message type, expected 0x46, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-7.24941492080688477e-06), + "incorrect value for af0, expected -7.24941492080688477e-06, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(4.54747350886464119e-13), + "incorrect value for af1, expected 4.54747350886464119e-13, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(-4.65661287307739258e-08), + "incorrect value for c_ic, expected -4.65661287307739258e-08, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(-2.60770320892333984e-08), + "incorrect value for c_is, expected -2.60770320892333984e-08, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(2.36031250000000000e+02), + "incorrect value for c_rc, expected 2.36031250000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(-6.86250000000000000e+01), + "incorrect value for c_rs, expected -6.86250000000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(-3.47010791301727295e-06), + "incorrect value for c_uc, expected -3.47010791301727295e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(7.46175646781921387e-06), + "incorrect value for c_us, expected 7.46175646781921387e-06, is {:e}", + msg.c_us + ); + assert!( + msg.dn.almost_eq(4.46375736198265652e-09), + "incorrect value for dn, expected 4.46375736198265652e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(4.04041714500635862e-03), + "incorrect value for ecc, expected 4.04041714500635862e-03, is {:e}", + msg.ecc + ); + assert_eq!( + msg.healthy, 1, + "incorrect value for healthy, expected 1, is {}", + msg.healthy + ); + assert!( + msg.inc.almost_eq(9.61902192070141604e-01), + "incorrect value for inc, expected 9.61902192070141604e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(-3.36442585612711049e-10), + "incorrect value for inc_dot, expected -3.36442585612711049e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iode, 0, + "incorrect value for iode, expected 0, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(2.70552550587132945e+00), + "incorrect value for m0, expected 2.70552550587132945e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(-9.25877094131639655e-01), + "incorrect value for omega0, expected -9.25877094131639655e-01, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-8.08212236712318227e-09), + "incorrect value for omegadot, expected -8.08212236712318227e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.prn, 0, + "incorrect value for prn, expected 0, is {}", + msg.prn + ); + assert!( + msg.sqrta.almost_eq(5.15366935348510742e+03), + "incorrect value for sqrta, expected 5.15366935348510742e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(5.58793544769287109e-09), + "incorrect value for tgd, expected 5.58793544769287109e-09, is {:e}", + msg.tgd + ); + assert!( + msg.toc_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toc_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toc_tow + ); + assert_eq!( + msg.toc_wn, 1838, + "incorrect value for toc_wn, expected 1838, is {}", + msg.toc_wn + ); + assert!( + msg.toe_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toe_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toe_tow + ); + assert_eq!( + msg.toe_wn, 1838, + "incorrect value for toe_wn, expected 1838, is {}", + msg.toe_wn + ); + assert_eq!( + msg.valid, 1, + "incorrect value for valid, expected 1, is {}", + msg.valid + ); + assert!( + msg.w.almost_eq(3.78735666146418570e-01), + "incorrect value for w, expected 3.78735666146418570e-01, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 70, 0, 195, 4, 176, 0, 0, 0, 0, 0, 0, 68, 190, 0, 0, 0, 0, 0, 72, 66, 64, 0, 0, 0, + 0, 128, 188, 115, 64, 0, 0, 0, 0, 0, 80, 193, 62, 0, 0, 0, 0, 0, 164, 204, 62, 0, 0, 0, + 0, 0, 0, 130, 62, 0, 0, 0, 0, 0, 0, 128, 62, 72, 181, 127, 6, 208, 225, 52, 62, 158, + 174, 129, 91, 27, 105, 249, 191, 0, 0, 0, 96, 204, 57, 128, 63, 0, 0, 160, 35, 146, 33, + 180, 64, 247, 169, 1, 36, 133, 206, 243, 63, 79, 11, 109, 92, 156, 208, 65, 190, 103, + 78, 3, 253, 223, 147, 255, 191, 164, 214, 90, 250, 218, 240, 238, 63, 94, 239, 187, 37, + 36, 10, 242, 61, 0, 0, 0, 0, 176, 91, 19, 63, 0, 0, 0, 0, 0, 0, 137, 189, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 1, + 1, 13, 0, 180, 21, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x46, + "Incorrect message type, expected 0x46, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(7.38454982638359070e-05), + "incorrect value for af0, expected 7.38454982638359070e-05, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-2.84217094304040074e-12), + "incorrect value for af1, expected -2.84217094304040074e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(1.34110450744628906e-07), + "incorrect value for c_ic, expected 1.34110450744628906e-07, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(1.19209289550781250e-07), + "incorrect value for c_is, expected 1.19209289550781250e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(3.15781250000000000e+02), + "incorrect value for c_rc, expected 3.15781250000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(3.65625000000000000e+01), + "incorrect value for c_rs, expected 3.65625000000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(2.06381082534790039e-06), + "incorrect value for c_uc, expected 2.06381082534790039e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(3.41422855854034424e-06), + "incorrect value for c_us, expected 3.41422855854034424e-06, is {:e}", + msg.c_us + ); + assert!( + msg.dn.almost_eq(4.86198823561129038e-09), + "incorrect value for dn, expected 4.86198823561129038e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(7.92274158447980881e-03), + "incorrect value for ecc, expected 7.92274158447980881e-03, is {:e}", + msg.ecc + ); + assert_eq!( + msg.healthy, 1, + "incorrect value for healthy, expected 1, is {}", + msg.healthy + ); + assert!( + msg.inc.almost_eq(9.66901291822712228e-01), + "incorrect value for inc, expected 9.66901291822712228e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(2.62510934634121662e-10), + "incorrect value for inc_dot, expected 2.62510934634121662e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iode, 0, + "incorrect value for iode, expected 0, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(-1.58816085572008303e+00), + "incorrect value for m0, expected -1.58816085572008303e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(1.23791994156874607e+00), + "incorrect value for omega0, expected 1.23791994156874607e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-8.29570269217244098e-09), + "incorrect value for omegadot, expected -8.29570269217244098e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.prn, 13, + "incorrect value for prn, expected 13, is {}", + msg.prn + ); + assert!( + msg.sqrta.almost_eq(5.15357085609436035e+03), + "incorrect value for sqrta, expected 5.15357085609436035e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-9.31322574615478516e-09), + "incorrect value for tgd, expected -9.31322574615478516e-09, is {:e}", + msg.tgd + ); + assert!( + msg.toc_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toc_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toc_tow + ); + assert_eq!( + msg.toc_wn, 1838, + "incorrect value for toc_wn, expected 1838, is {}", + msg.toc_wn + ); + assert!( + msg.toe_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toe_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toe_tow + ); + assert_eq!( + msg.toe_wn, 1838, + "incorrect value for toe_wn, expected 1838, is {}", + msg.toe_wn + ); + assert_eq!( + msg.valid, 1, + "incorrect value for valid, expected 1, is {}", + msg.valid + ); + assert!( + msg.w.almost_eq(-1.97360228379411651e+00), + "incorrect value for w, expected -1.97360228379411651e+00, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 70, 0, 195, 4, 176, 0, 0, 0, 0, 0, 128, 85, 190, 0, 0, 0, 0, 0, 156, 69, 64, 0, 0, + 0, 0, 128, 19, 115, 64, 0, 0, 0, 0, 0, 160, 193, 62, 0, 0, 0, 0, 0, 152, 207, 62, 0, 0, + 0, 0, 0, 0, 97, 190, 0, 0, 0, 0, 0, 192, 139, 190, 26, 26, 13, 149, 16, 152, 54, 62, + 104, 7, 46, 214, 75, 84, 5, 192, 0, 0, 0, 128, 230, 82, 132, 63, 0, 0, 160, 252, 162, + 33, 180, 64, 73, 6, 130, 54, 217, 171, 242, 63, 81, 224, 163, 123, 238, 42, 66, 190, + 206, 43, 141, 67, 243, 157, 5, 192, 113, 179, 153, 187, 43, 92, 238, 63, 254, 236, 31, + 43, 224, 157, 244, 61, 0, 0, 0, 0, 232, 4, 26, 191, 0, 0, 0, 0, 0, 0, 134, 189, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 0, 0, 0, 0, 128, 12, 25, 65, 46, + 7, 1, 1, 22, 0, 99, 61, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x46, + "Incorrect message type, expected 0x46, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-9.92552377283573151e-05), + "incorrect value for af0, expected -9.92552377283573151e-05, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-2.50111042987555265e-12), + "incorrect value for af1, expected -2.50111042987555265e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(-3.16649675369262695e-08), + "incorrect value for c_ic, expected -3.16649675369262695e-08, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(-2.06753611564636230e-07), + "incorrect value for c_is, expected -2.06753611564636230e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(3.05218750000000000e+02), + "incorrect value for c_rc, expected 3.05218750000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(4.32187500000000000e+01), + "incorrect value for c_rs, expected 4.32187500000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(2.10106372833251953e-06), + "incorrect value for c_uc, expected 2.10106372833251953e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(3.76626849174499512e-06), + "incorrect value for c_us, expected 3.76626849174499512e-06, is {:e}", + msg.c_us + ); + assert!( + msg.dn.almost_eq(5.26057626697411973e-09), + "incorrect value for dn, expected 5.26057626697411973e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(9.92374494671821594e-03), + "incorrect value for ecc, expected 9.92374494671821594e-03, is {:e}", + msg.ecc + ); + assert_eq!( + msg.healthy, 1, + "incorrect value for healthy, expected 1, is {}", + msg.healthy + ); + assert!( + msg.inc.almost_eq(9.48751322180767187e-01), + "incorrect value for inc, expected 9.48751322180767187e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(3.00012496724710486e-10), + "incorrect value for inc_dot, expected 3.00012496724710486e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iode, 0, + "incorrect value for iode, expected 0, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(-2.66616027191132687e+00), + "incorrect value for m0, expected -2.66616027191132687e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(1.16695519725944252e+00), + "incorrect value for omega0, expected 1.16695519725944252e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-8.45999524990263948e-09), + "incorrect value for omegadot, expected -8.45999524990263948e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.prn, 22, + "incorrect value for prn, expected 22, is {}", + msg.prn + ); + assert!( + msg.sqrta.almost_eq(5.15363666725158691e+03), + "incorrect value for sqrta, expected 5.15363666725158691e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-2.00234353542327881e-08), + "incorrect value for tgd, expected -2.00234353542327881e-08, is {:e}", + msg.tgd + ); + assert!( + msg.toc_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toc_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toc_tow + ); + assert_eq!( + msg.toc_wn, 1838, + "incorrect value for toc_wn, expected 1838, is {}", + msg.toc_wn + ); + assert!( + msg.toe_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toe_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toe_tow + ); + assert_eq!( + msg.toe_wn, 1838, + "incorrect value for toe_wn, expected 1838, is {}", + msg.toe_wn + ); + assert_eq!( + msg.valid, 1, + "incorrect value for valid, expected 1, is {}", + msg.valid + ); + assert!( + msg.w.almost_eq(-2.70212414526529354e+00), + "incorrect value for w, expected -2.70212414526529354e+00, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 70, 0, 195, 4, 176, 0, 0, 0, 0, 0, 0, 77, 190, 0, 0, 0, 0, 0, 122, 83, 192, 0, 0, + 0, 0, 0, 233, 110, 64, 0, 0, 0, 0, 0, 60, 207, 190, 0, 0, 0, 0, 0, 28, 222, 62, 0, 0, + 0, 0, 0, 128, 120, 62, 0, 0, 0, 0, 0, 0, 108, 62, 10, 230, 183, 140, 214, 230, 50, 62, + 54, 86, 196, 164, 252, 10, 255, 63, 0, 0, 0, 36, 247, 191, 128, 63, 0, 0, 160, 5, 193, + 33, 180, 64, 186, 138, 81, 129, 88, 239, 1, 64, 94, 210, 120, 170, 106, 25, 65, 190, + 103, 213, 32, 155, 227, 194, 224, 191, 156, 47, 104, 93, 101, 55, 239, 63, 196, 83, + 100, 254, 51, 54, 4, 190, 0, 0, 0, 0, 50, 242, 52, 63, 0, 0, 0, 0, 0, 0, 114, 189, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 12, 25, 65, 46, 7, 0, 0, 0, 0, 128, 12, 25, 65, + 46, 7, 1, 1, 30, 0, 170, 33, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x46, + "Incorrect message type, expected 0x46, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(3.19611746817827225e-04), + "incorrect value for af0, expected 3.19611746817827225e-04, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-1.02318153949454427e-12), + "incorrect value for af1, expected -1.02318153949454427e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(9.12696123123168945e-08), + "incorrect value for c_ic, expected 9.12696123123168945e-08, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(5.21540641784667969e-08), + "incorrect value for c_is, expected 5.21540641784667969e-08, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(2.47281250000000000e+02), + "incorrect value for c_rc, expected 2.47281250000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(-7.79062500000000000e+01), + "incorrect value for c_rs, expected -7.79062500000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(-3.72342765331268311e-06), + "incorrect value for c_uc, expected -3.72342765331268311e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(7.17863440513610840e-06), + "incorrect value for c_us, expected 7.17863440513610840e-06, is {:e}", + msg.c_us + ); + assert!( + msg.dn.almost_eq(4.40089760076414576e-09), + "incorrect value for dn, expected 4.40089760076414576e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(8.17864493001252413e-03), + "incorrect value for ecc, expected 8.17864493001252413e-03, is {:e}", + msg.ecc + ); + assert_eq!( + msg.healthy, 1, + "incorrect value for healthy, expected 1, is {}", + msg.healthy + ); + assert!( + msg.inc.almost_eq(9.75512201724530126e-01), + "incorrect value for inc, expected 9.75512201724530126e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(-5.88238788220950205e-10), + "incorrect value for inc_dot, expected -5.88238788220950205e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iode, 0, + "incorrect value for iode, expected 0, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(1.94018234598241923e+00), + "incorrect value for m0, expected 1.94018234598241923e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(2.24186802892776615e+00), + "incorrect value for omega0, expected 2.24186802892776615e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-7.96247452616749420e-09), + "incorrect value for omegadot, expected -7.96247452616749420e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.prn, 30, + "incorrect value for prn, expected 30, is {}", + msg.prn + ); + assert!( + msg.sqrta.almost_eq(5.15375399208068848e+03), + "incorrect value for sqrta, expected 5.15375399208068848e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-1.35041773319244385e-08), + "incorrect value for tgd, expected -1.35041773319244385e-08, is {:e}", + msg.tgd + ); + assert!( + msg.toc_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toc_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toc_tow + ); + assert_eq!( + msg.toc_wn, 1838, + "incorrect value for toc_wn, expected 1838, is {}", + msg.toc_wn + ); + assert!( + msg.toe_tow.almost_eq(4.10400000000000000e+05), + "incorrect value for toe_tow, expected 4.10400000000000000e+05, is {:e}", + msg.toe_tow + ); + assert_eq!( + msg.toe_wn, 1838, + "incorrect value for toe_wn, expected 1838, is {}", + msg.toe_wn + ); + assert_eq!( + msg.valid, 1, + "incorrect value for valid, expected 1, is {}", + msg.valid + ); + assert!( + msg.w.almost_eq(-5.23790171608806143e-01), + "incorrect value for w, expected -5.23790171608806143e-01, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gal.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gal.rs index 28b8d47861..67fa8f2f37 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gal.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gal.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_ephemeris_gal() { { @@ -221,6 +226,13 @@ fn test_auto_check_sbp_observation_msg_ephemeris_gal() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_gal() { @@ -228,14 +240,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_gal() { let json_input = r#"{"common":{"sid":{"sat":27,"code":14},"toe":{"tow":448800,"wn":2154},"ura":3.119999885559082,"fit_interval":14400,"valid":1,"health_bits":0},"bgd_e1e5a":2.0954757928848267e-09,"bgd_e1e5b":2.0954757928848267e-09,"c_rs":10.125,"c_rc":265.4375,"c_uc":5.364418029785156e-07,"c_us":3.993511199951172e-06,"c_ic":-3.166496753692627e-08,"c_is":-3.166496753692627e-08,"dn":3.2262058129932258e-09,"m0":-1.8457115744155868,"ecc":0.00017060607206076384,"sqrta":5440.602401733398,"omega0":1.16967730598334,"omegadot":-5.757382675240872e-09,"w":0.12250912091662625,"inc":0.9777456094977858,"inc_dot":-3.1787038343451465e-10,"af0":-1.7088896129280325e-05,"af1":-8.185452315956353e-12,"af2":0.0,"toc":{"tow":448800,"wn":2154},"iode":108,"iodc":108,"source":0,"preamble":85,"msg_type":141,"sender":61568,"payload":"Gw4g2QYAaggUrkdAQDgAAAEAAAAQMQAAEDEAACJBALiEQwAAEDUAAIY2AAAIswAACLPZzIJpgLYrPvhqH9wIiP2/AAAAAJdcJj8AAAA3mkC1QDgmAY3/tvI/3pOIJ0+6OL5Qcsz7wVy/P+03EymxSe8/MUG98AjY9b3/////Q+vxvv///////6G9AAAAACDZBgBqCGwAbAAA","crc":53319,"length":153}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -429,3 +441,239 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_gal() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_ephemeris_gal`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_ephemeris_gal() { + { + let mut payload = Cursor::new(vec![ + 85, 141, 0, 128, 240, 153, 27, 14, 32, 217, 6, 0, 106, 8, 20, 174, 71, 64, 64, 56, 0, + 0, 1, 0, 0, 0, 16, 49, 0, 0, 16, 49, 0, 0, 34, 65, 0, 184, 132, 67, 0, 0, 16, 53, 0, 0, + 134, 54, 0, 0, 8, 179, 0, 0, 8, 179, 217, 204, 130, 105, 128, 182, 43, 62, 248, 106, + 31, 220, 8, 136, 253, 191, 0, 0, 0, 0, 151, 92, 38, 63, 0, 0, 0, 55, 154, 64, 181, 64, + 56, 38, 1, 141, 255, 182, 242, 63, 222, 147, 136, 39, 79, 186, 56, 190, 80, 114, 204, + 251, 193, 92, 191, 63, 237, 55, 19, 41, 177, 73, 239, 63, 49, 65, 189, 240, 8, 216, + 245, 189, 255, 255, 255, 255, 67, 235, 241, 190, 255, 255, 255, 255, 255, 255, 161, + 189, 0, 0, 0, 0, 32, 217, 6, 0, 106, 8, 108, 0, 108, 0, 0, 71, 208, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisGal( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisGal(msg) => { + assert_eq!( + msg.message_type(), + 0x8d, + "Incorrect message type, expected 0x8d, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xf080, + "incorrect sender id, expected 0xf080, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-1.70888961292803254e-05), + "incorrect value for af0, expected -1.70888961292803254e-05, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-8.18545231595635253e-12), + "incorrect value for af1, expected -8.18545231595635253e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.bgd_e1e5a.almost_eq(2.09547579288482666e-09), + "incorrect value for bgd_e1e5a, expected 2.09547579288482666e-09, is {:e}", + msg.bgd_e1e5a + ); + assert!( + msg.bgd_e1e5b.almost_eq(2.09547579288482666e-09), + "incorrect value for bgd_e1e5b, expected 2.09547579288482666e-09, is {:e}", + msg.bgd_e1e5b + ); + assert!( + msg.c_ic.almost_eq(-3.16649675369262695e-08), + "incorrect value for c_ic, expected -3.16649675369262695e-08, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(-3.16649675369262695e-08), + "incorrect value for c_is, expected -3.16649675369262695e-08, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(2.65437500000000000e+02), + "incorrect value for c_rc, expected 2.65437500000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(1.01250000000000000e+01), + "incorrect value for c_rs, expected 1.01250000000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(5.36441802978515625e-07), + "incorrect value for c_uc, expected 5.36441802978515625e-07, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(3.99351119995117188e-06), + "incorrect value for c_us, expected 3.99351119995117188e-06, is {:e}", + msg.c_us + ); + assert_eq!( + msg.common.fit_interval, 14400, + "incorrect value for common.fit_interval, expected 14400, is {}", + msg.common.fit_interval + ); + assert_eq!( + msg.common.health_bits, 0, + "incorrect value for common.health_bits, expected 0, is {}", + msg.common.health_bits + ); + assert_eq!( + msg.common.sid.code, 14, + "incorrect value for common.sid.code, expected 14, is {}", + msg.common.sid.code + ); + assert_eq!( + msg.common.sid.sat, 27, + "incorrect value for common.sid.sat, expected 27, is {}", + msg.common.sid.sat + ); + assert_eq!( + msg.common.toe.tow, 448800, + "incorrect value for common.toe.tow, expected 448800, is {}", + msg.common.toe.tow + ); + assert_eq!( + msg.common.toe.wn, 2154, + "incorrect value for common.toe.wn, expected 2154, is {}", + msg.common.toe.wn + ); + assert!( + msg.common.ura.almost_eq(3.11999988555908203e+00), + "incorrect value for common.ura, expected 3.11999988555908203e+00, is {:e}", + msg.common.ura + ); + assert_eq!( + msg.common.valid, 1, + "incorrect value for common.valid, expected 1, is {}", + msg.common.valid + ); + assert!( + msg.dn.almost_eq(3.22620581299322575e-09), + "incorrect value for dn, expected 3.22620581299322575e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(1.70606072060763836e-04), + "incorrect value for ecc, expected 1.70606072060763836e-04, is {:e}", + msg.ecc + ); + assert!( + msg.inc.almost_eq(9.77745609497785773e-01), + "incorrect value for inc, expected 9.77745609497785773e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(-3.17870383434514646e-10), + "incorrect value for inc_dot, expected -3.17870383434514646e-10, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iodc, 108, + "incorrect value for iodc, expected 108, is {}", + msg.iodc + ); + assert_eq!( + msg.iode, 108, + "incorrect value for iode, expected 108, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(-1.84571157441558675e+00), + "incorrect value for m0, expected -1.84571157441558675e+00, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(1.16967730598334008e+00), + "incorrect value for omega0, expected 1.16967730598334008e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-5.75738267524087236e-09), + "incorrect value for omegadot, expected -5.75738267524087236e-09, is {:e}", + msg.omegadot + ); + assert_eq!( + msg.source, 0, + "incorrect value for source, expected 0, is {}", + msg.source + ); + assert!( + msg.sqrta.almost_eq(5.44060240173339844e+03), + "incorrect value for sqrta, expected 5.44060240173339844e+03, is {:e}", + msg.sqrta + ); + assert_eq!( + msg.toc.tow, 448800, + "incorrect value for toc.tow, expected 448800, is {}", + msg.toc.tow + ); + assert_eq!( + msg.toc.wn, 2154, + "incorrect value for toc.wn, expected 2154, is {}", + msg.toc.wn + ); + assert!( + msg.w.almost_eq(1.22509120916626246e-01), + "incorrect value for w, expected 1.22509120916626246e-01, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisGal"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_glo.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_glo.rs index b4d10d4ca3..7ec810d5c7 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_glo.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_glo.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_ephemeris_glo() { { @@ -163,6 +168,13 @@ fn test_auto_check_sbp_observation_msg_ephemeris_glo() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_glo() { @@ -170,14 +182,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_glo() { let json_input = r#"{"common":{"sid":{"sat":4,"code":3},"toe":{"tow":443718,"wn":2154},"ura":5.0,"fit_interval":2400,"valid":1,"health_bits":0},"gamma":9.094947017729282e-13,"tau":-8.36281105875969e-05,"d_tau":-2.7939677238464355e-09,"pos":[-12177330.078125,599893.06640625,-22373708.49609375],"vel":[-1726.506233215332,-2542.6149368286133,869.8177337646484],"acc":[9.313225746154785e-07,9.313225746154785e-07,2.7939677238464355e-06],"fcn":14,"iod":100,"preamble":85,"msg_type":139,"sender":2314,"payload":"BANGxQYAaggAAKBAYAkAAAEAAACAK4Bhr7gAAECxAACAQvY5Z8EAAAAiqk4iQQAA8MdUVnXBAAAAYgb6msAAAADZOt2jwAAAALiKLotAAAB6NQAAejUAgDs2DmQ=","crc":38233,"length":92}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -316,3 +328,181 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_glo() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_ephemeris_glo`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_ephemeris_glo() { + { + let mut payload = Cursor::new(vec![ + 85, 139, 0, 10, 9, 92, 4, 3, 70, 197, 6, 0, 106, 8, 0, 0, 160, 64, 96, 9, 0, 0, 1, 0, + 0, 0, 128, 43, 128, 97, 175, 184, 0, 0, 64, 177, 0, 0, 128, 66, 246, 57, 103, 193, 0, + 0, 0, 34, 170, 78, 34, 65, 0, 0, 240, 199, 84, 86, 117, 193, 0, 0, 0, 98, 6, 250, 154, + 192, 0, 0, 0, 217, 58, 221, 163, 192, 0, 0, 0, 184, 138, 46, 139, 64, 0, 0, 122, 53, 0, + 0, 122, 53, 0, 128, 59, 54, 14, 100, 89, 149, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisGlo( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisGlo(msg) => { + assert_eq!( + msg.message_type(), + 0x8b, + "Incorrect message type, expected 0x8b, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x90a, + "incorrect sender id, expected 0x90a, is {}", + sender_id + ); + assert!( + msg.acc[0].almost_eq(9.31322574615478516e-07), + "incorrect value for acc[0], expected 9.31322574615478516e-07, is {:e}", + msg.acc[0] + ); + assert!( + msg.acc[1].almost_eq(9.31322574615478516e-07), + "incorrect value for acc[1], expected 9.31322574615478516e-07, is {:e}", + msg.acc[1] + ); + assert!( + msg.acc[2].almost_eq(2.79396772384643555e-06), + "incorrect value for acc[2], expected 2.79396772384643555e-06, is {:e}", + msg.acc[2] + ); + assert_eq!( + msg.common.fit_interval, 2400, + "incorrect value for common.fit_interval, expected 2400, is {}", + msg.common.fit_interval + ); + assert_eq!( + msg.common.health_bits, 0, + "incorrect value for common.health_bits, expected 0, is {}", + msg.common.health_bits + ); + assert_eq!( + msg.common.sid.code, 3, + "incorrect value for common.sid.code, expected 3, is {}", + msg.common.sid.code + ); + assert_eq!( + msg.common.sid.sat, 4, + "incorrect value for common.sid.sat, expected 4, is {}", + msg.common.sid.sat + ); + assert_eq!( + msg.common.toe.tow, 443718, + "incorrect value for common.toe.tow, expected 443718, is {}", + msg.common.toe.tow + ); + assert_eq!( + msg.common.toe.wn, 2154, + "incorrect value for common.toe.wn, expected 2154, is {}", + msg.common.toe.wn + ); + assert!( + msg.common.ura.almost_eq(5.00000000000000000e+00), + "incorrect value for common.ura, expected 5.00000000000000000e+00, is {:e}", + msg.common.ura + ); + assert_eq!( + msg.common.valid, 1, + "incorrect value for common.valid, expected 1, is {}", + msg.common.valid + ); + assert!( + msg.d_tau.almost_eq(-2.79396772384643555e-09), + "incorrect value for d_tau, expected -2.79396772384643555e-09, is {:e}", + msg.d_tau + ); + assert_eq!( + msg.fcn, 14, + "incorrect value for fcn, expected 14, is {}", + msg.fcn + ); + assert!( + msg.gamma.almost_eq(9.09494701772928238e-13), + "incorrect value for gamma, expected 9.09494701772928238e-13, is {:e}", + msg.gamma + ); + assert_eq!( + msg.iod, 100, + "incorrect value for iod, expected 100, is {}", + msg.iod + ); + assert!( + msg.pos[0].almost_eq(-1.21773300781250000e+07), + "incorrect value for pos[0], expected -1.21773300781250000e+07, is {:e}", + msg.pos[0] + ); + assert!( + msg.pos[1].almost_eq(5.99893066406250000e+05), + "incorrect value for pos[1], expected 5.99893066406250000e+05, is {:e}", + msg.pos[1] + ); + assert!( + msg.pos[2].almost_eq(-2.23737084960937500e+07), + "incorrect value for pos[2], expected -2.23737084960937500e+07, is {:e}", + msg.pos[2] + ); + assert!( + msg.tau.almost_eq(-8.36281105875968933e-05), + "incorrect value for tau, expected -8.36281105875968933e-05, is {:e}", + msg.tau + ); + assert!( + msg.vel[0].almost_eq(-1.72650623321533203e+03), + "incorrect value for vel[0], expected -1.72650623321533203e+03, is {:e}", + msg.vel[0] + ); + assert!( + msg.vel[1].almost_eq(-2.54261493682861328e+03), + "incorrect value for vel[1], expected -2.54261493682861328e+03, is {:e}", + msg.vel[1] + ); + assert!( + msg.vel[2].almost_eq(8.69817733764648438e+02), + "incorrect value for vel[2], expected 8.69817733764648438e+02, is {:e}", + msg.vel[2] + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisGlo"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gps.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gps.rs index 2d3c56e40b..c7103e408a 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gps.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_gps.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_ephemeris_gps() { { @@ -211,6 +216,13 @@ fn test_auto_check_sbp_observation_msg_ephemeris_gps() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_gps() { @@ -218,14 +230,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_gps() { let json_input = r#"{"common":{"sid":{"sat":22,"code":0},"toe":{"tow":446384,"wn":2154},"ura":2.0,"fit_interval":14400,"valid":1,"health_bits":0},"tgd":-1.7695128917694092e-08,"c_rs":-52.3125,"c_rc":308.625,"c_uc":-2.7436763048171997e-06,"c_us":3.1366944313049316e-06,"c_ic":7.450580596923828e-09,"c_is":-1.1548399925231934e-07,"dn":5.694522914022375e-09,"m0":-0.02200078842114688,"ecc":0.007072207052260637,"sqrta":5153.550029754639,"omega0":-1.8731818448797617,"omegadot":-8.903585155774196e-09,"w":-0.9893036629599647,"inc":0.9341514480259797,"inc_dot":-4.035882396415757e-11,"af0":-0.0006315018981695175,"af1":8.981260180007666e-12,"af2":0.0,"toc":{"tow":446384,"wn":2154},"iode":45,"iodc":45,"preamble":85,"msg_type":138,"sender":2314,"payload":"FgCwzwYAaggAAABAQDgAAAEAAACYsgBAUcIAUJpDACA4tgCAUjYAAAAyAAD4s3LYYLQxdTg+jikF61+Hlr8AAAAgv/d8PwAAwM6MIbRAKYOzho34/b/jhVE2zB5Dvtg7xydgqO+/RwvZk5Hk7T/dL2Tg/y/GvWCLJboAAB4tAAAAALDPBgBqCC0tAA==","crc":1194,"length":139}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -409,3 +421,229 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_gps() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_ephemeris_gps`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_ephemeris_gps() { + { + let mut payload = Cursor::new(vec![ + 85, 138, 0, 10, 9, 139, 22, 0, 176, 207, 6, 0, 106, 8, 0, 0, 0, 64, 64, 56, 0, 0, 1, 0, + 0, 0, 152, 178, 0, 64, 81, 194, 0, 80, 154, 67, 0, 32, 56, 182, 0, 128, 82, 54, 0, 0, + 0, 50, 0, 0, 248, 179, 114, 216, 96, 180, 49, 117, 56, 62, 142, 41, 5, 235, 95, 135, + 150, 191, 0, 0, 0, 32, 191, 247, 124, 63, 0, 0, 192, 206, 140, 33, 180, 64, 41, 131, + 179, 134, 141, 248, 253, 191, 227, 133, 81, 54, 204, 30, 67, 190, 216, 59, 199, 39, 96, + 168, 239, 191, 71, 11, 217, 147, 145, 228, 237, 63, 221, 47, 100, 224, 255, 47, 198, + 189, 96, 139, 37, 186, 0, 0, 30, 45, 0, 0, 0, 0, 176, 207, 6, 0, 106, 8, 45, 45, 0, + 170, 4, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisGps( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisGps(msg) => { + assert_eq!( + msg.message_type(), + 0x8a, + "Incorrect message type, expected 0x8a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x90a, + "incorrect sender id, expected 0x90a, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-6.31501898169517517e-04), + "incorrect value for af0, expected -6.31501898169517517e-04, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(8.98126018000766635e-12), + "incorrect value for af1, expected 8.98126018000766635e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(7.45058059692382812e-09), + "incorrect value for c_ic, expected 7.45058059692382812e-09, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(-1.15483999252319336e-07), + "incorrect value for c_is, expected -1.15483999252319336e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(3.08625000000000000e+02), + "incorrect value for c_rc, expected 3.08625000000000000e+02, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(-5.23125000000000000e+01), + "incorrect value for c_rs, expected -5.23125000000000000e+01, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(-2.74367630481719971e-06), + "incorrect value for c_uc, expected -2.74367630481719971e-06, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(3.13669443130493164e-06), + "incorrect value for c_us, expected 3.13669443130493164e-06, is {:e}", + msg.c_us + ); + assert_eq!( + msg.common.fit_interval, 14400, + "incorrect value for common.fit_interval, expected 14400, is {}", + msg.common.fit_interval + ); + assert_eq!( + msg.common.health_bits, 0, + "incorrect value for common.health_bits, expected 0, is {}", + msg.common.health_bits + ); + assert_eq!( + msg.common.sid.code, 0, + "incorrect value for common.sid.code, expected 0, is {}", + msg.common.sid.code + ); + assert_eq!( + msg.common.sid.sat, 22, + "incorrect value for common.sid.sat, expected 22, is {}", + msg.common.sid.sat + ); + assert_eq!( + msg.common.toe.tow, 446384, + "incorrect value for common.toe.tow, expected 446384, is {}", + msg.common.toe.tow + ); + assert_eq!( + msg.common.toe.wn, 2154, + "incorrect value for common.toe.wn, expected 2154, is {}", + msg.common.toe.wn + ); + assert!( + msg.common.ura.almost_eq(2.00000000000000000e+00), + "incorrect value for common.ura, expected 2.00000000000000000e+00, is {:e}", + msg.common.ura + ); + assert_eq!( + msg.common.valid, 1, + "incorrect value for common.valid, expected 1, is {}", + msg.common.valid + ); + assert!( + msg.dn.almost_eq(5.69452291402237483e-09), + "incorrect value for dn, expected 5.69452291402237483e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(7.07220705226063728e-03), + "incorrect value for ecc, expected 7.07220705226063728e-03, is {:e}", + msg.ecc + ); + assert!( + msg.inc.almost_eq(9.34151448025979714e-01), + "incorrect value for inc, expected 9.34151448025979714e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(-4.03588239641575691e-11), + "incorrect value for inc_dot, expected -4.03588239641575691e-11, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iodc, 45, + "incorrect value for iodc, expected 45, is {}", + msg.iodc + ); + assert_eq!( + msg.iode, 45, + "incorrect value for iode, expected 45, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(-2.20007884211468815e-02), + "incorrect value for m0, expected -2.20007884211468815e-02, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(-1.87318184487976169e+00), + "incorrect value for omega0, expected -1.87318184487976169e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-8.90358515577419627e-09), + "incorrect value for omegadot, expected -8.90358515577419627e-09, is {:e}", + msg.omegadot + ); + assert!( + msg.sqrta.almost_eq(5.15355002975463867e+03), + "incorrect value for sqrta, expected 5.15355002975463867e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-1.76951289176940918e-08), + "incorrect value for tgd, expected -1.76951289176940918e-08, is {:e}", + msg.tgd + ); + assert_eq!( + msg.toc.tow, 446384, + "incorrect value for toc.tow, expected 446384, is {}", + msg.toc.tow + ); + assert_eq!( + msg.toc.wn, 2154, + "incorrect value for toc.wn, expected 2154, is {}", + msg.toc.wn + ); + assert!( + msg.w.almost_eq(-9.89303662959964747e-01), + "incorrect value for w, expected -9.89303662959964747e-01, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisGPS"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_qzss.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_qzss.rs index b6d95669f9..36efb94875 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_qzss.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_ephemeris_qzss.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_ephemeris_qzss() { { @@ -211,6 +216,13 @@ fn test_auto_check_sbp_observation_msg_ephemeris_qzss() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_qzss() { @@ -218,14 +230,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_qzss() { let json_input = r#"{"common":{"sid":{"sat":193,"code":31},"toe":{"tow":450000,"wn":2154},"ura":2.0,"fit_interval":14400,"valid":0,"health_bits":0},"tgd":-5.587935447692871e-09,"c_rs":-457.8125,"c_rc":-36.90625,"c_uc":-1.6197562217712402e-05,"c_us":8.247792720794678e-06,"c_ic":-1.1753290891647339e-06,"c_is":1.6205012798309326e-07,"dn":2.678325848736433e-09,"m0":0.30694242158961144,"ecc":0.07550019584596157,"sqrta":6493.172393798828,"omega0":-1.1693743795366662,"omegadot":-2.7936877968817684e-09,"w":-1.5662079690885238,"inc":0.7309715119432375,"inc_dot":2.0000833114980698e-11,"af0":-0.00036908406764268875,"af1":-4.774847184307873e-12,"af2":0.0,"toc":{"tow":450000,"wn":2154},"iode":49,"iodc":817,"preamble":85,"msg_type":142,"sender":61568,"payload":"wR/Q3QYAaggAAABAQDgAAAAAAADAsQDo5MMAoBPCAOCHtwBgCjcAwJ21AAAuNKdIa2mzASc+D+Ce0/Gk0z8AAAAY+1OzPwAAACIsXblAjz7O6MG18r/P2EVqYv8nvkGEXxYwD/m/+VJDXh5k5z91p7vpu/21PaCBwbkAAKisAAAAANDdBgBqCDExAw==","crc":6014,"length":139}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -409,3 +421,229 @@ fn test_json2sbp_auto_check_sbp_observation_msg_ephemeris_qzss() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_ephemeris_qzss`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_ephemeris_qzss() { + { + let mut payload = Cursor::new(vec![ + 85, 142, 0, 128, 240, 139, 193, 31, 208, 221, 6, 0, 106, 8, 0, 0, 0, 64, 64, 56, 0, 0, + 0, 0, 0, 0, 192, 177, 0, 232, 228, 195, 0, 160, 19, 194, 0, 224, 135, 183, 0, 96, 10, + 55, 0, 192, 157, 181, 0, 0, 46, 52, 167, 72, 107, 105, 179, 1, 39, 62, 15, 224, 158, + 211, 241, 164, 211, 63, 0, 0, 0, 24, 251, 83, 179, 63, 0, 0, 0, 34, 44, 93, 185, 64, + 143, 62, 206, 232, 193, 181, 242, 191, 207, 216, 69, 106, 98, 255, 39, 190, 65, 132, + 95, 22, 48, 15, 249, 191, 249, 82, 67, 94, 30, 100, 231, 63, 117, 167, 187, 233, 187, + 253, 181, 61, 160, 129, 193, 185, 0, 0, 168, 172, 0, 0, 0, 0, 208, 221, 6, 0, 106, 8, + 49, 49, 3, 126, 23, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgEphemerisQzss( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgEphemerisQzss(msg) => { + assert_eq!( + msg.message_type(), + 0x8e, + "Incorrect message type, expected 0x8e, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xf080, + "incorrect sender id, expected 0xf080, is {}", + sender_id + ); + assert!( + msg.af0.almost_eq(-3.69084067642688751e-04), + "incorrect value for af0, expected -3.69084067642688751e-04, is {:e}", + msg.af0 + ); + assert!( + msg.af1.almost_eq(-4.77484718430787325e-12), + "incorrect value for af1, expected -4.77484718430787325e-12, is {:e}", + msg.af1 + ); + assert!( + msg.af2.almost_eq(0.00000000000000000e+00), + "incorrect value for af2, expected 0.00000000000000000e+00, is {:e}", + msg.af2 + ); + assert!( + msg.c_ic.almost_eq(-1.17532908916473389e-06), + "incorrect value for c_ic, expected -1.17532908916473389e-06, is {:e}", + msg.c_ic + ); + assert!( + msg.c_is.almost_eq(1.62050127983093262e-07), + "incorrect value for c_is, expected 1.62050127983093262e-07, is {:e}", + msg.c_is + ); + assert!( + msg.c_rc.almost_eq(-3.69062500000000000e+01), + "incorrect value for c_rc, expected -3.69062500000000000e+01, is {:e}", + msg.c_rc + ); + assert!( + msg.c_rs.almost_eq(-4.57812500000000000e+02), + "incorrect value for c_rs, expected -4.57812500000000000e+02, is {:e}", + msg.c_rs + ); + assert!( + msg.c_uc.almost_eq(-1.61975622177124023e-05), + "incorrect value for c_uc, expected -1.61975622177124023e-05, is {:e}", + msg.c_uc + ); + assert!( + msg.c_us.almost_eq(8.24779272079467773e-06), + "incorrect value for c_us, expected 8.24779272079467773e-06, is {:e}", + msg.c_us + ); + assert_eq!( + msg.common.fit_interval, 14400, + "incorrect value for common.fit_interval, expected 14400, is {}", + msg.common.fit_interval + ); + assert_eq!( + msg.common.health_bits, 0, + "incorrect value for common.health_bits, expected 0, is {}", + msg.common.health_bits + ); + assert_eq!( + msg.common.sid.code, 31, + "incorrect value for common.sid.code, expected 31, is {}", + msg.common.sid.code + ); + assert_eq!( + msg.common.sid.sat, 193, + "incorrect value for common.sid.sat, expected 193, is {}", + msg.common.sid.sat + ); + assert_eq!( + msg.common.toe.tow, 450000, + "incorrect value for common.toe.tow, expected 450000, is {}", + msg.common.toe.tow + ); + assert_eq!( + msg.common.toe.wn, 2154, + "incorrect value for common.toe.wn, expected 2154, is {}", + msg.common.toe.wn + ); + assert!( + msg.common.ura.almost_eq(2.00000000000000000e+00), + "incorrect value for common.ura, expected 2.00000000000000000e+00, is {:e}", + msg.common.ura + ); + assert_eq!( + msg.common.valid, 0, + "incorrect value for common.valid, expected 0, is {}", + msg.common.valid + ); + assert!( + msg.dn.almost_eq(2.67832584873643293e-09), + "incorrect value for dn, expected 2.67832584873643293e-09, is {:e}", + msg.dn + ); + assert!( + msg.ecc.almost_eq(7.55001958459615707e-02), + "incorrect value for ecc, expected 7.55001958459615707e-02, is {:e}", + msg.ecc + ); + assert!( + msg.inc.almost_eq(7.30971511943237506e-01), + "incorrect value for inc, expected 7.30971511943237506e-01, is {:e}", + msg.inc + ); + assert!( + msg.inc_dot.almost_eq(2.00008331149806980e-11), + "incorrect value for inc_dot, expected 2.00008331149806980e-11, is {:e}", + msg.inc_dot + ); + assert_eq!( + msg.iodc, 817, + "incorrect value for iodc, expected 817, is {}", + msg.iodc + ); + assert_eq!( + msg.iode, 49, + "incorrect value for iode, expected 49, is {}", + msg.iode + ); + assert!( + msg.m0.almost_eq(3.06942421589611436e-01), + "incorrect value for m0, expected 3.06942421589611436e-01, is {:e}", + msg.m0 + ); + assert!( + msg.omega0.almost_eq(-1.16937437953666623e+00), + "incorrect value for omega0, expected -1.16937437953666623e+00, is {:e}", + msg.omega0 + ); + assert!( + msg.omegadot.almost_eq(-2.79368779688176837e-09), + "incorrect value for omegadot, expected -2.79368779688176837e-09, is {:e}", + msg.omegadot + ); + assert!( + msg.sqrta.almost_eq(6.49317239379882812e+03), + "incorrect value for sqrta, expected 6.49317239379882812e+03, is {:e}", + msg.sqrta + ); + assert!( + msg.tgd.almost_eq(-5.58793544769287109e-09), + "incorrect value for tgd, expected -5.58793544769287109e-09, is {:e}", + msg.tgd + ); + assert_eq!( + msg.toc.tow, 450000, + "incorrect value for toc.tow, expected 450000, is {}", + msg.toc.tow + ); + assert_eq!( + msg.toc.wn, 2154, + "incorrect value for toc.wn, expected 2154, is {}", + msg.toc.wn + ); + assert!( + msg.w.almost_eq(-1.56620796908852378e+00), + "incorrect value for w, expected -1.56620796908852378e+00, is {:e}", + msg.w + ); + } + _ => panic!("Invalid message type! Expected a MsgEphemerisQzss"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_glo_biases.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_glo_biases.rs index 75f51cda72..b747569e61 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_glo_biases.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_glo_biases.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_glo_biases() { { @@ -74,6 +79,13 @@ fn test_auto_check_sbp_observation_msg_glo_biases() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_glo_biases() { @@ -81,14 +93,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_glo_biases() { let json_input = r#"{"mask":0,"l1ca_bias":0,"l1p_bias":0,"l2ca_bias":0,"l2p_bias":0,"preamble":85,"msg_type":117,"sender":0,"payload":"AAAAAAAAAAAA","crc":54093,"length":9}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -142,3 +154,92 @@ fn test_json2sbp_auto_check_sbp_observation_msg_glo_biases() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_glo_biases`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_glo_biases() { + { + let mut payload = Cursor::new(vec![ + 85, 117, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 211, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGloBiases( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGloBiases(msg) => { + assert_eq!( + msg.message_type(), + 0x75, + "Incorrect message type, expected 0x75, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0, + "incorrect sender id, expected 0, is {}", + sender_id + ); + assert_eq!( + msg.l1ca_bias, 0, + "incorrect value for l1ca_bias, expected 0, is {}", + msg.l1ca_bias + ); + assert_eq!( + msg.l1p_bias, 0, + "incorrect value for l1p_bias, expected 0, is {}", + msg.l1p_bias + ); + assert_eq!( + msg.l2ca_bias, 0, + "incorrect value for l2ca_bias, expected 0, is {}", + msg.l2ca_bias + ); + assert_eq!( + msg.l2p_bias, 0, + "incorrect value for l2p_bias, expected 0, is {}", + msg.l2p_bias + ); + assert_eq!( + msg.mask, 0, + "incorrect value for mask, expected 0, is {}", + msg.mask + ); + } + _ => panic!("Invalid message type! Expected a MsgGloBiases"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs.rs index c1be8d52be..3686bf3328 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_obs() { { @@ -833,6 +838,13 @@ fn test_auto_check_sbp_observation_msg_obs() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_obs() { @@ -840,14 +852,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs() { let json_input = r#"{"header":{"t":{"tow":434293400,"ns_residual":0,"wn":2154},"n_obs":32},"obs":[{"P":1056891697,"L":{"i":111080057,"f":146},"D":{"i":-1536,"f":172},"cn0":182,"lock":10,"flags":15,"sid":{"sat":10,"code":0}},{"P":1056891934,"L":{"i":86555916,"f":59},"D":{"i":-1197,"f":172},"cn0":178,"lock":10,"flags":15,"sid":{"sat":10,"code":1}},{"P":1217449431,"L":{"i":127954794,"f":243},"D":{"i":-3219,"f":119},"cn0":158,"lock":10,"flags":15,"sid":{"sat":18,"code":0}},{"P":1217449753,"L":{"i":99705055,"f":12},"D":{"i":-2508,"f":27},"cn0":125,"lock":9,"flags":11,"sid":{"sat":18,"code":1}},{"P":1256175650,"L":{"i":132024982,"f":53},"D":{"i":2829,"f":245},"cn0":114,"lock":9,"flags":15,"sid":{"sat":22,"code":0}},{"P":1158041713,"L":{"i":121711010,"f":70},"D":{"i":-2433,"f":246},"cn0":189,"lock":9,"flags":15,"sid":{"sat":23,"code":0}},{"P":1158041847,"L":{"i":94839765,"f":221},"D":{"i":-1896,"f":231},"cn0":158,"lock":9,"flags":11,"sid":{"sat":23,"code":1}},{"P":1084658184,"L":{"i":113998348,"f":114},"D":{"i":-1997,"f":67},"cn0":93,"lock":3,"flags":11,"sid":{"sat":27,"code":0}},{"P":1269673181,"L":{"i":133443545,"f":232},"D":{"i":3041,"f":237},"cn0":123,"lock":5,"flags":15,"sid":{"sat":31,"code":0}},{"P":1269673722,"L":{"i":103982040,"f":40},"D":{"i":2374,"f":62},"cn0":120,"lock":3,"flags":11,"sid":{"sat":31,"code":1}},{"P":1107693703,"L":{"i":118217315,"f":7},"D":{"i":-3446,"f":96},"cn0":176,"lock":10,"flags":15,"sid":{"sat":2,"code":3}},{"P":973505172,"L":{"i":104224985,"f":203},"D":{"i":-1003,"f":96},"cn0":170,"lock":10,"flags":15,"sid":{"sat":3,"code":3}},{"P":1069903034,"L":{"i":114505343,"f":80},"D":{"i":-3836,"f":219},"cn0":200,"lock":10,"flags":15,"sid":{"sat":17,"code":3}},{"P":956875687,"L":{"i":102157331,"f":105},"D":{"i":-461,"f":182},"cn0":152,"lock":10,"flags":15,"sid":{"sat":18,"code":3}}],"preamble":85,"msg_type":74,"sender":61569,"payload":"mMriGQAAAABqCCAx4/4+efKeBpIA+qy2Cg8KAB7k/j4MvSgFO1P7rLIKDwoB182QSGpvoAfzbfN3ngoPEgAZz5BI32DxBQw09ht9CQsSASK430qWit4HNQ0L9XIJDxYAcVAGRaIpQQdGf/b2vQkPFwD3UAZF1SOnBd2Y+OeeCQsXAQiSpkAMessGcjP4Q10DCxsA3aytS9kv9Afo4QvtewUPHwD6rq1L2KMyBihGCT54AwsfAYcQBkJj2gsHB4ryYLAKDwIDlIIGOtlYNgbLFfxgqgoPAwO6bMU/fzbTBlAE8dvICg8RA6fDCDkTzBYGaTP+tpgKDxID","crc":63725,"length":249}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1599,14 +1611,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs() { let json_input = r#"{"header":{"t":{"tow":434293400,"ns_residual":0,"wn":2154},"n_obs":16},"obs":[],"preamble":85,"msg_type":74,"sender":61569,"payload":"mMriGQAAAABqCBA=","crc":26057,"length":11}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1655,3 +1667,870 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_obs`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_obs() { + { + let mut payload = Cursor::new(vec![ + 85, 74, 0, 129, 240, 249, 152, 202, 226, 25, 0, 0, 0, 0, 106, 8, 32, 49, 227, 254, 62, + 121, 242, 158, 6, 146, 0, 250, 172, 182, 10, 15, 10, 0, 30, 228, 254, 62, 12, 189, 40, + 5, 59, 83, 251, 172, 178, 10, 15, 10, 1, 215, 205, 144, 72, 106, 111, 160, 7, 243, 109, + 243, 119, 158, 10, 15, 18, 0, 25, 207, 144, 72, 223, 96, 241, 5, 12, 52, 246, 27, 125, + 9, 11, 18, 1, 34, 184, 223, 74, 150, 138, 222, 7, 53, 13, 11, 245, 114, 9, 15, 22, 0, + 113, 80, 6, 69, 162, 41, 65, 7, 70, 127, 246, 246, 189, 9, 15, 23, 0, 247, 80, 6, 69, + 213, 35, 167, 5, 221, 152, 248, 231, 158, 9, 11, 23, 1, 8, 146, 166, 64, 12, 122, 203, + 6, 114, 51, 248, 67, 93, 3, 11, 27, 0, 221, 172, 173, 75, 217, 47, 244, 7, 232, 225, + 11, 237, 123, 5, 15, 31, 0, 250, 174, 173, 75, 216, 163, 50, 6, 40, 70, 9, 62, 120, 3, + 11, 31, 1, 135, 16, 6, 66, 99, 218, 11, 7, 7, 138, 242, 96, 176, 10, 15, 2, 3, 148, + 130, 6, 58, 217, 88, 54, 6, 203, 21, 252, 96, 170, 10, 15, 3, 3, 186, 108, 197, 63, + 127, 54, 211, 6, 80, 4, 241, 219, 200, 10, 15, 17, 3, 167, 195, 8, 57, 19, 204, 22, 6, + 105, 51, 254, 182, 152, 10, 15, 18, 3, 237, 248, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObs( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObs(msg) => { + assert_eq!( + msg.message_type(), + 0x4a, + "Incorrect message type, expected 0x4a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xf081, + "incorrect sender id, expected 0xf081, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.ns_residual, 0, + "incorrect value for header.t.ns_residual, expected 0, is {}", + msg.header.t.ns_residual + ); + assert_eq!( + msg.header.t.tow, 434293400, + "incorrect value for header.t.tow, expected 434293400, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 2154, + "incorrect value for header.t.wn, expected 2154, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].d.f, 172, + "incorrect value for obs[0].d.f, expected 172, is {}", + msg.obs[0].d.f + ); + assert_eq!( + msg.obs[0].d.i, -1536, + "incorrect value for obs[0].d.i, expected -1536, is {}", + msg.obs[0].d.i + ); + assert_eq!( + msg.obs[0].l.f, 146, + "incorrect value for obs[0].l.f, expected 146, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 111080057, + "incorrect value for obs[0].l.i, expected 111080057, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1056891697, + "incorrect value for obs[0].p, expected 1056891697, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 182, + "incorrect value for obs[0].cn0, expected 182, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].flags, 15, + "incorrect value for obs[0].flags, expected 15, is {}", + msg.obs[0].flags + ); + assert_eq!( + msg.obs[0].lock, 10, + "incorrect value for obs[0].lock, expected 10, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.sat, 10, + "incorrect value for obs[0].sid.sat, expected 10, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].d.f, 172, + "incorrect value for obs[1].d.f, expected 172, is {}", + msg.obs[1].d.f + ); + assert_eq!( + msg.obs[1].d.i, -1197, + "incorrect value for obs[1].d.i, expected -1197, is {}", + msg.obs[1].d.i + ); + assert_eq!( + msg.obs[1].l.f, 59, + "incorrect value for obs[1].l.f, expected 59, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 86555916, + "incorrect value for obs[1].l.i, expected 86555916, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1056891934, + "incorrect value for obs[1].p, expected 1056891934, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 178, + "incorrect value for obs[1].cn0, expected 178, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].flags, 15, + "incorrect value for obs[1].flags, expected 15, is {}", + msg.obs[1].flags + ); + assert_eq!( + msg.obs[1].lock, 10, + "incorrect value for obs[1].lock, expected 10, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 1, + "incorrect value for obs[1].sid.code, expected 1, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.sat, 10, + "incorrect value for obs[1].sid.sat, expected 10, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].d.f, 119, + "incorrect value for obs[2].d.f, expected 119, is {}", + msg.obs[2].d.f + ); + assert_eq!( + msg.obs[2].d.i, -3219, + "incorrect value for obs[2].d.i, expected -3219, is {}", + msg.obs[2].d.i + ); + assert_eq!( + msg.obs[2].l.f, 243, + "incorrect value for obs[2].l.f, expected 243, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 127954794, + "incorrect value for obs[2].l.i, expected 127954794, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1217449431, + "incorrect value for obs[2].p, expected 1217449431, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 158, + "incorrect value for obs[2].cn0, expected 158, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].flags, 15, + "incorrect value for obs[2].flags, expected 15, is {}", + msg.obs[2].flags + ); + assert_eq!( + msg.obs[2].lock, 10, + "incorrect value for obs[2].lock, expected 10, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.sat, 18, + "incorrect value for obs[2].sid.sat, expected 18, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].d.f, 27, + "incorrect value for obs[3].d.f, expected 27, is {}", + msg.obs[3].d.f + ); + assert_eq!( + msg.obs[3].d.i, -2508, + "incorrect value for obs[3].d.i, expected -2508, is {}", + msg.obs[3].d.i + ); + assert_eq!( + msg.obs[3].l.f, 12, + "incorrect value for obs[3].l.f, expected 12, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 99705055, + "incorrect value for obs[3].l.i, expected 99705055, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 1217449753, + "incorrect value for obs[3].p, expected 1217449753, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 125, + "incorrect value for obs[3].cn0, expected 125, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].flags, 11, + "incorrect value for obs[3].flags, expected 11, is {}", + msg.obs[3].flags + ); + assert_eq!( + msg.obs[3].lock, 9, + "incorrect value for obs[3].lock, expected 9, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 1, + "incorrect value for obs[3].sid.code, expected 1, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.sat, 18, + "incorrect value for obs[3].sid.sat, expected 18, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].d.f, 245, + "incorrect value for obs[4].d.f, expected 245, is {}", + msg.obs[4].d.f + ); + assert_eq!( + msg.obs[4].d.i, 2829, + "incorrect value for obs[4].d.i, expected 2829, is {}", + msg.obs[4].d.i + ); + assert_eq!( + msg.obs[4].l.f, 53, + "incorrect value for obs[4].l.f, expected 53, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 132024982, + "incorrect value for obs[4].l.i, expected 132024982, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1256175650, + "incorrect value for obs[4].p, expected 1256175650, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 114, + "incorrect value for obs[4].cn0, expected 114, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].flags, 15, + "incorrect value for obs[4].flags, expected 15, is {}", + msg.obs[4].flags + ); + assert_eq!( + msg.obs[4].lock, 9, + "incorrect value for obs[4].lock, expected 9, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.sat, 22, + "incorrect value for obs[4].sid.sat, expected 22, is {}", + msg.obs[4].sid.sat + ); + assert_eq!( + msg.obs[5].d.f, 246, + "incorrect value for obs[5].d.f, expected 246, is {}", + msg.obs[5].d.f + ); + assert_eq!( + msg.obs[5].d.i, -2433, + "incorrect value for obs[5].d.i, expected -2433, is {}", + msg.obs[5].d.i + ); + assert_eq!( + msg.obs[5].l.f, 70, + "incorrect value for obs[5].l.f, expected 70, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 121711010, + "incorrect value for obs[5].l.i, expected 121711010, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 1158041713, + "incorrect value for obs[5].p, expected 1158041713, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].cn0, 189, + "incorrect value for obs[5].cn0, expected 189, is {}", + msg.obs[5].cn0 + ); + assert_eq!( + msg.obs[5].flags, 15, + "incorrect value for obs[5].flags, expected 15, is {}", + msg.obs[5].flags + ); + assert_eq!( + msg.obs[5].lock, 9, + "incorrect value for obs[5].lock, expected 9, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].sid.code, 0, + "incorrect value for obs[5].sid.code, expected 0, is {}", + msg.obs[5].sid.code + ); + assert_eq!( + msg.obs[5].sid.sat, 23, + "incorrect value for obs[5].sid.sat, expected 23, is {}", + msg.obs[5].sid.sat + ); + assert_eq!( + msg.obs[6].d.f, 231, + "incorrect value for obs[6].d.f, expected 231, is {}", + msg.obs[6].d.f + ); + assert_eq!( + msg.obs[6].d.i, -1896, + "incorrect value for obs[6].d.i, expected -1896, is {}", + msg.obs[6].d.i + ); + assert_eq!( + msg.obs[6].l.f, 221, + "incorrect value for obs[6].l.f, expected 221, is {}", + msg.obs[6].l.f + ); + assert_eq!( + msg.obs[6].l.i, 94839765, + "incorrect value for obs[6].l.i, expected 94839765, is {}", + msg.obs[6].l.i + ); + assert_eq!( + msg.obs[6].p, 1158041847, + "incorrect value for obs[6].p, expected 1158041847, is {}", + msg.obs[6].p + ); + assert_eq!( + msg.obs[6].cn0, 158, + "incorrect value for obs[6].cn0, expected 158, is {}", + msg.obs[6].cn0 + ); + assert_eq!( + msg.obs[6].flags, 11, + "incorrect value for obs[6].flags, expected 11, is {}", + msg.obs[6].flags + ); + assert_eq!( + msg.obs[6].lock, 9, + "incorrect value for obs[6].lock, expected 9, is {}", + msg.obs[6].lock + ); + assert_eq!( + msg.obs[6].sid.code, 1, + "incorrect value for obs[6].sid.code, expected 1, is {}", + msg.obs[6].sid.code + ); + assert_eq!( + msg.obs[6].sid.sat, 23, + "incorrect value for obs[6].sid.sat, expected 23, is {}", + msg.obs[6].sid.sat + ); + assert_eq!( + msg.obs[7].d.f, 67, + "incorrect value for obs[7].d.f, expected 67, is {}", + msg.obs[7].d.f + ); + assert_eq!( + msg.obs[7].d.i, -1997, + "incorrect value for obs[7].d.i, expected -1997, is {}", + msg.obs[7].d.i + ); + assert_eq!( + msg.obs[7].l.f, 114, + "incorrect value for obs[7].l.f, expected 114, is {}", + msg.obs[7].l.f + ); + assert_eq!( + msg.obs[7].l.i, 113998348, + "incorrect value for obs[7].l.i, expected 113998348, is {}", + msg.obs[7].l.i + ); + assert_eq!( + msg.obs[7].p, 1084658184, + "incorrect value for obs[7].p, expected 1084658184, is {}", + msg.obs[7].p + ); + assert_eq!( + msg.obs[7].cn0, 93, + "incorrect value for obs[7].cn0, expected 93, is {}", + msg.obs[7].cn0 + ); + assert_eq!( + msg.obs[7].flags, 11, + "incorrect value for obs[7].flags, expected 11, is {}", + msg.obs[7].flags + ); + assert_eq!( + msg.obs[7].lock, 3, + "incorrect value for obs[7].lock, expected 3, is {}", + msg.obs[7].lock + ); + assert_eq!( + msg.obs[7].sid.code, 0, + "incorrect value for obs[7].sid.code, expected 0, is {}", + msg.obs[7].sid.code + ); + assert_eq!( + msg.obs[7].sid.sat, 27, + "incorrect value for obs[7].sid.sat, expected 27, is {}", + msg.obs[7].sid.sat + ); + assert_eq!( + msg.obs[8].d.f, 237, + "incorrect value for obs[8].d.f, expected 237, is {}", + msg.obs[8].d.f + ); + assert_eq!( + msg.obs[8].d.i, 3041, + "incorrect value for obs[8].d.i, expected 3041, is {}", + msg.obs[8].d.i + ); + assert_eq!( + msg.obs[8].l.f, 232, + "incorrect value for obs[8].l.f, expected 232, is {}", + msg.obs[8].l.f + ); + assert_eq!( + msg.obs[8].l.i, 133443545, + "incorrect value for obs[8].l.i, expected 133443545, is {}", + msg.obs[8].l.i + ); + assert_eq!( + msg.obs[8].p, 1269673181, + "incorrect value for obs[8].p, expected 1269673181, is {}", + msg.obs[8].p + ); + assert_eq!( + msg.obs[8].cn0, 123, + "incorrect value for obs[8].cn0, expected 123, is {}", + msg.obs[8].cn0 + ); + assert_eq!( + msg.obs[8].flags, 15, + "incorrect value for obs[8].flags, expected 15, is {}", + msg.obs[8].flags + ); + assert_eq!( + msg.obs[8].lock, 5, + "incorrect value for obs[8].lock, expected 5, is {}", + msg.obs[8].lock + ); + assert_eq!( + msg.obs[8].sid.code, 0, + "incorrect value for obs[8].sid.code, expected 0, is {}", + msg.obs[8].sid.code + ); + assert_eq!( + msg.obs[8].sid.sat, 31, + "incorrect value for obs[8].sid.sat, expected 31, is {}", + msg.obs[8].sid.sat + ); + assert_eq!( + msg.obs[9].d.f, 62, + "incorrect value for obs[9].d.f, expected 62, is {}", + msg.obs[9].d.f + ); + assert_eq!( + msg.obs[9].d.i, 2374, + "incorrect value for obs[9].d.i, expected 2374, is {}", + msg.obs[9].d.i + ); + assert_eq!( + msg.obs[9].l.f, 40, + "incorrect value for obs[9].l.f, expected 40, is {}", + msg.obs[9].l.f + ); + assert_eq!( + msg.obs[9].l.i, 103982040, + "incorrect value for obs[9].l.i, expected 103982040, is {}", + msg.obs[9].l.i + ); + assert_eq!( + msg.obs[9].p, 1269673722, + "incorrect value for obs[9].p, expected 1269673722, is {}", + msg.obs[9].p + ); + assert_eq!( + msg.obs[9].cn0, 120, + "incorrect value for obs[9].cn0, expected 120, is {}", + msg.obs[9].cn0 + ); + assert_eq!( + msg.obs[9].flags, 11, + "incorrect value for obs[9].flags, expected 11, is {}", + msg.obs[9].flags + ); + assert_eq!( + msg.obs[9].lock, 3, + "incorrect value for obs[9].lock, expected 3, is {}", + msg.obs[9].lock + ); + assert_eq!( + msg.obs[9].sid.code, 1, + "incorrect value for obs[9].sid.code, expected 1, is {}", + msg.obs[9].sid.code + ); + assert_eq!( + msg.obs[9].sid.sat, 31, + "incorrect value for obs[9].sid.sat, expected 31, is {}", + msg.obs[9].sid.sat + ); + assert_eq!( + msg.obs[10].d.f, 96, + "incorrect value for obs[10].d.f, expected 96, is {}", + msg.obs[10].d.f + ); + assert_eq!( + msg.obs[10].d.i, -3446, + "incorrect value for obs[10].d.i, expected -3446, is {}", + msg.obs[10].d.i + ); + assert_eq!( + msg.obs[10].l.f, 7, + "incorrect value for obs[10].l.f, expected 7, is {}", + msg.obs[10].l.f + ); + assert_eq!( + msg.obs[10].l.i, 118217315, + "incorrect value for obs[10].l.i, expected 118217315, is {}", + msg.obs[10].l.i + ); + assert_eq!( + msg.obs[10].p, 1107693703, + "incorrect value for obs[10].p, expected 1107693703, is {}", + msg.obs[10].p + ); + assert_eq!( + msg.obs[10].cn0, 176, + "incorrect value for obs[10].cn0, expected 176, is {}", + msg.obs[10].cn0 + ); + assert_eq!( + msg.obs[10].flags, 15, + "incorrect value for obs[10].flags, expected 15, is {}", + msg.obs[10].flags + ); + assert_eq!( + msg.obs[10].lock, 10, + "incorrect value for obs[10].lock, expected 10, is {}", + msg.obs[10].lock + ); + assert_eq!( + msg.obs[10].sid.code, 3, + "incorrect value for obs[10].sid.code, expected 3, is {}", + msg.obs[10].sid.code + ); + assert_eq!( + msg.obs[10].sid.sat, 2, + "incorrect value for obs[10].sid.sat, expected 2, is {}", + msg.obs[10].sid.sat + ); + assert_eq!( + msg.obs[11].d.f, 96, + "incorrect value for obs[11].d.f, expected 96, is {}", + msg.obs[11].d.f + ); + assert_eq!( + msg.obs[11].d.i, -1003, + "incorrect value for obs[11].d.i, expected -1003, is {}", + msg.obs[11].d.i + ); + assert_eq!( + msg.obs[11].l.f, 203, + "incorrect value for obs[11].l.f, expected 203, is {}", + msg.obs[11].l.f + ); + assert_eq!( + msg.obs[11].l.i, 104224985, + "incorrect value for obs[11].l.i, expected 104224985, is {}", + msg.obs[11].l.i + ); + assert_eq!( + msg.obs[11].p, 973505172, + "incorrect value for obs[11].p, expected 973505172, is {}", + msg.obs[11].p + ); + assert_eq!( + msg.obs[11].cn0, 170, + "incorrect value for obs[11].cn0, expected 170, is {}", + msg.obs[11].cn0 + ); + assert_eq!( + msg.obs[11].flags, 15, + "incorrect value for obs[11].flags, expected 15, is {}", + msg.obs[11].flags + ); + assert_eq!( + msg.obs[11].lock, 10, + "incorrect value for obs[11].lock, expected 10, is {}", + msg.obs[11].lock + ); + assert_eq!( + msg.obs[11].sid.code, 3, + "incorrect value for obs[11].sid.code, expected 3, is {}", + msg.obs[11].sid.code + ); + assert_eq!( + msg.obs[11].sid.sat, 3, + "incorrect value for obs[11].sid.sat, expected 3, is {}", + msg.obs[11].sid.sat + ); + assert_eq!( + msg.obs[12].d.f, 219, + "incorrect value for obs[12].d.f, expected 219, is {}", + msg.obs[12].d.f + ); + assert_eq!( + msg.obs[12].d.i, -3836, + "incorrect value for obs[12].d.i, expected -3836, is {}", + msg.obs[12].d.i + ); + assert_eq!( + msg.obs[12].l.f, 80, + "incorrect value for obs[12].l.f, expected 80, is {}", + msg.obs[12].l.f + ); + assert_eq!( + msg.obs[12].l.i, 114505343, + "incorrect value for obs[12].l.i, expected 114505343, is {}", + msg.obs[12].l.i + ); + assert_eq!( + msg.obs[12].p, 1069903034, + "incorrect value for obs[12].p, expected 1069903034, is {}", + msg.obs[12].p + ); + assert_eq!( + msg.obs[12].cn0, 200, + "incorrect value for obs[12].cn0, expected 200, is {}", + msg.obs[12].cn0 + ); + assert_eq!( + msg.obs[12].flags, 15, + "incorrect value for obs[12].flags, expected 15, is {}", + msg.obs[12].flags + ); + assert_eq!( + msg.obs[12].lock, 10, + "incorrect value for obs[12].lock, expected 10, is {}", + msg.obs[12].lock + ); + assert_eq!( + msg.obs[12].sid.code, 3, + "incorrect value for obs[12].sid.code, expected 3, is {}", + msg.obs[12].sid.code + ); + assert_eq!( + msg.obs[12].sid.sat, 17, + "incorrect value for obs[12].sid.sat, expected 17, is {}", + msg.obs[12].sid.sat + ); + assert_eq!( + msg.obs[13].d.f, 182, + "incorrect value for obs[13].d.f, expected 182, is {}", + msg.obs[13].d.f + ); + assert_eq!( + msg.obs[13].d.i, -461, + "incorrect value for obs[13].d.i, expected -461, is {}", + msg.obs[13].d.i + ); + assert_eq!( + msg.obs[13].l.f, 105, + "incorrect value for obs[13].l.f, expected 105, is {}", + msg.obs[13].l.f + ); + assert_eq!( + msg.obs[13].l.i, 102157331, + "incorrect value for obs[13].l.i, expected 102157331, is {}", + msg.obs[13].l.i + ); + assert_eq!( + msg.obs[13].p, 956875687, + "incorrect value for obs[13].p, expected 956875687, is {}", + msg.obs[13].p + ); + assert_eq!( + msg.obs[13].cn0, 152, + "incorrect value for obs[13].cn0, expected 152, is {}", + msg.obs[13].cn0 + ); + assert_eq!( + msg.obs[13].flags, 15, + "incorrect value for obs[13].flags, expected 15, is {}", + msg.obs[13].flags + ); + assert_eq!( + msg.obs[13].lock, 10, + "incorrect value for obs[13].lock, expected 10, is {}", + msg.obs[13].lock + ); + assert_eq!( + msg.obs[13].sid.code, 3, + "incorrect value for obs[13].sid.code, expected 3, is {}", + msg.obs[13].sid.code + ); + assert_eq!( + msg.obs[13].sid.sat, 18, + "incorrect value for obs[13].sid.sat, expected 18, is {}", + msg.obs[13].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObs"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 74, 0, 129, 240, 11, 152, 202, 226, 25, 0, 0, 0, 0, 106, 8, 16, 201, 101, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObs( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObs(msg) => { + assert_eq!( + msg.message_type(), + 0x4a, + "Incorrect message type, expected 0x4a, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xf081, + "incorrect sender id, expected 0xf081, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 16, + "incorrect value for header.n_obs, expected 16, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.ns_residual, 0, + "incorrect value for header.t.ns_residual, expected 0, is {}", + msg.header.t.ns_residual + ); + assert_eq!( + msg.header.t.tow, 434293400, + "incorrect value for header.t.tow, expected 434293400, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 2154, + "incorrect value for header.t.wn, expected 2154, is {}", + msg.header.t.wn + ); + } + _ => panic!("Invalid message type! Expected a MsgObs"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_a.rs index bea6f6c31d..3b853b587a 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_obs_dep_a() { { @@ -1099,6 +1104,13 @@ fn test_auto_check_sbp_observation_msg_obs_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { @@ -1106,14 +1118,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 69, "header": {"n_obs": 32, "t": {"wn": 1838, "tow": 407084600}}, "obs": [{"P": 2046421816, "prn": 0, "L": {"i": -36108, "f": 33}, "cn0": 46, "lock": 55875}, {"P": 2085014510, "prn": 2, "L": {"i": 203030, "f": 98}, "cn0": 43, "lock": 40376}, {"P": 2110096816, "prn": 3, "L": {"i": -178306, "f": 185}, "cn0": 39, "lock": 14148}, {"P": 2208476476, "prn": 10, "L": {"i": -137374, "f": 139}, "cn0": 30, "lock": 4129}, {"P": 2298000000, "prn": 13, "L": {"i": -167638, "f": 40}, "cn0": 20, "lock": 18218}, {"P": 2266101494, "prn": 22, "L": {"i": 209919, "f": 64}, "cn0": 27, "lock": 63852}, {"P": 1987193298, "prn": 30, "L": {"i": -53117, "f": 31}, "cn0": 52, "lock": 15074}], "crc": 55575, "length": 98, "preamble": 85, "payload": "OJ5DGC4HIDjr+Xn0cv//IS5D2gDuy0Z8FhkDAGIruJ0CsIXFfX5H/f+5J0Q3AzytooNi5/3/ix4hEAqAsviIKnH9/ygUKkcN9vYRh/8zAwBAG2z5FtIpcnaDMP//HzTiOh4="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1370,14 +1382,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 69, "header": {"n_obs": 33, "t": {"wn": 1838, "tow": 407084600}}, "obs": [{"P": 1973695572, "prn": 31, "L": {"i": 8294, "f": 147}, "cn0": 62, "lock": 64062}], "crc": 3818, "length": 20, "preamble": 85, "payload": "OJ5DGC4HIVQ0pHVmIAAAkz4++h8="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1454,14 +1466,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 69, "header": {"n_obs": 32, "t": {"wn": 1838, "tow": 407084800}}, "obs": [{"P": 2046415136, "prn": 0, "L": {"i": -36207, "f": 141}, "cn0": 45, "lock": 55875}, {"P": 2084995249, "prn": 2, "L": {"i": 203599, "f": 159}, "cn0": 44, "lock": 40376}, {"P": 2110097211, "prn": 3, "L": {"i": -178769, "f": 77}, "cn0": 40, "lock": 14148}, {"P": 2208476371, "prn": 10, "L": {"i": -137807, "f": 20}, "cn0": 31, "lock": 4129}, {"P": 2298000000, "prn": 13, "L": {"i": -168076, "f": 94}, "cn0": 21, "lock": 18218}, {"P": 2266082742, "prn": 22, "L": {"i": 210469, "f": 214}, "cn0": 27, "lock": 63852}, {"P": 1987187803, "prn": 30, "L": {"i": -53264, "f": 129}, "cn0": 52, "lock": 15074}], "crc": 30664, "length": 98, "preamble": 85, "payload": "AJ9DGC4HICDR+XmRcv//jS1D2gCxgEZ8TxsDAJ8suJ0CO4fFfa9F/f9NKEQ3A9OsooOx5f3/FB8hEAqAsviIdG/9/14VKkcNtq0RhyU2AwDWG2z5FlsUcnbwL///gTTiOh4="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1718,14 +1730,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 69, "header": {"n_obs": 33, "t": {"wn": 1838, "tow": 407084800}}, "obs": [{"P": 1973687089, "prn": 31, "L": {"i": 8312, "f": 222}, "cn0": 63, "lock": 64062}], "crc": 59147, "length": 20, "preamble": 85, "payload": "AJ9DGC4HITETpHV4IAAA3j8++h8="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1802,14 +1814,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 69, "header": {"n_obs": 16, "t": {"wn": 1838, "tow": 407151200}}, "obs": [{"P": 2044298327, "prn": 0, "L": {"i": -27527, "f": 189}, "cn0": 43, "lock": 37807}, {"P": 2110275716, "prn": 3, "L": {"i": -123030, "f": 1}, "cn0": 41, "lock": 45326}, {"P": 2298000000, "prn": 13, "L": {"i": -113594, "f": 166}, "cn0": 18, "lock": 34232}, {"P": 2259844888, "prn": 22, "L": {"i": 137478, "f": 249}, "cn0": 28, "lock": 24609}, {"P": 1985374378, "prn": 30, "L": {"i": -36797, "f": 203}, "cn0": 56, "lock": 22736}], "crc": 27435, "length": 72, "preamble": 85, "payload": "YKJEGC4HEFeE2Xl5lP//vSuvkwCEQMh9ah/+/wEpDrEDgLL4iEZE/v+mEriFDRh/soYGGQIA+RwhYBaqaFZ2Q3D//8s40Fge"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2006,14 +2018,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 69, "header": {"n_obs": 16, "t": {"wn": 1838, "tow": 407151400}}, "obs": [{"P": 2044291972, "prn": 0, "L": {"i": -27634, "f": 1}, "cn0": 44, "lock": 37807}, {"P": 2110276225, "prn": 3, "L": {"i": -123500, "f": 153}, "cn0": 41, "lock": 45326}, {"P": 2298000000, "prn": 13, "L": {"i": -114033, "f": 222}, "cn0": 18, "lock": 34232}, {"P": 2259826078, "prn": 22, "L": {"i": 138026, "f": 237}, "cn0": 30, "lock": 24609}, {"P": 1985368870, "prn": 30, "L": {"i": -36952, "f": 45}, "cn0": 56, "lock": 22736}], "crc": 44801, "length": 72, "preamble": 85, "payload": "KKNEGC4HEIRr2XkOlP//ASyvkwCBQsh9lB3+/5kpDrEDgLL4iI9C/v/eEriFDZ41soYqGwIA7R4hYBYmU1Z2qG///y040Fge"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2207,3 +2219,1212 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_obs_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_obs_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 69, 0, 195, 4, 98, 56, 158, 67, 24, 46, 7, 32, 56, 235, 249, 121, 244, 114, 255, + 255, 33, 46, 67, 218, 0, 238, 203, 70, 124, 22, 25, 3, 0, 98, 43, 184, 157, 2, 176, + 133, 197, 125, 126, 71, 253, 255, 185, 39, 68, 55, 3, 60, 173, 162, 131, 98, 231, 253, + 255, 139, 30, 33, 16, 10, 128, 178, 248, 136, 42, 113, 253, 255, 40, 20, 42, 71, 13, + 246, 246, 17, 135, 255, 51, 3, 0, 64, 27, 108, 249, 22, 210, 41, 114, 118, 131, 48, + 255, 255, 31, 52, 226, 58, 30, 23, 217, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x45, + "Incorrect message type, expected 0x45, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 407084600, + "incorrect value for header.t.tow, expected 407084600, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1838, + "incorrect value for header.t.wn, expected 1838, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 33, + "incorrect value for obs[0].l.f, expected 33, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -36108, + "incorrect value for obs[0].l.i, expected -36108, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2046421816, + "incorrect value for obs[0].p, expected 2046421816, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 46, + "incorrect value for obs[0].cn0, expected 46, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 55875, + "incorrect value for obs[0].lock, expected 55875, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].prn, 0, + "incorrect value for obs[0].prn, expected 0, is {}", + msg.obs[0].prn + ); + assert_eq!( + msg.obs[1].l.f, 98, + "incorrect value for obs[1].l.f, expected 98, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 203030, + "incorrect value for obs[1].l.i, expected 203030, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2085014510, + "incorrect value for obs[1].p, expected 2085014510, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 43, + "incorrect value for obs[1].cn0, expected 43, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 40376, + "incorrect value for obs[1].lock, expected 40376, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].prn, 2, + "incorrect value for obs[1].prn, expected 2, is {}", + msg.obs[1].prn + ); + assert_eq!( + msg.obs[2].l.f, 185, + "incorrect value for obs[2].l.f, expected 185, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -178306, + "incorrect value for obs[2].l.i, expected -178306, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2110096816, + "incorrect value for obs[2].p, expected 2110096816, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 39, + "incorrect value for obs[2].cn0, expected 39, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 14148, + "incorrect value for obs[2].lock, expected 14148, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].prn, 3, + "incorrect value for obs[2].prn, expected 3, is {}", + msg.obs[2].prn + ); + assert_eq!( + msg.obs[3].l.f, 139, + "incorrect value for obs[3].l.f, expected 139, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, -137374, + "incorrect value for obs[3].l.i, expected -137374, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2208476476, + "incorrect value for obs[3].p, expected 2208476476, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 30, + "incorrect value for obs[3].cn0, expected 30, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 4129, + "incorrect value for obs[3].lock, expected 4129, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].prn, 10, + "incorrect value for obs[3].prn, expected 10, is {}", + msg.obs[3].prn + ); + assert_eq!( + msg.obs[4].l.f, 40, + "incorrect value for obs[4].l.f, expected 40, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, -167638, + "incorrect value for obs[4].l.i, expected -167638, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 2298000000, + "incorrect value for obs[4].p, expected 2298000000, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 20, + "incorrect value for obs[4].cn0, expected 20, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 18218, + "incorrect value for obs[4].lock, expected 18218, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].prn, 13, + "incorrect value for obs[4].prn, expected 13, is {}", + msg.obs[4].prn + ); + assert_eq!( + msg.obs[5].l.f, 64, + "incorrect value for obs[5].l.f, expected 64, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 209919, + "incorrect value for obs[5].l.i, expected 209919, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 2266101494, + "incorrect value for obs[5].p, expected 2266101494, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].cn0, 27, + "incorrect value for obs[5].cn0, expected 27, is {}", + msg.obs[5].cn0 + ); + assert_eq!( + msg.obs[5].lock, 63852, + "incorrect value for obs[5].lock, expected 63852, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].prn, 22, + "incorrect value for obs[5].prn, expected 22, is {}", + msg.obs[5].prn + ); + assert_eq!( + msg.obs[6].l.f, 31, + "incorrect value for obs[6].l.f, expected 31, is {}", + msg.obs[6].l.f + ); + assert_eq!( + msg.obs[6].l.i, -53117, + "incorrect value for obs[6].l.i, expected -53117, is {}", + msg.obs[6].l.i + ); + assert_eq!( + msg.obs[6].p, 1987193298, + "incorrect value for obs[6].p, expected 1987193298, is {}", + msg.obs[6].p + ); + assert_eq!( + msg.obs[6].cn0, 52, + "incorrect value for obs[6].cn0, expected 52, is {}", + msg.obs[6].cn0 + ); + assert_eq!( + msg.obs[6].lock, 15074, + "incorrect value for obs[6].lock, expected 15074, is {}", + msg.obs[6].lock + ); + assert_eq!( + msg.obs[6].prn, 30, + "incorrect value for obs[6].prn, expected 30, is {}", + msg.obs[6].prn + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 69, 0, 195, 4, 20, 56, 158, 67, 24, 46, 7, 33, 84, 52, 164, 117, 102, 32, 0, 0, + 147, 62, 62, 250, 31, 234, 14, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x45, + "Incorrect message type, expected 0x45, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 33, + "incorrect value for header.n_obs, expected 33, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 407084600, + "incorrect value for header.t.tow, expected 407084600, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1838, + "incorrect value for header.t.wn, expected 1838, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 147, + "incorrect value for obs[0].l.f, expected 147, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 8294, + "incorrect value for obs[0].l.i, expected 8294, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1973695572, + "incorrect value for obs[0].p, expected 1973695572, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 62, + "incorrect value for obs[0].cn0, expected 62, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 64062, + "incorrect value for obs[0].lock, expected 64062, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].prn, 31, + "incorrect value for obs[0].prn, expected 31, is {}", + msg.obs[0].prn + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 69, 0, 195, 4, 98, 0, 159, 67, 24, 46, 7, 32, 32, 209, 249, 121, 145, 114, 255, + 255, 141, 45, 67, 218, 0, 177, 128, 70, 124, 79, 27, 3, 0, 159, 44, 184, 157, 2, 59, + 135, 197, 125, 175, 69, 253, 255, 77, 40, 68, 55, 3, 211, 172, 162, 131, 177, 229, 253, + 255, 20, 31, 33, 16, 10, 128, 178, 248, 136, 116, 111, 253, 255, 94, 21, 42, 71, 13, + 182, 173, 17, 135, 37, 54, 3, 0, 214, 27, 108, 249, 22, 91, 20, 114, 118, 240, 47, 255, + 255, 129, 52, 226, 58, 30, 200, 119, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x45, + "Incorrect message type, expected 0x45, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 407084800, + "incorrect value for header.t.tow, expected 407084800, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1838, + "incorrect value for header.t.wn, expected 1838, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 141, + "incorrect value for obs[0].l.f, expected 141, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -36207, + "incorrect value for obs[0].l.i, expected -36207, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2046415136, + "incorrect value for obs[0].p, expected 2046415136, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 45, + "incorrect value for obs[0].cn0, expected 45, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 55875, + "incorrect value for obs[0].lock, expected 55875, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].prn, 0, + "incorrect value for obs[0].prn, expected 0, is {}", + msg.obs[0].prn + ); + assert_eq!( + msg.obs[1].l.f, 159, + "incorrect value for obs[1].l.f, expected 159, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 203599, + "incorrect value for obs[1].l.i, expected 203599, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2084995249, + "incorrect value for obs[1].p, expected 2084995249, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 44, + "incorrect value for obs[1].cn0, expected 44, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 40376, + "incorrect value for obs[1].lock, expected 40376, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].prn, 2, + "incorrect value for obs[1].prn, expected 2, is {}", + msg.obs[1].prn + ); + assert_eq!( + msg.obs[2].l.f, 77, + "incorrect value for obs[2].l.f, expected 77, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -178769, + "incorrect value for obs[2].l.i, expected -178769, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2110097211, + "incorrect value for obs[2].p, expected 2110097211, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 40, + "incorrect value for obs[2].cn0, expected 40, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 14148, + "incorrect value for obs[2].lock, expected 14148, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].prn, 3, + "incorrect value for obs[2].prn, expected 3, is {}", + msg.obs[2].prn + ); + assert_eq!( + msg.obs[3].l.f, 20, + "incorrect value for obs[3].l.f, expected 20, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, -137807, + "incorrect value for obs[3].l.i, expected -137807, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2208476371, + "incorrect value for obs[3].p, expected 2208476371, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 31, + "incorrect value for obs[3].cn0, expected 31, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 4129, + "incorrect value for obs[3].lock, expected 4129, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].prn, 10, + "incorrect value for obs[3].prn, expected 10, is {}", + msg.obs[3].prn + ); + assert_eq!( + msg.obs[4].l.f, 94, + "incorrect value for obs[4].l.f, expected 94, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, -168076, + "incorrect value for obs[4].l.i, expected -168076, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 2298000000, + "incorrect value for obs[4].p, expected 2298000000, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 21, + "incorrect value for obs[4].cn0, expected 21, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 18218, + "incorrect value for obs[4].lock, expected 18218, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].prn, 13, + "incorrect value for obs[4].prn, expected 13, is {}", + msg.obs[4].prn + ); + assert_eq!( + msg.obs[5].l.f, 214, + "incorrect value for obs[5].l.f, expected 214, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 210469, + "incorrect value for obs[5].l.i, expected 210469, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 2266082742, + "incorrect value for obs[5].p, expected 2266082742, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].cn0, 27, + "incorrect value for obs[5].cn0, expected 27, is {}", + msg.obs[5].cn0 + ); + assert_eq!( + msg.obs[5].lock, 63852, + "incorrect value for obs[5].lock, expected 63852, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].prn, 22, + "incorrect value for obs[5].prn, expected 22, is {}", + msg.obs[5].prn + ); + assert_eq!( + msg.obs[6].l.f, 129, + "incorrect value for obs[6].l.f, expected 129, is {}", + msg.obs[6].l.f + ); + assert_eq!( + msg.obs[6].l.i, -53264, + "incorrect value for obs[6].l.i, expected -53264, is {}", + msg.obs[6].l.i + ); + assert_eq!( + msg.obs[6].p, 1987187803, + "incorrect value for obs[6].p, expected 1987187803, is {}", + msg.obs[6].p + ); + assert_eq!( + msg.obs[6].cn0, 52, + "incorrect value for obs[6].cn0, expected 52, is {}", + msg.obs[6].cn0 + ); + assert_eq!( + msg.obs[6].lock, 15074, + "incorrect value for obs[6].lock, expected 15074, is {}", + msg.obs[6].lock + ); + assert_eq!( + msg.obs[6].prn, 30, + "incorrect value for obs[6].prn, expected 30, is {}", + msg.obs[6].prn + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 69, 0, 195, 4, 20, 0, 159, 67, 24, 46, 7, 33, 49, 19, 164, 117, 120, 32, 0, 0, 222, + 63, 62, 250, 31, 11, 231, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x45, + "Incorrect message type, expected 0x45, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 33, + "incorrect value for header.n_obs, expected 33, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 407084800, + "incorrect value for header.t.tow, expected 407084800, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1838, + "incorrect value for header.t.wn, expected 1838, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 222, + "incorrect value for obs[0].l.f, expected 222, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 8312, + "incorrect value for obs[0].l.i, expected 8312, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1973687089, + "incorrect value for obs[0].p, expected 1973687089, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 63, + "incorrect value for obs[0].cn0, expected 63, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 64062, + "incorrect value for obs[0].lock, expected 64062, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].prn, 31, + "incorrect value for obs[0].prn, expected 31, is {}", + msg.obs[0].prn + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 69, 0, 195, 4, 72, 96, 162, 68, 24, 46, 7, 16, 87, 132, 217, 121, 121, 148, 255, + 255, 189, 43, 175, 147, 0, 132, 64, 200, 125, 106, 31, 254, 255, 1, 41, 14, 177, 3, + 128, 178, 248, 136, 70, 68, 254, 255, 166, 18, 184, 133, 13, 24, 127, 178, 134, 6, 25, + 2, 0, 249, 28, 33, 96, 22, 170, 104, 86, 118, 67, 112, 255, 255, 203, 56, 208, 88, 30, + 43, 107, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x45, + "Incorrect message type, expected 0x45, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 16, + "incorrect value for header.n_obs, expected 16, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 407151200, + "incorrect value for header.t.tow, expected 407151200, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1838, + "incorrect value for header.t.wn, expected 1838, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 189, + "incorrect value for obs[0].l.f, expected 189, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -27527, + "incorrect value for obs[0].l.i, expected -27527, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2044298327, + "incorrect value for obs[0].p, expected 2044298327, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 43, + "incorrect value for obs[0].cn0, expected 43, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 37807, + "incorrect value for obs[0].lock, expected 37807, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].prn, 0, + "incorrect value for obs[0].prn, expected 0, is {}", + msg.obs[0].prn + ); + assert_eq!( + msg.obs[1].l.f, 1, + "incorrect value for obs[1].l.f, expected 1, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, -123030, + "incorrect value for obs[1].l.i, expected -123030, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2110275716, + "incorrect value for obs[1].p, expected 2110275716, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 41, + "incorrect value for obs[1].cn0, expected 41, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 45326, + "incorrect value for obs[1].lock, expected 45326, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].prn, 3, + "incorrect value for obs[1].prn, expected 3, is {}", + msg.obs[1].prn + ); + assert_eq!( + msg.obs[2].l.f, 166, + "incorrect value for obs[2].l.f, expected 166, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -113594, + "incorrect value for obs[2].l.i, expected -113594, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2298000000, + "incorrect value for obs[2].p, expected 2298000000, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 18, + "incorrect value for obs[2].cn0, expected 18, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 34232, + "incorrect value for obs[2].lock, expected 34232, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].prn, 13, + "incorrect value for obs[2].prn, expected 13, is {}", + msg.obs[2].prn + ); + assert_eq!( + msg.obs[3].l.f, 249, + "incorrect value for obs[3].l.f, expected 249, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 137478, + "incorrect value for obs[3].l.i, expected 137478, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2259844888, + "incorrect value for obs[3].p, expected 2259844888, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 28, + "incorrect value for obs[3].cn0, expected 28, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 24609, + "incorrect value for obs[3].lock, expected 24609, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].prn, 22, + "incorrect value for obs[3].prn, expected 22, is {}", + msg.obs[3].prn + ); + assert_eq!( + msg.obs[4].l.f, 203, + "incorrect value for obs[4].l.f, expected 203, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, -36797, + "incorrect value for obs[4].l.i, expected -36797, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1985374378, + "incorrect value for obs[4].p, expected 1985374378, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 56, + "incorrect value for obs[4].cn0, expected 56, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 22736, + "incorrect value for obs[4].lock, expected 22736, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].prn, 30, + "incorrect value for obs[4].prn, expected 30, is {}", + msg.obs[4].prn + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 69, 0, 195, 4, 72, 40, 163, 68, 24, 46, 7, 16, 132, 107, 217, 121, 14, 148, 255, + 255, 1, 44, 175, 147, 0, 129, 66, 200, 125, 148, 29, 254, 255, 153, 41, 14, 177, 3, + 128, 178, 248, 136, 143, 66, 254, 255, 222, 18, 184, 133, 13, 158, 53, 178, 134, 42, + 27, 2, 0, 237, 30, 33, 96, 22, 38, 83, 86, 118, 168, 111, 255, 255, 45, 56, 208, 88, + 30, 1, 175, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x45, + "Incorrect message type, expected 0x45, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 16, + "incorrect value for header.n_obs, expected 16, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 407151400, + "incorrect value for header.t.tow, expected 407151400, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1838, + "incorrect value for header.t.wn, expected 1838, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 1, + "incorrect value for obs[0].l.f, expected 1, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -27634, + "incorrect value for obs[0].l.i, expected -27634, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2044291972, + "incorrect value for obs[0].p, expected 2044291972, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 44, + "incorrect value for obs[0].cn0, expected 44, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 37807, + "incorrect value for obs[0].lock, expected 37807, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].prn, 0, + "incorrect value for obs[0].prn, expected 0, is {}", + msg.obs[0].prn + ); + assert_eq!( + msg.obs[1].l.f, 153, + "incorrect value for obs[1].l.f, expected 153, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, -123500, + "incorrect value for obs[1].l.i, expected -123500, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2110276225, + "incorrect value for obs[1].p, expected 2110276225, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 41, + "incorrect value for obs[1].cn0, expected 41, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 45326, + "incorrect value for obs[1].lock, expected 45326, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].prn, 3, + "incorrect value for obs[1].prn, expected 3, is {}", + msg.obs[1].prn + ); + assert_eq!( + msg.obs[2].l.f, 222, + "incorrect value for obs[2].l.f, expected 222, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -114033, + "incorrect value for obs[2].l.i, expected -114033, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2298000000, + "incorrect value for obs[2].p, expected 2298000000, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 18, + "incorrect value for obs[2].cn0, expected 18, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 34232, + "incorrect value for obs[2].lock, expected 34232, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].prn, 13, + "incorrect value for obs[2].prn, expected 13, is {}", + msg.obs[2].prn + ); + assert_eq!( + msg.obs[3].l.f, 237, + "incorrect value for obs[3].l.f, expected 237, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 138026, + "incorrect value for obs[3].l.i, expected 138026, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2259826078, + "incorrect value for obs[3].p, expected 2259826078, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 30, + "incorrect value for obs[3].cn0, expected 30, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 24609, + "incorrect value for obs[3].lock, expected 24609, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].prn, 22, + "incorrect value for obs[3].prn, expected 22, is {}", + msg.obs[3].prn + ); + assert_eq!( + msg.obs[4].l.f, 45, + "incorrect value for obs[4].l.f, expected 45, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, -36952, + "incorrect value for obs[4].l.i, expected -36952, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1985368870, + "incorrect value for obs[4].p, expected 1985368870, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 56, + "incorrect value for obs[4].cn0, expected 56, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 22736, + "incorrect value for obs[4].lock, expected 22736, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].prn, 30, + "incorrect value for obs[4].prn, expected 30, is {}", + msg.obs[4].prn + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_b.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_b.rs index d26f2c2b65..ca6cc610e9 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_b.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_b.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_obs_dep_b() { { @@ -1231,6 +1236,13 @@ fn test_auto_check_sbp_observation_msg_obs_dep_b() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { @@ -1238,14 +1250,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 67, "header": {"n_obs": 32, "t": {"wn": 1787, "tow": 2567800}}, "obs": [{"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 202}, "L": {"i": 117913055, "f": 27}, "cn0": 157, "P": 2243669940}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 203}, "L": {"i": 129899608, "f": 175}, "cn0": 144, "P": 2471857210}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 208}, "L": {"i": 122531024, "f": 135}, "cn0": 151, "P": 2331544796}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 212}, "L": {"i": 119280243, "f": 242}, "cn0": 156, "P": 2269692589}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 217}, "L": {"i": 109691922, "f": 120}, "cn0": 168, "P": 2087293092}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 218}, "L": {"i": 123340754, "f": 87}, "cn0": 150, "P": 2347034654}], "crc": 21929, "length": 103, "preamble": 85, "payload": "eC4nAPsGILSvu4XfNQcHG50AAMoAAAA6jFWTWBy+B6+QAADLAAAA3Iz4itCsTQeHlwAA0AAAAK3CSIdzEhwH8pwAANQAAACkkGl8EsSJBnioAADZAAAAHujki9IHWgdXlgAA2gAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1532,14 +1544,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 67, "header": {"n_obs": 33, "t": {"wn": 1787, "tow": 2567800}}, "obs": [{"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 220}, "L": {"i": 120256389, "f": 219}, "cn0": 154, "P": 2288371524}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 222}, "L": {"i": 117692256, "f": 235}, "cn0": 156, "P": 2239434459}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 225}, "L": {"i": 107851013, "f": 174}, "cn0": 170, "P": 2052171351}], "crc": 59659, "length": 55, "preamble": 85, "payload": "eC4nAPsGIUTHZYiF9yoH25oAANwAAADbDnuFYNcDB+ucAADeAAAAV6ZRegWtbQauqgAA4QAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1706,14 +1718,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 67, "header": {"n_obs": 32, "t": {"wn": 1787, "tow": 2568000}}, "obs": [{"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 202}, "L": {"i": 117912556, "f": 94}, "cn0": 156, "P": 2243658852}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 203}, "L": {"i": 129900210, "f": 40}, "cn0": 140, "P": 2471868513}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 208}, "L": {"i": 122530650, "f": 2}, "cn0": 150, "P": 2331537287}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 212}, "L": {"i": 119280830, "f": 241}, "cn0": 155, "P": 2269703860}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 217}, "L": {"i": 109691996, "f": 153}, "cn0": 168, "P": 2087295247}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 218}, "L": {"i": 123340176, "f": 41}, "cn0": 150, "P": 2347022641}], "crc": 25329, "length": 103, "preamble": 85, "payload": "QC8nAPsGIGSEu4XsMwcHXpwAAMoAAABhuFWTsh6+ByiMAADLAAAAh2/4ilqrTQcClgAA0AAAALTuSIe+FBwH8ZsAANQAAAAPmWl8XMSJBpmoAADZAAAAMbnki5AFWgcplgAA2gAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2000,14 +2012,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 67, "header": {"n_obs": 33, "t": {"wn": 1787, "tow": 2568000}}, "obs": [{"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 220}, "L": {"i": 120255759, "f": 20}, "cn0": 154, "P": 2288358634}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 222}, "L": {"i": 117691920, "f": 38}, "cn0": 156, "P": 2239428560}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 225}, "L": {"i": 107850774, "f": 7}, "cn0": 172, "P": 2052167183}], "crc": 3529, "length": 55, "preamble": 85, "payload": "QC8nAPsGIeqUZYgP9SoHFJoAANwAAADQ93qFENYDByacAADeAAAAD5ZRehasbQYHrAAA4QAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2174,14 +2186,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { let json_input = r#"{"sender": 55286, "msg_type": 67, "header": {"n_obs": 32, "t": {"wn": 1787, "tow": 2568200}}, "obs": [{"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 202}, "L": {"i": 117912057, "f": 165}, "cn0": 156, "P": 2243649790}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 203}, "L": {"i": 129900811, "f": 106}, "cn0": 143, "P": 2471880049}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 208}, "L": {"i": 122530275, "f": 159}, "cn0": 150, "P": 2331530678}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 212}, "L": {"i": 119281418, "f": 7}, "cn0": 156, "P": 2269714449}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 217}, "L": {"i": 109692070, "f": 186}, "cn0": 170, "P": 2087295852}, {"lock": 0, "sid": {"code": 0, "reserved": 0, "sat": 218}, "L": {"i": 123339597, "f": 236}, "cn0": 151, "P": 2347011798}], "crc": 30267, "length": 103, "preamble": 85, "payload": "CDAnAPsGIP5gu4X5MQcHpZwAAMoAAABx5VWTCyG+B2qPAADLAAAAtlX4iuOpTQeflgAA0AAAABEYSYcKFxwHB5wAANQAAABsm2l8psSJBrqqAADZAAAA1o7ki00DWgfslwAA2gAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2465,3 +2477,1325 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_b() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_obs_dep_b`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_obs_dep_b() { + { + let mut payload = Cursor::new(vec![ + 85, 67, 0, 246, 215, 103, 120, 46, 39, 0, 251, 6, 32, 180, 175, 187, 133, 223, 53, 7, + 7, 27, 157, 0, 0, 202, 0, 0, 0, 58, 140, 85, 147, 88, 28, 190, 7, 175, 144, 0, 0, 203, + 0, 0, 0, 220, 140, 248, 138, 208, 172, 77, 7, 135, 151, 0, 0, 208, 0, 0, 0, 173, 194, + 72, 135, 115, 18, 28, 7, 242, 156, 0, 0, 212, 0, 0, 0, 164, 144, 105, 124, 18, 196, + 137, 6, 120, 168, 0, 0, 217, 0, 0, 0, 30, 232, 228, 139, 210, 7, 90, 7, 87, 150, 0, 0, + 218, 0, 0, 0, 169, 85, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x43, + "Incorrect message type, expected 0x43, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 2567800, + "incorrect value for header.t.tow, expected 2567800, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1787, + "incorrect value for header.t.wn, expected 1787, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 27, + "incorrect value for obs[0].l.f, expected 27, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 117913055, + "incorrect value for obs[0].l.i, expected 117913055, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2243669940, + "incorrect value for obs[0].p, expected 2243669940, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 157, + "incorrect value for obs[0].cn0, expected 157, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 0, + "incorrect value for obs[0].lock, expected 0, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 202, + "incorrect value for obs[0].sid.sat, expected 202, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 175, + "incorrect value for obs[1].l.f, expected 175, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 129899608, + "incorrect value for obs[1].l.i, expected 129899608, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2471857210, + "incorrect value for obs[1].p, expected 2471857210, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 144, + "incorrect value for obs[1].cn0, expected 144, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 0, + "incorrect value for obs[1].lock, expected 0, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 203, + "incorrect value for obs[1].sid.sat, expected 203, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 135, + "incorrect value for obs[2].l.f, expected 135, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 122531024, + "incorrect value for obs[2].l.i, expected 122531024, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2331544796, + "incorrect value for obs[2].p, expected 2331544796, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 151, + "incorrect value for obs[2].cn0, expected 151, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 0, + "incorrect value for obs[2].lock, expected 0, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 208, + "incorrect value for obs[2].sid.sat, expected 208, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].l.f, 242, + "incorrect value for obs[3].l.f, expected 242, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 119280243, + "incorrect value for obs[3].l.i, expected 119280243, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2269692589, + "incorrect value for obs[3].p, expected 2269692589, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 156, + "incorrect value for obs[3].cn0, expected 156, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 0, + "incorrect value for obs[3].lock, expected 0, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.reserved, 0, + "incorrect value for obs[3].sid.reserved, expected 0, is {}", + msg.obs[3].sid.reserved + ); + assert_eq!( + msg.obs[3].sid.sat, 212, + "incorrect value for obs[3].sid.sat, expected 212, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].l.f, 120, + "incorrect value for obs[4].l.f, expected 120, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 109691922, + "incorrect value for obs[4].l.i, expected 109691922, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 2087293092, + "incorrect value for obs[4].p, expected 2087293092, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 168, + "incorrect value for obs[4].cn0, expected 168, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 0, + "incorrect value for obs[4].lock, expected 0, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.reserved, 0, + "incorrect value for obs[4].sid.reserved, expected 0, is {}", + msg.obs[4].sid.reserved + ); + assert_eq!( + msg.obs[4].sid.sat, 217, + "incorrect value for obs[4].sid.sat, expected 217, is {}", + msg.obs[4].sid.sat + ); + assert_eq!( + msg.obs[5].l.f, 87, + "incorrect value for obs[5].l.f, expected 87, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 123340754, + "incorrect value for obs[5].l.i, expected 123340754, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 2347034654, + "incorrect value for obs[5].p, expected 2347034654, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].cn0, 150, + "incorrect value for obs[5].cn0, expected 150, is {}", + msg.obs[5].cn0 + ); + assert_eq!( + msg.obs[5].lock, 0, + "incorrect value for obs[5].lock, expected 0, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].sid.code, 0, + "incorrect value for obs[5].sid.code, expected 0, is {}", + msg.obs[5].sid.code + ); + assert_eq!( + msg.obs[5].sid.reserved, 0, + "incorrect value for obs[5].sid.reserved, expected 0, is {}", + msg.obs[5].sid.reserved + ); + assert_eq!( + msg.obs[5].sid.sat, 218, + "incorrect value for obs[5].sid.sat, expected 218, is {}", + msg.obs[5].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 67, 0, 246, 215, 55, 120, 46, 39, 0, 251, 6, 33, 68, 199, 101, 136, 133, 247, 42, + 7, 219, 154, 0, 0, 220, 0, 0, 0, 219, 14, 123, 133, 96, 215, 3, 7, 235, 156, 0, 0, 222, + 0, 0, 0, 87, 166, 81, 122, 5, 173, 109, 6, 174, 170, 0, 0, 225, 0, 0, 0, 11, 233, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x43, + "Incorrect message type, expected 0x43, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 33, + "incorrect value for header.n_obs, expected 33, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 2567800, + "incorrect value for header.t.tow, expected 2567800, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1787, + "incorrect value for header.t.wn, expected 1787, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 219, + "incorrect value for obs[0].l.f, expected 219, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 120256389, + "incorrect value for obs[0].l.i, expected 120256389, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2288371524, + "incorrect value for obs[0].p, expected 2288371524, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 154, + "incorrect value for obs[0].cn0, expected 154, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 0, + "incorrect value for obs[0].lock, expected 0, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 220, + "incorrect value for obs[0].sid.sat, expected 220, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 235, + "incorrect value for obs[1].l.f, expected 235, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 117692256, + "incorrect value for obs[1].l.i, expected 117692256, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2239434459, + "incorrect value for obs[1].p, expected 2239434459, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 156, + "incorrect value for obs[1].cn0, expected 156, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 0, + "incorrect value for obs[1].lock, expected 0, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 222, + "incorrect value for obs[1].sid.sat, expected 222, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 174, + "incorrect value for obs[2].l.f, expected 174, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 107851013, + "incorrect value for obs[2].l.i, expected 107851013, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2052171351, + "incorrect value for obs[2].p, expected 2052171351, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 170, + "incorrect value for obs[2].cn0, expected 170, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 0, + "incorrect value for obs[2].lock, expected 0, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 225, + "incorrect value for obs[2].sid.sat, expected 225, is {}", + msg.obs[2].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 67, 0, 246, 215, 103, 64, 47, 39, 0, 251, 6, 32, 100, 132, 187, 133, 236, 51, 7, 7, + 94, 156, 0, 0, 202, 0, 0, 0, 97, 184, 85, 147, 178, 30, 190, 7, 40, 140, 0, 0, 203, 0, + 0, 0, 135, 111, 248, 138, 90, 171, 77, 7, 2, 150, 0, 0, 208, 0, 0, 0, 180, 238, 72, + 135, 190, 20, 28, 7, 241, 155, 0, 0, 212, 0, 0, 0, 15, 153, 105, 124, 92, 196, 137, 6, + 153, 168, 0, 0, 217, 0, 0, 0, 49, 185, 228, 139, 144, 5, 90, 7, 41, 150, 0, 0, 218, 0, + 0, 0, 241, 98, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x43, + "Incorrect message type, expected 0x43, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 2568000, + "incorrect value for header.t.tow, expected 2568000, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1787, + "incorrect value for header.t.wn, expected 1787, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 94, + "incorrect value for obs[0].l.f, expected 94, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 117912556, + "incorrect value for obs[0].l.i, expected 117912556, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2243658852, + "incorrect value for obs[0].p, expected 2243658852, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 156, + "incorrect value for obs[0].cn0, expected 156, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 0, + "incorrect value for obs[0].lock, expected 0, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 202, + "incorrect value for obs[0].sid.sat, expected 202, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 40, + "incorrect value for obs[1].l.f, expected 40, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 129900210, + "incorrect value for obs[1].l.i, expected 129900210, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2471868513, + "incorrect value for obs[1].p, expected 2471868513, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 140, + "incorrect value for obs[1].cn0, expected 140, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 0, + "incorrect value for obs[1].lock, expected 0, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 203, + "incorrect value for obs[1].sid.sat, expected 203, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 2, + "incorrect value for obs[2].l.f, expected 2, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 122530650, + "incorrect value for obs[2].l.i, expected 122530650, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2331537287, + "incorrect value for obs[2].p, expected 2331537287, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 150, + "incorrect value for obs[2].cn0, expected 150, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 0, + "incorrect value for obs[2].lock, expected 0, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 208, + "incorrect value for obs[2].sid.sat, expected 208, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].l.f, 241, + "incorrect value for obs[3].l.f, expected 241, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 119280830, + "incorrect value for obs[3].l.i, expected 119280830, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2269703860, + "incorrect value for obs[3].p, expected 2269703860, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 155, + "incorrect value for obs[3].cn0, expected 155, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 0, + "incorrect value for obs[3].lock, expected 0, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.reserved, 0, + "incorrect value for obs[3].sid.reserved, expected 0, is {}", + msg.obs[3].sid.reserved + ); + assert_eq!( + msg.obs[3].sid.sat, 212, + "incorrect value for obs[3].sid.sat, expected 212, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].l.f, 153, + "incorrect value for obs[4].l.f, expected 153, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 109691996, + "incorrect value for obs[4].l.i, expected 109691996, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 2087295247, + "incorrect value for obs[4].p, expected 2087295247, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 168, + "incorrect value for obs[4].cn0, expected 168, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 0, + "incorrect value for obs[4].lock, expected 0, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.reserved, 0, + "incorrect value for obs[4].sid.reserved, expected 0, is {}", + msg.obs[4].sid.reserved + ); + assert_eq!( + msg.obs[4].sid.sat, 217, + "incorrect value for obs[4].sid.sat, expected 217, is {}", + msg.obs[4].sid.sat + ); + assert_eq!( + msg.obs[5].l.f, 41, + "incorrect value for obs[5].l.f, expected 41, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 123340176, + "incorrect value for obs[5].l.i, expected 123340176, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 2347022641, + "incorrect value for obs[5].p, expected 2347022641, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].cn0, 150, + "incorrect value for obs[5].cn0, expected 150, is {}", + msg.obs[5].cn0 + ); + assert_eq!( + msg.obs[5].lock, 0, + "incorrect value for obs[5].lock, expected 0, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].sid.code, 0, + "incorrect value for obs[5].sid.code, expected 0, is {}", + msg.obs[5].sid.code + ); + assert_eq!( + msg.obs[5].sid.reserved, 0, + "incorrect value for obs[5].sid.reserved, expected 0, is {}", + msg.obs[5].sid.reserved + ); + assert_eq!( + msg.obs[5].sid.sat, 218, + "incorrect value for obs[5].sid.sat, expected 218, is {}", + msg.obs[5].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 67, 0, 246, 215, 55, 64, 47, 39, 0, 251, 6, 33, 234, 148, 101, 136, 15, 245, 42, 7, + 20, 154, 0, 0, 220, 0, 0, 0, 208, 247, 122, 133, 16, 214, 3, 7, 38, 156, 0, 0, 222, 0, + 0, 0, 15, 150, 81, 122, 22, 172, 109, 6, 7, 172, 0, 0, 225, 0, 0, 0, 201, 13, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x43, + "Incorrect message type, expected 0x43, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 33, + "incorrect value for header.n_obs, expected 33, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 2568000, + "incorrect value for header.t.tow, expected 2568000, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1787, + "incorrect value for header.t.wn, expected 1787, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 20, + "incorrect value for obs[0].l.f, expected 20, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 120255759, + "incorrect value for obs[0].l.i, expected 120255759, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2288358634, + "incorrect value for obs[0].p, expected 2288358634, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 154, + "incorrect value for obs[0].cn0, expected 154, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 0, + "incorrect value for obs[0].lock, expected 0, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 220, + "incorrect value for obs[0].sid.sat, expected 220, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 38, + "incorrect value for obs[1].l.f, expected 38, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 117691920, + "incorrect value for obs[1].l.i, expected 117691920, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2239428560, + "incorrect value for obs[1].p, expected 2239428560, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 156, + "incorrect value for obs[1].cn0, expected 156, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 0, + "incorrect value for obs[1].lock, expected 0, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 222, + "incorrect value for obs[1].sid.sat, expected 222, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 7, + "incorrect value for obs[2].l.f, expected 7, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 107850774, + "incorrect value for obs[2].l.i, expected 107850774, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2052167183, + "incorrect value for obs[2].p, expected 2052167183, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 172, + "incorrect value for obs[2].cn0, expected 172, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 0, + "incorrect value for obs[2].lock, expected 0, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 225, + "incorrect value for obs[2].sid.sat, expected 225, is {}", + msg.obs[2].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 67, 0, 246, 215, 103, 8, 48, 39, 0, 251, 6, 32, 254, 96, 187, 133, 249, 49, 7, 7, + 165, 156, 0, 0, 202, 0, 0, 0, 113, 229, 85, 147, 11, 33, 190, 7, 106, 143, 0, 0, 203, + 0, 0, 0, 182, 85, 248, 138, 227, 169, 77, 7, 159, 150, 0, 0, 208, 0, 0, 0, 17, 24, 73, + 135, 10, 23, 28, 7, 7, 156, 0, 0, 212, 0, 0, 0, 108, 155, 105, 124, 166, 196, 137, 6, + 186, 170, 0, 0, 217, 0, 0, 0, 214, 142, 228, 139, 77, 3, 90, 7, 236, 151, 0, 0, 218, 0, + 0, 0, 59, 118, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x43, + "Incorrect message type, expected 0x43, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 2568200, + "incorrect value for header.t.tow, expected 2568200, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1787, + "incorrect value for header.t.wn, expected 1787, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 165, + "incorrect value for obs[0].l.f, expected 165, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 117912057, + "incorrect value for obs[0].l.i, expected 117912057, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 2243649790, + "incorrect value for obs[0].p, expected 2243649790, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 156, + "incorrect value for obs[0].cn0, expected 156, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 0, + "incorrect value for obs[0].lock, expected 0, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 202, + "incorrect value for obs[0].sid.sat, expected 202, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 106, + "incorrect value for obs[1].l.f, expected 106, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 129900811, + "incorrect value for obs[1].l.i, expected 129900811, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 2471880049, + "incorrect value for obs[1].p, expected 2471880049, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 143, + "incorrect value for obs[1].cn0, expected 143, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 0, + "incorrect value for obs[1].lock, expected 0, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 203, + "incorrect value for obs[1].sid.sat, expected 203, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 159, + "incorrect value for obs[2].l.f, expected 159, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 122530275, + "incorrect value for obs[2].l.i, expected 122530275, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 2331530678, + "incorrect value for obs[2].p, expected 2331530678, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 150, + "incorrect value for obs[2].cn0, expected 150, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 0, + "incorrect value for obs[2].lock, expected 0, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 208, + "incorrect value for obs[2].sid.sat, expected 208, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].l.f, 7, + "incorrect value for obs[3].l.f, expected 7, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 119281418, + "incorrect value for obs[3].l.i, expected 119281418, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 2269714449, + "incorrect value for obs[3].p, expected 2269714449, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 156, + "incorrect value for obs[3].cn0, expected 156, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 0, + "incorrect value for obs[3].lock, expected 0, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.reserved, 0, + "incorrect value for obs[3].sid.reserved, expected 0, is {}", + msg.obs[3].sid.reserved + ); + assert_eq!( + msg.obs[3].sid.sat, 212, + "incorrect value for obs[3].sid.sat, expected 212, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].l.f, 186, + "incorrect value for obs[4].l.f, expected 186, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 109692070, + "incorrect value for obs[4].l.i, expected 109692070, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 2087295852, + "incorrect value for obs[4].p, expected 2087295852, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 170, + "incorrect value for obs[4].cn0, expected 170, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 0, + "incorrect value for obs[4].lock, expected 0, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.reserved, 0, + "incorrect value for obs[4].sid.reserved, expected 0, is {}", + msg.obs[4].sid.reserved + ); + assert_eq!( + msg.obs[4].sid.sat, 217, + "incorrect value for obs[4].sid.sat, expected 217, is {}", + msg.obs[4].sid.sat + ); + assert_eq!( + msg.obs[5].l.f, 236, + "incorrect value for obs[5].l.f, expected 236, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 123339597, + "incorrect value for obs[5].l.i, expected 123339597, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 2347011798, + "incorrect value for obs[5].p, expected 2347011798, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].cn0, 151, + "incorrect value for obs[5].cn0, expected 151, is {}", + msg.obs[5].cn0 + ); + assert_eq!( + msg.obs[5].lock, 0, + "incorrect value for obs[5].lock, expected 0, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].sid.code, 0, + "incorrect value for obs[5].sid.code, expected 0, is {}", + msg.obs[5].sid.code + ); + assert_eq!( + msg.obs[5].sid.reserved, 0, + "incorrect value for obs[5].sid.reserved, expected 0, is {}", + msg.obs[5].sid.reserved + ); + assert_eq!( + msg.obs[5].sid.sat, 218, + "incorrect value for obs[5].sid.sat, expected 218, is {}", + msg.obs[5].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_c.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_c.rs index 76d8e3d63f..56f40ea08e 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_c.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_obs_dep_c.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_obs_dep_c() { { @@ -1108,6 +1113,13 @@ fn test_auto_check_sbp_observation_msg_obs_dep_c() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { @@ -1115,14 +1127,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { let json_input = r#"{"sender": 38982, "msg_type": 73, "header": {"n_obs": 32, "t": {"wn": 1898, "tow": 414670600}}, "obs": [{"lock": 58853, "sid": {"code": 0, "reserved": 0, "sat": 4}, "L": {"i": -565647, "f": 231}, "cn0": 163, "P": 1347025534}, {"lock": 7188, "sid": {"code": 0, "reserved": 0, "sat": 6}, "L": {"i": -355503, "f": 196}, "cn0": 208, "P": 1180752956}, {"lock": 45748, "sid": {"code": 0, "reserved": 0, "sat": 7}, "L": {"i": -902116, "f": 110}, "cn0": 171, "P": 1295924728}, {"lock": 42217, "sid": {"code": 0, "reserved": 0, "sat": 10}, "L": {"i": 861612, "f": 41}, "cn0": 170, "P": 1304319213}, {"lock": 53700, "sid": {"code": 0, "reserved": 0, "sat": 12}, "L": {"i": 1424624, "f": 19}, "cn0": 182, "P": 1258902820}], "crc": 44990, "length": 87, "preamble": 85, "payload": "CF+3GGoHIH76SVBxXvf/56Pl5QQAAAA83GBGUZP6/8TQFBwGAAAA+D0+TRw88v9uq7SyBwAAAO1Uvk2sJQ0AKarppAoAAAAkVQlL8LwVABO2xNEMAAAA"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1369,14 +1381,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { let json_input = r#"{"sender": 38982, "msg_type": 73, "header": {"n_obs": 33, "t": {"wn": 1898, "tow": 414670600}}, "obs": [{"lock": 26274, "sid": {"code": 0, "reserved": 0, "sat": 16}, "L": {"i": 1631930, "f": 101}, "cn0": 186, "P": 1296803396}, {"lock": 7886, "sid": {"code": 0, "reserved": 0, "sat": 27}, "L": {"i": 368202, "f": 26}, "cn0": 190, "P": 1167851351}, {"lock": 18913, "sid": {"code": 0, "reserved": 0, "sat": 29}, "L": {"i": 202266, "f": 114}, "cn0": 217, "P": 1149000000}], "crc": 45861, "length": 55, "preamble": 85, "payload": "CF+3GGoHIUSmS0265hgAZbqiZhAAAABX/5tFSp4FABq+zh4bAAAAQFl8RBoWAwBy2eFJHQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1543,14 +1555,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { let json_input = r#"{"sender": 0, "msg_type": 73, "header": {"n_obs": 32, "t": {"wn": 1898, "tow": 414670600}}, "obs": [{"lock": 20849, "sid": {"code": 0, "reserved": 0, "sat": 4}, "L": {"i": -505847, "f": 30}, "cn0": 168, "P": 1347025881}, {"lock": 36917, "sid": {"code": 0, "reserved": 0, "sat": 6}, "L": {"i": -300090, "f": 115}, "cn0": 195, "P": 1180753107}, {"lock": 36445, "sid": {"code": 0, "reserved": 0, "sat": 7}, "L": {"i": -810712, "f": 130}, "cn0": 176, "P": 1295924557}, {"lock": 54757, "sid": {"code": 0, "reserved": 0, "sat": 10}, "L": {"i": 806232, "f": 116}, "cn0": 199, "P": 1304319489}, {"lock": 49860, "sid": {"code": 0, "reserved": 0, "sat": 12}, "L": {"i": 1346368, "f": 120}, "cn0": 177, "P": 1258902877}], "crc": 41357, "length": 87, "preamble": 85, "payload": "CF+3GGoHINn7SVAJSPj/HqhxUQQAAADT3GBGxmv7/3PDNZAGAAAATT0+TSih8/+CsF2OBwAAAAFWvk1YTQwAdMfl1QoAAABdVQlLQIsUAHixxMIMAAAA"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1797,14 +1809,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { let json_input = r#"{"sender": 0, "msg_type": 73, "header": {"n_obs": 33, "t": {"wn": 1898, "tow": 414670600}}, "obs": [{"lock": 33182, "sid": {"code": 0, "reserved": 0, "sat": 16}, "L": {"i": 1542284, "f": 90}, "cn0": 187, "P": 1296803654}, {"lock": 14511, "sid": {"code": 0, "reserved": 0, "sat": 27}, "L": {"i": 372525, "f": 17}, "cn0": 208, "P": 1167851496}, {"lock": 52809, "sid": {"code": 0, "reserved": 0, "sat": 29}, "L": {"i": 221229, "f": 75}, "cn0": 185, "P": 1149000000}], "crc": 40668, "length": 55, "preamble": 85, "payload": "CF+3GGoHIUanS02MiBcAWruegRAAAADo/5tFLa8FABHQrzgbAAAAQFl8RC1gAwBLuUnOHQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1971,14 +1983,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { let json_input = r#"{"sender": 38982, "msg_type": 73, "header": {"n_obs": 32, "t": {"wn": 1898, "tow": 414670800}}, "obs": [{"lock": 58853, "sid": {"code": 0, "reserved": 0, "sat": 4}, "L": {"i": -565930, "f": 57}, "cn0": 158, "P": 1347029036}, {"lock": 7188, "sid": {"code": 0, "reserved": 0, "sat": 6}, "L": {"i": -355684, "f": 221}, "cn0": 200, "P": 1180755424}, {"lock": 45748, "sid": {"code": 0, "reserved": 0, "sat": 7}, "L": {"i": -902563, "f": 39}, "cn0": 164, "P": 1295929916}, {"lock": 42217, "sid": {"code": 0, "reserved": 0, "sat": 10}, "L": {"i": 861998, "f": 202}, "cn0": 181, "P": 1304316382}, {"lock": 53700, "sid": {"code": 0, "reserved": 0, "sat": 12}, "L": {"i": 1425266, "f": 249}, "cn0": 182, "P": 1258897557}], "crc": 2160, "length": 87, "preamble": 85, "payload": "0F+3GGoHICwISlBWXff/OZ7l5QQAAADg5WBGnJL6/93IFBwGAAAAPFI+TV068v8npLSyBwAAAN5Jvk0uJw0AyrXppAoAAACVQAlLcr8VAPm2xNEMAAAA"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2222,3 +2234,1202 @@ fn test_json2sbp_auto_check_sbp_observation_msg_obs_dep_c() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_obs_dep_c`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_obs_dep_c() { + { + let mut payload = Cursor::new(vec![ + 85, 73, 0, 70, 152, 87, 8, 95, 183, 24, 106, 7, 32, 126, 250, 73, 80, 113, 94, 247, + 255, 231, 163, 229, 229, 4, 0, 0, 0, 60, 220, 96, 70, 81, 147, 250, 255, 196, 208, 20, + 28, 6, 0, 0, 0, 248, 61, 62, 77, 28, 60, 242, 255, 110, 171, 180, 178, 7, 0, 0, 0, 237, + 84, 190, 77, 172, 37, 13, 0, 41, 170, 233, 164, 10, 0, 0, 0, 36, 85, 9, 75, 240, 188, + 21, 0, 19, 182, 196, 209, 12, 0, 0, 0, 190, 175, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x49, + "Incorrect message type, expected 0x49, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x9846, + "incorrect sender id, expected 0x9846, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 414670600, + "incorrect value for header.t.tow, expected 414670600, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1898, + "incorrect value for header.t.wn, expected 1898, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 231, + "incorrect value for obs[0].l.f, expected 231, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -565647, + "incorrect value for obs[0].l.i, expected -565647, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1347025534, + "incorrect value for obs[0].p, expected 1347025534, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 163, + "incorrect value for obs[0].cn0, expected 163, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 58853, + "incorrect value for obs[0].lock, expected 58853, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 4, + "incorrect value for obs[0].sid.sat, expected 4, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 196, + "incorrect value for obs[1].l.f, expected 196, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, -355503, + "incorrect value for obs[1].l.i, expected -355503, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1180752956, + "incorrect value for obs[1].p, expected 1180752956, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 208, + "incorrect value for obs[1].cn0, expected 208, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 7188, + "incorrect value for obs[1].lock, expected 7188, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 6, + "incorrect value for obs[1].sid.sat, expected 6, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 110, + "incorrect value for obs[2].l.f, expected 110, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -902116, + "incorrect value for obs[2].l.i, expected -902116, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1295924728, + "incorrect value for obs[2].p, expected 1295924728, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 171, + "incorrect value for obs[2].cn0, expected 171, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 45748, + "incorrect value for obs[2].lock, expected 45748, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 7, + "incorrect value for obs[2].sid.sat, expected 7, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].l.f, 41, + "incorrect value for obs[3].l.f, expected 41, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 861612, + "incorrect value for obs[3].l.i, expected 861612, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 1304319213, + "incorrect value for obs[3].p, expected 1304319213, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 170, + "incorrect value for obs[3].cn0, expected 170, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 42217, + "incorrect value for obs[3].lock, expected 42217, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.reserved, 0, + "incorrect value for obs[3].sid.reserved, expected 0, is {}", + msg.obs[3].sid.reserved + ); + assert_eq!( + msg.obs[3].sid.sat, 10, + "incorrect value for obs[3].sid.sat, expected 10, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].l.f, 19, + "incorrect value for obs[4].l.f, expected 19, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 1424624, + "incorrect value for obs[4].l.i, expected 1424624, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1258902820, + "incorrect value for obs[4].p, expected 1258902820, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 182, + "incorrect value for obs[4].cn0, expected 182, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 53700, + "incorrect value for obs[4].lock, expected 53700, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.reserved, 0, + "incorrect value for obs[4].sid.reserved, expected 0, is {}", + msg.obs[4].sid.reserved + ); + assert_eq!( + msg.obs[4].sid.sat, 12, + "incorrect value for obs[4].sid.sat, expected 12, is {}", + msg.obs[4].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 73, 0, 70, 152, 55, 8, 95, 183, 24, 106, 7, 33, 68, 166, 75, 77, 186, 230, 24, 0, + 101, 186, 162, 102, 16, 0, 0, 0, 87, 255, 155, 69, 74, 158, 5, 0, 26, 190, 206, 30, 27, + 0, 0, 0, 64, 89, 124, 68, 26, 22, 3, 0, 114, 217, 225, 73, 29, 0, 0, 0, 37, 179, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x49, + "Incorrect message type, expected 0x49, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x9846, + "incorrect sender id, expected 0x9846, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 33, + "incorrect value for header.n_obs, expected 33, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 414670600, + "incorrect value for header.t.tow, expected 414670600, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1898, + "incorrect value for header.t.wn, expected 1898, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 101, + "incorrect value for obs[0].l.f, expected 101, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 1631930, + "incorrect value for obs[0].l.i, expected 1631930, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1296803396, + "incorrect value for obs[0].p, expected 1296803396, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 186, + "incorrect value for obs[0].cn0, expected 186, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 26274, + "incorrect value for obs[0].lock, expected 26274, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 16, + "incorrect value for obs[0].sid.sat, expected 16, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 26, + "incorrect value for obs[1].l.f, expected 26, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 368202, + "incorrect value for obs[1].l.i, expected 368202, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1167851351, + "incorrect value for obs[1].p, expected 1167851351, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 190, + "incorrect value for obs[1].cn0, expected 190, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 7886, + "incorrect value for obs[1].lock, expected 7886, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 27, + "incorrect value for obs[1].sid.sat, expected 27, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 114, + "incorrect value for obs[2].l.f, expected 114, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 202266, + "incorrect value for obs[2].l.i, expected 202266, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1149000000, + "incorrect value for obs[2].p, expected 1149000000, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 217, + "incorrect value for obs[2].cn0, expected 217, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 18913, + "incorrect value for obs[2].lock, expected 18913, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 29, + "incorrect value for obs[2].sid.sat, expected 29, is {}", + msg.obs[2].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 73, 0, 0, 0, 87, 8, 95, 183, 24, 106, 7, 32, 217, 251, 73, 80, 9, 72, 248, 255, 30, + 168, 113, 81, 4, 0, 0, 0, 211, 220, 96, 70, 198, 107, 251, 255, 115, 195, 53, 144, 6, + 0, 0, 0, 77, 61, 62, 77, 40, 161, 243, 255, 130, 176, 93, 142, 7, 0, 0, 0, 1, 86, 190, + 77, 88, 77, 12, 0, 116, 199, 229, 213, 10, 0, 0, 0, 93, 85, 9, 75, 64, 139, 20, 0, 120, + 177, 196, 194, 12, 0, 0, 0, 141, 161, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x49, + "Incorrect message type, expected 0x49, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0, + "incorrect sender id, expected 0x0, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 414670600, + "incorrect value for header.t.tow, expected 414670600, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1898, + "incorrect value for header.t.wn, expected 1898, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 30, + "incorrect value for obs[0].l.f, expected 30, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -505847, + "incorrect value for obs[0].l.i, expected -505847, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1347025881, + "incorrect value for obs[0].p, expected 1347025881, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 168, + "incorrect value for obs[0].cn0, expected 168, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 20849, + "incorrect value for obs[0].lock, expected 20849, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 4, + "incorrect value for obs[0].sid.sat, expected 4, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 115, + "incorrect value for obs[1].l.f, expected 115, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, -300090, + "incorrect value for obs[1].l.i, expected -300090, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1180753107, + "incorrect value for obs[1].p, expected 1180753107, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 195, + "incorrect value for obs[1].cn0, expected 195, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 36917, + "incorrect value for obs[1].lock, expected 36917, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 6, + "incorrect value for obs[1].sid.sat, expected 6, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 130, + "incorrect value for obs[2].l.f, expected 130, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -810712, + "incorrect value for obs[2].l.i, expected -810712, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1295924557, + "incorrect value for obs[2].p, expected 1295924557, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 176, + "incorrect value for obs[2].cn0, expected 176, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 36445, + "incorrect value for obs[2].lock, expected 36445, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 7, + "incorrect value for obs[2].sid.sat, expected 7, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].l.f, 116, + "incorrect value for obs[3].l.f, expected 116, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 806232, + "incorrect value for obs[3].l.i, expected 806232, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 1304319489, + "incorrect value for obs[3].p, expected 1304319489, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 199, + "incorrect value for obs[3].cn0, expected 199, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 54757, + "incorrect value for obs[3].lock, expected 54757, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.reserved, 0, + "incorrect value for obs[3].sid.reserved, expected 0, is {}", + msg.obs[3].sid.reserved + ); + assert_eq!( + msg.obs[3].sid.sat, 10, + "incorrect value for obs[3].sid.sat, expected 10, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].l.f, 120, + "incorrect value for obs[4].l.f, expected 120, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 1346368, + "incorrect value for obs[4].l.i, expected 1346368, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1258902877, + "incorrect value for obs[4].p, expected 1258902877, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 177, + "incorrect value for obs[4].cn0, expected 177, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 49860, + "incorrect value for obs[4].lock, expected 49860, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.reserved, 0, + "incorrect value for obs[4].sid.reserved, expected 0, is {}", + msg.obs[4].sid.reserved + ); + assert_eq!( + msg.obs[4].sid.sat, 12, + "incorrect value for obs[4].sid.sat, expected 12, is {}", + msg.obs[4].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 73, 0, 0, 0, 55, 8, 95, 183, 24, 106, 7, 33, 70, 167, 75, 77, 140, 136, 23, 0, 90, + 187, 158, 129, 16, 0, 0, 0, 232, 255, 155, 69, 45, 175, 5, 0, 17, 208, 175, 56, 27, 0, + 0, 0, 64, 89, 124, 68, 45, 96, 3, 0, 75, 185, 73, 206, 29, 0, 0, 0, 220, 158, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x49, + "Incorrect message type, expected 0x49, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0, + "incorrect sender id, expected 0x0, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 33, + "incorrect value for header.n_obs, expected 33, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 414670600, + "incorrect value for header.t.tow, expected 414670600, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1898, + "incorrect value for header.t.wn, expected 1898, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 90, + "incorrect value for obs[0].l.f, expected 90, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 1542284, + "incorrect value for obs[0].l.i, expected 1542284, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1296803654, + "incorrect value for obs[0].p, expected 1296803654, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 187, + "incorrect value for obs[0].cn0, expected 187, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 33182, + "incorrect value for obs[0].lock, expected 33182, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 16, + "incorrect value for obs[0].sid.sat, expected 16, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 17, + "incorrect value for obs[1].l.f, expected 17, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 372525, + "incorrect value for obs[1].l.i, expected 372525, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1167851496, + "incorrect value for obs[1].p, expected 1167851496, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 208, + "incorrect value for obs[1].cn0, expected 208, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 14511, + "incorrect value for obs[1].lock, expected 14511, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 27, + "incorrect value for obs[1].sid.sat, expected 27, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 75, + "incorrect value for obs[2].l.f, expected 75, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 221229, + "incorrect value for obs[2].l.i, expected 221229, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1149000000, + "incorrect value for obs[2].p, expected 1149000000, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 185, + "incorrect value for obs[2].cn0, expected 185, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 52809, + "incorrect value for obs[2].lock, expected 52809, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 29, + "incorrect value for obs[2].sid.sat, expected 29, is {}", + msg.obs[2].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 73, 0, 70, 152, 87, 208, 95, 183, 24, 106, 7, 32, 44, 8, 74, 80, 86, 93, 247, 255, + 57, 158, 229, 229, 4, 0, 0, 0, 224, 229, 96, 70, 156, 146, 250, 255, 221, 200, 20, 28, + 6, 0, 0, 0, 60, 82, 62, 77, 93, 58, 242, 255, 39, 164, 180, 178, 7, 0, 0, 0, 222, 73, + 190, 77, 46, 39, 13, 0, 202, 181, 233, 164, 10, 0, 0, 0, 149, 64, 9, 75, 114, 191, 21, + 0, 249, 182, 196, 209, 12, 0, 0, 0, 112, 8, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgObsDepC( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgObsDepC(msg) => { + assert_eq!( + msg.message_type(), + 0x49, + "Incorrect message type, expected 0x49, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x9846, + "incorrect sender id, expected 0x9846, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 32, + "incorrect value for header.n_obs, expected 32, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.tow, 414670800, + "incorrect value for header.t.tow, expected 414670800, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 1898, + "incorrect value for header.t.wn, expected 1898, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 57, + "incorrect value for obs[0].l.f, expected 57, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, -565930, + "incorrect value for obs[0].l.i, expected -565930, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1347029036, + "incorrect value for obs[0].p, expected 1347029036, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].cn0, 158, + "incorrect value for obs[0].cn0, expected 158, is {}", + msg.obs[0].cn0 + ); + assert_eq!( + msg.obs[0].lock, 58853, + "incorrect value for obs[0].lock, expected 58853, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.reserved, 0, + "incorrect value for obs[0].sid.reserved, expected 0, is {}", + msg.obs[0].sid.reserved + ); + assert_eq!( + msg.obs[0].sid.sat, 4, + "incorrect value for obs[0].sid.sat, expected 4, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[1].l.f, 221, + "incorrect value for obs[1].l.f, expected 221, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, -355684, + "incorrect value for obs[1].l.i, expected -355684, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1180755424, + "incorrect value for obs[1].p, expected 1180755424, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].cn0, 200, + "incorrect value for obs[1].cn0, expected 200, is {}", + msg.obs[1].cn0 + ); + assert_eq!( + msg.obs[1].lock, 7188, + "incorrect value for obs[1].lock, expected 7188, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.reserved, 0, + "incorrect value for obs[1].sid.reserved, expected 0, is {}", + msg.obs[1].sid.reserved + ); + assert_eq!( + msg.obs[1].sid.sat, 6, + "incorrect value for obs[1].sid.sat, expected 6, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[2].l.f, 39, + "incorrect value for obs[2].l.f, expected 39, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, -902563, + "incorrect value for obs[2].l.i, expected -902563, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1295929916, + "incorrect value for obs[2].p, expected 1295929916, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].cn0, 164, + "incorrect value for obs[2].cn0, expected 164, is {}", + msg.obs[2].cn0 + ); + assert_eq!( + msg.obs[2].lock, 45748, + "incorrect value for obs[2].lock, expected 45748, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.reserved, 0, + "incorrect value for obs[2].sid.reserved, expected 0, is {}", + msg.obs[2].sid.reserved + ); + assert_eq!( + msg.obs[2].sid.sat, 7, + "incorrect value for obs[2].sid.sat, expected 7, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[3].l.f, 202, + "incorrect value for obs[3].l.f, expected 202, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 861998, + "incorrect value for obs[3].l.i, expected 861998, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 1304316382, + "incorrect value for obs[3].p, expected 1304316382, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].cn0, 181, + "incorrect value for obs[3].cn0, expected 181, is {}", + msg.obs[3].cn0 + ); + assert_eq!( + msg.obs[3].lock, 42217, + "incorrect value for obs[3].lock, expected 42217, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.reserved, 0, + "incorrect value for obs[3].sid.reserved, expected 0, is {}", + msg.obs[3].sid.reserved + ); + assert_eq!( + msg.obs[3].sid.sat, 10, + "incorrect value for obs[3].sid.sat, expected 10, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[4].l.f, 249, + "incorrect value for obs[4].l.f, expected 249, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 1425266, + "incorrect value for obs[4].l.i, expected 1425266, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1258897557, + "incorrect value for obs[4].p, expected 1258897557, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].cn0, 182, + "incorrect value for obs[4].cn0, expected 182, is {}", + msg.obs[4].cn0 + ); + assert_eq!( + msg.obs[4].lock, 53700, + "incorrect value for obs[4].lock, expected 53700, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.reserved, 0, + "incorrect value for obs[4].sid.reserved, expected 0, is {}", + msg.obs[4].sid.reserved + ); + assert_eq!( + msg.obs[4].sid.sat, 12, + "incorrect value for obs[4].sid.sat, expected 12, is {}", + msg.obs[4].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgObsDepC"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_osr.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_osr.rs index d0962c0822..010f9c362b 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_osr.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_osr.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_osr() { { @@ -679,6 +684,13 @@ fn test_auto_check_sbp_observation_msg_osr() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_osr() { @@ -686,14 +698,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_osr() { let json_input = r#"{"header":{"t":{"tow":501867000,"ns_residual":0,"wn":2152},"n_obs":64},"obs":[{"P":1156681547,"L":{"i":121567974,"f":66},"lock":15,"flags":3,"sid":{"sat":1,"code":0},"iono_std":13,"tropo_std":7,"range_std":7},{"P":1063905486,"L":{"i":111817196,"f":75},"lock":15,"flags":3,"sid":{"sat":13,"code":0},"iono_std":13,"tropo_std":3,"range_std":3},{"P":1053200685,"L":{"i":110692129,"f":128},"lock":15,"flags":3,"sid":{"sat":14,"code":0},"iono_std":13,"tropo_std":3,"range_std":3},{"P":1137476697,"L":{"i":119549583,"f":127},"lock":15,"flags":3,"sid":{"sat":15,"code":0},"iono_std":13,"tropo_std":5,"range_std":5},{"P":1017446132,"L":{"i":106934294,"f":55},"lock":15,"flags":3,"sid":{"sat":17,"code":0},"iono_std":0,"tropo_std":2,"range_std":2},{"P":1046846826,"L":{"i":110024343,"f":108},"lock":15,"flags":3,"sid":{"sat":19,"code":0},"iono_std":13,"tropo_std":3,"range_std":3},{"P":1060957521,"L":{"i":111507381,"f":206},"lock":15,"flags":3,"sid":{"sat":28,"code":0},"iono_std":13,"tropo_std":3,"range_std":3},{"P":1081009286,"L":{"i":113614775,"f":200},"lock":15,"flags":3,"sid":{"sat":30,"code":0},"iono_std":13,"tropo_std":3,"range_std":3},{"P":1156681781,"L":{"i":94728270,"f":170},"lock":15,"flags":3,"sid":{"sat":1,"code":6},"iono_std":21,"tropo_std":7,"range_std":7},{"P":1063905531,"L":{"i":87130275,"f":129},"lock":15,"flags":3,"sid":{"sat":13,"code":6},"iono_std":21,"tropo_std":3,"range_std":3},{"P":1053200752,"L":{"i":86253605,"f":46},"lock":15,"flags":3,"sid":{"sat":14,"code":6},"iono_std":21,"tropo_std":3,"range_std":3},{"P":1137476774,"L":{"i":93155512,"f":95},"lock":15,"flags":3,"sid":{"sat":15,"code":6},"iono_std":21,"tropo_std":5,"range_std":5}],"preamble":85,"msg_type":1600,"sender":0,"payload":"+OHpHQAAAABoCEBLj/FE5vo+B0IPAwEADQAHAAcAzuhpP+wxqgZLDwMNAA0AAwADAC2Rxj4hB5kGgA8DDgANAAMAAwBZhMxDjy4gB38PAw8ADQAFAAUA9P6kPBawXwY3DwMRAAAAAgACAGqdZT6X1o4GbA8DEwANAAMAAwBR7Tw/tXelBs4PAxwADQADAAMAhuRuQLefxQbIDwMeAA0AAwADADWQ8UROcKUFqg8DAQYVAAcABwD76Gk/o4AxBYEPAw0GFQADAAMAcJHGPiUgJAUuDwMOBhUAAwADAKaEzEO4cI0FXw8DDwYVAAUABQA=","crc":58233,"length":239}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1342,3 +1354,697 @@ fn test_json2sbp_auto_check_sbp_observation_msg_osr() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_osr`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_osr() { + { + let mut payload = Cursor::new(vec![ + 85, 64, 6, 0, 0, 239, 248, 225, 233, 29, 0, 0, 0, 0, 104, 8, 64, 75, 143, 241, 68, 230, + 250, 62, 7, 66, 15, 3, 1, 0, 13, 0, 7, 0, 7, 0, 206, 232, 105, 63, 236, 49, 170, 6, 75, + 15, 3, 13, 0, 13, 0, 3, 0, 3, 0, 45, 145, 198, 62, 33, 7, 153, 6, 128, 15, 3, 14, 0, + 13, 0, 3, 0, 3, 0, 89, 132, 204, 67, 143, 46, 32, 7, 127, 15, 3, 15, 0, 13, 0, 5, 0, 5, + 0, 244, 254, 164, 60, 22, 176, 95, 6, 55, 15, 3, 17, 0, 0, 0, 2, 0, 2, 0, 106, 157, + 101, 62, 151, 214, 142, 6, 108, 15, 3, 19, 0, 13, 0, 3, 0, 3, 0, 81, 237, 60, 63, 181, + 119, 165, 6, 206, 15, 3, 28, 0, 13, 0, 3, 0, 3, 0, 134, 228, 110, 64, 183, 159, 197, 6, + 200, 15, 3, 30, 0, 13, 0, 3, 0, 3, 0, 53, 144, 241, 68, 78, 112, 165, 5, 170, 15, 3, 1, + 6, 21, 0, 7, 0, 7, 0, 251, 232, 105, 63, 163, 128, 49, 5, 129, 15, 3, 13, 6, 21, 0, 3, + 0, 3, 0, 112, 145, 198, 62, 37, 32, 36, 5, 46, 15, 3, 14, 6, 21, 0, 3, 0, 3, 0, 166, + 132, 204, 67, 184, 112, 141, 5, 95, 15, 3, 15, 6, 21, 0, 5, 0, 5, 0, 121, 227, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgOsr( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgOsr(msg) => { + assert_eq!( + msg.message_type(), + 0x640, + "Incorrect message type, expected 0x640, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0, + "incorrect sender id, expected 0, is {}", + sender_id + ); + assert_eq!( + msg.header.n_obs, 64, + "incorrect value for header.n_obs, expected 64, is {}", + msg.header.n_obs + ); + assert_eq!( + msg.header.t.ns_residual, 0, + "incorrect value for header.t.ns_residual, expected 0, is {}", + msg.header.t.ns_residual + ); + assert_eq!( + msg.header.t.tow, 501867000, + "incorrect value for header.t.tow, expected 501867000, is {}", + msg.header.t.tow + ); + assert_eq!( + msg.header.t.wn, 2152, + "incorrect value for header.t.wn, expected 2152, is {}", + msg.header.t.wn + ); + assert_eq!( + msg.obs[0].l.f, 66, + "incorrect value for obs[0].l.f, expected 66, is {}", + msg.obs[0].l.f + ); + assert_eq!( + msg.obs[0].l.i, 121567974, + "incorrect value for obs[0].l.i, expected 121567974, is {}", + msg.obs[0].l.i + ); + assert_eq!( + msg.obs[0].p, 1156681547, + "incorrect value for obs[0].p, expected 1156681547, is {}", + msg.obs[0].p + ); + assert_eq!( + msg.obs[0].flags, 3, + "incorrect value for obs[0].flags, expected 3, is {}", + msg.obs[0].flags + ); + assert_eq!( + msg.obs[0].iono_std, 13, + "incorrect value for obs[0].iono_std, expected 13, is {}", + msg.obs[0].iono_std + ); + assert_eq!( + msg.obs[0].lock, 15, + "incorrect value for obs[0].lock, expected 15, is {}", + msg.obs[0].lock + ); + assert_eq!( + msg.obs[0].range_std, 7, + "incorrect value for obs[0].range_std, expected 7, is {}", + msg.obs[0].range_std + ); + assert_eq!( + msg.obs[0].sid.code, 0, + "incorrect value for obs[0].sid.code, expected 0, is {}", + msg.obs[0].sid.code + ); + assert_eq!( + msg.obs[0].sid.sat, 1, + "incorrect value for obs[0].sid.sat, expected 1, is {}", + msg.obs[0].sid.sat + ); + assert_eq!( + msg.obs[0].tropo_std, 7, + "incorrect value for obs[0].tropo_std, expected 7, is {}", + msg.obs[0].tropo_std + ); + assert_eq!( + msg.obs[1].l.f, 75, + "incorrect value for obs[1].l.f, expected 75, is {}", + msg.obs[1].l.f + ); + assert_eq!( + msg.obs[1].l.i, 111817196, + "incorrect value for obs[1].l.i, expected 111817196, is {}", + msg.obs[1].l.i + ); + assert_eq!( + msg.obs[1].p, 1063905486, + "incorrect value for obs[1].p, expected 1063905486, is {}", + msg.obs[1].p + ); + assert_eq!( + msg.obs[1].flags, 3, + "incorrect value for obs[1].flags, expected 3, is {}", + msg.obs[1].flags + ); + assert_eq!( + msg.obs[1].iono_std, 13, + "incorrect value for obs[1].iono_std, expected 13, is {}", + msg.obs[1].iono_std + ); + assert_eq!( + msg.obs[1].lock, 15, + "incorrect value for obs[1].lock, expected 15, is {}", + msg.obs[1].lock + ); + assert_eq!( + msg.obs[1].range_std, 3, + "incorrect value for obs[1].range_std, expected 3, is {}", + msg.obs[1].range_std + ); + assert_eq!( + msg.obs[1].sid.code, 0, + "incorrect value for obs[1].sid.code, expected 0, is {}", + msg.obs[1].sid.code + ); + assert_eq!( + msg.obs[1].sid.sat, 13, + "incorrect value for obs[1].sid.sat, expected 13, is {}", + msg.obs[1].sid.sat + ); + assert_eq!( + msg.obs[1].tropo_std, 3, + "incorrect value for obs[1].tropo_std, expected 3, is {}", + msg.obs[1].tropo_std + ); + assert_eq!( + msg.obs[2].l.f, 128, + "incorrect value for obs[2].l.f, expected 128, is {}", + msg.obs[2].l.f + ); + assert_eq!( + msg.obs[2].l.i, 110692129, + "incorrect value for obs[2].l.i, expected 110692129, is {}", + msg.obs[2].l.i + ); + assert_eq!( + msg.obs[2].p, 1053200685, + "incorrect value for obs[2].p, expected 1053200685, is {}", + msg.obs[2].p + ); + assert_eq!( + msg.obs[2].flags, 3, + "incorrect value for obs[2].flags, expected 3, is {}", + msg.obs[2].flags + ); + assert_eq!( + msg.obs[2].iono_std, 13, + "incorrect value for obs[2].iono_std, expected 13, is {}", + msg.obs[2].iono_std + ); + assert_eq!( + msg.obs[2].lock, 15, + "incorrect value for obs[2].lock, expected 15, is {}", + msg.obs[2].lock + ); + assert_eq!( + msg.obs[2].range_std, 3, + "incorrect value for obs[2].range_std, expected 3, is {}", + msg.obs[2].range_std + ); + assert_eq!( + msg.obs[2].sid.code, 0, + "incorrect value for obs[2].sid.code, expected 0, is {}", + msg.obs[2].sid.code + ); + assert_eq!( + msg.obs[2].sid.sat, 14, + "incorrect value for obs[2].sid.sat, expected 14, is {}", + msg.obs[2].sid.sat + ); + assert_eq!( + msg.obs[2].tropo_std, 3, + "incorrect value for obs[2].tropo_std, expected 3, is {}", + msg.obs[2].tropo_std + ); + assert_eq!( + msg.obs[3].l.f, 127, + "incorrect value for obs[3].l.f, expected 127, is {}", + msg.obs[3].l.f + ); + assert_eq!( + msg.obs[3].l.i, 119549583, + "incorrect value for obs[3].l.i, expected 119549583, is {}", + msg.obs[3].l.i + ); + assert_eq!( + msg.obs[3].p, 1137476697, + "incorrect value for obs[3].p, expected 1137476697, is {}", + msg.obs[3].p + ); + assert_eq!( + msg.obs[3].flags, 3, + "incorrect value for obs[3].flags, expected 3, is {}", + msg.obs[3].flags + ); + assert_eq!( + msg.obs[3].iono_std, 13, + "incorrect value for obs[3].iono_std, expected 13, is {}", + msg.obs[3].iono_std + ); + assert_eq!( + msg.obs[3].lock, 15, + "incorrect value for obs[3].lock, expected 15, is {}", + msg.obs[3].lock + ); + assert_eq!( + msg.obs[3].range_std, 5, + "incorrect value for obs[3].range_std, expected 5, is {}", + msg.obs[3].range_std + ); + assert_eq!( + msg.obs[3].sid.code, 0, + "incorrect value for obs[3].sid.code, expected 0, is {}", + msg.obs[3].sid.code + ); + assert_eq!( + msg.obs[3].sid.sat, 15, + "incorrect value for obs[3].sid.sat, expected 15, is {}", + msg.obs[3].sid.sat + ); + assert_eq!( + msg.obs[3].tropo_std, 5, + "incorrect value for obs[3].tropo_std, expected 5, is {}", + msg.obs[3].tropo_std + ); + assert_eq!( + msg.obs[4].l.f, 55, + "incorrect value for obs[4].l.f, expected 55, is {}", + msg.obs[4].l.f + ); + assert_eq!( + msg.obs[4].l.i, 106934294, + "incorrect value for obs[4].l.i, expected 106934294, is {}", + msg.obs[4].l.i + ); + assert_eq!( + msg.obs[4].p, 1017446132, + "incorrect value for obs[4].p, expected 1017446132, is {}", + msg.obs[4].p + ); + assert_eq!( + msg.obs[4].flags, 3, + "incorrect value for obs[4].flags, expected 3, is {}", + msg.obs[4].flags + ); + assert_eq!( + msg.obs[4].iono_std, 0, + "incorrect value for obs[4].iono_std, expected 0, is {}", + msg.obs[4].iono_std + ); + assert_eq!( + msg.obs[4].lock, 15, + "incorrect value for obs[4].lock, expected 15, is {}", + msg.obs[4].lock + ); + assert_eq!( + msg.obs[4].range_std, 2, + "incorrect value for obs[4].range_std, expected 2, is {}", + msg.obs[4].range_std + ); + assert_eq!( + msg.obs[4].sid.code, 0, + "incorrect value for obs[4].sid.code, expected 0, is {}", + msg.obs[4].sid.code + ); + assert_eq!( + msg.obs[4].sid.sat, 17, + "incorrect value for obs[4].sid.sat, expected 17, is {}", + msg.obs[4].sid.sat + ); + assert_eq!( + msg.obs[4].tropo_std, 2, + "incorrect value for obs[4].tropo_std, expected 2, is {}", + msg.obs[4].tropo_std + ); + assert_eq!( + msg.obs[5].l.f, 108, + "incorrect value for obs[5].l.f, expected 108, is {}", + msg.obs[5].l.f + ); + assert_eq!( + msg.obs[5].l.i, 110024343, + "incorrect value for obs[5].l.i, expected 110024343, is {}", + msg.obs[5].l.i + ); + assert_eq!( + msg.obs[5].p, 1046846826, + "incorrect value for obs[5].p, expected 1046846826, is {}", + msg.obs[5].p + ); + assert_eq!( + msg.obs[5].flags, 3, + "incorrect value for obs[5].flags, expected 3, is {}", + msg.obs[5].flags + ); + assert_eq!( + msg.obs[5].iono_std, 13, + "incorrect value for obs[5].iono_std, expected 13, is {}", + msg.obs[5].iono_std + ); + assert_eq!( + msg.obs[5].lock, 15, + "incorrect value for obs[5].lock, expected 15, is {}", + msg.obs[5].lock + ); + assert_eq!( + msg.obs[5].range_std, 3, + "incorrect value for obs[5].range_std, expected 3, is {}", + msg.obs[5].range_std + ); + assert_eq!( + msg.obs[5].sid.code, 0, + "incorrect value for obs[5].sid.code, expected 0, is {}", + msg.obs[5].sid.code + ); + assert_eq!( + msg.obs[5].sid.sat, 19, + "incorrect value for obs[5].sid.sat, expected 19, is {}", + msg.obs[5].sid.sat + ); + assert_eq!( + msg.obs[5].tropo_std, 3, + "incorrect value for obs[5].tropo_std, expected 3, is {}", + msg.obs[5].tropo_std + ); + assert_eq!( + msg.obs[6].l.f, 206, + "incorrect value for obs[6].l.f, expected 206, is {}", + msg.obs[6].l.f + ); + assert_eq!( + msg.obs[6].l.i, 111507381, + "incorrect value for obs[6].l.i, expected 111507381, is {}", + msg.obs[6].l.i + ); + assert_eq!( + msg.obs[6].p, 1060957521, + "incorrect value for obs[6].p, expected 1060957521, is {}", + msg.obs[6].p + ); + assert_eq!( + msg.obs[6].flags, 3, + "incorrect value for obs[6].flags, expected 3, is {}", + msg.obs[6].flags + ); + assert_eq!( + msg.obs[6].iono_std, 13, + "incorrect value for obs[6].iono_std, expected 13, is {}", + msg.obs[6].iono_std + ); + assert_eq!( + msg.obs[6].lock, 15, + "incorrect value for obs[6].lock, expected 15, is {}", + msg.obs[6].lock + ); + assert_eq!( + msg.obs[6].range_std, 3, + "incorrect value for obs[6].range_std, expected 3, is {}", + msg.obs[6].range_std + ); + assert_eq!( + msg.obs[6].sid.code, 0, + "incorrect value for obs[6].sid.code, expected 0, is {}", + msg.obs[6].sid.code + ); + assert_eq!( + msg.obs[6].sid.sat, 28, + "incorrect value for obs[6].sid.sat, expected 28, is {}", + msg.obs[6].sid.sat + ); + assert_eq!( + msg.obs[6].tropo_std, 3, + "incorrect value for obs[6].tropo_std, expected 3, is {}", + msg.obs[6].tropo_std + ); + assert_eq!( + msg.obs[7].l.f, 200, + "incorrect value for obs[7].l.f, expected 200, is {}", + msg.obs[7].l.f + ); + assert_eq!( + msg.obs[7].l.i, 113614775, + "incorrect value for obs[7].l.i, expected 113614775, is {}", + msg.obs[7].l.i + ); + assert_eq!( + msg.obs[7].p, 1081009286, + "incorrect value for obs[7].p, expected 1081009286, is {}", + msg.obs[7].p + ); + assert_eq!( + msg.obs[7].flags, 3, + "incorrect value for obs[7].flags, expected 3, is {}", + msg.obs[7].flags + ); + assert_eq!( + msg.obs[7].iono_std, 13, + "incorrect value for obs[7].iono_std, expected 13, is {}", + msg.obs[7].iono_std + ); + assert_eq!( + msg.obs[7].lock, 15, + "incorrect value for obs[7].lock, expected 15, is {}", + msg.obs[7].lock + ); + assert_eq!( + msg.obs[7].range_std, 3, + "incorrect value for obs[7].range_std, expected 3, is {}", + msg.obs[7].range_std + ); + assert_eq!( + msg.obs[7].sid.code, 0, + "incorrect value for obs[7].sid.code, expected 0, is {}", + msg.obs[7].sid.code + ); + assert_eq!( + msg.obs[7].sid.sat, 30, + "incorrect value for obs[7].sid.sat, expected 30, is {}", + msg.obs[7].sid.sat + ); + assert_eq!( + msg.obs[7].tropo_std, 3, + "incorrect value for obs[7].tropo_std, expected 3, is {}", + msg.obs[7].tropo_std + ); + assert_eq!( + msg.obs[8].l.f, 170, + "incorrect value for obs[8].l.f, expected 170, is {}", + msg.obs[8].l.f + ); + assert_eq!( + msg.obs[8].l.i, 94728270, + "incorrect value for obs[8].l.i, expected 94728270, is {}", + msg.obs[8].l.i + ); + assert_eq!( + msg.obs[8].p, 1156681781, + "incorrect value for obs[8].p, expected 1156681781, is {}", + msg.obs[8].p + ); + assert_eq!( + msg.obs[8].flags, 3, + "incorrect value for obs[8].flags, expected 3, is {}", + msg.obs[8].flags + ); + assert_eq!( + msg.obs[8].iono_std, 21, + "incorrect value for obs[8].iono_std, expected 21, is {}", + msg.obs[8].iono_std + ); + assert_eq!( + msg.obs[8].lock, 15, + "incorrect value for obs[8].lock, expected 15, is {}", + msg.obs[8].lock + ); + assert_eq!( + msg.obs[8].range_std, 7, + "incorrect value for obs[8].range_std, expected 7, is {}", + msg.obs[8].range_std + ); + assert_eq!( + msg.obs[8].sid.code, 6, + "incorrect value for obs[8].sid.code, expected 6, is {}", + msg.obs[8].sid.code + ); + assert_eq!( + msg.obs[8].sid.sat, 1, + "incorrect value for obs[8].sid.sat, expected 1, is {}", + msg.obs[8].sid.sat + ); + assert_eq!( + msg.obs[8].tropo_std, 7, + "incorrect value for obs[8].tropo_std, expected 7, is {}", + msg.obs[8].tropo_std + ); + assert_eq!( + msg.obs[9].l.f, 129, + "incorrect value for obs[9].l.f, expected 129, is {}", + msg.obs[9].l.f + ); + assert_eq!( + msg.obs[9].l.i, 87130275, + "incorrect value for obs[9].l.i, expected 87130275, is {}", + msg.obs[9].l.i + ); + assert_eq!( + msg.obs[9].p, 1063905531, + "incorrect value for obs[9].p, expected 1063905531, is {}", + msg.obs[9].p + ); + assert_eq!( + msg.obs[9].flags, 3, + "incorrect value for obs[9].flags, expected 3, is {}", + msg.obs[9].flags + ); + assert_eq!( + msg.obs[9].iono_std, 21, + "incorrect value for obs[9].iono_std, expected 21, is {}", + msg.obs[9].iono_std + ); + assert_eq!( + msg.obs[9].lock, 15, + "incorrect value for obs[9].lock, expected 15, is {}", + msg.obs[9].lock + ); + assert_eq!( + msg.obs[9].range_std, 3, + "incorrect value for obs[9].range_std, expected 3, is {}", + msg.obs[9].range_std + ); + assert_eq!( + msg.obs[9].sid.code, 6, + "incorrect value for obs[9].sid.code, expected 6, is {}", + msg.obs[9].sid.code + ); + assert_eq!( + msg.obs[9].sid.sat, 13, + "incorrect value for obs[9].sid.sat, expected 13, is {}", + msg.obs[9].sid.sat + ); + assert_eq!( + msg.obs[9].tropo_std, 3, + "incorrect value for obs[9].tropo_std, expected 3, is {}", + msg.obs[9].tropo_std + ); + assert_eq!( + msg.obs[10].l.f, 46, + "incorrect value for obs[10].l.f, expected 46, is {}", + msg.obs[10].l.f + ); + assert_eq!( + msg.obs[10].l.i, 86253605, + "incorrect value for obs[10].l.i, expected 86253605, is {}", + msg.obs[10].l.i + ); + assert_eq!( + msg.obs[10].p, 1053200752, + "incorrect value for obs[10].p, expected 1053200752, is {}", + msg.obs[10].p + ); + assert_eq!( + msg.obs[10].flags, 3, + "incorrect value for obs[10].flags, expected 3, is {}", + msg.obs[10].flags + ); + assert_eq!( + msg.obs[10].iono_std, 21, + "incorrect value for obs[10].iono_std, expected 21, is {}", + msg.obs[10].iono_std + ); + assert_eq!( + msg.obs[10].lock, 15, + "incorrect value for obs[10].lock, expected 15, is {}", + msg.obs[10].lock + ); + assert_eq!( + msg.obs[10].range_std, 3, + "incorrect value for obs[10].range_std, expected 3, is {}", + msg.obs[10].range_std + ); + assert_eq!( + msg.obs[10].sid.code, 6, + "incorrect value for obs[10].sid.code, expected 6, is {}", + msg.obs[10].sid.code + ); + assert_eq!( + msg.obs[10].sid.sat, 14, + "incorrect value for obs[10].sid.sat, expected 14, is {}", + msg.obs[10].sid.sat + ); + assert_eq!( + msg.obs[10].tropo_std, 3, + "incorrect value for obs[10].tropo_std, expected 3, is {}", + msg.obs[10].tropo_std + ); + assert_eq!( + msg.obs[11].l.f, 95, + "incorrect value for obs[11].l.f, expected 95, is {}", + msg.obs[11].l.f + ); + assert_eq!( + msg.obs[11].l.i, 93155512, + "incorrect value for obs[11].l.i, expected 93155512, is {}", + msg.obs[11].l.i + ); + assert_eq!( + msg.obs[11].p, 1137476774, + "incorrect value for obs[11].p, expected 1137476774, is {}", + msg.obs[11].p + ); + assert_eq!( + msg.obs[11].flags, 3, + "incorrect value for obs[11].flags, expected 3, is {}", + msg.obs[11].flags + ); + assert_eq!( + msg.obs[11].iono_std, 21, + "incorrect value for obs[11].iono_std, expected 21, is {}", + msg.obs[11].iono_std + ); + assert_eq!( + msg.obs[11].lock, 15, + "incorrect value for obs[11].lock, expected 15, is {}", + msg.obs[11].lock + ); + assert_eq!( + msg.obs[11].range_std, 5, + "incorrect value for obs[11].range_std, expected 5, is {}", + msg.obs[11].range_std + ); + assert_eq!( + msg.obs[11].sid.code, 6, + "incorrect value for obs[11].sid.code, expected 6, is {}", + msg.obs[11].sid.code + ); + assert_eq!( + msg.obs[11].sid.sat, 15, + "incorrect value for obs[11].sid.sat, expected 15, is {}", + msg.obs[11].sid.sat + ); + assert_eq!( + msg.obs[11].tropo_std, 5, + "incorrect value for obs[11].tropo_std, expected 5, is {}", + msg.obs[11].tropo_std + ); + } + _ => panic!("Invalid message type! Expected a MsgOsr"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_sv_az_el.rs b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_sv_az_el.rs index 2fe26713ca..9cf3d902ed 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_observation_msg_sv_az_el.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_observation_msg_sv_az_el.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_observation_msg_sv_az_el() { { @@ -715,6 +720,13 @@ fn test_auto_check_sbp_observation_msg_sv_az_el() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_observation_msg_sv_az_el() { @@ -722,14 +734,14 @@ fn test_json2sbp_auto_check_sbp_observation_msg_sv_az_el() { let json_input = r#"{"preamble": 85, "msg_type": 151, "sender": 31183, "length": 132, "payload": "CACgDAoAi0INABABDwAYGRAAfxISACo1FAAfEBcADEMYAC8KGgB0CBsAmSsdAE0KIABeGgEDEDoCA2w1CAMRDREDpSgXAz8jGAMpSRQMchobDEg2HAxFAx0Mng4eDJhEIAx4UgIOgwYEDhssBQ5lFQkOUUELDjE4DA47Bh4OmgQkDqU+", "crc": 9384, "azel": [{"sid": {"sat": 8, "code": 0}, "az": 160, "el": 12}, {"sid": {"sat": 10, "code": 0}, "az": 139, "el": 66}, {"sid": {"sat": 13, "code": 0}, "az": 16, "el": 1}, {"sid": {"sat": 15, "code": 0}, "az": 24, "el": 25}, {"sid": {"sat": 16, "code": 0}, "az": 127, "el": 18}, {"sid": {"sat": 18, "code": 0}, "az": 42, "el": 53}, {"sid": {"sat": 20, "code": 0}, "az": 31, "el": 16}, {"sid": {"sat": 23, "code": 0}, "az": 12, "el": 67}, {"sid": {"sat": 24, "code": 0}, "az": 47, "el": 10}, {"sid": {"sat": 26, "code": 0}, "az": 116, "el": 8}, {"sid": {"sat": 27, "code": 0}, "az": 153, "el": 43}, {"sid": {"sat": 29, "code": 0}, "az": 77, "el": 10}, {"sid": {"sat": 32, "code": 0}, "az": 94, "el": 26}, {"sid": {"sat": 1, "code": 3}, "az": 16, "el": 58}, {"sid": {"sat": 2, "code": 3}, "az": 108, "el": 53}, {"sid": {"sat": 8, "code": 3}, "az": 17, "el": 13}, {"sid": {"sat": 17, "code": 3}, "az": 165, "el": 40}, {"sid": {"sat": 23, "code": 3}, "az": 63, "el": 35}, {"sid": {"sat": 24, "code": 3}, "az": 41, "el": 73}, {"sid": {"sat": 20, "code": 12}, "az": 114, "el": 26}, {"sid": {"sat": 27, "code": 12}, "az": 72, "el": 54}, {"sid": {"sat": 28, "code": 12}, "az": 69, "el": 3}, {"sid": {"sat": 29, "code": 12}, "az": 158, "el": 14}, {"sid": {"sat": 30, "code": 12}, "az": 152, "el": 68}, {"sid": {"sat": 32, "code": 12}, "az": 120, "el": 82}, {"sid": {"sat": 2, "code": 14}, "az": 131, "el": 6}, {"sid": {"sat": 4, "code": 14}, "az": 27, "el": 44}, {"sid": {"sat": 5, "code": 14}, "az": 101, "el": 21}, {"sid": {"sat": 9, "code": 14}, "az": 81, "el": 65}, {"sid": {"sat": 11, "code": 14}, "az": 49, "el": 56}, {"sid": {"sat": 12, "code": 14}, "az": 59, "el": 6}, {"sid": {"sat": 30, "code": 14}, "az": 154, "el": 4}, {"sid": {"sat": 36, "code": 14}, "az": 165, "el": 62}]}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1418,3 +1430,733 @@ fn test_json2sbp_auto_check_sbp_observation_msg_sv_az_el() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_observation_msg_sv_az_el`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_observation_msg_sv_az_el() { + { + let mut payload = Cursor::new(vec![ + 85, 151, 0, 207, 121, 132, 8, 0, 160, 12, 10, 0, 139, 66, 13, 0, 16, 1, 15, 0, 24, 25, + 16, 0, 127, 18, 18, 0, 42, 53, 20, 0, 31, 16, 23, 0, 12, 67, 24, 0, 47, 10, 26, 0, 116, + 8, 27, 0, 153, 43, 29, 0, 77, 10, 32, 0, 94, 26, 1, 3, 16, 58, 2, 3, 108, 53, 8, 3, 17, + 13, 17, 3, 165, 40, 23, 3, 63, 35, 24, 3, 41, 73, 20, 12, 114, 26, 27, 12, 72, 54, 28, + 12, 69, 3, 29, 12, 158, 14, 30, 12, 152, 68, 32, 12, 120, 82, 2, 14, 131, 6, 4, 14, 27, + 44, 5, 14, 101, 21, 9, 14, 81, 65, 11, 14, 49, 56, 12, 14, 59, 6, 30, 14, 154, 4, 36, + 14, 165, 62, 168, 36, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSvAzEl( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSvAzEl(msg) => { + assert_eq!( + msg.message_type(), + 0x97, + "Incorrect message type, expected 0x97, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x79CF, + "incorrect sender id, expected 0x79CF, is {}", + sender_id + ); + assert_eq!( + msg.azel[0].az, 160, + "incorrect value for azel[0].az, expected 160, is {}", + msg.azel[0].az + ); + assert_eq!( + msg.azel[0].el, 12, + "incorrect value for azel[0].el, expected 12, is {}", + msg.azel[0].el + ); + assert_eq!( + msg.azel[0].sid.code, 0, + "incorrect value for azel[0].sid.code, expected 0, is {}", + msg.azel[0].sid.code + ); + assert_eq!( + msg.azel[0].sid.sat, 8, + "incorrect value for azel[0].sid.sat, expected 8, is {}", + msg.azel[0].sid.sat + ); + assert_eq!( + msg.azel[1].az, 139, + "incorrect value for azel[1].az, expected 139, is {}", + msg.azel[1].az + ); + assert_eq!( + msg.azel[1].el, 66, + "incorrect value for azel[1].el, expected 66, is {}", + msg.azel[1].el + ); + assert_eq!( + msg.azel[1].sid.code, 0, + "incorrect value for azel[1].sid.code, expected 0, is {}", + msg.azel[1].sid.code + ); + assert_eq!( + msg.azel[1].sid.sat, 10, + "incorrect value for azel[1].sid.sat, expected 10, is {}", + msg.azel[1].sid.sat + ); + assert_eq!( + msg.azel[2].az, 16, + "incorrect value for azel[2].az, expected 16, is {}", + msg.azel[2].az + ); + assert_eq!( + msg.azel[2].el, 1, + "incorrect value for azel[2].el, expected 1, is {}", + msg.azel[2].el + ); + assert_eq!( + msg.azel[2].sid.code, 0, + "incorrect value for azel[2].sid.code, expected 0, is {}", + msg.azel[2].sid.code + ); + assert_eq!( + msg.azel[2].sid.sat, 13, + "incorrect value for azel[2].sid.sat, expected 13, is {}", + msg.azel[2].sid.sat + ); + assert_eq!( + msg.azel[3].az, 24, + "incorrect value for azel[3].az, expected 24, is {}", + msg.azel[3].az + ); + assert_eq!( + msg.azel[3].el, 25, + "incorrect value for azel[3].el, expected 25, is {}", + msg.azel[3].el + ); + assert_eq!( + msg.azel[3].sid.code, 0, + "incorrect value for azel[3].sid.code, expected 0, is {}", + msg.azel[3].sid.code + ); + assert_eq!( + msg.azel[3].sid.sat, 15, + "incorrect value for azel[3].sid.sat, expected 15, is {}", + msg.azel[3].sid.sat + ); + assert_eq!( + msg.azel[4].az, 127, + "incorrect value for azel[4].az, expected 127, is {}", + msg.azel[4].az + ); + assert_eq!( + msg.azel[4].el, 18, + "incorrect value for azel[4].el, expected 18, is {}", + msg.azel[4].el + ); + assert_eq!( + msg.azel[4].sid.code, 0, + "incorrect value for azel[4].sid.code, expected 0, is {}", + msg.azel[4].sid.code + ); + assert_eq!( + msg.azel[4].sid.sat, 16, + "incorrect value for azel[4].sid.sat, expected 16, is {}", + msg.azel[4].sid.sat + ); + assert_eq!( + msg.azel[5].az, 42, + "incorrect value for azel[5].az, expected 42, is {}", + msg.azel[5].az + ); + assert_eq!( + msg.azel[5].el, 53, + "incorrect value for azel[5].el, expected 53, is {}", + msg.azel[5].el + ); + assert_eq!( + msg.azel[5].sid.code, 0, + "incorrect value for azel[5].sid.code, expected 0, is {}", + msg.azel[5].sid.code + ); + assert_eq!( + msg.azel[5].sid.sat, 18, + "incorrect value for azel[5].sid.sat, expected 18, is {}", + msg.azel[5].sid.sat + ); + assert_eq!( + msg.azel[6].az, 31, + "incorrect value for azel[6].az, expected 31, is {}", + msg.azel[6].az + ); + assert_eq!( + msg.azel[6].el, 16, + "incorrect value for azel[6].el, expected 16, is {}", + msg.azel[6].el + ); + assert_eq!( + msg.azel[6].sid.code, 0, + "incorrect value for azel[6].sid.code, expected 0, is {}", + msg.azel[6].sid.code + ); + assert_eq!( + msg.azel[6].sid.sat, 20, + "incorrect value for azel[6].sid.sat, expected 20, is {}", + msg.azel[6].sid.sat + ); + assert_eq!( + msg.azel[7].az, 12, + "incorrect value for azel[7].az, expected 12, is {}", + msg.azel[7].az + ); + assert_eq!( + msg.azel[7].el, 67, + "incorrect value for azel[7].el, expected 67, is {}", + msg.azel[7].el + ); + assert_eq!( + msg.azel[7].sid.code, 0, + "incorrect value for azel[7].sid.code, expected 0, is {}", + msg.azel[7].sid.code + ); + assert_eq!( + msg.azel[7].sid.sat, 23, + "incorrect value for azel[7].sid.sat, expected 23, is {}", + msg.azel[7].sid.sat + ); + assert_eq!( + msg.azel[8].az, 47, + "incorrect value for azel[8].az, expected 47, is {}", + msg.azel[8].az + ); + assert_eq!( + msg.azel[8].el, 10, + "incorrect value for azel[8].el, expected 10, is {}", + msg.azel[8].el + ); + assert_eq!( + msg.azel[8].sid.code, 0, + "incorrect value for azel[8].sid.code, expected 0, is {}", + msg.azel[8].sid.code + ); + assert_eq!( + msg.azel[8].sid.sat, 24, + "incorrect value for azel[8].sid.sat, expected 24, is {}", + msg.azel[8].sid.sat + ); + assert_eq!( + msg.azel[9].az, 116, + "incorrect value for azel[9].az, expected 116, is {}", + msg.azel[9].az + ); + assert_eq!( + msg.azel[9].el, 8, + "incorrect value for azel[9].el, expected 8, is {}", + msg.azel[9].el + ); + assert_eq!( + msg.azel[9].sid.code, 0, + "incorrect value for azel[9].sid.code, expected 0, is {}", + msg.azel[9].sid.code + ); + assert_eq!( + msg.azel[9].sid.sat, 26, + "incorrect value for azel[9].sid.sat, expected 26, is {}", + msg.azel[9].sid.sat + ); + assert_eq!( + msg.azel[10].az, 153, + "incorrect value for azel[10].az, expected 153, is {}", + msg.azel[10].az + ); + assert_eq!( + msg.azel[10].el, 43, + "incorrect value for azel[10].el, expected 43, is {}", + msg.azel[10].el + ); + assert_eq!( + msg.azel[10].sid.code, 0, + "incorrect value for azel[10].sid.code, expected 0, is {}", + msg.azel[10].sid.code + ); + assert_eq!( + msg.azel[10].sid.sat, 27, + "incorrect value for azel[10].sid.sat, expected 27, is {}", + msg.azel[10].sid.sat + ); + assert_eq!( + msg.azel[11].az, 77, + "incorrect value for azel[11].az, expected 77, is {}", + msg.azel[11].az + ); + assert_eq!( + msg.azel[11].el, 10, + "incorrect value for azel[11].el, expected 10, is {}", + msg.azel[11].el + ); + assert_eq!( + msg.azel[11].sid.code, 0, + "incorrect value for azel[11].sid.code, expected 0, is {}", + msg.azel[11].sid.code + ); + assert_eq!( + msg.azel[11].sid.sat, 29, + "incorrect value for azel[11].sid.sat, expected 29, is {}", + msg.azel[11].sid.sat + ); + assert_eq!( + msg.azel[12].az, 94, + "incorrect value for azel[12].az, expected 94, is {}", + msg.azel[12].az + ); + assert_eq!( + msg.azel[12].el, 26, + "incorrect value for azel[12].el, expected 26, is {}", + msg.azel[12].el + ); + assert_eq!( + msg.azel[12].sid.code, 0, + "incorrect value for azel[12].sid.code, expected 0, is {}", + msg.azel[12].sid.code + ); + assert_eq!( + msg.azel[12].sid.sat, 32, + "incorrect value for azel[12].sid.sat, expected 32, is {}", + msg.azel[12].sid.sat + ); + assert_eq!( + msg.azel[13].az, 16, + "incorrect value for azel[13].az, expected 16, is {}", + msg.azel[13].az + ); + assert_eq!( + msg.azel[13].el, 58, + "incorrect value for azel[13].el, expected 58, is {}", + msg.azel[13].el + ); + assert_eq!( + msg.azel[13].sid.code, 3, + "incorrect value for azel[13].sid.code, expected 3, is {}", + msg.azel[13].sid.code + ); + assert_eq!( + msg.azel[13].sid.sat, 1, + "incorrect value for azel[13].sid.sat, expected 1, is {}", + msg.azel[13].sid.sat + ); + assert_eq!( + msg.azel[14].az, 108, + "incorrect value for azel[14].az, expected 108, is {}", + msg.azel[14].az + ); + assert_eq!( + msg.azel[14].el, 53, + "incorrect value for azel[14].el, expected 53, is {}", + msg.azel[14].el + ); + assert_eq!( + msg.azel[14].sid.code, 3, + "incorrect value for azel[14].sid.code, expected 3, is {}", + msg.azel[14].sid.code + ); + assert_eq!( + msg.azel[14].sid.sat, 2, + "incorrect value for azel[14].sid.sat, expected 2, is {}", + msg.azel[14].sid.sat + ); + assert_eq!( + msg.azel[15].az, 17, + "incorrect value for azel[15].az, expected 17, is {}", + msg.azel[15].az + ); + assert_eq!( + msg.azel[15].el, 13, + "incorrect value for azel[15].el, expected 13, is {}", + msg.azel[15].el + ); + assert_eq!( + msg.azel[15].sid.code, 3, + "incorrect value for azel[15].sid.code, expected 3, is {}", + msg.azel[15].sid.code + ); + assert_eq!( + msg.azel[15].sid.sat, 8, + "incorrect value for azel[15].sid.sat, expected 8, is {}", + msg.azel[15].sid.sat + ); + assert_eq!( + msg.azel[16].az, 165, + "incorrect value for azel[16].az, expected 165, is {}", + msg.azel[16].az + ); + assert_eq!( + msg.azel[16].el, 40, + "incorrect value for azel[16].el, expected 40, is {}", + msg.azel[16].el + ); + assert_eq!( + msg.azel[16].sid.code, 3, + "incorrect value for azel[16].sid.code, expected 3, is {}", + msg.azel[16].sid.code + ); + assert_eq!( + msg.azel[16].sid.sat, 17, + "incorrect value for azel[16].sid.sat, expected 17, is {}", + msg.azel[16].sid.sat + ); + assert_eq!( + msg.azel[17].az, 63, + "incorrect value for azel[17].az, expected 63, is {}", + msg.azel[17].az + ); + assert_eq!( + msg.azel[17].el, 35, + "incorrect value for azel[17].el, expected 35, is {}", + msg.azel[17].el + ); + assert_eq!( + msg.azel[17].sid.code, 3, + "incorrect value for azel[17].sid.code, expected 3, is {}", + msg.azel[17].sid.code + ); + assert_eq!( + msg.azel[17].sid.sat, 23, + "incorrect value for azel[17].sid.sat, expected 23, is {}", + msg.azel[17].sid.sat + ); + assert_eq!( + msg.azel[18].az, 41, + "incorrect value for azel[18].az, expected 41, is {}", + msg.azel[18].az + ); + assert_eq!( + msg.azel[18].el, 73, + "incorrect value for azel[18].el, expected 73, is {}", + msg.azel[18].el + ); + assert_eq!( + msg.azel[18].sid.code, 3, + "incorrect value for azel[18].sid.code, expected 3, is {}", + msg.azel[18].sid.code + ); + assert_eq!( + msg.azel[18].sid.sat, 24, + "incorrect value for azel[18].sid.sat, expected 24, is {}", + msg.azel[18].sid.sat + ); + assert_eq!( + msg.azel[19].az, 114, + "incorrect value for azel[19].az, expected 114, is {}", + msg.azel[19].az + ); + assert_eq!( + msg.azel[19].el, 26, + "incorrect value for azel[19].el, expected 26, is {}", + msg.azel[19].el + ); + assert_eq!( + msg.azel[19].sid.code, 12, + "incorrect value for azel[19].sid.code, expected 12, is {}", + msg.azel[19].sid.code + ); + assert_eq!( + msg.azel[19].sid.sat, 20, + "incorrect value for azel[19].sid.sat, expected 20, is {}", + msg.azel[19].sid.sat + ); + assert_eq!( + msg.azel[20].az, 72, + "incorrect value for azel[20].az, expected 72, is {}", + msg.azel[20].az + ); + assert_eq!( + msg.azel[20].el, 54, + "incorrect value for azel[20].el, expected 54, is {}", + msg.azel[20].el + ); + assert_eq!( + msg.azel[20].sid.code, 12, + "incorrect value for azel[20].sid.code, expected 12, is {}", + msg.azel[20].sid.code + ); + assert_eq!( + msg.azel[20].sid.sat, 27, + "incorrect value for azel[20].sid.sat, expected 27, is {}", + msg.azel[20].sid.sat + ); + assert_eq!( + msg.azel[21].az, 69, + "incorrect value for azel[21].az, expected 69, is {}", + msg.azel[21].az + ); + assert_eq!( + msg.azel[21].el, 3, + "incorrect value for azel[21].el, expected 3, is {}", + msg.azel[21].el + ); + assert_eq!( + msg.azel[21].sid.code, 12, + "incorrect value for azel[21].sid.code, expected 12, is {}", + msg.azel[21].sid.code + ); + assert_eq!( + msg.azel[21].sid.sat, 28, + "incorrect value for azel[21].sid.sat, expected 28, is {}", + msg.azel[21].sid.sat + ); + assert_eq!( + msg.azel[22].az, 158, + "incorrect value for azel[22].az, expected 158, is {}", + msg.azel[22].az + ); + assert_eq!( + msg.azel[22].el, 14, + "incorrect value for azel[22].el, expected 14, is {}", + msg.azel[22].el + ); + assert_eq!( + msg.azel[22].sid.code, 12, + "incorrect value for azel[22].sid.code, expected 12, is {}", + msg.azel[22].sid.code + ); + assert_eq!( + msg.azel[22].sid.sat, 29, + "incorrect value for azel[22].sid.sat, expected 29, is {}", + msg.azel[22].sid.sat + ); + assert_eq!( + msg.azel[23].az, 152, + "incorrect value for azel[23].az, expected 152, is {}", + msg.azel[23].az + ); + assert_eq!( + msg.azel[23].el, 68, + "incorrect value for azel[23].el, expected 68, is {}", + msg.azel[23].el + ); + assert_eq!( + msg.azel[23].sid.code, 12, + "incorrect value for azel[23].sid.code, expected 12, is {}", + msg.azel[23].sid.code + ); + assert_eq!( + msg.azel[23].sid.sat, 30, + "incorrect value for azel[23].sid.sat, expected 30, is {}", + msg.azel[23].sid.sat + ); + assert_eq!( + msg.azel[24].az, 120, + "incorrect value for azel[24].az, expected 120, is {}", + msg.azel[24].az + ); + assert_eq!( + msg.azel[24].el, 82, + "incorrect value for azel[24].el, expected 82, is {}", + msg.azel[24].el + ); + assert_eq!( + msg.azel[24].sid.code, 12, + "incorrect value for azel[24].sid.code, expected 12, is {}", + msg.azel[24].sid.code + ); + assert_eq!( + msg.azel[24].sid.sat, 32, + "incorrect value for azel[24].sid.sat, expected 32, is {}", + msg.azel[24].sid.sat + ); + assert_eq!( + msg.azel[25].az, 131, + "incorrect value for azel[25].az, expected 131, is {}", + msg.azel[25].az + ); + assert_eq!( + msg.azel[25].el, 6, + "incorrect value for azel[25].el, expected 6, is {}", + msg.azel[25].el + ); + assert_eq!( + msg.azel[25].sid.code, 14, + "incorrect value for azel[25].sid.code, expected 14, is {}", + msg.azel[25].sid.code + ); + assert_eq!( + msg.azel[25].sid.sat, 2, + "incorrect value for azel[25].sid.sat, expected 2, is {}", + msg.azel[25].sid.sat + ); + assert_eq!( + msg.azel[26].az, 27, + "incorrect value for azel[26].az, expected 27, is {}", + msg.azel[26].az + ); + assert_eq!( + msg.azel[26].el, 44, + "incorrect value for azel[26].el, expected 44, is {}", + msg.azel[26].el + ); + assert_eq!( + msg.azel[26].sid.code, 14, + "incorrect value for azel[26].sid.code, expected 14, is {}", + msg.azel[26].sid.code + ); + assert_eq!( + msg.azel[26].sid.sat, 4, + "incorrect value for azel[26].sid.sat, expected 4, is {}", + msg.azel[26].sid.sat + ); + assert_eq!( + msg.azel[27].az, 101, + "incorrect value for azel[27].az, expected 101, is {}", + msg.azel[27].az + ); + assert_eq!( + msg.azel[27].el, 21, + "incorrect value for azel[27].el, expected 21, is {}", + msg.azel[27].el + ); + assert_eq!( + msg.azel[27].sid.code, 14, + "incorrect value for azel[27].sid.code, expected 14, is {}", + msg.azel[27].sid.code + ); + assert_eq!( + msg.azel[27].sid.sat, 5, + "incorrect value for azel[27].sid.sat, expected 5, is {}", + msg.azel[27].sid.sat + ); + assert_eq!( + msg.azel[28].az, 81, + "incorrect value for azel[28].az, expected 81, is {}", + msg.azel[28].az + ); + assert_eq!( + msg.azel[28].el, 65, + "incorrect value for azel[28].el, expected 65, is {}", + msg.azel[28].el + ); + assert_eq!( + msg.azel[28].sid.code, 14, + "incorrect value for azel[28].sid.code, expected 14, is {}", + msg.azel[28].sid.code + ); + assert_eq!( + msg.azel[28].sid.sat, 9, + "incorrect value for azel[28].sid.sat, expected 9, is {}", + msg.azel[28].sid.sat + ); + assert_eq!( + msg.azel[29].az, 49, + "incorrect value for azel[29].az, expected 49, is {}", + msg.azel[29].az + ); + assert_eq!( + msg.azel[29].el, 56, + "incorrect value for azel[29].el, expected 56, is {}", + msg.azel[29].el + ); + assert_eq!( + msg.azel[29].sid.code, 14, + "incorrect value for azel[29].sid.code, expected 14, is {}", + msg.azel[29].sid.code + ); + assert_eq!( + msg.azel[29].sid.sat, 11, + "incorrect value for azel[29].sid.sat, expected 11, is {}", + msg.azel[29].sid.sat + ); + assert_eq!( + msg.azel[30].az, 59, + "incorrect value for azel[30].az, expected 59, is {}", + msg.azel[30].az + ); + assert_eq!( + msg.azel[30].el, 6, + "incorrect value for azel[30].el, expected 6, is {}", + msg.azel[30].el + ); + assert_eq!( + msg.azel[30].sid.code, 14, + "incorrect value for azel[30].sid.code, expected 14, is {}", + msg.azel[30].sid.code + ); + assert_eq!( + msg.azel[30].sid.sat, 12, + "incorrect value for azel[30].sid.sat, expected 12, is {}", + msg.azel[30].sid.sat + ); + assert_eq!( + msg.azel[31].az, 154, + "incorrect value for azel[31].az, expected 154, is {}", + msg.azel[31].az + ); + assert_eq!( + msg.azel[31].el, 4, + "incorrect value for azel[31].el, expected 4, is {}", + msg.azel[31].el + ); + assert_eq!( + msg.azel[31].sid.code, 14, + "incorrect value for azel[31].sid.code, expected 14, is {}", + msg.azel[31].sid.code + ); + assert_eq!( + msg.azel[31].sid.sat, 30, + "incorrect value for azel[31].sid.sat, expected 30, is {}", + msg.azel[31].sid.sat + ); + assert_eq!( + msg.azel[32].az, 165, + "incorrect value for azel[32].az, expected 165, is {}", + msg.azel[32].az + ); + assert_eq!( + msg.azel[32].el, 62, + "incorrect value for azel[32].el, expected 62, is {}", + msg.azel[32].el + ); + assert_eq!( + msg.azel[32].sid.code, 14, + "incorrect value for azel[32].sid.code, expected 14, is {}", + msg.azel[32].sid.code + ); + assert_eq!( + msg.azel[32].sid.sat, 36, + "incorrect value for azel[32].sid.sat, expected 36, is {}", + msg.azel[32].sid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgSvAzEl"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_angular_rate.rs b/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_angular_rate.rs index c22e825864..e6526098ed 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_angular_rate.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_angular_rate.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_orientation_msg_angular_rate() { { @@ -62,6 +67,13 @@ fn test_auto_check_sbp_orientation_msg_angular_rate() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_orientation_msg_angular_rate() { @@ -69,14 +81,14 @@ fn test_json2sbp_auto_check_sbp_orientation_msg_angular_rate() { let json_input = r#"{"sender": 66, "msg_type": 546, "tow": 2, "crc": 18008, "length": 17, "flags": 0, "y": 5, "x": 2, "z": 2, "preamble": 85, "payload": "AgAAAAIAAAAFAAAAAgAAAAA="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -118,3 +130,80 @@ fn test_json2sbp_auto_check_sbp_orientation_msg_angular_rate() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_orientation_msg_angular_rate`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_orientation_msg_angular_rate() { + { + let mut payload = Cursor::new(vec![ + 85, 34, 2, 66, 0, 17, 2, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 88, 70, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgAngularRate( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgAngularRate(msg) => { + assert_eq!( + msg.message_type(), + 0x222, + "Incorrect message type, expected 0x222, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.tow, 2, + "incorrect value for tow, expected 2, is {}", + msg.tow + ); + assert_eq!(msg.x, 2, "incorrect value for x, expected 2, is {}", msg.x); + assert_eq!(msg.y, 5, "incorrect value for y, expected 5, is {}", msg.y); + assert_eq!(msg.z, 2, "incorrect value for z, expected 2, is {}", msg.z); + } + _ => panic!("Invalid message type! Expected a MsgAngularRate"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_euler.rs b/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_euler.rs index be6c3630ee..11aca26b39 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_euler.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_euler.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_orientation_msg_orient_euler() { { @@ -90,6 +95,13 @@ fn test_auto_check_sbp_orientation_msg_orient_euler() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_orientation_msg_orient_euler() { @@ -97,14 +109,14 @@ fn test_json2sbp_auto_check_sbp_orientation_msg_orient_euler() { let json_input = r#"{"pitch": 2, "sender": 66, "msg_type": 545, "roll": 1, "yaw": 8, "tow": 1, "roll_accuracy": 7, "crc": 57900, "length": 29, "flags": 3, "pitch_accuracy": 3, "yaw_accuracy": 7, "preamble": 85, "payload": "AQAAAAEAAAACAAAACAAAAAAA4EAAAEBAAADgQAM="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -173,3 +185,108 @@ fn test_json2sbp_auto_check_sbp_orientation_msg_orient_euler() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_orientation_msg_orient_euler`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_orientation_msg_orient_euler() { + { + let mut payload = Cursor::new(vec![ + 85, 33, 2, 66, 0, 29, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 0, 0, 224, 64, 0, + 0, 64, 64, 0, 0, 224, 64, 3, 44, 226, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgOrientEuler( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgOrientEuler(msg) => { + assert_eq!( + msg.message_type(), + 0x221, + "Incorrect message type, expected 0x221, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.flags, 3, + "incorrect value for flags, expected 3, is {}", + msg.flags + ); + assert_eq!( + msg.pitch, 2, + "incorrect value for pitch, expected 2, is {}", + msg.pitch + ); + assert!( + msg.pitch_accuracy.almost_eq(3.00000000000000000e+00), + "incorrect value for pitch_accuracy, expected 3.00000000000000000e+00, is {:e}", + msg.pitch_accuracy + ); + assert_eq!( + msg.roll, 1, + "incorrect value for roll, expected 1, is {}", + msg.roll + ); + assert!( + msg.roll_accuracy.almost_eq(7.00000000000000000e+00), + "incorrect value for roll_accuracy, expected 7.00000000000000000e+00, is {:e}", + msg.roll_accuracy + ); + assert_eq!( + msg.tow, 1, + "incorrect value for tow, expected 1, is {}", + msg.tow + ); + assert_eq!( + msg.yaw, 8, + "incorrect value for yaw, expected 8, is {}", + msg.yaw + ); + assert!( + msg.yaw_accuracy.almost_eq(7.00000000000000000e+00), + "incorrect value for yaw_accuracy, expected 7.00000000000000000e+00, is {:e}", + msg.yaw_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgOrientEuler"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_quat.rs b/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_quat.rs index fe69fa6d8d..8d1fe40140 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_quat.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_orientation_msg_orient_quat.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_orientation_msg_orient_quat() { { @@ -84,6 +89,13 @@ fn test_auto_check_sbp_orientation_msg_orient_quat() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_orientation_msg_orient_quat() { @@ -91,14 +103,14 @@ fn test_json2sbp_auto_check_sbp_orientation_msg_orient_quat() { let json_input = r#"{"sender": 66, "msg_type": 544, "tow": 0, "z_accuracy": 3, "w_accuracy": 3, "crc": 1722, "length": 37, "flags": 1, "w": 3, "y": 8, "x": 7, "z": 4, "preamble": 85, "payload": "AAAAAAMAAAAHAAAACAAAAAQAAAAAAEBAAACAQAAAAEEAAEBAAQ==", "y_accuracy": 8, "x_accuracy": 4}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -161,3 +173,102 @@ fn test_json2sbp_auto_check_sbp_orientation_msg_orient_quat() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_orientation_msg_orient_quat`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_orientation_msg_orient_quat() { + { + let mut payload = Cursor::new(vec![ + 85, 32, 2, 66, 0, 37, 0, 0, 0, 0, 3, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 4, 0, 0, 0, 0, 0, + 64, 64, 0, 0, 128, 64, 0, 0, 0, 65, 0, 0, 64, 64, 1, 186, 6, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgOrientQuat( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgOrientQuat(msg) => { + assert_eq!( + msg.message_type(), + 0x220, + "Incorrect message type, expected 0x220, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.tow, 0, + "incorrect value for tow, expected 0, is {}", + msg.tow + ); + assert_eq!(msg.w, 3, "incorrect value for w, expected 3, is {}", msg.w); + assert!( + msg.w_accuracy.almost_eq(3.00000000000000000e+00), + "incorrect value for w_accuracy, expected 3.00000000000000000e+00, is {:e}", + msg.w_accuracy + ); + assert_eq!(msg.x, 7, "incorrect value for x, expected 7, is {}", msg.x); + assert!( + msg.x_accuracy.almost_eq(4.00000000000000000e+00), + "incorrect value for x_accuracy, expected 4.00000000000000000e+00, is {:e}", + msg.x_accuracy + ); + assert_eq!(msg.y, 8, "incorrect value for y, expected 8, is {}", msg.y); + assert!( + msg.y_accuracy.almost_eq(8.00000000000000000e+00), + "incorrect value for y_accuracy, expected 8.00000000000000000e+00, is {:e}", + msg.y_accuracy + ); + assert_eq!(msg.z, 4, "incorrect value for z, expected 4, is {}", msg.z); + assert!( + msg.z_accuracy.almost_eq(3.00000000000000000e+00), + "incorrect value for z_accuracy, expected 3.00000000000000000e+00, is {:e}", + msg.z_accuracy + ); + } + _ => panic!("Invalid message type! Expected a MsgOrientQuat"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_device_monitor.rs b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_device_monitor.rs index 11f795a1c7..f195d79b67 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_device_monitor.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_device_monitor.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_piksi_msg_device_monitor() { { @@ -302,6 +307,13 @@ fn test_auto_check_sbp_piksi_msg_device_monitor() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { @@ -309,14 +321,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { let json_input = r#"{"cpu_vint": 987, "sender": 16991, "msg_type": 181, "cpu_vaux": 1789, "fe_temperature": 4776, "dev_vin": -9999, "crc": 59855, "length": 10, "cpu_temperature": 6165, "preamble": 85, "payload": "8djbA/0GFRioEg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -373,14 +385,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { let json_input = r#"{"cpu_vint": 987, "sender": 16991, "msg_type": 181, "cpu_vaux": 1790, "fe_temperature": 4776, "dev_vin": -9999, "crc": 7849, "length": 10, "cpu_temperature": 6168, "preamble": 85, "payload": "8djbA/4GGBioEg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -437,14 +449,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { let json_input = r#"{"cpu_vint": 987, "sender": 16991, "msg_type": 181, "cpu_vaux": 1789, "fe_temperature": 4776, "dev_vin": -9999, "crc": 29203, "length": 10, "cpu_temperature": 6166, "preamble": 85, "payload": "8djbA/0GFhioEg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -501,14 +513,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { let json_input = r#"{"cpu_vint": 986, "sender": 16991, "msg_type": 181, "cpu_vaux": 1788, "fe_temperature": 4776, "dev_vin": -9999, "crc": 27591, "length": 10, "cpu_temperature": 6150, "preamble": 85, "payload": "8djaA/wGBhioEg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -565,14 +577,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { let json_input = r#"{"cpu_vint": 988, "sender": 16991, "msg_type": 181, "cpu_vaux": 1789, "fe_temperature": 4776, "dev_vin": -9999, "crc": 16369, "length": 10, "cpu_temperature": 6123, "preamble": 85, "payload": "8djcA/0G6xeoEg=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -626,3 +638,396 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_device_monitor() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_piksi_msg_device_monitor`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_piksi_msg_device_monitor() { + { + let mut payload = Cursor::new(vec![ + 85, 181, 0, 95, 66, 10, 241, 216, 219, 3, 253, 6, 21, 24, 168, 18, 207, 233, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDeviceMonitor( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDeviceMonitor(msg) => { + assert_eq!( + msg.message_type(), + 0xb5, + "Incorrect message type, expected 0xb5, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x425f, + "incorrect sender id, expected 0x425f, is {}", + sender_id + ); + assert_eq!( + msg.cpu_temperature, 6165, + "incorrect value for cpu_temperature, expected 6165, is {}", + msg.cpu_temperature + ); + assert_eq!( + msg.cpu_vaux, 1789, + "incorrect value for cpu_vaux, expected 1789, is {}", + msg.cpu_vaux + ); + assert_eq!( + msg.cpu_vint, 987, + "incorrect value for cpu_vint, expected 987, is {}", + msg.cpu_vint + ); + assert_eq!( + msg.dev_vin, -9999, + "incorrect value for dev_vin, expected -9999, is {}", + msg.dev_vin + ); + assert_eq!( + msg.fe_temperature, 4776, + "incorrect value for fe_temperature, expected 4776, is {}", + msg.fe_temperature + ); + } + _ => panic!("Invalid message type! Expected a MsgDeviceMonitor"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 181, 0, 95, 66, 10, 241, 216, 219, 3, 254, 6, 24, 24, 168, 18, 169, 30, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDeviceMonitor( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDeviceMonitor(msg) => { + assert_eq!( + msg.message_type(), + 0xb5, + "Incorrect message type, expected 0xb5, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x425f, + "incorrect sender id, expected 0x425f, is {}", + sender_id + ); + assert_eq!( + msg.cpu_temperature, 6168, + "incorrect value for cpu_temperature, expected 6168, is {}", + msg.cpu_temperature + ); + assert_eq!( + msg.cpu_vaux, 1790, + "incorrect value for cpu_vaux, expected 1790, is {}", + msg.cpu_vaux + ); + assert_eq!( + msg.cpu_vint, 987, + "incorrect value for cpu_vint, expected 987, is {}", + msg.cpu_vint + ); + assert_eq!( + msg.dev_vin, -9999, + "incorrect value for dev_vin, expected -9999, is {}", + msg.dev_vin + ); + assert_eq!( + msg.fe_temperature, 4776, + "incorrect value for fe_temperature, expected 4776, is {}", + msg.fe_temperature + ); + } + _ => panic!("Invalid message type! Expected a MsgDeviceMonitor"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 181, 0, 95, 66, 10, 241, 216, 219, 3, 253, 6, 22, 24, 168, 18, 19, 114, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDeviceMonitor( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDeviceMonitor(msg) => { + assert_eq!( + msg.message_type(), + 0xb5, + "Incorrect message type, expected 0xb5, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x425f, + "incorrect sender id, expected 0x425f, is {}", + sender_id + ); + assert_eq!( + msg.cpu_temperature, 6166, + "incorrect value for cpu_temperature, expected 6166, is {}", + msg.cpu_temperature + ); + assert_eq!( + msg.cpu_vaux, 1789, + "incorrect value for cpu_vaux, expected 1789, is {}", + msg.cpu_vaux + ); + assert_eq!( + msg.cpu_vint, 987, + "incorrect value for cpu_vint, expected 987, is {}", + msg.cpu_vint + ); + assert_eq!( + msg.dev_vin, -9999, + "incorrect value for dev_vin, expected -9999, is {}", + msg.dev_vin + ); + assert_eq!( + msg.fe_temperature, 4776, + "incorrect value for fe_temperature, expected 4776, is {}", + msg.fe_temperature + ); + } + _ => panic!("Invalid message type! Expected a MsgDeviceMonitor"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 181, 0, 95, 66, 10, 241, 216, 218, 3, 252, 6, 6, 24, 168, 18, 199, 107, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDeviceMonitor( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDeviceMonitor(msg) => { + assert_eq!( + msg.message_type(), + 0xb5, + "Incorrect message type, expected 0xb5, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x425f, + "incorrect sender id, expected 0x425f, is {}", + sender_id + ); + assert_eq!( + msg.cpu_temperature, 6150, + "incorrect value for cpu_temperature, expected 6150, is {}", + msg.cpu_temperature + ); + assert_eq!( + msg.cpu_vaux, 1788, + "incorrect value for cpu_vaux, expected 1788, is {}", + msg.cpu_vaux + ); + assert_eq!( + msg.cpu_vint, 986, + "incorrect value for cpu_vint, expected 986, is {}", + msg.cpu_vint + ); + assert_eq!( + msg.dev_vin, -9999, + "incorrect value for dev_vin, expected -9999, is {}", + msg.dev_vin + ); + assert_eq!( + msg.fe_temperature, 4776, + "incorrect value for fe_temperature, expected 4776, is {}", + msg.fe_temperature + ); + } + _ => panic!("Invalid message type! Expected a MsgDeviceMonitor"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 181, 0, 95, 66, 10, 241, 216, 220, 3, 253, 6, 235, 23, 168, 18, 241, 63, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDeviceMonitor( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDeviceMonitor(msg) => { + assert_eq!( + msg.message_type(), + 0xb5, + "Incorrect message type, expected 0xb5, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x425f, + "incorrect sender id, expected 0x425f, is {}", + sender_id + ); + assert_eq!( + msg.cpu_temperature, 6123, + "incorrect value for cpu_temperature, expected 6123, is {}", + msg.cpu_temperature + ); + assert_eq!( + msg.cpu_vaux, 1789, + "incorrect value for cpu_vaux, expected 1789, is {}", + msg.cpu_vaux + ); + assert_eq!( + msg.cpu_vint, 988, + "incorrect value for cpu_vint, expected 988, is {}", + msg.cpu_vint + ); + assert_eq!( + msg.dev_vin, -9999, + "incorrect value for dev_vin, expected -9999, is {}", + msg.dev_vin + ); + assert_eq!( + msg.fe_temperature, 4776, + "incorrect value for fe_temperature, expected 4776, is {}", + msg.fe_temperature + ); + } + _ => panic!("Invalid message type! Expected a MsgDeviceMonitor"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_iar_state.rs b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_iar_state.rs index a520928827..78dcf9c19d 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_iar_state.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_iar_state.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_piksi_msg_iar_state() { { @@ -262,6 +267,13 @@ fn test_auto_check_sbp_piksi_msg_iar_state() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { @@ -269,14 +281,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 55286, "msg_type": 25, "num_hyps": 1, "crc": 36056, "length": 4, "preamble": 85, "payload": "AQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -313,14 +325,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 1219, "msg_type": 25, "num_hyps": 0, "crc": 45074, "length": 4, "preamble": 85, "payload": "AAAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -357,14 +369,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 1219, "msg_type": 25, "num_hyps": 1, "crc": 50854, "length": 4, "preamble": 85, "payload": "AQAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -401,14 +413,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 1219, "msg_type": 25, "num_hyps": 729, "crc": 34054, "length": 4, "preamble": 85, "payload": "2QIAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -445,14 +457,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 1219, "msg_type": 25, "num_hyps": 728, "crc": 62386, "length": 4, "preamble": 85, "payload": "2AIAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -489,14 +501,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 1219, "msg_type": 25, "num_hyps": 727, "crc": 10076, "length": 4, "preamble": 85, "payload": "1wIAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -533,14 +545,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { let json_input = r#"{"sender": 1219, "msg_type": 25, "num_hyps": 723, "crc": 60845, "length": 4, "preamble": 85, "payload": "0wIAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -574,3 +586,394 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_iar_state() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_piksi_msg_iar_state`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_piksi_msg_iar_state() { + { + let mut payload = Cursor::new(vec![85, 25, 0, 246, 215, 4, 1, 0, 0, 0, 216, 140]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 1, + "incorrect value for num_hyps, expected 1, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 25, 0, 195, 4, 4, 0, 0, 0, 0, 18, 176]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 0, + "incorrect value for num_hyps, expected 0, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 25, 0, 195, 4, 4, 1, 0, 0, 0, 166, 198]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 1, + "incorrect value for num_hyps, expected 1, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 25, 0, 195, 4, 4, 217, 2, 0, 0, 6, 133]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 729, + "incorrect value for num_hyps, expected 729, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 25, 0, 195, 4, 4, 216, 2, 0, 0, 178, 243]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 728, + "incorrect value for num_hyps, expected 728, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 25, 0, 195, 4, 4, 215, 2, 0, 0, 92, 39]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 727, + "incorrect value for num_hyps, expected 727, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 25, 0, 195, 4, 4, 211, 2, 0, 0, 173, 237]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgIarState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgIarState(msg) => { + assert_eq!( + msg.message_type(), + 0x19, + "Incorrect message type, expected 0x19, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.num_hyps, 723, + "incorrect value for num_hyps, expected 723, is {}", + msg.num_hyps + ); + } + _ => panic!("Invalid message type! Expected a MsgIarState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_network_bandwidth_usage.rs b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_network_bandwidth_usage.rs index 5aa5e926ea..3bee6f9bf2 100644 Binary files a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_network_bandwidth_usage.rs and b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_network_bandwidth_usage.rs differ diff --git a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_thread_state.rs b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_thread_state.rs index 1eb0f09fbd..cb87708b0f 100644 Binary files a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_thread_state.rs and b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_thread_state.rs differ diff --git a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state.rs b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state.rs index 8cbcba3d9f..9e7157f2d9 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_piksi_msg_uart_state() { { @@ -257,6 +262,13 @@ fn test_auto_check_sbp_piksi_msg_uart_state() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state() { @@ -264,14 +276,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 55286, "msg_type": 24, "crc": 31815, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 40, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 2.9718310832977295}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 24, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.8661972284317017}, "preamble": 85, "payload": "Gr9dPwAAAAAAAAAAGAB7Mj5AAAAAAAAAAAAoADYHokCxORA9AAAAAFEB/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 1, "tx_buffer_level": 81, "rx_throughput": 0.035211268812417984, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 5.063380241394043}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -389,14 +401,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 55286, "msg_type": 24, "crc": 63641, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 40, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 2.995774745941162}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 24, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.8746479153633118}, "preamble": 85, "payload": "7ehfPwAAAAAAAAAAGADGuj9AAAAAAAAAAAAoANZI2UAdSLQ+AAAAAFUB/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 1, "tx_buffer_level": 85, "rx_throughput": 0.35211268067359924, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 6.7901411056518555}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -511,3 +523,294 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_piksi_msg_uart_state`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_piksi_msg_uart_state() { + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 246, 215, 58, 26, 191, 93, 63, 0, 0, 0, 0, 0, 0, 0, 0, 24, 0, 123, 50, 62, + 64, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 54, 7, 162, 64, 177, 57, 16, 61, 0, 0, 0, 0, 81, 1, + 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 71, 124, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 24, + "incorrect value for uart_a.tx_buffer_level, expected 24, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 8.66197228431701660e-01 ), "incorrect value for uart_a.tx_throughput, expected 8.66197228431701660e-01, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 40, + "incorrect value for uart_b.tx_buffer_level, expected 40, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 2.97183108329772949e+00 ), "incorrect value for uart_b.tx_throughput, expected 2.97183108329772949e+00, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 1, + "incorrect value for uart_ftdi.rx_buffer_level, expected 1, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 3.52112688124179840e-02 ), "incorrect value for uart_ftdi.rx_throughput, expected 3.52112688124179840e-02, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 81, + "incorrect value for uart_ftdi.tx_buffer_level, expected 81, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 5.06338024139404297e+00 ), "incorrect value for uart_ftdi.tx_throughput, expected 5.06338024139404297e+00, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 246, 215, 58, 237, 232, 95, 63, 0, 0, 0, 0, 0, 0, 0, 0, 24, 0, 198, 186, 63, + 64, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 214, 72, 217, 64, 29, 72, 180, 62, 0, 0, 0, 0, 85, + 1, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 153, 248, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 24, + "incorrect value for uart_a.tx_buffer_level, expected 24, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 8.74647915363311768e-01 ), "incorrect value for uart_a.tx_throughput, expected 8.74647915363311768e-01, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 40, + "incorrect value for uart_b.tx_buffer_level, expected 40, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 2.99577474594116211e+00 ), "incorrect value for uart_b.tx_throughput, expected 2.99577474594116211e+00, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 1, + "incorrect value for uart_ftdi.rx_buffer_level, expected 1, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 3.52112680673599243e-01 ), "incorrect value for uart_ftdi.rx_throughput, expected 3.52112680673599243e-01, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 85, + "incorrect value for uart_ftdi.tx_buffer_level, expected 85, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 6.79014110565185547e+00 ), "incorrect value for uart_ftdi.tx_throughput, expected 6.79014110565185547e+00, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state_dep_a.rs index f87a3acfad..4137ac2ab5 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_piksi_msg_uart_state_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_piksi_msg_uart_state_dep_a() { { @@ -737,6 +742,13 @@ fn test_auto_check_sbp_piksi_msg_uart_state_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { @@ -744,14 +756,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 1219, "msg_type": 24, "crc": 1527, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "preamble": 85, "payload": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJqZOUEAAAAAAAAAAA8A/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 0, "tx_buffer_level": 15, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 11.600000381469727}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -869,14 +881,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 1219, "msg_type": 24, "crc": 28225, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "preamble": 85, "payload": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIrhz0AAAAAAAAAAAAA/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.06599999964237213}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -994,14 +1006,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 1219, "msg_type": 24, "crc": 9414, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "preamble": 85, "payload": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARWDj4AAAAAAAAAAAoA/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 0, "tx_buffer_level": 10, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.13899999856948853}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1119,14 +1131,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 1219, "msg_type": 24, "crc": 28225, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "preamble": 85, "payload": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIrhz0AAAAAAAAAAAAA/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.06599999964237213}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1244,14 +1256,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 1219, "msg_type": 24, "crc": 28528, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 2, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.09836065769195557}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 0, "rx_throughput": 0.008196720853447914, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.0}, "preamble": 85, "payload": "AAAAAIpLBjwAAAAAAABQcck9AAAAAAAAAAACAJHt/D4AAAAAAAAAACYA/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 0, "tx_buffer_level": 38, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.49399998784065247}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1369,14 +1381,14 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { let json_input = r#"{"latency": {"current": -1, "lmax": 0, "avg": -1, "lmin": 0}, "sender": 1219, "msg_type": 24, "crc": 18454, "length": 58, "uart_b": {"rx_buffer_level": 0, "tx_buffer_level": 2, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.012000000104308128}, "uart_a": {"rx_buffer_level": 0, "tx_buffer_level": 2, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 0.012000000104308128}, "preamble": 85, "payload": "pptEPAAAAAAAAAAAAgCmm0Q8AAAAAAAAAAACAOxRqD8AAAAAAAAAADIA/////wAAAAAAAAAA/////w==", "uart_ftdi": {"rx_buffer_level": 0, "tx_buffer_level": 50, "rx_throughput": 0.0, "crc_error_count": 0, "io_error_count": 0, "tx_throughput": 1.315000057220459}}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1491,3 +1503,850 @@ fn test_json2sbp_auto_check_sbp_piksi_msg_uart_state_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_piksi_msg_uart_state_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_piksi_msg_uart_state_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 195, 4, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 154, 153, 57, 65, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 255, 255, 255, + 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 247, 5, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 0, + "incorrect value for uart_a.tx_buffer_level, expected 0, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 0, + "incorrect value for uart_b.tx_buffer_level, expected 0, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 0, + "incorrect value for uart_ftdi.rx_buffer_level, expected 0, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_ftdi.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 15, + "incorrect value for uart_ftdi.tx_buffer_level, expected 15, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 1.16000003814697266e+01 ), "incorrect value for uart_ftdi.tx_throughput, expected 1.16000003814697266e+01, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 195, 4, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 2, 43, 135, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, + 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 65, 110, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 0, + "incorrect value for uart_a.tx_buffer_level, expected 0, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 0, + "incorrect value for uart_b.tx_buffer_level, expected 0, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 0, + "incorrect value for uart_ftdi.rx_buffer_level, expected 0, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_ftdi.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 0, + "incorrect value for uart_ftdi.tx_buffer_level, expected 0, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 6.59999996423721313e-02 ), "incorrect value for uart_ftdi.tx_throughput, expected 6.59999996423721313e-02, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 195, 4, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 4, 86, 14, 62, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 255, 255, 255, 255, + 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 198, 36, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 0, + "incorrect value for uart_a.tx_buffer_level, expected 0, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 0, + "incorrect value for uart_b.tx_buffer_level, expected 0, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 0, + "incorrect value for uart_ftdi.rx_buffer_level, expected 0, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_ftdi.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 10, + "incorrect value for uart_ftdi.tx_buffer_level, expected 10, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 1.38999998569488525e-01 ), "incorrect value for uart_ftdi.tx_throughput, expected 1.38999998569488525e-01, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 195, 4, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 2, 43, 135, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, + 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 65, 110, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 0, + "incorrect value for uart_a.tx_buffer_level, expected 0, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 0, + "incorrect value for uart_b.tx_buffer_level, expected 0, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 0, + "incorrect value for uart_ftdi.rx_buffer_level, expected 0, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_ftdi.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 0, + "incorrect value for uart_ftdi.tx_buffer_level, expected 0, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 6.59999996423721313e-02 ), "incorrect value for uart_ftdi.tx_throughput, expected 6.59999996423721313e-02, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 195, 4, 58, 0, 0, 0, 0, 138, 75, 6, 60, 0, 0, 0, 0, 0, 0, 80, 113, 201, 61, + 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 145, 237, 252, 62, 0, 0, 0, 0, 0, 0, 0, 0, 38, 0, 255, + 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 112, 111, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 8.19672085344791412e-03 ), "incorrect value for uart_a.rx_throughput, expected 8.19672085344791412e-03, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 0, + "incorrect value for uart_a.tx_buffer_level, expected 0, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.tx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 2, + "incorrect value for uart_b.tx_buffer_level, expected 2, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 9.83606576919555664e-02 ), "incorrect value for uart_b.tx_throughput, expected 9.83606576919555664e-02, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 0, + "incorrect value for uart_ftdi.rx_buffer_level, expected 0, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_ftdi.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 38, + "incorrect value for uart_ftdi.tx_buffer_level, expected 38, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 4.93999987840652466e-01 ), "incorrect value for uart_ftdi.tx_throughput, expected 4.93999987840652466e-01, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 24, 0, 195, 4, 58, 166, 155, 68, 60, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 166, 155, 68, + 60, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 236, 81, 168, 63, 0, 0, 0, 0, 0, 0, 0, 0, 50, 0, 255, + 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 22, 72, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgUartStateDepa( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgUartStateDepa(msg) => { + assert_eq!( + msg.message_type(), + 0x18, + "Incorrect message type, expected 0x18, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.latency.avg, -1, + "incorrect value for latency.avg, expected -1, is {}", + msg.latency.avg + ); + assert_eq!( + msg.latency.current, -1, + "incorrect value for latency.current, expected -1, is {}", + msg.latency.current + ); + assert_eq!( + msg.latency.lmax, 0, + "incorrect value for latency.lmax, expected 0, is {}", + msg.latency.lmax + ); + assert_eq!( + msg.latency.lmin, 0, + "incorrect value for latency.lmin, expected 0, is {}", + msg.latency.lmin + ); + assert_eq!( + msg.uart_a.crc_error_count, 0, + "incorrect value for uart_a.crc_error_count, expected 0, is {}", + msg.uart_a.crc_error_count + ); + assert_eq!( + msg.uart_a.io_error_count, 0, + "incorrect value for uart_a.io_error_count, expected 0, is {}", + msg.uart_a.io_error_count + ); + assert_eq!( + msg.uart_a.rx_buffer_level, 0, + "incorrect value for uart_a.rx_buffer_level, expected 0, is {}", + msg.uart_a.rx_buffer_level + ); + assert!(msg.uart_a.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_a.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_a.rx_throughput); + assert_eq!( + msg.uart_a.tx_buffer_level, 2, + "incorrect value for uart_a.tx_buffer_level, expected 2, is {}", + msg.uart_a.tx_buffer_level + ); + assert!(msg.uart_a.tx_throughput.almost_eq( 1.20000001043081284e-02 ), "incorrect value for uart_a.tx_throughput, expected 1.20000001043081284e-02, is {:e}", msg.uart_a.tx_throughput); + assert_eq!( + msg.uart_b.crc_error_count, 0, + "incorrect value for uart_b.crc_error_count, expected 0, is {}", + msg.uart_b.crc_error_count + ); + assert_eq!( + msg.uart_b.io_error_count, 0, + "incorrect value for uart_b.io_error_count, expected 0, is {}", + msg.uart_b.io_error_count + ); + assert_eq!( + msg.uart_b.rx_buffer_level, 0, + "incorrect value for uart_b.rx_buffer_level, expected 0, is {}", + msg.uart_b.rx_buffer_level + ); + assert!(msg.uart_b.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_b.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_b.rx_throughput); + assert_eq!( + msg.uart_b.tx_buffer_level, 2, + "incorrect value for uart_b.tx_buffer_level, expected 2, is {}", + msg.uart_b.tx_buffer_level + ); + assert!(msg.uart_b.tx_throughput.almost_eq( 1.20000001043081284e-02 ), "incorrect value for uart_b.tx_throughput, expected 1.20000001043081284e-02, is {:e}", msg.uart_b.tx_throughput); + assert_eq!( + msg.uart_ftdi.crc_error_count, 0, + "incorrect value for uart_ftdi.crc_error_count, expected 0, is {}", + msg.uart_ftdi.crc_error_count + ); + assert_eq!( + msg.uart_ftdi.io_error_count, 0, + "incorrect value for uart_ftdi.io_error_count, expected 0, is {}", + msg.uart_ftdi.io_error_count + ); + assert_eq!( + msg.uart_ftdi.rx_buffer_level, 0, + "incorrect value for uart_ftdi.rx_buffer_level, expected 0, is {}", + msg.uart_ftdi.rx_buffer_level + ); + assert!(msg.uart_ftdi.rx_throughput.almost_eq( 0.00000000000000000e+00 ), "incorrect value for uart_ftdi.rx_throughput, expected 0.00000000000000000e+00, is {:e}", msg.uart_ftdi.rx_throughput); + assert_eq!( + msg.uart_ftdi.tx_buffer_level, 50, + "incorrect value for uart_ftdi.tx_buffer_level, expected 50, is {}", + msg.uart_ftdi.tx_buffer_level + ); + assert!(msg.uart_ftdi.tx_throughput.almost_eq( 1.31500005722045898e+00 ), "incorrect value for uart_ftdi.tx_throughput, expected 1.31500005722045898e+00, is {:e}", msg.uart_ftdi.tx_throughput); + } + _ => panic!("Invalid message type! Expected a MsgUartStateDepa"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_sbas_msg_sbas_raw.rs b/rust/sbp/tests/integration/auto_check_sbp_sbas_msg_sbas_raw.rs index 3e0fbda711..0923a9fc30 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_sbas_msg_sbas_raw.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_sbas_msg_sbas_raw.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_sbas_msg_sbas_raw() { { @@ -206,6 +211,13 @@ fn test_auto_check_sbp_sbas_msg_sbas_raw() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_sbas_msg_sbas_raw() { @@ -213,14 +225,14 @@ fn test_json2sbp_auto_check_sbp_sbas_msg_sbas_raw() { let json_input = r#"{"sid":{"sat":131,"code":2},"tow":501867721,"message_type":4,"data":[23,255,0,23,255,0,23,255,127,240,2,255,192,3,127,247,255,127,247,255,229,229,238,170,175,255,240],"preamble":85,"msg_type":30583,"sender":51228,"payload":"gwLJ5OkdBBf/ABf/ABf/f/AC/8ADf/f/f/f/5eXuqq//8A==","crc":3751,"length":34}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -404,3 +416,224 @@ fn test_json2sbp_auto_check_sbp_sbas_msg_sbas_raw() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_sbas_msg_sbas_raw`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_sbas_msg_sbas_raw() { + { + let mut payload = Cursor::new(vec![ + 85, 119, 119, 28, 200, 34, 131, 2, 201, 228, 233, 29, 4, 23, 255, 0, 23, 255, 0, 23, + 255, 127, 240, 2, 255, 192, 3, 127, 247, 255, 127, 247, 255, 229, 229, 238, 170, 175, + 255, 240, 167, 14, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSbasRaw( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSbasRaw(msg) => { + assert_eq!( + msg.message_type(), + 0x7777, + "Incorrect message type, expected 0x7777, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xc81c, + "incorrect sender id, expected 0xc81c, is {}", + sender_id + ); + assert_eq!( + msg.data[0], 23, + "incorrect value for data[0], expected 23, is {}", + msg.data[0] + ); + assert_eq!( + msg.data[1], 255, + "incorrect value for data[1], expected 255, is {}", + msg.data[1] + ); + assert_eq!( + msg.data[2], 0, + "incorrect value for data[2], expected 0, is {}", + msg.data[2] + ); + assert_eq!( + msg.data[3], 23, + "incorrect value for data[3], expected 23, is {}", + msg.data[3] + ); + assert_eq!( + msg.data[4], 255, + "incorrect value for data[4], expected 255, is {}", + msg.data[4] + ); + assert_eq!( + msg.data[5], 0, + "incorrect value for data[5], expected 0, is {}", + msg.data[5] + ); + assert_eq!( + msg.data[6], 23, + "incorrect value for data[6], expected 23, is {}", + msg.data[6] + ); + assert_eq!( + msg.data[7], 255, + "incorrect value for data[7], expected 255, is {}", + msg.data[7] + ); + assert_eq!( + msg.data[8], 127, + "incorrect value for data[8], expected 127, is {}", + msg.data[8] + ); + assert_eq!( + msg.data[9], 240, + "incorrect value for data[9], expected 240, is {}", + msg.data[9] + ); + assert_eq!( + msg.data[10], 2, + "incorrect value for data[10], expected 2, is {}", + msg.data[10] + ); + assert_eq!( + msg.data[11], 255, + "incorrect value for data[11], expected 255, is {}", + msg.data[11] + ); + assert_eq!( + msg.data[12], 192, + "incorrect value for data[12], expected 192, is {}", + msg.data[12] + ); + assert_eq!( + msg.data[13], 3, + "incorrect value for data[13], expected 3, is {}", + msg.data[13] + ); + assert_eq!( + msg.data[14], 127, + "incorrect value for data[14], expected 127, is {}", + msg.data[14] + ); + assert_eq!( + msg.data[15], 247, + "incorrect value for data[15], expected 247, is {}", + msg.data[15] + ); + assert_eq!( + msg.data[16], 255, + "incorrect value for data[16], expected 255, is {}", + msg.data[16] + ); + assert_eq!( + msg.data[17], 127, + "incorrect value for data[17], expected 127, is {}", + msg.data[17] + ); + assert_eq!( + msg.data[18], 247, + "incorrect value for data[18], expected 247, is {}", + msg.data[18] + ); + assert_eq!( + msg.data[19], 255, + "incorrect value for data[19], expected 255, is {}", + msg.data[19] + ); + assert_eq!( + msg.data[20], 229, + "incorrect value for data[20], expected 229, is {}", + msg.data[20] + ); + assert_eq!( + msg.data[21], 229, + "incorrect value for data[21], expected 229, is {}", + msg.data[21] + ); + assert_eq!( + msg.data[22], 238, + "incorrect value for data[22], expected 238, is {}", + msg.data[22] + ); + assert_eq!( + msg.data[23], 170, + "incorrect value for data[23], expected 170, is {}", + msg.data[23] + ); + assert_eq!( + msg.data[24], 175, + "incorrect value for data[24], expected 175, is {}", + msg.data[24] + ); + assert_eq!( + msg.data[25], 255, + "incorrect value for data[25], expected 255, is {}", + msg.data[25] + ); + assert_eq!( + msg.data[26], 240, + "incorrect value for data[26], expected 240, is {}", + msg.data[26] + ); + assert_eq!( + msg.message_type, 4, + "incorrect value for message_type, expected 4, is {}", + msg.message_type + ); + assert_eq!( + msg.sid.code, 2, + "incorrect value for sid.code, expected 2, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.sat, 131, + "incorrect value for sid.sat, expected 131, is {}", + msg.sid.sat + ); + assert_eq!( + msg.tow, 501867721, + "incorrect value for tow, expected 501867721, is {}", + msg.tow + ); + } + _ => panic!("Invalid message type! Expected a MsgSbasRaw"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_done.rs b/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_done.rs index ecf5b7d78f..0ff5a417e6 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_done.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_done.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_settings_msg_settings_read_by_index_done() { { @@ -47,6 +52,13 @@ fn test_auto_check_sbp_settings_msg_settings_read_by_index_done() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_settings_msg_settings_read_by_index_done() { @@ -54,14 +66,14 @@ fn test_json2sbp_auto_check_sbp_settings_msg_settings_read_by_index_done() { let json_input = r#"{"sender": 55286, "msg_type": 166, "crc": 15011, "length": 0, "preamble": 85, "payload": ""}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -90,3 +102,65 @@ fn test_json2sbp_auto_check_sbp_settings_msg_settings_read_by_index_done() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_settings_msg_settings_read_by_index_done`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_settings_msg_settings_read_by_index_done() { + { + let mut payload = Cursor::new(vec![85, 166, 0, 246, 215, 0, 163, 58]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSettingsReadByIndexDone( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSettingsReadByIndexDone(msg) => { + assert_eq!( + msg.message_type(), + 0xa6, + "Incorrect message type, expected 0xa6, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + } + _ => panic!("Invalid message type! Expected a MsgSettingsReadByIndexDone"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_resp.rs b/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_resp.rs index 7d909f9af4..8f96eb2598 100644 Binary files a/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_resp.rs and b/rust/sbp/tests/integration/auto_check_sbp_settings_msg_settings_read_by_index_resp.rs differ diff --git a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds.rs b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds.rs index 907a02ee41..bd1532baf4 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds() { { @@ -137,6 +142,13 @@ fn test_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds() { @@ -144,14 +156,14 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds() { let json_input = r#"{"header": {"time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "update_interval": 1, "sol_id": 14}, "ssr_iod": 15, "const_id": 1, "n_sats_signals": 3, "satellites_signals": [{ "sat_id": 0, "signal_id": 3, "code_bias_bound_mu": 39, "code_bias_bound_sig": 1, "phase_bias_bound_mu": 39, "phase_bias_bound_sig": 1}, {"sat_id": 1, "signal_id": 3, "code_bias_bound_mu": 39, "code_bias_bound_sig": 1, "phase_bias_bound_mu": 39, "phase_bias_bound_sig": 1}, {"sat_id": 1, "signal_id": 1, "code_bias_bound_mu": 39, "code_bias_bound_sig": 1, "phase_bias_bound_mu": 39, "phase_bias_bound_sig": 1}], "preamble": 85, "msg_type": 1516, "sender": 66, "length": 31, "payload": "tAAAAAMAAQIBDg8BAwADJwEnAQEDJwEnAQEBJwEnAQ==", "crc": 28951}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -267,3 +279,155 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ssr_msg_ssr_code_phase_biases_bounds() { + { + let mut payload = Cursor::new(vec![ + 85, 236, 5, 66, 0, 31, 180, 0, 0, 0, 3, 0, 1, 2, 1, 14, 15, 1, 3, 0, 3, 39, 1, 39, 1, + 1, 3, 39, 1, 39, 1, 1, 1, 39, 1, 39, 1, 23, 113, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrCodePhaseBiasesBounds( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrCodePhaseBiasesBounds(msg) => { + assert_eq!( + msg.message_type(), + 1516, + "Incorrect message type, expected 1516, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.const_id, 1, + "incorrect value for const_id, expected 1, is {}", + msg.const_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.sol_id, 14, + "incorrect value for header.sol_id, expected 14, is {}", + msg.header.sol_id + ); + assert_eq!( + msg.header.time.tow, 180, + "incorrect value for header.time.tow, expected 180, is {}", + msg.header.time.tow + ); + assert_eq!( + msg.header.time.wn, 3, + "incorrect value for header.time.wn, expected 3, is {}", + msg.header.time.wn + ); + assert_eq!( + msg.header.update_interval, 1, + "incorrect value for header.update_interval, expected 1, is {}", + msg.header.update_interval + ); + assert_eq!( + msg.n_sats_signals, 3, + "incorrect value for n_sats_signals, expected 3, is {}", + msg.n_sats_signals + ); + assert_eq!(msg.satellites_signals[0].code_bias_bound_mu, 39, "incorrect value for satellites_signals[0].code_bias_bound_mu, expected 39, is {}", msg.satellites_signals[0].code_bias_bound_mu); + assert_eq!(msg.satellites_signals[0].code_bias_bound_sig, 1, "incorrect value for satellites_signals[0].code_bias_bound_sig, expected 1, is {}", msg.satellites_signals[0].code_bias_bound_sig); + assert_eq!(msg.satellites_signals[0].phase_bias_bound_mu, 39, "incorrect value for satellites_signals[0].phase_bias_bound_mu, expected 39, is {}", msg.satellites_signals[0].phase_bias_bound_mu); + assert_eq!(msg.satellites_signals[0].phase_bias_bound_sig, 1, "incorrect value for satellites_signals[0].phase_bias_bound_sig, expected 1, is {}", msg.satellites_signals[0].phase_bias_bound_sig); + assert_eq!( + msg.satellites_signals[0].sat_id, 0, + "incorrect value for satellites_signals[0].sat_id, expected 0, is {}", + msg.satellites_signals[0].sat_id + ); + assert_eq!( + msg.satellites_signals[0].signal_id, 3, + "incorrect value for satellites_signals[0].signal_id, expected 3, is {}", + msg.satellites_signals[0].signal_id + ); + assert_eq!(msg.satellites_signals[1].code_bias_bound_mu, 39, "incorrect value for satellites_signals[1].code_bias_bound_mu, expected 39, is {}", msg.satellites_signals[1].code_bias_bound_mu); + assert_eq!(msg.satellites_signals[1].code_bias_bound_sig, 1, "incorrect value for satellites_signals[1].code_bias_bound_sig, expected 1, is {}", msg.satellites_signals[1].code_bias_bound_sig); + assert_eq!(msg.satellites_signals[1].phase_bias_bound_mu, 39, "incorrect value for satellites_signals[1].phase_bias_bound_mu, expected 39, is {}", msg.satellites_signals[1].phase_bias_bound_mu); + assert_eq!(msg.satellites_signals[1].phase_bias_bound_sig, 1, "incorrect value for satellites_signals[1].phase_bias_bound_sig, expected 1, is {}", msg.satellites_signals[1].phase_bias_bound_sig); + assert_eq!( + msg.satellites_signals[1].sat_id, 1, + "incorrect value for satellites_signals[1].sat_id, expected 1, is {}", + msg.satellites_signals[1].sat_id + ); + assert_eq!( + msg.satellites_signals[1].signal_id, 3, + "incorrect value for satellites_signals[1].signal_id, expected 3, is {}", + msg.satellites_signals[1].signal_id + ); + assert_eq!(msg.satellites_signals[2].code_bias_bound_mu, 39, "incorrect value for satellites_signals[2].code_bias_bound_mu, expected 39, is {}", msg.satellites_signals[2].code_bias_bound_mu); + assert_eq!(msg.satellites_signals[2].code_bias_bound_sig, 1, "incorrect value for satellites_signals[2].code_bias_bound_sig, expected 1, is {}", msg.satellites_signals[2].code_bias_bound_sig); + assert_eq!(msg.satellites_signals[2].phase_bias_bound_mu, 39, "incorrect value for satellites_signals[2].phase_bias_bound_mu, expected 39, is {}", msg.satellites_signals[2].phase_bias_bound_mu); + assert_eq!(msg.satellites_signals[2].phase_bias_bound_sig, 1, "incorrect value for satellites_signals[2].phase_bias_bound_sig, expected 1, is {}", msg.satellites_signals[2].phase_bias_bound_sig); + assert_eq!( + msg.satellites_signals[2].sat_id, 1, + "incorrect value for satellites_signals[2].sat_id, expected 1, is {}", + msg.satellites_signals[2].sat_id + ); + assert_eq!( + msg.satellites_signals[2].signal_id, 1, + "incorrect value for satellites_signals[2].signal_id, expected 1, is {}", + msg.satellites_signals[2].signal_id + ); + assert_eq!( + msg.ssr_iod, 15, + "incorrect value for ssr_iod, expected 15, is {}", + msg.ssr_iod + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrCodePhaseBiasesBounds"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds.rs b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds.rs index 734efcaa83..8bb4660711 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds() { { @@ -202,6 +207,13 @@ fn test_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds() { @@ -209,14 +221,14 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds() { let json_input = r#"{"header": {"time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 1, "update_interval": 10, "sol_id": 0}, "ssr_iod_atmo": 15, "tile_set_id": 1, "tile_id": 10, "tropo_qi": 39, "grid_point_id": 1000, "tropo_delay_correction": {"hydro": 500, "wet": 100, "stddev": 200}, "tropo_v_hydro_bound_mu": 150, "tropo_v_hydro_bound_sig": 100, "tropo_v_wet_bound_mu": 150, "tropo_v_wet_bound_sig": 100, "n_sats": 2, "stec_sat_list": [{"stec_residual": {"sv_id": {"satId": 5, "constellation": 10}, "residual": 16, "stddev": 17}, "stec_bound_mu": 18, "stec_bound_sig": 19, "stec_bound_mu_dot": 20, "stec_bound_sig_dot": 21}, {"stec_residual": {"sv_id": {"satId": 6, "constellation": 10}, "residual": 22, "stddev": 23}, "stec_bound_mu": 24, "stec_bound_sig": 25, "stec_bound_mu_dot": 26, "stec_bound_sig_dot": 27}], "preamble": 85, "msg_type": 1534, "sender": 66, "length": 45, "payload": "tAAAAAMAAQEKAA8BAAoAJ+gD9AFkyJZklmQCBQoQABESExQVBgoWABcYGRob", "crc": 21111}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -396,3 +408,220 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ssr_msg_ssr_gridded_correction_bounds() { + { + let mut payload = Cursor::new(vec![ + 85, 254, 5, 66, 0, 45, 180, 0, 0, 0, 3, 0, 1, 1, 10, 0, 15, 1, 0, 10, 0, 39, 232, 3, + 244, 1, 100, 200, 150, 100, 150, 100, 2, 5, 10, 16, 0, 17, 18, 19, 20, 21, 6, 10, 22, + 0, 23, 24, 25, 26, 27, 119, 82, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrGriddedCorrectionBounds( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrGriddedCorrectionBounds(msg) => { + assert_eq!( + msg.message_type(), + 1534, + "Incorrect message type, expected 1534, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.grid_point_id, 1000, + "incorrect value for grid_point_id, expected 1000, is {}", + msg.grid_point_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.seq_num, 1, + "incorrect value for header.seq_num, expected 1, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.sol_id, 0, + "incorrect value for header.sol_id, expected 0, is {}", + msg.header.sol_id + ); + assert_eq!( + msg.header.time.tow, 180, + "incorrect value for header.time.tow, expected 180, is {}", + msg.header.time.tow + ); + assert_eq!( + msg.header.time.wn, 3, + "incorrect value for header.time.wn, expected 3, is {}", + msg.header.time.wn + ); + assert_eq!( + msg.header.update_interval, 10, + "incorrect value for header.update_interval, expected 10, is {}", + msg.header.update_interval + ); + assert_eq!( + msg.n_sats, 2, + "incorrect value for n_sats, expected 2, is {}", + msg.n_sats + ); + assert_eq!( + msg.ssr_iod_atmo, 15, + "incorrect value for ssr_iod_atmo, expected 15, is {}", + msg.ssr_iod_atmo + ); + assert_eq!( + msg.stec_sat_list[0].stec_bound_mu, 18, + "incorrect value for stec_sat_list[0].stec_bound_mu, expected 18, is {}", + msg.stec_sat_list[0].stec_bound_mu + ); + assert_eq!( + msg.stec_sat_list[0].stec_bound_mu_dot, 20, + "incorrect value for stec_sat_list[0].stec_bound_mu_dot, expected 20, is {}", + msg.stec_sat_list[0].stec_bound_mu_dot + ); + assert_eq!( + msg.stec_sat_list[0].stec_bound_sig, 19, + "incorrect value for stec_sat_list[0].stec_bound_sig, expected 19, is {}", + msg.stec_sat_list[0].stec_bound_sig + ); + assert_eq!( + msg.stec_sat_list[0].stec_bound_sig_dot, 21, + "incorrect value for stec_sat_list[0].stec_bound_sig_dot, expected 21, is {}", + msg.stec_sat_list[0].stec_bound_sig_dot + ); + assert_eq!(msg.stec_sat_list[0].stec_residual.residual, 16, "incorrect value for stec_sat_list[0].stec_residual.residual, expected 16, is {}", msg.stec_sat_list[0].stec_residual.residual); + assert_eq!( + msg.stec_sat_list[0].stec_residual.stddev, 17, + "incorrect value for stec_sat_list[0].stec_residual.stddev, expected 17, is {}", + msg.stec_sat_list[0].stec_residual.stddev + ); + assert_eq!(msg.stec_sat_list[0].stec_residual.sv_id.constellation, 10, "incorrect value for stec_sat_list[0].stec_residual.sv_id.constellation, expected 10, is {}", msg.stec_sat_list[0].stec_residual.sv_id.constellation); + assert_eq!(msg.stec_sat_list[0].stec_residual.sv_id.sat_id, 5, "incorrect value for stec_sat_list[0].stec_residual.sv_id.sat_id, expected 5, is {}", msg.stec_sat_list[0].stec_residual.sv_id.sat_id); + assert_eq!( + msg.stec_sat_list[1].stec_bound_mu, 24, + "incorrect value for stec_sat_list[1].stec_bound_mu, expected 24, is {}", + msg.stec_sat_list[1].stec_bound_mu + ); + assert_eq!( + msg.stec_sat_list[1].stec_bound_mu_dot, 26, + "incorrect value for stec_sat_list[1].stec_bound_mu_dot, expected 26, is {}", + msg.stec_sat_list[1].stec_bound_mu_dot + ); + assert_eq!( + msg.stec_sat_list[1].stec_bound_sig, 25, + "incorrect value for stec_sat_list[1].stec_bound_sig, expected 25, is {}", + msg.stec_sat_list[1].stec_bound_sig + ); + assert_eq!( + msg.stec_sat_list[1].stec_bound_sig_dot, 27, + "incorrect value for stec_sat_list[1].stec_bound_sig_dot, expected 27, is {}", + msg.stec_sat_list[1].stec_bound_sig_dot + ); + assert_eq!(msg.stec_sat_list[1].stec_residual.residual, 22, "incorrect value for stec_sat_list[1].stec_residual.residual, expected 22, is {}", msg.stec_sat_list[1].stec_residual.residual); + assert_eq!( + msg.stec_sat_list[1].stec_residual.stddev, 23, + "incorrect value for stec_sat_list[1].stec_residual.stddev, expected 23, is {}", + msg.stec_sat_list[1].stec_residual.stddev + ); + assert_eq!(msg.stec_sat_list[1].stec_residual.sv_id.constellation, 10, "incorrect value for stec_sat_list[1].stec_residual.sv_id.constellation, expected 10, is {}", msg.stec_sat_list[1].stec_residual.sv_id.constellation); + assert_eq!(msg.stec_sat_list[1].stec_residual.sv_id.sat_id, 6, "incorrect value for stec_sat_list[1].stec_residual.sv_id.sat_id, expected 6, is {}", msg.stec_sat_list[1].stec_residual.sv_id.sat_id); + assert_eq!( + msg.tile_id, 10, + "incorrect value for tile_id, expected 10, is {}", + msg.tile_id + ); + assert_eq!( + msg.tile_set_id, 1, + "incorrect value for tile_set_id, expected 1, is {}", + msg.tile_set_id + ); + assert_eq!( + msg.tropo_delay_correction.hydro, 500, + "incorrect value for tropo_delay_correction.hydro, expected 500, is {}", + msg.tropo_delay_correction.hydro + ); + assert_eq!( + msg.tropo_delay_correction.stddev, 200, + "incorrect value for tropo_delay_correction.stddev, expected 200, is {}", + msg.tropo_delay_correction.stddev + ); + assert_eq!( + msg.tropo_delay_correction.wet, 100, + "incorrect value for tropo_delay_correction.wet, expected 100, is {}", + msg.tropo_delay_correction.wet + ); + assert_eq!( + msg.tropo_qi, 39, + "incorrect value for tropo_qi, expected 39, is {}", + msg.tropo_qi + ); + assert_eq!( + msg.tropo_v_hydro_bound_mu, 150, + "incorrect value for tropo_v_hydro_bound_mu, expected 150, is {}", + msg.tropo_v_hydro_bound_mu + ); + assert_eq!( + msg.tropo_v_hydro_bound_sig, 100, + "incorrect value for tropo_v_hydro_bound_sig, expected 100, is {}", + msg.tropo_v_hydro_bound_sig + ); + assert_eq!( + msg.tropo_v_wet_bound_mu, 150, + "incorrect value for tropo_v_wet_bound_mu, expected 150, is {}", + msg.tropo_v_wet_bound_mu + ); + assert_eq!( + msg.tropo_v_wet_bound_sig, 100, + "incorrect value for tropo_v_wet_bound_sig, expected 100, is {}", + msg.tropo_v_wet_bound_sig + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrGriddedCorrectionBounds"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds.rs b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds.rs index 5b3554cd6a..ad4454f844 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds() { { @@ -137,6 +142,13 @@ fn test_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds() { @@ -144,14 +156,14 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds() { let json_input = r#"{"header": {"time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "update_interval": 3, "sol_id": 48}, "ssr_iod": 15, "const_id": 1, "n_sats": 2, "orbit_clock_bounds": [{"sat_id": 24, "orb_radial_bound_mu": 39, "orb_along_bound_mu": 38, "orb_cross_bound_mu": 37, "orb_radial_bound_sig": 1, "orb_along_bound_sig": 2, "orb_cross_bound_sig": 3, "clock_bound_mu": 39, "clock_bound_sig": 1}, {"sat_id": 3, "orb_radial_bound_mu": 39, "orb_along_bound_mu": 38, "orb_cross_bound_mu": 37, "orb_radial_bound_sig": 1, "orb_along_bound_sig": 2, "orb_cross_bound_sig": 3, "clock_bound_mu": 39, "clock_bound_sig": 1}], "preamble": 85, "msg_type": 1502, "sender": 66, "length": 31, "payload": "tAAAAAMAAQIDMA8BAhgnJiUBAgMnAQMnJiUBAgMnAQ==", "crc": 21781}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -267,3 +279,155 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds() { + { + let mut payload = Cursor::new(vec![ + 85, 222, 5, 66, 0, 31, 180, 0, 0, 0, 3, 0, 1, 2, 3, 48, 15, 1, 2, 24, 39, 38, 37, 1, 2, + 3, 39, 1, 3, 39, 38, 37, 1, 2, 3, 39, 1, 21, 85, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrOrbitClockBounds( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrOrbitClockBounds(msg) => { + assert_eq!( + msg.message_type(), + 1502, + "Incorrect message type, expected 1502, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.const_id, 1, + "incorrect value for const_id, expected 1, is {}", + msg.const_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.sol_id, 48, + "incorrect value for header.sol_id, expected 48, is {}", + msg.header.sol_id + ); + assert_eq!( + msg.header.time.tow, 180, + "incorrect value for header.time.tow, expected 180, is {}", + msg.header.time.tow + ); + assert_eq!( + msg.header.time.wn, 3, + "incorrect value for header.time.wn, expected 3, is {}", + msg.header.time.wn + ); + assert_eq!( + msg.header.update_interval, 3, + "incorrect value for header.update_interval, expected 3, is {}", + msg.header.update_interval + ); + assert_eq!( + msg.n_sats, 2, + "incorrect value for n_sats, expected 2, is {}", + msg.n_sats + ); + assert_eq!( + msg.orbit_clock_bounds[0].clock_bound_mu, 39, + "incorrect value for orbit_clock_bounds[0].clock_bound_mu, expected 39, is {}", + msg.orbit_clock_bounds[0].clock_bound_mu + ); + assert_eq!( + msg.orbit_clock_bounds[0].clock_bound_sig, 1, + "incorrect value for orbit_clock_bounds[0].clock_bound_sig, expected 1, is {}", + msg.orbit_clock_bounds[0].clock_bound_sig + ); + assert_eq!(msg.orbit_clock_bounds[0].orb_along_bound_mu, 38, "incorrect value for orbit_clock_bounds[0].orb_along_bound_mu, expected 38, is {}", msg.orbit_clock_bounds[0].orb_along_bound_mu); + assert_eq!(msg.orbit_clock_bounds[0].orb_along_bound_sig, 2, "incorrect value for orbit_clock_bounds[0].orb_along_bound_sig, expected 2, is {}", msg.orbit_clock_bounds[0].orb_along_bound_sig); + assert_eq!(msg.orbit_clock_bounds[0].orb_cross_bound_mu, 37, "incorrect value for orbit_clock_bounds[0].orb_cross_bound_mu, expected 37, is {}", msg.orbit_clock_bounds[0].orb_cross_bound_mu); + assert_eq!(msg.orbit_clock_bounds[0].orb_cross_bound_sig, 3, "incorrect value for orbit_clock_bounds[0].orb_cross_bound_sig, expected 3, is {}", msg.orbit_clock_bounds[0].orb_cross_bound_sig); + assert_eq!(msg.orbit_clock_bounds[0].orb_radial_bound_mu, 39, "incorrect value for orbit_clock_bounds[0].orb_radial_bound_mu, expected 39, is {}", msg.orbit_clock_bounds[0].orb_radial_bound_mu); + assert_eq!(msg.orbit_clock_bounds[0].orb_radial_bound_sig, 1, "incorrect value for orbit_clock_bounds[0].orb_radial_bound_sig, expected 1, is {}", msg.orbit_clock_bounds[0].orb_radial_bound_sig); + assert_eq!( + msg.orbit_clock_bounds[0].sat_id, 24, + "incorrect value for orbit_clock_bounds[0].sat_id, expected 24, is {}", + msg.orbit_clock_bounds[0].sat_id + ); + assert_eq!( + msg.orbit_clock_bounds[1].clock_bound_mu, 39, + "incorrect value for orbit_clock_bounds[1].clock_bound_mu, expected 39, is {}", + msg.orbit_clock_bounds[1].clock_bound_mu + ); + assert_eq!( + msg.orbit_clock_bounds[1].clock_bound_sig, 1, + "incorrect value for orbit_clock_bounds[1].clock_bound_sig, expected 1, is {}", + msg.orbit_clock_bounds[1].clock_bound_sig + ); + assert_eq!(msg.orbit_clock_bounds[1].orb_along_bound_mu, 38, "incorrect value for orbit_clock_bounds[1].orb_along_bound_mu, expected 38, is {}", msg.orbit_clock_bounds[1].orb_along_bound_mu); + assert_eq!(msg.orbit_clock_bounds[1].orb_along_bound_sig, 2, "incorrect value for orbit_clock_bounds[1].orb_along_bound_sig, expected 2, is {}", msg.orbit_clock_bounds[1].orb_along_bound_sig); + assert_eq!(msg.orbit_clock_bounds[1].orb_cross_bound_mu, 37, "incorrect value for orbit_clock_bounds[1].orb_cross_bound_mu, expected 37, is {}", msg.orbit_clock_bounds[1].orb_cross_bound_mu); + assert_eq!(msg.orbit_clock_bounds[1].orb_cross_bound_sig, 3, "incorrect value for orbit_clock_bounds[1].orb_cross_bound_sig, expected 3, is {}", msg.orbit_clock_bounds[1].orb_cross_bound_sig); + assert_eq!(msg.orbit_clock_bounds[1].orb_radial_bound_mu, 39, "incorrect value for orbit_clock_bounds[1].orb_radial_bound_mu, expected 39, is {}", msg.orbit_clock_bounds[1].orb_radial_bound_mu); + assert_eq!(msg.orbit_clock_bounds[1].orb_radial_bound_sig, 1, "incorrect value for orbit_clock_bounds[1].orb_radial_bound_sig, expected 1, is {}", msg.orbit_clock_bounds[1].orb_radial_bound_sig); + assert_eq!( + msg.orbit_clock_bounds[1].sat_id, 3, + "incorrect value for orbit_clock_bounds[1].sat_id, expected 3, is {}", + msg.orbit_clock_bounds[1].sat_id + ); + assert_eq!( + msg.ssr_iod, 15, + "incorrect value for ssr_iod, expected 15, is {}", + msg.ssr_iod + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrOrbitClockBounds"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation.rs b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation.rs index dc7f92eb59..08e4f312b3 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation() { { @@ -103,6 +108,13 @@ fn test_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation() { @@ -110,14 +122,14 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation() { let json_input = r#"{"header": {"time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 2, "update_interval": 3, "sol_id": 48}, "ssr_iod": 15, "const_id": 1, "sat_bitmask": 10, "orbit_clock_bounds_degradation": {"orb_radial_bound_mu_dot": 200, "orb_along_bound_mu_dot": 199, "orb_cross_bound_mu_dot": 199, "orb_cross_bound_mu_dot": 198, "orb_radial_bound_sig_dot": 197, "orb_along_bound_sig_dot": 196, "orb_cross_bound_sig_dot": 195, "clock_bound_mu_dot": 194, "clock_bound_sig_dot": 193}, "preamble": 85, "msg_type": 1503, "sender": 66, "length": 28, "payload": "tAAAAAMAAQIDMA8BCgAAAAAAAADIx8bFxMPCwQ==", "crc": 30152}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -199,3 +211,121 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ssr_msg_ssr_orbit_clock_bounds_degradation() { + { + let mut payload = Cursor::new(vec![ + 85, 223, 5, 66, 0, 28, 180, 0, 0, 0, 3, 0, 1, 2, 3, 48, 15, 1, 10, 0, 0, 0, 0, 0, 0, 0, + 200, 199, 198, 197, 196, 195, 194, 193, 200, 117, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrOrbitClockBoundsDegradation( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrOrbitClockBoundsDegradation(msg) => { + assert_eq!( + msg.message_type(), + 1503, + "Incorrect message type, expected 1503, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.const_id, 1, + "incorrect value for const_id, expected 1, is {}", + msg.const_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.seq_num, 2, + "incorrect value for header.seq_num, expected 2, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.sol_id, 48, + "incorrect value for header.sol_id, expected 48, is {}", + msg.header.sol_id + ); + assert_eq!( + msg.header.time.tow, 180, + "incorrect value for header.time.tow, expected 180, is {}", + msg.header.time.tow + ); + assert_eq!( + msg.header.time.wn, 3, + "incorrect value for header.time.wn, expected 3, is {}", + msg.header.time.wn + ); + assert_eq!( + msg.header.update_interval, 3, + "incorrect value for header.update_interval, expected 3, is {}", + msg.header.update_interval + ); + assert_eq!(msg.orbit_clock_bounds_degradation.clock_bound_mu_dot, 194, "incorrect value for orbit_clock_bounds_degradation.clock_bound_mu_dot, expected 194, is {}", msg.orbit_clock_bounds_degradation.clock_bound_mu_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.clock_bound_sig_dot, 193, "incorrect value for orbit_clock_bounds_degradation.clock_bound_sig_dot, expected 193, is {}", msg.orbit_clock_bounds_degradation.clock_bound_sig_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.orb_along_bound_mu_dot, 199, "incorrect value for orbit_clock_bounds_degradation.orb_along_bound_mu_dot, expected 199, is {}", msg.orbit_clock_bounds_degradation.orb_along_bound_mu_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.orb_along_bound_sig_dot, 196, "incorrect value for orbit_clock_bounds_degradation.orb_along_bound_sig_dot, expected 196, is {}", msg.orbit_clock_bounds_degradation.orb_along_bound_sig_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.orb_cross_bound_mu_dot, 198, "incorrect value for orbit_clock_bounds_degradation.orb_cross_bound_mu_dot, expected 198, is {}", msg.orbit_clock_bounds_degradation.orb_cross_bound_mu_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.orb_cross_bound_sig_dot, 195, "incorrect value for orbit_clock_bounds_degradation.orb_cross_bound_sig_dot, expected 195, is {}", msg.orbit_clock_bounds_degradation.orb_cross_bound_sig_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.orb_radial_bound_mu_dot, 200, "incorrect value for orbit_clock_bounds_degradation.orb_radial_bound_mu_dot, expected 200, is {}", msg.orbit_clock_bounds_degradation.orb_radial_bound_mu_dot); + assert_eq!(msg.orbit_clock_bounds_degradation.orb_radial_bound_sig_dot, 197, "incorrect value for orbit_clock_bounds_degradation.orb_radial_bound_sig_dot, expected 197, is {}", msg.orbit_clock_bounds_degradation.orb_radial_bound_sig_dot); + assert_eq!( + msg.sat_bitmask, 10, + "incorrect value for sat_bitmask, expected 10, is {}", + msg.sat_bitmask + ); + assert_eq!( + msg.ssr_iod, 15, + "incorrect value for ssr_iod, expected 15, is {}", + msg.ssr_iod + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrOrbitClockBoundsDegradation"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_stec_correction.rs b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_stec_correction.rs index 7c93db8de3..87c8a953ba 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_stec_correction.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_stec_correction.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ssr_msg_ssr_stec_correction() { { @@ -162,6 +167,13 @@ fn test_auto_check_sbp_ssr_msg_ssr_stec_correction() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_stec_correction() { @@ -169,14 +181,14 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_stec_correction() { let json_input = r#"{"header": {"time": {"tow": 180, "wn": 3}, "num_msgs": 1, "seq_num": 1, "update_interval": 10, "sol_id": 0}, "ssr_iod_atmo": 15, "tile_set_id": 1, "tile_id": 10, "n_sats": 2, "stec_sat_list": [{"sv_id": {"satId": 1, "constellation": 1}, "stec_quality_indicator": 1, "stec_coeff": [63, 62, 61, 60]}, {"sv_id": {"satId": 31, "constellation": 15}, "stec_quality_indicator": 5, "stec_coeff": [63, 64, 65, 66]}], "preamble": 85, "msg_type": 1533, "sender": 66, "length": 38, "payload": "tAAAAAMAAQEKAA8BAAoAAgEBAT8APgA9ADwAHw8FPwBAAEEAQgA=", "crc": 12919}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -317,3 +329,180 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_stec_correction() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ssr_msg_ssr_stec_correction`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ssr_msg_ssr_stec_correction() { + { + let mut payload = Cursor::new(vec![ + 85, 253, 5, 66, 0, 38, 180, 0, 0, 0, 3, 0, 1, 1, 10, 0, 15, 1, 0, 10, 0, 2, 1, 1, 1, + 63, 0, 62, 0, 61, 0, 60, 0, 31, 15, 5, 63, 0, 64, 0, 65, 0, 66, 0, 119, 50, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrStecCorrection( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrStecCorrection(msg) => { + assert_eq!( + msg.message_type(), + 1533, + "Incorrect message type, expected 1533, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.header.num_msgs, 1, + "incorrect value for header.num_msgs, expected 1, is {}", + msg.header.num_msgs + ); + assert_eq!( + msg.header.seq_num, 1, + "incorrect value for header.seq_num, expected 1, is {}", + msg.header.seq_num + ); + assert_eq!( + msg.header.sol_id, 0, + "incorrect value for header.sol_id, expected 0, is {}", + msg.header.sol_id + ); + assert_eq!( + msg.header.time.tow, 180, + "incorrect value for header.time.tow, expected 180, is {}", + msg.header.time.tow + ); + assert_eq!( + msg.header.time.wn, 3, + "incorrect value for header.time.wn, expected 3, is {}", + msg.header.time.wn + ); + assert_eq!( + msg.header.update_interval, 10, + "incorrect value for header.update_interval, expected 10, is {}", + msg.header.update_interval + ); + assert_eq!( + msg.n_sats, 2, + "incorrect value for n_sats, expected 2, is {}", + msg.n_sats + ); + assert_eq!( + msg.ssr_iod_atmo, 15, + "incorrect value for ssr_iod_atmo, expected 15, is {}", + msg.ssr_iod_atmo + ); + assert_eq!( + msg.stec_sat_list[0].stec_coeff[0], 63, + "incorrect value for stec_sat_list[0].stec_coeff[0], expected 63, is {}", + msg.stec_sat_list[0].stec_coeff[0] + ); + assert_eq!( + msg.stec_sat_list[0].stec_coeff[1], 62, + "incorrect value for stec_sat_list[0].stec_coeff[1], expected 62, is {}", + msg.stec_sat_list[0].stec_coeff[1] + ); + assert_eq!( + msg.stec_sat_list[0].stec_coeff[2], 61, + "incorrect value for stec_sat_list[0].stec_coeff[2], expected 61, is {}", + msg.stec_sat_list[0].stec_coeff[2] + ); + assert_eq!( + msg.stec_sat_list[0].stec_coeff[3], 60, + "incorrect value for stec_sat_list[0].stec_coeff[3], expected 60, is {}", + msg.stec_sat_list[0].stec_coeff[3] + ); + assert_eq!(msg.stec_sat_list[0].stec_quality_indicator, 1, "incorrect value for stec_sat_list[0].stec_quality_indicator, expected 1, is {}", msg.stec_sat_list[0].stec_quality_indicator); + assert_eq!( + msg.stec_sat_list[0].sv_id.constellation, 1, + "incorrect value for stec_sat_list[0].sv_id.constellation, expected 1, is {}", + msg.stec_sat_list[0].sv_id.constellation + ); + assert_eq!( + msg.stec_sat_list[0].sv_id.sat_id, 1, + "incorrect value for stec_sat_list[0].sv_id.sat_id, expected 1, is {}", + msg.stec_sat_list[0].sv_id.sat_id + ); + assert_eq!( + msg.stec_sat_list[1].stec_coeff[0], 63, + "incorrect value for stec_sat_list[1].stec_coeff[0], expected 63, is {}", + msg.stec_sat_list[1].stec_coeff[0] + ); + assert_eq!( + msg.stec_sat_list[1].stec_coeff[1], 64, + "incorrect value for stec_sat_list[1].stec_coeff[1], expected 64, is {}", + msg.stec_sat_list[1].stec_coeff[1] + ); + assert_eq!( + msg.stec_sat_list[1].stec_coeff[2], 65, + "incorrect value for stec_sat_list[1].stec_coeff[2], expected 65, is {}", + msg.stec_sat_list[1].stec_coeff[2] + ); + assert_eq!( + msg.stec_sat_list[1].stec_coeff[3], 66, + "incorrect value for stec_sat_list[1].stec_coeff[3], expected 66, is {}", + msg.stec_sat_list[1].stec_coeff[3] + ); + assert_eq!(msg.stec_sat_list[1].stec_quality_indicator, 5, "incorrect value for stec_sat_list[1].stec_quality_indicator, expected 5, is {}", msg.stec_sat_list[1].stec_quality_indicator); + assert_eq!( + msg.stec_sat_list[1].sv_id.constellation, 15, + "incorrect value for stec_sat_list[1].sv_id.constellation, expected 15, is {}", + msg.stec_sat_list[1].sv_id.constellation + ); + assert_eq!( + msg.stec_sat_list[1].sv_id.sat_id, 31, + "incorrect value for stec_sat_list[1].sv_id.sat_id, expected 31, is {}", + msg.stec_sat_list[1].sv_id.sat_id + ); + assert_eq!( + msg.tile_id, 10, + "incorrect value for tile_id, expected 10, is {}", + msg.tile_id + ); + assert_eq!( + msg.tile_set_id, 1, + "incorrect value for tile_set_id, expected 1, is {}", + msg.tile_set_id + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrStecCorrection"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_tile_definition.rs b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_tile_definition.rs index 4d18446306..1c2687dc3f 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_tile_definition.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_ssr_msg_ssr_tile_definition.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_ssr_msg_ssr_tile_definition() { { @@ -100,6 +105,13 @@ fn test_auto_check_sbp_ssr_msg_ssr_tile_definition() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_tile_definition() { @@ -107,14 +119,14 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_tile_definition() { let json_input = r#"{"ssr_sol_id": 31, "tile_set_id": 256, "tile_id": 512, "corner_nw_lat": 1024, "corner_nw_lon": 2048, "spacing_lat": 4096, "spacing_lon": 8192, "rows": 16384, "cols": 32768, "bitmask": 1234567890, "preamble": 85, "msg_type": 1527, "sender": 66, "length": 25, "payload": "HwABAAIABAAIABAAIABAAIDSApZJAAAAAA==", "crc": 18390}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -193,3 +205,118 @@ fn test_json2sbp_auto_check_sbp_ssr_msg_ssr_tile_definition() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_ssr_msg_ssr_tile_definition`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_ssr_msg_ssr_tile_definition() { + { + let mut payload = Cursor::new(vec![ + 85, 247, 5, 66, 0, 25, 31, 0, 1, 0, 2, 0, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 210, 2, + 150, 73, 0, 0, 0, 0, 214, 71, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSsrTileDefinition( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSsrTileDefinition(msg) => { + assert_eq!( + msg.message_type(), + 1527, + "Incorrect message type, expected 1527, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x0042, + "incorrect sender id, expected 0x0042, is {}", + sender_id + ); + assert_eq!( + msg.bitmask, 1234567890, + "incorrect value for bitmask, expected 1234567890, is {}", + msg.bitmask + ); + assert_eq!( + msg.cols, 32768, + "incorrect value for cols, expected 32768, is {}", + msg.cols + ); + assert_eq!( + msg.corner_nw_lat, 1024, + "incorrect value for corner_nw_lat, expected 1024, is {}", + msg.corner_nw_lat + ); + assert_eq!( + msg.corner_nw_lon, 2048, + "incorrect value for corner_nw_lon, expected 2048, is {}", + msg.corner_nw_lon + ); + assert_eq!( + msg.rows, 16384, + "incorrect value for rows, expected 16384, is {}", + msg.rows + ); + assert_eq!( + msg.spacing_lat, 4096, + "incorrect value for spacing_lat, expected 4096, is {}", + msg.spacing_lat + ); + assert_eq!( + msg.spacing_lon, 8192, + "incorrect value for spacing_lon, expected 8192, is {}", + msg.spacing_lon + ); + assert_eq!( + msg.ssr_sol_id, 31, + "incorrect value for ssr_sol_id, expected 31, is {}", + msg.ssr_sol_id + ); + assert_eq!( + msg.tile_id, 512, + "incorrect value for tile_id, expected 512, is {}", + msg.tile_id + ); + assert_eq!( + msg.tile_set_id, 256, + "incorrect value for tile_set_id, expected 256, is {}", + msg.tile_set_id + ); + } + _ => panic!("Invalid message type! Expected a MsgSsrTileDefinition"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_dgnss_status.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_dgnss_status.rs index 353335ba7f..fc02f2f4b3 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_dgnss_status.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_dgnss_status.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_dgnss_status() { { @@ -71,6 +76,13 @@ fn test_auto_check_sbp_system_msg_dgnss_status() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_dgnss_status() { @@ -78,14 +90,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_dgnss_status() { let json_input = r#"{"latency": 50, "sender": 66, "msg_type": 65282, "source": "Skylark", "num_signals": 12, "crc": 458, "length": 11, "flags": 0, "preamble": 85, "payload": "ADIADFNreWxhcms="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -136,3 +148,89 @@ fn test_json2sbp_auto_check_sbp_system_msg_dgnss_status() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_dgnss_status`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_dgnss_status() { + { + let mut payload = Cursor::new(vec![ + 85, 2, 255, 66, 0, 11, 0, 50, 0, 12, 83, 107, 121, 108, 97, 114, 107, 202, 1, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgDgnssStatus( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgDgnssStatus(msg) => { + assert_eq!( + msg.message_type(), + 0xff02, + "Incorrect message type, expected 0xff02, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.latency, 50, + "incorrect value for latency, expected 50, is {}", + msg.latency + ); + assert_eq!( + msg.num_signals, 12, + "incorrect value for num_signals, expected 12, is {}", + msg.num_signals + ); + assert_eq!( + msg.source.to_string(), + "Skylark".to_string(), + "incorrect value for msg.source, expected string '{}', is '{}'", + "Skylark".to_string(), + msg.source + ); + } + _ => panic!("Invalid message type! Expected a MsgDgnssStatus"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_group_meta.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_group_meta.rs index e5bce17ffb..9b39f7ba0d 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_group_meta.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_group_meta.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_group_meta() { { @@ -197,6 +202,13 @@ fn test_auto_check_sbp_system_msg_group_meta() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_group_meta() { @@ -204,14 +216,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_group_meta() { let json_input = r#"{"crc": 3586, "flags": 2, "group_id": 1, "sender": 61166, "msg_type": 65290, "length": 9, "n_group_msgs": 3 ,"group_msgs": [65290, 522, 65282], "preamble": 85, "payload": "AQIDCv8KAgL/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -273,14 +285,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_group_meta() { let json_input = r#"{"group_id":1,"flags":1,"n_group_msgs":14,"group_msgs":[258,259,522,529,521,532,526,530,525,533,545,65283,65286,65294],"preamble":85,"msg_type":65290,"sender":789,"payload":"AQEOAgEDAQoCEQIJAhQCDgISAg0CFQIhAgP/Bv8O/w==","crc":39506,"length":31}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -394,3 +406,234 @@ fn test_json2sbp_auto_check_sbp_system_msg_group_meta() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_group_meta`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_group_meta() { + { + let mut payload = Cursor::new(vec![ + 85, 10, 255, 238, 238, 9, 1, 2, 3, 10, 255, 10, 2, 2, 255, 2, 14, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGroupMeta( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGroupMeta(msg) => { + assert_eq!( + msg.message_type(), + 0xFF0A, + "Incorrect message type, expected 0xFF0A, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xEEEE, + "incorrect sender id, expected 0xEEEE, is {}", + sender_id + ); + assert_eq!( + msg.flags, 2, + "incorrect value for flags, expected 2, is {}", + msg.flags + ); + assert_eq!( + msg.group_id, 1, + "incorrect value for group_id, expected 1, is {}", + msg.group_id + ); + assert_eq!( + msg.group_msgs[0], 65290, + "incorrect value for group_msgs[0], expected 65290, is {}", + msg.group_msgs[0] + ); + assert_eq!( + msg.group_msgs[1], 522, + "incorrect value for group_msgs[1], expected 522, is {}", + msg.group_msgs[1] + ); + assert_eq!( + msg.group_msgs[2], 65282, + "incorrect value for group_msgs[2], expected 65282, is {}", + msg.group_msgs[2] + ); + assert_eq!( + msg.n_group_msgs, 3, + "incorrect value for n_group_msgs, expected 3, is {}", + msg.n_group_msgs + ); + } + _ => panic!("Invalid message type! Expected a MsgGroupMeta"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 10, 255, 21, 3, 31, 1, 1, 14, 2, 1, 3, 1, 10, 2, 17, 2, 9, 2, 20, 2, 14, 2, 18, 2, + 13, 2, 21, 2, 33, 2, 3, 255, 6, 255, 14, 255, 82, 154, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgGroupMeta( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgGroupMeta(msg) => { + assert_eq!( + msg.message_type(), + 0xFF0A, + "Incorrect message type, expected 0xFF0A, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x315, + "incorrect sender id, expected 0x315, is {}", + sender_id + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.group_id, 1, + "incorrect value for group_id, expected 1, is {}", + msg.group_id + ); + assert_eq!( + msg.group_msgs[0], 258, + "incorrect value for group_msgs[0], expected 258, is {}", + msg.group_msgs[0] + ); + assert_eq!( + msg.group_msgs[1], 259, + "incorrect value for group_msgs[1], expected 259, is {}", + msg.group_msgs[1] + ); + assert_eq!( + msg.group_msgs[2], 522, + "incorrect value for group_msgs[2], expected 522, is {}", + msg.group_msgs[2] + ); + assert_eq!( + msg.group_msgs[3], 529, + "incorrect value for group_msgs[3], expected 529, is {}", + msg.group_msgs[3] + ); + assert_eq!( + msg.group_msgs[4], 521, + "incorrect value for group_msgs[4], expected 521, is {}", + msg.group_msgs[4] + ); + assert_eq!( + msg.group_msgs[5], 532, + "incorrect value for group_msgs[5], expected 532, is {}", + msg.group_msgs[5] + ); + assert_eq!( + msg.group_msgs[6], 526, + "incorrect value for group_msgs[6], expected 526, is {}", + msg.group_msgs[6] + ); + assert_eq!( + msg.group_msgs[7], 530, + "incorrect value for group_msgs[7], expected 530, is {}", + msg.group_msgs[7] + ); + assert_eq!( + msg.group_msgs[8], 525, + "incorrect value for group_msgs[8], expected 525, is {}", + msg.group_msgs[8] + ); + assert_eq!( + msg.group_msgs[9], 533, + "incorrect value for group_msgs[9], expected 533, is {}", + msg.group_msgs[9] + ); + assert_eq!( + msg.group_msgs[10], 545, + "incorrect value for group_msgs[10], expected 545, is {}", + msg.group_msgs[10] + ); + assert_eq!( + msg.group_msgs[11], 65283, + "incorrect value for group_msgs[11], expected 65283, is {}", + msg.group_msgs[11] + ); + assert_eq!( + msg.group_msgs[12], 65286, + "incorrect value for group_msgs[12], expected 65286, is {}", + msg.group_msgs[12] + ); + assert_eq!( + msg.group_msgs[13], 65294, + "incorrect value for group_msgs[13], expected 65294, is {}", + msg.group_msgs[13] + ); + assert_eq!( + msg.n_group_msgs, 14, + "incorrect value for n_group_msgs, expected 14, is {}", + msg.n_group_msgs + ); + } + _ => panic!("Invalid message type! Expected a MsgGroupMeta"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_heartbeat.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_heartbeat.rs index ddfa76e26f..c1e378d1fe 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_heartbeat.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_heartbeat.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_heartbeat() { { @@ -87,6 +92,13 @@ fn test_auto_check_sbp_system_msg_heartbeat() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_heartbeat() { @@ -94,14 +106,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_heartbeat() { let json_input = r#"{"sender": 55286, "msg_type": 65535, "crc": 55545, "length": 4, "flags": 12800, "preamble": 85, "payload": "ADIAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -138,14 +150,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_heartbeat() { let json_input = r#"{"sender": 1219, "msg_type": 65535, "crc": 14658, "length": 4, "flags": 0, "preamble": 85, "payload": "AAAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -179,3 +191,124 @@ fn test_json2sbp_auto_check_sbp_system_msg_heartbeat() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_heartbeat`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_heartbeat() { + { + let mut payload = Cursor::new(vec![85, 255, 255, 246, 215, 4, 0, 50, 0, 0, 249, 216]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgHeartbeat( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgHeartbeat(msg) => { + assert_eq!( + msg.message_type(), + 0xffff, + "Incorrect message type, expected 0xffff, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert_eq!( + msg.flags, 12800, + "incorrect value for flags, expected 12800, is {}", + msg.flags + ); + } + _ => panic!("Invalid message type! Expected a MsgHeartbeat"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 255, 255, 195, 4, 4, 0, 0, 0, 0, 66, 57]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgHeartbeat( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgHeartbeat(msg) => { + assert_eq!( + msg.message_type(), + 0xffff, + "Incorrect message type, expected 0xffff, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + } + _ => panic!("Invalid message type! Expected a MsgHeartbeat"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_status.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_status.rs index f6e1050280..e175b3cb91 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_status.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_status.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_ins_status() { { @@ -52,6 +57,13 @@ fn test_auto_check_sbp_system_msg_ins_status() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_ins_status() { @@ -59,14 +71,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_ins_status() { let json_input = r#"{"flags":536870921,"preamble":85,"msg_type":65283,"sender":789,"payload":"CQAAIA==","crc":26404,"length":4}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -100,3 +112,70 @@ fn test_json2sbp_auto_check_sbp_system_msg_ins_status() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_ins_status`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_ins_status() { + { + let mut payload = Cursor::new(vec![85, 3, 255, 21, 3, 4, 9, 0, 0, 32, 36, 103]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgInsStatus( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgInsStatus(msg) => { + assert_eq!( + msg.message_type(), + 0xff03, + "Incorrect message type, expected 0xff03, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x315, + "incorrect sender id, expected 0x315, is {}", + sender_id + ); + assert_eq!( + msg.flags, 536870921, + "incorrect value for flags, expected 536870921, is {}", + msg.flags + ); + } + _ => panic!("Invalid message type! Expected a MsgInsStatus"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_updates.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_updates.rs index 78ffe7018c..f049c246fd 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_updates.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_ins_updates.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_ins_updates() { { @@ -84,6 +89,13 @@ fn test_auto_check_sbp_system_msg_ins_updates() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_ins_updates() { @@ -91,14 +103,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_ins_updates() { let json_input = r#"{"tow":504489300,"gnsspos":0,"gnssvel":0,"wheelticks":0,"speed":0,"nhc":0,"zerovel":0,"preamble":85,"msg_type":65286,"sender":789,"payload":"VOURHgAAAAAAAA==","crc":16209,"length":10}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -162,3 +174,102 @@ fn test_json2sbp_auto_check_sbp_system_msg_ins_updates() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_ins_updates`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_ins_updates() { + { + let mut payload = Cursor::new(vec![ + 85, 6, 255, 21, 3, 10, 84, 229, 17, 30, 0, 0, 0, 0, 0, 0, 81, 63, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgInsUpdates( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgInsUpdates(msg) => { + assert_eq!( + msg.message_type(), + 0xff06, + "Incorrect message type, expected 0xff06, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x315, + "incorrect sender id, expected 0x315, is {}", + sender_id + ); + assert_eq!( + msg.gnsspos, 0, + "incorrect value for gnsspos, expected 0, is {}", + msg.gnsspos + ); + assert_eq!( + msg.gnssvel, 0, + "incorrect value for gnssvel, expected 0, is {}", + msg.gnssvel + ); + assert_eq!( + msg.nhc, 0, + "incorrect value for nhc, expected 0, is {}", + msg.nhc + ); + assert_eq!( + msg.speed, 0, + "incorrect value for speed, expected 0, is {}", + msg.speed + ); + assert_eq!( + msg.tow, 504489300, + "incorrect value for tow, expected 504489300, is {}", + msg.tow + ); + assert_eq!( + msg.wheelticks, 0, + "incorrect value for wheelticks, expected 0, is {}", + msg.wheelticks + ); + assert_eq!( + msg.zerovel, 0, + "incorrect value for zerovel, expected 0, is {}", + msg.zerovel + ); + } + _ => panic!("Invalid message type! Expected a MsgInsUpdates"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_sensor_aid_event.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_sensor_aid_event.rs index 798f521e74..3f19870dd9 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_sensor_aid_event.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_sensor_aid_event.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_sensor_aid_event() { { @@ -89,6 +94,13 @@ fn test_auto_check_sbp_system_msg_sensor_aid_event() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_sensor_aid_event() { @@ -96,14 +108,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_sensor_aid_event() { let json_input = r#"{"preamble": 85, "msg_type": 65289, "sender": 35027, "length": 15, "payload": "MPZ6EwAAAAAAAAAAAAAA", "crc": 60449, "time": 326825520, "sensor_type": 0, "sensor_id": 0, "sensor_state": 0, "n_available_meas": 0, "n_attempted_meas": 0, "n_accepted_meas": 0, "flags": 0}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -172,3 +184,107 @@ fn test_json2sbp_auto_check_sbp_system_msg_sensor_aid_event() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_sensor_aid_event`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_sensor_aid_event() { + { + let mut payload = Cursor::new(vec![ + 85, 9, 255, 211, 136, 15, 48, 246, 122, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 236, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgSensorAidEvent( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgSensorAidEvent(msg) => { + assert_eq!( + msg.message_type(), + 0xFF09, + "Incorrect message type, expected 0xFF09, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88D3, + "incorrect sender id, expected 0x88D3, is {}", + sender_id + ); + assert_eq!( + msg.flags, 0, + "incorrect value for flags, expected 0, is {}", + msg.flags + ); + assert_eq!( + msg.n_accepted_meas, 0, + "incorrect value for n_accepted_meas, expected 0, is {}", + msg.n_accepted_meas + ); + assert_eq!( + msg.n_attempted_meas, 0, + "incorrect value for n_attempted_meas, expected 0, is {}", + msg.n_attempted_meas + ); + assert_eq!( + msg.n_available_meas, 0, + "incorrect value for n_available_meas, expected 0, is {}", + msg.n_available_meas + ); + assert_eq!( + msg.sensor_id, 0, + "incorrect value for sensor_id, expected 0, is {}", + msg.sensor_id + ); + assert_eq!( + msg.sensor_state, 0, + "incorrect value for sensor_state, expected 0, is {}", + msg.sensor_state + ); + assert_eq!( + msg.sensor_type, 0, + "incorrect value for sensor_type, expected 0, is {}", + msg.sensor_type + ); + assert_eq!( + msg.time, 326825520, + "incorrect value for time, expected 326825520, is {}", + msg.time + ); + } + _ => panic!("Invalid message type! Expected a MsgSensorAidEvent"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_startup.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_startup.rs index 0572b3c787..4b1daf742c 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_startup.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_startup.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_startup() { { @@ -107,6 +112,13 @@ fn test_auto_check_sbp_system_msg_startup() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_startup() { @@ -114,14 +126,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_startup() { let json_input = r#"{"reserved": 0, "sender": 66, "msg_type": 65280, "cause": 0, "startup_type": 0, "crc": 41030, "length": 4, "preamble": 85, "payload": "AAAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -168,14 +180,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_startup() { let json_input = r#"{"reserved": 0, "cause":0, "startup_type":0, "sender": 1219, "msg_type": 65280, "crc": 46463, "length": 4, "preamble": 85, "payload": "AAAAAA=="}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -219,3 +231,144 @@ fn test_json2sbp_auto_check_sbp_system_msg_startup() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_startup`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_startup() { + { + let mut payload = Cursor::new(vec![85, 0, 255, 66, 0, 4, 0, 0, 0, 0, 70, 160]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgStartup( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgStartup(msg) => { + assert_eq!( + msg.message_type(), + 0xff00, + "Incorrect message type, expected 0xff00, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.cause, 0, + "incorrect value for cause, expected 0, is {}", + msg.cause + ); + assert_eq!( + msg.reserved, 0, + "incorrect value for reserved, expected 0, is {}", + msg.reserved + ); + assert_eq!( + msg.startup_type, 0, + "incorrect value for startup_type, expected 0, is {}", + msg.startup_type + ); + } + _ => panic!("Invalid message type! Expected a MsgStartup"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![85, 0, 255, 195, 4, 4, 0, 0, 0, 0, 127, 181]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgStartup( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgStartup(msg) => { + assert_eq!( + msg.message_type(), + 0xff00, + "Incorrect message type, expected 0xff00, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert_eq!( + msg.cause, 0, + "incorrect value for cause, expected 0, is {}", + msg.cause + ); + assert_eq!( + msg.reserved, 0, + "incorrect value for reserved, expected 0, is {}", + msg.reserved + ); + assert_eq!( + msg.startup_type, 0, + "incorrect value for startup_type, expected 0, is {}", + msg.startup_type + ); + } + _ => panic!("Invalid message type! Expected a MsgStartup"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_system_msg_status_journal.rs b/rust/sbp/tests/integration/auto_check_sbp_system_msg_status_journal.rs index bae31c2fc7..26684934b8 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_system_msg_status_journal.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_system_msg_status_journal.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_system_msg_status_journal() { { @@ -203,6 +208,13 @@ fn test_auto_check_sbp_system_msg_status_journal() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_system_msg_status_journal() { @@ -210,14 +222,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_status_journal() { let json_input = r#"{"preamble": 85, "msg_type": 65533, "sender": 35027, "length": 33, "payload": "AQABBGQAAAAQkhAAAAYAAQ26EwAABgABDrgiAAAGAAEP", "crc": 30577, "reporting_system": 1, "sbp_version": 1025, "total_status_reports": 100, "sequence_descriptor": 16, "journal": [{"uptime": 4242, "report": {"component": 6, "generic": 1, "specific": 13}}, {"uptime": 5050, "report": {"component": 6, "generic": 1, "specific": 14}}, {"uptime": 8888, "report": {"component": 6, "generic": 1, "specific": 15}}]}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -329,14 +341,14 @@ fn test_json2sbp_auto_check_sbp_system_msg_status_journal() { let json_input = r#"{"preamble": 85, "msg_type": 65533, "sender": 35027, "length": 17, "payload": "AQABBGQAAAAQkhAAAAYAAQ0=", "crc": 31120, "reporting_system": 1, "sbp_version": 1025, "total_status_reports": 100, "sequence_descriptor": 16, "journal": [{"uptime": 4242, "report": {"component": 6, "generic": 1, "specific": 13}}]}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -405,3 +417,240 @@ fn test_json2sbp_auto_check_sbp_system_msg_status_journal() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_system_msg_status_journal`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_system_msg_status_journal() { + { + let mut payload = Cursor::new(vec![ + 85, 253, 255, 211, 136, 33, 1, 0, 1, 4, 100, 0, 0, 0, 16, 146, 16, 0, 0, 6, 0, 1, 13, + 186, 19, 0, 0, 6, 0, 1, 14, 184, 34, 0, 0, 6, 0, 1, 15, 113, 119, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgStatusJournal( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgStatusJournal(msg) => { + assert_eq!( + msg.message_type(), + 0xFFFD, + "Incorrect message type, expected 0xFFFD, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88D3, + "incorrect sender id, expected 0x88D3, is {}", + sender_id + ); + assert_eq!( + msg.journal[0].report.component, 6, + "incorrect value for journal[0].report.component, expected 6, is {}", + msg.journal[0].report.component + ); + assert_eq!( + msg.journal[0].report.generic, 1, + "incorrect value for journal[0].report.generic, expected 1, is {}", + msg.journal[0].report.generic + ); + assert_eq!( + msg.journal[0].report.specific, 13, + "incorrect value for journal[0].report.specific, expected 13, is {}", + msg.journal[0].report.specific + ); + assert_eq!( + msg.journal[0].uptime, 4242, + "incorrect value for journal[0].uptime, expected 4242, is {}", + msg.journal[0].uptime + ); + assert_eq!( + msg.journal[1].report.component, 6, + "incorrect value for journal[1].report.component, expected 6, is {}", + msg.journal[1].report.component + ); + assert_eq!( + msg.journal[1].report.generic, 1, + "incorrect value for journal[1].report.generic, expected 1, is {}", + msg.journal[1].report.generic + ); + assert_eq!( + msg.journal[1].report.specific, 14, + "incorrect value for journal[1].report.specific, expected 14, is {}", + msg.journal[1].report.specific + ); + assert_eq!( + msg.journal[1].uptime, 5050, + "incorrect value for journal[1].uptime, expected 5050, is {}", + msg.journal[1].uptime + ); + assert_eq!( + msg.journal[2].report.component, 6, + "incorrect value for journal[2].report.component, expected 6, is {}", + msg.journal[2].report.component + ); + assert_eq!( + msg.journal[2].report.generic, 1, + "incorrect value for journal[2].report.generic, expected 1, is {}", + msg.journal[2].report.generic + ); + assert_eq!( + msg.journal[2].report.specific, 15, + "incorrect value for journal[2].report.specific, expected 15, is {}", + msg.journal[2].report.specific + ); + assert_eq!( + msg.journal[2].uptime, 8888, + "incorrect value for journal[2].uptime, expected 8888, is {}", + msg.journal[2].uptime + ); + assert_eq!( + msg.reporting_system, 1, + "incorrect value for reporting_system, expected 1, is {}", + msg.reporting_system + ); + assert_eq!( + msg.sbp_version, 1025, + "incorrect value for sbp_version, expected 1025, is {}", + msg.sbp_version + ); + assert_eq!( + msg.sequence_descriptor, 16, + "incorrect value for sequence_descriptor, expected 16, is {}", + msg.sequence_descriptor + ); + assert_eq!( + msg.total_status_reports, 100, + "incorrect value for total_status_reports, expected 100, is {}", + msg.total_status_reports + ); + } + _ => panic!("Invalid message type! Expected a MsgStatusJournal"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 253, 255, 211, 136, 17, 1, 0, 1, 4, 100, 0, 0, 0, 16, 146, 16, 0, 0, 6, 0, 1, 13, + 144, 121, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgStatusJournal( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgStatusJournal(msg) => { + assert_eq!( + msg.message_type(), + 0xFFFD, + "Incorrect message type, expected 0xFFFD, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x88D3, + "incorrect sender id, expected 0x88D3, is {}", + sender_id + ); + assert_eq!( + msg.journal[0].report.component, 6, + "incorrect value for journal[0].report.component, expected 6, is {}", + msg.journal[0].report.component + ); + assert_eq!( + msg.journal[0].report.generic, 1, + "incorrect value for journal[0].report.generic, expected 1, is {}", + msg.journal[0].report.generic + ); + assert_eq!( + msg.journal[0].report.specific, 13, + "incorrect value for journal[0].report.specific, expected 13, is {}", + msg.journal[0].report.specific + ); + assert_eq!( + msg.journal[0].uptime, 4242, + "incorrect value for journal[0].uptime, expected 4242, is {}", + msg.journal[0].uptime + ); + assert_eq!( + msg.reporting_system, 1, + "incorrect value for reporting_system, expected 1, is {}", + msg.reporting_system + ); + assert_eq!( + msg.sbp_version, 1025, + "incorrect value for sbp_version, expected 1025, is {}", + msg.sbp_version + ); + assert_eq!( + msg.sequence_descriptor, 16, + "incorrect value for sequence_descriptor, expected 16, is {}", + msg.sequence_descriptor + ); + assert_eq!( + msg.total_status_reports, 100, + "incorrect value for total_status_reports, expected 100, is {}", + msg.total_status_reports + ); + } + _ => panic!("Invalid message type! Expected a MsgStatusJournal"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_measurement_state.rs b/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_measurement_state.rs index 7009613556..8afb411dc0 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_measurement_state.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_measurement_state.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_tracking_msg_measurement_state() { { @@ -1244,6 +1249,13 @@ fn test_auto_check_sbp_tracking_msg_measurement_state() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_tracking_msg_measurement_state() { @@ -1251,14 +1263,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_measurement_state() { let json_input = r#"{"preamble": 85, "msg_type": 97, "sender": 31183, "length": 237, "payload": "HQCiAAAAAAAAGwDJFACoIAC4DwC7AAAAEgDSEACnAAAAFwDVCgDfAAAAAAAAAAAAAAAAgwLKGwHADwGlHQGSIAGqEgHJAAAAAAAAAAAAFwHUCgHNAAAAYAPmAAAAZQPWZwPUaAPRagOdZgPmAAAAAAAAZQS9YATPagSkaATBAAAAZgTQAAAAGwzUHQyhIAzYHgzYFAyyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJA7LAAAABQ6eBA7CCw7ACQ7PAAAAAAAAAAAACRTaBRSwJBTZCxTIBBTNAAAAAAAA", "crc": 13859, "states": [{"mesid": {"sat": 29, "code": 0}, "cn0": 162}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 27, "code": 0}, "cn0": 201}, {"mesid": {"sat": 20, "code": 0}, "cn0": 168}, {"mesid": {"sat": 32, "code": 0}, "cn0": 184}, {"mesid": {"sat": 15, "code": 0}, "cn0": 187}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 18, "code": 0}, "cn0": 210}, {"mesid": {"sat": 16, "code": 0}, "cn0": 167}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 23, "code": 0}, "cn0": 213}, {"mesid": {"sat": 10, "code": 0}, "cn0": 223}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 131, "code": 2}, "cn0": 202}, {"mesid": {"sat": 27, "code": 1}, "cn0": 192}, {"mesid": {"sat": 15, "code": 1}, "cn0": 165}, {"mesid": {"sat": 29, "code": 1}, "cn0": 146}, {"mesid": {"sat": 32, "code": 1}, "cn0": 170}, {"mesid": {"sat": 18, "code": 1}, "cn0": 201}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 23, "code": 1}, "cn0": 212}, {"mesid": {"sat": 10, "code": 1}, "cn0": 205}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 96, "code": 3}, "cn0": 230}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 101, "code": 3}, "cn0": 214}, {"mesid": {"sat": 103, "code": 3}, "cn0": 212}, {"mesid": {"sat": 104, "code": 3}, "cn0": 209}, {"mesid": {"sat": 106, "code": 3}, "cn0": 157}, {"mesid": {"sat": 102, "code": 3}, "cn0": 230}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 101, "code": 4}, "cn0": 189}, {"mesid": {"sat": 96, "code": 4}, "cn0": 207}, {"mesid": {"sat": 106, "code": 4}, "cn0": 164}, {"mesid": {"sat": 104, "code": 4}, "cn0": 193}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 102, "code": 4}, "cn0": 208}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 27, "code": 12}, "cn0": 212}, {"mesid": {"sat": 29, "code": 12}, "cn0": 161}, {"mesid": {"sat": 32, "code": 12}, "cn0": 216}, {"mesid": {"sat": 30, "code": 12}, "cn0": 216}, {"mesid": {"sat": 20, "code": 12}, "cn0": 178}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 36, "code": 14}, "cn0": 203}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 5, "code": 14}, "cn0": 158}, {"mesid": {"sat": 4, "code": 14}, "cn0": 194}, {"mesid": {"sat": 11, "code": 14}, "cn0": 192}, {"mesid": {"sat": 9, "code": 14}, "cn0": 207}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 9, "code": 20}, "cn0": 218}, {"mesid": {"sat": 5, "code": 20}, "cn0": 176}, {"mesid": {"sat": 36, "code": 20}, "cn0": 217}, {"mesid": {"sat": 11, "code": 20}, "cn0": 200}, {"mesid": {"sat": 4, "code": 20}, "cn0": 205}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}, {"mesid": {"sat": 0, "code": 0}, "cn0": 0}]}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2472,3 +2484,1262 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_measurement_state() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_tracking_msg_measurement_state`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_tracking_msg_measurement_state() { + { + let mut payload = Cursor::new(vec![ + 85, 97, 0, 207, 121, 237, 29, 0, 162, 0, 0, 0, 0, 0, 0, 27, 0, 201, 20, 0, 168, 32, 0, + 184, 15, 0, 187, 0, 0, 0, 18, 0, 210, 16, 0, 167, 0, 0, 0, 23, 0, 213, 10, 0, 223, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 131, 2, 202, 27, 1, 192, 15, 1, 165, 29, 1, 146, 32, + 1, 170, 18, 1, 201, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 1, 212, 10, 1, 205, 0, 0, 0, 96, 3, + 230, 0, 0, 0, 101, 3, 214, 103, 3, 212, 104, 3, 209, 106, 3, 157, 102, 3, 230, 0, 0, 0, + 0, 0, 0, 101, 4, 189, 96, 4, 207, 106, 4, 164, 104, 4, 193, 0, 0, 0, 102, 4, 208, 0, 0, + 0, 27, 12, 212, 29, 12, 161, 32, 12, 216, 30, 12, 216, 20, 12, 178, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 36, 14, 203, 0, 0, 0, 5, 14, 158, 4, 14, 194, 11, 14, 192, 9, 14, 207, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 9, 20, 218, 5, 20, 176, 36, 20, 217, 11, 20, 200, 4, 20, 205, + 0, 0, 0, 0, 0, 0, 35, 54, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgMeasurementState( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgMeasurementState(msg) => { + assert_eq!( + msg.message_type(), + 0x61, + "Incorrect message type, expected 0x61, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x79CF, + "incorrect sender id, expected 0x79CF, is {}", + sender_id + ); + assert_eq!( + msg.states[0].cn0, 162, + "incorrect value for states[0].cn0, expected 162, is {}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].mesid.code, 0, + "incorrect value for states[0].mesid.code, expected 0, is {}", + msg.states[0].mesid.code + ); + assert_eq!( + msg.states[0].mesid.sat, 29, + "incorrect value for states[0].mesid.sat, expected 29, is {}", + msg.states[0].mesid.sat + ); + assert_eq!( + msg.states[1].cn0, 0, + "incorrect value for states[1].cn0, expected 0, is {}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].mesid.code, 0, + "incorrect value for states[1].mesid.code, expected 0, is {}", + msg.states[1].mesid.code + ); + assert_eq!( + msg.states[1].mesid.sat, 0, + "incorrect value for states[1].mesid.sat, expected 0, is {}", + msg.states[1].mesid.sat + ); + assert_eq!( + msg.states[2].cn0, 0, + "incorrect value for states[2].cn0, expected 0, is {}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].mesid.code, 0, + "incorrect value for states[2].mesid.code, expected 0, is {}", + msg.states[2].mesid.code + ); + assert_eq!( + msg.states[2].mesid.sat, 0, + "incorrect value for states[2].mesid.sat, expected 0, is {}", + msg.states[2].mesid.sat + ); + assert_eq!( + msg.states[3].cn0, 201, + "incorrect value for states[3].cn0, expected 201, is {}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].mesid.code, 0, + "incorrect value for states[3].mesid.code, expected 0, is {}", + msg.states[3].mesid.code + ); + assert_eq!( + msg.states[3].mesid.sat, 27, + "incorrect value for states[3].mesid.sat, expected 27, is {}", + msg.states[3].mesid.sat + ); + assert_eq!( + msg.states[4].cn0, 168, + "incorrect value for states[4].cn0, expected 168, is {}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].mesid.code, 0, + "incorrect value for states[4].mesid.code, expected 0, is {}", + msg.states[4].mesid.code + ); + assert_eq!( + msg.states[4].mesid.sat, 20, + "incorrect value for states[4].mesid.sat, expected 20, is {}", + msg.states[4].mesid.sat + ); + assert_eq!( + msg.states[5].cn0, 184, + "incorrect value for states[5].cn0, expected 184, is {}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].mesid.code, 0, + "incorrect value for states[5].mesid.code, expected 0, is {}", + msg.states[5].mesid.code + ); + assert_eq!( + msg.states[5].mesid.sat, 32, + "incorrect value for states[5].mesid.sat, expected 32, is {}", + msg.states[5].mesid.sat + ); + assert_eq!( + msg.states[6].cn0, 187, + "incorrect value for states[6].cn0, expected 187, is {}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].mesid.code, 0, + "incorrect value for states[6].mesid.code, expected 0, is {}", + msg.states[6].mesid.code + ); + assert_eq!( + msg.states[6].mesid.sat, 15, + "incorrect value for states[6].mesid.sat, expected 15, is {}", + msg.states[6].mesid.sat + ); + assert_eq!( + msg.states[7].cn0, 0, + "incorrect value for states[7].cn0, expected 0, is {}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].mesid.code, 0, + "incorrect value for states[7].mesid.code, expected 0, is {}", + msg.states[7].mesid.code + ); + assert_eq!( + msg.states[7].mesid.sat, 0, + "incorrect value for states[7].mesid.sat, expected 0, is {}", + msg.states[7].mesid.sat + ); + assert_eq!( + msg.states[8].cn0, 210, + "incorrect value for states[8].cn0, expected 210, is {}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].mesid.code, 0, + "incorrect value for states[8].mesid.code, expected 0, is {}", + msg.states[8].mesid.code + ); + assert_eq!( + msg.states[8].mesid.sat, 18, + "incorrect value for states[8].mesid.sat, expected 18, is {}", + msg.states[8].mesid.sat + ); + assert_eq!( + msg.states[9].cn0, 167, + "incorrect value for states[9].cn0, expected 167, is {}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].mesid.code, 0, + "incorrect value for states[9].mesid.code, expected 0, is {}", + msg.states[9].mesid.code + ); + assert_eq!( + msg.states[9].mesid.sat, 16, + "incorrect value for states[9].mesid.sat, expected 16, is {}", + msg.states[9].mesid.sat + ); + assert_eq!( + msg.states[10].cn0, 0, + "incorrect value for states[10].cn0, expected 0, is {}", + msg.states[10].cn0 + ); + assert_eq!( + msg.states[10].mesid.code, 0, + "incorrect value for states[10].mesid.code, expected 0, is {}", + msg.states[10].mesid.code + ); + assert_eq!( + msg.states[10].mesid.sat, 0, + "incorrect value for states[10].mesid.sat, expected 0, is {}", + msg.states[10].mesid.sat + ); + assert_eq!( + msg.states[11].cn0, 213, + "incorrect value for states[11].cn0, expected 213, is {}", + msg.states[11].cn0 + ); + assert_eq!( + msg.states[11].mesid.code, 0, + "incorrect value for states[11].mesid.code, expected 0, is {}", + msg.states[11].mesid.code + ); + assert_eq!( + msg.states[11].mesid.sat, 23, + "incorrect value for states[11].mesid.sat, expected 23, is {}", + msg.states[11].mesid.sat + ); + assert_eq!( + msg.states[12].cn0, 223, + "incorrect value for states[12].cn0, expected 223, is {}", + msg.states[12].cn0 + ); + assert_eq!( + msg.states[12].mesid.code, 0, + "incorrect value for states[12].mesid.code, expected 0, is {}", + msg.states[12].mesid.code + ); + assert_eq!( + msg.states[12].mesid.sat, 10, + "incorrect value for states[12].mesid.sat, expected 10, is {}", + msg.states[12].mesid.sat + ); + assert_eq!( + msg.states[13].cn0, 0, + "incorrect value for states[13].cn0, expected 0, is {}", + msg.states[13].cn0 + ); + assert_eq!( + msg.states[13].mesid.code, 0, + "incorrect value for states[13].mesid.code, expected 0, is {}", + msg.states[13].mesid.code + ); + assert_eq!( + msg.states[13].mesid.sat, 0, + "incorrect value for states[13].mesid.sat, expected 0, is {}", + msg.states[13].mesid.sat + ); + assert_eq!( + msg.states[14].cn0, 0, + "incorrect value for states[14].cn0, expected 0, is {}", + msg.states[14].cn0 + ); + assert_eq!( + msg.states[14].mesid.code, 0, + "incorrect value for states[14].mesid.code, expected 0, is {}", + msg.states[14].mesid.code + ); + assert_eq!( + msg.states[14].mesid.sat, 0, + "incorrect value for states[14].mesid.sat, expected 0, is {}", + msg.states[14].mesid.sat + ); + assert_eq!( + msg.states[15].cn0, 0, + "incorrect value for states[15].cn0, expected 0, is {}", + msg.states[15].cn0 + ); + assert_eq!( + msg.states[15].mesid.code, 0, + "incorrect value for states[15].mesid.code, expected 0, is {}", + msg.states[15].mesid.code + ); + assert_eq!( + msg.states[15].mesid.sat, 0, + "incorrect value for states[15].mesid.sat, expected 0, is {}", + msg.states[15].mesid.sat + ); + assert_eq!( + msg.states[16].cn0, 0, + "incorrect value for states[16].cn0, expected 0, is {}", + msg.states[16].cn0 + ); + assert_eq!( + msg.states[16].mesid.code, 0, + "incorrect value for states[16].mesid.code, expected 0, is {}", + msg.states[16].mesid.code + ); + assert_eq!( + msg.states[16].mesid.sat, 0, + "incorrect value for states[16].mesid.sat, expected 0, is {}", + msg.states[16].mesid.sat + ); + assert_eq!( + msg.states[17].cn0, 202, + "incorrect value for states[17].cn0, expected 202, is {}", + msg.states[17].cn0 + ); + assert_eq!( + msg.states[17].mesid.code, 2, + "incorrect value for states[17].mesid.code, expected 2, is {}", + msg.states[17].mesid.code + ); + assert_eq!( + msg.states[17].mesid.sat, 131, + "incorrect value for states[17].mesid.sat, expected 131, is {}", + msg.states[17].mesid.sat + ); + assert_eq!( + msg.states[18].cn0, 192, + "incorrect value for states[18].cn0, expected 192, is {}", + msg.states[18].cn0 + ); + assert_eq!( + msg.states[18].mesid.code, 1, + "incorrect value for states[18].mesid.code, expected 1, is {}", + msg.states[18].mesid.code + ); + assert_eq!( + msg.states[18].mesid.sat, 27, + "incorrect value for states[18].mesid.sat, expected 27, is {}", + msg.states[18].mesid.sat + ); + assert_eq!( + msg.states[19].cn0, 165, + "incorrect value for states[19].cn0, expected 165, is {}", + msg.states[19].cn0 + ); + assert_eq!( + msg.states[19].mesid.code, 1, + "incorrect value for states[19].mesid.code, expected 1, is {}", + msg.states[19].mesid.code + ); + assert_eq!( + msg.states[19].mesid.sat, 15, + "incorrect value for states[19].mesid.sat, expected 15, is {}", + msg.states[19].mesid.sat + ); + assert_eq!( + msg.states[20].cn0, 146, + "incorrect value for states[20].cn0, expected 146, is {}", + msg.states[20].cn0 + ); + assert_eq!( + msg.states[20].mesid.code, 1, + "incorrect value for states[20].mesid.code, expected 1, is {}", + msg.states[20].mesid.code + ); + assert_eq!( + msg.states[20].mesid.sat, 29, + "incorrect value for states[20].mesid.sat, expected 29, is {}", + msg.states[20].mesid.sat + ); + assert_eq!( + msg.states[21].cn0, 170, + "incorrect value for states[21].cn0, expected 170, is {}", + msg.states[21].cn0 + ); + assert_eq!( + msg.states[21].mesid.code, 1, + "incorrect value for states[21].mesid.code, expected 1, is {}", + msg.states[21].mesid.code + ); + assert_eq!( + msg.states[21].mesid.sat, 32, + "incorrect value for states[21].mesid.sat, expected 32, is {}", + msg.states[21].mesid.sat + ); + assert_eq!( + msg.states[22].cn0, 201, + "incorrect value for states[22].cn0, expected 201, is {}", + msg.states[22].cn0 + ); + assert_eq!( + msg.states[22].mesid.code, 1, + "incorrect value for states[22].mesid.code, expected 1, is {}", + msg.states[22].mesid.code + ); + assert_eq!( + msg.states[22].mesid.sat, 18, + "incorrect value for states[22].mesid.sat, expected 18, is {}", + msg.states[22].mesid.sat + ); + assert_eq!( + msg.states[23].cn0, 0, + "incorrect value for states[23].cn0, expected 0, is {}", + msg.states[23].cn0 + ); + assert_eq!( + msg.states[23].mesid.code, 0, + "incorrect value for states[23].mesid.code, expected 0, is {}", + msg.states[23].mesid.code + ); + assert_eq!( + msg.states[23].mesid.sat, 0, + "incorrect value for states[23].mesid.sat, expected 0, is {}", + msg.states[23].mesid.sat + ); + assert_eq!( + msg.states[24].cn0, 0, + "incorrect value for states[24].cn0, expected 0, is {}", + msg.states[24].cn0 + ); + assert_eq!( + msg.states[24].mesid.code, 0, + "incorrect value for states[24].mesid.code, expected 0, is {}", + msg.states[24].mesid.code + ); + assert_eq!( + msg.states[24].mesid.sat, 0, + "incorrect value for states[24].mesid.sat, expected 0, is {}", + msg.states[24].mesid.sat + ); + assert_eq!( + msg.states[25].cn0, 0, + "incorrect value for states[25].cn0, expected 0, is {}", + msg.states[25].cn0 + ); + assert_eq!( + msg.states[25].mesid.code, 0, + "incorrect value for states[25].mesid.code, expected 0, is {}", + msg.states[25].mesid.code + ); + assert_eq!( + msg.states[25].mesid.sat, 0, + "incorrect value for states[25].mesid.sat, expected 0, is {}", + msg.states[25].mesid.sat + ); + assert_eq!( + msg.states[26].cn0, 212, + "incorrect value for states[26].cn0, expected 212, is {}", + msg.states[26].cn0 + ); + assert_eq!( + msg.states[26].mesid.code, 1, + "incorrect value for states[26].mesid.code, expected 1, is {}", + msg.states[26].mesid.code + ); + assert_eq!( + msg.states[26].mesid.sat, 23, + "incorrect value for states[26].mesid.sat, expected 23, is {}", + msg.states[26].mesid.sat + ); + assert_eq!( + msg.states[27].cn0, 205, + "incorrect value for states[27].cn0, expected 205, is {}", + msg.states[27].cn0 + ); + assert_eq!( + msg.states[27].mesid.code, 1, + "incorrect value for states[27].mesid.code, expected 1, is {}", + msg.states[27].mesid.code + ); + assert_eq!( + msg.states[27].mesid.sat, 10, + "incorrect value for states[27].mesid.sat, expected 10, is {}", + msg.states[27].mesid.sat + ); + assert_eq!( + msg.states[28].cn0, 0, + "incorrect value for states[28].cn0, expected 0, is {}", + msg.states[28].cn0 + ); + assert_eq!( + msg.states[28].mesid.code, 0, + "incorrect value for states[28].mesid.code, expected 0, is {}", + msg.states[28].mesid.code + ); + assert_eq!( + msg.states[28].mesid.sat, 0, + "incorrect value for states[28].mesid.sat, expected 0, is {}", + msg.states[28].mesid.sat + ); + assert_eq!( + msg.states[29].cn0, 230, + "incorrect value for states[29].cn0, expected 230, is {}", + msg.states[29].cn0 + ); + assert_eq!( + msg.states[29].mesid.code, 3, + "incorrect value for states[29].mesid.code, expected 3, is {}", + msg.states[29].mesid.code + ); + assert_eq!( + msg.states[29].mesid.sat, 96, + "incorrect value for states[29].mesid.sat, expected 96, is {}", + msg.states[29].mesid.sat + ); + assert_eq!( + msg.states[30].cn0, 0, + "incorrect value for states[30].cn0, expected 0, is {}", + msg.states[30].cn0 + ); + assert_eq!( + msg.states[30].mesid.code, 0, + "incorrect value for states[30].mesid.code, expected 0, is {}", + msg.states[30].mesid.code + ); + assert_eq!( + msg.states[30].mesid.sat, 0, + "incorrect value for states[30].mesid.sat, expected 0, is {}", + msg.states[30].mesid.sat + ); + assert_eq!( + msg.states[31].cn0, 214, + "incorrect value for states[31].cn0, expected 214, is {}", + msg.states[31].cn0 + ); + assert_eq!( + msg.states[31].mesid.code, 3, + "incorrect value for states[31].mesid.code, expected 3, is {}", + msg.states[31].mesid.code + ); + assert_eq!( + msg.states[31].mesid.sat, 101, + "incorrect value for states[31].mesid.sat, expected 101, is {}", + msg.states[31].mesid.sat + ); + assert_eq!( + msg.states[32].cn0, 212, + "incorrect value for states[32].cn0, expected 212, is {}", + msg.states[32].cn0 + ); + assert_eq!( + msg.states[32].mesid.code, 3, + "incorrect value for states[32].mesid.code, expected 3, is {}", + msg.states[32].mesid.code + ); + assert_eq!( + msg.states[32].mesid.sat, 103, + "incorrect value for states[32].mesid.sat, expected 103, is {}", + msg.states[32].mesid.sat + ); + assert_eq!( + msg.states[33].cn0, 209, + "incorrect value for states[33].cn0, expected 209, is {}", + msg.states[33].cn0 + ); + assert_eq!( + msg.states[33].mesid.code, 3, + "incorrect value for states[33].mesid.code, expected 3, is {}", + msg.states[33].mesid.code + ); + assert_eq!( + msg.states[33].mesid.sat, 104, + "incorrect value for states[33].mesid.sat, expected 104, is {}", + msg.states[33].mesid.sat + ); + assert_eq!( + msg.states[34].cn0, 157, + "incorrect value for states[34].cn0, expected 157, is {}", + msg.states[34].cn0 + ); + assert_eq!( + msg.states[34].mesid.code, 3, + "incorrect value for states[34].mesid.code, expected 3, is {}", + msg.states[34].mesid.code + ); + assert_eq!( + msg.states[34].mesid.sat, 106, + "incorrect value for states[34].mesid.sat, expected 106, is {}", + msg.states[34].mesid.sat + ); + assert_eq!( + msg.states[35].cn0, 230, + "incorrect value for states[35].cn0, expected 230, is {}", + msg.states[35].cn0 + ); + assert_eq!( + msg.states[35].mesid.code, 3, + "incorrect value for states[35].mesid.code, expected 3, is {}", + msg.states[35].mesid.code + ); + assert_eq!( + msg.states[35].mesid.sat, 102, + "incorrect value for states[35].mesid.sat, expected 102, is {}", + msg.states[35].mesid.sat + ); + assert_eq!( + msg.states[36].cn0, 0, + "incorrect value for states[36].cn0, expected 0, is {}", + msg.states[36].cn0 + ); + assert_eq!( + msg.states[36].mesid.code, 0, + "incorrect value for states[36].mesid.code, expected 0, is {}", + msg.states[36].mesid.code + ); + assert_eq!( + msg.states[36].mesid.sat, 0, + "incorrect value for states[36].mesid.sat, expected 0, is {}", + msg.states[36].mesid.sat + ); + assert_eq!( + msg.states[37].cn0, 0, + "incorrect value for states[37].cn0, expected 0, is {}", + msg.states[37].cn0 + ); + assert_eq!( + msg.states[37].mesid.code, 0, + "incorrect value for states[37].mesid.code, expected 0, is {}", + msg.states[37].mesid.code + ); + assert_eq!( + msg.states[37].mesid.sat, 0, + "incorrect value for states[37].mesid.sat, expected 0, is {}", + msg.states[37].mesid.sat + ); + assert_eq!( + msg.states[38].cn0, 189, + "incorrect value for states[38].cn0, expected 189, is {}", + msg.states[38].cn0 + ); + assert_eq!( + msg.states[38].mesid.code, 4, + "incorrect value for states[38].mesid.code, expected 4, is {}", + msg.states[38].mesid.code + ); + assert_eq!( + msg.states[38].mesid.sat, 101, + "incorrect value for states[38].mesid.sat, expected 101, is {}", + msg.states[38].mesid.sat + ); + assert_eq!( + msg.states[39].cn0, 207, + "incorrect value for states[39].cn0, expected 207, is {}", + msg.states[39].cn0 + ); + assert_eq!( + msg.states[39].mesid.code, 4, + "incorrect value for states[39].mesid.code, expected 4, is {}", + msg.states[39].mesid.code + ); + assert_eq!( + msg.states[39].mesid.sat, 96, + "incorrect value for states[39].mesid.sat, expected 96, is {}", + msg.states[39].mesid.sat + ); + assert_eq!( + msg.states[40].cn0, 164, + "incorrect value for states[40].cn0, expected 164, is {}", + msg.states[40].cn0 + ); + assert_eq!( + msg.states[40].mesid.code, 4, + "incorrect value for states[40].mesid.code, expected 4, is {}", + msg.states[40].mesid.code + ); + assert_eq!( + msg.states[40].mesid.sat, 106, + "incorrect value for states[40].mesid.sat, expected 106, is {}", + msg.states[40].mesid.sat + ); + assert_eq!( + msg.states[41].cn0, 193, + "incorrect value for states[41].cn0, expected 193, is {}", + msg.states[41].cn0 + ); + assert_eq!( + msg.states[41].mesid.code, 4, + "incorrect value for states[41].mesid.code, expected 4, is {}", + msg.states[41].mesid.code + ); + assert_eq!( + msg.states[41].mesid.sat, 104, + "incorrect value for states[41].mesid.sat, expected 104, is {}", + msg.states[41].mesid.sat + ); + assert_eq!( + msg.states[42].cn0, 0, + "incorrect value for states[42].cn0, expected 0, is {}", + msg.states[42].cn0 + ); + assert_eq!( + msg.states[42].mesid.code, 0, + "incorrect value for states[42].mesid.code, expected 0, is {}", + msg.states[42].mesid.code + ); + assert_eq!( + msg.states[42].mesid.sat, 0, + "incorrect value for states[42].mesid.sat, expected 0, is {}", + msg.states[42].mesid.sat + ); + assert_eq!( + msg.states[43].cn0, 208, + "incorrect value for states[43].cn0, expected 208, is {}", + msg.states[43].cn0 + ); + assert_eq!( + msg.states[43].mesid.code, 4, + "incorrect value for states[43].mesid.code, expected 4, is {}", + msg.states[43].mesid.code + ); + assert_eq!( + msg.states[43].mesid.sat, 102, + "incorrect value for states[43].mesid.sat, expected 102, is {}", + msg.states[43].mesid.sat + ); + assert_eq!( + msg.states[44].cn0, 0, + "incorrect value for states[44].cn0, expected 0, is {}", + msg.states[44].cn0 + ); + assert_eq!( + msg.states[44].mesid.code, 0, + "incorrect value for states[44].mesid.code, expected 0, is {}", + msg.states[44].mesid.code + ); + assert_eq!( + msg.states[44].mesid.sat, 0, + "incorrect value for states[44].mesid.sat, expected 0, is {}", + msg.states[44].mesid.sat + ); + assert_eq!( + msg.states[45].cn0, 212, + "incorrect value for states[45].cn0, expected 212, is {}", + msg.states[45].cn0 + ); + assert_eq!( + msg.states[45].mesid.code, 12, + "incorrect value for states[45].mesid.code, expected 12, is {}", + msg.states[45].mesid.code + ); + assert_eq!( + msg.states[45].mesid.sat, 27, + "incorrect value for states[45].mesid.sat, expected 27, is {}", + msg.states[45].mesid.sat + ); + assert_eq!( + msg.states[46].cn0, 161, + "incorrect value for states[46].cn0, expected 161, is {}", + msg.states[46].cn0 + ); + assert_eq!( + msg.states[46].mesid.code, 12, + "incorrect value for states[46].mesid.code, expected 12, is {}", + msg.states[46].mesid.code + ); + assert_eq!( + msg.states[46].mesid.sat, 29, + "incorrect value for states[46].mesid.sat, expected 29, is {}", + msg.states[46].mesid.sat + ); + assert_eq!( + msg.states[47].cn0, 216, + "incorrect value for states[47].cn0, expected 216, is {}", + msg.states[47].cn0 + ); + assert_eq!( + msg.states[47].mesid.code, 12, + "incorrect value for states[47].mesid.code, expected 12, is {}", + msg.states[47].mesid.code + ); + assert_eq!( + msg.states[47].mesid.sat, 32, + "incorrect value for states[47].mesid.sat, expected 32, is {}", + msg.states[47].mesid.sat + ); + assert_eq!( + msg.states[48].cn0, 216, + "incorrect value for states[48].cn0, expected 216, is {}", + msg.states[48].cn0 + ); + assert_eq!( + msg.states[48].mesid.code, 12, + "incorrect value for states[48].mesid.code, expected 12, is {}", + msg.states[48].mesid.code + ); + assert_eq!( + msg.states[48].mesid.sat, 30, + "incorrect value for states[48].mesid.sat, expected 30, is {}", + msg.states[48].mesid.sat + ); + assert_eq!( + msg.states[49].cn0, 178, + "incorrect value for states[49].cn0, expected 178, is {}", + msg.states[49].cn0 + ); + assert_eq!( + msg.states[49].mesid.code, 12, + "incorrect value for states[49].mesid.code, expected 12, is {}", + msg.states[49].mesid.code + ); + assert_eq!( + msg.states[49].mesid.sat, 20, + "incorrect value for states[49].mesid.sat, expected 20, is {}", + msg.states[49].mesid.sat + ); + assert_eq!( + msg.states[50].cn0, 0, + "incorrect value for states[50].cn0, expected 0, is {}", + msg.states[50].cn0 + ); + assert_eq!( + msg.states[50].mesid.code, 0, + "incorrect value for states[50].mesid.code, expected 0, is {}", + msg.states[50].mesid.code + ); + assert_eq!( + msg.states[50].mesid.sat, 0, + "incorrect value for states[50].mesid.sat, expected 0, is {}", + msg.states[50].mesid.sat + ); + assert_eq!( + msg.states[51].cn0, 0, + "incorrect value for states[51].cn0, expected 0, is {}", + msg.states[51].cn0 + ); + assert_eq!( + msg.states[51].mesid.code, 0, + "incorrect value for states[51].mesid.code, expected 0, is {}", + msg.states[51].mesid.code + ); + assert_eq!( + msg.states[51].mesid.sat, 0, + "incorrect value for states[51].mesid.sat, expected 0, is {}", + msg.states[51].mesid.sat + ); + assert_eq!( + msg.states[52].cn0, 0, + "incorrect value for states[52].cn0, expected 0, is {}", + msg.states[52].cn0 + ); + assert_eq!( + msg.states[52].mesid.code, 0, + "incorrect value for states[52].mesid.code, expected 0, is {}", + msg.states[52].mesid.code + ); + assert_eq!( + msg.states[52].mesid.sat, 0, + "incorrect value for states[52].mesid.sat, expected 0, is {}", + msg.states[52].mesid.sat + ); + assert_eq!( + msg.states[53].cn0, 0, + "incorrect value for states[53].cn0, expected 0, is {}", + msg.states[53].cn0 + ); + assert_eq!( + msg.states[53].mesid.code, 0, + "incorrect value for states[53].mesid.code, expected 0, is {}", + msg.states[53].mesid.code + ); + assert_eq!( + msg.states[53].mesid.sat, 0, + "incorrect value for states[53].mesid.sat, expected 0, is {}", + msg.states[53].mesid.sat + ); + assert_eq!( + msg.states[54].cn0, 0, + "incorrect value for states[54].cn0, expected 0, is {}", + msg.states[54].cn0 + ); + assert_eq!( + msg.states[54].mesid.code, 0, + "incorrect value for states[54].mesid.code, expected 0, is {}", + msg.states[54].mesid.code + ); + assert_eq!( + msg.states[54].mesid.sat, 0, + "incorrect value for states[54].mesid.sat, expected 0, is {}", + msg.states[54].mesid.sat + ); + assert_eq!( + msg.states[55].cn0, 0, + "incorrect value for states[55].cn0, expected 0, is {}", + msg.states[55].cn0 + ); + assert_eq!( + msg.states[55].mesid.code, 0, + "incorrect value for states[55].mesid.code, expected 0, is {}", + msg.states[55].mesid.code + ); + assert_eq!( + msg.states[55].mesid.sat, 0, + "incorrect value for states[55].mesid.sat, expected 0, is {}", + msg.states[55].mesid.sat + ); + assert_eq!( + msg.states[56].cn0, 0, + "incorrect value for states[56].cn0, expected 0, is {}", + msg.states[56].cn0 + ); + assert_eq!( + msg.states[56].mesid.code, 0, + "incorrect value for states[56].mesid.code, expected 0, is {}", + msg.states[56].mesid.code + ); + assert_eq!( + msg.states[56].mesid.sat, 0, + "incorrect value for states[56].mesid.sat, expected 0, is {}", + msg.states[56].mesid.sat + ); + assert_eq!( + msg.states[57].cn0, 0, + "incorrect value for states[57].cn0, expected 0, is {}", + msg.states[57].cn0 + ); + assert_eq!( + msg.states[57].mesid.code, 0, + "incorrect value for states[57].mesid.code, expected 0, is {}", + msg.states[57].mesid.code + ); + assert_eq!( + msg.states[57].mesid.sat, 0, + "incorrect value for states[57].mesid.sat, expected 0, is {}", + msg.states[57].mesid.sat + ); + assert_eq!( + msg.states[58].cn0, 0, + "incorrect value for states[58].cn0, expected 0, is {}", + msg.states[58].cn0 + ); + assert_eq!( + msg.states[58].mesid.code, 0, + "incorrect value for states[58].mesid.code, expected 0, is {}", + msg.states[58].mesid.code + ); + assert_eq!( + msg.states[58].mesid.sat, 0, + "incorrect value for states[58].mesid.sat, expected 0, is {}", + msg.states[58].mesid.sat + ); + assert_eq!( + msg.states[59].cn0, 0, + "incorrect value for states[59].cn0, expected 0, is {}", + msg.states[59].cn0 + ); + assert_eq!( + msg.states[59].mesid.code, 0, + "incorrect value for states[59].mesid.code, expected 0, is {}", + msg.states[59].mesid.code + ); + assert_eq!( + msg.states[59].mesid.sat, 0, + "incorrect value for states[59].mesid.sat, expected 0, is {}", + msg.states[59].mesid.sat + ); + assert_eq!( + msg.states[60].cn0, 0, + "incorrect value for states[60].cn0, expected 0, is {}", + msg.states[60].cn0 + ); + assert_eq!( + msg.states[60].mesid.code, 0, + "incorrect value for states[60].mesid.code, expected 0, is {}", + msg.states[60].mesid.code + ); + assert_eq!( + msg.states[60].mesid.sat, 0, + "incorrect value for states[60].mesid.sat, expected 0, is {}", + msg.states[60].mesid.sat + ); + assert_eq!( + msg.states[61].cn0, 0, + "incorrect value for states[61].cn0, expected 0, is {}", + msg.states[61].cn0 + ); + assert_eq!( + msg.states[61].mesid.code, 0, + "incorrect value for states[61].mesid.code, expected 0, is {}", + msg.states[61].mesid.code + ); + assert_eq!( + msg.states[61].mesid.sat, 0, + "incorrect value for states[61].mesid.sat, expected 0, is {}", + msg.states[61].mesid.sat + ); + assert_eq!( + msg.states[62].cn0, 0, + "incorrect value for states[62].cn0, expected 0, is {}", + msg.states[62].cn0 + ); + assert_eq!( + msg.states[62].mesid.code, 0, + "incorrect value for states[62].mesid.code, expected 0, is {}", + msg.states[62].mesid.code + ); + assert_eq!( + msg.states[62].mesid.sat, 0, + "incorrect value for states[62].mesid.sat, expected 0, is {}", + msg.states[62].mesid.sat + ); + assert_eq!( + msg.states[63].cn0, 203, + "incorrect value for states[63].cn0, expected 203, is {}", + msg.states[63].cn0 + ); + assert_eq!( + msg.states[63].mesid.code, 14, + "incorrect value for states[63].mesid.code, expected 14, is {}", + msg.states[63].mesid.code + ); + assert_eq!( + msg.states[63].mesid.sat, 36, + "incorrect value for states[63].mesid.sat, expected 36, is {}", + msg.states[63].mesid.sat + ); + assert_eq!( + msg.states[64].cn0, 0, + "incorrect value for states[64].cn0, expected 0, is {}", + msg.states[64].cn0 + ); + assert_eq!( + msg.states[64].mesid.code, 0, + "incorrect value for states[64].mesid.code, expected 0, is {}", + msg.states[64].mesid.code + ); + assert_eq!( + msg.states[64].mesid.sat, 0, + "incorrect value for states[64].mesid.sat, expected 0, is {}", + msg.states[64].mesid.sat + ); + assert_eq!( + msg.states[65].cn0, 158, + "incorrect value for states[65].cn0, expected 158, is {}", + msg.states[65].cn0 + ); + assert_eq!( + msg.states[65].mesid.code, 14, + "incorrect value for states[65].mesid.code, expected 14, is {}", + msg.states[65].mesid.code + ); + assert_eq!( + msg.states[65].mesid.sat, 5, + "incorrect value for states[65].mesid.sat, expected 5, is {}", + msg.states[65].mesid.sat + ); + assert_eq!( + msg.states[66].cn0, 194, + "incorrect value for states[66].cn0, expected 194, is {}", + msg.states[66].cn0 + ); + assert_eq!( + msg.states[66].mesid.code, 14, + "incorrect value for states[66].mesid.code, expected 14, is {}", + msg.states[66].mesid.code + ); + assert_eq!( + msg.states[66].mesid.sat, 4, + "incorrect value for states[66].mesid.sat, expected 4, is {}", + msg.states[66].mesid.sat + ); + assert_eq!( + msg.states[67].cn0, 192, + "incorrect value for states[67].cn0, expected 192, is {}", + msg.states[67].cn0 + ); + assert_eq!( + msg.states[67].mesid.code, 14, + "incorrect value for states[67].mesid.code, expected 14, is {}", + msg.states[67].mesid.code + ); + assert_eq!( + msg.states[67].mesid.sat, 11, + "incorrect value for states[67].mesid.sat, expected 11, is {}", + msg.states[67].mesid.sat + ); + assert_eq!( + msg.states[68].cn0, 207, + "incorrect value for states[68].cn0, expected 207, is {}", + msg.states[68].cn0 + ); + assert_eq!( + msg.states[68].mesid.code, 14, + "incorrect value for states[68].mesid.code, expected 14, is {}", + msg.states[68].mesid.code + ); + assert_eq!( + msg.states[68].mesid.sat, 9, + "incorrect value for states[68].mesid.sat, expected 9, is {}", + msg.states[68].mesid.sat + ); + assert_eq!( + msg.states[69].cn0, 0, + "incorrect value for states[69].cn0, expected 0, is {}", + msg.states[69].cn0 + ); + assert_eq!( + msg.states[69].mesid.code, 0, + "incorrect value for states[69].mesid.code, expected 0, is {}", + msg.states[69].mesid.code + ); + assert_eq!( + msg.states[69].mesid.sat, 0, + "incorrect value for states[69].mesid.sat, expected 0, is {}", + msg.states[69].mesid.sat + ); + assert_eq!( + msg.states[70].cn0, 0, + "incorrect value for states[70].cn0, expected 0, is {}", + msg.states[70].cn0 + ); + assert_eq!( + msg.states[70].mesid.code, 0, + "incorrect value for states[70].mesid.code, expected 0, is {}", + msg.states[70].mesid.code + ); + assert_eq!( + msg.states[70].mesid.sat, 0, + "incorrect value for states[70].mesid.sat, expected 0, is {}", + msg.states[70].mesid.sat + ); + assert_eq!( + msg.states[71].cn0, 0, + "incorrect value for states[71].cn0, expected 0, is {}", + msg.states[71].cn0 + ); + assert_eq!( + msg.states[71].mesid.code, 0, + "incorrect value for states[71].mesid.code, expected 0, is {}", + msg.states[71].mesid.code + ); + assert_eq!( + msg.states[71].mesid.sat, 0, + "incorrect value for states[71].mesid.sat, expected 0, is {}", + msg.states[71].mesid.sat + ); + assert_eq!( + msg.states[72].cn0, 218, + "incorrect value for states[72].cn0, expected 218, is {}", + msg.states[72].cn0 + ); + assert_eq!( + msg.states[72].mesid.code, 20, + "incorrect value for states[72].mesid.code, expected 20, is {}", + msg.states[72].mesid.code + ); + assert_eq!( + msg.states[72].mesid.sat, 9, + "incorrect value for states[72].mesid.sat, expected 9, is {}", + msg.states[72].mesid.sat + ); + assert_eq!( + msg.states[73].cn0, 176, + "incorrect value for states[73].cn0, expected 176, is {}", + msg.states[73].cn0 + ); + assert_eq!( + msg.states[73].mesid.code, 20, + "incorrect value for states[73].mesid.code, expected 20, is {}", + msg.states[73].mesid.code + ); + assert_eq!( + msg.states[73].mesid.sat, 5, + "incorrect value for states[73].mesid.sat, expected 5, is {}", + msg.states[73].mesid.sat + ); + assert_eq!( + msg.states[74].cn0, 217, + "incorrect value for states[74].cn0, expected 217, is {}", + msg.states[74].cn0 + ); + assert_eq!( + msg.states[74].mesid.code, 20, + "incorrect value for states[74].mesid.code, expected 20, is {}", + msg.states[74].mesid.code + ); + assert_eq!( + msg.states[74].mesid.sat, 36, + "incorrect value for states[74].mesid.sat, expected 36, is {}", + msg.states[74].mesid.sat + ); + assert_eq!( + msg.states[75].cn0, 200, + "incorrect value for states[75].cn0, expected 200, is {}", + msg.states[75].cn0 + ); + assert_eq!( + msg.states[75].mesid.code, 20, + "incorrect value for states[75].mesid.code, expected 20, is {}", + msg.states[75].mesid.code + ); + assert_eq!( + msg.states[75].mesid.sat, 11, + "incorrect value for states[75].mesid.sat, expected 11, is {}", + msg.states[75].mesid.sat + ); + assert_eq!( + msg.states[76].cn0, 205, + "incorrect value for states[76].cn0, expected 205, is {}", + msg.states[76].cn0 + ); + assert_eq!( + msg.states[76].mesid.code, 20, + "incorrect value for states[76].mesid.code, expected 20, is {}", + msg.states[76].mesid.code + ); + assert_eq!( + msg.states[76].mesid.sat, 4, + "incorrect value for states[76].mesid.sat, expected 4, is {}", + msg.states[76].mesid.sat + ); + assert_eq!( + msg.states[77].cn0, 0, + "incorrect value for states[77].cn0, expected 0, is {}", + msg.states[77].cn0 + ); + assert_eq!( + msg.states[77].mesid.code, 0, + "incorrect value for states[77].mesid.code, expected 0, is {}", + msg.states[77].mesid.code + ); + assert_eq!( + msg.states[77].mesid.sat, 0, + "incorrect value for states[77].mesid.sat, expected 0, is {}", + msg.states[77].mesid.sat + ); + assert_eq!( + msg.states[78].cn0, 0, + "incorrect value for states[78].cn0, expected 0, is {}", + msg.states[78].cn0 + ); + assert_eq!( + msg.states[78].mesid.code, 0, + "incorrect value for states[78].mesid.code, expected 0, is {}", + msg.states[78].mesid.code + ); + assert_eq!( + msg.states[78].mesid.sat, 0, + "incorrect value for states[78].mesid.sat, expected 0, is {}", + msg.states[78].mesid.sat + ); + } + _ => panic!("Invalid message type! Expected a MsgMeasurementState"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state.rs b/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state.rs index e7fe9e8870..f446a8704d 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_tracking_msg_tracking_state() { { @@ -1552,6 +1557,13 @@ fn test_auto_check_sbp_tracking_msg_tracking_state() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { @@ -1559,14 +1571,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { let json_input = r#"{"sender": 55286, "msg_type": 19, "states": [{"state": 1, "cn0": 39.24782180786133, "sid": {"code": 0, "reserved": 0, "sat": 202}}, {"state": 1, "cn0": 36.09756088256836, "sid": {"code": 0, "reserved": 0, "sat": 203}}, {"state": 1, "cn0": 37.62678527832031, "sid": {"code": 0, "reserved": 0, "sat": 208}}, {"state": 1, "cn0": 39.020729064941406, "sid": {"code": 0, "reserved": 0, "sat": 212}}, {"state": 1, "cn0": 42.03290557861328, "sid": {"code": 0, "reserved": 0, "sat": 217}}, {"state": 1, "cn0": 37.43546676635742, "sid": {"code": 0, "reserved": 0, "sat": 218}}, {"state": 1, "cn0": 38.4229621887207, "sid": {"code": 0, "reserved": 0, "sat": 220}}, {"state": 1, "cn0": 38.91520309448242, "sid": {"code": 0, "reserved": 0, "sat": 222}}, {"state": 1, "cn0": 42.62259292602539, "sid": {"code": 0, "reserved": 0, "sat": 225}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}], "crc": 25054, "length": 99, "preamble": 85, "payload": "AcoAAADF/RxCAcsAAADnYxBCAdAAAADUgRZCAdQAAAA6FRxCAdkAAACyIShCAdoAAADrvRVCAdwAAAAdsRlCAd4AAAArqRtCAeEAAACJfSpCAAAAAAAAAIC/AAAAAAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1869,14 +1881,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { let json_input = r#"{"sender": 55286, "msg_type": 19, "states": [{"state": 1, "cn0": 38.994117736816406, "sid": {"code": 0, "reserved": 0, "sat": 202}}, {"state": 1, "cn0": 34.889801025390625, "sid": {"code": 0, "reserved": 0, "sat": 203}}, {"state": 1, "cn0": 37.44603729248047, "sid": {"code": 0, "reserved": 0, "sat": 208}}, {"state": 1, "cn0": 38.72849655151367, "sid": {"code": 0, "reserved": 0, "sat": 212}}, {"state": 1, "cn0": 41.983219146728516, "sid": {"code": 0, "reserved": 0, "sat": 217}}, {"state": 1, "cn0": 37.46448516845703, "sid": {"code": 0, "reserved": 0, "sat": 218}}, {"state": 1, "cn0": 38.44300079345703, "sid": {"code": 0, "reserved": 0, "sat": 220}}, {"state": 1, "cn0": 39.03423309326172, "sid": {"code": 0, "reserved": 0, "sat": 222}}, {"state": 1, "cn0": 42.89944839477539, "sid": {"code": 0, "reserved": 0, "sat": 225}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}], "crc": 7956, "length": 99, "preamble": 85, "payload": "AcoAAAD6+RtCAcsAAAAojwtCAdAAAAC+yBVCAdQAAAD76RpCAdkAAADR7idCAdoAAACi2xVCAdwAAACixRlCAd4AAAAOIxxCAeEAAAAJmStCAAAAAAAAAIC/AAAAAAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2179,14 +2191,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { let json_input = r#"{"sender": 55286, "msg_type": 19, "states": [{"state": 1, "cn0": 38.95457077026367, "sid": {"code": 0, "reserved": 0, "sat": 202}}, {"state": 1, "cn0": 35.813316345214844, "sid": {"code": 0, "reserved": 0, "sat": 203}}, {"state": 1, "cn0": 37.553924560546875, "sid": {"code": 0, "reserved": 0, "sat": 208}}, {"state": 1, "cn0": 38.88901901245117, "sid": {"code": 0, "reserved": 0, "sat": 212}}, {"state": 1, "cn0": 42.4013557434082, "sid": {"code": 0, "reserved": 0, "sat": 217}}, {"state": 1, "cn0": 37.63916015625, "sid": {"code": 0, "reserved": 0, "sat": 218}}, {"state": 1, "cn0": 37.919986724853516, "sid": {"code": 0, "reserved": 0, "sat": 220}}, {"state": 1, "cn0": 39.25254440307617, "sid": {"code": 0, "reserved": 0, "sat": 222}}, {"state": 1, "cn0": 42.59827423095703, "sid": {"code": 0, "reserved": 0, "sat": 225}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}], "crc": 18409, "length": 99, "preamble": 85, "payload": "AcoAAAB70RtCAcsAAADWQA9CAdAAAAA4NxZCAdQAAABbjhtCAdkAAAD9milCAdoAAACAjhZCAdwAAAARrhdCAd4AAACbAh1CAeEAAACiZCpCAAAAAAAAAIC/AAAAAAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2489,14 +2501,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { let json_input = r#"{"sender": 55286, "msg_type": 19, "states": [{"state": 1, "cn0": 39.369598388671875, "sid": {"code": 0, "reserved": 0, "sat": 202}}, {"state": 1, "cn0": 36.52173614501953, "sid": {"code": 0, "reserved": 0, "sat": 203}}, {"state": 1, "cn0": 38.15976333618164, "sid": {"code": 0, "reserved": 0, "sat": 208}}, {"state": 1, "cn0": 39.19989776611328, "sid": {"code": 0, "reserved": 0, "sat": 212}}, {"state": 1, "cn0": 41.55845642089844, "sid": {"code": 0, "reserved": 0, "sat": 217}}, {"state": 1, "cn0": 37.026981353759766, "sid": {"code": 0, "reserved": 0, "sat": 218}}, {"state": 1, "cn0": 38.1049690246582, "sid": {"code": 0, "reserved": 0, "sat": 220}}, {"state": 1, "cn0": 39.04584503173828, "sid": {"code": 0, "reserved": 0, "sat": 222}}, {"state": 1, "cn0": 42.37783432006836, "sid": {"code": 0, "reserved": 0, "sat": 225}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}], "crc": 49481, "length": 99, "preamble": 85, "payload": "AcoAAAB4eh1CAcsAAABCFhJCAdAAAACZoxhCAdQAAACyzBxCAdkAAADcOyZCAdoAAAChGxRCAdwAAAB9axhCAd4AAADyLhxCAeEAAADngilCAAAAAAAAAIC/AAAAAAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2799,14 +2811,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { let json_input = r#"{"sender": 55286, "msg_type": 19, "states": [{"state": 1, "cn0": 39.70351791381836, "sid": {"code": 0, "reserved": 0, "sat": 202}}, {"state": 1, "cn0": 36.52388381958008, "sid": {"code": 0, "reserved": 0, "sat": 203}}, {"state": 1, "cn0": 37.169708251953125, "sid": {"code": 0, "reserved": 0, "sat": 208}}, {"state": 1, "cn0": 38.81692886352539, "sid": {"code": 0, "reserved": 0, "sat": 212}}, {"state": 1, "cn0": 42.05073165893555, "sid": {"code": 0, "reserved": 0, "sat": 217}}, {"state": 1, "cn0": 37.807498931884766, "sid": {"code": 0, "reserved": 0, "sat": 218}}, {"state": 1, "cn0": 37.71632385253906, "sid": {"code": 0, "reserved": 0, "sat": 220}}, {"state": 1, "cn0": 38.5289192199707, "sid": {"code": 0, "reserved": 0, "sat": 222}}, {"state": 1, "cn0": 42.27101516723633, "sid": {"code": 0, "reserved": 0, "sat": 225}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}, {"state": 0, "cn0": -1.0, "sid": {"code": 0, "reserved": 0, "sat": 0}}], "crc": 12158, "length": 99, "preamble": 85, "payload": "AcoAAABn0B5CAcsAAAB1GBJCAdAAAADIrRRCAdQAAACJRBtCAdkAAADzMyhCAdoAAADhOhdCAdwAAACE3RZCAd4AAACdHRpCAeEAAACFFSlCAAAAAAAAAIC/AAAAAAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -3106,3 +3118,1646 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_tracking_msg_tracking_state`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_tracking_msg_tracking_state() { + { + let mut payload = Cursor::new(vec![ + 85, 19, 0, 246, 215, 99, 1, 202, 0, 0, 0, 197, 253, 28, 66, 1, 203, 0, 0, 0, 231, 99, + 16, 66, 1, 208, 0, 0, 0, 212, 129, 22, 66, 1, 212, 0, 0, 0, 58, 21, 28, 66, 1, 217, 0, + 0, 0, 178, 33, 40, 66, 1, 218, 0, 0, 0, 235, 189, 21, 66, 1, 220, 0, 0, 0, 29, 177, 25, + 66, 1, 222, 0, 0, 0, 43, 169, 27, 66, 1, 225, 0, 0, 0, 137, 125, 42, 66, 0, 0, 0, 0, 0, + 0, 0, 128, 191, 0, 0, 0, 0, 0, 0, 0, 128, 191, 222, 97, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x13, + "Incorrect message type, expected 0x13, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(3.92478218078613281e+01), + "incorrect value for states[0].cn0, expected 3.92478218078613281e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].sid.code, 0, + "incorrect value for states[0].sid.code, expected 0, is {}", + msg.states[0].sid.code + ); + assert_eq!( + msg.states[0].sid.reserved, 0, + "incorrect value for states[0].sid.reserved, expected 0, is {}", + msg.states[0].sid.reserved + ); + assert_eq!( + msg.states[0].sid.sat, 202, + "incorrect value for states[0].sid.sat, expected 202, is {}", + msg.states[0].sid.sat + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(3.60975608825683594e+01), + "incorrect value for states[1].cn0, expected 3.60975608825683594e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].sid.code, 0, + "incorrect value for states[1].sid.code, expected 0, is {}", + msg.states[1].sid.code + ); + assert_eq!( + msg.states[1].sid.reserved, 0, + "incorrect value for states[1].sid.reserved, expected 0, is {}", + msg.states[1].sid.reserved + ); + assert_eq!( + msg.states[1].sid.sat, 203, + "incorrect value for states[1].sid.sat, expected 203, is {}", + msg.states[1].sid.sat + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(3.76267852783203125e+01), + "incorrect value for states[2].cn0, expected 3.76267852783203125e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].sid.code, 0, + "incorrect value for states[2].sid.code, expected 0, is {}", + msg.states[2].sid.code + ); + assert_eq!( + msg.states[2].sid.reserved, 0, + "incorrect value for states[2].sid.reserved, expected 0, is {}", + msg.states[2].sid.reserved + ); + assert_eq!( + msg.states[2].sid.sat, 208, + "incorrect value for states[2].sid.sat, expected 208, is {}", + msg.states[2].sid.sat + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(3.90207290649414062e+01), + "incorrect value for states[3].cn0, expected 3.90207290649414062e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].sid.code, 0, + "incorrect value for states[3].sid.code, expected 0, is {}", + msg.states[3].sid.code + ); + assert_eq!( + msg.states[3].sid.reserved, 0, + "incorrect value for states[3].sid.reserved, expected 0, is {}", + msg.states[3].sid.reserved + ); + assert_eq!( + msg.states[3].sid.sat, 212, + "incorrect value for states[3].sid.sat, expected 212, is {}", + msg.states[3].sid.sat + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(4.20329055786132812e+01), + "incorrect value for states[4].cn0, expected 4.20329055786132812e+01, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].sid.code, 0, + "incorrect value for states[4].sid.code, expected 0, is {}", + msg.states[4].sid.code + ); + assert_eq!( + msg.states[4].sid.reserved, 0, + "incorrect value for states[4].sid.reserved, expected 0, is {}", + msg.states[4].sid.reserved + ); + assert_eq!( + msg.states[4].sid.sat, 217, + "incorrect value for states[4].sid.sat, expected 217, is {}", + msg.states[4].sid.sat + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(3.74354667663574219e+01), + "incorrect value for states[5].cn0, expected 3.74354667663574219e+01, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].sid.code, 0, + "incorrect value for states[5].sid.code, expected 0, is {}", + msg.states[5].sid.code + ); + assert_eq!( + msg.states[5].sid.reserved, 0, + "incorrect value for states[5].sid.reserved, expected 0, is {}", + msg.states[5].sid.reserved + ); + assert_eq!( + msg.states[5].sid.sat, 218, + "incorrect value for states[5].sid.sat, expected 218, is {}", + msg.states[5].sid.sat + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(3.84229621887207031e+01), + "incorrect value for states[6].cn0, expected 3.84229621887207031e+01, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].sid.code, 0, + "incorrect value for states[6].sid.code, expected 0, is {}", + msg.states[6].sid.code + ); + assert_eq!( + msg.states[6].sid.reserved, 0, + "incorrect value for states[6].sid.reserved, expected 0, is {}", + msg.states[6].sid.reserved + ); + assert_eq!( + msg.states[6].sid.sat, 220, + "incorrect value for states[6].sid.sat, expected 220, is {}", + msg.states[6].sid.sat + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(3.89152030944824219e+01), + "incorrect value for states[7].cn0, expected 3.89152030944824219e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].sid.code, 0, + "incorrect value for states[7].sid.code, expected 0, is {}", + msg.states[7].sid.code + ); + assert_eq!( + msg.states[7].sid.reserved, 0, + "incorrect value for states[7].sid.reserved, expected 0, is {}", + msg.states[7].sid.reserved + ); + assert_eq!( + msg.states[7].sid.sat, 222, + "incorrect value for states[7].sid.sat, expected 222, is {}", + msg.states[7].sid.sat + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(4.26225929260253906e+01), + "incorrect value for states[8].cn0, expected 4.26225929260253906e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].sid.code, 0, + "incorrect value for states[8].sid.code, expected 0, is {}", + msg.states[8].sid.code + ); + assert_eq!( + msg.states[8].sid.reserved, 0, + "incorrect value for states[8].sid.reserved, expected 0, is {}", + msg.states[8].sid.reserved + ); + assert_eq!( + msg.states[8].sid.sat, 225, + "incorrect value for states[8].sid.sat, expected 225, is {}", + msg.states[8].sid.sat + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].sid.code, 0, + "incorrect value for states[9].sid.code, expected 0, is {}", + msg.states[9].sid.code + ); + assert_eq!( + msg.states[9].sid.reserved, 0, + "incorrect value for states[9].sid.reserved, expected 0, is {}", + msg.states[9].sid.reserved + ); + assert_eq!( + msg.states[9].sid.sat, 0, + "incorrect value for states[9].sid.sat, expected 0, is {}", + msg.states[9].sid.sat + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].sid.code, 0, + "incorrect value for states[10].sid.code, expected 0, is {}", + msg.states[10].sid.code + ); + assert_eq!( + msg.states[10].sid.reserved, 0, + "incorrect value for states[10].sid.reserved, expected 0, is {}", + msg.states[10].sid.reserved + ); + assert_eq!( + msg.states[10].sid.sat, 0, + "incorrect value for states[10].sid.sat, expected 0, is {}", + msg.states[10].sid.sat + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 19, 0, 246, 215, 99, 1, 202, 0, 0, 0, 250, 249, 27, 66, 1, 203, 0, 0, 0, 40, 143, + 11, 66, 1, 208, 0, 0, 0, 190, 200, 21, 66, 1, 212, 0, 0, 0, 251, 233, 26, 66, 1, 217, + 0, 0, 0, 209, 238, 39, 66, 1, 218, 0, 0, 0, 162, 219, 21, 66, 1, 220, 0, 0, 0, 162, + 197, 25, 66, 1, 222, 0, 0, 0, 14, 35, 28, 66, 1, 225, 0, 0, 0, 9, 153, 43, 66, 0, 0, 0, + 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 0, 0, 0, 128, 191, 20, 31, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x13, + "Incorrect message type, expected 0x13, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(3.89941177368164062e+01), + "incorrect value for states[0].cn0, expected 3.89941177368164062e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].sid.code, 0, + "incorrect value for states[0].sid.code, expected 0, is {}", + msg.states[0].sid.code + ); + assert_eq!( + msg.states[0].sid.reserved, 0, + "incorrect value for states[0].sid.reserved, expected 0, is {}", + msg.states[0].sid.reserved + ); + assert_eq!( + msg.states[0].sid.sat, 202, + "incorrect value for states[0].sid.sat, expected 202, is {}", + msg.states[0].sid.sat + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(3.48898010253906250e+01), + "incorrect value for states[1].cn0, expected 3.48898010253906250e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].sid.code, 0, + "incorrect value for states[1].sid.code, expected 0, is {}", + msg.states[1].sid.code + ); + assert_eq!( + msg.states[1].sid.reserved, 0, + "incorrect value for states[1].sid.reserved, expected 0, is {}", + msg.states[1].sid.reserved + ); + assert_eq!( + msg.states[1].sid.sat, 203, + "incorrect value for states[1].sid.sat, expected 203, is {}", + msg.states[1].sid.sat + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(3.74460372924804688e+01), + "incorrect value for states[2].cn0, expected 3.74460372924804688e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].sid.code, 0, + "incorrect value for states[2].sid.code, expected 0, is {}", + msg.states[2].sid.code + ); + assert_eq!( + msg.states[2].sid.reserved, 0, + "incorrect value for states[2].sid.reserved, expected 0, is {}", + msg.states[2].sid.reserved + ); + assert_eq!( + msg.states[2].sid.sat, 208, + "incorrect value for states[2].sid.sat, expected 208, is {}", + msg.states[2].sid.sat + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(3.87284965515136719e+01), + "incorrect value for states[3].cn0, expected 3.87284965515136719e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].sid.code, 0, + "incorrect value for states[3].sid.code, expected 0, is {}", + msg.states[3].sid.code + ); + assert_eq!( + msg.states[3].sid.reserved, 0, + "incorrect value for states[3].sid.reserved, expected 0, is {}", + msg.states[3].sid.reserved + ); + assert_eq!( + msg.states[3].sid.sat, 212, + "incorrect value for states[3].sid.sat, expected 212, is {}", + msg.states[3].sid.sat + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(4.19832191467285156e+01), + "incorrect value for states[4].cn0, expected 4.19832191467285156e+01, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].sid.code, 0, + "incorrect value for states[4].sid.code, expected 0, is {}", + msg.states[4].sid.code + ); + assert_eq!( + msg.states[4].sid.reserved, 0, + "incorrect value for states[4].sid.reserved, expected 0, is {}", + msg.states[4].sid.reserved + ); + assert_eq!( + msg.states[4].sid.sat, 217, + "incorrect value for states[4].sid.sat, expected 217, is {}", + msg.states[4].sid.sat + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(3.74644851684570312e+01), + "incorrect value for states[5].cn0, expected 3.74644851684570312e+01, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].sid.code, 0, + "incorrect value for states[5].sid.code, expected 0, is {}", + msg.states[5].sid.code + ); + assert_eq!( + msg.states[5].sid.reserved, 0, + "incorrect value for states[5].sid.reserved, expected 0, is {}", + msg.states[5].sid.reserved + ); + assert_eq!( + msg.states[5].sid.sat, 218, + "incorrect value for states[5].sid.sat, expected 218, is {}", + msg.states[5].sid.sat + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(3.84430007934570312e+01), + "incorrect value for states[6].cn0, expected 3.84430007934570312e+01, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].sid.code, 0, + "incorrect value for states[6].sid.code, expected 0, is {}", + msg.states[6].sid.code + ); + assert_eq!( + msg.states[6].sid.reserved, 0, + "incorrect value for states[6].sid.reserved, expected 0, is {}", + msg.states[6].sid.reserved + ); + assert_eq!( + msg.states[6].sid.sat, 220, + "incorrect value for states[6].sid.sat, expected 220, is {}", + msg.states[6].sid.sat + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(3.90342330932617188e+01), + "incorrect value for states[7].cn0, expected 3.90342330932617188e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].sid.code, 0, + "incorrect value for states[7].sid.code, expected 0, is {}", + msg.states[7].sid.code + ); + assert_eq!( + msg.states[7].sid.reserved, 0, + "incorrect value for states[7].sid.reserved, expected 0, is {}", + msg.states[7].sid.reserved + ); + assert_eq!( + msg.states[7].sid.sat, 222, + "incorrect value for states[7].sid.sat, expected 222, is {}", + msg.states[7].sid.sat + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(4.28994483947753906e+01), + "incorrect value for states[8].cn0, expected 4.28994483947753906e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].sid.code, 0, + "incorrect value for states[8].sid.code, expected 0, is {}", + msg.states[8].sid.code + ); + assert_eq!( + msg.states[8].sid.reserved, 0, + "incorrect value for states[8].sid.reserved, expected 0, is {}", + msg.states[8].sid.reserved + ); + assert_eq!( + msg.states[8].sid.sat, 225, + "incorrect value for states[8].sid.sat, expected 225, is {}", + msg.states[8].sid.sat + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].sid.code, 0, + "incorrect value for states[9].sid.code, expected 0, is {}", + msg.states[9].sid.code + ); + assert_eq!( + msg.states[9].sid.reserved, 0, + "incorrect value for states[9].sid.reserved, expected 0, is {}", + msg.states[9].sid.reserved + ); + assert_eq!( + msg.states[9].sid.sat, 0, + "incorrect value for states[9].sid.sat, expected 0, is {}", + msg.states[9].sid.sat + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].sid.code, 0, + "incorrect value for states[10].sid.code, expected 0, is {}", + msg.states[10].sid.code + ); + assert_eq!( + msg.states[10].sid.reserved, 0, + "incorrect value for states[10].sid.reserved, expected 0, is {}", + msg.states[10].sid.reserved + ); + assert_eq!( + msg.states[10].sid.sat, 0, + "incorrect value for states[10].sid.sat, expected 0, is {}", + msg.states[10].sid.sat + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 19, 0, 246, 215, 99, 1, 202, 0, 0, 0, 123, 209, 27, 66, 1, 203, 0, 0, 0, 214, 64, + 15, 66, 1, 208, 0, 0, 0, 56, 55, 22, 66, 1, 212, 0, 0, 0, 91, 142, 27, 66, 1, 217, 0, + 0, 0, 253, 154, 41, 66, 1, 218, 0, 0, 0, 128, 142, 22, 66, 1, 220, 0, 0, 0, 17, 174, + 23, 66, 1, 222, 0, 0, 0, 155, 2, 29, 66, 1, 225, 0, 0, 0, 162, 100, 42, 66, 0, 0, 0, 0, + 0, 0, 0, 128, 191, 0, 0, 0, 0, 0, 0, 0, 128, 191, 233, 71, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x13, + "Incorrect message type, expected 0x13, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(3.89545707702636719e+01), + "incorrect value for states[0].cn0, expected 3.89545707702636719e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].sid.code, 0, + "incorrect value for states[0].sid.code, expected 0, is {}", + msg.states[0].sid.code + ); + assert_eq!( + msg.states[0].sid.reserved, 0, + "incorrect value for states[0].sid.reserved, expected 0, is {}", + msg.states[0].sid.reserved + ); + assert_eq!( + msg.states[0].sid.sat, 202, + "incorrect value for states[0].sid.sat, expected 202, is {}", + msg.states[0].sid.sat + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(3.58133163452148438e+01), + "incorrect value for states[1].cn0, expected 3.58133163452148438e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].sid.code, 0, + "incorrect value for states[1].sid.code, expected 0, is {}", + msg.states[1].sid.code + ); + assert_eq!( + msg.states[1].sid.reserved, 0, + "incorrect value for states[1].sid.reserved, expected 0, is {}", + msg.states[1].sid.reserved + ); + assert_eq!( + msg.states[1].sid.sat, 203, + "incorrect value for states[1].sid.sat, expected 203, is {}", + msg.states[1].sid.sat + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(3.75539245605468750e+01), + "incorrect value for states[2].cn0, expected 3.75539245605468750e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].sid.code, 0, + "incorrect value for states[2].sid.code, expected 0, is {}", + msg.states[2].sid.code + ); + assert_eq!( + msg.states[2].sid.reserved, 0, + "incorrect value for states[2].sid.reserved, expected 0, is {}", + msg.states[2].sid.reserved + ); + assert_eq!( + msg.states[2].sid.sat, 208, + "incorrect value for states[2].sid.sat, expected 208, is {}", + msg.states[2].sid.sat + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(3.88890190124511719e+01), + "incorrect value for states[3].cn0, expected 3.88890190124511719e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].sid.code, 0, + "incorrect value for states[3].sid.code, expected 0, is {}", + msg.states[3].sid.code + ); + assert_eq!( + msg.states[3].sid.reserved, 0, + "incorrect value for states[3].sid.reserved, expected 0, is {}", + msg.states[3].sid.reserved + ); + assert_eq!( + msg.states[3].sid.sat, 212, + "incorrect value for states[3].sid.sat, expected 212, is {}", + msg.states[3].sid.sat + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(4.24013557434082031e+01), + "incorrect value for states[4].cn0, expected 4.24013557434082031e+01, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].sid.code, 0, + "incorrect value for states[4].sid.code, expected 0, is {}", + msg.states[4].sid.code + ); + assert_eq!( + msg.states[4].sid.reserved, 0, + "incorrect value for states[4].sid.reserved, expected 0, is {}", + msg.states[4].sid.reserved + ); + assert_eq!( + msg.states[4].sid.sat, 217, + "incorrect value for states[4].sid.sat, expected 217, is {}", + msg.states[4].sid.sat + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(3.76391601562500000e+01), + "incorrect value for states[5].cn0, expected 3.76391601562500000e+01, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].sid.code, 0, + "incorrect value for states[5].sid.code, expected 0, is {}", + msg.states[5].sid.code + ); + assert_eq!( + msg.states[5].sid.reserved, 0, + "incorrect value for states[5].sid.reserved, expected 0, is {}", + msg.states[5].sid.reserved + ); + assert_eq!( + msg.states[5].sid.sat, 218, + "incorrect value for states[5].sid.sat, expected 218, is {}", + msg.states[5].sid.sat + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(3.79199867248535156e+01), + "incorrect value for states[6].cn0, expected 3.79199867248535156e+01, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].sid.code, 0, + "incorrect value for states[6].sid.code, expected 0, is {}", + msg.states[6].sid.code + ); + assert_eq!( + msg.states[6].sid.reserved, 0, + "incorrect value for states[6].sid.reserved, expected 0, is {}", + msg.states[6].sid.reserved + ); + assert_eq!( + msg.states[6].sid.sat, 220, + "incorrect value for states[6].sid.sat, expected 220, is {}", + msg.states[6].sid.sat + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(3.92525444030761719e+01), + "incorrect value for states[7].cn0, expected 3.92525444030761719e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].sid.code, 0, + "incorrect value for states[7].sid.code, expected 0, is {}", + msg.states[7].sid.code + ); + assert_eq!( + msg.states[7].sid.reserved, 0, + "incorrect value for states[7].sid.reserved, expected 0, is {}", + msg.states[7].sid.reserved + ); + assert_eq!( + msg.states[7].sid.sat, 222, + "incorrect value for states[7].sid.sat, expected 222, is {}", + msg.states[7].sid.sat + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(4.25982742309570312e+01), + "incorrect value for states[8].cn0, expected 4.25982742309570312e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].sid.code, 0, + "incorrect value for states[8].sid.code, expected 0, is {}", + msg.states[8].sid.code + ); + assert_eq!( + msg.states[8].sid.reserved, 0, + "incorrect value for states[8].sid.reserved, expected 0, is {}", + msg.states[8].sid.reserved + ); + assert_eq!( + msg.states[8].sid.sat, 225, + "incorrect value for states[8].sid.sat, expected 225, is {}", + msg.states[8].sid.sat + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].sid.code, 0, + "incorrect value for states[9].sid.code, expected 0, is {}", + msg.states[9].sid.code + ); + assert_eq!( + msg.states[9].sid.reserved, 0, + "incorrect value for states[9].sid.reserved, expected 0, is {}", + msg.states[9].sid.reserved + ); + assert_eq!( + msg.states[9].sid.sat, 0, + "incorrect value for states[9].sid.sat, expected 0, is {}", + msg.states[9].sid.sat + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].sid.code, 0, + "incorrect value for states[10].sid.code, expected 0, is {}", + msg.states[10].sid.code + ); + assert_eq!( + msg.states[10].sid.reserved, 0, + "incorrect value for states[10].sid.reserved, expected 0, is {}", + msg.states[10].sid.reserved + ); + assert_eq!( + msg.states[10].sid.sat, 0, + "incorrect value for states[10].sid.sat, expected 0, is {}", + msg.states[10].sid.sat + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 19, 0, 246, 215, 99, 1, 202, 0, 0, 0, 120, 122, 29, 66, 1, 203, 0, 0, 0, 66, 22, + 18, 66, 1, 208, 0, 0, 0, 153, 163, 24, 66, 1, 212, 0, 0, 0, 178, 204, 28, 66, 1, 217, + 0, 0, 0, 220, 59, 38, 66, 1, 218, 0, 0, 0, 161, 27, 20, 66, 1, 220, 0, 0, 0, 125, 107, + 24, 66, 1, 222, 0, 0, 0, 242, 46, 28, 66, 1, 225, 0, 0, 0, 231, 130, 41, 66, 0, 0, 0, + 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 0, 0, 0, 128, 191, 73, 193, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x13, + "Incorrect message type, expected 0x13, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(3.93695983886718750e+01), + "incorrect value for states[0].cn0, expected 3.93695983886718750e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].sid.code, 0, + "incorrect value for states[0].sid.code, expected 0, is {}", + msg.states[0].sid.code + ); + assert_eq!( + msg.states[0].sid.reserved, 0, + "incorrect value for states[0].sid.reserved, expected 0, is {}", + msg.states[0].sid.reserved + ); + assert_eq!( + msg.states[0].sid.sat, 202, + "incorrect value for states[0].sid.sat, expected 202, is {}", + msg.states[0].sid.sat + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(3.65217361450195312e+01), + "incorrect value for states[1].cn0, expected 3.65217361450195312e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].sid.code, 0, + "incorrect value for states[1].sid.code, expected 0, is {}", + msg.states[1].sid.code + ); + assert_eq!( + msg.states[1].sid.reserved, 0, + "incorrect value for states[1].sid.reserved, expected 0, is {}", + msg.states[1].sid.reserved + ); + assert_eq!( + msg.states[1].sid.sat, 203, + "incorrect value for states[1].sid.sat, expected 203, is {}", + msg.states[1].sid.sat + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(3.81597633361816406e+01), + "incorrect value for states[2].cn0, expected 3.81597633361816406e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].sid.code, 0, + "incorrect value for states[2].sid.code, expected 0, is {}", + msg.states[2].sid.code + ); + assert_eq!( + msg.states[2].sid.reserved, 0, + "incorrect value for states[2].sid.reserved, expected 0, is {}", + msg.states[2].sid.reserved + ); + assert_eq!( + msg.states[2].sid.sat, 208, + "incorrect value for states[2].sid.sat, expected 208, is {}", + msg.states[2].sid.sat + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(3.91998977661132812e+01), + "incorrect value for states[3].cn0, expected 3.91998977661132812e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].sid.code, 0, + "incorrect value for states[3].sid.code, expected 0, is {}", + msg.states[3].sid.code + ); + assert_eq!( + msg.states[3].sid.reserved, 0, + "incorrect value for states[3].sid.reserved, expected 0, is {}", + msg.states[3].sid.reserved + ); + assert_eq!( + msg.states[3].sid.sat, 212, + "incorrect value for states[3].sid.sat, expected 212, is {}", + msg.states[3].sid.sat + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(4.15584564208984375e+01), + "incorrect value for states[4].cn0, expected 4.15584564208984375e+01, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].sid.code, 0, + "incorrect value for states[4].sid.code, expected 0, is {}", + msg.states[4].sid.code + ); + assert_eq!( + msg.states[4].sid.reserved, 0, + "incorrect value for states[4].sid.reserved, expected 0, is {}", + msg.states[4].sid.reserved + ); + assert_eq!( + msg.states[4].sid.sat, 217, + "incorrect value for states[4].sid.sat, expected 217, is {}", + msg.states[4].sid.sat + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(3.70269813537597656e+01), + "incorrect value for states[5].cn0, expected 3.70269813537597656e+01, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].sid.code, 0, + "incorrect value for states[5].sid.code, expected 0, is {}", + msg.states[5].sid.code + ); + assert_eq!( + msg.states[5].sid.reserved, 0, + "incorrect value for states[5].sid.reserved, expected 0, is {}", + msg.states[5].sid.reserved + ); + assert_eq!( + msg.states[5].sid.sat, 218, + "incorrect value for states[5].sid.sat, expected 218, is {}", + msg.states[5].sid.sat + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(3.81049690246582031e+01), + "incorrect value for states[6].cn0, expected 3.81049690246582031e+01, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].sid.code, 0, + "incorrect value for states[6].sid.code, expected 0, is {}", + msg.states[6].sid.code + ); + assert_eq!( + msg.states[6].sid.reserved, 0, + "incorrect value for states[6].sid.reserved, expected 0, is {}", + msg.states[6].sid.reserved + ); + assert_eq!( + msg.states[6].sid.sat, 220, + "incorrect value for states[6].sid.sat, expected 220, is {}", + msg.states[6].sid.sat + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(3.90458450317382812e+01), + "incorrect value for states[7].cn0, expected 3.90458450317382812e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].sid.code, 0, + "incorrect value for states[7].sid.code, expected 0, is {}", + msg.states[7].sid.code + ); + assert_eq!( + msg.states[7].sid.reserved, 0, + "incorrect value for states[7].sid.reserved, expected 0, is {}", + msg.states[7].sid.reserved + ); + assert_eq!( + msg.states[7].sid.sat, 222, + "incorrect value for states[7].sid.sat, expected 222, is {}", + msg.states[7].sid.sat + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(4.23778343200683594e+01), + "incorrect value for states[8].cn0, expected 4.23778343200683594e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].sid.code, 0, + "incorrect value for states[8].sid.code, expected 0, is {}", + msg.states[8].sid.code + ); + assert_eq!( + msg.states[8].sid.reserved, 0, + "incorrect value for states[8].sid.reserved, expected 0, is {}", + msg.states[8].sid.reserved + ); + assert_eq!( + msg.states[8].sid.sat, 225, + "incorrect value for states[8].sid.sat, expected 225, is {}", + msg.states[8].sid.sat + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].sid.code, 0, + "incorrect value for states[9].sid.code, expected 0, is {}", + msg.states[9].sid.code + ); + assert_eq!( + msg.states[9].sid.reserved, 0, + "incorrect value for states[9].sid.reserved, expected 0, is {}", + msg.states[9].sid.reserved + ); + assert_eq!( + msg.states[9].sid.sat, 0, + "incorrect value for states[9].sid.sat, expected 0, is {}", + msg.states[9].sid.sat + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].sid.code, 0, + "incorrect value for states[10].sid.code, expected 0, is {}", + msg.states[10].sid.code + ); + assert_eq!( + msg.states[10].sid.reserved, 0, + "incorrect value for states[10].sid.reserved, expected 0, is {}", + msg.states[10].sid.reserved + ); + assert_eq!( + msg.states[10].sid.sat, 0, + "incorrect value for states[10].sid.sat, expected 0, is {}", + msg.states[10].sid.sat + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 19, 0, 246, 215, 99, 1, 202, 0, 0, 0, 103, 208, 30, 66, 1, 203, 0, 0, 0, 117, 24, + 18, 66, 1, 208, 0, 0, 0, 200, 173, 20, 66, 1, 212, 0, 0, 0, 137, 68, 27, 66, 1, 217, 0, + 0, 0, 243, 51, 40, 66, 1, 218, 0, 0, 0, 225, 58, 23, 66, 1, 220, 0, 0, 0, 132, 221, 22, + 66, 1, 222, 0, 0, 0, 157, 29, 26, 66, 1, 225, 0, 0, 0, 133, 21, 41, 66, 0, 0, 0, 0, 0, + 0, 0, 128, 191, 0, 0, 0, 0, 0, 0, 0, 128, 191, 126, 47, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepB( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepB(msg) => { + assert_eq!( + msg.message_type(), + 0x13, + "Incorrect message type, expected 0x13, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0xd7f6, + "incorrect sender id, expected 0xd7f6, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(3.97035179138183594e+01), + "incorrect value for states[0].cn0, expected 3.97035179138183594e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].sid.code, 0, + "incorrect value for states[0].sid.code, expected 0, is {}", + msg.states[0].sid.code + ); + assert_eq!( + msg.states[0].sid.reserved, 0, + "incorrect value for states[0].sid.reserved, expected 0, is {}", + msg.states[0].sid.reserved + ); + assert_eq!( + msg.states[0].sid.sat, 202, + "incorrect value for states[0].sid.sat, expected 202, is {}", + msg.states[0].sid.sat + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(3.65238838195800781e+01), + "incorrect value for states[1].cn0, expected 3.65238838195800781e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].sid.code, 0, + "incorrect value for states[1].sid.code, expected 0, is {}", + msg.states[1].sid.code + ); + assert_eq!( + msg.states[1].sid.reserved, 0, + "incorrect value for states[1].sid.reserved, expected 0, is {}", + msg.states[1].sid.reserved + ); + assert_eq!( + msg.states[1].sid.sat, 203, + "incorrect value for states[1].sid.sat, expected 203, is {}", + msg.states[1].sid.sat + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(3.71697082519531250e+01), + "incorrect value for states[2].cn0, expected 3.71697082519531250e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].sid.code, 0, + "incorrect value for states[2].sid.code, expected 0, is {}", + msg.states[2].sid.code + ); + assert_eq!( + msg.states[2].sid.reserved, 0, + "incorrect value for states[2].sid.reserved, expected 0, is {}", + msg.states[2].sid.reserved + ); + assert_eq!( + msg.states[2].sid.sat, 208, + "incorrect value for states[2].sid.sat, expected 208, is {}", + msg.states[2].sid.sat + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(3.88169288635253906e+01), + "incorrect value for states[3].cn0, expected 3.88169288635253906e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].sid.code, 0, + "incorrect value for states[3].sid.code, expected 0, is {}", + msg.states[3].sid.code + ); + assert_eq!( + msg.states[3].sid.reserved, 0, + "incorrect value for states[3].sid.reserved, expected 0, is {}", + msg.states[3].sid.reserved + ); + assert_eq!( + msg.states[3].sid.sat, 212, + "incorrect value for states[3].sid.sat, expected 212, is {}", + msg.states[3].sid.sat + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(4.20507316589355469e+01), + "incorrect value for states[4].cn0, expected 4.20507316589355469e+01, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].sid.code, 0, + "incorrect value for states[4].sid.code, expected 0, is {}", + msg.states[4].sid.code + ); + assert_eq!( + msg.states[4].sid.reserved, 0, + "incorrect value for states[4].sid.reserved, expected 0, is {}", + msg.states[4].sid.reserved + ); + assert_eq!( + msg.states[4].sid.sat, 217, + "incorrect value for states[4].sid.sat, expected 217, is {}", + msg.states[4].sid.sat + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(3.78074989318847656e+01), + "incorrect value for states[5].cn0, expected 3.78074989318847656e+01, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].sid.code, 0, + "incorrect value for states[5].sid.code, expected 0, is {}", + msg.states[5].sid.code + ); + assert_eq!( + msg.states[5].sid.reserved, 0, + "incorrect value for states[5].sid.reserved, expected 0, is {}", + msg.states[5].sid.reserved + ); + assert_eq!( + msg.states[5].sid.sat, 218, + "incorrect value for states[5].sid.sat, expected 218, is {}", + msg.states[5].sid.sat + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(3.77163238525390625e+01), + "incorrect value for states[6].cn0, expected 3.77163238525390625e+01, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].sid.code, 0, + "incorrect value for states[6].sid.code, expected 0, is {}", + msg.states[6].sid.code + ); + assert_eq!( + msg.states[6].sid.reserved, 0, + "incorrect value for states[6].sid.reserved, expected 0, is {}", + msg.states[6].sid.reserved + ); + assert_eq!( + msg.states[6].sid.sat, 220, + "incorrect value for states[6].sid.sat, expected 220, is {}", + msg.states[6].sid.sat + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(3.85289192199707031e+01), + "incorrect value for states[7].cn0, expected 3.85289192199707031e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].sid.code, 0, + "incorrect value for states[7].sid.code, expected 0, is {}", + msg.states[7].sid.code + ); + assert_eq!( + msg.states[7].sid.reserved, 0, + "incorrect value for states[7].sid.reserved, expected 0, is {}", + msg.states[7].sid.reserved + ); + assert_eq!( + msg.states[7].sid.sat, 222, + "incorrect value for states[7].sid.sat, expected 222, is {}", + msg.states[7].sid.sat + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(4.22710151672363281e+01), + "incorrect value for states[8].cn0, expected 4.22710151672363281e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].sid.code, 0, + "incorrect value for states[8].sid.code, expected 0, is {}", + msg.states[8].sid.code + ); + assert_eq!( + msg.states[8].sid.reserved, 0, + "incorrect value for states[8].sid.reserved, expected 0, is {}", + msg.states[8].sid.reserved + ); + assert_eq!( + msg.states[8].sid.sat, 225, + "incorrect value for states[8].sid.sat, expected 225, is {}", + msg.states[8].sid.sat + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].sid.code, 0, + "incorrect value for states[9].sid.code, expected 0, is {}", + msg.states[9].sid.code + ); + assert_eq!( + msg.states[9].sid.reserved, 0, + "incorrect value for states[9].sid.reserved, expected 0, is {}", + msg.states[9].sid.reserved + ); + assert_eq!( + msg.states[9].sid.sat, 0, + "incorrect value for states[9].sid.sat, expected 0, is {}", + msg.states[9].sid.sat + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].sid.code, 0, + "incorrect value for states[10].sid.code, expected 0, is {}", + msg.states[10].sid.code + ); + assert_eq!( + msg.states[10].sid.reserved, 0, + "incorrect value for states[10].sid.reserved, expected 0, is {}", + msg.states[10].sid.reserved + ); + assert_eq!( + msg.states[10].sid.sat, 0, + "incorrect value for states[10].sid.sat, expected 0, is {}", + msg.states[10].sid.sat + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepB"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state_detailed_dep.rs b/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state_detailed_dep.rs index 7e8ab40ccd..c0915e9695 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state_detailed_dep.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_tracking_msg_tracking_state_detailed_dep.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { { @@ -792,6 +797,13 @@ fn test_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { @@ -799,14 +811,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { let json_input = r#"{"track_flags": 11, "doppler": 15701, "clock_offset": 0, "msg_type": 17, "lock": 14032, "nav_flags": 0, "P_std": 0, "L": {"i": 1319, "f": 169}, "pset_flags": 0, "P": 0, "misc_flags": 9, "preamble": 85, "payload": "o5dw1wEAAAAAAAAAAAAAAAAAAAAnBQAAqbHQNg8AAABVPQAAJwABAAAAAAAAACgAbAEACwAACQ==", "recv_time": 7909447587, "acceleration": 108, "uptime": 1, "sender": 26427, "cn0": 177, "doppler_std": 39, "tow_flags": 0, "tot": {"wn": 0, "tow": 0}, "crc": 54950, "length": 55, "clock_drift": 0, "sid": {"code": 0, "reserved": 0, "sat": 15}, "sync_flags": 1, "corr_spacing": 40}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -959,14 +971,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { let json_input = r#"{"track_flags": 11, "doppler": 15667, "clock_offset": 0, "msg_type": 17, "lock": 14032, "nav_flags": 0, "P_std": 0, "L": {"i": 1810, "f": 14}, "pset_flags": 0, "P": 0, "misc_flags": 9, "preamble": 85, "payload": "Yfs99QEAAAAAAAAAAAAAAAAAAAASBwAADq/QNg8AAAAzPQAAHgABAAAAAAAAACgA4AEACwAACQ==", "recv_time": 8409447265, "acceleration": -32, "uptime": 1, "sender": 26427, "cn0": 175, "doppler_std": 30, "tow_flags": 0, "tot": {"wn": 0, "tow": 0}, "crc": 45960, "length": 55, "clock_drift": 0, "sid": {"code": 0, "reserved": 0, "sat": 15}, "sync_flags": 1, "corr_spacing": 40}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1119,14 +1131,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { let json_input = r#"{"track_flags": 11, "doppler": 15683, "clock_offset": 0, "msg_type": 17, "lock": 14032, "nav_flags": 0, "P_std": 0, "L": {"i": 2298, "f": 8}, "pset_flags": 2, "P": 0, "misc_flags": 9, "preamble": 85, "payload": "i9rsEgIAAAAAAAAAAAAAAAAAAAD6CAAACLPQNg8AAABDPQAAFgACAAAAAAAAACgAGwEACwACCQ==", "recv_time": 8907446923, "acceleration": 27, "uptime": 2, "sender": 26427, "cn0": 179, "doppler_std": 22, "tow_flags": 0, "tot": {"wn": 0, "tow": 0}, "crc": 40921, "length": 55, "clock_drift": 0, "sid": {"code": 0, "reserved": 0, "sat": 15}, "sync_flags": 1, "corr_spacing": 40}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1279,14 +1291,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { let json_input = r#"{"track_flags": 11, "doppler": 15645, "clock_offset": 0, "msg_type": 17, "lock": 14032, "nav_flags": 0, "P_std": 0, "L": {"i": 2786, "f": 125}, "pset_flags": 3, "P": 0, "misc_flags": 9, "preamble": 85, "payload": "//uqMAIAAAAAAAAAAAAAAAAAAADiCgAAfbXQNg8AAAAdPQAACgACAAAAAAAAACgA3AEACwADCQ==", "recv_time": 9406446591, "acceleration": -36, "uptime": 2, "sender": 26427, "cn0": 181, "doppler_std": 10, "tow_flags": 0, "tot": {"wn": 0, "tow": 0}, "crc": 24386, "length": 55, "clock_drift": 0, "sid": {"code": 0, "reserved": 0, "sat": 15}, "sync_flags": 1, "corr_spacing": 40}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1439,14 +1451,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { let json_input = r#"{"track_flags": 11, "doppler": 15640, "clock_offset": 0, "msg_type": 17, "lock": 14032, "nav_flags": 0, "P_std": 0, "L": {"i": 3275, "f": 64}, "pset_flags": 3, "P": 0, "misc_flags": 9, "preamble": 85, "payload": "vV94TgIAAAAAAAAAAAAAAAAAAADLDAAAQLjQNg8AAAAYPQAABAADAAAAAAAAACgAAgEACwADCQ==", "recv_time": 9906446269, "acceleration": 2, "uptime": 3, "sender": 26427, "cn0": 184, "doppler_std": 4, "tow_flags": 0, "tot": {"wn": 0, "tow": 0}, "crc": 52930, "length": 55, "clock_drift": 0, "sid": {"code": 0, "reserved": 0, "sat": 15}, "sync_flags": 1, "corr_spacing": 40}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1596,3 +1608,886 @@ fn test_json2sbp_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_tracking_msg_tracking_state_detailed_dep`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_tracking_msg_tracking_state_detailed_dep() { + { + let mut payload = Cursor::new(vec![ + 85, 17, 0, 59, 103, 55, 163, 151, 112, 215, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 39, 5, 0, 0, 169, 177, 208, 54, 15, 0, 0, 0, 85, 61, 0, 0, 39, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 40, 0, 108, 1, 0, 11, 0, 0, 9, 166, 214, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDetailedDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDetailedDep(msg) => { + assert_eq!( + msg.message_type(), + 0x11, + "Incorrect message type, expected 0x11, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x673b, + "incorrect sender id, expected 0x673b, is {}", + sender_id + ); + assert_eq!( + msg.l.f, 169, + "incorrect value for l.f, expected 169, is {}", + msg.l.f + ); + assert_eq!( + msg.l.i, 1319, + "incorrect value for l.i, expected 1319, is {}", + msg.l.i + ); + assert_eq!(msg.p, 0, "incorrect value for p, expected 0, is {}", msg.p); + assert_eq!( + msg.p_std, 0, + "incorrect value for p_std, expected 0, is {}", + msg.p_std + ); + assert_eq!( + msg.acceleration, 108, + "incorrect value for acceleration, expected 108, is {}", + msg.acceleration + ); + assert_eq!( + msg.clock_drift, 0, + "incorrect value for clock_drift, expected 0, is {}", + msg.clock_drift + ); + assert_eq!( + msg.clock_offset, 0, + "incorrect value for clock_offset, expected 0, is {}", + msg.clock_offset + ); + assert_eq!( + msg.cn0, 177, + "incorrect value for cn0, expected 177, is {}", + msg.cn0 + ); + assert_eq!( + msg.corr_spacing, 40, + "incorrect value for corr_spacing, expected 40, is {}", + msg.corr_spacing + ); + assert_eq!( + msg.doppler, 15701, + "incorrect value for doppler, expected 15701, is {}", + msg.doppler + ); + assert_eq!( + msg.doppler_std, 39, + "incorrect value for doppler_std, expected 39, is {}", + msg.doppler_std + ); + assert_eq!( + msg.lock, 14032, + "incorrect value for lock, expected 14032, is {}", + msg.lock + ); + assert_eq!( + msg.misc_flags, 9, + "incorrect value for misc_flags, expected 9, is {}", + msg.misc_flags + ); + assert_eq!( + msg.nav_flags, 0, + "incorrect value for nav_flags, expected 0, is {}", + msg.nav_flags + ); + assert_eq!( + msg.pset_flags, 0, + "incorrect value for pset_flags, expected 0, is {}", + msg.pset_flags + ); + assert_eq!( + msg.recv_time, 7909447587, + "incorrect value for recv_time, expected 7909447587, is {}", + msg.recv_time + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 15, + "incorrect value for sid.sat, expected 15, is {}", + msg.sid.sat + ); + assert_eq!( + msg.sync_flags, 1, + "incorrect value for sync_flags, expected 1, is {}", + msg.sync_flags + ); + assert_eq!( + msg.tot.tow, 0, + "incorrect value for tot.tow, expected 0, is {}", + msg.tot.tow + ); + assert_eq!( + msg.tot.wn, 0, + "incorrect value for tot.wn, expected 0, is {}", + msg.tot.wn + ); + assert_eq!( + msg.tow_flags, 0, + "incorrect value for tow_flags, expected 0, is {}", + msg.tow_flags + ); + assert_eq!( + msg.track_flags, 11, + "incorrect value for track_flags, expected 11, is {}", + msg.track_flags + ); + assert_eq!( + msg.uptime, 1, + "incorrect value for uptime, expected 1, is {}", + msg.uptime + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDetailedDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 17, 0, 59, 103, 55, 97, 251, 61, 245, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 18, 7, 0, 0, 14, 175, 208, 54, 15, 0, 0, 0, 51, 61, 0, 0, 30, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 40, 0, 224, 1, 0, 11, 0, 0, 9, 136, 179, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDetailedDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDetailedDep(msg) => { + assert_eq!( + msg.message_type(), + 0x11, + "Incorrect message type, expected 0x11, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x673b, + "incorrect sender id, expected 0x673b, is {}", + sender_id + ); + assert_eq!( + msg.l.f, 14, + "incorrect value for l.f, expected 14, is {}", + msg.l.f + ); + assert_eq!( + msg.l.i, 1810, + "incorrect value for l.i, expected 1810, is {}", + msg.l.i + ); + assert_eq!(msg.p, 0, "incorrect value for p, expected 0, is {}", msg.p); + assert_eq!( + msg.p_std, 0, + "incorrect value for p_std, expected 0, is {}", + msg.p_std + ); + assert_eq!( + msg.acceleration, -32, + "incorrect value for acceleration, expected -32, is {}", + msg.acceleration + ); + assert_eq!( + msg.clock_drift, 0, + "incorrect value for clock_drift, expected 0, is {}", + msg.clock_drift + ); + assert_eq!( + msg.clock_offset, 0, + "incorrect value for clock_offset, expected 0, is {}", + msg.clock_offset + ); + assert_eq!( + msg.cn0, 175, + "incorrect value for cn0, expected 175, is {}", + msg.cn0 + ); + assert_eq!( + msg.corr_spacing, 40, + "incorrect value for corr_spacing, expected 40, is {}", + msg.corr_spacing + ); + assert_eq!( + msg.doppler, 15667, + "incorrect value for doppler, expected 15667, is {}", + msg.doppler + ); + assert_eq!( + msg.doppler_std, 30, + "incorrect value for doppler_std, expected 30, is {}", + msg.doppler_std + ); + assert_eq!( + msg.lock, 14032, + "incorrect value for lock, expected 14032, is {}", + msg.lock + ); + assert_eq!( + msg.misc_flags, 9, + "incorrect value for misc_flags, expected 9, is {}", + msg.misc_flags + ); + assert_eq!( + msg.nav_flags, 0, + "incorrect value for nav_flags, expected 0, is {}", + msg.nav_flags + ); + assert_eq!( + msg.pset_flags, 0, + "incorrect value for pset_flags, expected 0, is {}", + msg.pset_flags + ); + assert_eq!( + msg.recv_time, 8409447265, + "incorrect value for recv_time, expected 8409447265, is {}", + msg.recv_time + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 15, + "incorrect value for sid.sat, expected 15, is {}", + msg.sid.sat + ); + assert_eq!( + msg.sync_flags, 1, + "incorrect value for sync_flags, expected 1, is {}", + msg.sync_flags + ); + assert_eq!( + msg.tot.tow, 0, + "incorrect value for tot.tow, expected 0, is {}", + msg.tot.tow + ); + assert_eq!( + msg.tot.wn, 0, + "incorrect value for tot.wn, expected 0, is {}", + msg.tot.wn + ); + assert_eq!( + msg.tow_flags, 0, + "incorrect value for tow_flags, expected 0, is {}", + msg.tow_flags + ); + assert_eq!( + msg.track_flags, 11, + "incorrect value for track_flags, expected 11, is {}", + msg.track_flags + ); + assert_eq!( + msg.uptime, 1, + "incorrect value for uptime, expected 1, is {}", + msg.uptime + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDetailedDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 17, 0, 59, 103, 55, 139, 218, 236, 18, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 250, 8, 0, 0, 8, 179, 208, 54, 15, 0, 0, 0, 67, 61, 0, 0, 22, 0, 2, 0, 0, 0, 0, 0, + 0, 0, 40, 0, 27, 1, 0, 11, 0, 2, 9, 217, 159, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDetailedDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDetailedDep(msg) => { + assert_eq!( + msg.message_type(), + 0x11, + "Incorrect message type, expected 0x11, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x673b, + "incorrect sender id, expected 0x673b, is {}", + sender_id + ); + assert_eq!( + msg.l.f, 8, + "incorrect value for l.f, expected 8, is {}", + msg.l.f + ); + assert_eq!( + msg.l.i, 2298, + "incorrect value for l.i, expected 2298, is {}", + msg.l.i + ); + assert_eq!(msg.p, 0, "incorrect value for p, expected 0, is {}", msg.p); + assert_eq!( + msg.p_std, 0, + "incorrect value for p_std, expected 0, is {}", + msg.p_std + ); + assert_eq!( + msg.acceleration, 27, + "incorrect value for acceleration, expected 27, is {}", + msg.acceleration + ); + assert_eq!( + msg.clock_drift, 0, + "incorrect value for clock_drift, expected 0, is {}", + msg.clock_drift + ); + assert_eq!( + msg.clock_offset, 0, + "incorrect value for clock_offset, expected 0, is {}", + msg.clock_offset + ); + assert_eq!( + msg.cn0, 179, + "incorrect value for cn0, expected 179, is {}", + msg.cn0 + ); + assert_eq!( + msg.corr_spacing, 40, + "incorrect value for corr_spacing, expected 40, is {}", + msg.corr_spacing + ); + assert_eq!( + msg.doppler, 15683, + "incorrect value for doppler, expected 15683, is {}", + msg.doppler + ); + assert_eq!( + msg.doppler_std, 22, + "incorrect value for doppler_std, expected 22, is {}", + msg.doppler_std + ); + assert_eq!( + msg.lock, 14032, + "incorrect value for lock, expected 14032, is {}", + msg.lock + ); + assert_eq!( + msg.misc_flags, 9, + "incorrect value for misc_flags, expected 9, is {}", + msg.misc_flags + ); + assert_eq!( + msg.nav_flags, 0, + "incorrect value for nav_flags, expected 0, is {}", + msg.nav_flags + ); + assert_eq!( + msg.pset_flags, 2, + "incorrect value for pset_flags, expected 2, is {}", + msg.pset_flags + ); + assert_eq!( + msg.recv_time, 8907446923, + "incorrect value for recv_time, expected 8907446923, is {}", + msg.recv_time + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 15, + "incorrect value for sid.sat, expected 15, is {}", + msg.sid.sat + ); + assert_eq!( + msg.sync_flags, 1, + "incorrect value for sync_flags, expected 1, is {}", + msg.sync_flags + ); + assert_eq!( + msg.tot.tow, 0, + "incorrect value for tot.tow, expected 0, is {}", + msg.tot.tow + ); + assert_eq!( + msg.tot.wn, 0, + "incorrect value for tot.wn, expected 0, is {}", + msg.tot.wn + ); + assert_eq!( + msg.tow_flags, 0, + "incorrect value for tow_flags, expected 0, is {}", + msg.tow_flags + ); + assert_eq!( + msg.track_flags, 11, + "incorrect value for track_flags, expected 11, is {}", + msg.track_flags + ); + assert_eq!( + msg.uptime, 2, + "incorrect value for uptime, expected 2, is {}", + msg.uptime + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDetailedDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 17, 0, 59, 103, 55, 255, 251, 170, 48, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 226, 10, 0, 0, 125, 181, 208, 54, 15, 0, 0, 0, 29, 61, 0, 0, 10, 0, 2, 0, 0, 0, 0, + 0, 0, 0, 40, 0, 220, 1, 0, 11, 0, 3, 9, 66, 95, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDetailedDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDetailedDep(msg) => { + assert_eq!( + msg.message_type(), + 0x11, + "Incorrect message type, expected 0x11, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x673b, + "incorrect sender id, expected 0x673b, is {}", + sender_id + ); + assert_eq!( + msg.l.f, 125, + "incorrect value for l.f, expected 125, is {}", + msg.l.f + ); + assert_eq!( + msg.l.i, 2786, + "incorrect value for l.i, expected 2786, is {}", + msg.l.i + ); + assert_eq!(msg.p, 0, "incorrect value for p, expected 0, is {}", msg.p); + assert_eq!( + msg.p_std, 0, + "incorrect value for p_std, expected 0, is {}", + msg.p_std + ); + assert_eq!( + msg.acceleration, -36, + "incorrect value for acceleration, expected -36, is {}", + msg.acceleration + ); + assert_eq!( + msg.clock_drift, 0, + "incorrect value for clock_drift, expected 0, is {}", + msg.clock_drift + ); + assert_eq!( + msg.clock_offset, 0, + "incorrect value for clock_offset, expected 0, is {}", + msg.clock_offset + ); + assert_eq!( + msg.cn0, 181, + "incorrect value for cn0, expected 181, is {}", + msg.cn0 + ); + assert_eq!( + msg.corr_spacing, 40, + "incorrect value for corr_spacing, expected 40, is {}", + msg.corr_spacing + ); + assert_eq!( + msg.doppler, 15645, + "incorrect value for doppler, expected 15645, is {}", + msg.doppler + ); + assert_eq!( + msg.doppler_std, 10, + "incorrect value for doppler_std, expected 10, is {}", + msg.doppler_std + ); + assert_eq!( + msg.lock, 14032, + "incorrect value for lock, expected 14032, is {}", + msg.lock + ); + assert_eq!( + msg.misc_flags, 9, + "incorrect value for misc_flags, expected 9, is {}", + msg.misc_flags + ); + assert_eq!( + msg.nav_flags, 0, + "incorrect value for nav_flags, expected 0, is {}", + msg.nav_flags + ); + assert_eq!( + msg.pset_flags, 3, + "incorrect value for pset_flags, expected 3, is {}", + msg.pset_flags + ); + assert_eq!( + msg.recv_time, 9406446591, + "incorrect value for recv_time, expected 9406446591, is {}", + msg.recv_time + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 15, + "incorrect value for sid.sat, expected 15, is {}", + msg.sid.sat + ); + assert_eq!( + msg.sync_flags, 1, + "incorrect value for sync_flags, expected 1, is {}", + msg.sync_flags + ); + assert_eq!( + msg.tot.tow, 0, + "incorrect value for tot.tow, expected 0, is {}", + msg.tot.tow + ); + assert_eq!( + msg.tot.wn, 0, + "incorrect value for tot.wn, expected 0, is {}", + msg.tot.wn + ); + assert_eq!( + msg.tow_flags, 0, + "incorrect value for tow_flags, expected 0, is {}", + msg.tow_flags + ); + assert_eq!( + msg.track_flags, 11, + "incorrect value for track_flags, expected 11, is {}", + msg.track_flags + ); + assert_eq!( + msg.uptime, 2, + "incorrect value for uptime, expected 2, is {}", + msg.uptime + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDetailedDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 17, 0, 59, 103, 55, 189, 95, 120, 78, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 203, 12, 0, 0, 64, 184, 208, 54, 15, 0, 0, 0, 24, 61, 0, 0, 4, 0, 3, 0, 0, 0, 0, 0, + 0, 0, 40, 0, 2, 1, 0, 11, 0, 3, 9, 194, 206, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDetailedDep( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDetailedDep(msg) => { + assert_eq!( + msg.message_type(), + 0x11, + "Incorrect message type, expected 0x11, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x673b, + "incorrect sender id, expected 0x673b, is {}", + sender_id + ); + assert_eq!( + msg.l.f, 64, + "incorrect value for l.f, expected 64, is {}", + msg.l.f + ); + assert_eq!( + msg.l.i, 3275, + "incorrect value for l.i, expected 3275, is {}", + msg.l.i + ); + assert_eq!(msg.p, 0, "incorrect value for p, expected 0, is {}", msg.p); + assert_eq!( + msg.p_std, 0, + "incorrect value for p_std, expected 0, is {}", + msg.p_std + ); + assert_eq!( + msg.acceleration, 2, + "incorrect value for acceleration, expected 2, is {}", + msg.acceleration + ); + assert_eq!( + msg.clock_drift, 0, + "incorrect value for clock_drift, expected 0, is {}", + msg.clock_drift + ); + assert_eq!( + msg.clock_offset, 0, + "incorrect value for clock_offset, expected 0, is {}", + msg.clock_offset + ); + assert_eq!( + msg.cn0, 184, + "incorrect value for cn0, expected 184, is {}", + msg.cn0 + ); + assert_eq!( + msg.corr_spacing, 40, + "incorrect value for corr_spacing, expected 40, is {}", + msg.corr_spacing + ); + assert_eq!( + msg.doppler, 15640, + "incorrect value for doppler, expected 15640, is {}", + msg.doppler + ); + assert_eq!( + msg.doppler_std, 4, + "incorrect value for doppler_std, expected 4, is {}", + msg.doppler_std + ); + assert_eq!( + msg.lock, 14032, + "incorrect value for lock, expected 14032, is {}", + msg.lock + ); + assert_eq!( + msg.misc_flags, 9, + "incorrect value for misc_flags, expected 9, is {}", + msg.misc_flags + ); + assert_eq!( + msg.nav_flags, 0, + "incorrect value for nav_flags, expected 0, is {}", + msg.nav_flags + ); + assert_eq!( + msg.pset_flags, 3, + "incorrect value for pset_flags, expected 3, is {}", + msg.pset_flags + ); + assert_eq!( + msg.recv_time, 9906446269, + "incorrect value for recv_time, expected 9906446269, is {}", + msg.recv_time + ); + assert_eq!( + msg.sid.code, 0, + "incorrect value for sid.code, expected 0, is {}", + msg.sid.code + ); + assert_eq!( + msg.sid.reserved, 0, + "incorrect value for sid.reserved, expected 0, is {}", + msg.sid.reserved + ); + assert_eq!( + msg.sid.sat, 15, + "incorrect value for sid.sat, expected 15, is {}", + msg.sid.sat + ); + assert_eq!( + msg.sync_flags, 1, + "incorrect value for sync_flags, expected 1, is {}", + msg.sync_flags + ); + assert_eq!( + msg.tot.tow, 0, + "incorrect value for tot.tow, expected 0, is {}", + msg.tot.tow + ); + assert_eq!( + msg.tot.wn, 0, + "incorrect value for tot.wn, expected 0, is {}", + msg.tot.wn + ); + assert_eq!( + msg.tow_flags, 0, + "incorrect value for tow_flags, expected 0, is {}", + msg.tow_flags + ); + assert_eq!( + msg.track_flags, 11, + "incorrect value for track_flags, expected 11, is {}", + msg.track_flags + ); + assert_eq!( + msg.uptime, 3, + "incorrect value for uptime, expected 3, is {}", + msg.uptime + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDetailedDep"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_tracking_msgtracking_state_dep_a.rs b/rust/sbp/tests/integration/auto_check_sbp_tracking_msgtracking_state_dep_a.rs index 3efe53e195..f5a42c0030 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_tracking_msgtracking_state_dep_a.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_tracking_msgtracking_state_dep_a.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_tracking_msgtracking_state_dep_a() { { @@ -1205,6 +1210,13 @@ fn test_auto_check_sbp_tracking_msgtracking_state_dep_a() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { @@ -1212,14 +1224,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 22, "states": [{"state": 1, "prn": 0, "cn0": 11.230907440185547}, {"state": 1, "prn": 2, "cn0": 10.438665390014648}, {"state": 1, "prn": 3, "cn0": 9.732142448425293}, {"state": 1, "prn": 7, "cn0": 14.341922760009766}, {"state": 1, "prn": 10, "cn0": 7.8549017906188965}, {"state": 1, "prn": 13, "cn0": 5.0982866287231445}, {"state": 1, "prn": 22, "cn0": 6.741272926330566}, {"state": 1, "prn": 30, "cn0": 12.700549125671387}, {"state": 1, "prn": 31, "cn0": 15.893081665039062}, {"state": 1, "prn": 25, "cn0": 4.242738723754883}, {"state": 1, "prn": 6, "cn0": 6.97599983215332}], "crc": 57617, "length": 66, "preamble": 85, "payload": "AQDMsTNBAQLGBCdBAQPbthtBAQeEeGVBAQpbW/tAAQ0qJaNAARaCuNdAAR5zNUtBAR8QSn5BARmExIdAAQZkO99A"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1416,14 +1428,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 22, "states": [{"state": 1, "prn": 0, "cn0": 11.014122009277344}, {"state": 1, "prn": 2, "cn0": 10.885148048400879}, {"state": 1, "prn": 3, "cn0": 10.131351470947266}, {"state": 1, "prn": 7, "cn0": 14.829026222229004}, {"state": 1, "prn": 10, "cn0": 7.79104471206665}, {"state": 1, "prn": 13, "cn0": 4.868161201477051}, {"state": 1, "prn": 22, "cn0": 6.721095561981201}, {"state": 1, "prn": 30, "cn0": 12.971323013305664}, {"state": 1, "prn": 31, "cn0": 15.481405258178711}, {"state": 1, "prn": 25, "cn0": 3.8834354877471924}, {"state": 1, "prn": 6, "cn0": 4.061488628387451}], "crc": 44456, "length": 66, "preamble": 85, "payload": "AQDYOTBBAQKRKS5BAQMEGiJBAQexQ21BAQo9UPlAAQ36x5tAARY3E9dAAR6Kik9BAR/Ws3dBARk1inhAAQa394FA"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1620,14 +1632,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 22, "states": [{"state": 1, "prn": 0, "cn0": 11.768689155578613}, {"state": 1, "prn": 2, "cn0": 10.909001350402832}, {"state": 1, "prn": 3, "cn0": 9.881731033325195}, {"state": 1, "prn": 7, "cn0": 14.076395988464355}, {"state": 1, "prn": 10, "cn0": 7.619818210601807}, {"state": 1, "prn": 13, "cn0": 5.208371162414551}, {"state": 1, "prn": 22, "cn0": 6.2935872077941895}, {"state": 1, "prn": 30, "cn0": 13.232341766357422}, {"state": 1, "prn": 31, "cn0": 15.547346115112305}, {"state": 1, "prn": 25, "cn0": 4.130964279174805}, {"state": 1, "prn": 6, "cn0": 2.856823205947876}], "crc": 45934, "length": 66, "preamble": 85, "payload": "AQCNTDxBAQJFiy5BAQOSGx5BAQfrOGFBAQqN1fNAAQ36qqZAARYRZclAAR6st1NBAR/uwXhBARncMIRAAQYx1jZA"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -1824,14 +1836,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 22, "states": [{"state": 1, "prn": 0, "cn0": 62.13985824584961}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}], "crc": 23032, "length": 66, "preamble": 85, "payload": "AQA3j3hCAAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2024,14 +2036,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 22, "states": [{"state": 1, "prn": 0, "cn0": 36.764503479003906}, {"state": 1, "prn": 2, "cn0": 9.313432693481445}, {"state": 1, "prn": 3, "cn0": 16.854938507080078}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}], "crc": 25940, "length": 66, "preamble": 85, "payload": "AQDaDhNCAQLSAxVBAQPq1oZBAAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2224,14 +2236,14 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { let json_input = r#"{"sender": 1219, "msg_type": 22, "states": [{"state": 1, "prn": 0, "cn0": 27.394229888916016}, {"state": 1, "prn": 2, "cn0": 2.875}, {"state": 1, "prn": 3, "cn0": 8.467644691467285}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}, {"state": 0, "prn": 0, "cn0": -1.0}], "crc": 31525, "length": 66, "preamble": 85, "payload": "AQBiJ9tBAQIAADhAAQN5ewdBAAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/AAAAAIC/"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -2421,3 +2433,1318 @@ fn test_json2sbp_auto_check_sbp_tracking_msgtracking_state_dep_a() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_tracking_msgtracking_state_dep_a`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_tracking_msgtracking_state_dep_a() { + { + let mut payload = Cursor::new(vec![ + 85, 22, 0, 195, 4, 66, 1, 0, 204, 177, 51, 65, 1, 2, 198, 4, 39, 65, 1, 3, 219, 182, + 27, 65, 1, 7, 132, 120, 101, 65, 1, 10, 91, 91, 251, 64, 1, 13, 42, 37, 163, 64, 1, 22, + 130, 184, 215, 64, 1, 30, 115, 53, 75, 65, 1, 31, 16, 74, 126, 65, 1, 25, 132, 196, + 135, 64, 1, 6, 100, 59, 223, 64, 17, 225, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x16, + "Incorrect message type, expected 0x16, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(1.12309074401855469e+01), + "incorrect value for states[0].cn0, expected 1.12309074401855469e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].prn, 0, + "incorrect value for states[0].prn, expected 0, is {}", + msg.states[0].prn + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(1.04386653900146484e+01), + "incorrect value for states[1].cn0, expected 1.04386653900146484e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].prn, 2, + "incorrect value for states[1].prn, expected 2, is {}", + msg.states[1].prn + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(9.73214244842529297e+00), + "incorrect value for states[2].cn0, expected 9.73214244842529297e+00, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].prn, 3, + "incorrect value for states[2].prn, expected 3, is {}", + msg.states[2].prn + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(1.43419227600097656e+01), + "incorrect value for states[3].cn0, expected 1.43419227600097656e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].prn, 7, + "incorrect value for states[3].prn, expected 7, is {}", + msg.states[3].prn + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(7.85490179061889648e+00), + "incorrect value for states[4].cn0, expected 7.85490179061889648e+00, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].prn, 10, + "incorrect value for states[4].prn, expected 10, is {}", + msg.states[4].prn + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(5.09828662872314453e+00), + "incorrect value for states[5].cn0, expected 5.09828662872314453e+00, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].prn, 13, + "incorrect value for states[5].prn, expected 13, is {}", + msg.states[5].prn + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(6.74127292633056641e+00), + "incorrect value for states[6].cn0, expected 6.74127292633056641e+00, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].prn, 22, + "incorrect value for states[6].prn, expected 22, is {}", + msg.states[6].prn + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(1.27005491256713867e+01), + "incorrect value for states[7].cn0, expected 1.27005491256713867e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].prn, 30, + "incorrect value for states[7].prn, expected 30, is {}", + msg.states[7].prn + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(1.58930816650390625e+01), + "incorrect value for states[8].cn0, expected 1.58930816650390625e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].prn, 31, + "incorrect value for states[8].prn, expected 31, is {}", + msg.states[8].prn + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(4.24273872375488281e+00), + "incorrect value for states[9].cn0, expected 4.24273872375488281e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].prn, 25, + "incorrect value for states[9].prn, expected 25, is {}", + msg.states[9].prn + ); + assert_eq!( + msg.states[9].state, 1, + "incorrect value for states[9].state, expected 1, is {}", + msg.states[9].state + ); + assert!( + msg.states[10].cn0.almost_eq(6.97599983215332031e+00), + "incorrect value for states[10].cn0, expected 6.97599983215332031e+00, is {:e}", + msg.states[10].cn0 + ); + assert_eq!( + msg.states[10].prn, 6, + "incorrect value for states[10].prn, expected 6, is {}", + msg.states[10].prn + ); + assert_eq!( + msg.states[10].state, 1, + "incorrect value for states[10].state, expected 1, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 22, 0, 195, 4, 66, 1, 0, 216, 57, 48, 65, 1, 2, 145, 41, 46, 65, 1, 3, 4, 26, 34, + 65, 1, 7, 177, 67, 109, 65, 1, 10, 61, 80, 249, 64, 1, 13, 250, 199, 155, 64, 1, 22, + 55, 19, 215, 64, 1, 30, 138, 138, 79, 65, 1, 31, 214, 179, 119, 65, 1, 25, 53, 138, + 120, 64, 1, 6, 183, 247, 129, 64, 168, 173, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x16, + "Incorrect message type, expected 0x16, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(1.10141220092773438e+01), + "incorrect value for states[0].cn0, expected 1.10141220092773438e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].prn, 0, + "incorrect value for states[0].prn, expected 0, is {}", + msg.states[0].prn + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(1.08851480484008789e+01), + "incorrect value for states[1].cn0, expected 1.08851480484008789e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].prn, 2, + "incorrect value for states[1].prn, expected 2, is {}", + msg.states[1].prn + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(1.01313514709472656e+01), + "incorrect value for states[2].cn0, expected 1.01313514709472656e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].prn, 3, + "incorrect value for states[2].prn, expected 3, is {}", + msg.states[2].prn + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(1.48290262222290039e+01), + "incorrect value for states[3].cn0, expected 1.48290262222290039e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].prn, 7, + "incorrect value for states[3].prn, expected 7, is {}", + msg.states[3].prn + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(7.79104471206665039e+00), + "incorrect value for states[4].cn0, expected 7.79104471206665039e+00, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].prn, 10, + "incorrect value for states[4].prn, expected 10, is {}", + msg.states[4].prn + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(4.86816120147705078e+00), + "incorrect value for states[5].cn0, expected 4.86816120147705078e+00, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].prn, 13, + "incorrect value for states[5].prn, expected 13, is {}", + msg.states[5].prn + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(6.72109556198120117e+00), + "incorrect value for states[6].cn0, expected 6.72109556198120117e+00, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].prn, 22, + "incorrect value for states[6].prn, expected 22, is {}", + msg.states[6].prn + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(1.29713230133056641e+01), + "incorrect value for states[7].cn0, expected 1.29713230133056641e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].prn, 30, + "incorrect value for states[7].prn, expected 30, is {}", + msg.states[7].prn + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(1.54814052581787109e+01), + "incorrect value for states[8].cn0, expected 1.54814052581787109e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].prn, 31, + "incorrect value for states[8].prn, expected 31, is {}", + msg.states[8].prn + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(3.88343548774719238e+00), + "incorrect value for states[9].cn0, expected 3.88343548774719238e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].prn, 25, + "incorrect value for states[9].prn, expected 25, is {}", + msg.states[9].prn + ); + assert_eq!( + msg.states[9].state, 1, + "incorrect value for states[9].state, expected 1, is {}", + msg.states[9].state + ); + assert!( + msg.states[10].cn0.almost_eq(4.06148862838745117e+00), + "incorrect value for states[10].cn0, expected 4.06148862838745117e+00, is {:e}", + msg.states[10].cn0 + ); + assert_eq!( + msg.states[10].prn, 6, + "incorrect value for states[10].prn, expected 6, is {}", + msg.states[10].prn + ); + assert_eq!( + msg.states[10].state, 1, + "incorrect value for states[10].state, expected 1, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 22, 0, 195, 4, 66, 1, 0, 141, 76, 60, 65, 1, 2, 69, 139, 46, 65, 1, 3, 146, 27, 30, + 65, 1, 7, 235, 56, 97, 65, 1, 10, 141, 213, 243, 64, 1, 13, 250, 170, 166, 64, 1, 22, + 17, 101, 201, 64, 1, 30, 172, 183, 83, 65, 1, 31, 238, 193, 120, 65, 1, 25, 220, 48, + 132, 64, 1, 6, 49, 214, 54, 64, 110, 179, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x16, + "Incorrect message type, expected 0x16, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(1.17686891555786133e+01), + "incorrect value for states[0].cn0, expected 1.17686891555786133e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].prn, 0, + "incorrect value for states[0].prn, expected 0, is {}", + msg.states[0].prn + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(1.09090013504028320e+01), + "incorrect value for states[1].cn0, expected 1.09090013504028320e+01, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].prn, 2, + "incorrect value for states[1].prn, expected 2, is {}", + msg.states[1].prn + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(9.88173103332519531e+00), + "incorrect value for states[2].cn0, expected 9.88173103332519531e+00, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].prn, 3, + "incorrect value for states[2].prn, expected 3, is {}", + msg.states[2].prn + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(1.40763959884643555e+01), + "incorrect value for states[3].cn0, expected 1.40763959884643555e+01, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].prn, 7, + "incorrect value for states[3].prn, expected 7, is {}", + msg.states[3].prn + ); + assert_eq!( + msg.states[3].state, 1, + "incorrect value for states[3].state, expected 1, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(7.61981821060180664e+00), + "incorrect value for states[4].cn0, expected 7.61981821060180664e+00, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].prn, 10, + "incorrect value for states[4].prn, expected 10, is {}", + msg.states[4].prn + ); + assert_eq!( + msg.states[4].state, 1, + "incorrect value for states[4].state, expected 1, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(5.20837116241455078e+00), + "incorrect value for states[5].cn0, expected 5.20837116241455078e+00, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].prn, 13, + "incorrect value for states[5].prn, expected 13, is {}", + msg.states[5].prn + ); + assert_eq!( + msg.states[5].state, 1, + "incorrect value for states[5].state, expected 1, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(6.29358720779418945e+00), + "incorrect value for states[6].cn0, expected 6.29358720779418945e+00, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].prn, 22, + "incorrect value for states[6].prn, expected 22, is {}", + msg.states[6].prn + ); + assert_eq!( + msg.states[6].state, 1, + "incorrect value for states[6].state, expected 1, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(1.32323417663574219e+01), + "incorrect value for states[7].cn0, expected 1.32323417663574219e+01, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].prn, 30, + "incorrect value for states[7].prn, expected 30, is {}", + msg.states[7].prn + ); + assert_eq!( + msg.states[7].state, 1, + "incorrect value for states[7].state, expected 1, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(1.55473461151123047e+01), + "incorrect value for states[8].cn0, expected 1.55473461151123047e+01, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].prn, 31, + "incorrect value for states[8].prn, expected 31, is {}", + msg.states[8].prn + ); + assert_eq!( + msg.states[8].state, 1, + "incorrect value for states[8].state, expected 1, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(4.13096427917480469e+00), + "incorrect value for states[9].cn0, expected 4.13096427917480469e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].prn, 25, + "incorrect value for states[9].prn, expected 25, is {}", + msg.states[9].prn + ); + assert_eq!( + msg.states[9].state, 1, + "incorrect value for states[9].state, expected 1, is {}", + msg.states[9].state + ); + assert!( + msg.states[10].cn0.almost_eq(2.85682320594787598e+00), + "incorrect value for states[10].cn0, expected 2.85682320594787598e+00, is {:e}", + msg.states[10].cn0 + ); + assert_eq!( + msg.states[10].prn, 6, + "incorrect value for states[10].prn, expected 6, is {}", + msg.states[10].prn + ); + assert_eq!( + msg.states[10].state, 1, + "incorrect value for states[10].state, expected 1, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 22, 0, 195, 4, 66, 1, 0, 55, 143, 120, 66, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, + 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, + 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, + 191, 248, 89, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x16, + "Incorrect message type, expected 0x16, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(6.21398582458496094e+01), + "incorrect value for states[0].cn0, expected 6.21398582458496094e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].prn, 0, + "incorrect value for states[0].prn, expected 0, is {}", + msg.states[0].prn + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[1].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].prn, 0, + "incorrect value for states[1].prn, expected 0, is {}", + msg.states[1].prn + ); + assert_eq!( + msg.states[1].state, 0, + "incorrect value for states[1].state, expected 0, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[2].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].prn, 0, + "incorrect value for states[2].prn, expected 0, is {}", + msg.states[2].prn + ); + assert_eq!( + msg.states[2].state, 0, + "incorrect value for states[2].state, expected 0, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[3].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].prn, 0, + "incorrect value for states[3].prn, expected 0, is {}", + msg.states[3].prn + ); + assert_eq!( + msg.states[3].state, 0, + "incorrect value for states[3].state, expected 0, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[4].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].prn, 0, + "incorrect value for states[4].prn, expected 0, is {}", + msg.states[4].prn + ); + assert_eq!( + msg.states[4].state, 0, + "incorrect value for states[4].state, expected 0, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[5].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].prn, 0, + "incorrect value for states[5].prn, expected 0, is {}", + msg.states[5].prn + ); + assert_eq!( + msg.states[5].state, 0, + "incorrect value for states[5].state, expected 0, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[6].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].prn, 0, + "incorrect value for states[6].prn, expected 0, is {}", + msg.states[6].prn + ); + assert_eq!( + msg.states[6].state, 0, + "incorrect value for states[6].state, expected 0, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[7].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].prn, 0, + "incorrect value for states[7].prn, expected 0, is {}", + msg.states[7].prn + ); + assert_eq!( + msg.states[7].state, 0, + "incorrect value for states[7].state, expected 0, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[8].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].prn, 0, + "incorrect value for states[8].prn, expected 0, is {}", + msg.states[8].prn + ); + assert_eq!( + msg.states[8].state, 0, + "incorrect value for states[8].state, expected 0, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].prn, 0, + "incorrect value for states[9].prn, expected 0, is {}", + msg.states[9].prn + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].prn, 0, + "incorrect value for states[10].prn, expected 0, is {}", + msg.states[10].prn + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 22, 0, 195, 4, 66, 1, 0, 218, 14, 19, 66, 1, 2, 210, 3, 21, 65, 1, 3, 234, 214, + 134, 65, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, + 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, + 128, 191, 84, 101, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x16, + "Incorrect message type, expected 0x16, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(3.67645034790039062e+01), + "incorrect value for states[0].cn0, expected 3.67645034790039062e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].prn, 0, + "incorrect value for states[0].prn, expected 0, is {}", + msg.states[0].prn + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(9.31343269348144531e+00), + "incorrect value for states[1].cn0, expected 9.31343269348144531e+00, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].prn, 2, + "incorrect value for states[1].prn, expected 2, is {}", + msg.states[1].prn + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(1.68549385070800781e+01), + "incorrect value for states[2].cn0, expected 1.68549385070800781e+01, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].prn, 3, + "incorrect value for states[2].prn, expected 3, is {}", + msg.states[2].prn + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[3].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].prn, 0, + "incorrect value for states[3].prn, expected 0, is {}", + msg.states[3].prn + ); + assert_eq!( + msg.states[3].state, 0, + "incorrect value for states[3].state, expected 0, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[4].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].prn, 0, + "incorrect value for states[4].prn, expected 0, is {}", + msg.states[4].prn + ); + assert_eq!( + msg.states[4].state, 0, + "incorrect value for states[4].state, expected 0, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[5].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].prn, 0, + "incorrect value for states[5].prn, expected 0, is {}", + msg.states[5].prn + ); + assert_eq!( + msg.states[5].state, 0, + "incorrect value for states[5].state, expected 0, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[6].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].prn, 0, + "incorrect value for states[6].prn, expected 0, is {}", + msg.states[6].prn + ); + assert_eq!( + msg.states[6].state, 0, + "incorrect value for states[6].state, expected 0, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[7].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].prn, 0, + "incorrect value for states[7].prn, expected 0, is {}", + msg.states[7].prn + ); + assert_eq!( + msg.states[7].state, 0, + "incorrect value for states[7].state, expected 0, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[8].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].prn, 0, + "incorrect value for states[8].prn, expected 0, is {}", + msg.states[8].prn + ); + assert_eq!( + msg.states[8].state, 0, + "incorrect value for states[8].state, expected 0, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].prn, 0, + "incorrect value for states[9].prn, expected 0, is {}", + msg.states[9].prn + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].prn, 0, + "incorrect value for states[10].prn, expected 0, is {}", + msg.states[10].prn + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } + { + let mut payload = Cursor::new(vec![ + 85, 22, 0, 195, 4, 66, 1, 0, 98, 39, 219, 65, 1, 2, 0, 0, 56, 64, 1, 3, 121, 123, 7, + 65, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, + 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 128, + 191, 37, 123, + ]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgTrackingStateDepA( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgTrackingStateDepA(msg) => { + assert_eq!( + msg.message_type(), + 0x16, + "Incorrect message type, expected 0x16, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x4c3, + "incorrect sender id, expected 0x4c3, is {}", + sender_id + ); + assert!( + msg.states[0].cn0.almost_eq(2.73942298889160156e+01), + "incorrect value for states[0].cn0, expected 2.73942298889160156e+01, is {:e}", + msg.states[0].cn0 + ); + assert_eq!( + msg.states[0].prn, 0, + "incorrect value for states[0].prn, expected 0, is {}", + msg.states[0].prn + ); + assert_eq!( + msg.states[0].state, 1, + "incorrect value for states[0].state, expected 1, is {}", + msg.states[0].state + ); + assert!( + msg.states[1].cn0.almost_eq(2.87500000000000000e+00), + "incorrect value for states[1].cn0, expected 2.87500000000000000e+00, is {:e}", + msg.states[1].cn0 + ); + assert_eq!( + msg.states[1].prn, 2, + "incorrect value for states[1].prn, expected 2, is {}", + msg.states[1].prn + ); + assert_eq!( + msg.states[1].state, 1, + "incorrect value for states[1].state, expected 1, is {}", + msg.states[1].state + ); + assert!( + msg.states[2].cn0.almost_eq(8.46764469146728516e+00), + "incorrect value for states[2].cn0, expected 8.46764469146728516e+00, is {:e}", + msg.states[2].cn0 + ); + assert_eq!( + msg.states[2].prn, 3, + "incorrect value for states[2].prn, expected 3, is {}", + msg.states[2].prn + ); + assert_eq!( + msg.states[2].state, 1, + "incorrect value for states[2].state, expected 1, is {}", + msg.states[2].state + ); + assert!( + msg.states[3].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[3].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[3].cn0 + ); + assert_eq!( + msg.states[3].prn, 0, + "incorrect value for states[3].prn, expected 0, is {}", + msg.states[3].prn + ); + assert_eq!( + msg.states[3].state, 0, + "incorrect value for states[3].state, expected 0, is {}", + msg.states[3].state + ); + assert!( + msg.states[4].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[4].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[4].cn0 + ); + assert_eq!( + msg.states[4].prn, 0, + "incorrect value for states[4].prn, expected 0, is {}", + msg.states[4].prn + ); + assert_eq!( + msg.states[4].state, 0, + "incorrect value for states[4].state, expected 0, is {}", + msg.states[4].state + ); + assert!( + msg.states[5].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[5].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[5].cn0 + ); + assert_eq!( + msg.states[5].prn, 0, + "incorrect value for states[5].prn, expected 0, is {}", + msg.states[5].prn + ); + assert_eq!( + msg.states[5].state, 0, + "incorrect value for states[5].state, expected 0, is {}", + msg.states[5].state + ); + assert!( + msg.states[6].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[6].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[6].cn0 + ); + assert_eq!( + msg.states[6].prn, 0, + "incorrect value for states[6].prn, expected 0, is {}", + msg.states[6].prn + ); + assert_eq!( + msg.states[6].state, 0, + "incorrect value for states[6].state, expected 0, is {}", + msg.states[6].state + ); + assert!( + msg.states[7].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[7].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[7].cn0 + ); + assert_eq!( + msg.states[7].prn, 0, + "incorrect value for states[7].prn, expected 0, is {}", + msg.states[7].prn + ); + assert_eq!( + msg.states[7].state, 0, + "incorrect value for states[7].state, expected 0, is {}", + msg.states[7].state + ); + assert!( + msg.states[8].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[8].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[8].cn0 + ); + assert_eq!( + msg.states[8].prn, 0, + "incorrect value for states[8].prn, expected 0, is {}", + msg.states[8].prn + ); + assert_eq!( + msg.states[8].state, 0, + "incorrect value for states[8].state, expected 0, is {}", + msg.states[8].state + ); + assert!( + msg.states[9].cn0.almost_eq(-1.00000000000000000e+00), + "incorrect value for states[9].cn0, expected -1.00000000000000000e+00, is {:e}", + msg.states[9].cn0 + ); + assert_eq!( + msg.states[9].prn, 0, + "incorrect value for states[9].prn, expected 0, is {}", + msg.states[9].prn + ); + assert_eq!( + msg.states[9].state, 0, + "incorrect value for states[9].state, expected 0, is {}", + msg.states[9].state + ); + assert!(msg.states[10].cn0.almost_eq( -1.00000000000000000e+00 ), "incorrect value for states[10].cn0, expected -1.00000000000000000e+00, is {:e}", msg.states[10].cn0); + assert_eq!( + msg.states[10].prn, 0, + "incorrect value for states[10].prn, expected 0, is {}", + msg.states[10].prn + ); + assert_eq!( + msg.states[10].state, 0, + "incorrect value for states[10].state, expected 0, is {}", + msg.states[10].state + ); + } + _ => panic!("Invalid message type! Expected a MsgTrackingStateDepA"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/rust/sbp/tests/integration/auto_check_sbp_vehicle_msg_odometry.rs b/rust/sbp/tests/integration/auto_check_sbp_vehicle_msg_odometry.rs index c387a2d8f3..f468197bf3 100644 --- a/rust/sbp/tests/integration/auto_check_sbp_vehicle_msg_odometry.rs +++ b/rust/sbp/tests/integration/auto_check_sbp_vehicle_msg_odometry.rs @@ -13,6 +13,11 @@ use crate::*; +/// Tests [`sbp::iter_messages`], from payload into SBP messages +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] fn test_auto_check_sbp_vehicle_msg_odometry() { { @@ -62,6 +67,13 @@ fn test_auto_check_sbp_vehicle_msg_odometry() { } } +/// Tests [`sbp::json::iter_messages`] for JSON payload -> SBP message +/// and [`sbp::json::iter_messages_from_fields`] for JSON fields -> SBP message. +/// +/// Asserts: +/// - SBP message constructed via payload is identical to from fields +/// - SBP fields equates to that of the field +/// - Payload is identical #[test] #[cfg(feature = "json")] fn test_json2sbp_auto_check_sbp_vehicle_msg_odometry() { @@ -69,14 +81,14 @@ fn test_json2sbp_auto_check_sbp_vehicle_msg_odometry() { let json_input = r#"{"sender": 66, "msg_type": 2307, "tow": 8, "crc": 25396, "length": 9, "flags": 1, "velocity": 7, "preamble": 85, "payload": "CAAAAAcAAAAB"}"#.as_bytes(); let sbp_msg = { - // Json to Sbp message from payload + // JSON to SBP message from payload let mut iter = json2sbp_iter_msg(json_input); let from_payload = iter .next() .expect("no message found") .expect("failed to parse message"); - // Json to Sbp message from payload + // JSON to SBP message from fields let mut iter = iter_messages_from_fields(json_input); let from_fields = iter .next() @@ -120,3 +132,80 @@ fn test_json2sbp_auto_check_sbp_vehicle_msg_odometry() { }; } } + +/// Tests [`sbp::json::JsonEncoder`] for roundtrip SBP message -> JSON +/// +/// Assumes: +/// - [`self::test_auto_check_sbp_vehicle_msg_odometry`] passes +/// +/// Asserts: +/// - SBP fields equates to that of the field +/// - Payload is identical +#[test] +#[cfg(feature = "json")] +fn test_sbp2json_auto_check_sbp_vehicle_msg_odometry() { + { + let mut payload = Cursor::new(vec![85, 3, 9, 66, 0, 9, 8, 0, 0, 0, 7, 0, 0, 0, 1, 52, 99]); + + // Construct sbp message + let sbp_msg = { + let mut msgs = iter_messages(&mut payload); + msgs.next() + .expect("no message found") + .expect("failed to parse message") + }; + + let mut json_buffer = vec![]; + // Populate json buffer, CompactFormatter + sbp::json::JsonEncoder::new(&mut json_buffer, sbp::json::CompactFormatter {}) + .send(&sbp_msg) + .unwrap(); + + // Reconstruct Sbp message from json fields, roundtrip + let sbp_msg = sbp::messages::Sbp::MsgOdometry( + serde_json::from_str( + std::str::from_utf8(json_buffer.as_slice()) + .unwrap() + .to_string() + .as_str(), + ) + .unwrap(), + ); + match &sbp_msg { + sbp::messages::Sbp::MsgOdometry(msg) => { + assert_eq!( + msg.message_type(), + 0x903, + "Incorrect message type, expected 0x903, is {}", + msg.message_type() + ); + let sender_id = msg.sender_id().unwrap(); + assert_eq!( + sender_id, 0x42, + "incorrect sender id, expected 0x42, is {}", + sender_id + ); + assert_eq!( + msg.flags, 1, + "incorrect value for flags, expected 1, is {}", + msg.flags + ); + assert_eq!( + msg.tow, 8, + "incorrect value for tow, expected 8, is {}", + msg.tow + ); + assert_eq!( + msg.velocity, 7, + "incorrect value for velocity, expected 7, is {}", + msg.velocity + ); + } + _ => panic!("Invalid message type! Expected a MsgOdometry"), + }; + + // Check payload is still identical + let frame = sbp::to_vec(&sbp_msg).unwrap(); + assert_eq!(frame, payload.into_inner()); + } +} diff --git a/test_data/benchmark_main.py b/test_data/benchmark_main.py index 1452534b30..e85451a580 100755 --- a/test_data/benchmark_main.py +++ b/test_data/benchmark_main.py @@ -11,8 +11,8 @@ # How much faster Rust should be than other implementations RATIOS_SBP2JSON = { - "haskell": 2.17, - "python": 21.93, + "haskell": 2.12, + "python": 19.46, } RATIOS_JSON2SBP = { @@ -20,7 +20,7 @@ } RATIOS_JSON2JSON = { - "haskell": 1.71, + "haskell": 2.40, } FAILED = [False]