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

Commitb0a5fdb

Browse files
committed
CanDumpRecord removed lifetime, and made device an owned string. Made Reader an iterator for full records. CanAnyFrame now implements crate::Frame trait
1 parent30f09bc commitb0a5fdb

File tree

3 files changed

+107
-20
lines changed

3 files changed

+107
-20
lines changed

‎CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ The change log for the Rust [socketcan](https://crates.io/crates/socketcan) libr
66
##Unreleased features
77

88
- Added FdFlags::FDF bit mask for CANFD_FDF
9-
- The FDF flag is forced on when creating a CanFdFrame.
10-
- Updates to dump module:
9+
- The FDF flag is forced on when creating a CanFdFrame.
10+
- Updates to dump module:
1111
- Re-implemented with text parsing
12+
-`ParseError` now implements std`Error` trait via`thiserror::Error`
1213
- Parses FdFlags field properly
1314
- CANFD_FDF bit flag recognized on input
1415
- Fixed reading remote frames
1516
- Now reads remote length
17+
-`CanDumpRecord` removed lifetime, made`device` field an owned`String`, and is clonable.
18+
-`dump::Reader` is now an Iterator itself
19+
-
1620
- New unit tests
1721
-[#59](https://github.com/socketcan-rs/socketcan-rs/issues/59) Embedded Hal for CanFdSocket
1822

‎src/dump.rs

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@
3030
//! [csv](https://crates.io/crates/csv) crate.
3131
3232
usecrate::{
33+
frame::Frame,
3334
id::{id_from_raw,FdFlags},
3435
CanAnyFrame,CanDataFrame,CanFdFrame,CanFrame,CanRemoteFrame,ConstructionError,
3536
};
36-
use embedded_can::Frame;
37+
use embedded_can::FrameasEmbeddedFrame;
3738
use hex::FromHex;
39+
use itertools::Itertools;
3840
use libc::canid_t;
3941
use std::{
42+
fmt,
4043
fs::File,
4144
io::{self,BufRead,BufReader},
4245
path::Path,
43-
str,
4446
};
4547
use thiserror::Error;
4648

@@ -69,16 +71,45 @@ pub enum ParseError {
6971

7072
/// Recorded CAN frame.
7173
/// This corresponds to the information in a line from the candump log.
72-
#[derive(Debug)]
73-
pubstructCanDumpRecord<'a>{
74+
#[derive(Debug,Clone)]
75+
pubstructCanDumpRecord{
7476
/// The timestamp
7577
pubt_us:u64,
7678
/// The name of the device
77-
pubdevice:&'astr,
79+
pubdevice:String,
7880
/// The parsed frame
7981
pubframe:CanAnyFrame,
8082
}
8183

84+
impl fmt::DisplayforCanDumpRecord{
85+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
86+
write!(
87+
f,
88+
"({:.6}) {} {:03X}",
89+
1.0e-6*self.t_usasf64,
90+
self.device,
91+
self.frame.raw_id()
92+
)?;
93+
94+
useCanAnyFrame::*;
95+
matchself.frame{
96+
Normal(frame) =>{
97+
letmut parts = frame.data().iter().map(|v|format!("{:02X}", v));
98+
write!(f,"#{}", parts.join(""))
99+
}
100+
Remote(frame)if frame.len() ==0 => f.write_str("#R"),
101+
Remote(frame) =>{
102+
write!(f,"#R{}", frame.dlc())
103+
}
104+
Error(_frame) => f.write_str(""),
105+
Fd(frame) =>{
106+
letmut parts = frame.data().iter().map(|v|format!("{:02X}", v));
107+
write!(f,"##{}", parts.join(""))
108+
}
109+
}
110+
}
111+
}
112+
82113
/////////////////////////////////////////////////////////////////////////////
83114
// Reader
84115

@@ -110,10 +141,16 @@ impl Reader<File> {
110141

111142
impl<R:BufRead>Reader<R>{
112143
/// Returns an iterator over all records
113-
pubfnrecords(&mutself) ->CanDumpIter<R>{
114-
CanDumpIter{src:self}
144+
#[deprecated(since ="3.5.0", note ="Use `iter()`")]
145+
pubfnrecords(&mutself) ->CanDumpRecords<R>{
146+
CanDumpRecords{src:self}
115147
}
116148

149+
/// Returns an iterator over all records
150+
// pub fn iter(&mut self) -> CanDumpIter<R> {
151+
// CanDumpIter { src: self }
152+
// }
153+
117154
/// Advance state, returning next record.
118155
pubfnnext_record(&mutself) ->Result<Option<CanDumpRecord>,ParseError>{
119156
self.buf.clear();
@@ -151,7 +188,10 @@ impl<R: BufRead> Reader<R> {
151188
};
152189

153190
// device name
154-
let device = field_iter.next().ok_or(ParseError::UnexpectedEndOfLine)?;
191+
let device = field_iter
192+
.next()
193+
.ok_or(ParseError::UnexpectedEndOfLine)?
194+
.to_string();
155195

156196
// parse packet
157197
let can_raw = field_iter.next().ok_or(ParseError::UnexpectedEndOfLine)?;
@@ -206,13 +246,26 @@ impl<R: BufRead> Reader<R> {
206246
}
207247
}
208248

209-
/// Record iterator
249+
impl<R:BufRead>IteratorforReader<R>{
250+
typeItem =Result<CanDumpRecord,ParseError>;
251+
252+
fnnext(&mutself) ->Option<Self::Item>{
253+
// lift Option:
254+
matchself.next_record(){
255+
Ok(Some(rec)) =>Some(Ok(rec)),
256+
Ok(None) =>None,
257+
Err(e) =>Some(Err(e)),
258+
}
259+
}
260+
}
261+
262+
/// Original Record iterator
210263
#[derive(Debug)]
211-
pubstructCanDumpIter<'a,R:'a>{
264+
pubstructCanDumpRecords<'a,R:'a>{
212265
src:&'amutReader<R>,
213266
}
214267

215-
impl<R: io::Read>IteratorforCanDumpIter<'_,BufReader<R>>{
268+
impl<R: io::Read>IteratorforCanDumpRecords<'_,BufReader<R>>{
216269
typeItem =Result<(u64,CanAnyFrame),ParseError>;
217270

218271
fnnext(&mutself) ->Option<Self::Item>{
@@ -225,12 +278,6 @@ impl<R: io::Read> Iterator for CanDumpIter<'_, BufReader<R>> {
225278
}
226279
}
227280

228-
// TODO: Remove in the next major version update
229-
/// Obsolete iterator name, now called `CanDumpIter`
230-
#[allow(type_alias_bounds)]
231-
#[deprecated(since="3.5.0", note="Renamed to `CanDumpIter`")]
232-
pubtypeCanDumpRecords<'a,R:'a> =CanDumpIter<'a,R>;
233-
234281
/////////////////////////////////////////////////////////////////////////////
235282

236283
#[cfg(test)]

‎src/frame.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,44 @@ pub enum CanAnyFrame {
216216
Fd(CanFdFrame),
217217
}
218218

219+
implFrameforCanAnyFrame{
220+
/// Get the composite SocketCAN ID word, with EFF/RTR/ERR flags
221+
fnid_word(&self) ->canid_t{
222+
useCanAnyFrame::*;
223+
matchself{
224+
Normal(frame) => frame.id_word(),
225+
Remote(frame) => frame.id_word(),
226+
Error(frame) => frame.id_word(),
227+
Fd(frame) => frame.id_word(),
228+
}
229+
}
230+
231+
/// Sets the CAN ID for the frame
232+
fnset_id(&mutself,id:implInto<Id>){
233+
useCanAnyFrame::*;
234+
matchself{
235+
Normal(frame) => frame.set_id(id),
236+
Remote(frame) => frame.set_id(id),
237+
Error(frame) => frame.set_id(id),
238+
Fd(frame) => frame.set_id(id),
239+
}
240+
}
241+
242+
/// Sets the data payload of the frame.
243+
fnset_data(&mutself,data:&[u8]) ->Result<(),ConstructionError>{
244+
useCanAnyFrame::*;
245+
matchself{
246+
Normal(frame) => frame.set_data(data),
247+
Remote(frame) => frame.set_data(data),
248+
Error(frame) => frame.set_data(data),
249+
Fd(frame) => frame.set_data(data),
250+
}
251+
}
252+
}
253+
219254
implEmbeddedFrameforCanAnyFrame{
220-
/// Create a new CAN 2.0 data frame
255+
/// Create a new CAN frame
256+
/// If the data
221257
fnnew(id:implInto<Id>,data:&[u8]) ->Option<Self>{
222258
if data.len() <=CAN_MAX_DLEN{
223259
CanDataFrame::new(id, data).map(CanAnyFrame::Normal)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp