Skip to content

Commit 475e60f

Browse files
authored
Merge pull request #4446 from tgross35/libc-reorganization
Begin source reorganization with `linux/can.h`
2 parents 2a9f3d5 + d08c394 commit 475e60f

File tree

10 files changed

+255
-190
lines changed

10 files changed

+255
-190
lines changed

libc-test/test/check_style.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@ use std::path::Path;
1616

1717
use style::{Result, StyleChecker};
1818

19+
/// Relative to `src/`.
20+
const SKIP_PREFIXES: &[&str] = &[
21+
// Don't run the style checker on the reorganized portion of the crate while we figure
22+
// out what style we want.
23+
"new/",
24+
];
25+
1926
#[test]
2027
fn check_style() {
21-
let root_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../src");
22-
walk(&root_dir).unwrap();
28+
let src_root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../src");
29+
walk(&src_root).unwrap();
2330
eprintln!("good style!");
2431
}
2532

26-
fn walk(root_dir: &Path) -> Result<()> {
33+
fn walk(src_root: &Path) -> Result<()> {
2734
let mut style_checker = StyleChecker::new();
2835

2936
for entry in glob::glob(&format!(
3037
"{}/**/*.rs",
31-
root_dir.to_str().expect("dir should be valid UTF-8")
38+
src_root.to_str().expect("dir should be valid UTF-8")
3239
))? {
3340
let entry = entry?;
41+
let relpath = entry.strip_prefix(src_root).expect("known path");
42+
if SKIP_PREFIXES.iter().any(|pfx| relpath.starts_with(pfx)) {
43+
continue;
44+
}
3445

3546
let name = entry
3647
.file_name()

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#[macro_use]
2424
mod macros;
25+
mod new;
2526

2627
cfg_if! {
2728
if #[cfg(feature = "rustc-dep-of-std")] {
@@ -31,6 +32,9 @@ cfg_if! {
3132

3233
pub use core::ffi::c_void;
3334

35+
#[allow(unused_imports)] // needed while the module is empty on some platforms
36+
pub use new::*;
37+
3438
cfg_if! {
3539
if #[cfg(windows)] {
3640
mod primitives;

src/new/linux_uapi/linux/can.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//! Header: `uapi/linux/can.h`
2+
3+
// FIXME(ctest): we shouldn't have to specify the path but garando doesn't find modules otherwise
4+
#[path = "can/j1939.rs"]
5+
pub(crate) mod j1939;
6+
#[path = "can/raw.rs"]
7+
pub(crate) mod raw;
8+
9+
pub use j1939::*;
10+
pub use raw::*;
11+
12+
use crate::prelude::*;
13+
14+
pub const CAN_EFF_FLAG: canid_t = 0x80000000;
15+
pub const CAN_RTR_FLAG: canid_t = 0x40000000;
16+
pub const CAN_ERR_FLAG: canid_t = 0x20000000;
17+
18+
pub const CAN_SFF_MASK: canid_t = 0x000007FF;
19+
pub const CAN_EFF_MASK: canid_t = 0x1FFFFFFF;
20+
pub const CAN_ERR_MASK: canid_t = 0x1FFFFFFF;
21+
pub const CANXL_PRIO_MASK: crate::canid_t = CAN_SFF_MASK;
22+
23+
pub type canid_t = u32;
24+
25+
pub const CAN_SFF_ID_BITS: c_int = 11;
26+
pub const CAN_EFF_ID_BITS: c_int = 29;
27+
pub const CANXL_PRIO_BITS: c_int = CAN_SFF_ID_BITS;
28+
29+
pub type can_err_mask_t = u32;
30+
31+
pub const CAN_MAX_DLC: c_int = 8;
32+
pub const CAN_MAX_DLEN: usize = 8;
33+
34+
pub const CANFD_MAX_DLC: c_int = 15;
35+
pub const CANFD_MAX_DLEN: usize = 64;
36+
37+
pub const CANXL_MIN_DLC: c_int = 0;
38+
pub const CANXL_MAX_DLC: c_int = 2047;
39+
pub const CANXL_MAX_DLC_MASK: c_int = 0x07FF;
40+
pub const CANXL_MIN_DLEN: usize = 1;
41+
pub const CANXL_MAX_DLEN: usize = 2048;
42+
43+
s! {
44+
#[repr(align(8))]
45+
pub struct can_frame {
46+
pub can_id: canid_t,
47+
// FIXME(1.0): this field was renamed to `len` in Linux 5.11
48+
pub can_dlc: u8,
49+
__pad: u8,
50+
__res0: u8,
51+
pub len8_dlc: u8,
52+
pub data: [u8; CAN_MAX_DLEN],
53+
}
54+
}
55+
56+
pub const CANFD_BRS: c_int = 0x01;
57+
pub const CANFD_ESI: c_int = 0x02;
58+
pub const CANFD_FDF: c_int = 0x04;
59+
60+
s! {
61+
#[repr(align(8))]
62+
pub struct canfd_frame {
63+
pub can_id: canid_t,
64+
pub len: u8,
65+
pub flags: u8,
66+
__res0: u8,
67+
__res1: u8,
68+
pub data: [u8; CANFD_MAX_DLEN],
69+
}
70+
}
71+
72+
pub const CANXL_XLF: c_int = 0x80;
73+
pub const CANXL_SEC: c_int = 0x01;
74+
75+
s! {
76+
#[repr(align(8))]
77+
pub struct canxl_frame {
78+
pub prio: canid_t,
79+
pub flags: u8,
80+
pub sdt: u8,
81+
pub len: u16,
82+
pub af: u32,
83+
pub data: [u8; CANXL_MAX_DLEN],
84+
}
85+
}
86+
87+
pub const CAN_MTU: usize = size_of::<can_frame>();
88+
pub const CANFD_MTU: usize = size_of::<canfd_frame>();
89+
pub const CANXL_MTU: usize = size_of::<canxl_frame>();
90+
// FIXME(offset_of): use `core::mem::offset_of!` once that is available
91+
// https://github.com/rust-lang/rfcs/pull/3308
92+
// pub const CANXL_HDR_SIZE: usize = core::mem::offset_of!(canxl_frame, data);
93+
pub const CANXL_HDR_SIZE: usize = 12;
94+
pub const CANXL_MIN_MTU: usize = CANXL_HDR_SIZE + 64;
95+
pub const CANXL_MAX_MTU: usize = CANXL_MTU;
96+
97+
pub const CAN_RAW: c_int = 1;
98+
pub const CAN_BCM: c_int = 2;
99+
pub const CAN_TP16: c_int = 3;
100+
pub const CAN_TP20: c_int = 4;
101+
pub const CAN_MCNET: c_int = 5;
102+
pub const CAN_ISOTP: c_int = 6;
103+
pub const CAN_J1939: c_int = 7;
104+
pub const CAN_NPROTO: c_int = 8;
105+
106+
pub const SOL_CAN_BASE: c_int = 100;
107+
108+
s_no_extra_traits! {
109+
pub struct sockaddr_can {
110+
pub can_family: crate::sa_family_t,
111+
pub can_ifindex: c_int,
112+
pub can_addr: __c_anonymous_sockaddr_can_can_addr,
113+
}
114+
115+
pub union __c_anonymous_sockaddr_can_can_addr {
116+
pub tp: __c_anonymous_sockaddr_can_tp,
117+
pub j1939: __c_anonymous_sockaddr_can_j1939,
118+
}
119+
120+
pub struct __c_anonymous_sockaddr_can_tp {
121+
pub rx_id: canid_t,
122+
pub tx_id: canid_t,
123+
}
124+
125+
pub struct __c_anonymous_sockaddr_can_j1939 {
126+
pub name: u64,
127+
pub pgn: u32,
128+
pub addr: u8,
129+
}
130+
131+
pub struct can_filter {
132+
pub can_id: canid_t,
133+
pub can_mask: canid_t,
134+
}
135+
}
136+
137+
pub const CAN_INV_FILTER: canid_t = 0x20000000;

src/new/linux_uapi/linux/can/j1939.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! `linux/can/j1939.h`
2+
3+
pub use crate::linux::can::*;
4+
5+
pub const J1939_MAX_UNICAST_ADDR: c_uchar = 0xfd;
6+
pub const J1939_IDLE_ADDR: c_uchar = 0xfe;
7+
pub const J1939_NO_ADDR: c_uchar = 0xff;
8+
pub const J1939_NO_NAME: c_ulong = 0;
9+
pub const J1939_PGN_REQUEST: c_uint = 0x0ea00;
10+
pub const J1939_PGN_ADDRESS_CLAIMED: c_uint = 0x0ee00;
11+
pub const J1939_PGN_ADDRESS_COMMANDED: c_uint = 0x0fed8;
12+
pub const J1939_PGN_PDU1_MAX: c_uint = 0x3ff00;
13+
pub const J1939_PGN_MAX: c_uint = 0x3ffff;
14+
pub const J1939_NO_PGN: c_uint = 0x40000;
15+
16+
pub type pgn_t = u32;
17+
pub type priority_t = u8;
18+
pub type name_t = u64;
19+
20+
pub const SOL_CAN_J1939: c_int = SOL_CAN_BASE + CAN_J1939;
21+
22+
// FIXME(cleanup): these could use c_enum if it can accept anonymous enums.
23+
24+
pub const SO_J1939_FILTER: c_int = 1;
25+
pub const SO_J1939_PROMISC: c_int = 2;
26+
pub const SO_J1939_SEND_PRIO: c_int = 3;
27+
pub const SO_J1939_ERRQUEUE: c_int = 4;
28+
29+
pub const SCM_J1939_DEST_ADDR: c_int = 1;
30+
pub const SCM_J1939_DEST_NAME: c_int = 2;
31+
pub const SCM_J1939_PRIO: c_int = 3;
32+
pub const SCM_J1939_ERRQUEUE: c_int = 4;
33+
34+
pub const J1939_NLA_PAD: c_int = 0;
35+
pub const J1939_NLA_BYTES_ACKED: c_int = 1;
36+
pub const J1939_NLA_TOTAL_SIZE: c_int = 2;
37+
pub const J1939_NLA_PGN: c_int = 3;
38+
pub const J1939_NLA_SRC_NAME: c_int = 4;
39+
pub const J1939_NLA_DEST_NAME: c_int = 5;
40+
pub const J1939_NLA_SRC_ADDR: c_int = 6;
41+
pub const J1939_NLA_DEST_ADDR: c_int = 7;
42+
43+
pub const J1939_EE_INFO_NONE: c_int = 0;
44+
pub const J1939_EE_INFO_TX_ABORT: c_int = 1;
45+
pub const J1939_EE_INFO_RX_RTS: c_int = 2;
46+
pub const J1939_EE_INFO_RX_DPO: c_int = 3;
47+
pub const J1939_EE_INFO_RX_ABORT: c_int = 4;
48+
49+
s! {
50+
pub struct j1939_filter {
51+
pub name: name_t,
52+
pub name_mask: name_t,
53+
pub pgn: pgn_t,
54+
pub pgn_mask: pgn_t,
55+
pub addr: u8,
56+
pub addr_mask: u8,
57+
}
58+
}
59+
60+
pub const J1939_FILTER_MAX: c_int = 512;

src/new/linux_uapi/linux/can/raw.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! `linux/can/raw.h`
2+
3+
pub use crate::linux::can::*;
4+
5+
pub const SOL_CAN_RAW: c_int = SOL_CAN_BASE + CAN_RAW;
6+
pub const CAN_RAW_FILTER_MAX: c_int = 512;
7+
8+
// FIXME(cleanup): use `c_enum!`, which needs to be adapted to allow omitting a type.
9+
pub const CAN_RAW_FILTER: c_int = 1;
10+
pub const CAN_RAW_ERR_FILTER: c_int = 2;
11+
pub const CAN_RAW_LOOPBACK: c_int = 3;
12+
pub const CAN_RAW_RECV_OWN_MSGS: c_int = 4;
13+
pub const CAN_RAW_FD_FRAMES: c_int = 5;
14+
pub const CAN_RAW_JOIN_FILTERS: c_int = 6;
15+
pub const CAN_RAW_XL_FRAMES: c_int = 7;

src/new/linux_uapi/linux/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! The `linux` directory within `include/uapi` in the Linux source tree.
2+
3+
pub(crate) mod can;
4+
pub use can::*;

src/new/linux_uapi/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! This directory maps to `include/uapi` in the Linux source tree.
2+
3+
pub(crate) mod linux;
4+
pub use linux::*;

src/new/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! This module contains the future directory structure. If possible, new definitions should
2+
//! get added here.
3+
//!
4+
//! Eventually everything should be moved over, and we will move this directory to the top
5+
//! level in `src`.
6+
7+
cfg_if! {
8+
if #[cfg(target_os = "linux")] {
9+
mod linux_uapi;
10+
pub use linux_uapi::*;
11+
}
12+
}

0 commit comments

Comments
 (0)