Skip to content

Begin source reorganization with linux/can.h #4446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#[macro_use]
mod macros;
mod reorg;

cfg_if! {
if #[cfg(feature = "rustc-dep-of-std")] {
Expand All @@ -38,6 +39,9 @@ cfg_if! {

pub use core::ffi::c_void;

#[allow(unused_imports)] // needed while the module is empty on some platforms
pub use reorg::*;

cfg_if! {
if #[cfg(windows)] {
mod primitives;
Expand Down
141 changes: 141 additions & 0 deletions src/reorg/linux_uapi/linux/can.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//! `linux/can.h`

pub(crate) mod j1939;
pub(crate) mod raw;

pub use j1939::*;
pub use raw::*;

use crate::prelude::*;

pub const CAN_EFF_FLAG: canid_t = 0x80000000;
pub const CAN_RTR_FLAG: canid_t = 0x40000000;
pub const CAN_ERR_FLAG: canid_t = 0x20000000;

pub const CAN_SFF_MASK: canid_t = 0x000007FF;
pub const CAN_EFF_MASK: canid_t = 0x1FFFFFFF;
pub const CAN_ERR_MASK: canid_t = 0x1FFFFFFF;
pub const CANXL_PRIO_MASK: crate::canid_t = CAN_SFF_MASK;

pub type canid_t = u32;

pub const CAN_SFF_ID_BITS: c_int = 11;
pub const CAN_EFF_ID_BITS: c_int = 29;
pub const CANXL_PRIO_BITS: c_int = CAN_SFF_ID_BITS;

pub type can_err_mask_t = u32;

pub const CAN_MAX_DLC: c_int = 8;
pub const CAN_MAX_DLEN: usize = 8;

pub const CANFD_MAX_DLC: c_int = 15;
pub const CANFD_MAX_DLEN: usize = 64;

pub const CANXL_MIN_DLC: c_int = 0;
pub const CANXL_MAX_DLC: c_int = 2047;
pub const CANXL_MAX_DLC_MASK: c_int = 0x07FF;
pub const CANXL_MIN_DLEN: usize = 1;
pub const CANXL_MAX_DLEN: usize = 2048;

s! {
#[repr(align(8))]
#[allow(missing_debug_implementations)]
pub struct can_frame {
pub can_id: canid_t,
// FIXME(1.0): this field was renamed to `len` in Linux 5.11
pub can_dlc: u8,
__pad: u8,
__res0: u8,
pub len8_dlc: u8,
pub data: [u8; CAN_MAX_DLEN],
}
}

pub const CANFD_BRS: c_int = 0x01;
pub const CANFD_ESI: c_int = 0x02;
pub const CANFD_FDF: c_int = 0x04;

s! {
#[repr(align(8))]
#[allow(missing_debug_implementations)]
pub struct canfd_frame {
pub can_id: canid_t,
pub len: u8,
pub flags: u8,
__res0: u8,
__res1: u8,
pub data: [u8; CANFD_MAX_DLEN],
}
}

pub const CANXL_XLF: c_int = 0x80;
pub const CANXL_SEC: c_int = 0x01;

s! {
#[repr(align(8))]
#[allow(missing_debug_implementations)]
pub struct canxl_frame {
pub prio: canid_t,
pub flags: u8,
pub sdt: u8,
pub len: u16,
pub af: u32,
pub data: [u8; CANXL_MAX_DLEN],
}
}

pub const CAN_MTU: usize = size_of::<can_frame>();
pub const CANFD_MTU: usize = size_of::<canfd_frame>();
pub const CANXL_MTU: usize = size_of::<canxl_frame>();
// FIXME(offset_of): use `core::mem::offset_of!` once that is available
// https://github.com/rust-lang/rfcs/pull/3308
// pub const CANXL_HDR_SIZE: usize = core::mem::offset_of!(canxl_frame, data);
pub const CANXL_HDR_SIZE: usize = 12;
pub const CANXL_MIN_MTU: usize = CANXL_HDR_SIZE + 64;
pub const CANXL_MAX_MTU: usize = CANXL_MTU;

pub const CAN_RAW: c_int = 1;
pub const CAN_BCM: c_int = 2;
pub const CAN_TP16: c_int = 3;
pub const CAN_TP20: c_int = 4;
pub const CAN_MCNET: c_int = 5;
pub const CAN_ISOTP: c_int = 6;
pub const CAN_J1939: c_int = 7;
pub const CAN_NPROTO: c_int = 8;

pub const SOL_CAN_BASE: c_int = 100;

s! {
#[allow(missing_debug_implementations)]
pub struct sockaddr_can {
pub can_family: crate::sa_family_t,
pub can_ifindex: c_int,
pub can_addr: __c_anonymous_sockaddr_can_can_addr,
}

pub struct __c_anonymous_sockaddr_can_tp {
pub rx_id: canid_t,
pub tx_id: canid_t,
}

pub struct __c_anonymous_sockaddr_can_j1939 {
pub name: u64,
pub pgn: u32,
pub addr: u8,
}

pub struct can_filter {
pub can_id: canid_t,
pub can_mask: canid_t,
}
}

s_no_extra_traits! {
#[allow(missing_debug_implementations)]
pub union __c_anonymous_sockaddr_can_can_addr {
pub tp: __c_anonymous_sockaddr_can_tp,
pub j1939: __c_anonymous_sockaddr_can_j1939,
}
}

pub const CAN_INV_FILTER: canid_t = 0x20000000;
60 changes: 60 additions & 0 deletions src/reorg/linux_uapi/linux/can/j1939.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! `linux/can/j1939.h`

pub use crate::linux::can::*;

pub const J1939_MAX_UNICAST_ADDR: c_uchar = 0xfd;
pub const J1939_IDLE_ADDR: c_uchar = 0xfe;
pub const J1939_NO_ADDR: c_uchar = 0xff;
pub const J1939_NO_NAME: c_ulong = 0;
pub const J1939_PGN_REQUEST: c_uint = 0x0ea00;
pub const J1939_PGN_ADDRESS_CLAIMED: c_uint = 0x0ee00;
pub const J1939_PGN_ADDRESS_COMMANDED: c_uint = 0x0fed8;
pub const J1939_PGN_PDU1_MAX: c_uint = 0x3ff00;
pub const J1939_PGN_MAX: c_uint = 0x3ffff;
pub const J1939_NO_PGN: c_uint = 0x40000;

pub type pgn_t = u32;
pub type priority_t = u8;
pub type name_t = u64;

pub const SOL_CAN_J1939: c_int = SOL_CAN_BASE + CAN_J1939;

// FIXME(cleanup): these could use c_enum if it can accept anonymous enums.

pub const SO_J1939_FILTER: c_int = 1;
pub const SO_J1939_PROMISC: c_int = 2;
pub const SO_J1939_SEND_PRIO: c_int = 3;
pub const SO_J1939_ERRQUEUE: c_int = 4;

pub const SCM_J1939_DEST_ADDR: c_int = 1;
pub const SCM_J1939_DEST_NAME: c_int = 2;
pub const SCM_J1939_PRIO: c_int = 3;
pub const SCM_J1939_ERRQUEUE: c_int = 4;

pub const J1939_NLA_PAD: c_int = 0;
pub const J1939_NLA_BYTES_ACKED: c_int = 1;
pub const J1939_NLA_TOTAL_SIZE: c_int = 2;
pub const J1939_NLA_PGN: c_int = 3;
pub const J1939_NLA_SRC_NAME: c_int = 4;
pub const J1939_NLA_DEST_NAME: c_int = 5;
pub const J1939_NLA_SRC_ADDR: c_int = 6;
pub const J1939_NLA_DEST_ADDR: c_int = 7;

pub const J1939_EE_INFO_NONE: c_int = 0;
pub const J1939_EE_INFO_TX_ABORT: c_int = 1;
pub const J1939_EE_INFO_RX_RTS: c_int = 2;
pub const J1939_EE_INFO_RX_DPO: c_int = 3;
pub const J1939_EE_INFO_RX_ABORT: c_int = 4;

s! {
pub struct j1939_filter {
pub name: name_t,
pub name_mask: name_t,
pub pgn: pgn_t,
pub pgn_mask: pgn_t,
pub addr: u8,
pub addr_mask: u8,
}
}

pub const J1939_FILTER_MAX: c_int = 512;
15 changes: 15 additions & 0 deletions src/reorg/linux_uapi/linux/can/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! `linux/can/raw.h`

pub use crate::linux::can::*;

pub const SOL_CAN_RAW: c_int = SOL_CAN_BASE + CAN_RAW;
pub const CAN_RAW_FILTER_MAX: c_int = 512;

// FIXME(cleanup): use `c_enum!`, which needs to be adapted to allow omitting a type.
pub const CAN_RAW_FILTER: c_int = 1;
pub const CAN_RAW_ERR_FILTER: c_int = 2;
pub const CAN_RAW_LOOPBACK: c_int = 3;
pub const CAN_RAW_RECV_OWN_MSGS: c_int = 4;
pub const CAN_RAW_FD_FRAMES: c_int = 5;
pub const CAN_RAW_JOIN_FILTERS: c_int = 6;
pub const CAN_RAW_XL_FRAMES: c_int = 7;
4 changes: 4 additions & 0 deletions src/reorg/linux_uapi/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! The `linux` directory within `include/uapi` in the Linux source tree.

pub(crate) mod can;
pub use can::*;
4 changes: 4 additions & 0 deletions src/reorg/linux_uapi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! This directory maps to `include/uapi` in the Linux source tree.

pub(crate) mod linux;
pub use linux::*;
12 changes: 12 additions & 0 deletions src/reorg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! This module contains the future directory structure. If possible, new definitions should
//! get added here.
//!
//! Eventually everything should be moved over, and we will move this directory to the top
//! level in `src`.

cfg_if! {
if #[cfg(target_os = "linux")] {
mod linux_uapi;
pub use linux_uapi::*;
}
}
Loading
Loading