1. 程式人生 > >修改設備驅動程序屬性

修改設備驅動程序屬性

bject text 定義 red tor cfs ffffff type ict

3環:

//
//  main.c
//  DriverIterator
//

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

int main (int argc, const char * argv[])
{
    CFDictionaryRef     matchingDict = NULL;
    io_iterator_t       iter = 0;
    io_service_t        service = 0;
    kern_return_t       kr;

    matchingDict = IOServiceMatching("com_osxkernel_driver_IOKitTest");
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
        return -1;

    //叠代所有匹配的對象
    while ((service = IOIteratorNext(iter)) != 0)
    {
        IORegistryEntrySetCFProperty(service, CFSTR("StopMessage"), CFSTR("The driver has stopped"));
        IOObjectRelease(service);
    }
    IOObjectRelease(iter);

    return 0;
}

0環:
//IOKitTest.h

#include <IOKit/IOService.h>

class com_osxkernel_driver_IOKitTest : public IOService
{
    OSDeclareDefaultStructors(com_osxkernel_driver_IOKitTest)

public: 
    virtual bool    init (OSDictionary* dictionary = NULL);
    virtual void    free (void);

    virtual IOService*  probe (IOService* provider, SInt32* score);
    virtual bool    start (IOService* provider);
    virtual void    stop (IOService* provider);

    virtual IOReturn    setProperties (OSObject* properties);
};

//IOKitTest.cpp

#include "IOKitTest.h"
#include <IOKit/IOLib.h>

#define super IOService

OSDefineMetaClassAndStructors(com_osxkernel_driver_IOKitTest, IOService)

bool com_osxkernel_driver_IOKitTest::init (OSDictionary* dict)
{
    bool res = super::init(dict);
    IOLog("IOKitTest::init\n");
    return res;
}

void com_osxkernel_driver_IOKitTest::free (void)
{
    IOLog("IOKitTest::free\n");
    super::free();
}

IOService* com_osxkernel_driver_IOKitTest::probe (IOService* provider, SInt32* score)
{
    IOService *res = super::probe(provider, score);
    IOLog("IOKitTest::probe\n");
    return res;
}

bool com_osxkernel_driver_IOKitTest::start (IOService *provider)
{
    bool res = super::start(provider);
    IOLog("IOKitTest::start\n");
    return res;
}

void com_osxkernel_driver_IOKitTest::stop (IOService *provider)
{
    OSString*   stopMessage;

    //從驅動程序屬性表讀取可能自定義字符串並輸出
    stopMessage = OSDynamicCast(OSString, getProperty("StopMessage"));
    IOLog("IOKitTest::stop\n");
    if (stopMessage)
        IOLog("%s\n", stopMessage->getCStringNoCopy());

    super::stop(provider);
}

IOReturn com_osxkernel_driver_IOKitTest::setProperties (OSObject* properties)
{
    OSDictionary*   propertyDict;

    //提供的屬性對象應該是一個OSDictionary對象
    propertyDict = OSDynamicCast(OSDictionary, properties);
    if (propertyDict != NULL)
    {
        OSObject*       theValue;
        OSString*       theString;

        //從字典讀取對應的StopMessage鍵值
        theValue = propertyDict->getObject("StopMessage");
        theString = OSDynamicCast(OSString, theValue);
        if (theString != NULL)
        {
            //將該值添加到驅動程序的屬性列表
            setProperty("StopMessage", theString);
            return kIOReturnSuccess;
        }
    }

    return kIOReturnUnsupported;
}

技術分享圖片
技術分享圖片
技術分享圖片

修改設備驅動程序屬性