-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support form parsing dlt-messages from streams
- Loading branch information
Showing
9 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2021 by Accenture ESR | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! # Provide API to parse dlt-messages from streams | ||
use crate::{ | ||
dlt::{HEADER_MAX_LENGTH, STORAGE_HEADER_LENGTH}, | ||
filtering::ProcessedDltFilterConfig, | ||
parse::{dlt_message, dlt_standard_header, DltParseError, ParsedMessage}, | ||
}; | ||
use futures::{AsyncRead, AsyncReadExt}; | ||
|
||
/// Parse a DLT-message from a stream. | ||
/// | ||
/// Note: This is an adapter for [`parse::dlt_message`] | ||
pub async fn dlt_stream<S: AsyncRead + Unpin>( | ||
stream: &mut S, | ||
filter_config_opt: Option<&ProcessedDltFilterConfig>, | ||
with_storage_header: bool, | ||
) -> Result<ParsedMessage, DltParseError> { | ||
let storage_len = if with_storage_header { | ||
STORAGE_HEADER_LENGTH as usize | ||
} else { | ||
0 | ||
}; | ||
|
||
let header_len = storage_len + HEADER_MAX_LENGTH as usize; | ||
let mut header_buf = vec![0u8; header_len]; | ||
stream.read_exact(&mut header_buf).await?; | ||
|
||
let (_, header) = dlt_standard_header(&header_buf[storage_len..])?; | ||
|
||
let message_len = storage_len + header.overall_length() as usize; | ||
let mut message_buf = vec![0u8; message_len]; | ||
message_buf[..header_len].copy_from_slice(&header_buf); | ||
|
||
stream.read_exact(&mut message_buf[header_len..]).await?; | ||
let (rest, message) = dlt_message(&message_buf, filter_config_opt, with_storage_header)?; | ||
|
||
if !rest.is_empty() { | ||
return Err(DltParseError::Unrecoverable(format!( | ||
"Incomplete parse ({} bytes remaining)!", | ||
rest.len() | ||
))); | ||
} | ||
Ok(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2021 by Accenture ESR | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::parse::ParsedMessage; | ||
use crate::stream::dlt_stream; | ||
use futures::{stream, TryStreamExt}; | ||
|
||
#[tokio::test] | ||
async fn test_dlt_stream() { | ||
#[rustfmt::skip] | ||
let bytes: Vec<u8> = vec![ | ||
0x44, 0x4C, 0x54, 0x01, 0x46, 0x93, 0x01, 0x5D, 0x79, 0x39, 0x0E, 0x00, 0x48, 0x46, | ||
0x50, 0x50, 0x3D, 0x1E, 0x00, 0xA8, 0x48, 0x46, 0x50, 0x50, 0x00, 0x00, 0x02, 0x48, | ||
0x00, 0x1C, 0x76, 0x49, 0x51, 0x08, 0x50, 0x61, 0x72, 0x61, 0x76, 0x63, 0x73, 0x6F, | ||
0x00, 0x82, 0x00, 0x00, 0x1A, 0x00, 0x5B, 0x35, 0x38, 0x34, 0x3A, 0x20, 0x53, 0x6F, | ||
0x6D, 0x65, 0x49, 0x70, 0x50, 0x6F, 0x73, 0x69, 0x78, 0x43, 0x6C, 0x69, 0x65, 0x6E, | ||
0x74, 0x5D, 0x20, 0x00, 0x00, 0x82, 0x00, 0x00, 0x12, 0x00, 0x53, 0x65, 0x6E, 0x64, | ||
0x53, 0x6F, 0x6D, 0x65, 0x49, 0x70, 0x4D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00, | ||
0x00, 0x82, 0x00, 0x00, 0x02, 0x00, 0x3A, 0x00, 0x23, 0x00, 0x00, 0x00, 0x10, 0x01, | ||
0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x11, 0x00, 0x3A, 0x20, 0x69, 0x6E, 0x73, 0x74, | ||
0x61, 0x6E, 0x63, 0x65, 0x5F, 0x69, 0x64, 0x20, 0x30, 0x78, 0x00, 0x42, 0x00, 0x01, | ||
0x00, 0x01, 0x00, 0x00, 0x82, 0x00, 0x00, 0x17, 0x00, 0x20, 0x6D, 0x65, 0x6D, 0x6F, | ||
0x72, 0x79, 0x20, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x20, 0x6C, 0x65, 0x6E, 0x67, | ||
0x74, 0x68, 0x20, 0x00, 0x44, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, | ||
]; | ||
|
||
let stream = stream::iter([Ok(bytes.clone())]); | ||
let mut reader = stream.into_async_read(); | ||
|
||
match dlt_stream(&mut reader, None, true).await { | ||
Ok(ParsedMessage::Item(message)) => { | ||
assert_eq!(bytes, message.as_bytes()); | ||
} | ||
_ => panic!("could not parse message"), | ||
} | ||
} | ||
} |