我正在制定一项要求,我需要监控Thunderbolt端口连接的变化。 (当连接或断开Thunderbolt电缆时)。
我尝试使用 IOKit框架中的 IOServiceMatching(kIOUSBInterfaceClassName),但我无法监控Thunderbolt端口的更改。
有什么办法可以实现吗?任何帮助表示赞赏。
答案 0 :(得分:1)
Thunderbolt设备(使用Thunderbolt端口的DisplayPort部分的显示器除外)是PCI设备,而不是USB,因此它们将作为IOPCIDevice
显示在IOService注册表中。然而,它们也将在Thunderbolt子树中显示为IOThunderboltPort
个对象,其中" PCI路径"属性将指示相关IOPCIDevice
的IOService路径。通过监视IOThunderboltPort
服务的外观和消失,并检查其PCI路径属性,可以避免匹配其他类型的PCI设备。
为了说明我正在谈论的内容,打开IORegistryExplorer或IOJones并热插拔Thunderbolt设备;你应该看到IOThunderboltPort(以及一堆其他类型的相关对象,例如AppleThunderboltPCIUpAdapter等)和IOPCIDevice(以及Thunderbolt总线工作的PCI2PCI桥接器)。 (或者,您可以使用ioreg
在热插拔之前和之后拍摄快照。)
总而言之,我会匹配IOThunderboltPort
服务,在没有PCI路径属性的情况下忽略任何服务,并在IO注册表中查找相应的IOPCIDevice
以获得实际的服务。设备
答案 1 :(得分:0)
最后,我想出了一种监控Thunderbolt Connection的方法。感谢苹果技术人员向我指出了正确的方向。 监视I / O注册表以查找IOEthernetInterface条目。过滤掉Thunderbolt网络相对容易(我不确定最佳选择是什么,但一个简单的选择是在父IOEthernetController的“IOModel”属性中查找“ThunderboltIP”)。这是Apple论坛上苹果公司的一位技术人员的回复。使用上面的信息,我写了一段代码,它将返回Thunderbolt端口的状态。
#include <IOKit/network/IOEthernetController.h>
- (void) monitorThunderboltConnection
{
CFMutableDictionaryRef matchingDict;
io_iterator_t iter;
io_object_t controllerService;
kern_return_t kr;
UInt8 MACAddress[kIOEthernetAddressSize];
QNInterfaceModel *interfaceModel = [[QNInterfaceModel alloc] initWithInterfaceModel];
/* set up a matching dictionary for the class */
matchingDict = IOServiceMatching(kIOEthernetInterfaceClass);
if (matchingDict == NULL)
{
NSLog(@"Failed");
return;
}
/* Now we have a dictionary, get an iterator.*/
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
if (kr == kIOReturnSuccess)
{
// Actually iterate through the found devices.
io_registry_entry_t serviceObject;
while ((serviceObject = IOIteratorNext(iter)))
{
// Put this services object into a dictionary object.
kr = IORegistryEntryGetParentEntry(serviceObject,
kIOServicePlane,
&controllerService);
if (KERN_SUCCESS != kr)
{
printf("IORegistryEntryGetParentEntry returned 0x%08x\n", kr);
}
else
{
CFMutableDictionaryRef serviceDictionary;
CFTypeRef networkType;
CFTypeRef MACAddressAsCFData;
NSNumber *linkStatus;
if (IORegistryEntryCreateCFProperties(serviceObject,
&serviceDictionary,
kCFAllocatorDefault,
kNilOptions) == kIOReturnSuccess)
{
networkType = IORegistryEntryCreateCFProperty(controllerService,
CFSTR(kIOModel),
kCFAllocatorDefault,
0);
if(networkType)
{
if (CFGetTypeID(networkType) == CFStringGetTypeID())
{
CFStringRef networkName = networkType;
interfaceModel.interfaceName = (__bridge NSString *)networkName;
}
CFRelease(networkType);
}
if([interfaceModel.interfaceName isEqualToString:@"ThunderboltIP"])
{
MACAddressAsCFData = IORegistryEntryCreateCFProperty(controllerService,
CFSTR(kIOMACAddress),
kCFAllocatorDefault,
0);
if (MACAddressAsCFData)
{
CFShow(MACAddressAsCFData); // for display purposes only; output goes to stderr
// Get the raw bytes of the MAC address from the CFData
CFDataGetBytes(MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
if (KERN_SUCCESS != kr)
{
printf("GetMACAddress returned 0x%08x\n", kr);
}
else
{
interfaceModel.macAddress = [[NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3], MACAddress[4], MACAddress[5]] uppercaseString];
}
CFRelease(MACAddressAsCFData);
}
linkStatus = (__bridge NSNumber *)(IORegistryEntryCreateCFProperty(controllerService,
CFSTR(kIOLinkStatus),
kCFAllocatorDefault,
0));
if (linkStatus)
{
NSLog(@"%@", [linkStatus stringValue]);
if([linkStatus integerValue] == 3) // Thunderbolt IP is Connnected
{
interfaceModel.connectedStatus = YES;
}
else
{
interfaceModel.connectedStatus = NO;
}
}
CFStringRef bsdName = ( CFStringRef ) IORegistryEntrySearchCFProperty (controllerService,
kIOServicePlane,
CFSTR ( kIOBSDNameKey ),
kCFAllocatorDefault,
kIORegistryIterateRecursively);
interfaceModel.interfaceName = (__bridge NSString *) bsdName;
if(interfaceModel.connectedStatus == YES)
{
NSLog(@"Connected");
}
else
{
NSLog(@"DisConnected");
}
}
// Failed to create a service dictionary, release and go on.
IOObjectRelease(serviceObject);
// Done with the parent Ethernet controller object so we release it.
(void) IOObjectRelease(controllerService);
continue;
}
}
}
}
/* Done, release the iterator */
IOObjectRelease(iter);
}
注意:我使用接口模型来收集所有的雷电信息,如硬件地址,BSD名称,链接状态等。您还需要为项目添加I / O Kit框架。