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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! # can-utils-rs
//!
//! `can-utils-rs` is a Rust library and CLI utility for working with
//! **Linux SocketCAN interfaces**. It provides an interactive workflow
//! for creating CAN interfaces and inspecting CAN traffic with a
//! colorized CAN dump.
//!
//! The tool currently supports:
//!
//! - Creating and managing Linux CAN interfaces
//! - Automatic installation of required system prerequisites
//! - Pretty, colorized CAN frame dumping
//!- Easy CAN frame sending once or cyclically
//!
//! The goal of this project is to provide a **friendlier and more visual
//! alternative to common Linux CAN tools** such as `candump`, while still
//! integrating seamlessly with the standard SocketCAN ecosystem.
//!
//! ---
//!
//! # Features
//!
//! ## CAN Interface Setup
//!
//! The interactive wizard helps configure CAN interfaces without needing
//! to remember the exact Linux commands.
//!
//! Supported interface types:
//!
//! - **Native CAN** (`can0`, `can1`, …)
//! - **SLCAN serial adapters** (`slcan0` via `slcand`)
//! - **Virtual CAN** (`vcan0`) for development and simulation
//!
//! The tool previews the commands before executing them so users can see
//! exactly what will happen.
//!
//! Example native CAN configuration:
//!
//! ```text
//! sudo ip link set can0 up type can bitrate 500000
//! ```
//!
//! ---
//!
//! ## Pretty CAN Dump
//!
//! The project includes a **colorized CAN dump utility** similar to
//! Linux `candump`, but optimized for readability.
//!
//! Example output:
//!
//! ```text
//! (1773430351.545039) vcan0 7FF#00 11 22 33 44 55 66
//! ```
//!
//! Enhancements include:
//!
//! - colored timestamps
//! - highlighted interface name
//! - colored CAN IDs
//! - **per-byte coloring of payload data**
//!
//! This makes it significantly easier to visually scan CAN traffic.
//!
//! ---
//!
//! ## Automatic Prerequisite Detection
//!
//! On startup the tool checks whether required system utilities exist.
//!
//! Missing prerequisites are detected automatically and the user is
//! offered an option to install them.
//!
//! Example prompt:
//!
//! ```text
//! Missing prerequisites:
//! - can-utils / slcand
//!
//! ? Some required tools are missing. What do you want to do?
//! ❯ Install prerequisites
//! Continue anyway
//! Exit
//! ```
//!
//! Installation currently supports **APT-based systems** (Debian / Ubuntu).
//!
//! ---
//!
//! # Supported CAN Interface Types
//!
//! ## Native CAN
//!
//! Configures a hardware CAN controller via SocketCAN.
//!
//! ```text
//! sudo ip link set can0 up type can bitrate 500000
//! ```
//!
//! Typical hardware:
//!
//! - PCI CAN adapters
//! - USB SocketCAN adapters
//! - Raspberry Pi CAN HATs
//! - embedded CAN controllers
//!
//! ---
//!
//! ## SLCAN (Serial CAN)
//!
//! Serial CAN adapters using the `slcand` daemon.
//!
//! Example command sequence:
//!
//! ```text
//! sudo slcand -c -o -f -s6 -t hw -S 3000000 /dev/ttyUSB0 slcan0
//! sudo ip link set up slcan0
//! ```
//!
//! ---
//!
//! ## Virtual CAN
//!
//! Virtual CAN interfaces are extremely useful for development,
//! testing, and CI environments.
//!
//! ```text
//! sudo ip link add dev vcan0 type vcan
//! sudo ip link set up vcan0
//! ```
//!
//! ---
//!
//! # CLI
//! ## Installation
//!
//! Install the CLI locally with Cargo:
//!
//! ```text
//! cargo install --path .
//! ```
//!
//! Or install it from crates.io without needing to build from source:
//! ```text
//! cargo install can-utils-rs
//! ```
//!
//! Then run:
//!
//! ```text
//! can-utils-rs
//! ```
//!
//! ---
//!
//! ## Demo
//! Set-up and CAN dump
//!
//! 
//!
//!
//! CAN send and CAN dump
//!
//! 
//!
//! ---
//!
//! ## CLI Usage
//!
//! Running the binary launches an interactive menu.
//!
//! ```text
//! $ can-utils-rs
//!
//! ? What do you want to do?
//! ❯ Create or manage a CAN interface
//! Start pretty CAN dump
//! Create/manage CAN interface then start dump
//! Send CAN frame(s)
//! ```
//!
//! The setup workflow asks for:
//!
//! - interface type
//! - interface name
//! - CAN bitrate
//! - serial device (for SLCAN)
//!
//! Before applying changes the tool prints the commands that will run.
//!
//! ---
//!
//! # Use as a Library
//! ## Import
//! ```bash
//! cargo add anyhow
//! cargo add can-utils-rs
//! ```
//!
//! ## Example
//! Example for native CAN interface `can0`
//! ```no_run
//! use can_utils_rs::{CanConfig, NativeConfig, CanBitrate, setup};
//!
//!fn main() -> anyhow::Result<()> {
//! let config = CanConfig::Native(NativeConfig::new("can0".to_string(), CanBitrate::B500K));
//! setup::setup(config)?;
//! Ok(())
//!
//! // Use your CAN interface with SocketCAN via socketcan crate
//! }
//!```
//!
//! # Interface Safety
//!
//! If an interface already exists the user is asked how to proceed:
//!
//! ```text
//! Interface 'can0' already exists.
//!
//! ❯ Replace existing interface
//! Enter another interface name
//! Keep existing and skip setup
//! Cancel
//! ```
//!
//! When using this crate as a library an existing interface with the same name will be replaced.
//!
//! When replacing an interface the tool will:
//!
//! - bring the interface down
//! - remove it if necessary (`vcan` or `slcan`)
//! - recreate the interface with the selected configuration
//!
//! ---
//!
//! # Linux Requirements
//!
//! The following system packages are required:
//!
//! ```text
//! iproute2
//! can-utils
//! kmod
//! ```
//!
//! For virtual CAN interfaces:
//!
//! ```text
//! sudo modprobe vcan
//! ```
//!
//! The tool can optionally install these automatically on supported systems.
//!
//! ---
//!
//! # Design
//!
//! The project is structured into modular subsystems:
//!
//! ```text
//! setup::models - shared configuration types
//! setup::prompt - interactive setup wizard
//! setup::plan - command planning / preview generation
//! setup::exec - command execution helpers
//! setup::prereqs - prerequisite detection and installation
//!
//! dump::format - colored frame formatting
//! dump::live - live SocketCAN frame reader
//! dump::mod - dump orchestration and interface selection
//! ```
//!
//! This architecture keeps setup logic, CAN traffic handling, and CLI
//! interaction clearly separated.
//!
//! ---
//!
//! # License
//!
//! MIT
//!
//! ---
use Result;
use Select;
use fmt;
pub use crate;