Doip server initial code #4
Conversation
- Renamed hearder_parser.rs to header_parser.rs (typo fix) - Added unit tests for header parsing - Updated MAX_DOIP_MESSAGE_SIZE to 4MB
|
Is this code supposed to build? When I ran cargo check and cargo build, I encountered some errors. IMO, code should build successfully. |
a11f029 to
c674ff3
Compare
build is successful is now. |
src/doip/diagnostic_message.rs
Outdated
| pub fn parse(payload: &[u8]) -> Result<Self, Error> { | ||
| let header: [u8; 4] = | ||
| payload | ||
| .get(..4) |
There was a problem hiding this comment.
magic number? other places I could see used const
Not just here, please check below ones
There was a problem hiding this comment.
Fixed all magic numbers across the codebase (named constants , protocol versions, and message sizes). Will push in the next commit.
|
fix minor formatting inconsistencies |
arvindr19
left a comment
There was a problem hiding this comment.
Only header_parser.rs has any tracing at all. Every other file has no tracing.
| /// when the `DoIP` entity cannot process a received message due to header-level issues. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| #[repr(u8)] | ||
| pub enum GenericNackCode { |
There was a problem hiding this comment.
Fixed , re-exported now
| // Response codes per ISO 13400-2:2019 Table 25 | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| #[repr(u8)] | ||
| pub enum ResponseCode { |
There was a problem hiding this comment.
Fixed , re-exported now.
| // Diagnostic message negative ack codes per ISO 13400-2:2019 Table 28 | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| #[repr(u8)] | ||
| pub enum NackCode { |
src/doip/header_parser.rs
Outdated
| inverse_version: DEFAULT_PROTOCOL_VERSION_INV, | ||
| payload_type: payload_type as u16, | ||
| payload_length: u32::try_from(payload.len()) | ||
| .expect("DoIP payload exceeds u32::MAX (protocol limit is 4MB)"), |
There was a problem hiding this comment.
lib should never panic, return Result instead
There was a problem hiding this comment.
Fixed, retruns Result now.
darkwisebear
left a comment
There was a problem hiding this comment.
I didn't go into details yet, but from what I see there are some general things that need cleanup before I will do that:
- Consolidate error types and use
thiserroreverywhere. - Introduce a trait for message types and use everywhere
- Deduplicate common parsing code
src/doip/alive_check.rs
Outdated
| use bytes::{BufMut, Bytes, BytesMut}; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum Error { |
There was a problem hiding this comment.
This crate references thiserror. You could use it to declare this error in a more convenient way.
src/doip/vehicle_id.rs
Outdated
| use bytes::{BufMut, Bytes, BytesMut}; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum Error { |
There was a problem hiding this comment.
A similar definition exists in alive_check.rs. Imo we should have a common definition of an error type for the entire module.
There was a problem hiding this comment.
moved to mod.rs and shared across
|
|
||
| // Vehicle Identification Request (0x0001) - no payload | ||
| #[derive(Debug, Clone, PartialEq, Eq, Default)] | ||
| pub struct Request; |
There was a problem hiding this comment.
The shape of this module is very similar to that of alive_check.rs. I think there should be a trait for message types so that there is an impl Request for VehicleIdRequest or the like.
src/doip/vehicle_id.rs
Outdated
| let eid: [u8; 6] = payload | ||
| .get(..Self::LEN) | ||
| .and_then(|s| s.try_into().ok()) | ||
| .ok_or(Error::PayloadTooShort { | ||
| expected: Self::LEN, | ||
| actual: payload.len(), | ||
| })?; |
There was a problem hiding this comment.
Such code could be factored out, as there is similar code also in alive_check.rs. I think there should be a submodule messages with common definitions and then types for each message.
src/doip/header_parser.rs
Outdated
|
|
||
| impl PayloadType { | ||
| #[must_use] | ||
| pub fn from_u16(value: u16) -> Option<Self> { |
There was a problem hiding this comment.
Why not impl TryFrom<u16> for PayloadType? This is more canonical than such a custom function.
src/server/config.rs
Outdated
| let bind = server | ||
| .bind_address | ||
| .as_deref() | ||
| .unwrap_or(DEFAULT_BIND_ADDRESS); |
There was a problem hiding this comment.
serde allows for specifying defaults inside the type definition. This would save you a lot of code that of the scheme "if value { copy } else { use default }".
There was a problem hiding this comment.
Yes , it is updated now
src/doip/alive_check.rs
Outdated
| @@ -0,0 +1,144 @@ | |||
| /* | |||
| * Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) | |||
There was a problem hiding this comment.
year should be 2026, please check all the license headers
c674ff3 to
81e9ff5
Compare
- Consolidate errors into DoipError; add TryFrom<u8> on all enums - Strongly type activation_type field (ActivationType, not u8) - Add DoipParseable / DoipSerializable traits + DoipPayload dispatch enum - Override serialized_len() on all impls; fix array size magic numbers - Make ParseError pub(crate); add serde(default), tracing
81e9ff5 to
9fa0c6e
Compare
|
Please run cargo fmt --check; it currently fails. Once the CI workflow PR is merged, this PR will need to be reworked, so it’s better to fix it beforehand. |
Summary
Checklist