__private_extern__ char * mach_error_string_int(mach_error_t err, boolean_t *diag) { const struct error_system *sys_p; const struct error_subsystem *sub_p; int sub, system, code; do_compat( &err ); system = err_get_system(err); sys_p = &_mach_errors[system]; sub = err_get_sub(err); code = err_get_code(err); *diag = TRUE; if (system > err_max_system) return((char *)"(?/?) unknown error system"); else if (sys_p->map_table) sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count); if (sub >= sys_p->max_sub) return((char *)sys_p->bad_sub); sub_p = &sys_p->subsystem[sub]; if (sub_p->map_table) code = err_sparse_mapit(code, sub_p->map_table, sub_p->map_count); if (code >= sub_p->max_code) return ((char *)NO_SUCH_ERROR); *diag = mach_error_full_diag; return( (char *)sub_p->codes[code] ); }
// 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; }
static int init_hid_manager(void) { IOReturn res; /* Initialize all the HID Manager Objects */ hid_mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); IOHIDManagerSetDeviceMatching(hid_mgr, NULL); IOHIDManagerScheduleWithRunLoop(hid_mgr, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); res = IOHIDManagerOpen(hid_mgr, kIOHIDOptionsTypeNone); if(res == kIOReturnSuccess) return 0; fprintf(stderr, "IOReturn error code: 0x%x\n", err_get_code(res)); return -1; }
/* Return a string describing the errno code in ERRNUM. */ char * strerror_l (int errnum, locale_t loc) { int system; int sub; int code; const struct error_system *es; extern void __mach_error_map_compat (int *); __mach_error_map_compat (&errnum); system = err_get_system (errnum); sub = err_get_sub (errnum); code = err_get_code (errnum); if (system > err_max_system || ! __mach_error_systems[system].bad_sub) { free (last_value); if (__asprintf (&last_value, "%s%X", translate ("Error in unknown error system: ", loc), errnum) == -1) last_value = NULL; return last_value; } es = &__mach_error_systems[system]; if (sub >= es->max_sub) return (char *) translate (es->bad_sub, loc); if (code >= es->subsystem[sub].max_code) { free (last_value); if (__asprintf (&last_value, "%s%s %d", translate ("Unknown error ", loc), translate (es->subsystem[sub].subsys_name, loc), errnum) == -1) last_value = NULL; return last_value; } return (char *) translate (es->subsystem[sub].codes[code], loc); }
void printInterpretedError(char *s, IOReturn err) { // These should be defined somewhere, but I can't find them. These from Accessing hardware. #if 0 static struct{int err; char *where;} systemSources[] = { {0, "kernel"}, {1, "user space library"}, {2, "user space servers"}, {3, "old ipc errors"}, {4, "mach-ipc errors"}, {7, "distributed ipc"}, {0x3e, "user defined errors"}, {0x3f, "(compatibility) mach-ipc errors"} }; #endif UInt32 system, sub, code; fprintf(stderr, "%s (0x%08X) ", s, err); system = err_get_system(err); sub = err_get_sub(err); code = err_get_code(err); if(system == err_get_system(sys_iokit)) { if(sub == err_get_sub(sub_iokit_usb)) { fprintf(stderr, "USB error %ld(0x%lX) ", code, code); } else if(sub == err_get_sub(sub_iokit_common)) { fprintf(stderr, "IOKit common error %ld(0x%lX) ", code, code); } else { fprintf(stderr, "IOKit error %ld(0x%lX) from subsytem %ld(0x%lX) ", code, code, sub, sub); } } else { fprintf(stderr, "error %ld(0x%lX) from system %ld(0x%lX) - subsytem %ld(0x%lX) ", code, code, system, system, sub, sub); } }
/* Return a string describing the errno code in ERRNUM. */ char * _strerror_internal (int errnum, char *buf, size_t buflen) { int system; int sub; int code; const struct error_system *es; extern void __mach_error_map_compat (int *); __mach_error_map_compat (&errnum); system = err_get_system (errnum); sub = err_get_sub (errnum); code = err_get_code (errnum); if (system > err_max_system || ! __mach_error_systems[system].bad_sub) { const char *unk = _("Error in unknown error system: "); const size_t unklen = strlen (unk); char *p = buf + buflen; *--p = '\0'; p = _itoa (errnum, p, 16, 1); return memcpy (p - unklen, unk, unklen); } es = &__mach_error_systems[system]; if (sub >= es->max_sub) return (char *) es->bad_sub; if (code >= es->subsystem[sub].max_code) { const char *unk = _("Unknown error "); const size_t unklen = strlen (unk); char *p = buf + buflen; size_t len = strlen (es->subsystem[sub].subsys_name); *--p = '\0'; p = _itoa (errnum, p, 16, 1); *p-- = ' '; p = memcpy (p - len, es->subsystem[sub].subsys_name, len); return memcpy (p - unklen, unk, unklen); } return (char *) _(es->subsystem[sub].codes[code]); }
const char * mach_error_string_int(mach_error_t err, boolean_t * diag) { int sub, system, code; __mach_error_map_compat( &err ); sub = err_get_sub(err); system = err_get_system(err); code = err_get_code(err); *diag = TRUE; if (system > err_max_system) return( "(?/?) unknown error system" ); if (sub >= errors[system].max_sub) return( errors[system].bad_sub ); if (code >= errors[system].subsystem[sub].max_code) return ( NO_SUCH_ERROR ); *diag = mach_error_full_diag; return( errors[system].subsystem[sub].codes[code] ); }
void *map_physical(uint64_t phys_addr, size_t len) { kern_return_t err; #if __LP64__ mach_vm_address_t addr; mach_vm_size_t size; #else vm_address_t addr; vm_size_t size; #endif size_t dataInLen = sizeof(map_t); size_t dataOutLen = sizeof(map_t); map_t in, out; in.addr = phys_addr; in.size = len; #ifdef DEBUG printf("map_phys: phys %08lx, %08x\n", phys_addr, len); #endif #if !defined(__LP64__) && defined(WANT_OLD_API) /* Check if OSX 10.5 API is available */ if (IOConnectCallStructMethod != NULL) { #endif err = IOConnectCallStructMethod(connect, kPrepareMap, &in, dataInLen, &out, &dataOutLen); #if !defined(__LP64__) && defined(WANT_OLD_API) } else { /* Use old API */ err = IOConnectMethodStructureIStructureO(connect, kPrepareMap, dataInLen, &dataOutLen, &in, &out); } #endif if (err != KERN_SUCCESS) { printf("\nError(kPrepareMap): system 0x%x subsystem 0x%x code 0x%x ", err_get_system(err), err_get_sub(err), err_get_code(err)); printf("physical 0x%08"PRIx64"[0x%zx]\n", phys_addr, len); switch (err_get_code(err)) { case 0x2c2: printf("Invalid argument.\n"); errno = EINVAL; break; case 0x2cd: printf("Device not open.\n"); errno = ENOENT; break; } return MAP_FAILED; } err = IOConnectMapMemory(connect, 0, mach_task_self(), &addr, &size, kIOMapAnywhere | kIOMapInhibitCache); /* Now this is odd; The above connect seems to be unfinished at the * time the function returns. So wait a little bit, or the calling * program will just segfault. Bummer. Who knows a better solution? */ usleep(1000); if (err != KERN_SUCCESS) { printf("\nError(IOConnectMapMemory): system 0x%x subsystem 0x%x code 0x%x ", err_get_system(err), err_get_sub(err), err_get_code(err)); printf("physical 0x%08"PRIx64"[0x%zx]\n", phys_addr, len); switch (err_get_code(err)) { case 0x2c2: printf("Invalid argument.\n"); errno = EINVAL; break; case 0x2cd: printf("Device not open.\n"); errno = ENOENT; break; } return MAP_FAILED; } #ifdef DEBUG printf("map_phys: virt %08x, %08x\n", addr, size); #endif return (void *)addr; }
bool ErrorName (IOReturn err, char* out_buf) { bool ok=true; switch (err) { case 0: sprintf(out_buf,"ok"); break; case kIOReturnError: sprintf(out_buf,"kIOReturnError - general error"); break; case kIOReturnNoMemory: sprintf(out_buf,"kIOReturnNoMemory - can't allocate memory"); break; case kIOReturnNoResources: sprintf(out_buf,"kIOReturnNoResources - resource shortage"); break; case kIOReturnIPCError: sprintf(out_buf,"kIOReturnIPCError - error during IPC"); break; case kIOReturnNoDevice: sprintf(out_buf,"kIOReturnNoDevice - no such device"); break; case kIOReturnNotPrivileged: sprintf(out_buf,"kIOReturnNotPrivileged - privilege violation"); break; case kIOReturnBadArgument: sprintf(out_buf,"kIOReturnBadArgument - invalid argument"); break; case kIOReturnLockedRead: sprintf(out_buf,"kIOReturnLockedRead - device read locked"); break; case kIOReturnLockedWrite: sprintf(out_buf,"kIOReturnLockedWrite - device write locked"); break; case kIOReturnExclusiveAccess: sprintf(out_buf,"kIOReturnExclusiveAccess - exclusive access and device already open"); break; case kIOReturnBadMessageID: sprintf(out_buf,"kIOReturnBadMessageID - sent/received messages had different msg_id"); break; case kIOReturnUnsupported: sprintf(out_buf,"kIOReturnUnsupported - unsupported function"); break; case kIOReturnVMError: sprintf(out_buf,"kIOReturnVMError - misc. VM failure"); break; case kIOReturnInternalError: sprintf(out_buf,"kIOReturnInternalError - internal error"); break; case kIOReturnIOError: sprintf(out_buf,"kIOReturnIOError - General I/O error"); break; case kIOReturnCannotLock: sprintf(out_buf,"kIOReturnCannotLock - can't acquire lock"); break; case kIOReturnNotOpen: sprintf(out_buf,"kIOReturnNotOpen - device not open"); break; case kIOReturnNotReadable: sprintf(out_buf,"kIOReturnNotReadable - read not supported"); break; case kIOReturnNotWritable: sprintf(out_buf,"kIOReturnNotWritable - write not supported"); break; case kIOReturnNotAligned: sprintf(out_buf,"kIOReturnNotAligned - alignment error"); break; case kIOReturnBadMedia: sprintf(out_buf,"kIOReturnBadMedia - Media Error"); break; case kIOReturnStillOpen: sprintf(out_buf,"kIOReturnStillOpen - device(s) still open"); break; case kIOReturnRLDError: sprintf(out_buf,"kIOReturnRLDError - rld failure"); break; case kIOReturnDMAError: sprintf(out_buf,"kIOReturnDMAError - DMA failure"); break; case kIOReturnBusy: sprintf(out_buf,"kIOReturnBusy - Device Busy"); break; case kIOReturnTimeout: sprintf(out_buf,"kIOReturnTimeout - I/O Timeout"); break; case kIOReturnOffline: sprintf(out_buf,"kIOReturnOffline - device offline"); break; case kIOReturnNotReady: sprintf(out_buf,"kIOReturnNotReady - not ready"); break; case kIOReturnNotAttached: sprintf(out_buf,"kIOReturnNotAttached - device not attached"); break; case kIOReturnNoChannels: sprintf(out_buf,"kIOReturnNoChannels - no DMA channels left"); break; case kIOReturnNoSpace: sprintf(out_buf,"kIOReturnNoSpace - no space for data"); break; case kIOReturnPortExists: sprintf(out_buf,"kIOReturnPortExists - port already exists"); break; case kIOReturnCannotWire: sprintf(out_buf,"kIOReturnCannotWire - can't wire down physical memory"); break; case kIOReturnNoInterrupt: sprintf(out_buf,"kIOReturnNoInterrupt - no interrupt attached"); break; case kIOReturnNoFrames: sprintf(out_buf,"kIOReturnNoFrames - no DMA frames enqueued"); break; case kIOReturnMessageTooLarge: sprintf(out_buf,"kIOReturnMessageTooLarge - oversized msg received on interrupt port"); break; case kIOReturnNotPermitted: sprintf(out_buf,"kIOReturnNotPermitted - not permitted"); break; case kIOReturnNoPower: sprintf(out_buf,"kIOReturnNoPower - no power to device"); break; case kIOReturnNoMedia: sprintf(out_buf,"kIOReturnNoMedia - media not present"); break; case kIOReturnUnformattedMedia: sprintf(out_buf,"kIOReturnUnformattedMedia - media not formatted"); break; case kIOReturnUnsupportedMode: sprintf(out_buf,"kIOReturnUnsupportedMode - no such mode"); break; case kIOReturnUnderrun: sprintf(out_buf,"kIOReturnUnderrun - data underrun"); break; case kIOReturnOverrun: sprintf(out_buf,"kIOReturnOverrun - data overrun"); break; case kIOReturnDeviceError: sprintf(out_buf,"kIOReturnDeviceError - the device is not working properly!"); break; case kIOReturnNoCompletion: sprintf(out_buf,"kIOReturnNoCompletion - a completion routine is required"); break; case kIOReturnAborted: sprintf(out_buf,"kIOReturnAborted - operation aborted"); break; case kIOReturnNoBandwidth: sprintf(out_buf,"kIOReturnNoBandwidth - bus bandwidth would be exceeded"); break; case kIOReturnNotResponding: sprintf(out_buf,"kIOReturnNotResponding - device not responding"); break; case kIOReturnIsoTooOld: sprintf(out_buf,"kIOReturnIsoTooOld - isochronous I/O request for distant past!"); break; case kIOReturnIsoTooNew: sprintf(out_buf,"kIOReturnIsoTooNew - isochronous I/O request for distant future"); break; case kIOReturnNotFound: sprintf(out_buf,"kIOReturnNotFound - data was not found"); break; case kIOReturnInvalid: sprintf(out_buf,"kIOReturnInvalid - should never be seen"); break; case kIOUSBUnknownPipeErr:sprintf(out_buf,"kIOUSBUnknownPipeErr - Pipe ref not recognised"); break; case kIOUSBTooManyPipesErr:sprintf(out_buf,"kIOUSBTooManyPipesErr - Too many pipes"); break; case kIOUSBNoAsyncPortErr:sprintf(out_buf,"kIOUSBNoAsyncPortErr - no async port"); break; case kIOUSBNotEnoughPipesErr:sprintf(out_buf,"kIOUSBNotEnoughPipesErr - not enough pipes in interface"); break; case kIOUSBNotEnoughPowerErr:sprintf(out_buf,"kIOUSBNotEnoughPowerErr - not enough power for selected configuration"); break; case kIOUSBEndpointNotFound:sprintf(out_buf,"kIOUSBEndpointNotFound - Not found"); break; case kIOUSBConfigNotFound:sprintf(out_buf,"kIOUSBConfigNotFound - Not found"); break; case kIOUSBTransactionTimeout:sprintf(out_buf,"kIOUSBTransactionTimeout - time out"); break; case kIOUSBTransactionReturned:sprintf(out_buf,"kIOUSBTransactionReturned - The transaction has been returned to the caller"); break; case kIOUSBPipeStalled:sprintf(out_buf,"kIOUSBPipeStalled - Pipe has stalled, error needs to be cleared"); break; case kIOUSBInterfaceNotFound:sprintf(out_buf,"kIOUSBInterfaceNotFound - Interface ref not recognised"); break; case kIOUSBLinkErr:sprintf(out_buf,"kIOUSBLinkErr - <no error description available>"); break; case kIOUSBNotSent2Err:sprintf(out_buf,"kIOUSBNotSent2Err - Transaction not sent"); break; case kIOUSBNotSent1Err:sprintf(out_buf,"kIOUSBNotSent1Err - Transaction not sent"); break; case kIOUSBBufferUnderrunErr:sprintf(out_buf,"kIOUSBBufferUnderrunErr - Buffer Underrun (Host hardware failure on data out, PCI busy?)"); break; case kIOUSBBufferOverrunErr:sprintf(out_buf,"kIOUSBBufferOverrunErr - Buffer Overrun (Host hardware failure on data out, PCI busy?)"); break; case kIOUSBReserved2Err:sprintf(out_buf,"kIOUSBReserved2Err - Reserved"); break; case kIOUSBReserved1Err:sprintf(out_buf,"kIOUSBReserved1Err - Reserved"); break; case kIOUSBWrongPIDErr:sprintf(out_buf,"kIOUSBWrongPIDErr - Pipe stall, Bad or wrong PID"); break; case kIOUSBPIDCheckErr:sprintf(out_buf,"kIOUSBPIDCheckErr - Pipe stall, PID CRC Err:or"); break; case kIOUSBDataToggleErr:sprintf(out_buf,"kIOUSBDataToggleErr - Pipe stall, Bad data toggle"); break; case kIOUSBBitstufErr:sprintf(out_buf,"kIOUSBBitstufErr - Pipe stall, bitstuffing"); break; case kIOUSBCRCErr:sprintf(out_buf,"kIOUSBCRCErr - Pipe stall, bad CRC"); break; default: sprintf(out_buf,"Unknown Error:%d Sub:%d System:%d",err_get_code(err), err_get_sub(err),err_get_system(err)); ok=false; break; } return ok; }