From 5bd273d78e1b226dc87a981ab6c2d99f3a39df14 Mon Sep 17 00:00:00 2001 From: Michael du Plessis Date: Mon, 8 Jan 2024 15:38:02 +0200 Subject: [PATCH 1/3] fixed bug in repo --- src/parser.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index a668a04..89b5708 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,7 +5,7 @@ use nom::{ combinator::{cond, verify}, error::ParseError, multi::{length_count, length_data}, - number::streaming::{be_u16, be_u32, be_u64, be_u8}, + number::streaming::{be_i32, be_u16, be_u32, be_u64, be_u8}, IResult, Parser, }; @@ -138,8 +138,8 @@ fn record<'a>(codec: Codec) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], AVLReco let (input, timestamp) = be_u64(input)?; let (input, priority) = priority(input)?; - let (input, longitude) = be_u32(input)?; - let (input, latitude) = be_u32(input)?; + let (input, longitude) = be_i32(input)?; + let (input, latitude) = be_i32(input)?; let (input, altitude) = be_u16(input)?; let (input, angle) = be_u16(input)?; let (input, satellites) = be_u8(input)?; @@ -156,18 +156,20 @@ fn record<'a>(codec: Codec) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], AVLReco // contruct a datetime using the timestamp in since the unix epoch let timestamp = Utc.timestamp_millis_opt(timestamp as i64).single().unwrap(); - let longitude = if longitude & 0x80000000 != 0 { - -(longitude as i32) - } else { - longitude as i32 - } as f64 - / 10000000.0; - let latitude = if latitude & 0x80000000 != 0 { - -(latitude as i32) - } else { - latitude as i32 - } as f64 - / 10000000.0; + // let longitude = if longitude & 0x80000000 != 0 { + // -(longitude as i32) + // } else { + // longitude as i32 + // } as f64 + // / 10000000.0; + // let latitude = if latitude & 0x80000000 != 0 { + // -(latitude as i32) + // } else { + // latitude as i32 + // } as f64 + // / 10000000.0; + let longitude = longitude as f64 / 10000000.0; + let latitude = latitude as f64 / 10000000.0; Ok(( input, From 53708f1f4c02dc75ea8925dde08b300db8c88515 Mon Sep 17 00:00:00 2001 From: Michael du Plessis Date: Mon, 8 Jan 2024 15:45:50 +0200 Subject: [PATCH 2/3] removed commented out code. The bug was that the number should be interperated as a signed 32 bit integer --- src/parser.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 89b5708..46ddd27 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -156,18 +156,6 @@ fn record<'a>(codec: Codec) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], AVLReco // contruct a datetime using the timestamp in since the unix epoch let timestamp = Utc.timestamp_millis_opt(timestamp as i64).single().unwrap(); - // let longitude = if longitude & 0x80000000 != 0 { - // -(longitude as i32) - // } else { - // longitude as i32 - // } as f64 - // / 10000000.0; - // let latitude = if latitude & 0x80000000 != 0 { - // -(latitude as i32) - // } else { - // latitude as i32 - // } as f64 - // / 10000000.0; let longitude = longitude as f64 / 10000000.0; let latitude = latitude as f64 / 10000000.0; From 69c5443f7a99e8ec6b8614a7b0a917bdbeb03367 Mon Sep 17 00:00:00 2001 From: DamianoPellegrini Date: Sat, 13 Jan 2024 03:21:48 +0100 Subject: [PATCH 3/3] test: added negative coordinates test also fixed a typo in docs Signed-off-by: Damiano Pellegrini --- src/parser.rs | 9 +++++++++ src/stream.rs | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 46ddd27..c2ccec7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -663,4 +663,13 @@ mod tests { panic!("Expected Incomplete error"); } } + + #[test] + fn parse_negative_emisphere_coordinates() { + let input = hex::decode("00000000000000460801000001776D58189001FA0A1F00F1194D80009C009D05000F9B0D06EF01F0001505C80045019B0105B5000BB6000A424257430F8044000002F1000060191000000BE1000100006E2B").unwrap(); + let (input, frame) = tcp_frame(&input).unwrap(); + assert_eq!(input, &[]); + assert_eq!(frame.records[0].longitude, -10.0); + assert_eq!(frame.records[0].latitude, -25.0); + } } diff --git a/src/stream.rs b/src/stream.rs index 4cab843..29e0ae0 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -53,7 +53,7 @@ impl TeltonikaStream { /// /// If this function encounters any form of I/O or other error, an error variant will be returned as in [`Read::read`]. /// - /// If no bytes are read from the stream, an error king of [`std::io::ErrorKind::ConnectionReset`] is returned. + /// If no bytes are read from the stream, an error kind of [`std::io::ErrorKind::ConnectionReset`] is returned. /// If the IMEI cannot be parsed, an error kind of [`std::io::ErrorKind::InvalidData`] is returned. pub fn read_imei(&mut self) -> io::Result { let mut parse_buf: Vec = Vec::with_capacity(self.imei_buf_capacity * 2); @@ -94,7 +94,7 @@ impl TeltonikaStream { /// /// If this function encounters any form of I/O or other error, an error variant will be returned as in [`Read::read`]. /// - /// If no bytes are read from the stream, an error king of [`std::io::ErrorKind::ConnectionReset`] is returned. + /// If no bytes are read from the stream, an error kind of [`std::io::ErrorKind::ConnectionReset`] is returned. /// If the frame cannot be parsed, an error kind of [`std::io::ErrorKind::InvalidData`] is returned. pub fn read_frame(&mut self) -> io::Result { let mut parse_buf: Vec = Vec::with_capacity(self.packet_buf_capacity * 2); @@ -220,7 +220,7 @@ impl TeltonikaStream { /// /// If this function encounters any form of I/O or other error, an error variant will be returned as in [`Read::read`]. /// - /// If no bytes are read from the stream, an error king of [`std::io::ErrorKind::ConnectionReset`] is returned. + /// If no bytes are read from the stream, an error kind of [`std::io::ErrorKind::ConnectionReset`] is returned. /// If the IMEI cannot be parsed, an error kind of [`std::io::ErrorKind::InvalidData`] is returned. pub async fn read_imei_async(&mut self) -> io::Result { let mut parse_buf: Vec = Vec::with_capacity(self.imei_buf_capacity * 2); @@ -261,7 +261,7 @@ impl TeltonikaStream { /// /// If this function encounters any form of I/O or other error, an error variant will be returned as in [`Read::read`]. /// - /// If no bytes are read from the stream, an error king of [`std::io::ErrorKind::ConnectionReset`] is returned. + /// If no bytes are read from the stream, an error kind of [`std::io::ErrorKind::ConnectionReset`] is returned. /// If the frame cannot be parsed, an error kind of [`std::io::ErrorKind::InvalidData`] is returned. pub async fn read_frame_async(&mut self) -> io::Result { let mut parse_buf: Vec = Vec::with_capacity(self.packet_buf_capacity * 2);