Skip to content

Commit 8603b5f

Browse files
committed
Auto merge of #2006 - djarek:linux-can.h, r=JohnTitor
Define basic Linux SocketCAN constants and types Add definitions from `linux/can.h`, which is a "base" header for remainder of SocketCAN functionality. CAN bus (ISO-11898) is a communication standard used in automotive, automation and industrial solutions. Linux provides a socket-like interface to access raw CAN and protocols based on CAN, such as ISO-TP(ISO-15765) or SAE-J1939.
2 parents 2ec333c + 86eb6c1 commit 8603b5f

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

libc-test/build.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,7 @@ fn test_linux(target: &str) {
24422442
headers! {
24432443
cfg:
24442444
"asm/mman.h",
2445+
"linux/can.h",
24452446
"linux/dccp.h",
24462447
"linux/errqueue.h",
24472448
"linux/falloc.h",
@@ -2556,6 +2557,9 @@ fn test_linux(target: &str) {
25562557
});
25572558

25582559
cfg.skip_struct(move |ty| {
2560+
if ty.starts_with("__c_anonymous_") {
2561+
return true;
2562+
}
25592563
match ty {
25602564
// These cannot be tested when "resolv.h" is included and are tested
25612565
// in the `linux_elf.rs` file.
@@ -2590,7 +2594,9 @@ fn test_linux(target: &str) {
25902594
// can be an anonymous struct, so an extra struct,
25912595
// which is absent in musl, has to be defined.
25922596
"__exit_status" if musl => true,
2593-
2597+
// FIXME: J1939 requires kernel header version 5.4 or higher,
2598+
// the MIPS CI target has a lower version
2599+
"CAN_J1939" if mips => true,
25942600
_ => false,
25952601
}
25962602
});
@@ -2735,7 +2741,9 @@ fn test_linux(target: &str) {
27352741
// this one is an anonymous union
27362742
(struct_ == "ff_effect" && field == "u") ||
27372743
// `__exit_status` type is a patch which is absent in musl
2738-
(struct_ == "utmpx" && field == "ut_exit" && musl)
2744+
(struct_ == "utmpx" && field == "ut_exit" && musl) ||
2745+
// `can_addr` is an anonymous union
2746+
(struct_ == "sockaddr_can" && field == "can_addr")
27392747
});
27402748

27412749
cfg.volatile_item(|i| {

src/unix/linux_like/linux/align.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ macro_rules! expand_align {
5252
pub fd: ::c_int,
5353
pub pid: ::c_int,
5454
}
55+
56+
// linux/can.h
57+
#[repr(align(8))]
58+
pub struct can_frame {
59+
pub can_id: canid_t,
60+
pub can_dlc: u8,
61+
__pad: u8,
62+
__res0: u8,
63+
__res1: u8,
64+
pub data: [u8; CAN_MAX_DLEN],
65+
}
66+
67+
#[repr(align(8))]
68+
pub struct canfd_frame {
69+
pub can_id: canid_t,
70+
pub len: u8,
71+
pub flags: u8,
72+
__res0: u8,
73+
__res1: u8,
74+
pub data: [u8; CANFD_MAX_DLEN],
75+
}
5576
}
5677

5778
s_no_extra_traits! {

src/unix/linux_like/linux/mod.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pub type Elf64_Sxword = i64;
3636
pub type Elf32_Section = u16;
3737
pub type Elf64_Section = u16;
3838

39+
// linux/can.h
40+
pub type canid_t = u32;
41+
pub type can_err_mask_t = u32;
42+
3943
#[cfg_attr(feature = "extra_traits", derive(Debug))]
4044
pub enum fpos64_t {} // FIXME: fill this out with a struct
4145
impl ::Copy for fpos64_t {}
@@ -504,6 +508,23 @@ s! {
504508
pub ee_info: u32,
505509
pub ee_data: u32,
506510
}
511+
512+
// linux/can.h
513+
pub struct __c_anonymous_sockaddr_can_tp {
514+
pub rx_id: canid_t,
515+
pub tx_id: canid_t,
516+
}
517+
518+
pub struct __c_anonymous_sockaddr_can_j1939 {
519+
pub name: u64,
520+
pub pgn: u32,
521+
pub addr: u8,
522+
}
523+
524+
pub struct can_filter {
525+
pub can_id: canid_t,
526+
pub can_mask: canid_t,
527+
}
507528
}
508529

509530
s_no_extra_traits! {
@@ -577,6 +598,26 @@ s_no_extra_traits! {
577598
}
578599
}
579600

601+
cfg_if! {
602+
if #[cfg(libc_union)] {
603+
s_no_extra_traits! {
604+
// linux/can.h
605+
#[allow(missing_debug_implementations)]
606+
pub union __c_anonymous_sockaddr_can_can_addr {
607+
pub tp: __c_anonymous_sockaddr_can_tp,
608+
pub j1939: __c_anonymous_sockaddr_can_j1939,
609+
}
610+
611+
#[allow(missing_debug_implementations)]
612+
pub struct sockaddr_can {
613+
pub can_family: ::sa_family_t,
614+
pub can_ifindex: ::c_int,
615+
pub can_addr: __c_anonymous_sockaddr_can_can_addr,
616+
}
617+
}
618+
}
619+
}
620+
580621
cfg_if! {
581622
if #[cfg(feature = "extra_traits")] {
582623
impl PartialEq for sockaddr_nl {
@@ -2567,6 +2608,46 @@ pub const EDOM: ::c_int = 33;
25672608
pub const ERANGE: ::c_int = 34;
25682609
pub const EWOULDBLOCK: ::c_int = EAGAIN;
25692610

2611+
// linux/can.h
2612+
pub const CAN_EFF_FLAG: canid_t = 0x80000000;
2613+
pub const CAN_RTR_FLAG: canid_t = 0x40000000;
2614+
pub const CAN_ERR_FLAG: canid_t = 0x20000000;
2615+
pub const CAN_SFF_MASK: canid_t = 0x000007FF;
2616+
pub const CAN_EFF_MASK: canid_t = 0x1FFFFFFF;
2617+
pub const CAN_ERR_MASK: canid_t = 0x1FFFFFFF;
2618+
2619+
pub const CAN_SFF_ID_BITS: ::c_int = 11;
2620+
pub const CAN_EFF_ID_BITS: ::c_int = 29;
2621+
2622+
pub const CAN_MAX_DLC: ::c_int = 8;
2623+
pub const CAN_MAX_DLEN: usize = 8;
2624+
pub const CANFD_MAX_DLC: ::c_int = 15;
2625+
pub const CANFD_MAX_DLEN: usize = 64;
2626+
2627+
pub const CANFD_BRS: ::c_int = 0x01;
2628+
pub const CANFD_ESI: ::c_int = 0x02;
2629+
2630+
cfg_if! {
2631+
if #[cfg(libc_const_size_of)] {
2632+
pub const CAN_MTU: usize = ::mem::size_of::<can_frame>();
2633+
pub const CANFD_MTU: usize = ::mem::size_of::<canfd_frame>();
2634+
}
2635+
}
2636+
2637+
pub const CAN_RAW: ::c_int = 1;
2638+
pub const CAN_BCM: ::c_int = 2;
2639+
pub const CAN_TP16: ::c_int = 3;
2640+
pub const CAN_TP20: ::c_int = 4;
2641+
pub const CAN_MCNET: ::c_int = 5;
2642+
pub const CAN_ISOTP: ::c_int = 6;
2643+
pub const CAN_J1939: ::c_int = 7;
2644+
pub const CAN_NPROTO: ::c_int = 8;
2645+
2646+
pub const SOL_CAN_BASE: ::c_int = 100;
2647+
2648+
pub const CAN_INV_FILTER: canid_t = 0x20000000;
2649+
pub const CAN_RAW_FILTER_MAX: ::c_int = 512;
2650+
25702651
f! {
25712652
pub fn NLA_ALIGN(len: ::c_int) -> ::c_int {
25722653
return ((len) + NLA_ALIGNTO - 1) & !(NLA_ALIGNTO - 1)

0 commit comments

Comments
 (0)