-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
298 lines (259 loc) · 9.59 KB
/
device.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package devcon
import (
"errors"
"fmt"
"strings"
)
// Device represents a device attached to the computer.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-ids for more information about Device IDs.
type Device struct {
// ID is a string reported by a device’s enumerator. A device has only one
// device ID. A device ID has the same format as a hardware ID.
ID string `json:"id"`
// Name is the name of the device.
Name string `json:"name"`
}
// DeviceRestartStatus indicates the restart status of a device when Restart()
// is called.
type DeviceRestartStatus struct {
// ID is the ID of the device being restarted.
ID string `json:"id"`
// WasRestarted indicates if the restart operation was successful.
WasRestarted bool
}
// Disable disables devices on the computer.
//
// To disable a device means that the device remains physically connected to the
// computer, but its driver is unloaded from memory and its resources are freed
// so that the device cannot be used.
//
// This will disable the device even if the device is already disabled. Before
// and after disabling a device, use Status() to verify the device status.
//
// Before using an ID pattern to disable a device, determine which devices will
// be affected. To do so, pass the pattern to the Status() function:
//
// dc.Status("USB\*")
//
// Or with the HwIDs() function:
//
// dc.HwIDs("USB\*")
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Disable()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-disable for more information.
func (dc *DevCon) Disable(idOrClass string, idsOrClasses ...string) error {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandDisable, allIdsOrClasses...)
if err != nil {
return err
}
dc.logResults(lines)
return err
}
// Enable enables devices on the computer.
//
// To enable a device means that the device driver is loaded into memory and the
// device is ready for use.
//
// This will enable the device even if it is already enabled. Before and after
// enabling a device, use Status() to verify the device status.
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Enable()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-enable for more information.
func (dc *DevCon) Enable(idOrClass string, idsOrClasses ...string) error {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandEnable, allIdsOrClasses...)
if err != nil {
return err
}
// TODO: Parse lines.
dc.logResults(lines)
return err
}
// Find returns a slice of Device instances that are currently attached to the computer.
//
// You can use Find to find devices that are not currently attached to the computer
// by specifying the full device instance ID of the device instead of a hardware
// ID or ID pattern. Specifying the full device instance ID overrides the
// restriction on the Find operation that limits it to attached devices.
//
// Calling Find with a single class argument returns the same results as the
// ListClass() method.
//
// To find all devices, including those that are not currently attached to the
// computer, use the FindAll() function.
//
// Running with the WithRemoteComputer() option is allowed.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-find for more information.
func (dc *DevCon) Find(idOrClass string, idsOrClasses ...string) ([]Device, error) {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandFind, allIdsOrClasses...)
if err != nil {
return nil, err
}
devices := make([]Device, 0)
for _, valuePair := range parseColonSeparatedLines(lines) {
device := Device{
ID: valuePair[0],
Name: valuePair[1],
}
devices = append(devices, device)
}
return devices, nil
}
// FindAll returns all devices on the computer, including devices that were once
// attached to the computer but have been detached or moved. (These are known as
// non-present devices or phantom devices.).
//
// It also returns devices that are enumerated differently as a result of a
// BIOS change.
//
// Running with the WithRemoteComputer() option is allowed.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-findall for more information.
func (dc *DevCon) FindAll(idOrClass string, idsOrClasses ...string) ([]Device, error) {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandFindAll, allIdsOrClasses...)
if err != nil {
return nil, err
}
devices := make([]Device, 0)
for _, valuePair := range parseColonSeparatedLines(lines) {
device := Device{
ID: valuePair[0],
Name: valuePair[1],
}
devices = append(devices, device)
}
return devices, nil
}
// Install creates a new, root-enumerated devnode for a non-Plug and Play device
// and installs its supporting software.
//
// The infFilePath parameter is the fully qualified path and name of the INF file
// for the driver package.
//
// The hardwareID specifies a hardware ID for the device. It must exactly match
// the hardware ID of the device. Patterns are not valid. Do not use a single
// quote character (') to indicate a literal value. For more information, see
// https://docs.microsoft.com/en-us/windows-hardware/drivers/install/hardware-ids
// and https://docs.microsoft.com/en-us/windows-hardware/drivers/install/device-identification-strings.
//
// You cannot use this function for Plug and Play devices. This operation creates
// a new non-Plug and Play device node. Then, it uses Update to install drivers
// for the newly added device. If any step of the installation operation fails,
// returns an error and does not proceed with the driver installation.
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Install()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-install for more information.
func (dc *DevCon) Install(infFilePath string, hardwareID string) error {
lines, err := dc.run(commandInstall, infFilePath, hardwareID)
if err != nil {
return err
}
if substrInLines(lines, "success") != -1 {
return errors.New("install command was not successful")
}
return nil
}
// Remove removes the device from the device tree and deletes the device stack
// for the device. As a result of these actions, child devices are removed from
// the device tree and the drivers that support the device are unloaded.
//
// This operation does not delete the device driver or any files installed for
// the device. After removing the device from the device tree, the files remain
// and the device is still represented internally as a non-present device that
// can be re-enumerated.
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Remove()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-remove for more information.
func (dc *DevCon) Remove(idsOrClasses ...string) error {
lines, err := dc.run(commandRemove, idsOrClasses...)
if err != nil {
return err
}
// TODO: Parse lines.
dc.logResults(lines)
return nil
}
// Rescan uses Windows Plug and Play features to update the device list for the
// computer.
//
// Rescanning can cause the Plug and Play manager to detect new devices and to
// install device drivers without warning.
//
// Rescanning can detect some non-Plug and Play devices, particularly those
// that cannot notify the system when they are installed, such as parallel-port
// devices and serial-port devices. As a result, you must have Administrator
// privileges to call this function.
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Rescan()
//
// Running with the WithRemoteComputer() option is allowed.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-rescan for more information.
func (dc *DevCon) Rescan() error {
lines, err := dc.run(commandRescan)
if err != nil {
return err
}
if substrInLines(lines, "completed") == -1 {
return fmt.Errorf("unable to rescan: %s", strings.Join(lines, ". "))
}
return nil
}
// Restart stops and restarts the specified devices.
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Restart()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-restart for more information.
func (dc *DevCon) Restart(idOrClass string, idsOrClasses ...string) ([]DeviceRestartStatus, error) {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandRestart, allIdsOrClasses...)
if err != nil {
return nil, err
}
statuses := make([]DeviceRestartStatus, 0)
valuePairs := parseColonSeparatedLines(lines)
for _, valuePair := range valuePairs {
statuses = append(statuses, DeviceRestartStatus{
ID: valuePair[0],
WasRestarted: valuePair[1] == "Restarted",
})
}
return statuses, nil
}