2014-07-19 63 views
0

给出一个非常简单的IO驱动:沟通与驱动 - OS X

class com_osxkernel_driver_IOKitTest : public IOService 
{ 
    OSDeclareDefaultStructors(com_osxkernel_driver_IOKitTest) 

    public: 
     virtual bool init (OSDictionary* dictionary = nullptr); 
     virtual void free(void); 
     virtual IOService* probe (IOService* provider, SInt32* score); 
     virtual bool start (IOService* provider); 
     virtual void stop (IOService* provider); 
}; 

这里的PLIST:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>IOKitTest</key> 
<dict> 
    <key>CFBundleIdentifier</key> 
    <string>com.osxkernel.${PRODUCT_NAME:rfc1034identifier}</string> 
    <key>IOClass</key> 
    <string>com_osxkernel_driver_IOKitTest</string> 
    <key>IOMatchCategory</key> 
    <string>com_osxkernel_driver_IOKitTest</string> 
    <key>IOProviderClass</key> 
    <string>IOResources</string> 
    <key>IOResourceMatch</key> 
    <string>IOKit</string> 
</dict> 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>com.apple.kpi.iokit</key> 
<string>9.0.0</string> 
<key>com.apple.kpi.libkern</key> 
<string>9.0.0</string> 
</dict> 
</plist> 

的驱动程序运行完全正常,我可以使用“内核日志”和打字时看到它:

kextstat | grep -v com.apple 

问题是我不知道如何使用用户空间应用程序进行通信,如何找到它? 我知道它有什么做的字典,我尝试使用下面的代码来找到它,但没有运气(用于苹果开发者网站):

int search_driver() 
{ 
io_iterator_t iter = 0; 
io_service_t service = 0; 
kern_return_t kr; 

CFDictionaryRef matchingDict = IOServiceMatching("IOResources"); 
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter); 
if (kr != KERN_SUCCESS) { 
    printf("Nothing found. \n"); 
    return -1; 
} 
// Iterate over all matching objects. 
while ((service = IOIteratorNext(iter)) != 0) 
{ 
    CFStringRef  className; 
    io_name_t  name; 
    // List all IOUSBDevice objects, ignoring objects that subclass IOUSBDevice. 
    className = IOObjectCopyClass(service); 
    IORegistryEntryGetName(service, name); 

    printf("Found device with name: %s\n", name); 
    CFRelease(className); 
    IOObjectRelease(service); 
    // Release the iterator. 
    IOObjectRelease(iter); 
} 
return 0; 
} 

我想:

CFDictionaryRef matchingDict = IOServiceMatching("IOResources"); 
CFDictionaryRef matchingDict = IOServiceMatching("IOService"); 

和更多。

但仍然没有运气。第一个发现:IOResources 第二个发现:Macbook Air 5,2

我在做什么错了?如何找到正在运行的驱动程序并使用简单的用户空间应用程序与它进行通信?

回答

0

你的IOKitTest实现文件是什么样的?在该文件中,您需要通过调用registerService()来注册服务,以便客户端找到它。它可以在启动功能被加入,例如:

bool com_osxkernel_driver_IOKitTest::start (IOService *provider) 
{ 
    bool res = super::start(provider); 
    super::registerService(); // <---- add this 
    IOLog("IOKitTest::start\n"); 
    return res; 
} 

然后在你的search_driver()函数,你可以再看看你的确切类,而不是在IOResources小块。

matchingDict = IOServiceMatching("com_osxkernel_driver_IOKitTest"); 

这里有一些较早的文档,这些函数的例子没有registerService()调用,它不能在较新的系统上工作。让我知道这是否解决了您的问题。