forked from brutella/can
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candump.go
87 lines (69 loc) · 1.65 KB
/
candump.go
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
// This program logs can frames to the console similar to candump from can-utils[1].
//
// [1]: https://github.com/linux-can/can-utils
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"github.com/aisprid/can"
)
var i = flag.String("if", "", "network interface name")
func main() {
flag.Parse()
if len(*i) == 0 {
flag.Usage()
os.Exit(1)
}
iface, err := net.InterfaceByName(*i)
if err != nil {
log.Fatalf("Could not find network interface %s (%v)", *i, err)
}
conn, err := can.NewReadWriteCloserForInterface(iface)
if err != nil {
log.Fatal(err)
}
bus := can.NewBus(conn)
bus.SubscribeFunc(logCANFrame)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
signal.Notify(c, os.Kill)
go func() {
select {
case <-c:
bus.Disconnect()
os.Exit(1)
}
}()
bus.ConnectAndPublish()
}
// logCANFrame logs a frame with the same format as candump from can-utils.
func logCANFrame(frm can.Frame) {
data := trimSuffix(frm.Data[:], 0x00)
length := fmt.Sprintf("[%x]", frm.Length)
log.Printf("%-3s %-4x %-3s % -24X '%s'\n", *i, frm.ID, length, data, printableString(data[:]))
}
// trim returns a subslice of s by slicing off all trailing b bytes.
func trimSuffix(s []byte, b byte) []byte {
for i := len(s) - 1; i >= 0; i-- {
if s[i] != b {
return s[:i+1]
}
}
return []byte{}
}
// printableString creates a string from s and replaces non-printable bytes (i.e. 0-32, 127)
// with '.' – similar how candump from can-utils does it.
func printableString(s []byte) string {
var ascii []byte
for _, b := range s {
if b < 32 || b > 126 {
b = byte('.')
}
ascii = append(ascii, b)
}
return string(ascii)
}