Linux USB HID Device test Code

Source: Internet
Author: User

/****************************************************************** author:drivermonkey* Mail:[email  protected]* phone:18575593141* qq:196568501* blog:http://blog.csdn.net/drivermonkey* Date:02/07/2015 ************** /#include <pthread.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include <fcntl.h> #include <errno.h> #include <stdio.h># Include <stdlib.h> #include <unistd.h> #define Hid_max_packet_size 64#define hid_buffer_size (10*1024*1024 )//10M bytestypedef struct {char null_array[hid_max_packet_size];} buffer_offset_size_t;static Char hid_read_buffer[hid_buffer_size];static char hid_write_buffer[hid_buffer_size]; static int hid_file_handle = 0;static Const char* hid_file_name = "/dev/hidg0"; static int hid_init (void); static int hid_re AD (void* buffer, int buffer_size), static int hid_write (void* buffer, int buffer_size);/******************************* ********************* Function Name:main * author:drivermonkey * function Description:main function just for HID temp testing * Input argv: * Output argv: * Return: * Be careful: *****************************************************************/in T Main (void) {int read_size = 0;int Test_count = 100;hid_init (); while (test_count--) {memset (Hid_read_buffer, 0x00, hid_ buffer_size); read_size = Hid_read (Hid_read_buffer, hid_buffer_size);//printf ("Getting report::%s \ n", Hid_read_ buffer); memset (Hid_write_buffer, 0x00, hid_buffer_size); strcpy (Hid_write_buffer, Hid_read_buffer); Hid_write (hid_ Write_buffer, read_size);} return 0;} /***************************************************************** * Function name:hid_init * Author:DriverMonkey * Function description:init HID * Input argv: * Output argv: * return:>= 0-no Error < 0-reading error * be Carefu L:must be called Befoore HID is used! /static int Hid_init (void) {if (hid_fIle_handle = open (Hid_file_name, O_RDWR, 0666)) < 0) {perror (hid_file_name); return hid_file_handle;} Else{return Hid_file_handle;}} /***************************************************************** * Function Name:hid_read * Author:DriverMonkey * Function description:read Data Form HID driver * Input argv:buffer_size-buffer size * Output Argv:buffer-buffer to S Ave reading out data * return:>= 0-read size < 0-reading error * be careful: ********************************** /static int Hid_read (void* buffer, int buffer_size) {if (buffer = = NULL) {perror ("hid_ READ::p ointer error! "); return-1;} Return read (hid_file_handle, buffer, buffer_size);} /***************************************************************** * Function Name:hid_read * Author:DriverMonkey * Function description:read Data Form HID driver * Input argv:buffer_size-buffer size * Output Argv:buffer-buffer to S Ave reading out data * return:>= 0-no error < 0-reading error * be careful: *****************************************************************/static int hid_write (void* buffer,  int buffer_size) {int Return_v = 0;int Writting_count = Buffer_size/hid_max_packet_size;int remainding_size = buffer_size % hid_max_packet_size;buffer_offset_size_t* Buffer_offset = (buffer_offset_size_t*) buffer;if (buffer = = NULL) {perror ( "Hid_write::p ointer error!"); return-1;} while (writting_count--) {Return_v = write (Hid_file_handle, buffer_offset,hid_max_packet_size); if (Return_v < 0) { Perror ("hid_write::writting error!"); return Return_v;} buffer_offset++;} Return_v = Write (Hid_file_handle, Buffer_offset, remainding_size); return return_v;}

Linux USB HID Device test Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.