Score:0

how to implement generic hid read with libusb interrupt callback

in flag

hello everyone I had been successful in enumerating my STM32 HID device on the Linux host and receiving input from it. In order to accomplish that, I used the "hid read" function and the hid-example from the Linux source code example. Currently, this read operation is in polling style. Now I want to continue and implement hid read operation as a callback with ISR without creating a read_thread. Is it possible? if any suggested documentation or hint given it would be great help.

Vishal Parmar avatar
in flag
I think this is what I am searching for https://vovkos.github.io/doxyrest/samples/libusb/group_libusb_asyncio.html
Vishal Parmar avatar
in flag
hi guys, I am using HIDAPI a LIBUSB wrapper for coding now
Vishal Parmar avatar
in flag
modified the code for an event callback-based hid read and used "hid get input report" to fill the read_buffer, but the read_buffer's contents is still "0."
Vishal Parmar avatar
in flag
I will update once, I get input from it.
Score:0
in flag

I discovered that the HID API wrapper automatically creates an interrupt read thread when used with libusb. I added print to "read_callback" and specified LIBUSB as the library dependency. there I got my code take print read data with hid interrupt callback.

some changes were as bellow:


  1. in hidapi/libusb/hid.c submodule

diff --git a/libusb/hid.c b/libusb/hid.c
index b46b1c7..20d8ee4 100644
--- a/libusb/hid.c
+++ b/libusb/hid.c
@@ -41,7 +41,7 @@
 #include <wchar.h>
 
 /* GNU / LibUSB */
-#include <libusb.h>
+#include <libusb-1.0/libusb.h>
 #if !defined(__ANDROID__) && !defined(NO_ICONV)
 #include <iconv.h>
 #ifndef ICONV_CONST
@@ -912,6 +912,8 @@ static void read_callback(struct libusb_transfer *transfer)
 
        pthread_mutex_lock(&dev->mutex);
 
+       printf("transfer data complate, length = %d, %s\r\n",transfer->actual_length, rpt->data);
+

  1. main.c code

/* 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>

/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>

/* custoum */
#include "hidapi.h"


unsigned short vid_to_find = 0x0483; 
unsigned short pid_to_find = 0x5750; 

int main(int argc, int* argv[])
{
    char hid_dev_path[1024], buf[256] = {0};
    char *device = "/dev/hidraw1";//had used for test
        
    int i, fd, res, desc_size;
    i = fd = desc_size = 0;

    buf[0] = 0x1;
    buf[1] = 0x2;

    struct hidraw_report_descriptor rpt_desc = {0};
    struct hidraw_devinfo info = {0};

/* hid enumaration test code */
    
    // Enumarate HID divice on system 
    struct hid_device_info * dev, * cur_dev;
    hid_device * my_hid_handle = NULL;

        // 1. enumarate device
        dev = hid_enumerate(vid_to_find,pid_to_find);
    
    if(dev->vendor_id == vid_to_find && dev->product_id == pid_to_find)
    {
        strcpy( hid_dev_path, dev->path );
        printf("Found our device!\r\n");
    }
    else
    {
        printf("device not found!\r\n");
        return 1;
    }

    /* hid file open test code */
    // 2. based on file descriptor open path
    if(dev)
    {
        // Open the Device with non-blocking reads.
        my_hid_handle = hid_open(vid_to_find,pid_to_find,NULL);
        //fd = open(dev->path, O_RDWR|O_NONBLOCK);
        //fd = open(hid_dev_path, O_RDWR|O_NONBLOCK);   //had used for test
        //fd = open(device, O_RDWR|O_NONBLOCK);     //had used for test
        
        printf("dev path: %s, fd: %d\r\n",dev->path,fd);
        
        if (fd < 0) 
        {
                    //perror("Unable to open device");
                    printf("Unable to open device\r\n");
                    printf("tip: try to execute binary with sudo\r\n");
                    return 1;
            }
        else
        {
                    printf("device open success\r\n");
        }

    }

    if(fd && dev)
    {
        printf("before hid write:\n\t hid_write result: %d\r\n", res);//used for test
        //res = hid_write(dev, buf, sizeof(buf));
        res = hid_write(my_hid_handle, buf, 3);

        printf("after hid write:\n\t hid_write result: %d\r\n", res);
        if(res)
        {
                    printf("device write success\r\n");
        }
        else
        {
            printf("device write fail\r\n");
        }
    }
    
    while (1)
    {
        sleep(5);
    }

    
    //last step free device
    hid_close(my_hid_handle);
    hid_free_enumeration(dev);

    return 0;
}

  1. build with bellow command

gcc -I ../hidapi/hidapi/ ../hidapi/libusb/hid.c  -lusb-1.0 hid_libusb.c -o ../build/hid_libusb
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.