Mac IOKit USB信号检测

 matchingDict = IOServiceMatching(kIOUSBDeviceClassName); 

 numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorId);  
 CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID), numberRef);  
 CFRelease(numberRef);  

 numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &deviceProductId);  
 CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID), numberRef);  
 CFRelease(numberRef);  
 numberRef = NULL;  

 kr = IOServiceAddMatchingNotification(gNotifyPort,   
                                       kIOFirstMatchNotification,   
                                       matchingDict,   
                                       DeviceAdded,   
                                       NULL,  
                                       &gAddedIter);
为了在将USB设备添加到Mac PC时处理通知是可以的,但是当用户按下USB设备上的按钮时,我能获得信号吗? 谢谢大家!     
已邀请:
如果用户按下设备上的按钮,则不会创建设备通知。除非这是Apple有驱动程序的设备(在这种情况下您可能会收到输入事件,例如NSEvent),否则您必须通过其界面获取设备接口并与该设备通信。例如。 USB设备具有接口,每个接口都有端点,与这些端点的连接标记为管道。有传出和传入管道,您可以向这些管道发送数据或从这些管道读取数据。存在不同类型的管道,批量,等时和中断管道。此外,每个设备都有一个控制管道,这是您可以双向使用的唯一管道。 Apple发布了一个名为USB Prober和Xcode的工具,用它来查看你的USB设备。例如。我的Apple Cinema Display也通过USB连接,报告如下:
Full Speed device @ 3 (0xFD520000): .............................................   Composite device: "Apple Cinema HD Display"
    Port Information:   0x0019
           Captive
           External Device
           Connected
           Enabled
    Device Descriptor   
        Descriptor Version Number:   0x0110
        Device Class:   0   (Composite)
        Device Subclass:   0
        Device Protocol:   0
        Device MaxPacketSize:   8
        Device VendorID/ProductID:   0x05AC/0x9223   (Apple Inc.)
        Device Version Number:   0x0114
        Number of Configurations:   1
        Manufacturer String:   1 "Apple Computer, Inc."
        Product String:   2 "Apple Cinema HD Display"
        Serial Number String:   0 (none)
    Configuration Descriptor   
        Length (and contents):   34
            Raw Descriptor (hex)    0000: 09 02 22 00 01 01 00 E0  01 09 04 00 00 01 03 00  
            Raw Descriptor (hex)    0010: 00 00 09 21 11 01 00 01  22 37 00 07 05 81 03 08  
            Raw Descriptor (hex)    0020: 00 10 
        Number of Interfaces:   1
        Configuration Value:   1
        Attributes:   0xE0 (self-powered, remote wakeup)
        MaxPower:   2 ma
        Interface #0 - HID   
            Alternate Setting   0
            Number of Endpoints   1
            Interface Class:   3   (HID)
            Interface Subclass;   0
            Interface Protocol:   0
            HID Descriptor   
                Descriptor Version Number:   0x0111
                Country Code:   0
                Descriptor Count:   1
                Descriptor 1   
                    Type:   0x22  (Report Descriptor)
                    Length (and contents):   55
                        Raw Descriptor (hex)    0000: 05 80 09 01 A1 01 15 00  26 FF 00 75 08 85 02 96  
                        Raw Descriptor (hex)    0010: 01 01 09 02 B2 02 01 05  82 95 02 85 10 09 10 B1  
                        Raw Descriptor (hex)    0020: 02 25 04 85 D6 09 D6 B1  02 25 07 85 E7 B1 02 26  
                        Raw Descriptor (hex)    0030: FF 00 85 E4 81 02 C0 
                    Parsed Report Descriptor:   
                          Usage Page    (Monitor) 
                          Usage 1 (0x1)    
                              Collection (Application)    
                                Logical Minimum.........    (0)  
                                Logical Maximum.........    (255)  
                                Report Size.............    (8)  
                                ReportID................    (2)  
                                Report Count............    (257)  
                                Usage 2 (0x2)    
                                Feature.................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Nonvolatile, Buffered bytes) 
                                Usage Page    (VESA Virtual Controls) 
                                Report Count............    (2)  
                                ReportID................    (16)  
                                Usage 16 (0x10)    
                                Feature.................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Nonvolatile, Bitfield) 
                                Logical Maximum.........    (4)  
                                ReportID................    (214)  
                                Usage 214 (0xd6)    
                                Feature.................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Nonvolatile, Bitfield) 
                                Logical Maximum.........    (7)  
                                ReportID................    (231)  
                                Feature.................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Nonvolatile, Bitfield) 
                                Logical Maximum.........    (255)  
                                ReportID................    (228)  
                                Input...................   (Data, Variable, Absolute, No Wrap, Linear, Preferred State, No Null Position, Bitfield) 
                              End Collection     
            Endpoint 0x81 - Interrupt Input   
                Address:   0x81  (IN)
                Attributes:   0x03  (Interrupt no synchronization data endpoint)
                Max Packet Size:   8
                Polling Interval:   16 ms
该设备有一个接口,接口#0,此接口上有一个端点(0x81),它通过中断管道连接,可以每16 ms传输最大8字节的数据包。如果您现在想要与该管道进行通信,则必须获取设备的USB驱动程序接口,并且此接口提供与相关管道同步或异步通信的功能,它们与网络代码非常相似。 如果您现在问自己,这个管道最初在监视器上有什么用处,那就是监视器如何将按钮按下到系统。 Apple的Cinema Display有3个按钮,“+”和“ - ”表示亮度和电源按钮。电源按钮的功能可在系统首选项中配置,例如,如果我愿意,它可以让整个Mac进入睡眠状态。每当我点击其中一个按钮时,就会创建一个中断事件并通过此USB管道发送到系统(实际上系统必须每隔16 ms轮询一次这样的事件,但系统会为您执行此操作 - 您的代码可以假装事件被监视器推动了)。 看一下IOUSBDeviceInterface197,请注意,它继承了IOUSBDeviceInterface187,它继承了IOUSBDeviceInterface182,它再次继承了IOUSBDeviceInterface。这意味着如果您检索接口的197版本,它继承的接口的所有方法也可用于此接口(它们不会再次在页面上列出)。 Apple必须使用名称中的版本接口才能与旧代码保持兼容。 Apple随着时间的推移扩展了USB接口(例如,使用新的OS X版本)并确保旧代码仍然运行,它们总是添加新接口,否则它们会破坏现有代码。 在这里,您将获得所有这些接口的列表。是的,文档太可怕了。您必须通过try和/或示例代码学习。请注意,有DeviceInterfaces和InterfaceInterfaces;那不是一回事。 DeviceInterface是设备本身的驱动程序接口,每个设备可以有多个“USB接口”(这是USB标准的术语),并且这种“usb接口”的接口被命名为“InterfaceInterface”。简单的设备,如上面显示的监视器,只有一个接口。 你认为这是复杂的吗?好吧,来自其他平台的大多数开发人员说,一旦他们弄清楚所有这些东西是如何工作的,如果你需要原始访问USB管道,Mac OS是所有平台中最简单的。然而,情况变得更糟:每个接口都可以有多个配置,你可以在接口上设置一个可能改变管道的配置(例如,在一个配置中,管道是一个大容量管道,而在另一个配置中,同一个管道现在是同步的管)。您可以在USB Prober应用程序中看到所有这些(它显示了替代配置)。请注意,USB设备可以拥有完全不同的接口,管道和配置,以适应不同的速度(例如,一个用于USB 1.2,一个用于低速+全速,一个用于USB 2,也就是高速)。 在您开始在此页面上编写任何代码之前,Apple会尝试解释您应该了解的大多数USB基础知识。如果您单击“使用USB设备接口”,Apple甚至会为您提供大量示例代码,以便您获取DeviceInterface,您可以使用它来获取InterfaceInterfaces。 我希望我能给你一些指导和一些有趣的阅读链接。这不是你问题的答案,但这是一个好的开始。当然,你也可以编写一个内核驱动程序(内核扩展)来为你的设备添加对系统的支持,这样按下按钮会生成一个任何软件都可以处理的事件,而不必使用IOKit ...但我不知何故怀疑你想去那里。     

要回复问题请先登录注册