Compare commits

...
Author SHA1 Message Date
gronod 3224558ac4 feat(telemetry): implement USB, Thunderbolt, and PCI peripherals provider (fixes #20)
MacMonitor CI/CD Pipeline / Build & Test (Intel x86_64) (push) Canceled after 0s
2026-09-08 12:48:30 +01:00
5 changed files with 392 additions and 0 deletions
+18
View File
@@ -12,10 +12,12 @@
1879A3DCF6E305D7F2CD472E /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2345555FF9780B02A90CC7A9 /* CoreFoundation.framework */; }; 1879A3DCF6E305D7F2CD472E /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2345555FF9780B02A90CC7A9 /* CoreFoundation.framework */; };
1B15F48F6B19E9EA4556ECD5 /* MMStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78325870C7CD8312AECA2236 /* MMStorageTests.swift */; }; 1B15F48F6B19E9EA4556ECD5 /* MMStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78325870C7CD8312AECA2236 /* MMStorageTests.swift */; };
1B3F1C8ABF0ADFAA290FF6F6 /* MMSMCParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */; }; 1B3F1C8ABF0ADFAA290FF6F6 /* MMSMCParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 375AA781A2AA0A20B119C7B7 /* MMSMCParser.m */; };
22CA7351A745F7837E487051 /* MMPeripheralsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1486E1512594482380CB492 /* MMPeripheralsTests.swift */; };
2C0F7AB93691ED8AA86FBCF0 /* MMProcessDetailInspector.m in Sources */ = {isa = PBXBuildFile; fileRef = 52E641F8A664D456518EDBA4 /* MMProcessDetailInspector.m */; }; 2C0F7AB93691ED8AA86FBCF0 /* MMProcessDetailInspector.m in Sources */ = {isa = PBXBuildFile; fileRef = 52E641F8A664D456518EDBA4 /* MMProcessDetailInspector.m */; };
3BA6709B727B1AAC6C4310B5 /* MMProcessDetailsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7187632FF63B3B0D07FE9194 /* MMProcessDetailsTests.swift */; }; 3BA6709B727B1AAC6C4310B5 /* MMProcessDetailsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7187632FF63B3B0D07FE9194 /* MMProcessDetailsTests.swift */; };
3C0AC14F803C813287D8A663 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 007F48A5A599AB4E8D216D16 /* IOKit.framework */; }; 3C0AC14F803C813287D8A663 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 007F48A5A599AB4E8D216D16 /* IOKit.framework */; };
4419FE7554489B23976E2970 /* MMCPULoadProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = D68FC8100D08DB6202F46339 /* MMCPULoadProvider.m */; }; 4419FE7554489B23976E2970 /* MMCPULoadProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = D68FC8100D08DB6202F46339 /* MMCPULoadProvider.m */; };
4752C49841375B88194F7635 /* MMPeripheralsProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 2E6A10A13A49E22E51862895 /* MMPeripheralsProvider.m */; };
4C1A92F4DEF4C824976115C2 /* MMNetworkSocketsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 374B1BF631CC4738CB52CA3B /* MMNetworkSocketsTests.swift */; }; 4C1A92F4DEF4C824976115C2 /* MMNetworkSocketsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 374B1BF631CC4738CB52CA3B /* MMNetworkSocketsTests.swift */; };
57BA02AE1CE24DBD02BD03A4 /* MMComponentThermalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B288C7402E2C00E75F8CD016 /* MMComponentThermalTests.swift */; }; 57BA02AE1CE24DBD02BD03A4 /* MMComponentThermalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B288C7402E2C00E75F8CD016 /* MMComponentThermalTests.swift */; };
5D31906EA986E2BA92E86FA8 /* MMNetworkBandwidthTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27C612287333D757C92A076A /* MMNetworkBandwidthTests.swift */; }; 5D31906EA986E2BA92E86FA8 /* MMNetworkBandwidthTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27C612287333D757C92A076A /* MMNetworkBandwidthTests.swift */; };
@@ -75,6 +77,7 @@
27C612287333D757C92A076A /* MMNetworkBandwidthTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMNetworkBandwidthTests.swift; sourceTree = "<group>"; }; 27C612287333D757C92A076A /* MMNetworkBandwidthTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMNetworkBandwidthTests.swift; sourceTree = "<group>"; };
2C55E977DBFBE7FF04621608 /* MMLoadAverageProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMLoadAverageProvider.h; sourceTree = "<group>"; }; 2C55E977DBFBE7FF04621608 /* MMLoadAverageProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMLoadAverageProvider.h; sourceTree = "<group>"; };
2DC6E55C9D3F646A1C62133D /* MMBatteryTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMBatteryTelemetryProvider.h; sourceTree = "<group>"; }; 2DC6E55C9D3F646A1C62133D /* MMBatteryTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMBatteryTelemetryProvider.h; sourceTree = "<group>"; };
2E6A10A13A49E22E51862895 /* MMPeripheralsProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMPeripheralsProvider.m; sourceTree = "<group>"; };
2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacMonitorTests.swift; sourceTree = "<group>"; }; 2EC3EDFCD5C50CB352FCED87 /* MacMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacMonitorTests.swift; sourceTree = "<group>"; };
32340166F6100A08105308D3 /* MMTelemetryCoordinator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryCoordinator.h; sourceTree = "<group>"; }; 32340166F6100A08105308D3 /* MMTelemetryCoordinator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMTelemetryCoordinator.h; sourceTree = "<group>"; };
336CEE35A745F3C7E9F70F43 /* MMFanTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMFanTests.swift; sourceTree = "<group>"; }; 336CEE35A745F3C7E9F70F43 /* MMFanTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMFanTests.swift; sourceTree = "<group>"; };
@@ -115,6 +118,8 @@
C65F5835516BC2A65BCE25D2 /* MMComponentThermalProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMComponentThermalProvider.m; sourceTree = "<group>"; }; C65F5835516BC2A65BCE25D2 /* MMComponentThermalProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMComponentThermalProvider.m; sourceTree = "<group>"; };
C9ED8F75E082D4F8AC2FF2F5 /* MMFanTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMFanTelemetryProvider.m; sourceTree = "<group>"; }; C9ED8F75E082D4F8AC2FF2F5 /* MMFanTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMFanTelemetryProvider.m; sourceTree = "<group>"; };
CBA91906CA8B7936CC82A36B /* MMPowerTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMPowerTelemetryProvider.h; sourceTree = "<group>"; }; CBA91906CA8B7936CC82A36B /* MMPowerTelemetryProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMPowerTelemetryProvider.h; sourceTree = "<group>"; };
CDE2777944B8F12EC7D2EAB5 /* MMPeripheralsProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MMPeripheralsProvider.h; sourceTree = "<group>"; };
D1486E1512594482380CB492 /* MMPeripheralsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMPeripheralsTests.swift; sourceTree = "<group>"; };
D48698681374BD25B980C000 /* MMPowerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMPowerTests.swift; sourceTree = "<group>"; }; D48698681374BD25B980C000 /* MMPowerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MMPowerTests.swift; sourceTree = "<group>"; };
D61AB99733A4AA52459BA7C4 /* MMGPUTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMGPUTelemetryProvider.m; sourceTree = "<group>"; }; D61AB99733A4AA52459BA7C4 /* MMGPUTelemetryProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMGPUTelemetryProvider.m; sourceTree = "<group>"; };
D68FC8100D08DB6202F46339 /* MMCPULoadProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMCPULoadProvider.m; sourceTree = "<group>"; }; D68FC8100D08DB6202F46339 /* MMCPULoadProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MMCPULoadProvider.m; sourceTree = "<group>"; };
@@ -193,6 +198,15 @@
path = Core; path = Core;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
65251E6B5694734BAAF1C1F1 /* Peripherals */ = {
isa = PBXGroup;
children = (
CDE2777944B8F12EC7D2EAB5 /* MMPeripheralsProvider.h */,
2E6A10A13A49E22E51862895 /* MMPeripheralsProvider.m */,
);
path = Peripherals;
sourceTree = "<group>";
};
78383CED205BCA772701AE48 /* CPU */ = { 78383CED205BCA772701AE48 /* CPU */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@@ -233,6 +247,7 @@
CA2F30473D63EC64CBD65437 /* Kernel */, CA2F30473D63EC64CBD65437 /* Kernel */,
EE2F5EFC9DC33CEDA79A131F /* Memory */, EE2F5EFC9DC33CEDA79A131F /* Memory */,
901D54FEAB6E9D6E787E599D /* Network */, 901D54FEAB6E9D6E787E599D /* Network */,
65251E6B5694734BAAF1C1F1 /* Peripherals */,
8B31CE7F9319FEC373E6CDC6 /* Power */, 8B31CE7F9319FEC373E6CDC6 /* Power */,
8719744C366432DD7C2B9644 /* Process */, 8719744C366432DD7C2B9644 /* Process */,
B6EB3F6545276C40212C2992 /* Storage */, B6EB3F6545276C40212C2992 /* Storage */,
@@ -315,6 +330,7 @@
099DCF7EF03A58ADA40A94A1 /* MMMemoryTests.swift */, 099DCF7EF03A58ADA40A94A1 /* MMMemoryTests.swift */,
27C612287333D757C92A076A /* MMNetworkBandwidthTests.swift */, 27C612287333D757C92A076A /* MMNetworkBandwidthTests.swift */,
374B1BF631CC4738CB52CA3B /* MMNetworkSocketsTests.swift */, 374B1BF631CC4738CB52CA3B /* MMNetworkSocketsTests.swift */,
D1486E1512594482380CB492 /* MMPeripheralsTests.swift */,
D48698681374BD25B980C000 /* MMPowerTests.swift */, D48698681374BD25B980C000 /* MMPowerTests.swift */,
7187632FF63B3B0D07FE9194 /* MMProcessDetailsTests.swift */, 7187632FF63B3B0D07FE9194 /* MMProcessDetailsTests.swift */,
52471E871E5CC1E444318070 /* MMProcessTests.swift */, 52471E871E5CC1E444318070 /* MMProcessTests.swift */,
@@ -470,6 +486,7 @@
6201FD599FED061F6C736E8B /* MMMemoryTests.swift in Sources */, 6201FD599FED061F6C736E8B /* MMMemoryTests.swift in Sources */,
5D31906EA986E2BA92E86FA8 /* MMNetworkBandwidthTests.swift in Sources */, 5D31906EA986E2BA92E86FA8 /* MMNetworkBandwidthTests.swift in Sources */,
4C1A92F4DEF4C824976115C2 /* MMNetworkSocketsTests.swift in Sources */, 4C1A92F4DEF4C824976115C2 /* MMNetworkSocketsTests.swift in Sources */,
22CA7351A745F7837E487051 /* MMPeripheralsTests.swift in Sources */,
9B5C651AD2AF7DE2738F0C44 /* MMPowerTests.swift in Sources */, 9B5C651AD2AF7DE2738F0C44 /* MMPowerTests.swift in Sources */,
3BA6709B727B1AAC6C4310B5 /* MMProcessDetailsTests.swift in Sources */, 3BA6709B727B1AAC6C4310B5 /* MMProcessDetailsTests.swift in Sources */,
7C9AE2ADAED52E593B52F3AE /* MMProcessTests.swift in Sources */, 7C9AE2ADAED52E593B52F3AE /* MMProcessTests.swift in Sources */,
@@ -497,6 +514,7 @@
B9C2D6574E808B517BA34C83 /* MMMemoryTelemetryProvider.m in Sources */, B9C2D6574E808B517BA34C83 /* MMMemoryTelemetryProvider.m in Sources */,
E3CCE93A7D4D1EB0187D3E52 /* MMNetworkBandwidthProvider.m in Sources */, E3CCE93A7D4D1EB0187D3E52 /* MMNetworkBandwidthProvider.m in Sources */,
A599E974E56F0B8D94A7E480 /* MMNetworkSocketsProvider.m in Sources */, A599E974E56F0B8D94A7E480 /* MMNetworkSocketsProvider.m in Sources */,
4752C49841375B88194F7635 /* MMPeripheralsProvider.m in Sources */,
0A7117B3B7CC7112F2527574 /* MMPowerTelemetryProvider.m in Sources */, 0A7117B3B7CC7112F2527574 /* MMPowerTelemetryProvider.m in Sources */,
2C0F7AB93691ED8AA86FBCF0 /* MMProcessDetailInspector.m in Sources */, 2C0F7AB93691ED8AA86FBCF0 /* MMProcessDetailInspector.m in Sources */,
93EF83FE087C6DEAAE3EC0CC /* MMProcessTelemetryProvider.m in Sources */, 93EF83FE087C6DEAAE3EC0CC /* MMProcessTelemetryProvider.m in Sources */,
@@ -28,5 +28,6 @@
#import "MMProcessDetailInspector.h" #import "MMProcessDetailInspector.h"
#import "MMGPUTelemetryProvider.h" #import "MMGPUTelemetryProvider.h"
#import "MMBatteryTelemetryProvider.h" #import "MMBatteryTelemetryProvider.h"
#import "MMPeripheralsProvider.h"
#endif /* MacMonitor_Bridging_Header_h */ #endif /* MacMonitor_Bridging_Header_h */
@@ -0,0 +1,45 @@
//
// MMPeripheralsProvider.h
// MacMonitor
//
// Telemetry provider enumerating connected USB, Thunderbolt, and PCI peripheral devices via IOKit.
//
#import <Foundation/Foundation.h>
#import "MMTelemetryProvider.h"
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, MMPeripheralBusType) {
MMPeripheralBusTypeUSB,
MMPeripheralBusTypeThunderbolt,
MMPeripheralBusTypePCI
};
@interface MMPeripheralDevice : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) MMPeripheralBusType busType;
@property (nonatomic, copy) NSString *vendorName;
@property (nonatomic, assign) uint32_t vendorID;
@property (nonatomic, assign) uint32_t productID;
@property (nonatomic, copy, nullable) NSString *serialNumber;
@property (nonatomic, copy, nullable) NSString *deviceClass;
@property (nonatomic, assign) uint64_t locationID;
@property (nonatomic, assign) BOOL isBuiltIn;
- (NSDictionary<NSString *, id> *)toDictionary;
@end
@interface MMPeripheralsProvider : NSObject <MMTelemetryProvider>
- (instancetype)init;
+ (NSArray<MMPeripheralDevice *> *)sampleUSBDevices;
+ (NSArray<MMPeripheralDevice *> *)sampleThunderboltDevices;
+ (NSArray<MMPeripheralDevice *> *)samplePCIDevices;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,255 @@
//
// MMPeripheralsProvider.m
// MacMonitor
//
// Telemetry provider enumerating connected USB, Thunderbolt, and PCI peripheral devices via IOKit.
//
#import "MMPeripheralsProvider.h"
#import <IOKit/IOKitLib.h>
@implementation MMPeripheralDevice
- (NSDictionary<NSString *, id> *)toDictionary {
NSString *busStr = @"USB";
if (self.busType == MMPeripheralBusTypeThunderbolt) {
busStr = @"Thunderbolt";
} else if (self.busType == MMPeripheralBusTypePCI) {
busStr = @"PCI";
}
return @{
@"name": self.name ?: @"Unknown Device",
@"busType": busStr,
@"vendorName": self.vendorName ?: @"Unknown",
@"vendorID": @(self.vendorID),
@"productID": @(self.productID),
@"serialNumber": self.serialNumber ?: @"",
@"deviceClass": self.deviceClass ?: @"",
@"locationID": @(self.locationID),
@"isBuiltIn": @(self.isBuiltIn)
};
}
@end
@implementation MMPeripheralsProvider
- (instancetype)init {
self = [super init];
return self;
}
- (MMTelemetryDomain)domain {
return MMTelemetryDomainPeripherals;
}
- (NSString *)providerIdentifier {
return @"com.i3omb.macmonitor.telemetry.peripherals";
}
- (BOOL)isAvailable {
return YES;
}
+ (NSArray<MMPeripheralDevice *> *)sampleUSBDevices {
NSMutableArray<MMPeripheralDevice *> *devices = [NSMutableArray array];
// Query both IOUSBHostDevice (macOS modern) and IOUSBDevice (legacy/fallback)
const char *classes[] = { "IOUSBHostDevice", "IOUSBDevice" };
NSMutableSet<NSNumber *> *seenRegistryIDs = [NSMutableSet set];
for (int i = 0; i < 2; i++) {
CFMutableDictionaryRef matching = IOServiceMatching(classes[i]);
if (!matching) continue;
io_iterator_t iterator = IO_OBJECT_NULL;
kern_return_t kr = IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iterator);
if (kr != KERN_SUCCESS || iterator == IO_OBJECT_NULL) continue;
io_service_t service = IO_OBJECT_NULL;
while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
uint64_t regID = 0;
IORegistryEntryGetRegistryEntryID(service, &regID);
if ([seenRegistryIDs containsObject:@(regID)]) {
IOObjectRelease(service);
continue;
}
[seenRegistryIDs addObject:@(regID)];
CFMutableDictionaryRef props = NULL;
if (IORegistryEntryCreateCFProperties(service, &props, kCFAllocatorDefault, 0) == KERN_SUCCESS && props) {
NSDictionary *dict = (__bridge NSDictionary *)props;
NSString *name = dict[@"USB Product Name"] ?: (dict[@"kUSBProductString"] ?: dict[@"IOName"]);
if (!name || name.length == 0) {
name = @"USB Device";
}
NSString *vendor = dict[@"USB Vendor Name"] ?: (dict[@"kUSBVendorString"] ?: @"");
uint32_t vendorID = [dict[@"idVendor"] unsignedIntValue];
uint32_t productID = [dict[@"idProduct"] unsignedIntValue];
NSString *serial = dict[@"USB Serial Number"] ?: dict[@"kUSBSerialNumberString"];
uint64_t locID = [dict[@"locationID"] unsignedLongLongValue];
BOOL builtIn = NO;
if (dict[@"Built-In"]) {
builtIn = [dict[@"Built-In"] boolValue];
} else if ([dict[@"non-removable"] isKindOfClass:[NSString class]]) {
builtIn = [dict[@"non-removable"] isEqualToString:@"yes"];
}
MMPeripheralDevice *dev = [[MMPeripheralDevice alloc] init];
dev.name = name;
dev.busType = MMPeripheralBusTypeUSB;
dev.vendorName = vendor;
dev.vendorID = vendorID;
dev.productID = productID;
dev.serialNumber = serial;
dev.locationID = locID;
dev.isBuiltIn = builtIn;
[devices addObject:dev];
CFRelease(props);
}
IOObjectRelease(service);
}
IOObjectRelease(iterator);
}
return devices;
}
+ (NSArray<MMPeripheralDevice *> *)sampleThunderboltDevices {
NSMutableArray<MMPeripheralDevice *> *devices = [NSMutableArray array];
CFMutableDictionaryRef matching = IOServiceMatching("IOThunderboltDevice");
if (!matching) return devices;
io_iterator_t iterator = IO_OBJECT_NULL;
kern_return_t kr = IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iterator);
if (kr != KERN_SUCCESS || iterator == IO_OBJECT_NULL) return devices;
io_service_t service = IO_OBJECT_NULL;
while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
CFMutableDictionaryRef props = NULL;
if (IORegistryEntryCreateCFProperties(service, &props, kCFAllocatorDefault, 0) == KERN_SUCCESS && props) {
NSDictionary *dict = (__bridge NSDictionary *)props;
NSString *name = dict[@"Device Name"] ?: dict[@"IOName"];
if (!name) name = @"Thunderbolt Device";
NSString *vendor = dict[@"Vendor Name"] ?: @"";
uint32_t vendorID = [dict[@"Vendor ID"] unsignedIntValue];
uint32_t productID = [dict[@"Device ID"] unsignedIntValue];
MMPeripheralDevice *dev = [[MMPeripheralDevice alloc] init];
dev.name = name;
dev.busType = MMPeripheralBusTypeThunderbolt;
dev.vendorName = vendor;
dev.vendorID = vendorID;
dev.productID = productID;
dev.isBuiltIn = NO;
[devices addObject:dev];
CFRelease(props);
}
IOObjectRelease(service);
}
IOObjectRelease(iterator);
return devices;
}
static uint32_t ParseDataUInt32(NSData *data) {
if (![data isKindOfClass:[NSData class]] || data.length == 0) return 0;
uint32_t val = 0;
size_t copyLen = MIN(sizeof(val), data.length);
memcpy(&val, data.bytes, copyLen);
return val;
}
+ (NSArray<MMPeripheralDevice *> *)samplePCIDevices {
NSMutableArray<MMPeripheralDevice *> *devices = [NSMutableArray array];
CFMutableDictionaryRef matching = IOServiceMatching("IOPCIDevice");
if (!matching) return devices;
io_iterator_t iterator = IO_OBJECT_NULL;
kern_return_t kr = IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iterator);
if (kr != KERN_SUCCESS || iterator == IO_OBJECT_NULL) return devices;
io_service_t service = IO_OBJECT_NULL;
while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
CFMutableDictionaryRef props = NULL;
if (IORegistryEntryCreateCFProperties(service, &props, kCFAllocatorDefault, 0) == KERN_SUCCESS && props) {
NSDictionary *dict = (__bridge NSDictionary *)props;
NSString *name = nil;
if ([dict[@"model"] isKindOfClass:[NSData class]]) {
name = [[NSString alloc] initWithData:dict[@"model"] encoding:NSUTF8StringEncoding];
name = [name stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
}
if (!name || name.length == 0) {
name = dict[@"IOName"];
}
if (!name || name.length == 0) {
name = @"PCI Device";
}
uint32_t vendorID = 0;
if ([dict[@"vendor-id"] isKindOfClass:[NSData class]]) {
vendorID = ParseDataUInt32(dict[@"vendor-id"]);
}
uint32_t deviceID = 0;
if ([dict[@"device-id"] isKindOfClass:[NSData class]]) {
deviceID = ParseDataUInt32(dict[@"device-id"]);
}
MMPeripheralDevice *dev = [[MMPeripheralDevice alloc] init];
dev.name = name;
dev.busType = MMPeripheralBusTypePCI;
dev.vendorID = vendorID;
dev.productID = deviceID;
dev.isBuiltIn = YES;
[devices addObject:dev];
CFRelease(props);
}
IOObjectRelease(service);
}
IOObjectRelease(iterator);
return devices;
}
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
NSArray<MMPeripheralDevice *> *usb = [MMPeripheralsProvider sampleUSBDevices];
NSArray<MMPeripheralDevice *> *tb = [MMPeripheralsProvider sampleThunderboltDevices];
NSArray<MMPeripheralDevice *> *pci = [MMPeripheralsProvider samplePCIDevices];
NSMutableArray<NSDictionary<NSString *, id> *> *usbList = [NSMutableArray arrayWithCapacity:usb.count];
for (MMPeripheralDevice *d in usb) {
[usbList addObject:[d toDictionary]];
}
NSMutableArray<NSDictionary<NSString *, id> *> *tbList = [NSMutableArray arrayWithCapacity:tb.count];
for (MMPeripheralDevice *d in tb) {
[tbList addObject:[d toDictionary]];
}
NSMutableArray<NSDictionary<NSString *, id> *> *pciList = [NSMutableArray arrayWithCapacity:pci.count];
for (MMPeripheralDevice *d in pci) {
[pciList addObject:[d toDictionary]];
}
NSUInteger totalCount = usb.count + tb.count + pci.count;
return @{
@"totalDeviceCount": @(totalCount),
@"usbDevices": usbList,
@"thunderboltDevices": tbList,
@"pciDevices": pciList
};
}
@end
+73
View File
@@ -0,0 +1,73 @@
//
// MMPeripheralsTests.swift
// MacMonitorTests
//
// Unit and integration tests for MMPeripheralsProvider.
//
import XCTest
@testable import MacMonitor
final class MMPeripheralsTests: XCTestCase {
var provider: MMPeripheralsProvider!
override func setUp() {
super.setUp()
provider = MMPeripheralsProvider()
}
override func tearDown() {
provider = nil
super.tearDown()
}
func testDomainAndIdentifier() {
XCTAssertEqual(provider.domain, .peripherals)
XCTAssertEqual(provider.providerIdentifier, "com.i3omb.macmonitor.telemetry.peripherals")
XCTAssertTrue(provider.isAvailable)
}
func testSamplePeripheralsLive() {
do {
let sample = try provider.sampleTelemetry()
XCTAssertNotNil(sample)
let total = sample["totalDeviceCount"] as? Int ?? 0
XCTAssertGreaterThan(total, 0, "Host system should have at least 1 PCI/USB device")
let pci = sample["pciDevices"] as? [[String: Any]] ?? []
XCTAssertGreaterThan(pci.count, 0, "Intel Mac should have PCI devices (host bridge, GPU, etc.)")
if let firstPCI = pci.first {
XCTAssertNotNil(firstPCI["name"])
XCTAssertEqual(firstPCI["busType"] as? String, "PCI")
}
let usb = sample["usbDevices"] as? [[String: Any]] ?? []
for dev in usb {
XCTAssertEqual(dev["busType"] as? String, "USB")
XCTAssertNotNil(dev["name"])
}
} catch {
XCTFail("sampleTelemetry threw error: \(error)")
}
}
func testPeripheralDeviceModel() {
let dev = MMPeripheralDevice()
dev.name = "Magic Trackpad"
dev.busType = .USB
dev.vendorName = "Apple Inc."
dev.vendorID = 0x05ac
dev.productID = 0x0265
dev.isBuiltIn = false
let dict = dev.toDictionary()
XCTAssertEqual(dict["name"] as? String, "Magic Trackpad")
XCTAssertEqual(dict["busType"] as? String, "USB")
XCTAssertEqual(dict["vendorName"] as? String, "Apple Inc.")
XCTAssertEqual(dict["vendorID"] as? UInt32, 0x05ac)
XCTAssertEqual(dict["productID"] as? UInt32, 0x0265)
XCTAssertEqual(dict["isBuiltIn"] as? Bool, false)
}
}