-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
112 lines (97 loc) · 2.13 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/miwagner/socanui/candevice"
"github.com/miwagner/socanui/ui"
"github.com/rivo/tview"
)
const (
DEFAULT_INTERFACE = "vcan0"
)
func main() {
uselog := flag.Bool("l", false, "log file")
usehelp := flag.Bool("h", false, "help")
useversion := flag.Bool("v", false, "version")
flag.Parse()
log.SetOutput(io.Discard)
if *uselog {
file, err := os.OpenFile("socanui.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(file)
}
if *usehelp {
help()
os.Exit(0)
}
if *useversion {
println(ui.VERSION)
os.Exit(0)
}
caninf := DEFAULT_INTERFACE
args := flag.Args()
if len(args) == 1 {
caninf = args[0]
}
if len(args) > 1 {
help()
os.Exit(1)
}
log.Printf("Interface: %s", caninf)
// CAN bus
candev, err := candevice.NewDevice(caninf)
if err != nil {
log.Println(err)
fmt.Printf("Error: %v\n", err)
if err.Error() != "Interface is not up" {
fmt.Println("########################################")
fmt.Println("You can add a virtual CAN Interface:")
fmt.Println("sudo modprobe vcan")
fmt.Println("sudo ip link add dev vcan0 type vcan")
fmt.Println("sudo ip link set up vcan0")
fmt.Print("\nYou can generate testdata as follow:\n")
fmt.Println("cangen vcan0")
fmt.Println("########################################")
}
os.Exit(1)
}
// CAN connect
err = candev.Connect()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer candev.Sck.Close()
// tview application
app := tview.NewApplication()
defer app.Stop()
// create ui
ui.CreateSocanUI(app, candev)
if err = app.EnableMouse(true).Run(); err != nil {
panic(err)
}
}
// help
func help() {
fmt.Println(`
SocketCAN User Interface (socanui)
Usage:
socanui [options] interface
Interface:
SocketCAN Interface such as "can0", "vcan0", "slcan0"
Options:
-l log debug to file "socanui.log"
-h display this help and exit
-v output version information and exit
Examples:
socanui can0
(connect to can0 interface)
socanui -l vcan0
(connect to vcan0 interface and write debug log)
`)
}