int main(int argc, char **argv) { FILE *pFile = NULL; int size; long lSize; unsigned char *buffer; unsigned char *buf2 = calloc(300, sizeof(unsigned char)); if (argc < 2) { fprintf(stderr, "Missing upload filename \n\n"); fprintf(stderr, "Usage: \n"); fprintf(stderr, "ISP808 <uploadfile> \n"); exit(1);; } pFile = fopen(argv[1], "rb"); if (pFile == NULL) { fprintf(stderr, "ERROR: Unable to open file %s \n", argv[1]); exit(1); } // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile); // allocate memory to contain the whole file: buffer = calloc(lSize, sizeof(unsigned char)); if (buffer == NULL) { fputs("Memory error", stderr); exit(2); } size = fread(buffer, 1, lSize, pFile); if (size != lSize) { fputs("Reading error", stderr); exit(3); } libusb_context *context; libusb_init(&context); libusb_set_debug(context, 3); device_handle = libusb_open_device_with_vid_pid(context, VID, PID); if (!device_handle) { fprintf(stderr, "ERROR: Unable to find device " "(Vendor ID = %04x, Product ID = %04x)\n", VID, PID); fprintf(stderr, "Press the Mode Button and connect the cam to USB \n"); return 1; } libusb_claim_interface(device_handle, 2); libusb_set_interface_alt_setting(device_handle, 0, 0); usleep(100); // Get the installed version fprintf(stdout, "\nRead 808 Version \n"); GetVersion(); GetVersion1(); GetVersion2(); fprintf(stdout, "\nStart Uploading the file: %s \n",argv[1]); char defout[] = { 0x00, 0x80, 0x04, 0x00, 0x2d, 0x10, 0x00, 0x00 }; control_in_vendor_device(0x70, 0, 0, 5); usleep(100); control_out(0xfd, 0, 0x4f3, defout, 8); usleep(200); // Fix me // Download the code fprintf(stdout, "\nDownload Code.... \n"); bulk_write(3, buffer, lSize); usleep(200); char defout2[] = { 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }; control_out(0xfd, 0, 0x4f1, defout2, 0x10); usleep(100); // Dont know why bulk_write(3, buf2, 256); fprintf(stdout, "\nDownload Ready.... \n"); usleep(100); if (pFile) fclose(pFile); free(buffer); libusb_release_interface(device_handle, 0); libusb_close(device_handle); libusb_exit(context); return 0; }
int main(int argc, char **argv) { const char *mode = "CGRAY"; int resolution = 100; if(argc > 1) { switch(*argv[1]) { case 'c': mode = "CGRAY"; break; case 'g': mode = "GRAY64"; break; case 't': mode = "TEXT"; break; case '-': mode = NULL; break; default: fprintf(stderr, "ERROR: unrecognised mode, " "should be one of [cgt]\n"); return 1; } } if(argc > 2) { resolution = atoi(argv[2]); if(resolution < 100 || resolution > 600 || resolution % 100) { fprintf(stderr, "ERROR: resolution must be a positive " "multiple of 100 no greater than 600\n"); return 1; } } libusb_context *context; libusb_init(&context); device_handle = libusb_open_device_with_vid_pid(context, VID, PID); if(!device_handle) { fprintf(stderr, "ERROR: Unable to find device " "(Vendor ID = %04x, Product ID = %04x)\n", VID, PID); return 1; } libusb_claim_interface(device_handle, 0); libusb_set_interface_alt_setting(device_handle, 0, 0); control_in_vendor_device(1, 2, 0, 255); /* returns 05 10 01 02 00 */ if(mode) { char *config = build_config( mode, MIN(resolution, 300), resolution, PAGE_WIDTH * MIN(resolution, 300)/100, PAGE_HEIGHT * resolution/100); send_config(config); free(config); } int page = 1; FILE *fp = NULL; int sleep_time = 0; while(1) { unsigned char buf[0x1000]; int num_bytes = bulk_read(4, buf, 0x1000); if(num_bytes) sleep_time = 0; if(num_bytes > 2) { if(!fp) { char fname[8]; snprintf(fname, 8, "%03d.raw", page); fprintf(stderr, "Opening '%s'...\n", fname); fp = fopen(fname, "wb"); } fwrite(buf, 1, num_bytes, fp); } else if(num_bytes == 0 && sleep_time < 10) { #ifdef DEBUG fprintf(stderr, "Sleeping\n"); #endif sleep_time++; usleep(200 * 1000); } else if(num_bytes == 2 && buf[0] == 0xc2 && buf[1] == 0x00) { fprintf(stderr, "ERROR: Nothing to scan\n"); break; } else if(num_bytes == 2 && buf[0] == 0xc3 && buf[1] == 0x00) { fprintf(stderr, "ERROR: Paper jam\n"); break; } else if(num_bytes == 1 && buf[0] == 0x80) { fprintf(stderr, "No more pages\n"); break; } else if((num_bytes == 1 && buf[0] == 0x81) || sleep_time >= 10) { fprintf(stderr, "Feeding in another page"); if(sleep_time >= 10) fprintf(stderr, " (timeout)"); fprintf(stderr, "\n"); fclose(fp); fp = NULL; send_config(""); page++; sleep_time = 0; } else if(num_bytes == 1 && buf[0] == 0xc3) { fprintf(stderr, "Paper jam\n"); break; } else if(num_bytes == 1 && buf[0] == 0xc4) { fprintf(stderr, "Scan aborted\n"); break; } else { fprintf(stderr, "Received unknown data: %02x", buf[0]); if(num_bytes == 2) fprintf(stderr, " %02x", buf[1]); fprintf(stderr, "\n"); break; } } if(fp) fclose(fp); control_in_vendor_device(2, 2, 0, 255); /* returns 05 10 02 02 00 */ libusb_release_interface(device_handle, 0); libusb_close(device_handle); libusb_exit(context); return 0; }
void GetVersion2() { control_in_vendor_device(0x20, 0, 2, 8); }