#include "IOAVLibUtil.h"
#include <AssertMacros.h>
static bool RegistryPathStringContainsPlane(CFStringRef path)
{
CFRange range = CFStringFind(path, CFSTR(":"), 0);
return ( range.location != kCFNotFound ) && ( ( range.location + range.length ) < CFStringGetLength(path) );
}
static CFStringRef CreateAbsoluteRegistryPathFromString(CFStringRef path, const io_name_t defaultPlane)
{
if ( ! RegistryPathStringContainsPlane(path) )
path = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s:%@"), defaultPlane, path);
else
CFRetain(path);
return path;
}
CFMutableDictionaryRef IOAVClassMatching(const char * typeName, CFStringRef rootPath, IOAVLocation location)
{
CFMutableDictionaryRef matching = NULL;
matching = IOServiceMatching(typeName);
require(matching, exit);
if ( location < kIOAVLocationCount ) {
CFStringRef locString = NULL;
locString = CFStringCreateWithCString(kCFAllocatorDefault, IOAVLocationString(location), kCFStringEncodingUTF8);
require(locString, exit);
CFDictionarySetValue(matching, CFSTR(kIOAVLocationKey), locString);
CFRelease(locString);
}
if ( rootPath ) {
CFMutableDictionaryRef parentMatching;
CFStringRef path;
path = CreateAbsoluteRegistryPathFromString(rootPath, kIODeviceTreePlane);
require(path, exit);
parentMatching = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
require_action(parentMatching, exit, CFRelease(path));
CFDictionarySetValue(parentMatching, CFSTR(kIOPathMatchKey), path);
CFRelease(path);
CFDictionarySetValue(matching, CFSTR(kIOParentMatchKey), parentMatching);
CFRelease(parentMatching);
}
exit:
return matching;
}
CFTypeRef __IOAVCopyFirstMatchingIOAVObjectOfType(const char * typeName, IOAVTypeConstructor * typeConstructor, CFStringRef rootPath, IOAVLocation location)
{
CFTypeRef object = NULL;
io_service_t service;
service = IOServiceGetMatchingService(kIOMasterPortDefault, IOAVClassMatching(typeName, rootPath, location));
require(service, exit);
object = typeConstructor(kCFAllocatorDefault, service);
exit:
if ( service )
IOObjectRelease(service);
return object;
}