-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.rs
179 lines (153 loc) · 5.3 KB
/
converter.rs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// MIT License
// Copyright (c) 2023 Michael de Gans
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use jeep::Event;
use clap::Parser;
use serde::{Deserialize, Serialize};
use socketcan::CANFrame;
use std::fs::File;
use std::io::{BufRead, BufReader};
#[derive(Parser, Debug)]
#[command(
author,
version,
about = "Parse a `candump -L` file into events or errors",
long_about = None
)]
struct Args {
/// Candump file
#[arg(short, long)]
in_file: String,
/// Json lines output file.
#[arg(short, long)]
out_file: String,
/// IDs to filter by
#[arg(short, long, value_parser=clap_num::maybe_hex::<u32>)]
filters: Option<Vec<u32>>,
}
/// parse a candump (-L) line into (timestamp, interface, id, data)
fn parse_candump_line(
line: &str,
filters: &Option<Vec<u32>>,
) -> Option<(u128, CANFrame)> {
// FIXME? use regex instead?
let split: Vec<&str> = line.split(['.', ' ', '#']).collect();
let components: [&str; 5] = split.try_into().ok()?;
let [timestamp_sec, timestamp_subsec, _, id, hex_data] = components;
// check id first, so we can quickly filter
let id = u32::from_str_radix(id, 16).ok()?;
if let Some(filters) = filters {
if !filters.contains(&id) {
return None;
}
}
let timestamp_sec = timestamp_sec.replace('(', "");
let timestamp_subsec = timestamp_subsec.replace(')', "");
let timestamp: u128 = (timestamp_sec.to_owned() + ×tamp_subsec)
.parse()
.ok()?;
// https://stackoverflow.com/questions/52987181/how-can-i-convert-a-hex-string-to-a-u8-slice
let data: Vec<u8> = (0..hex_data.len())
.step_by(2)
.map(|i| {
u8::from_str_radix(&hex_data[i..i + 2], 16)
.expect(&format!("invalid hex character in data:{line}"))
})
.collect();
debug_assert!(data.len() <= 8);
Some((timestamp, CANFrame::new(id, &data, false, false).ok()?))
}
fn write_json<W, M>(
writer: &mut W,
message: &M,
timestamp: u128,
) -> Result<(), Box<dyn std::error::Error>>
where
M: Serialize,
W: std::io::Write,
{
#[derive(Serialize, Deserialize)]
struct TimestampedPayload<P> {
timestamp: u128,
payload: P,
}
let json = serde_json::to_string(&TimestampedPayload {
timestamp,
payload: message,
})?;
writeln!(writer, "{json}")?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let in_file = File::open(args.in_file)?;
let mut out_file = File::create(args.out_file)?;
let mut lines = BufReader::new(in_file).lines();
while let Some(Ok(line)) = lines.next() {
if let Some((timestamp, frame)) =
parse_candump_line(&line, &args.filters)
{
let result = Event::parse(frame);
write_json(&mut out_file, &result, timestamp)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{parse_candump_line, CANFrame};
#[test]
fn test_from_candump_line() {
let filters = Some(vec![0x44, 0x236]);
let lines_frames: [(&str, Option<CANFrame>); 3] = [
(
"(1436509052.249713) vcan0 044#2A366C2BBA",
Some(
CANFrame::new(
0x044,
&[0x2A, 0x36, 0x6C, 0x2B, 0xBA],
false,
false,
)
.unwrap(),
),
),
("(1436509052.449847) vcan0 0F6#7ADFE07BD2", None),
(
"(1436509052.650004) vcan0 236#C3406B09F4C88036",
Some(
CANFrame::new(
0x236,
&[0xC3, 0x40, 0x6B, 0x09, 0xF4, 0xC8, 0x80, 0x36],
false,
false,
)
.unwrap(),
),
),
];
for (line, expected) in lines_frames {
let actual = parse_candump_line(line, &filters);
if let Some((_, frame)) = actual {
assert_eq!(frame.id(), expected.unwrap().id());
assert_eq!(frame.data(), expected.unwrap().data());
} else {
assert!(expected.is_none());
}
}
}
}