Beispiel #1
0
//  send - send a packet
//    Inputs:
//	num = device to transmit to (zero based)
//	buf = buffer containing packet to send
//	len = number of bytes to transmit
//	timeout = time to wait, in milliseconds
//    Output:
//	number of bytes sent, or -1 on error
//
int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
{
    hid_t *hid;
    int result=-100;

    hid = get_hid(num);
    if (!hid || !hid->open) return -1;
#if 1
#warning "Send timeout not implemented on MACOSX"
    uint8_t *report_buf = (uint8_t *) malloc(len);
    memcpy(&report_buf[0], buf,len);
    // Note: packet processing done in OS indepdent code
    IOReturn ret = IOHIDDeviceSetReport(hid->ref, kIOHIDReportTypeOutput, 2, (uint8_t *)report_buf, len);
    result = (ret == kIOReturnSuccess) ? len : -1;
    if (err_get_system(ret) == err_get_system(sys_iokit))
    {

        // The error was in the I/O Kit system
        UInt32 codeValue = err_get_code(ret);
        qDebug("Returned: %x", codeValue);
        // Can now perform test on error code, display it to user, or whatever.
        usleep(1000000);
    }

#endif
#if 0
    // No matter what I tried this never actually sends an output
    // report and output_callback never gets called.  Why??
    // Did I miss something?  This is exactly the same params as
    // the sync call that works.  Is it an Apple bug?
    // (submitted to Apple on 22-sep-2009, problem ID 7245050)
    //
    IOHIDDeviceScheduleWithRunLoop(hid->ref, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    // should already be scheduled with run loop by attach_callback,
    // sadly this doesn't make any difference either way
    //
    IOHIDDeviceSetReportWithCallback(hid->ref, kIOHIDReportTypeOutput,
                                     0, buf, len, (double)timeout / 1000.0, output_callback, &result);
    while (1) {
        printf("enter run loop (send)\n");
        CFRunLoopRun();
        printf("leave run loop (send)\n");
        if (result > -100) break;
        if (!hid->open) {
            result = -1;
            break;
        }
    }
#endif
    return result;
}
Beispiel #2
0
//  rawhid_send - send a packet
//    Inputs:
//	num = device to transmit to (zero based)
//	buf = buffer containing packet to send
//	len = number of bytes to transmit
//	timeout = time to wait, in milliseconds
//    Output:
//	number of bytes sent, or -1 on error
//
int rawhid_send(int num, void *buf, int len, int timeout)
{
   //fprintf(stderr,"rawhid_send num: %d\n",num);
	hid_t *hid;
	int result=-100;
   
	hid = get_hid(num);
	if (!hid || !hid->open) return -1;
   //fprintf(stderr,"rawhid_send A\n");
#if 1
#warning "Send timeout not implemented on MACOSX"
	IOReturn ret = IOHIDDeviceSetReport(hid->ref, kIOHIDReportTypeOutput, 0, buf, len);
	result = (ret == kIOReturnSuccess) ? len : -1;
   //fprintf(stderr,"rawhid_send B result: %d\n",result);
#endif
#if 0
	// No matter what I tried this never actually sends an output
	// report and output_callback never gets called.  Why??
	// Did I miss something?  This is exactly the same params as
	// the sync call that works.  Is it an Apple bug?
	// (submitted to Apple on 22-sep-2009, problem ID 7245050)
	//
   fprintf(stderr,"rawhid_send C\n");
	IOHIDDeviceScheduleWithRunLoop(hid->ref, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	// should already be scheduled with run loop by attach_callback,
	// sadly this doesn't make any difference either way
	//
	IOHIDDeviceSetReportWithCallback(hid->ref, kIOHIDReportTypeOutput,
                                    0, buf, len, (double)timeout / 1000.0, output_callback, &result);
   //fprintf(stderr,"rawhid_send D\n");
	while (1) 
   {
		fprintf(stderr,"enter run loop (send)\n");
		CFRunLoopRun();
		fprintf(stderr,"leave run loop (send)\n");
		if (result > -100) break;
		if (!hid->open) 
      {
			result = -1;
			break;
		}
	}
#endif
   fprintf(stderr,"rawhid_send result: %d\n",result);
	return result;
}