Skip to main content

simple_someip/protocol/
message_id.rs

1use crate::SD_MESSAGE_ID_VALUE;
2
3/// Newtype for a message ID.
4/// The Message ID is a 32-bit identifier that is unique for each message.
5/// It encodes both the service ID and the method ID.
6/// Message IDs are assumed to be unique for an entire vehicle network.
7#[derive(Clone, Copy, Eq, PartialEq)]
8pub struct MessageId(u32);
9
10impl From<u32> for MessageId {
11    fn from(message_id: u32) -> Self {
12        MessageId(message_id)
13    }
14}
15
16impl MessageId {
17    /// Message ID for Service Discovery
18    pub const SD: Self = Self::new(SD_MESSAGE_ID_VALUE);
19
20    /// Create a new `MessageId` directly.
21    #[must_use]
22    pub const fn new(message_id: u32) -> Self {
23        MessageId(message_id)
24    }
25
26    /// Create a new `MessageId` from service and method IDs.
27    #[must_use]
28    pub const fn new_from_service_and_method(service_id: u16, method_id: u16) -> Self {
29        MessageId(((service_id as u32) << 16) | method_id as u32)
30    }
31
32    /// Get the message ID
33    #[inline]
34    #[must_use]
35    pub const fn message_id(&self) -> u32 {
36        self.0
37    }
38
39    /// Set the message ID
40    #[inline]
41    pub const fn set_message_id(&mut self, message_id: u32) {
42        self.0 = message_id;
43    }
44
45    /// Get the service ID
46    #[inline]
47    #[must_use]
48    pub const fn service_id(&self) -> u16 {
49        (self.0 >> 16) as u16
50    }
51
52    /// Set the service ID
53    #[inline]
54    pub const fn set_service_id(&mut self, service_id: u16) {
55        self.0 = (self.0 & 0xFFFF) | ((service_id as u32) << 16);
56    }
57
58    /// Get the method ID
59    #[inline]
60    #[must_use]
61    pub const fn method_id(&self) -> u16 {
62        (self.0 & 0xFFFF) as u16
63    }
64
65    /// Set the method ID
66    #[inline]
67    pub const fn set_method_id(&mut self, method_id: u16) {
68        self.0 = (self.0 & 0xFFFF_0000) | method_id as u32;
69    }
70
71    /// Message is Event/Notification
72    #[inline]
73    #[must_use]
74    pub const fn is_event(&self) -> bool {
75        self.method_id() & 0x8000 != 0
76    }
77
78    /// Message is SOME/IP Service Discovery
79    #[inline]
80    #[must_use]
81    pub const fn is_sd(&self) -> bool {
82        self.0 == crate::SD_MESSAGE_ID_VALUE
83    }
84}
85
86impl std::fmt::Debug for MessageId {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        write!(
89            f,
90            "Message Id: {{ service_id: {:#02X}, method_id: {:#02X} }}",
91            self.service_id(),
92            self.method_id(),
93        )
94    }
95}