/
can.go
146 lines (125 loc) · 2.89 KB
/
can.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
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
// @@
// @ Author : Eacher
// @ Date : 2023-09-06 13:56:59
// @ LastEditTime : 2024-01-09 16:29:37
// @ LastEditors : Eacher
// @ --------------------------------------------------------------------------------<
// @ Description :
// @ --------------------------------------------------------------------------------<
// @ FilePath : /20yyq/can-debugger/flag/can.go
// @@
package flag
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/20yyq/can-debugger/iface"
"github.com/20yyq/can-debugger/read"
"github.com/20yyq/can-debugger/sockcan"
"github.com/20yyq/can-debugger/write"
)
type FlagSetFunc func(*flag.FlagSet)
const helpOutput = `
candebugber CAN-frames via CAN_RAW sockets.
Usage: candebugber <device> <debugber_name>.
<device>:
Examples:
can0
<debugber_name>:
{read}:
{write}:
{-id} for 'classic' CAN 2.0 data frames
3 (SFF) or 8 (EFF) hex chars
{-data} for 'classic' CAN 2.0 data frames
0-8 byte string
Examples:
-id 888 -data 1122334455667788
-id 123 -data DEADBEEF
-id 999 -data
-id 123 -data 1
-id 213 -data 311223344
`
var (
canInterfaceName string
canDebuggerName string
)
var (
runMap map[string]func() error
)
func init() {
if len(os.Args) < 3 {
if len(os.Args) > 0 && (os.Args[1] == "help" || os.Args[1] == "-h") {
fmt.Print(helpOutput)
os.Exit(0)
}
fmt.Println("os.Args need min three arg")
os.Exit(1)
}
canInterfaceName, canDebuggerName = os.Args[1], os.Args[2]
runMap = map[string]func() error{
"read": readRuning,
"write": writeRuning,
"iface": ifaceRuning,
}
}
func Init() error {
fs := flag.NewFlagSet(canDebuggerName, flag.ContinueOnError)
switch canDebuggerName {
case "write":
write.InitFlagArge(fs)
case "iface":
iface.InitFlagArge(fs)
case "read":
fallthrough
case "":
return nil
default:
return fmt.Errorf("flag init '%s' not", canDebuggerName)
}
return fs.Parse(os.Args[3:])
}
func Runing() error {
if f, ok := runMap[canDebuggerName]; ok && f != nil {
return f()
}
return fmt.Errorf("running '%s' not method", canDebuggerName)
}
func ifaceRuning() error {
notify, stop := make(chan struct{}), make(chan struct{})
go listening(notify, stop)
err := iface.Run(canInterfaceName)
close(stop)
<-notify
return err
}
func readRuning() error {
return sockcanRuning(read.Run)
}
func writeRuning() error {
return sockcanRuning(write.Run)
}
func sockcanRuning(f func(*sockcan.Can)) error {
can, err := sockcan.NewCan(canInterfaceName)
if err == nil {
notify, stop := make(chan struct{}), make(chan struct{})
go func() {
listening(notify, stop)
can.Disconnect()
}()
f(can)
close(stop)
<-notify
}
return err
}
func listening(notify chan struct{}, stop <-chan struct{}) {
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGTSTP)
select {
case <-stop:
case <-quit:
}
close(notify)
}