Skip to content

Commit

Permalink
Merge pull request #146 from numtide/fix/usb-pci-classification
Browse files Browse the repository at this point in the history
fix: classification of pci and usb devices
  • Loading branch information
brianmcgee authored Nov 5, 2024
2 parents 042166c + f7d23ee commit fcb8731
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 0 additions & 1 deletion docs/content/getting-started/generate-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ To generate a report, you will need to have [Nix] installed on the target machin
github:numtide/nixos-facter -- -o facter.json
```


This will scan your system and produce a JSON-based report in a file named `facter.json`:

```json title="facter.json"
Expand Down
4 changes: 2 additions & 2 deletions pkg/ephem/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func TestReadSwapFile(t *testing.T) {
as.NoError(err)
as.Empty(swaps)

swaps, err = ReadSwapFile(strings.NewReader(corrupt))
_, err = ReadSwapFile(strings.NewReader(corrupt))
as.Errorf(err, "malformed entry in swaps file: '%s'", "foo bar baz")

swaps, err = ReadSwapFile(strings.NewReader(badPartition))
_, err = ReadSwapFile(strings.NewReader(badPartition))
as.Errorf(err, "malformed entry in swaps file: '%s'", `/var/lib/swap-1 foo 1048576 123 -3`)

swaps, err = ReadSwapFile(strings.NewReader(sample))
Expand Down
10 changes: 9 additions & 1 deletion pkg/facter/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,15 @@ func compareDevice(a hwinfo.HardwareDevice, b hwinfo.HardwareDevice) int {
}

func (h *Hardware) add(device hwinfo.HardwareDevice) error {
switch device.Class {
// start with the overall device class
class := device.Class

// attempt to fall back to the class list if it's unknown
if class == hwinfo.HardwareClassUnknown && len(device.ClassList) > 0 {
class = device.ClassList[0]
}

switch class {
case hwinfo.HardwareClassBios:
if h.Bios != nil {
return fmt.Errorf("bios field is already set")
Expand Down
18 changes: 15 additions & 3 deletions pkg/hwinfo/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,18 @@ const (

// HardwareDevice represents a hardware component detected in the system.
type HardwareDevice struct {
// Class represents the type of the hardware component.
Class HardwareClass `json:"-"`

// Index is a unique index provided by hwinfo, starting at 1
Index uint `json:"index"`

// AttachedTo is the index of the hardware device this is attached to
AttachedTo uint `json:"attached_to"`

// Class represents the type of the hardware component.
Class HardwareClass `json:"-"`

// Class represents a list of hardware types which matches the component.
ClassList []HardwareClass `json:"class_list,omitempty"`

// BusType represents the type of bus to which the hardware device is connected.
BusType *Id `json:"bus_type,omitempty"`

Expand Down Expand Up @@ -422,11 +425,19 @@ func NewHardwareDevice(hd *C.hd_t) (*HardwareDevice, error) {
return nil, fmt.Errorf("failed to read driver info: %w", err)
}
model := C.GoString(hd.model)

hwClass := HardwareClass(hd.hw_class)
if hwClass == HardwareClassCpu {
model = stripCpuFreq(model)
}

var hwClassList []HardwareClass
for i := HardwareClassSystem; i < HardwareClassAll; i++ {
if C.hd_is_hw_class(hd, C.hd_hw_item_t(i)) == 1 {
hwClassList = append(hwClassList, i)
}
}

result := &HardwareDevice{
Index: uint(hd.idx),
AttachedTo: uint(hd.attached_to),
Expand All @@ -443,6 +454,7 @@ func NewHardwareDevice(hd *C.hd_t) (*HardwareDevice, error) {
CompatVendor: NewId(hd.compat_vendor),
CompatDevice: NewId(hd.compat_device),
Class: hwClass,
ClassList: hwClassList,
Model: model,
SysfsId: C.GoString(hd.sysfs_id),
SysfsBusId: C.GoString(hd.sysfs_bus_id),
Expand Down

0 comments on commit fcb8731

Please sign in to comment.