Skip to content

Commit

Permalink
Handle replacing default created device's deviceId during enumeration.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed Jan 14, 2024
1 parent 7f6f195 commit c232727
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 3 additions & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ type device struct {
dr *deviceRemoval

// Mutable data, obtain lock first.
deviceId uint16
deviceId uint16
deviceIdSet bool

capabilities map[da.Capability]implcaps.ZDACapability
productData productData
}
Expand Down
23 changes: 22 additions & 1 deletion enumerate_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ func (e enumerateDevice) groupInventoryDevices(inv inventory) []inventoryDevice

func (e enumerateDevice) updateNodeTable(n *node, inventoryDevices []inventoryDevice) map[uint16]*device {
deviceIdMapping := map[uint16]*device{}
var unsetDevice []*device = nil

/* Look for devices that exist but don't have a deviceId. */
n.m.RLock()
for _, d := range n.device {
d.m.RLock()
if !d.deviceIdSet {
unsetDevice = append(unsetDevice, d)
}
d.m.RUnlock()
}
n.m.RUnlock()

/* Find existing devices that match the deviceId. */
n.m.RLock()
Expand All @@ -272,9 +284,18 @@ func (e enumerateDevice) updateNodeTable(n *node, inventoryDevices []inventoryDe
/* Create new devices for those that are missing. */
for _, i := range inventoryDevices {
if _, found := deviceIdMapping[i.deviceId]; !found {
d := e.dm.createNextDevice(n)
var d *device

if len(unsetDevice) > 0 {
d = unsetDevice[0]
unsetDevice = unsetDevice[1:]
} else {
d = e.dm.createNextDevice(n)
}

d.m.Lock()
d.deviceId = i.deviceId
d.deviceIdSet = true
d.m.Unlock()
deviceIdMapping[i.deviceId] = d
}
Expand Down
24 changes: 23 additions & 1 deletion enumerate_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,29 @@ func Test_enumerateDevice_updateNodeTable(t *testing.T) {
existingDeviceId := uint16(0x2000)

ed := enumerateDevice{logger: logwrap.New(discard.Discard()), dm: mdm}
d := &device{m: &sync.RWMutex{}, deviceId: existingDeviceId}
d := &device{m: &sync.RWMutex{}, deviceId: existingDeviceId, deviceIdSet: true}
n := &node{m: &sync.RWMutex{}, device: map[uint8]*device{0: d}}

id := []inventoryDevice{
{
deviceId: existingDeviceId,
},
}

mapping := ed.updateNodeTable(n, id)

assert.Equal(t, d, mapping[existingDeviceId])
assert.Equal(t, existingDeviceId, d.deviceId)
})

t.Run("returns an existing an existing device that has its deviceId unset", func(t *testing.T) {
mdm := &mockDeviceManager{}
defer mdm.AssertExpectations(t)

existingDeviceId := uint16(0x2000)

ed := enumerateDevice{logger: logwrap.New(discard.Discard()), dm: mdm}
d := &device{m: &sync.RWMutex{}, deviceId: 0}
n := &node{m: &sync.RWMutex{}, device: map[uint8]*device{0: d}}

id := []inventoryDevice{
Expand Down

0 comments on commit c232727

Please sign in to comment.