-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
207 lines (184 loc) · 5.9 KB
/
main.go
File metadata and controls
207 lines (184 loc) · 5.9 KB
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
type command struct {
name, params, desc string
}
var (
menus = map[string][]command{
"main": {
{"list", "", "List available controllers"},
{"show", "[ctrl]", "Controller information"},
{"select", "<ctrl>", "Select default controller"},
{"devices", "", "List available devices"},
{"paired-devices", "", "List paired devices"},
{"system-alias", "<name>", "Set controller alias"},
{"reset-alias", "", "Reset controller alias"},
{"power", "<on/off>", "Set controller power"},
{"pairable", "<on/off>", "Set controller pairable mode"},
{"discoverable", "<on/off>", "Set controller discoverable mode"},
{"agent", "<on/off/capability>", "Enable/disable agent with given capability"},
{"default-agent", "", "Set agent as the default one"},
{"advertise", "<on/off/type>", "Enable/disable advertising with given type"},
{"set-alias", "<alias>", "Set device alias"},
{"scan", "<on/off>", "Scan for devices"},
{"info", "[dev]", "Device information"},
{"pair", "[dev]", "Pair with device"},
{"trust", "[dev]", "Trust device"},
{"untrust", "[dev]", "Untrust device"},
{"block", "[dev]", "Block device"},
{"unblock", "[dev]", "Unblock device"},
{"remove", "<dev>", "Remove device"},
{"connect", "<dev>", "Connect device"},
{"disconnect", "[dev]", "Disconnect device"},
},
"advertise": {
{"set-uuids", "[uuid1 uuid2 ...]", "Set advertise uuids"},
{"set-service", "[uuid] [data=xx xx ...]", "Set advertise service data"},
{"set-manufacturer", "[id]", "Set advertise manufacturer data"},
{"set-tx-power", "<on/off>", "Enable/disable TX power to be advertised"},
{"set-name", "<on/off/name>", "Enable/disable local name to be advertised"},
{"set-appearance", "<value>", "Set custom appearance to be advertised"},
{"set-duration", "<seconds>", "Set advertise duration"},
{"set-timeout", "<seconds>", "Set advertise timeout"},
},
"scan": {
{"uuids", "[all/uuid1 uuid2 ...]", "Set/Get UUIDs filter"},
{"rssi", "[rssi]", "Set/Get RSSI filter, and clears pathloss"},
{"pathloss", "[pathloss]", "Set/Get Pathloss filter, and clears RSSI"},
{"transport", "[transport]", "Set/Get transport filter"},
{"duplicate-data", "[on/off]", "Set/Get duplicate data filter"},
{"clear", "[uuids/rssi/pathloss/transport/duplicate-data]", "Clears discovery filter"},
},
"gatt": {
{"list-attributes", "[dev]", "List attributes"},
{"select-attribute", "<attribute/UUID>", "Select attribute"},
{"attribute-info", "[attribute/UUID]", "Attribute info"},
{"read", "", "Read attribute value"},
{"write", "<data=xx xx ...>", "Write attribute value"},
{"acquire-write", "", "Acquire Write file descriptor"},
{"release-write", "", "Release Write file descriptor"},
{"acquire-notify", "", "Acquire Notify file descriptor"},
{"release-notify", "", "Release Notify file descriptor"},
{"notify", "<on/off>", "Notify attribute value"},
{"register-application", "[UUID ...]", "Register profile to connect"},
{"unregister-application", "", "Unregister profile"},
{"register-service", "<UUID>", "Register application service"},
{"unregister-service", "<UUID/object>", "Unregister application service"},
{"register-characteristic", "<UUID> <Flags=read,write,notify...>", "Register application characteristic"},
{"unregister-characteristic", "<UUID/object>", "Unregister application characteristic"},
{"register-descriptor", "<UUID> <Flags=read,write...>", "Register application descriptor"},
{"unregister-descriptor", "<UUID/object>", "Unregister application descriptor"},
},
}
commonCmds = []command{
{"version", "", "Print version"},
{"exit", "", "Exit"},
{"quit", "", "Quit"},
}
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
cmd := getCmd("bluetoothctl")
stdin, err := cmd.StdinPipe()
if err != nil {
return fmt.Errorf("stdin pipe: %v", err)
}
// run bluetoothctl in a separate thread
// TODO: track whether bluetoothctl is still alive
errc := make(chan error)
go func() {
errc <- runCmd(cmd)
}()
// TODO: pass tabs and arrows as well
s := bufio.NewScanner(os.Stdin)
menu := "main"
for s.Scan() {
l := s.Text()
if isCmd(l, "quit") || isCmd(l, "exit") {
break
}
// ignore submenu control commands
if isCmd(l, "menu") || isCmd(l, "back") {
_, _ = stdin.Write([]byte("\n"))
continue
}
// print help for all menus
if isCmd(l, "help") {
printHelp()
_, _ = stdin.Write([]byte("\n"))
continue
}
menu = jumpToSubMenu(stdin, menu, l)
_, _ = stdin.Write([]byte(l + "\n"))
}
// close stdin to make bluetoothctl stop
_ = stdin.Close()
return <-errc
}
func getCmd(name string) *exec.Cmd {
cmd := exec.Command(name, os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func runCmd(cmd *exec.Cmd) error {
if err := cmd.Run(); err != nil {
return fmt.Errorf("run: %v", err)
}
return nil
}
func isCmd(cline, cname string) bool {
return strings.Split(cline, " ")[0] == cname
}
func jumpToSubMenu(stdin io.WriteCloser, menu, cline string) string {
loop:
for m, cs := range menus {
for _, c := range cs {
if isCmd(cline, c.name) {
if menu != m {
if menu != "main" {
// back to main menu from submenus
_, _ = stdin.Write([]byte("back\n"))
menu = "main"
}
if m != "main" {
// from main menu to submenus
stdin.Write([]byte("menu " + m + "\n"))
}
}
menu = m
break loop
}
}
}
return menu
}
func printHelp() {
pl := func(cmd *command) {
fmt.Fprintf(os.Stderr, " - %-30s %-50s %s\n", cmd.name, cmd.params, cmd.desc)
}
for m, cs := range menus {
fmt.Fprintln(os.Stderr, "---------------")
fmt.Fprintln(os.Stderr, m+":")
for _, c := range cs {
pl(&c)
}
}
fmt.Fprintln(os.Stderr, "---------------")
fmt.Fprintln(os.Stderr, "common:")
for _, c := range commonCmds {
pl(&c)
}
}