8000 #9 Added functions to mutate a frame, as applicable. · socketcan-rs/socketcan-rs@329e9f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 329e9f4

Browse files
committed
#9 Added functions to mutate a frame, as applicable.
1 parent 6d56fd5 commit 329e9f4

File tree

2 files changed

+136
-32
lines changed

2 files changed

+136
-32
lines changed

src/frame.rs

Lines changed: 136 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn can_frame_default() -> can_frame {
8686
}
8787

8888
/// Creates a default C `can_frame`.
89+
/// This initializes the entire structure to zeros.
8990
#[inline(always)]
9091
pub fn canfd_frame_default() -> canfd_frame {
9192
unsafe { mem::zeroed() }
@@ -120,7 +121,6 @@ pub trait Frame: EmbeddedFrame {
120121

121122
/// Return the actual raw CAN ID (without EFF/RTR/ERR flags)
122123
fn raw_id(&self) -> canid_t {
123-
// TODO: This probably isn't necessary. Just use EFF_MASK?
124124
let mask = if self.is_extended() {
125125
CAN_EFF_MASK
126126
} else {
@@ -137,9 +137,9 @@ pub trait Frame: EmbeddedFrame {
137137
/// Return the CAN ID as the embedded HAL Id type.
138138
fn hal_id(&self) -> Id {
139139
if self.is_extended() {
140-
Id::Extended(ExtendedId::new(self.id_word() & CAN_EFF_MASK).unwrap())
140+
ExtendedId::new(self.id_word() & CAN_EFF_MASK).unwrap().into()
141141
} else {
142-
Id::Standard(StandardId::new((self.id_word() & CAN_SFF_MASK) as u16).unwrap())
142+
StandardId::new((self.id_word() & CAN_SFF_MASK) as u16).unwrap().into()
143143
}
144144
}
145145

@@ -152,6 +152,12 @@ pub trait Frame: EmbeddedFrame {
152152
fn is_error_frame(&self) -> bool {
153153
self.id_flags().contains(IdFlags::ERR)
154154
}
155+
156+
/// Sets the CAN ID for the frame
157+
fn set_id(&mut self, id: impl Into<Id>);
158+
159+
/// Sets the data payload of the frame.
160+
fn set_data(&mut self, data: &[u8]) -> Result<(), ConstructionError>;
155161
}
156162

157163
// ===== CanAnyFrame =====
@@ -303,6 +309,26 @@ impl Frame for CanFrame {
303309
Error(frame) => frame.id_word(),
304310
}
305311
}
312+
313+
/// Sets the CAN ID for the frame
314+
fn set_id(&mut self, id: impl Into<Id>) {
315+
use CanFrame::*;
316+
match self {
317+
Data(frame) => frame.set_id(id),
318+
Remote(frame) => frame.set_id(id),
319+
Error(frame) => frame.set_id(id),
320+
}
321+
}
322+
323+
/// Sets the data payload of the frame.
324+
fn set_data(&mut self, data< 6D40 span class=pl-kos>: &[u8]) -> Result<(), ConstructionError> {
325+
use CanFrame::*;
326+
match self {
327+
Data(frame) => frame.set_data(data),
328+
Remote(frame) => frame.set_data(data),
329+
Error(frame) => frame.set_data(data),
330+
}
331+
}
306332
}
307333

308334
impl Default for CanFrame {
@@ -391,18 +417,20 @@ pub struct CanDataFrame(can_frame);
391417

392418
impl CanDataFrame {
393419
/// Initializes a CAN frame from raw parts.
394-
pub fn init(id: u32, data: &[u8], flags: IdFlags) -> Result<Self, ConstructionError> {
395-
let n = data.len();
396-
397-
if n > CAN_MAX_DLEN {
398-
return Err(ConstructionError::TooMuchData);
420+
pub fn init(id: u32, data: &[u8], mut flags: IdFlags) -> Result<Self, ConstructionError> {
421+
match data.len() {
422+
n if n <= CAN_MAX_DLEN => {
423+
// TODO: Should this be a 'Wrong Frame Type' error?
424+
flags.remove(IdFlags::RTR | IdFlags::ERR);
425+
426+
let mut frame = can_frame_default();
427+
frame.can_id = init_id_word(id, flags)?;
428+
frame.can_dlc = n as u8;
429+
frame.data[..n].copy_from_slice(data);
430+
Ok(Self(frame))
431+
}
432+
_ => Err(ConstructionError::TooMuchData)
399433
}
400-
401-
let mut frame = can_frame_default();
402-
frame.can_id = init_id_word(id, flags)?;
403-
frame.can_dlc = n as u8;
404-
frame.data[..n].copy_from_slice(data);
405-
Ok(Self(frame))
406434
}
407435
}
408436

@@ -469,6 +497,27 @@ impl Frame for CanDataFrame {
469497
fn id_word(&self) -> u32 {
470498
self.0.can_id
471499
}
500+
501+
/// Sets the CAN ID for the frame
502+
fn set_id(&mut self, id: impl Into<Id>) {
503+
let id = id.into();
504+
let mut flags = IdFlags::empty();
505+
flags.set(IdFlags::EFF, id_is_extended(&id));
506+
507+
self.0.can_id = id_to_raw(id) | flags.bits();
508+
}
509+
510+
/// Sets the data payload of the frame.
511+
fn set_data(&mut self, data: &[u8]) -> Result<(), ConstructionError> {
512+
match data.len() {
513+
n if n <= CAN_MAX_DLEN => {
514+
self.0.can_dlc = n as u8;
515+
self.0.data[..n].copy_from_slice(data);
516+
Ok(())
517+
}
518+
_ => Err(ConstructionError::TooMuchData)
519+
}
520+
}
472521
}
473522

474523
impl Default for CanDataFrame {
@@ -541,6 +590,19 @@ impl AsRef<can_frame> for CanDataFrame {
541590
#[derive(Clone, Copy)]
542591
pub struct CanRemoteFrame(can_frame);
543592

593+
impl CanRemoteFrame {
594+
/// Sets the data length code for the frame
595+
pub fn set_dlc(&mut self, dlc: usize) -> Result<(), ConstructionError> {
596+
if dlc <= CAN_MAX_DLEN {
597+
self.0.can_dlc = dlc as u8;
598+
Ok(())
599+
}
600+
else {
601+
Err(ConstructionError::TooMuchData)
602+
}
603+
}
604+
}
605+
544606
im 6D38 pl AsPtr for CanRemoteFrame {
545607
type Inner = can_frame;
546608

@@ -611,6 +673,22 @@ impl Frame for CanRemoteFrame {
611673
fn id_word(&self) -> u32 {
612674
self.0.can_id
613675
}
676+
677+
/// Sets the CAN ID for the frame
678+
fn set_id(&mut self, id: impl Into<Id>) {
679+
let id = id.into();
680+
let mut flags = IdFlags::RTR;
681+
flags.set(IdFlags::EFF, id_is_extended(&id));
682+
683+
self.0.can_id = id_to_raw(id) | flags.bits();
684+
}
685+
686+
/// Sets the data payload of the frame.
687+
/// For the Remote frame, this just updates the DLC to the length of the
688+
/// data slice.
689+
fn set_data(&mut self, data: &[u8]) -> Result<(), ConstructionError> {
690+
self.set_dlc(data.len())
691+
}
614692
}
615693

616694
impl Default for CanRemoteFrame {
@@ -746,6 +824,16 @@ impl Frame for CanErrorFrame {
746824
fn id_word(&self) -> u32 {
747825
self.0.can_id
748826
}
827+
828+
/// Sets the CAN ID for the frame
829+
/// This does nothing on an error frame.
830+
fn set_id(&mut self, _id: impl Into<Id>) {}
831+
832+
/// Sets the data payload of the frame.
833+
/// This is an error on an error frame.
834+
fn set_data(&mut self, _data: &[u8]) -> Result<(), ConstructionError> {
835+
Err(ConstructionError::WrongFrameType)
836+
}
749837
}
750838

751839
impl fmt::Debug for CanErrorFrame {
@@ -802,21 +890,21 @@ impl CanFdFrame {
802890
mut flags: IdFlags,
803891
fd_flags: FdFlags,
804892
) -> Result<Self, ConstructionError> {
805-
let n = data.len();
806-
807-
if n > CANFD_MAX_DLEN {
808-
return Err(ConstructionError::TooMuchData);
893+
match data.len() {
894+
n if n > CANFD_MAX_DLEN => {
895+
// TODO: Should this be a 'Wrong Frame Type' error?
896+
flags.remove(IdFlags::RTR | IdFlags::ERR);
897+
898+
let mut frame = canfd_frame_default();
899+
frame.can_id = init_id_word(id, flags)?;
900+
frame.len = n as u8;
901+
frame.flags = fd_flags.bits();
902+
frame.data[..n].copy_from_slice(data);
903+
Ok(Self(frame))
904+
}
905+
_ => Err(ConstructionError::TooMuchData)
809906
}
810907

811-
flags.remove(IdFlags::RTR);
812-
813-
let mut frame = canfd_frame_default();
814-
frame.can_id = init_id_word(id, flags)?;
815-
frame.len = n as u8;
816-
frame.flags = fd_flags.bits();
817-
frame.data[..n].copy_from_slice(data);
818-
819-
Ok(Self(frame))
820908
}
821909

822910
/// Gets the flags for the FD frame.
@@ -922,6 +1010,27 @@ impl Frame for CanFdFrame {
9221010
fn id_word(&self) -> u32 {
9231011
self.0.can_id
9241012
}
1013+
1014+
/// Sets the CAN ID for the frame
1015+
fn set_id(&mut self, id: impl Into<Id>) {
1016+
let id = id.into();
1017+
let mut flags = IdFlags::empty();
1018+
flags.set(IdFlags::EFF, id_is_extended(&id));
1019+
1020+
self.0.can_id = id_to_raw(id) | flags.bits();
1021+
}
1022+
1023+
/// Sets the data payload of the frame.
1024+
fn set_data(&mut self, data: &[u8]) -> Result<(), ConstructionError> {
1025+
match data.len() {
1026+
n if n <= CANFD_MAX_DLEN => {
1027+
self.0.len = n as u8;
1028+
self.0.data[..n].copy_from_slice(data);
1029+
Ok(())
1030+
}
1031+
_ => Err(ConstructionError::TooMuchData)
1032+
}
1033+
}
9251034
}
9261035

9271036
impl Default for CanFdFrame {

src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ impl embedded_can::blocking::Can for CanSocket {
113113
fn receive(&mut self) -> Result<Self::Frame> {
114114
use CanFrame::*;
115115
match self.read_frame() {
116-
/*
117-
Ok(Data(frame)) => Ok(Data(frame)),
118-
Ok(Remote(frame)) => Ok(Remote(frame)),
119-
Ok(Error(frame)) => Err(frame.into_error().into()),
120-
*/
121116
Ok(Error(frame)) => Err(frame.into_error().into()),
122117
Ok(frame) => Ok(frame),
123118
Err(e) => Err(e.into()),

0 commit comments

Comments
 (0)
0