1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#![no_std]
pub mod blocking;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct StandardId(u16);
impl StandardId {
pub fn new(id: u16) -> Result<StandardId, ()> {
if id <= 0x7FF {
Ok(StandardId(id))
} else {
Err(())
}
}
}
impl core::convert::From<StandardId> for u16 {
fn from(id: StandardId) -> u16 {
id.0
}
}
impl core::convert::From<StandardId> for u32 {
fn from(id: StandardId) -> u32 {
id.0 as u32
}
}
impl ExtendedId {
pub fn new(id: u32) -> Result<ExtendedId, ()> {
if id <= 0x1FFF_FFFF {
Ok(ExtendedId(id))
} else {
Err(())
}
}
}
impl core::convert::From<ExtendedId> for u32 {
fn from(id: ExtendedId) -> u32 {
id.0
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ExtendedId(u32);
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Id {
Standard(StandardId),
Extended(ExtendedId),
}
impl Id {
pub fn new_standard(id: u16) -> Result<Id, ()> {
Ok(StandardId::new(id)?.into())
}
pub fn new_extended(id: u32) -> Result<Id, ()> {
Ok(ExtendedId::new(id)?.into())
}
}
impl core::convert::From<StandardId> for Id {
fn from(id: StandardId) -> Id {
Id::Standard(id)
}
}
impl core::convert::From<ExtendedId> for Id {
fn from(id: ExtendedId) -> Id {
Id::Extended(id)
}
}
pub trait Frame: Sized {
fn new(id: Id, data: &[u8]) -> Result<Self, ()>;
fn new_remote(id: Id, dlc: usize) -> Result<Self, ()>;
fn is_extended(&self) -> bool;
fn is_standard(&self) -> bool {
!self.is_extended()
}
fn is_remote_frame(&self) -> bool;
fn is_data_frame(&self) -> bool {
!self.is_remote_frame()
}
fn id(&self) -> Id;
fn dlc(&self) -> usize;
fn data(&self) -> &[u8];
}
pub trait Can {
type Frame: Frame;
type Error;
fn try_transmit(&mut self, frame: &Self::Frame)
-> nb::Result<Option<Self::Frame>, Self::Error>;
fn try_receive(&mut self) -> nb::Result<Self::Frame, Self::Error>;
}