1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| #include <stdio.h>
#include <errno.h>
#include <usb.h>
// Clé 1 Go
//#define VENDOR_ID 0x13FE
//#define PRODUCT_ID 0x1A23
// Carte test
#define VENDOR_ID 0x04D8
#define PRODUCT_ID 0x000C
#define SIZE 32
#define IN_EP 0x81
#define OUT_EP 0x01
#define READ_VERSION 0x00
#define WRITE_READ_DATA 0x04
#define SET_LED 0x06
#define SET_OUT 0x40
#define READ_IN 0x50
#define UsbBufSize 64
#define TIMEOUT 1000
struct struct_usb_data {
int command;
int data_length;
char data[UsbBufSize-1];
};
void write_data (usb_dev_handle *udev, int led_num, int led_status){
struct struct_usb_data usb_data;
int bytes_written;
char test;
test=0x40;
usb_data.command = SET_OUT;
switch (led_num) {
case 1:
if(led_status == 0 || led_status == 1){
usb_data.data[1]=led_status;}
else{
printf("The LED status is incorrect\n");}
usb_data.data[0]=1;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
default:
printf("This LED number does not exist.\n");
break;
} // End of the switch
usb_data.data_length=4;
// usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout)
printf("BEFORE THE WRITE\n");
printf("DATA 0 %s DATA 1 %s\n",usb_data.data[0],usb_data.data[1]);
bytes_written = usb_bulk_write(udev, OUT_EP, &test, usb_data.data_length, TIMEOUT);
if (bytes_written < 0){
printf("Impossible to write %s\n",usb_strerror());}
else{
printf("Command sent. %d bytes written\n",bytes_written);}
}
///////////////////////////////////////////////////////////////////////////////////////
// //
// MAIN //
// //
///////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]){
int nbr_bus_change;
int nbr_device_change;
struct usb_bus *busses;
struct usb_bus *bus;
int retval = 0;
// Initialize the internal USB structures
usb_init();
// Indicate the number of bus change since the last connection
nbr_bus_change = usb_find_busses();
/*if (nbr_bus_change==0){
printf("No busses change\n");
}
else {
printf("%d change(s) of busses\n",nbr_bus_change);
}*/
// Indicate the number of device change since the last connection
nbr_device_change = usb_find_devices();
/*if (nbr_device_change==0){
printf("No device change\n");
}
else {
printf("%d change(s) of device\n",nbr_device_change);
}*/
// Return the found USB list
busses = usb_get_busses();
// For each busses
for (bus = busses; bus; bus = bus->next) {
struct usb_device *dev; // Declaration of a device struct
// For each device of each busses
for (dev = bus->devices; dev; dev = dev->next) {
usb_dev_handle *open_device; // Declaration of the current opened device
// If it is the device that we are looking for
if (dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct == PRODUCT_ID) {
printf("Device Found\n");
// Opening of the device
open_device = usb_open(dev);
if (!open_device) {
printf("Failed to open device\n");
exit(1);}
printf("Opened device\n");
// Choose the first configuration (1)
// Activation of the configuration
if (usb_set_configuration(open_device,1) < 0) {
printf("Impossible to activate the configuration : %s\n",usb_strerror());
exit(1);
}
printf("Configuration activated\n");
//Choose the first interface (0)
retval = usb_claim_interface(open_device,1);
if (retval == -EBUSY) {
printf("Interface busy\n");
exit(1);}
else if (retval == -ENOMEM) {
printf("Insufficient memory on the device\n");
exit(1);}
printf("Device ready ...\n");
write_data(open_device,1,1);
if(usb_release_interface(open_device,0) == 0){
printf("Device released.\n");}
else{
printf("The device cannot be released : %s\n",usb_strerror());}
if(usb_close(open_device) == 0) {
printf("Device correctly closed\n");}
else {
printf("Device cannot be closed : %s\n",usb_strerror());
exit(1);}
exit(0);
}
/*else {
printf("Next device\n");
continue;
}*/
}
}
printf("No more device\n");
exit(1);
} |
Partager