Post

Replies

Boosts

Views

Activity

Unable to connect to any HID device using Core HID
Hello, I am currently working on a USB HID-class device and I wanted to test communications between various OSes and the device. I was able to communicate through standard USB with the device on other OSes such as Windows and Linux, through their integrated kernel modules and generic HID drivers. As a last test, I wanted to test macOS as well. This is my code, running in a Swift-based command line utility: import Foundation import CoreHID let matchingCriteria = HIDDeviceManager.DeviceMatchingCriteria(vendorID: 0x1234, productID: 0x0006) // This is the VID/PID combination that the device is actually listed under let manager = HIDDeviceManager() for try await notification in await manager.monitorNotifications(matchingCriteria: [matchingCriteria]) { switch notification { case .deviceMatched(let deviceReference): print("Device Matched!") guard let client = HIDDeviceClient(deviceReference: deviceReference) else { fatalError("Unable to create client. Exiting.") // crash on purpose } let report = try await client.dispatchGetReportRequest(type: .input) print("Get report data: [\(report.map { String(format: "%02x", $0) }.joined(separator: " "))]") case .deviceRemoved(_): print("A device was removed.") default: continue } } The client.dispatchGetReportRequest(...) line always fails, and if I turn the try expression into a force-unwrapped one (try!) then the code, unsurprisingly, crashes. The line raises a CoreHID.HIDDeviceError.unknown() exception with a seemingly meaningless IOReturn code (last time I tried I got an IOReturn code with the value of -536870211). The first instinct is to blame my own custom USB device for not working properly, but it doesn't cooperate with with ANY USB device currently connected: not a keyboard (with permissions granted), not a controller, nothing. I did make sure to enable USB device access in the entitlements (when I tried to run this code in a simple Cocoa app) as well. ...What am I doing wrong here? What does the IOReturn code mean? Thanks in advance for anybody willing to help out!
1
0
496
Feb ’25
init(from decoder: Decoder) not running when decoding an array of classes
Let's say we have 2 decodable structs in Swift 5.5, and one of them has a property that contains an array of one of the structs: struct MySecondDecodable: Decodable { public let mySecondProperty: String private enum CodingKeys: String, CodingKey { case mySecondProperty } init(from decoder: Decoder) { let values = try decoder.container(keyedBy: CodingKeys.self) mySecondProperty = try values.decode(String.self, forKey: .mySecondProperty) } } struct MyDecodable: Decodable { public let myProperty: [MySecondDecodable] private enum CodingKeys: String, CodingKey { case myProperty } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) myProperty = try values.decode([MySecondDecodable].self, forKey: .myProperty) } } I cannot breakpoint on MySecondDecodable's init function. In fact, I cannot even see it's running. Why is this happening? Why can't I breakpoint the initialization of every single MySecondDecodable in the array? Is there a way to do such a thing?
4
0
3.0k
Feb ’22
Unable to connect to any HID device using Core HID
Hello, I am currently working on a USB HID-class device and I wanted to test communications between various OSes and the device. I was able to communicate through standard USB with the device on other OSes such as Windows and Linux, through their integrated kernel modules and generic HID drivers. As a last test, I wanted to test macOS as well. This is my code, running in a Swift-based command line utility: import Foundation import CoreHID let matchingCriteria = HIDDeviceManager.DeviceMatchingCriteria(vendorID: 0x1234, productID: 0x0006) // This is the VID/PID combination that the device is actually listed under let manager = HIDDeviceManager() for try await notification in await manager.monitorNotifications(matchingCriteria: [matchingCriteria]) { switch notification { case .deviceMatched(let deviceReference): print("Device Matched!") guard let client = HIDDeviceClient(deviceReference: deviceReference) else { fatalError("Unable to create client. Exiting.") // crash on purpose } let report = try await client.dispatchGetReportRequest(type: .input) print("Get report data: [\(report.map { String(format: "%02x", $0) }.joined(separator: " "))]") case .deviceRemoved(_): print("A device was removed.") default: continue } } The client.dispatchGetReportRequest(...) line always fails, and if I turn the try expression into a force-unwrapped one (try!) then the code, unsurprisingly, crashes. The line raises a CoreHID.HIDDeviceError.unknown() exception with a seemingly meaningless IOReturn code (last time I tried I got an IOReturn code with the value of -536870211). The first instinct is to blame my own custom USB device for not working properly, but it doesn't cooperate with with ANY USB device currently connected: not a keyboard (with permissions granted), not a controller, nothing. I did make sure to enable USB device access in the entitlements (when I tried to run this code in a simple Cocoa app) as well. ...What am I doing wrong here? What does the IOReturn code mean? Thanks in advance for anybody willing to help out!
Replies
1
Boosts
0
Views
496
Activity
Feb ’25
init(from decoder: Decoder) not running when decoding an array of classes
Let's say we have 2 decodable structs in Swift 5.5, and one of them has a property that contains an array of one of the structs: struct MySecondDecodable: Decodable { public let mySecondProperty: String private enum CodingKeys: String, CodingKey { case mySecondProperty } init(from decoder: Decoder) { let values = try decoder.container(keyedBy: CodingKeys.self) mySecondProperty = try values.decode(String.self, forKey: .mySecondProperty) } } struct MyDecodable: Decodable { public let myProperty: [MySecondDecodable] private enum CodingKeys: String, CodingKey { case myProperty } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) myProperty = try values.decode([MySecondDecodable].self, forKey: .myProperty) } } I cannot breakpoint on MySecondDecodable's init function. In fact, I cannot even see it's running. Why is this happening? Why can't I breakpoint the initialization of every single MySecondDecodable in the array? Is there a way to do such a thing?
Replies
4
Boosts
0
Views
3.0k
Activity
Feb ’22