Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // usbBoot
- //
- // Created by Vlad Victor Ungureanu on 6/11/13.
- // Copyright (c) 2013 Vlad Victor Ungureanu. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <libusb.h>
- int main(int argc, const char * argv[]) {
- libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
- libusb_device_handle *dev_handle; //a device handle
- libusb_context *ctx = NULL; //a libusb session
- int r; //for return values
- ssize_t cnt; //holding number of devices in list
- r = libusb_init(&ctx); //initialize the library for the session we just declared
- if(r < 0) {
- printf("Init error!\n");
- return 1;
- }
- libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
- cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
- if(cnt < 0) {
- printf("Cannot get device list!\n");
- return 1;
- }
- printf("%zd devices in list!\n",cnt);
- dev_handle = libusb_open_device_with_vid_pid(ctx, 0x0451, 0x6141); //these are vendorID and productID I found for my usb device
- if(dev_handle == NULL) {
- printf("Cannot open device!\n");
- return 1;
- }
- else
- printf("Device opened!\n");
- libusb_free_device_list(devs, 1); //free the list, unref the devices in it
- unsigned char *data = (unsigned char*)malloc(64*sizeof(unsigned char));
- int actual; //used to find out how many bytes were written
- if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
- printf("Kernel has driver!\n");
- if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
- printf("Kernel deattached!\n!");
- }
- r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
- if(r < 0) {
- printf("Cannot Claim Interface!\n");
- return 1;
- }
- printf("Claimed interface!\n");
- r = libusb_bulk_transfer(dev_handle, (129 | LIBUSB_ENDPOINT_IN), data, 64, &actual, 0); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
- while(r==0) {
- int i;
- for(i = 0; i<63; i++)printf("%c ",data[i]);
- r = libusb_bulk_transfer(dev_handle, (129 | LIBUSB_ENDPOINT_IN), data, 64, &actual, 0);
- }
- r = libusb_release_interface(dev_handle, 0); //release the claimed interface
- if(r!=0) {
- printf("Cannot release interface!\n");
- return 1;
- }
- libusb_close(dev_handle); //close the device we opened
- libusb_exit(ctx); //needs to be called to end the
- free(data); //delete the allocated memory for data
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment