static void __deviceReportCallback(void * context, IOReturn result, void * sender, IOHIDReportType type, uint32_t reportID, uint8_t * report, CFIndex reportLength)
{
    int index;
    
    printf("IOHIDDeviceRef[%p]: result=0x%08x reportType=%s reportID=%d reportLength=%ld: ", sender, result, getReportTypeString(type), reportID, reportLength);
    for (index=0; result==kIOReturnSuccess && index<reportLength; index++)
        printf("%02x ", report[index]);
    printf("\n");
    
    // toggle a report
    if ( gSend || gSendTransaction ) {
        CFArrayRef outputElements  = NULL;
        
        outputElements = CFDictionaryGetValue(gOutputElements, sender);
        
        if ( outputElements ) {
            static uint8_t      value       = 0;
            IOHIDTransactionRef transaction = NULL;
            
            transaction = IOHIDTransactionCreate(kCFAllocatorDefault, (IOHIDDeviceRef)sender, kIOHIDTransactionDirectionTypeOutput, 0);
            if ( transaction ) {
                IOReturn    ret;
                CFIndex     index, count;
                
                for ( index=0, count = CFArrayGetCount(outputElements); index<count; index++) {
                    
                    IOHIDElementRef element     = (IOHIDElementRef)CFArrayGetValueAtIndex(outputElements, index);
                    IOHIDValueRef   hidValue    = 0;
                    
                    if ( !element )
                        continue;
                    
                    IOHIDTransactionAddElement(transaction, element);
                    
                    hidValue = IOHIDValueCreateWithIntegerValue(kCFAllocatorDefault, element, 0, value);
                    if ( !hidValue )
                        continue;
                    
                    if ( gSendTransaction ) {
                        IOHIDTransactionSetValue(transaction, element, hidValue, 0);
                    } else {
                        ret = IOHIDDeviceSetValue((IOHIDDeviceRef)sender, element, hidValue);
                        printf("Attempt to send value. Ret = 0x%08x\n", ret);
                    }
                    CFRelease(hidValue);
                    
                }
                
                if ( gSendTransaction ) {
                    ret = IOHIDTransactionCommit(transaction);
                    printf("Attempt to send transaction. Ret = 0x%08x\n", ret);
                }
                value = value+1 % 2;
                
                CFRelease(transaction);
            }
        }
    }
}
Exemple #2
0
void GPUSB_SetBit(int bit, int val)
{
    if (!pGPUSB) return;  // no device, bail
    IOHIDElementRef pCurrentHIDElement = NULL;
    //IOHIDEventStruct HID_IOEvent;
    
    int i;
    static int bitarray[8]={0,0,0,0,1,1,0,0};
    static unsigned char GPUSB_reg = 0x30;

    if (GPUSB_Model)
    { 
        // Newer models - use a single byte
        pCurrentHIDElement = GetFirstOutputElement(pGPUSB);
        if(pCurrentHIDElement == NULL)
        {
            wxMessageBox(std::string(__PRETTY_FUNCTION__) + " Null element");
            return;
        }
//      HIDTransactionAddElement(pGPUSB,pCurrentHIDElement);
        
        unsigned char bmask = 1;
        bmask = bmask << bit;
        if (val)
        {
            GPUSB_reg = GPUSB_reg | bmask;
        }
        else
        {
            GPUSB_reg = GPUSB_reg & ~bmask;
        }
        
        //HID_IOEvent.longValueSize = 0;
        //HID_IOEvent.longValue = nil;

        /*IOHIDValueRef valueToSend = IOHIDValueCreateWithBytes(
                                        kCFAllocatorDefault, 
                                        pCurrentHIDElement, 
                                        uint64_t        inTimeStamp,
                                        &GPUSB_reg,
                                        1);
        */

        //IOHIDElementCookie cookie = IOHIDElementGetCookie(pCurrentHIDElement);
        IOHIDValueRef valueToSend;
        IOReturn tIOReturn = IOHIDDeviceGetValue(pGPUSB, pCurrentHIDElement, &valueToSend);
        if(tIOReturn != kIOReturnSuccess)
        {
            CFRelease(pCurrentHIDElement);
            wxMessageBox(std::string(__PRETTY_FUNCTION__) + " Cannot retrieve value (1)");
            return;
        }
        
        assert(IOHIDValueGetLength(valueToSend) == 1);
        
        IOHIDValueRef valueToSendCopied = IOHIDValueCreateWithBytes(
                                        kCFAllocatorDefault, 
                                        pCurrentHIDElement, 
                                        IOHIDValueGetTimeStamp(valueToSend),
                                        &GPUSB_reg,
                                        1);
        
        
        tIOReturn = IOHIDDeviceSetValue(pGPUSB, pCurrentHIDElement, valueToSendCopied);
        if(tIOReturn != kIOReturnSuccess)
        {
            CFRelease(pCurrentHIDElement);
            wxMessageBox(std::string(__PRETTY_FUNCTION__) + " Cannot send value (1)");
            return;
        }

        
        
        //pGPUSB_interface->getElementValue (pGPUSB_interface, cookie, &HID_IOEvent);

        //HID_IOEvent.value = (SInt32) GPUSB_reg;
//      wxMessageBox(wxString::Format("%x - %x %x    %d %d",foo,GPUSB_reg,bmask,bit,val));
//      HID_IOEvent.type = (IOHIDElementType) pCurrentHIDElement->type;
        //HIDSetElementValue(pGPUSB,pCurrentHIDElement,&HID_IOEvent);
//      HIDTransactionCommit(pGPUSB);
        CFRelease(pCurrentHIDElement);
    }
    else {
        // Generic bit-set routine.  For older adapters, we can't send a whole
        // byte and things are setup as SInt32's per bit with 8 bits total...
        //IOHIDEventStruct hidstruct = {kIOHIDElementTypeOutput};
        
        
        IOHIDTransactionRef transaction = IOHIDTransactionCreate(
                          kCFAllocatorDefault,
                          pGPUSB,
                          kIOHIDTransactionDirectionTypeOutput, 
                          kIOHIDOptionsTypeNone);    
                          
        if(transaction == NULL)
        {
            wxMessageBox(std::string(__PRETTY_FUNCTION__) + " Cannot create a transaction");
        }
        
        
        bitarray[bit]=val;
//      std::cout << "Setting to ";
        for (i=0; i<8; i++) {  // write
//          std::cout << " " << bitarray[i];
            if (i==0)
            {
                pCurrentHIDElement = GetFirstOutputElement(pGPUSB);
            }
            else
            {
                pCurrentHIDElement = GetNextOutputElement(pGPUSB, pCurrentHIDElement);
            }
            
            IOHIDValueRef valueToSend;
            IOReturn tIOReturn = IOHIDDeviceGetValue(pGPUSB, pCurrentHIDElement, &valueToSend);
            if(tIOReturn != kIOReturnSuccess)
            {
                CFRelease(pCurrentHIDElement);
                wxMessageBox(std::string(__PRETTY_FUNCTION__) + " Cannot retrieve value (1)");
                return;
            }
            
            //CFTypeID tID = IOHIDValueGetTypeID(valueToSend);
            
            /*IOHIDValueRef valueToSendCopied = IOHIDValueCreateWithBytes(
                                                  kCFAllocatorDefault, 
                                                  pCurrentHIDElement, 
                                                  IOHIDValueGetTimeStamp(valueToSend),
                                                  &bitarray[i],
                                                  sizeof(bitarray[i]));   */
                                                  
            IOHIDValueRef valueToSendCopied = IOHIDValueCreateWithIntegerValue(
                                                  kCFAllocatorDefault,
                                                  pCurrentHIDElement,
                                                  IOHIDValueGetTimeStamp(valueToSend),
                                                  bitarray[i]);
            
            IOHIDTransactionAddElement(transaction, pCurrentHIDElement);
            IOHIDTransactionSetValue(transaction, pCurrentHIDElement, valueToSendCopied, 0);
            
            
            //HIDTransactionAddElement(pGPUSB,pCurrentHIDElement);
            //hidstruct.type = (IOHIDElementType) pCurrentHIDElement->type;
            //hidstruct.value = (SInt32) bitarray[i];
            //HIDTransactionSetElementValue(pGPUSB,pCurrentHIDElement,&hidstruct);
        }
//      std::cout << "\n";
        IOHIDTransactionCommit(transaction);
    }

//  wxMilliSleep(30);
}