From a53f4cc224f3f16e80f4b41facc4f6abd3856ac4 Mon Sep 17 00:00:00 2001 From: Cyril Fougeray Date: Fri, 25 Oct 2024 09:23:29 +0200 Subject: [PATCH] can: fix ack number with a high pid (#278) The ack number uses the pid in its 16 most significant bits and a counter in the lower 16 bits. This allows us to filter out acks that are not for the current process. But, the pid can be encoded over more than 16 bits, so this fix allows the usage of the 16 lsb of the pid in the ack number, the msb of the pid are lost. Likelyhood that the 16 lsb of the PID are the same between two processes that use raw can-fd is small-enough for us to use this method. --- mcu-interface/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mcu-interface/src/lib.rs b/mcu-interface/src/lib.rs index 2b707578..c2b750de 100644 --- a/mcu-interface/src/lib.rs +++ b/mcu-interface/src/lib.rs @@ -46,20 +46,21 @@ pub trait MessagingInterface { } /// Create a unique ack number -/// - prefix with process ID +/// - prefix with process ID (16 bits, the least significant bits) /// - suffix with counter /// /// this added piece of information in the ack number is not strictly necessary /// but helps filter out acks that are not for us (e.g. acks for other processes) #[inline] fn create_ack(counter: u16) -> u32 { - process::id() << 16 | counter as u32 + (process::id() & 0xFFFF) << 16_u32 | counter as u32 } -/// Check that ack contains the process ID +/// Check that ack contains 16 least significant bits of the process ID #[inline] fn is_ack_for_us(ack_number: u32) -> bool { - ack_number >> 16 == process::id() + // cast looses the upper bits + ((ack_number >> 16) as u16) == (process::id() as u16) } /// handle new main mcu message, reference implementation