using System.Buffers.Binary;
namespace CanCommon;
///
/// Helpers to build / inspect a classic Linux SocketCAN struct can_frame
/// (16 bytes, host byte order for can_id):
///
/// offset 0..3 : can_id (u32, host endian, flags in high bits)
/// offset 4 : len / can_dlc
/// offset 5 : __pad
/// offset 6 : __res0
/// offset 7 : len8_dlc
/// offset 8..15 : data[8]
///
/// The benchmark works directly on 16-byte buffers to avoid per-frame
/// allocations on the hot path.
///
public static class CanFrame
{
public const int Size = 16;
// can_id flag bits (Linux )
public const uint CAN_EFF_FLAG = 0x80000000; // extended (29-bit) frame
public const uint CAN_RTR_FLAG = 0x40000000; // remote transmission request
public const uint CAN_ERR_FLAG = 0x20000000; // error message frame
public const uint CAN_SFF_MASK = 0x000007FF; // standard 11-bit id mask
public const uint CAN_EFF_MASK = 0x1FFFFFFF; // extended 29-bit id mask
public const uint CAN_ERR_MASK = 0x1FFFFFFF; // error-frame class mask
/// Allocate a 16-byte standard-frame buffer with a constant payload fill.
public static byte[] BuildStandard(uint standardId, int dlc, byte payloadFill)
{
if (dlc is < 0 or > 8) throw new ArgumentOutOfRangeException(nameof(dlc));
var buf = new byte[Size];
BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(0, 4), standardId & CAN_SFF_MASK);
buf[4] = (byte)dlc;
for (int i = 0; i < dlc; i++) buf[8 + i] = payloadFill;
return buf;
}
// --- Bus-load modelling -------------------------------------------------
// On-the-wire size of a classic CAN data frame, used to convert a frame rate
// into a bus-load %. Fixed-form (never-stuffed) tail = CRC delim(1) + ACK(1)
// + ACK delim(1) + EOF(7) + IFS(3) = 13 bits.
//
// nominal : no bit-stuffing (matches `canbusload -i`)
// worstcase: max bit-stuffing possible (matches `canbusload` default)
private const int FixedTailBits = 13;
/// Bits subject to bit-stuffing: SOF..CRC (control/arbitration + data + CRC).
private static int StuffableBits(int dlc, bool extended) =>
(extended ? 54 : 34) + 8 * dlc; // std: SOF1+ID11+RTR1+IDE1+r0_1+DLC4+CRC15=34; eff adds SRR+IDB18+r1=+20
/// On-wire bits per frame assuming no bit-stuffing (lower bound of bus load).
public static int BusBitsNominal(int dlc, bool extended) =>
StuffableBits(dlc, extended) + FixedTailBits;
/// On-wire bits per frame with worst-case bit-stuffing (upper bound of bus load).
public static int BusBitsWorstCase(int dlc, bool extended) =>
BusBitsNominal(dlc, extended) + (StuffableBits(dlc, extended) - 1) / 4;
public static uint GetRawId(byte[] frame16) =>
BinaryPrimitives.ReadUInt32LittleEndian(frame16.AsSpan(0, 4));
public static bool IsError(byte[] frame16) => (GetRawId(frame16) & CAN_ERR_FLAG) != 0;
public static bool IsExtended(byte[] frame16) => (GetRawId(frame16) & CAN_EFF_FLAG) != 0;
public static uint GetStandardId(byte[] frame16) => GetRawId(frame16) & CAN_SFF_MASK;
public static int GetDlc(byte[] frame16) => frame16[4] & 0x0F;
/// True when the first data bytes all equal .
public static bool PayloadAllEqual(byte[] frame16, int dlc, byte value)
{
for (int i = 0; i < dlc; i++)
if (frame16[8 + i] != value) return false;
return true;
}
}