-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
111 lines (87 loc) · 2.68 KB
/
resource.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
package devcon
import (
"regexp"
"strings"
)
var reResource = regexp.MustCompile(`(?P<Name>.*)\s*: (?P<Value>.*)`)
// Resource represents an assignable and addressable bus paths, such as DMA
// channels, I/O ports, IRQ, and memory addresses.
type Resource struct {
// Name is the name of the resource (e.g. IO or IRQ).
Name string `json:"name"`
// Value is the value of the resource (e.g. "ffa0-ffaf" or 14).
Value string `json:"value"`
}
// DeviceResourceUsage describes the resources that a device is currently using.
type DeviceResourceUsage struct {
// Device is the device associated with the resource usages.
Device Device `json:"device"`
// Resources are the resources currently being used by the device.
Resources []Resource `json:"resources"`
}
// Resources returns the resources allocated to the specified devices. Resources
// are assignable and addressable bus paths, such as DMA channels, I/O ports,
// IRQ, and memory addresses.
//
// Running with the WithRemoteComputer() option is allowed.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-resources for more information.
func (dc *DevCon) Resources() ([]DeviceResourceUsage, error) {
lines, err := dc.run(commandResources)
if err != nil {
return nil, err
}
groupIndices := make([]int, 0)
for index, line := range lines {
if !strings.HasPrefix(line, " ") {
groupIndices = append(groupIndices, index)
}
}
groupIndices = append(groupIndices, len(lines))
resourceUsages := make([]DeviceResourceUsage, 0)
for index, groupStart := range groupIndices {
nextIndex := index + 1
if len(groupIndices) == nextIndex {
break
}
groupEnd := groupIndices[nextIndex]
resourceUsage := DeviceResourceUsage{
Device: Device{
ID: "",
Name: "",
},
Resources: make([]Resource, 0),
}
for lineIndex := groupStart; lineIndex < groupEnd; lineIndex++ {
line := trimSpaces(lines[lineIndex])
switch {
case lineIndex == groupStart:
resourceUsage.Device.ID = line
case lineIndex == groupStart+1:
nameParams := parseParams(reName, line)
if name, ok := nameParams["Name"]; ok {
resourceUsage.Device.Name = name
}
default:
params := parseParams(reResource, line)
resource := Resource{
Name: "",
Value: "",
}
if name, ok := params["Name"]; ok {
resource.Name = trimSpaces(name)
}
if value, ok := params["Value"]; ok {
resource.Value = trimSpaces(value)
}
if resource.Name != "" {
resourceUsage.Resources = append(resourceUsage.Resources, resource)
}
}
}
if resourceUsage.Device.Name != "" {
resourceUsages = append(resourceUsages, resourceUsage)
}
}
return resourceUsages, nil
}