Variables in class definition in .iig

I am trying to add instance variables to my DriverKit class. I declare them in the class definition in the .iig file but when i try to access them in the .cpp file i get an error "Use of undeclared identifier". Is there a limitation that prevents instance variables in the classes defined for DriverKit?

.iig file

class VirtualJoystick: public IOUserUSBHostHIDDevice
{
public:
    virtual kern_return_t
    Start(IOService * provider) override;

    virtual kern_return_t
    Stop(IOService * provider) override;

    virtual bool init() override;

    virtual void free() override;

    virtual OSDictionary * newDeviceDescription(void) override;

    virtual OSData * newReportDescriptor(void) override;

    virtual kern_return_t getReport(
                                    IOMemoryDescriptor *report,
                                    IOHIDReportType reportType,
                                    IOOptionBits options,
                                    uint32_t completionTimeout,
                                    OSAction *action
                                    ) override;
private:
    // the stored state of all the buttons
    uint32_t _buttons = 0;
};

example Usage in .cpp

bool VirtualJoystick::init() {
    this->_buttons = 0; // Use of undeclared identifier '_buttons'
}
Accepted Answer

Hi EKLynx

IIG does not permit the declaration of member variables in the class definition. Instead, inside your CPP file you declare a structure named classname_IVars.

struct VirtualJoystick_IVars {
    // the stored state of all the buttons
    uint32_t _buttons = 0;
}

In your init() implementation, you allocate an instance of this structure and assign it to the ivars member. The ivars member is generated by IIG, you don't need to declare it.

bool VirtualJoystick::init()
{
	if (!super::init()) return false;

    ivars = IONewZero(VirtualJoystick_IVars, 1);
    if (!ivars) return false;

    ...
}

And remember to free it in your free() implementation:

void VirtualJoystick::free()
{
    IOSafeDeleteNULL(ivars, VirtualJoystick_IVars, 1);
    super::free();
}

You access your ivars from a member function like so:

ivars->_buttons = 0;

That worked. Is there documentation showing what is and is not allowed in .iig files? Is the case similar with new class member functions? i.e. void setButton(uint8_t buttonToSet)?

Variables in class definition in .iig
 
 
Q