Esempio n. 1
0
//http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/
int list_all_usb_devices() {

	libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
	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 a library session
	if(r < 0) {
		std::cout<<"Init Error "<<r<<std::endl; //there was an error
				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)
		std::cout<<"Get Device Error"<<std::endl; //there was an error

	std::cout<<cnt<<" Devices in list."<<std::endl; //print total number of usb devices
	ssize_t i; //for iterating through the list

	for(i = 0; i < cnt; i++)
		printdev(devs[i]); //print specs of this device

		libusb_free_device_list(devs, 1); //free the list, unref the devices in it
		libusb_exit(ctx); //close the session

		return 0;
}
int main(int argc, char **argv) {

   printf("Arguments: %i\n",argc);
   int arg;
   for (arg = 0 ; arg < argc ; arg++)
      printf("  %i: %s;\n", arg,argv[arg]);
   if ((argc > 1) && argv[1][0] == '1') {
      test = 1;
      printf("Set test\n");
   }

   libusb_device **list;
   //libusb_device *accel;
   libusb_context *ctx = NULL;


   libusb_init(&ctx);
   libusb_set_debug(ctx,3);
   names_init("./usb.ids");

   ssize_t cnt = libusb_get_device_list(ctx,&list);

   ssize_t i = 0;
   int err = 0;

   if (cnt < 0) {
      printf("Error getting usb device list\n");
      exit(1);
   }

   for (i = 0 ; i < cnt ; i++) {
      libusb_device *device = list[i];
      struct libusb_device_descriptor desc;
      err = libusb_get_device_descriptor(device,&desc);
      if (err < 0)
         printf("Error getting device descriptor: %i\n",err);
      else {
         if (desc.idVendor == VENDOR_ID && desc.idProduct == PRODUCT_ID) {
            printf("Device Found: idVendor = %xh, idProduct = %xh\n",desc.idVendor, desc.idProduct);
            printdev(device);
            int reattach = 0;
            libusb_device_handle *handle;
            int r = libusb_open(device,&handle);
            if (r < 0){
               printf("Error opening device: %i\n",r);
               exit(1);
            }

            if (libusb_kernel_driver_active(handle,0)) {
               printf("Device already has a kernel driver\n");
               r = libusb_detach_kernel_driver(handle,0);
               if (r < 0) {
                  printf("Unable to detach kernel driver: %i\n",r);
                  goto CLOSE;
               }
               reattach = 1;
            }

            if (libusb_claim_interface(handle,0) == 0) {
               printf("Claimed Device\n");
               dump_hid(device,handle);

               if (test) {
                  printf("Beginning report testing\n");
                  test_report(handle);
               }
            }
CLOSE:

            libusb_release_interface(handle, 0);
            if (reattach) {
               r = libusb_attach_kernel_driver(handle,0);
               if (r < 0) {
                  printf("Failed to reattach kernel driver: %i\n",r);
                  switch (-1) {
                     case LIBUSB_ERROR_IO: 
                        printf("I/O error\n");
                        break;
                     case LIBUSB_ERROR_ACCESS:
                        printf("Access Denied\n");
                        break;
                     case LIBUSB_ERROR_NOT_FOUND:
                        printf("Kernel driver wasn't active\n");
                        break;
                     case LIBUSB_ERROR_INVALID_PARAM:
                        printf("Interface doesn't exist\n");
                        break;
                     case LIBUSB_ERROR_NO_DEVICE:
                        printf("Device was disconnected\n");
                        break;
                     case LIBUSB_ERROR_BUSY:
                        printf("Interface is still claimed\n");
                        break;
                     case LIBUSB_ERROR_TIMEOUT:
                        printf("Operation timed out\n");
                        break;
                     case LIBUSB_ERROR_OVERFLOW:
                        printf("Overflow\n");
                        break;
                     case LIBUSB_ERROR_PIPE:
                        printf("Pipe Error\n");
                        break;
                     case LIBUSB_ERROR_INTERRUPTED:
                        printf("System call interrupted\n");
                        break;
                     case LIBUSB_ERROR_NO_MEM:
                        printf("Insufficient Memory\n");
                        break;
                     case LIBUSB_ERROR_NOT_SUPPORTED:
                        printf("Operation not supported\n");
                        break;
                     default:
                        printf("Some other error occured\n");
                        break;
                  }
               }
            }
            libusb_close(handle);
         }
      }
   }
   libusb_free_device_list(list,1);
   libusb_exit(ctx);
   exit(err);
}
Esempio n. 3
0
void
printlong(const DISPLAY *dp)
{
	struct stat *sp;
	FTSENT *p;
	NAMES *np;
	char buf[20];
#ifdef COLORLS
	int color_printed = 0;
#endif

	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
	    (f_longform || f_size)) {
		xo_emit("{L:total} {:total-blocks/%lu}\n",
			howmany(dp->btotal, blocksize));
	}

	xo_open_list("entry");
	for (p = dp->list; p; p = p->fts_link) {
		char *name, *type;
		if (IS_NOPRINT(p))
			continue;
		xo_open_instance("entry");
		sp = p->fts_statp;
		name = getname(p->fts_name);
		if (name)
		    xo_emit("{ke:name/%hs}", name);
		if (f_inode)
			xo_emit("{t:inode/%*ju} ",
			    dp->s_inode, (uintmax_t)sp->st_ino);
		if (f_size)
			xo_emit("{t:blocks/%*jd} ",
			    dp->s_block, howmany(sp->st_blocks, blocksize));
		strmode(sp->st_mode, buf);
		aclmode(buf, p);
		np = p->fts_pointer;
		xo_attr("value", "%03o", (int) sp->st_mode & ALLPERMS);
		if (f_numericonly) {
			xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} {td:user/%-*s}{e:user/%ju}  {td:group/%-*s}{e:group/%ju}  ",
				buf, (int) sp->st_mode & ALLPERMS, dp->s_nlink, sp->st_nlink,
				dp->s_user, np->user, sp->st_uid, dp->s_group, np->group, sp->st_gid);
		} else {
			xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} {t:user/%-*s}  {t:group/%-*s}  ",
				buf, (int) sp->st_mode & ALLPERMS, dp->s_nlink, sp->st_nlink,
				dp->s_user, np->user, dp->s_group, np->group);
		}
		if (S_ISBLK(sp->st_mode))
			asprintf(&type, "block");
		if (S_ISCHR(sp->st_mode))
			asprintf(&type, "character");
		if (S_ISDIR(sp->st_mode))
			asprintf(&type, "directory");
		if (S_ISFIFO(sp->st_mode))
			asprintf(&type, "fifo");
		if (S_ISLNK(sp->st_mode))
			asprintf(&type, "symlink");
		if (S_ISREG(sp->st_mode))
			asprintf(&type, "regular");
		if (S_ISSOCK(sp->st_mode))
			asprintf(&type, "socket");
		if (S_ISWHT(sp->st_mode))
			asprintf(&type, "whiteout");
		xo_emit("{e:type/%s}", type);
		free(type);
		if (f_flags)
			xo_emit("{:flags/%-*s} ", dp->s_flags, np->flags);
		if (f_label)
			xo_emit("{t:label/%-*s} ", dp->s_label, np->label);
		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
			printdev(dp->s_size, sp->st_rdev);
		else
			printsize("size", dp->s_size, sp->st_size);
		if (f_accesstime)
			printtime("access-time", sp->st_atime);
		else if (f_birthtime)
			printtime("birth-time", sp->st_birthtime);
		else if (f_statustime)
			printtime("change-time", sp->st_ctime);
		else
			printtime("modify-time", sp->st_mtime);
#ifdef COLORLS
		if (f_color)
			color_printed = colortype(sp->st_mode);
#endif

		if (name) {
		    xo_emit("{dk:name/%hs}", name);
		    free(name);
		}
		
#ifdef COLORLS
		if (f_color && color_printed)
			endcolor(0);
#endif
		if (f_type)
			(void)printtype(sp->st_mode);
		if (S_ISLNK(sp->st_mode))
			printlink(p);
		xo_close_instance("entry");
		xo_emit("\n");
	}
	xo_close_list("entry");
}
Esempio n. 4
0
void
printlong(const DISPLAY *dp)
{
	struct stat *sp;
	FTSENT *p;
	NAMES *np;
	char buf[20];
#ifdef COLORLS
	int color_printed = 0;
#endif

	if ((dp->list == NULL || dp->list->fts_level != FTS_ROOTLEVEL) &&
	    (f_longform || f_size)) {
		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
	}

	for (p = dp->list; p; p = p->fts_link) {
		if (IS_NOPRINT(p))
			continue;
		sp = p->fts_statp;
		if (f_inode)
			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
		if (f_size)
			(void)printf("%*jd ",
			    dp->s_block, howmany(sp->st_blocks, blocksize));
		strmode(sp->st_mode, buf);
		aclmode(buf, p);
		np = p->fts_pointer;
		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
		    np->group);
		if (f_flags)
			(void)printf("%-*s ", dp->s_flags, np->flags);
		if (f_label)
			(void)printf("%-*s ", dp->s_label, np->label);
		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
			printdev(dp->s_size, sp->st_rdev);
		else
			printsize(dp->s_size, sp->st_size);
		if (f_accesstime)
			printtime(sp->st_atime);
		else if (f_birthtime)
			printtime(sp->st_birthtime);
		else if (f_statustime)
			printtime(sp->st_ctime);
		else
			printtime(sp->st_mtime);
#ifdef COLORLS
		if (f_color)
			color_printed = colortype(sp->st_mode);
#endif
		(void)printname(p->fts_name);
#ifdef COLORLS
		if (f_color && color_printed)
			endcolor(0);
#endif
		if (f_type)
			(void)printtype(sp->st_mode);
		if (S_ISLNK(sp->st_mode))
			printlink(p);
		(void)putchar('\n');
	}
}
Esempio n. 5
0
int setup_usb(void){
        int status;                             //Holds status value for functions
        ssize_t dev_cnt;                        //Holds number of devices found

        //////////////////-Create libusb session
        status = libusb_init(&ctx);             //Create a libusb session
        if(status < 0){
                cout<<"Cannot create libusb session. Error: "<<status<<endl;
                exit(-2);
        }
        libusb_set_debug(ctx,3);                //Set verbosity to lvl 3
        //////////////////

        //////////////////-Available USB devices
        dev_cnt = libusb_get_device_list(ctx, &dev);    //Get list of USB devices

#ifdef USB_DEBUG
        if(dev_cnt < 0 ){
                cout<<"No USB devices found"<<endl;
        }
        ssize_t i;                              //Search through device list
        for(i = 0; i < dev_cnt; i++){
                status = libusb_get_device_descriptor(dev[i], &desc);
                if(status < 0){
                        cout<<"Failed to get device descriptor"<<endl;
                        exit(-2);
                }
                if(desc.idVendor == SCROD_USB_VID && desc.idProduct == SCROD_USB_PID){
                        cout<<"Found SCROD USB Device"<<endl;
                        printdev(dev[i]);
                        list_endpoints(dev[i]);
                        break;
                }
        }
        if(i == dev_cnt){
                cout<<"Could not find SCROD USB Device"<<endl;
                exit(-2);
        }
#endif
        //////////////////

        //////////////////-Open USB device
        dev_handle = libusb_open_device_with_vid_pid(ctx,SCROD_USB_VID,SCROD_USB_PID);  //Open SCROD USB Device
        if(dev_handle == NULL){
                cout<<"Could not open SCROD USB Device"<<endl;
                exit(-2);
        }
        else{
                cout<<"SCROD USB Device Opened"<<endl;
        }
        libusb_free_device_list(dev,1);         //Unreferencing devices in device list as suggested in documentation
        //////////////////

        //////////////////-Check kernel status
        status = libusb_kernel_driver_active(dev_handle,0);
        if(status == 1){
                cout<<"Kernel driver active"<<endl;
                cout<<"Trying to detach driver..."<<endl;
                status = libusb_detach_kernel_driver(dev_handle,0);
                if(status == 0){
                        cout<<"Kernel driver detached"<<endl;
                }
                else{
                        cout<<"Kernel driver cannot be detached"<<endl;
                        exit(-3);
                }
        }
        //////////////////

        //////////////////-Claim an interface
        status = libusb_claim_interface(dev_handle,0);
        if(status < 0){
                cout<<"Could not claim an interface"<<endl;
                exit(-3);
        }
        else{
                cout<<"Interface claimed"<<endl;
        }
        //////////////////
}
Esempio n. 6
0
int main(int argc, char **argv) {


	libusb_context* m_context;

	int init_result= libusb_init(&m_context);
	if(init_result){
		std::cerr << "USB init failed: "<< init_result << std::endl;

		exit(EXIT_FAILURE);
	}


	libusb_set_debug(m_context,3);


//	libusb_device** m_device_list;
//	libusb_device** actual_device_ptr;
//
//	ssize_t num_devices=libusb_get_device_list(m_context, &m_device_list);
//
//	actual_device_ptr=m_device_list;
//
//	// iterate device list
//	while(*actual_device_ptr!=0){
//		libusb_device_descriptor desc;
//
//		if(int error=libusb_get_device_descriptor(*m_device_list,&desc)){
//
//			std::cerr << "Get device error: " << libusb_error_name(error) <<std::endl;
//		}
//
//		std::cout << std::hex
//				<< "VendorID: " <<  desc.idVendor << std::endl
//				<< "ProductID: " << desc.idProduct << std::endl
//				<< "USB Release: "<< desc.bcdUSB << std::endl
//				<< "Serial number: " <<(int) desc.iSerialNumber << std::endl
//
//				<< std::dec;
//
//
//
//	   std::cout << "******************************************"<< std::endl;
//
//		actual_device_ptr++;
//	}
//
//	std::cout << "Devices found: "<< num_devices << std::endl;
//
//	libusb_free_device_list(m_device_list,0);
//



	#define VENDORID 0x19a2
	#define PRODUCTID 0x5001


	libusb_device_handle* m_usb_device_handle = libusb_open_device_with_vid_pid(m_context, VENDORID, PRODUCTID);
//	libusb_device_handle* m_device_handle = libusb_open_device_with_vid_pid(m_context, 0x05e3,0x1205);

	if( !m_usb_device_handle){

			std::cerr << "Error open device:" <<  std::endl;
	}else{



		libusb_device* m_device =libusb_get_device(m_usb_device_handle);

		printdev(m_device);

		std::cout << " Device Class 0: All Interfaces operate independently" << std::endl;
		std::cout << " Interface Number: 0" << std::endl;
		std::cout << " Two descriptors of type Endpoint" << std::endl;
		std::cout << " Endpoint Adresses 129 and 2, does thes mean we have Adress 2 with different directions? " << std::endl;

		// Our only available interface number
		int the_usb_interface_number=0;

		if(libusb_kernel_driver_active(m_usb_device_handle,the_usb_interface_number)){

			std::cout << "Error: Kernel driver found" << std::endl;

			if (int error=libusb_detach_kernel_driver(m_usb_device_handle,the_usb_interface_number)){
				std::cout << "Detaching Kernel Driver failed " << error << std::endl;
				exit(EXIT_FAILURE);
			}

		}else{
			std::cout << " No Kernel driver for attached Device found, "
					"that's good, libusb handels the device" << std::endl;
		}


		// Now we claim the device
		int error = libusb_claim_interface(m_usb_device_handle,the_usb_interface_number);
		if(error){

			std::cout << "Claiming interface failed " << error << std::endl;
			exit(EXIT_FAILURE);

		}


		static const uint8_t	DATA_STX=2; //Start of text
		static const uint8_t	DATA_ETX=3; // End of Text

		uint8_t start_one_scan[]={DATA_STX,'s','R','N',' ','L','M','D','s','c','a','n','d','a','t','a',DATA_ETX};

		uint8_t start_continuous_scan[]={DATA_STX,'s','E','N',' ','L','M','D','s','c','a','n','d','a','t','a',' ','1',DATA_ETX};
		uint8_t stop_continuous_scan[]={DATA_STX,'s','E','N',' ','L','M','D','s','c','a','n','d','a','t','a',' ','0',DATA_ETX};



		uint8_t	read_endpoint=129;
		uint8_t	write_endpoint=2;
		unsigned int timeout_millis=10000;

//		std::cout << sizeof(start_one_scan) << std::endl;


		int transferred_data_size=0;

		uint8_t receive_buf[2049];


			error =libusb_bulk_transfer(
					m_usb_device_handle,
					write_endpoint,
//					start_one_scan,
//					sizeof(start_one_scan),
					start_continuous_scan,
					sizeof(start_continuous_scan),
					&transferred_data_size,
					timeout_millis);

			if(error){

				std::cout << "Write Bulk Transfer failed " << error << std::endl;
				exit(EXIT_FAILURE);

			}

			std::cout<< "Transfered Data written: " << transferred_data_size<< std::endl;

			// Receive acknoledge

			error =libusb_bulk_transfer(
					m_usb_device_handle,
					read_endpoint,
					receive_buf ,
					sizeof(receive_buf)-1, // leave one for termination
					&transferred_data_size,
					timeout_millis);

			if(error){

				std::cout << "Read Bulk Transfer failed " << error << std::endl;
				exit(EXIT_FAILURE);

			}


			// String terminierung
			receive_buf[transferred_data_size]=0;
			std::cout<< "Acknoledge: " << receive_buf<< std::endl;



		TiM3xx_Data_Parser mdp;


		for (int i = 0; i < 10; ++i) {


			error =libusb_bulk_transfer(
					m_usb_device_handle,
					read_endpoint,
					receive_buf ,
					sizeof(receive_buf)-1, // leave one for termination
					&transferred_data_size,
					timeout_millis);

			if(error){

				std::cout << "Read Bulk Transfer failed " << error << std::endl;
				exit(EXIT_FAILURE);

			}

			//std::cout<< "Transfered Data read: " << transferred_data_size<< std::endl;

			// String terminierung
			receive_buf[transferred_data_size]=0;

			if(receive_buf[0]!=DATA_STX){ // read from device buff until it is empty, and next data set starts with STX-Code
				std::cout << ".";
			}else{
//				std::cout << receive_buf << std::endl;
				mdp.set_pointer_to_data_buf(receive_buf,transferred_data_size);
				mdp.parse_data();
				mdp.print_data();

			}

		} // end of for loop


		// Stop Measurment

		error =libusb_bulk_transfer(
				m_usb_device_handle,
				write_endpoint,
				stop_continuous_scan,
				sizeof(stop_continuous_scan),
				&transferred_data_size,
				timeout_millis);

		if(error){

			std::cout << "Write Bulk Transfer failed " << error << std::endl;
			exit(EXIT_FAILURE);

		}

		std::cout<< "Transfered Data written: " << transferred_data_size<< std::endl;



		error =libusb_bulk_transfer(
				m_usb_device_handle,
				read_endpoint,
				receive_buf ,
				sizeof(receive_buf)-1, // leave one for termination
				&transferred_data_size,
				timeout_millis);

		if(error){

			std::cout << "Read Bulk Transfer failed " << error << std::endl;
			exit(EXIT_FAILURE);

		}



		std::cout<< "Transfered Data read: " << transferred_data_size<< std::endl;
		// String terminierung
		receive_buf[transferred_data_size]=0;
		std::cout << receive_buf << std::endl;



		error= libusb_release_interface(m_usb_device_handle,the_usb_interface_number);

		if(error){

			std::cout << "Releasing interface failed " << error << std::endl;
			exit(EXIT_FAILURE);

		}





//		libusb_device_descriptor desc;
//
//		libusb_get_device_descriptor(m_device,&desc);

//		uint8_t buff[2048];
//
//			for(int desc_index=1; desc_index<256;desc_index++){
//
//				int num_bytes_read=libusb_get_string_descriptor_ascii(m_device_handle,desc_index,buff,2048);
//
//
//				if(num_bytes_read>0){
//					std::cout<< desc_index << ": " << num_bytes_read << ": " << buff << std::endl;
//
//					if(libusb_kernel_driver_active(m_device_handle,0)){
//
//						std::cout << "Kernel driver found" << std::endl;
//					}
//
//
//				}else{
//					std::cerr<< desc_index << ": " << libusb_error_name(num_bytes_read) << std::endl;
//				}
//			}
//			std::cout  << "**********************************************" << std::endl;






	}





	if( m_usb_device_handle){

		libusb_close(m_usb_device_handle);

	}


	libusb_exit(m_context);

}