Skip to content

Commit 44e2b98

Browse files
lujjjhclaude
andcommitted
Treat the Unifying receiver device count as a paired count
Register 0x02 on a Unifying receiver reports every paired device, including offline ones. Reading it as a connected count meant receiver discovery could never complete while a paired device was offline, so it retried for as long as the app ran. Lightspeed receivers keep the connected-count interpretation. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 20c5dbc commit 44e2b98

3 files changed

Lines changed: 85 additions & 5 deletions

File tree

‎LinearMouse/Device/ReceiverMonitor.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ struct ReceiverSlotStateStore {
497497
}
498498
}
499499

500-
// Bolt needs presence evidence for each actual paired slot, including
501-
// offline devices. Classic/Lightspeed retain their connected-count check.
500+
// Bolt and Unifying need presence evidence for each actual paired slot,
501+
// including offline devices. Lightspeed retains its connected-count check.
502502
let inventoryComplete = discovery.hasCompletePresence
503503
&& slotKinds.count == connectedSlots.count
504504
&& !hasUnresolvedConnectedSlot

‎LinearMouse/Device/VendorSpecific/Logitech/HIDPP/LogitechHIDPPDeviceMetadataProvider.swift‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,22 @@ struct LogitechHIDPPDeviceMetadataProvider: VendorSpecificDeviceMetadataProvider
352352
return nil
353353
}
354354

355+
/// Interprets the device count a receiver reports in register 0x02.
356+
///
357+
/// A Unifying receiver counts every paired device, including offline ones: with four pairings
358+
/// and one device online it reports 4. Treating that as a connected count never matches while
359+
/// any paired device is offline. Lightspeed receivers keep the connected-count interpretation.
360+
static func classicReceiverDeviceCount(
361+
_ count: Int,
362+
vendorID: Int?,
363+
productID: Int?,
364+
transport: String?
365+
) -> ReceiverDeviceCount {
366+
receiverProtocolFamily(vendorID: vendorID, productID: productID, transport: transport) == .classic
367+
? .paired(count)
368+
: .connected(count)
369+
}
370+
355371
static func supportsClassicReceiverMonitoring(vendorID: Int?, productID: Int?, transport: String?) -> Bool {
356372
receiverProtocolFamily(vendorID: vendorID, productID: productID, transport: transport) == .classic
357373
}
@@ -2188,12 +2204,20 @@ final class LogitechReceiverChannel: VendorSpecificDeviceContext, HIDPPCancellab
21882204
return .init(
21892205
slots: pairedSlots,
21902206
connectionSnapshots: connectionSnapshots,
2191-
expectedDeviceCount: connectedDeviceCount
2192-
.map(LogitechHIDPPDeviceMetadataProvider.ReceiverDeviceCount.connected),
2207+
expectedDeviceCount: connectedDeviceCount.map(receiverDeviceCount),
21932208
inventoryAvailable: true
21942209
)
21952210
}
21962211

2212+
private func receiverDeviceCount(_ count: Int) -> LogitechHIDPPDeviceMetadataProvider.ReceiverDeviceCount {
2213+
LogitechHIDPPDeviceMetadataProvider.classicReceiverDeviceCount(
2214+
count,
2215+
vendorID: vendorID,
2216+
productID: productID,
2217+
transport: transport
2218+
)
2219+
}
2220+
21972221
func connectedDeviceCount(
21982222
deadline: Date? = nil,
21992223
until shouldContinue: @escaping () -> Bool = { true }
@@ -2328,7 +2352,7 @@ final class LogitechReceiverChannel: VendorSpecificDeviceContext, HIDPPCancellab
23282352
}
23292353

23302354
var collector = LogitechHIDPPDeviceMetadataProvider.ReceiverConnectionSnapshotCollector(
2331-
expectedDeviceCount: expectedCount.map(LogitechHIDPPDeviceMetadataProvider.ReceiverDeviceCount.connected)
2355+
expectedDeviceCount: expectedCount.map(receiverDeviceCount)
23322356
)
23332357
let deadline = Date().addingTimeInterval(0.5)
23342358
while shouldContinue(), Date() < deadline {

‎LinearMouseUnitTests/Device/VendorSpecificDeviceMetadataTests.swift‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,62 @@ final class VendorSpecificDeviceMetadataTests: XCTestCase {
620620
XCTAssertEqual(store.currentPublishedIdentities(), [online])
621621
}
622622

623+
func testUnifyingReceiverReportsPairedDeviceCount() {
624+
let usb = PointerDeviceTransportName.usb
625+
for productID in [0xC52B, 0xC532] {
626+
XCTAssertEqual(
627+
LogitechHIDPPDeviceMetadataProvider.classicReceiverDeviceCount(
628+
4, vendorID: 0x046D, productID: productID, transport: usb
629+
),
630+
.paired(4)
631+
)
632+
}
633+
XCTAssertEqual(
634+
LogitechHIDPPDeviceMetadataProvider.classicReceiverDeviceCount(
635+
1, vendorID: 0x046D, productID: 0xC547, transport: usb
636+
),
637+
.connected(1)
638+
)
639+
}
640+
641+
func testUnifyingReceiverInventoryCompletesWithOfflinePairedDevices() {
642+
// Four pairings with only slot 4 online: the receiver reports 4.
643+
let offlineMouseA = receiverIdentity(slot: 1, name: "Offline Mouse A")
644+
let offlineMouseB = receiverIdentity(slot: 2, name: "Offline Mouse B")
645+
let online = receiverIdentity(slot: 4, name: "Online Mouse")
646+
var store = ReceiverSlotStateStore()
647+
let result = store.mergeDiscovery(.init(
648+
identities: [offlineMouseA, offlineMouseB, online],
649+
connectionSnapshots: [
650+
1: .init(isConnected: false, kind: 0x02),
651+
2: .init(isConnected: false, kind: 0x02),
652+
3: .init(isConnected: false, kind: 0x01),
653+
4: .init(isConnected: true, kind: 0x02)
654+
],
655+
liveReachableSlots: [4],
656+
expectedDeviceCount: LogitechHIDPPDeviceMetadataProvider.classicReceiverDeviceCount(
657+
4, vendorID: 0x046D, productID: 0xC52B, transport: PointerDeviceTransportName.usb
658+
),
659+
observedSlotKinds: [1: 0x02, 2: 0x02, 3: 0x01, 4: 0x02]
660+
))
661+
XCTAssertTrue(result.inventoryComplete)
662+
XCTAssertEqual(store.currentPublishedIdentities(), [online])
663+
664+
// A single pairing whose device is offline: the receiver still reports 1.
665+
var offlineOnlyStore = ReceiverSlotStateStore()
666+
let offlineOnly = offlineOnlyStore.mergeDiscovery(.init(
667+
identities: [offlineMouseA],
668+
connectionSnapshots: [1: .init(isConnected: false, kind: 0x02)],
669+
liveReachableSlots: [],
670+
expectedDeviceCount: LogitechHIDPPDeviceMetadataProvider.classicReceiverDeviceCount(
671+
1, vendorID: 0x046D, productID: 0xC52B, transport: PointerDeviceTransportName.usb
672+
),
673+
observedSlotKinds: [1: 0x02]
674+
))
675+
XCTAssertTrue(offlineOnly.inventoryComplete)
676+
XCTAssertTrue(offlineOnlyStore.currentPublishedIdentities().isEmpty)
677+
}
678+
623679
func testBoltReceiverDiscoveryEnablesNotificationsAndTriggersOneInitialSnapshot() throws {
624680
let device = MockVendorSpecificDeviceContext(
625681
vendorID: 0x046D,

0 commit comments

Comments
 (0)