Skip to content

Commit

Permalink
can: fix ack number with a high pid (#278)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fouge authored Oct 25, 2024
1 parent e8c998e commit a53f4cc
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions mcu-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a53f4cc

Please sign in to comment.