forked from tarm/serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum_windows.go
252 lines (226 loc) · 7.2 KB
/
enum_windows.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// +build windows
package serial
import (
"encoding/binary"
"fmt"
"runtime"
"strings"
"syscall"
"unsafe"
"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows"
)
var (
modsetupapi = windows.NewLazySystemDLL("setupapi.dll")
procSetupDiGetClassDevsExW = modsetupapi.NewProc("SetupDiGetClassDevsExW")
procSetupDiDestroyDeviceInfoList = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList")
procSetupDiEnumDeviceInfo = modsetupapi.NewProc("SetupDiEnumDeviceInfo")
procSetupDiGetDeviceRegistryPropertyW = modsetupapi.NewProc("SetupDiGetDeviceRegistryPropertyW")
)
var deviceClassPortsGUID = windows.GUID{0x4d36e978, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}
// DIGCF flags controll what is included in the device information set built by SetupDiGetClassDevs
type DIGCF uint32
const (
DIGCF_DEFAULT DIGCF = 0x00000001 // only valid with DIGCF_DEVICEINTERFACE
DIGCF_PRESENT DIGCF = 0x00000002
DIGCF_ALLCLASSES DIGCF = 0x00000004
DIGCF_PROFILE DIGCF = 0x00000008
DIGCF_DEVICEINTERFACE DIGCF = 0x00000010
)
type SPDRP uint32
const SPDRP_FRIENDLYNAME SPDRP = 0x0000000C // FriendlyName (R/W)
// DevInfo holds reference to device information set
type DevInfo windows.Handle
// DevInfoData is a device information structure (references a device instance that is a member of a device information set)
type DevInfoData struct {
size uint32
ClassGUID windows.GUID
DevInst uint32 // handle
_ uintptr
}
// Do the interface allocations only once for common Errno values
const (
errnoERROR_IO_PENDING = 997
)
var (
errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
)
// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
func errnoErr(e syscall.Errno) error {
switch e {
case 0:
return nil
case errnoERROR_IO_PENDING:
return errERROR_IO_PENDING
}
return e
}
// Close function deletes a device information set and frees all associated memory.
func (h DevInfo) Close() error {
if h != DevInfo(windows.InvalidHandle) {
return SetupDiDestroyDeviceInfoList(h)
}
return nil
}
func SetupDiDestroyDeviceInfoList(DeviceInfoSet DevInfo) (err error) {
r1, _, e1 := syscall.Syscall(procSetupDiDestroyDeviceInfoList.Addr(), 1, uintptr(DeviceInfoSet), 0, 0)
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
// SetupDiGetClassDevsEx function returns a handle to a device information set that contains requested device information elements for a local or a remote computer.
func SetupDiGetClassDevsEx(ClassGUID *windows.GUID, Enumerator string, hwndParent uintptr, Flags DIGCF, DeviceInfoSet DevInfo, MachineName string) (handle DevInfo, err error) {
var _p0 *uint16
if Enumerator != "" {
_p0, err = syscall.UTF16PtrFromString(Enumerator)
if err != nil {
return
}
}
var _p1 *uint16
if MachineName != "" {
_p1, err = syscall.UTF16PtrFromString(MachineName)
if err != nil {
return
}
}
r0, _, e1 := syscall.Syscall9(procSetupDiGetClassDevsExW.Addr(), 7, uintptr(unsafe.Pointer(ClassGUID)),
uintptr(unsafe.Pointer(_p0)), uintptr(hwndParent), uintptr(Flags), uintptr(DeviceInfoSet),
uintptr(unsafe.Pointer(_p1)), uintptr(0), 0, 0)
handle = DevInfo(r0)
if handle == DevInfo(windows.InvalidHandle) {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
// SetupDiEnumDeviceInfo function returns a SP_DEVINFO_DATA structure that specifies a device information element in a device information set.
func SetupDiEnumDeviceInfo(DeviceInfoSet DevInfo, MemberIndex int, data *DevInfoData) (err error) {
data.size = uint32(unsafe.Sizeof(*data))
r1, _, e1 := syscall.Syscall(procSetupDiEnumDeviceInfo.Addr(), 3, uintptr(DeviceInfoSet), uintptr(MemberIndex), uintptr(unsafe.Pointer(data)))
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
// SetupDiGetDeviceRegistryProperty function retrieves a specified Plug and Play device property.
func SetupDiGetDeviceRegistryProperty(deviceInfoSet DevInfo, deviceInfoData *DevInfoData, property SPDRP) (value interface{}, err error) {
reqSize := uint32(256)
for {
var dataType uint32
buf := make([]byte, reqSize)
r1, _, e1 := syscall.Syscall9(procSetupDiGetDeviceRegistryPropertyW.Addr(), 7, uintptr(deviceInfoSet),
uintptr(unsafe.Pointer(deviceInfoData)), uintptr(property), uintptr(unsafe.Pointer(&dataType)),
uintptr(unsafe.Pointer(&buf[0])), uintptr(uint32(len(buf))), uintptr(unsafe.Pointer(&reqSize)), 0, 0)
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
if err == windows.ERROR_INSUFFICIENT_BUFFER {
continue
}
if err != nil {
return
}
return getRegistryValue(buf[:reqSize], dataType)
}
}
func getRegistryValue(buf []byte, dataType uint32) (interface{}, error) {
switch dataType {
case windows.REG_SZ:
ret := windows.UTF16ToString(bufToUTF16(buf))
runtime.KeepAlive(buf)
return ret, nil
case windows.REG_EXPAND_SZ:
ret, err := registry.ExpandString(windows.UTF16ToString(bufToUTF16(buf)))
runtime.KeepAlive(buf)
return ret, err
case windows.REG_BINARY:
return buf, nil
case windows.REG_DWORD_LITTLE_ENDIAN:
return binary.LittleEndian.Uint32(buf), nil
case windows.REG_DWORD_BIG_ENDIAN:
return binary.BigEndian.Uint32(buf), nil
case windows.REG_MULTI_SZ:
bufW := bufToUTF16(buf)
a := []string{}
for i := 0; i < len(bufW); {
j := i + wcslen(bufW[i:])
if i < j {
a = append(a, windows.UTF16ToString(bufW[i:j]))
}
i = j + 1
}
runtime.KeepAlive(buf)
return a, nil
case windows.REG_QWORD_LITTLE_ENDIAN:
return binary.LittleEndian.Uint64(buf), nil
default:
return nil, fmt.Errorf("Unsupported registry value type: %v", dataType)
}
}
func wcslen(str []uint16) int {
for i := 0; i < len(str); i++ {
if str[i] == 0 {
return i
}
}
return len(str)
}
// bufToUTF16 function reinterprets []byte buffer as []uint16
func bufToUTF16(buf []byte) []uint16 {
sl := struct {
addr *uint16
len int
cap int
}{(*uint16)(unsafe.Pointer(&buf[0])), len(buf) / 2, cap(buf) / 2}
return *(*[]uint16)(unsafe.Pointer(&sl))
}
// EnumerateSerialPorts will return a list of port names and a list of descriptions
func EnumerateSerialPorts() ([]string, []string, error) {
devInfoList, err := SetupDiGetClassDevsEx(&deviceClassPortsGUID, "", 0, DIGCF_PRESENT, DevInfo(0), "")
if err != nil {
return nil, nil, fmt.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
}
var info []string
var ports []string
var data DevInfoData
for i := 0; true; i++ {
err := SetupDiEnumDeviceInfo(devInfoList, i, &data)
if err != nil {
if errWin, ok := err.(syscall.Errno); ok && errWin == 259 /*ERROR_NO_MORE_ITEMS*/ {
break
}
continue
}
value, err := SetupDiGetDeviceRegistryProperty(devInfoList, &data, SPDRP_FRIENDLYNAME)
if s, ok := value.(string); ok {
if strings.Contains(s, "COM") {
pos := strings.Index(s, "COM")
info = append(info, s[0:pos-1])
ports = append(ports, s[pos:len(s)-1])
}
}
if data.ClassGUID != deviceClassPortsGUID {
return nil, nil, fmt.Errorf("SetupDiEnumDeviceInfo returned different class GUID")
}
}
_ = devInfoList.Close()
return ports, info, nil
}