views:

935

answers:

1

Hi

i have problem with libhid .

i found that there 2 way 4 accessing the usb-hid in linux

1)linux default libraries like input.h and hiddev.h and ...

2)using libhid

i found libhid some confusing and try to use input.h but i have problem with that 2.

i dont know how to get information about my device from ubuntu

i use open() to open the device

str="/dev/inpt/eventX" \\where X=0,1,...,7(I'm not sure about this)
open(str,O_RDWR)

then get info with ioctl

ioctl(fd,EVIOCGVERSION,&version);

but it give me wrong vendor and product IDs

then i try to use libhid but had know idea how to use libhid (or any other library) in eclipse or netbeans

can you tell me how you compiled your codes any IDE like eclipse or netbeans or just using terminal and gcc? or how to work with ioctl() and open() ?

my whole example code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ftw.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <asm/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
#include <linux/input.h>
#include <strings.h>


struct input_devinfo
{
        uint16_t bustype;
        uint16_t vendor;
        uint16_t product;
        uint16_t version;
};


int main(void) {
    //puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    //usb_device ud;
    //int i=0;
    //string str;
    //str=char[100];
    //str="/dev/input/event0\n";

    printf("------------- start -----------------\n");
    char str[]="" ;
    int version=0;
    char c[16];
    char t;
    int i,fd;
    //for (i=0 ; i<8 ; i++)
    {
     //strcpy(c,str);
     //t=i-'0';
     //printf("salam5\n");
     //c[15]=t;

     //openning
     //open(str,O_RDONLY);//read and write
     if ((fd = open(str,O_RDWR)) < 0)
      perror("str open\n");
     else
      printf("%s opened successfully\n",str);


     ioctl(fd,EVIOCGVERSION,&version);
     printf("version = %d \n",version);
     printf("evdev driver version is %d.%d.%d\n",version >> 16, (version >> 8) & 0xff, version & 0xff);

     //geting info from device
     struct input_devinfo device_info;
     ioctl(fd,EVIOCGID,&device_info);
     printf("vendor 0x%04hx product 0x%04hx version 0x%04hx is on ?",
      device_info.vendor, device_info.product,
      device_info.version);
    }



    return EXIT_SUCCESS;
}
A: 

I find a way to compile my code in eclipse

1 problem solved

to compile your code with GCC in terminal you should define libhid for GCC by adding "-lhid" to your command : gcc test_libhid.c -lhid

if your using eclipse and you want to use libhid with it you should add "-lhid" to gcc linker in order to gcc could use libhid when its compiling your code follow the steps:

1)on the project Explorer panel , R-click on your project select properties (last option) or select your project and press Alt+Enter

2)in the left panel expand "c/c++ build" and select "setting"

3)in the right side select "tool setting" tab

4)you should see GCC C compiler and GCC C linker and GCC assembler in there . expand GCC C linker and select Libraries

5)after selecting in the right side you should see 2 boxes: Libraries(-l) and Library search path(-L) in the Libraries(-l) add "hid"

note:eclipse use GCC to compile your codes when you do this steps eclipse add "-lhid" parameter to gcc to able it recognizing the libhid.