-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (148 loc) · 3.51 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
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
package main
import (
"errors"
"log"
"net/http"
"syscall"
"vitrhid/bluez"
"vitrhid/growcastle"
"vitrhid/mgmt"
"golang.org/x/sys/unix"
)
var (
controlListenFd int
interruptListenFd int
)
func l2capListen(psm uint16) (int, error) {
fd, err := unix.Socket(syscall.AF_BLUETOOTH, syscall.SOCK_SEQPACKET, unix.BTPROTO_L2CAP)
if err != nil {
return -1, err
}
if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
return -1, err
}
if err := unix.Bind(fd, &unix.SockaddrL2{
PSM: psm,
//Addr: [6]byte{0xB8, 0x27, 0xEB, 0xCC, 0x85, 0x7A},
}); err != nil {
return -1, err
}
if err := unix.Listen(fd, 5); err != nil {
return -1, err
}
return fd, nil
}
func initLowLevelBluetooth() (uint16, error) {
ll := mgmt.NewBluetoothLowLevel()
if err := ll.Connect(); err != nil {
return 0, err
}
list, err := ll.ReadControllerIndexList()
if err != nil {
return 0, err
}
if len(list.Controllers) == 0 {
return 0, errors.New("no controller")
}
index := list.Controllers[0]
if _, err := ll.SetPowered(index, mgmt.On); err != nil {
return 0, err
}
log.Printf("Bluetooth Powered On")
if _, err := ll.SetConnectable(index, mgmt.On); err != nil {
return 0, err
}
log.Printf("Bluetooth Connectable On")
if err := ll.SetLocalName(index, "AnonymousCheat", "AC"); err != nil {
return 0, err
}
log.Printf("Bluetooth SetLocalName")
if _, err := ll.SetSecureSimplePairing(index, mgmt.On); err != nil {
return 0, err
}
log.Printf("Bluetooth Set Secure Simple Pairing")
if _, err := ll.SetDiscoverable(index, mgmt.On, 0x500); err != nil {
return 0, err
}
log.Printf("Bluetooth Set Discovereable")
if _, err := ll.SetDeviceClass(index, 5, 64); err != nil {
return 0, err
}
log.Printf("Bluetooth Set Device Class")
if err := ll.SetAppearance(index, 0x03C0); err != nil {
return 0, err
}
log.Printf("Bluetooth Set Appearance")
return index, nil
}
func initBluez(index uint16) error {
am, err := bluez.NewAgentManager()
if err != nil {
return err
}
_, err = bluez.NewSimpleAgent(growcastle.AgentPath)
if err != nil {
return err
}
err = am.RegisterAgent(growcastle.AgentPath, bluez.AgentCapabilityDisplayOnly)
if err != nil {
return err
}
err = am.RequestDefaultAgent(growcastle.AgentPath)
if err != nil {
return err
}
pm, err := bluez.NewProfileManager()
if err != nil {
return err
}
_, err = growcastle.NewProfile()
if err != nil {
return err
}
var descriptor [][]byte
descriptor = append(descriptor, growcastle.MouseDescriptor())
record, err := growcastle.SDPRecord(descriptor)
if err != nil {
return err
}
service := "00001124-0000-1000-8000-00805f9b34fb"
opts := make(map[string]interface{})
opts["Name"] = "GrowCastle"
opts["Role"] = "server"
opts["AutoConnect"] = true
opts["ServiceRecord"] = record
err = pm.RegisterProfile(
growcastle.ProfilePath,
service, // HumanInterfaceDeviceServiceClass
opts,
)
if err != nil {
return err
}
return nil
}
func main() {
var err error
controlListenFd, err = l2capListen(0x11)
if err != nil {
log.Fatalf("l2cap: listen control")
}
interruptListenFd, err = l2capListen(0x13)
if err != nil {
log.Fatalf("l2cap: listen interrupt")
}
index, err := initLowLevelBluetooth()
if err != nil {
log.Fatalf("bluetooth: %s\n", err)
}
if err := initBluez(index); err != nil {
log.Fatalf("bluez: %s\n", err)
}
s := NewServices()
go s.AcceptControl()
go s.AcceptInterrupt()
if err := http.ListenAndServe(":8080", s); err != nil {
log.Fatalf("http: %s\n", err)
}
}