Skip to content

Instantly share code, notes, and snippets.

@andrealmeid
Created November 30, 2021 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrealmeid/b495999b8efce3c741e14a709a21e49b to your computer and use it in GitHub Desktop.
Save andrealmeid/b495999b8efce3c741e14a709a21e49b to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-2.0
/*
* Hidraw Userspace Example
*
* Copyright (c) 2010 Alan Ott <alan@signal11.us>
* Copyright (c) 2010 Signal 11 Software
* Copyright (c) 2021 Collabora
*
* The code may be used by anyone for any purpose,
* and can serve as a starting point for developing
* applications using hidraw.
*/
/* Linux */
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
/* Unix */
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define NUM_RUNS 10000
void *thread(void *arg)
{
int fd = (int)(unsigned long) arg;
struct timespec start, end;
struct hidraw_report_descriptor rpt_desc;
struct hidraw_devinfo info;
int i, res, desc_size = 0;
char buf[256];
char device[12];
strcpy(device, "/dev/hidraw");
device[11] = fd + '0';
device[12] = '\0';
fd = open(device, O_RDWR|O_NONBLOCK);
if (fd < 0) {
perror("open");
return 0;
}
memset(&rpt_desc, 0x0, sizeof(rpt_desc));
memset(&info, 0x0, sizeof(info));
memset(buf, 0x0, sizeof(buf));
clock_gettime(CLOCK_MONOTONIC, &start);
for (i = 0; i < NUM_RUNS; i++) {
/* Get Report Descriptor Size */
res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
if (res < 0)
perror("HIDIOCGRDESCSIZE");
/* Get Report Descriptor */
rpt_desc.size = desc_size;
res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
if (res < 0) {
perror("HIDIOCGRDESC");
}
/* Get Raw Name */
res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
if (res < 0)
perror("HIDIOCGRAWNAME");
/* Get Physical Location */
res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
if (res < 0)
perror("HIDIOCGRAWPHYS");
/* Get Raw Info */
res = ioctl(fd, HIDIOCGRAWINFO, &info);
if (res < 0) {
perror("HIDIOCGRAWINFO");
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
printf("total time %lu.%lu\n",
end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
printf("mean time %f nsec\n", (end.tv_nsec - start.tv_nsec) / (NUM_RUNS * 5.0));
}
int main(int argc, char **argv)
{
pthread_t threads[6];
int i;
for (i = 0; i < 6; i++)
pthread_create(&threads[i], NULL, &thread,
(void *)(unsigned long) i);
for (i = 0; i < 6; i++)
pthread_join(threads[i], NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment