Write Unit ID Example

This example uses the Transaction method for writing cookies. This is method must be used to set multiple cookies simultaneously. First the element must be added using TransactionAddElement, then the value for the element set using SetTransactionCookie, then SetTransactionCommit once all the cookies are set to actually commit the transaction. Finally the cookies that were set must be cleared again in order for the next write to complete properly otherwise these values will stick forever, even if unplugged and plugged again. After zeroing the values do a SetTransactionCommit and remove the elements using TransactionRemoveElement.

In the following example:
_unitIdCookieforWrite=141 for Desktop, Pro, Stick, Controller and Editor
_unitIdCookieforWrite=197 for Matrix, Footpedal and Switch Interface

Functions referenced are:
unsigned long HIDDevice::SetTransactionCookieValue( IOHIDElementCookie inCookie,SInt32 value)
{
    IOHIDEventStruct hidstruct = {kIOHIDElementTypeOutput};
    pRecElement pElem = GetCookieMapping(inCookie);
    hidstruct.type = (IOHIDElementType) pElem->type;
    hidstruct.elementCookie = inCookie; hidstruct.value = value;
    return HIDTransactionSetElementValue(mDevice,pElem,&hidstruct;);
}
unsigned long HIDDevice::SetTransactionCommit()
{
    return HIDTransactionCommit(mDevice);
}
unsigned long HIDDevice::TransactionAddElement(IOHIDElementCookie inCookie)
{
    pRecElement pElem = GetCookieMapping(inCookie);
    return HIDTransactionAddElement(mDevice,pElem);
}
unsigned long HIDDevice::TransactionRemoveElement(IOHIDElementCookie inCookie)
{
    pRecElement pElem = GetCookieMapping(inCookie);
    return HIDTransactionRemoveElement(mDevice,pElem);
}
//this function returns element associated with a cookie pRecElement HIDDevice::GetCookieMapping( IOHIDElementCookie inCookie )
{
    if( mCookieToElementMap == NULL ) return NULL;
    return (pRecElement) CFDictionaryGetValue( mCookieToElementMap, inCookie );
}

Start of Example

void HIDDevice::SetUnitId(UInt8 value)
{
    unsigned int err;
    SInt16 i;
    SInt16 current;

    for ( i = 0 ; i <= 7 ; i++)
       err = TransactionAddElement( (IOHIDElementCookie) ( _unitIdCookieforWrite + i) ) ;
    //Set cookies for Unit ID
    for ( i = 0 ; i <= 7 ; i++)
       err = SetTransactionCookieValue((IOHIDElementCookie)( _unitIdCookieforWrite + i ), ( value >> i ) & 0x01 ) ;

    current = _unitIdCookieforWrite - 48; //set cookie 93, 96,100, 101, 104 and 108(or 149, 152, 156,     157, 160, 164 for matrix, fp, si) to 1
    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    //no set byte 9=16, cookie 153 (or 209 for matrix, fp or si) to 1
    current = _unitIdCookieforWrite + 8; // 149 - onwards in case of first five devices     err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,1);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);
    current++;

    err=TransactionAddElement( (IOHIDElementCookie) (current)) ;
    err=SetTransactionCookieValue((IOHIDElementCookie) current,0);

    //write the data
    err = SetTransactionCommit();

    //make them zero again
    current = _unitIdCookieforWrite - 48;
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);     err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);

    //byte 9=16
    current = _unitIdCookieforWrite + 8; // 149 - onwards in case of first five devices     err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);
    err=SetTransactionCookieValue((IOHIDElementCookie) current++,0);

    err = SetTransactionCommit();

    current = _unitIdCookieforWrite - 48;
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);     err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);     err=TransactionRemoveElement((IOHIDElementCookie) current++);

    current = _unitIdCookieforWrite + 8; // 149 - onwards in case of first five devices
    //byte 9=16
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);
    err=TransactionRemoveElement((IOHIDElementCookie) current++);

    for ( i = 0 ; i <= 7 ; i++)
       err = TransactionRemoveElement( (IOHIDElementCookie) ( _unitIdCookieforWrite + i) ) ;

    Delay(100,0); //give some time to device

}

Back to Contents