Linux USB HID Device 测试代码
2018-03-08 08:56
218 查看
转载地址:http://blog.csdn.net/drivermonkey/article/details/43635083
[cpp] view
plain copy
//测试平台:linux 3.2, am335x
[cpp] view
plain copy
/*****************************************************************
* Author: DriverMonkey
* Mail: bookworepeng@Hotmail.com
* 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 bytes
typedef 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_read(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:
*****************************************************************/
int 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 careful: Must be called befoore HID be 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 save 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::pointer 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 save 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::pointer 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;
}
[cpp] view
plain copy
//测试平台:linux 3.2, am335x
[cpp] view
plain copy
/*****************************************************************
* Author: DriverMonkey
* Mail: bookworepeng@Hotmail.com
* 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 bytes
typedef 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_read(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:
*****************************************************************/
int 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 careful: Must be called befoore HID be 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 save 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::pointer 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 save 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::pointer 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 测试代码
- 禁止站外提交参数测试代码
- 病毒软件测试代码
- JQuery 和Ajax测试代码
- php代码效率测试
- 测试一下csdn blog的代码高亮
- 使用WDM驱动实现在NT下读取物理端口,特殊寄存器,物理内存的代码(C++测试代码)
- 代码测试之代码调试
- SVO: Fast Semi-Direct Monocular Visual Odometry 代码测试
- 在Amazon device farm上使用appium进行自动化测试
- 正数取反、原码、反码、int最大与最小值的测试小代码
- mocha框架下,异步测试代码错误造成的问题----用例超时错误
- 前端环境搭建——代码开发测试部署
- Android手工测试的代码覆盖率
- Live writer 2011, 代码染色测试
- JUint4 简单测试代码
- [转] 如何让代码可测试化(C#)
- 测试代码运行时间
- 代码测试