Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit403e004

Browse files
committed
#59 Embedded Hal for CanFdSocket
1 parente7b6848 commit403e004

File tree

4 files changed

+176
-67
lines changed

4 files changed

+176
-67
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The change log for the Rust [socketcan](https://crates.io/crates/socketcan) libr
1414
- Fixed reading remote frames
1515
- Now reads remote length
1616
- New unit tests
17-
17+
-[#59](https://github.com/socketcan-rs/socketcan-rs/issues/59) Embedded Hal for CanFdSocket
1818

1919
##[Version 3.4.0](https://github.com/socketcan-rs/socketcan-rs/compare/v3.3.1..v3.4.0) (2024-12-26)
2020

‎src/frame.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,71 @@ pub enum CanAnyFrame {
216216
Fd(CanFdFrame),
217217
}
218218

219+
implEmbeddedFrameforCanAnyFrame{
220+
/// Create a new CAN 2.0 data frame
221+
fnnew(id:implInto<Id>,data:&[u8]) ->Option<Self>{
222+
if data.len() <=CAN_MAX_DLEN{
223+
CanDataFrame::new(id, data).map(CanAnyFrame::Normal)
224+
}else{
225+
CanFdFrame::new(id, data).map(CanAnyFrame::Fd)
226+
}
227+
}
228+
229+
/// Create a new remote transmission request frame.
230+
fnnew_remote(id:implInto<Id>,dlc:usize) ->Option<Self>{
231+
CanRemoteFrame::new_remote(id, dlc).map(CanAnyFrame::Remote)
232+
}
233+
234+
/// Check if frame uses 29-bit extended ID format.
235+
fnis_extended(&self) ->bool{
236+
useCanAnyFrame::*;
237+
matchself{
238+
Normal(frame) => frame.is_extended(),
239+
Remote(frame) => frame.is_extended(),
240+
Error(frame) => frame.is_extended(),
241+
Fd(frame) => frame.is_extended(),
242+
}
243+
}
244+
245+
/// Check if frame is a remote transmission request.
246+
fnis_remote_frame(&self) ->bool{
247+
matches!(self,CanAnyFrame::Remote(_))
248+
}
249+
250+
/// Return the frame identifier.
251+
fnid(&self) ->Id{
252+
useCanAnyFrame::*;
253+
matchself{
254+
Normal(frame) => frame.id(),
255+
Remote(frame) => frame.id(),
256+
Error(frame) => frame.id(),
257+
Fd(frame) => frame.id(),
258+
}
259+
}
260+
261+
/// Data length
262+
fndlc(&self) ->usize{
263+
useCanAnyFrame::*;
264+
matchself{
265+
Normal(frame) => frame.dlc(),
266+
Remote(frame) => frame.dlc(),
267+
Error(frame) => frame.dlc(),
268+
Fd(frame) => frame.dlc(),
269+
}
270+
}
271+
272+
/// A slice into the actual data. Slice will always be <= 8 bytes in length
273+
fndata(&self) ->&[u8]{
274+
useCanAnyFrame::*;
275+
matchself{
276+
Normal(frame) => frame.data(),
277+
Remote(frame) => frame.data(),
278+
Error(frame) => frame.data(),
279+
Fd(frame) => frame.data(),
280+
}
281+
}
282+
}
283+
219284
impl fmt::UpperHexforCanAnyFrame{
220285
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
221286
matchself{

‎src/lib.rs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
unsafe_op_in_unsafe_fn
126126
)]
127127

128-
use std::{io::ErrorKind,mem::size_of};
128+
use std::mem::size_of;
129129

130130
// Re-export the embedded_can crate so that applications can rely on
131131
// finding the same version we use.
@@ -205,67 +205,3 @@ pub(crate) fn as_bytes_mut<T: Sized>(val: &mut T) -> &mut [u8] {
205205
let sz =size_of::<T>();
206206
unsafe{ std::slice::from_raw_parts_mut(valas*mut_as*mutu8, sz)}
207207
}
208-
209-
// ===== embedded_can I/O traits =====
210-
211-
impl embedded_can::blocking::CanforCanSocket{
212-
typeFrame =CanFrame;
213-
typeError =Error;
214-
215-
/// Blocking call to receive the next frame from the bus.
216-
///
217-
/// This block and wait for the next frame to be received from the bus.
218-
/// If an error frame is received, it will be converted to a `CanError`
219-
/// and returned as an error.
220-
fnreceive(&mutself) ->Result<Self::Frame>{
221-
useCanFrame::*;
222-
matchself.read_frame(){
223-
Ok(Error(frame)) =>Err(frame.into_error().into()),
224-
Ok(frame) =>Ok(frame),
225-
Err(e) =>Err(e.into()),
226-
}
227-
}
228-
229-
/// Blocking transmit of a frame to the bus.
230-
fntransmit(&mutself,frame:&Self::Frame) ->Result<()>{
231-
self.write_frame_insist(frame).map_err(|err| err.into())
232-
}
233-
}
234-
235-
impl embedded_can::nb::CanforCanSocket{
236-
typeFrame =CanFrame;
237-
typeError =Error;
238-
239-
/// Non-blocking call to receive the next frame from the bus.
240-
///
241-
/// If an error frame is received, it will be converted to a `CanError`
242-
/// and returned as an error.
243-
/// If no frame is available, it returns a `WouldBlck` error.
244-
fnreceive(&mutself) -> nb::Result<Self::Frame,Self::Error>{
245-
useCanFrame::*;
246-
matchself.read_frame(){
247-
Ok(Data(frame)) =>Ok(Data(frame)),
248-
Ok(Remote(frame)) =>Ok(Remote(frame)),
249-
Ok(Error(frame)) =>Err(crate::Error::from(frame.into_error()).into()),
250-
Err(err) =>Err(match err.kind(){
251-
ErrorKind::WouldBlock => nb::Error::WouldBlock,
252-
_ =>crate::Error::from(err).into(),
253-
}),
254-
}
255-
}
256-
257-
/// Non-blocking transmit of a frame to the bus.
258-
fntransmit(&mutself,frame:&Self::Frame) -> nb::Result<Option<Self::Frame>,Self::Error>{
259-
matchself.write_frame(frame){
260-
Ok(_) =>Ok(None),
261-
Err(err) =>{
262-
match err.kind(){
263-
ErrorKind::WouldBlock =>Err(nb::Error::WouldBlock),
264-
// TODO: How to indicate buffer is full?
265-
// ErrorKind::StorageFull => Ok(frame),
266-
_ =>Err(Error::from(err).into()),
267-
}
268-
}
269-
}
270-
}
271-
}

‎src/socket.rs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ use crate::{
1717
id::CAN_ERR_MASK,
1818
CanAnyFrame,CanFdFrame,CanFrame,CanRawFrame,IoError,IoErrorKind,IoResult,
1919
};
20+
pubuse embedded_can::{
21+
self, blocking::CanasBlockingCan, nb::CanasNonBlockingCan,ExtendedId,
22+
FrameasEmbeddedFrame,Id,StandardId,
23+
};
2024
use libc::{canid_t, socklen_t,AF_CAN,EINPROGRESS};
2125
use socket2::SockAddr;
2226
use std::{
2327
fmt,
24-
io::{Read,Write},
28+
io::{ErrorKind,Read,Write},
2529
mem::{size_of, size_of_val},
2630
os::{
2731
raw::{c_int, c_void},
@@ -538,6 +542,59 @@ impl Socket for CanSocket {
538542
}
539543
}
540544

545+
// ===== embedded_can I/O traits =====
546+
547+
impl embedded_can::blocking::CanforCanSocket{
548+
typeFrame =CanFrame;
549+
typeError =crate::Error;
550+
551+
/// Blocking call to receive the next frame from the bus.
552+
///
553+
/// This block and wait for the next frame to be received from the bus.
554+
/// If an error frame is received, it will be converted to a `CanError`
555+
/// and returned as an error.
556+
fnreceive(&mutself) ->crate::Result<Self::Frame>{
557+
matchself.read_frame(){
558+
Ok(CanFrame::Error(frame)) =>Err(frame.into_error().into()),
559+
Ok(frame) =>Ok(frame),
560+
Err(e) =>Err(e.into()),
561+
}
562+
}
563+
564+
/// Blocking transmit of a frame to the bus.
565+
fntransmit(&mutself,frame:&Self::Frame) ->crate::Result<()>{
566+
self.write_frame_insist(frame).map_err(|err| err.into())
567+
}
568+
}
569+
570+
impl embedded_can::nb::CanforCanSocket{
571+
typeFrame =CanFrame;
572+
typeError =crate::Error;
573+
574+
/// Non-blocking call to receive the next frame from the bus.
575+
///
576+
/// If an error frame is received, it will be converted to a `CanError`
577+
/// and returned as an error.
578+
/// If no frame is available, it returns a `WouldBlck` error.
579+
fnreceive(&mutself) -> nb::Result<Self::Frame,Self::Error>{
580+
matchself.read_frame(){
581+
Ok(CanFrame::Error(frame)) =>Err(Self::Error::from(frame.into_error()).into()),
582+
Ok(frame) =>Ok(frame),
583+
Err(err)if err.kind() ==ErrorKind::WouldBlock =>Err(nb::Error::WouldBlock),
584+
Err(err) =>Err(crate::Error::from(err).into()),
585+
}
586+
}
587+
588+
/// Non-blocking transmit of a frame to the bus.
589+
fntransmit(&mutself,frame:&Self::Frame) -> nb::Result<Option<Self::Frame>,Self::Error>{
590+
matchself.write_frame(frame){
591+
Ok(_) =>Ok(None),
592+
Err(err)if err.kind() ==ErrorKind::WouldBlock =>Err(nb::Error::WouldBlock),
593+
Err(err) =>Err(Self::Error::from(err).into()),
594+
}
595+
}
596+
}
597+
541598
implSocketOptionsforCanSocket{}
542599

543600
// Has no effect: #[deprecated(since = "3.1", note = "Use AsFd::as_fd() instead.")]
@@ -684,6 +741,57 @@ impl Socket for CanFdSocket {
684741

685742
implSocketOptionsforCanFdSocket{}
686743

744+
impl embedded_can::blocking::CanforCanFdSocket{
745+
typeFrame =CanAnyFrame;
746+
typeError =crate::Error;
747+
748+
/// Blocking call to receive the next frame from the bus.
749+
///
750+
/// This block and wait for the next frame to be received from the bus.
751+
/// If an error frame is received, it will be converted to a `CanError`
752+
/// and returned as an error.
753+
fnreceive(&mutself) ->crate::Result<Self::Frame>{
754+
matchself.read_frame(){
755+
Ok(CanAnyFrame::Error(frame)) =>Err(frame.into_error().into()),
756+
Ok(frame) =>Ok(frame),
757+
Err(e) =>Err(e.into()),
758+
}
759+
}
760+
761+
/// Blocking transmit of a frame to the bus.
762+
fntransmit(&mutself,frame:&Self::Frame) ->crate::Result<()>{
763+
self.write_frame_insist(frame).map_err(|err| err.into())
764+
}
765+
}
766+
767+
impl embedded_can::nb::CanforCanFdSocket{
768+
typeFrame =CanAnyFrame;
769+
typeError =crate::Error;
770+
771+
/// Non-blocking call to receive the next frame from the bus.
772+
///
773+
/// If an error frame is received, it will be converted to a `CanError`
774+
/// and returned as an error.
775+
/// If no frame is available, it returns a `WouldBlck` error.
776+
fnreceive(&mutself) -> nb::Result<Self::Frame,Self::Error>{
777+
matchself.read_frame(){
778+
Ok(CanAnyFrame::Error(frame)) =>Err(Self::Error::from(frame.into_error()).into()),
779+
Ok(frame) =>Ok(frame),
780+
Err(err)if err.kind() ==ErrorKind::WouldBlock =>Err(nb::Error::WouldBlock),
781+
Err(err) =>Err(crate::Error::from(err).into()),
782+
}
783+
}
784+
785+
/// Non-blocking transmit of a frame to the bus.
786+
fntransmit(&mutself,frame:&Self::Frame) -> nb::Result<Option<Self::Frame>,Self::Error>{
787+
matchself.write_frame(frame){
788+
Ok(_) =>Ok(None),
789+
Err(err)if err.kind() ==ErrorKind::WouldBlock =>Err(nb::Error::WouldBlock),
790+
Err(err) =>Err(Self::Error::from(err).into()),
791+
}
792+
}
793+
}
794+
687795
// Has no effect: #[deprecated(since = "3.1", note = "Use AsFd::as_fd() instead.")]
688796
implAsRawFdforCanFdSocket{
689797
fnas_raw_fd(&self) ->RawFd{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp