// // HIDExample_CoreHID.swift // HIDExample_CoreHID // // Created by Edgars Klepers on 2/28/25. // // This example demonstrates CoreHID throwing an unknown error when trying to get a specific report. // Required Hardware: A Logitech G600 Gaming Mouse (https://support.logi.com/hc/en-ca/articles/360023465173-Logitech-G600-MMO-Gaming-Mouse-Technical-Specifications) // The purpose is to detect the macro buttons labeled G9-G20 on the side. import Foundation import CoreHID @main struct HIDExample_CoreHID { static func main() async throws { let DeviceVendorId: UInt32 = 0x046D // Logitech let DeviceProductId: UInt32 = 0xC24A // Gaming Mouse G600 let deviceUsages = [ HIDUsage(page: 0xFF80, usage: 0x80) ] let deviceManager = HIDDeviceManager() let matchingCriteria : HIDDeviceManager.DeviceMatchingCriteria = .init(primaryUsage: HIDUsage(page: 0x01, usage: 0x06) , deviceUsages:deviceUsages, vendorID: DeviceVendorId, productID: DeviceProductId) let reportId = HIDReportID(rawValue: 0x80) var deviceRef: HIDDeviceClient.DeviceReference? = nil for try await notification in await deviceManager.monitorNotifications(matchingCriteria: [matchingCriteria]) { switch notification { case .deviceMatched(let dr): if deviceRef != nil {continue} deviceRef = dr let deviceClient = HIDDeviceClient(deviceReference: deviceRef!) let deviceName = await deviceClient!.product! print("DeviceName: \(deviceName)") let elemToMon = await deviceClient?.elements.filter({ ele in return ele.usage.page == 0xFF80 && ele.usage.usage == 0x80 }) assert(!(elemToMon!.isEmpty)) // make sure that this is the correct device. // ***** This errors with CoreHid.HIDDeviceError.unknown(0xE0005000) ***** let report = try await deviceClient!.dispatchGetReportRequest(type: .input, id: reportId) assert(!report.isEmpty) // ***** This also errors with CoreHid.HIDDeviceError.unknown(0xE0005000) ***** let request = HIDDeviceClient.RequestElementUpdate(elements: elemToMon!) let results = await deviceClient!.updateElements([request]) let rval = try results[request]?.get() // <----- error is thrown here print ("Report \(String(describing: rval))") default: continue } } } }