Blame view

src/main.c 4.04 KB
b24d2980   achemin1   Nettoyage du code...
1
  #include "libusb_wrapper.h"
d6e16d2e   achemin1   Tout propre
2
  #include <stdio.h>
4d621b43   achemin1   ajout interface d...
3
  #include <string.h>
b24d2980   achemin1   Nettoyage du code...
4
  
7d6f5a45   achemin1   add begin of main...
5
6
7
  #define VID 1003
  #define PID 8259
  
f6896773   achemin1   Prise en charge d...
8
  int main(int argc, char *argv[]){
d6e16d2e   achemin1   Tout propre
9
  	printf("Hello World :-|\n");
b24d2980   achemin1   Nettoyage du code...
10
  
d6e16d2e   achemin1   Tout propre
11
      libusb_context *context;
f6896773   achemin1   Prise en charge d...
12
      usbinit(&context);
7e2160f8   achemin1   Ajout du mode debug
13
14
15
      #ifdef DEBUG
      libusb_set_option(context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
      #endif
b24d2980   achemin1   Nettoyage du code...
16
  
f6896773   achemin1   Prise en charge d...
17
18
19
20
21
22
  	if(argc>1){
  	    //command mode
          if(strcmp(argv[1], "demo4.1") == 0) { //if 4.1
              printf("\e[91mDemo 4.1\e[39m\n");
              displayDevices(context);
          }
72cd0d89   achemin1   (Ré)Ajout 4.1 bonus
23
24
25
26
          else if(strcmp(argv[1], "demo4.1plus") == 0) { //if 4.1 bonus
              printf("\e[91mDemo 4.1\e[39m\n");
              displayDevicesMore(context);
          }
f6896773   achemin1   Prise en charge d...
27
28
          else if(strcmp(argv[1], "demo4.2") == 0){//if 4.2
              printf("\e[91mDemo 4.2\e[39m\n");
4d621b43   achemin1   ajout interface d...
29
30
31
32
33
34
35
36
37
38
39
  
              if(argc > 3){
                  uint16_t vendor_id;
                  uint16_t product_id;
                  sscanf(argv[2], "%hu", &vendor_id);
                  sscanf(argv[3], "%hu", &product_id);
  
                  libusb_device_handle* handle;
                  handle = libusb_open_device_with_vid_pid(context, vendor_id, product_id);
                  if(handle != NULL){
                      printf("Show endpoints of VID:%hu;PID:%hu\n", vendor_id, product_id);
7d6f5a45   achemin1   add begin of main...
40
                      displayDeviceEndpoints(handle);
4d621b43   achemin1   ajout interface d...
41
42
43
44
45
46
47
                  }
                  else{
                      printf("Error while getting handle of VID:%hu;PID:%hu\n", vendor_id, product_id);
                  }
              }else{
                  printf("Error, need VID and PID\n");
              }
f6896773   achemin1   Prise en charge d...
48
49
50
51
52
53
54
          }else{
              printf("Wrong command\n");
          }
  
  	}else{
          //interactive mode
          printf("Welcome to interactive mode\n");
7d6f5a45   achemin1   add begin of main...
55
56
57
58
59
60
61
62
63
64
65
66
          printf("Compiled for VID:PID => %d:%d\n", VID, PID);
  
          //Find the device
          printf("Iterate list of devices, and getting the right one\n");
          libusb_device *device = NULL;
          getFirstDeviceFromID(context, VID, PID, &device);
          //code
          if(device==NULL){
              printf("Error : cannot find the device !\n");
              return 1;
          }
  
9424ae9a   achemin1   ajouts claim inte...
67
68
69
70
71
72
73
          //take the handle
          libusb_device_handle *device_handle = NULL;
          if(libusb_open(device, &device_handle) != 0){
              printf("Error : cannot handle the device\n");
              return 1;
          }
  
7d6f5a45   achemin1   add begin of main...
74
          //Take the interfaces
f492428d   achemin1   meilleurs printf
75
          printf("\nSearch for interfaces\n");
9424ae9a   achemin1   ajouts claim inte...
76
77
78
79
80
81
          struct libusb_interface * int_hidjoy = NULL;
          struct libusb_interface * int_leds = NULL;
          struct libusb_interface * int_vibrators = NULL;
          getOurInterfaces(device, &int_hidjoy, &int_leds, &int_vibrators);
  
          //Claim interface
f492428d   achemin1   meilleurs printf
82
          printf("\nClaim interfaces ...\n");
9424ae9a   achemin1   ajouts claim inte...
83
84
85
86
87
          printf("-Leds\n");
          interfaceclaim(device_handle, int_leds);
          printf("-Vibrators\n");
          interfaceclaim(device_handle, int_vibrators);
  
47ec0d5e   achemin1   récupération des ...
88
89
90
91
92
93
94
95
96
97
          //récupération des endpoints
          printf("\nRécupération endpoints\n");
          printf("-Leds");
          int endpoint_leds=getOnlyEndpoint(int_leds);   /* ID of endpoint (bit 8 is 0) */
          printf(" is %d\n", endpoint_leds);
          printf("-Vibrators");
          int endpoint_vibrators=getOnlyEndpoint(int_vibrators);   /* ID of endpoint (bit 8 is 0) */
          printf(" is %d\n", endpoint_vibrators);
  
  
8b5e1e86   achemin1   fix ajout d'un pr...
98
          /*LE CODE UTILE*/
0cd64dcd   gperson   communication ave...
99
100
101
102
  while(1){
          unsigned char data[2] = {0xff,0xff};    // data to send or to receive
          int size=2;           // size to send or maximum size to receive
          int timeout=1000;        // timeout in ms
47ec0d5e   achemin1   récupération des ...
103
104
105
  
          //OUT interrupt, from host to device
          int bytes_out;
0cd64dcd   gperson   communication ave...
106
107
  	printf("%p %02X\n",device_handle,endpoint_leds);
          int status=libusb_interrupt_transfer(device_handle,endpoint_leds,data,size,&bytes_out,timeout);
47ec0d5e   achemin1   récupération des ...
108
          if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
0cd64dcd   gperson   communication ave...
109
  }
47ec0d5e   achemin1   récupération des ...
110
          /*FIN DU CODE UTILE*/
8b5e1e86   achemin1   fix ajout d'un pr...
111
  
9424ae9a   achemin1   ajouts claim inte...
112
          //Close
f492428d   achemin1   meilleurs printf
113
          printf("\nUnclaim interfaces ...\n");
9424ae9a   achemin1   ajouts claim inte...
114
115
          printf("-Leds\n");
          interfaceclose(device_handle, int_leds);
8b5e1e86   achemin1   fix ajout d'un pr...
116
          printf("-Vibrators\n");
9424ae9a   achemin1   ajouts claim inte...
117
          interfaceclose(device_handle, int_vibrators);
f6896773   achemin1   Prise en charge d...
118
  
f6896773   achemin1   Prise en charge d...
119
120
121
  
          printf("Finished\n");
  	}
b24d2980   achemin1   Nettoyage du code...
122
  
b24d2980   achemin1   Nettoyage du code...
123
  
d6e16d2e   achemin1   Tout propre
124
  	usbclose(context);
b24d2980   achemin1   Nettoyage du code...
125
126
127
  
  	return 0;
  }