-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
200 lines (168 loc) · 6.41 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright © 2019 Marcus Mengs
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/mame82/munifying/unifying"
"github.com/mame82/munifying/cmd"
)
func main() {
cmd.Execute()
}
func mainold() {
enterPairing := true
unpairAll := true
usb, err := unifying.NewLocalUSBDongle()
if err != nil {
panic(err)
}
defer usb.Close()
usb.SetShowInOut(false)
//usb.GetDeviceActivityCounters()
set,err := usb.GetSetInfo()
if err == nil {
fmt.Println(set.String())
set.StoreAutoname()
}
//usb.PrintInfoForAllConnectedDevices()
if unpairAll {
for i:=byte(1);i<7;i++ {
usb.Unpair(i)
}
}
//Pair new device
if enterPairing {
deviceNumber := byte(0x01) //According to specs: Same value as device index transmitted in 0x41 notification, but we haven't tx'ed anything
openLockTimeout := byte(60)
pe := usb.EnablePairing(openLockTimeout, deviceNumber,true)
fmt.Printf("Pairing mode exitted: %v\n", pe)
}
return
// Memdump using undocumented vendor command
// Test mem dump
startAddr := uint16(0x0000)
endAddr := uint16(0xf000)
linebreakCount := 20
linecount := 0
for addr := startAddr; addr < endAddr; addr++ {
dB,eDb := usb.DumpFlashByte(addr)
byteStr := "xx"
if eDb == nil {
byteStr = fmt.Sprintf("%02x", dB)
}
if linecount == 0 {
fmt.Printf("%#04x: ", addr)
}
linecount++
fmt.Print(byteStr)
if linecount == linebreakCount {
fmt.Println()
linecount = 0
}
}
responses, err := usb.HIDPP_SendAndCollectResponses(0xff, unifying.HIDPP_MSG_ID_GET_REGISTER_REQ, []byte{byte(0xd4), 0x00,0xff})
for _, r := range responses {
fmt.Println(r.String())
}
/*
// Request dongle firmware version from firmware register
fmt.Println("!!!Request dongle firmware version from firmware register")
responses, err := usb.HIDPP_SendAndCollectResponses(0xff, HIDPP_MSG_ID_GET_REGISTER_REQ, []byte{byte(DONGLE_HIDPP_REGISTER_FIRMWARE_INFO), 0x01, 0x00})
for _, r := range responses {
fmt.Println(r.String())
}
fmt.Println("!!!Request dongle firmware version from firmware register\n")
// Request dongle firmware build version from firmware register
fmt.Println("!!!Request dongle firmware build version from firmware register")
responses, err = usb.HIDPP_SendAndCollectResponses(0xff, HIDPP_MSG_ID_GET_REGISTER_REQ, []byte{byte(DONGLE_HIDPP_REGISTER_FIRMWARE_INFO), 0x02, 0x00})
for _, r := range responses {
fmt.Println(r.String())
}
fmt.Println("!!!Request dongle firmware build version from firmware register\n")
// Request dongle bootloader version from firmware register
fmt.Println("!!!Request dongle bootloader version from firmware register")
responses, err = usb.HIDPP_SendAndCollectResponses(0xff, HIDPP_MSG_ID_GET_REGISTER_REQ, []byte{byte(DONGLE_HIDPP_REGISTER_FIRMWARE_INFO), 0x04, 0x00})
for _, r := range responses {
fmt.Println(r.String())
}
fmt.Println("!!!Request dongle bootloader version from firmware register\n")
*/
/*
//Enable wireless notifications (to be able to receive infos via device connect notify on new devices)
fmt.Println("!!!Enable wireless notifications")
responses, err = usb.HIDPP_SendAndCollectResponses(0xff, HIDPP_MSG_ID_SET_REGISTER_REQ, []byte{byte(DONGLE_HIDPP_REGISTER_WIRELESS_NOTIFICATIONS), 0x00, 0x01})
for _, r := range responses {
fmt.Println(r.String())
}
fmt.Println("!!!END enable wireless notifications\n")
fmt.Println("!!!Get connected device info")
responses, err = usb.HIDPP_SendAndCollectResponses(0xff, HIDPP_MSG_ID_SET_REGISTER_REQ, []byte{byte(DONGLE_HIDPP_REGISTER_CONNECTION_STATE), 0x02})
for _, r := range responses {
fmt.Println(r.String())
}
fmt.Println("!!!END get connected device info\n")
*/
//Following part is only for firmware hot-patched with illegal HID command for memdump
//Test dump mem
usb.SetShowInOut(false)
for pos := 0x8000; pos < 0x8400; pos += 0x10 {
memType := byte(0x01)
addrH := byte((pos & 0xff00) >> 8)
addrL := byte(pos & 0xff)
//fmt.Printf("Reading MemType: %#02x from %#02x%02x\n", memType, addrH, addrL)
responses, err = usb.HIDPP_SendAndCollectResponses(0xff, unifying.HIDPP_MSG_ID_GET_LONG_REGISTER_REQ, []byte{byte(unifying.DONGLE_HIDPP_REGISTER_PAIRING_INFORMATION), memType + 0x80, addrH, addrL})
for _, r := range responses {
if r.IsHIDPP() {
rpp := r.(*unifying.HidPPMsg)
if rpp.MsgSubID == unifying.HIDPP_MSG_ID_GET_LONG_REGISTER_RSP && rpp.Parameters[0] == byte(unifying.DONGLE_HIDPP_REGISTER_PAIRING_INFORMATION) {
fmt.Printf("MemType %#02x addr %#02x%02x: % 02x\n", memType, addrH, addrL, rpp.Parameters[1:])
}
}
//fmt.Println(r.String())
}
//fmt.Printf("Done reading MemType: %#02x from %#02x%02x\n", memType, addrH, addrL)
}
usb.SetShowInOut(true)
usb.SetShowInOut(false)
fmt.Println("Code")
for pos := 0x6000; pos < 0x7400; pos += 0x10 {
memType := byte(0x02)
addrH := byte((pos & 0xff00) >> 8)
addrL := byte(pos & 0xff)
//fmt.Printf("Reading MemType: %#02x from %#02x%02x\n", memType, addrH, addrL)
responses, err = usb.HIDPP_SendAndCollectResponses(0xff, unifying.HIDPP_MSG_ID_GET_LONG_REGISTER_REQ, []byte{byte(unifying.DONGLE_HIDPP_REGISTER_PAIRING_INFORMATION), memType + 0x80, addrH, addrL})
for _, r := range responses {
if r.IsHIDPP() {
rpp := r.(*unifying.HidPPMsg)
if rpp.MsgSubID == unifying.HIDPP_MSG_ID_GET_LONG_REGISTER_RSP && rpp.Parameters[0] == byte(unifying.DONGLE_HIDPP_REGISTER_PAIRING_INFORMATION) {
fmt.Printf("MemType %#02x addr %#02x%02x: % 02x\n", memType, addrH, addrL, rpp.Parameters[1:])
}
}
//fmt.Println(r.String())
}
//fmt.Printf("Done reading MemType: %#02x from %#02x%02x\n", memType, addrH, addrL)
}
usb.SetShowInOut(true)
//return
//End test
//Parse successive input reports in endless loop
fmt.Println("!!!!Parse successive input reports in endless loop...")
for {
rspUSB, err := usb.ReceiveUSBReport(500)
if err == nil {
fmt.Println(rspUSB.String())
}
}
}