void report( off_t filepos, const uint8_t* src, size_t len, const char* type ) { fprinthex(stdout, filepos, 8); printf(": "); while(len--) { uint8_t c = *src++; printf("%02X ", c); } printf("(%s)\n", type); }
// Send a command to the portal bool Write(libusb_device_handle *hPortalHandle, RWBlock *pb) { int transferred; int err; printf("Write\n"); pb->buf[0] = 0; // Use report 0 fprinthex(stdout,pb->buf,0x21); err = libusb_interrupt_transfer (hPortalHandle, 0x1, pb->buf, 0x21, &transferred, 30000); printf("Write, %d bytes transferred (err = %s)\n",transferred,libusb_error(err)); return err == 0; // return HidD_SetOutputReport(hPortalHandle, pb->buf, 0x21); }
bool ReadBlock(libusb_device_handle *hPortalHandle, unsigned int block, unsigned char data[0x10], bool isNEWskylander) { RWBlock req, res; bool running = true; bool gotData; unsigned char ovlr[OVERLAP_SIZE]; unsigned short err; unsigned char followup; printf("ReadBlock\n"); if(block >= 0x40) { return false; } printf("."); for(int retries = 0; retries < 3; retries++) { // Send query request memset(req.buf, 0, rw_buf_size); req.buf[1] = 'Q'; if(isNEWskylander) { followup = 0x11; if(block == 0) { req.buf[2] = 0x21; } else { req.buf[2] = followup; } } else { followup = 0x10; if(block == 0) { req.buf[2] = 0x20; } else { req.buf[2] = followup; } } req.buf[3] = (unsigned char)block; memset(&(res.buf), 0, rw_buf_size); // Must set the Offset and OffsetHigh members of the OVERLAPPED structure to zero. // memset(&ovlr, 0, sizeof(ovlr)); // ovlr.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // read event int i=0; gotData = false; Write(hPortalHandle, &req); // Don't wait. Start polling for result immediately for(; i<40; ++i) // try up to 40 reads { int b; // bool b = ReadFile(hPortalHandle, res.buf, rw_buf_size, &(res.dwBytesTransferred), &ovlr); b = libusb_interrupt_transfer (hPortalHandle, 0x81, res.buf, rw_buf_size, &(res.dwBytesTransferred), 30000); if (b>=0) fprinthex(stdout,res.buf,res.dwBytesTransferred); if(b<0) { printf("error reading from usb handle: %s\n",libusb_error(b)); /* failed to get data immediately*/ // err = GetLastError(); if(b == 0) { /* wait for data */ // b = GetOverlappedResult(hPortalHandle, &ovlr, &res.dwBytesTransferred, TRUE); if(!b) { /* wait failed */ break; } } else { /* some other error */ break; } } if(res.dwBytesTransferred > 0) { /* has data */ if(res.buf[1] == 'Q' && res.buf[3] == (unsigned char)block) { // Got our query back if(res.buf[2] == followup) { /* got the query back with no error */ gotData = true; break; } } res.buf[0] = 0; // make sure we are using report 0 } } /* read loop */ // CloseHandle(ovlr.hEvent); if(gotData) { break; } } // retries if(gotData) { memcpy(data, res.buf + 4, 0x10); } return gotData; }