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
|
#include "StdAfx.h"
#include <iostream>
#include "Connection.h"
extern "C"{
#include "setupapi.h"
#include "hidsdi.h"
}
#pragma comment (lib, "hid.lib")
#pragma comment (lib, "setupAPI.lib")
struct {DWORD cbSize; char DevicePath[256];} FunctionClassDeviceData;
using namespace std;
Connection::Connection(void)
{
}
Connection::~Connection(void)
{
}
HANDLE Connection::connectToBoard(){
if (checkConnectionUSB() && h!=INVALID_HANDLE_VALUE){
cout<<"Plateau connecté en USB"<<endl;
}
else {
h = CreateFile("COM6",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (h!=INVALID_HANDLE_VALUE){
cout<<"Plateau connecté en BlueTooth"<<endl;
}
}
return h;
}
bool Connection::checkConnectionUSB(){
DWORD wmWritten;
char strData[] = "Test data written to explain the win32 file sample";
HANDLE HIDHandle;
bool success=false;
DWORD nbBytesReturned;
DWORD needed;
SP_DEVICE_INTERFACE_DETAIL_DATA buffer;
HIDD_ATTRIBUTES HIDAttributes;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
struct _GUID GUID;
HidD_GetHidGuid(&GUID);
DWORD i;
DWORD InterfaceNumber= 0;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(&GUID,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_INTERFACEDEVICE );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
cout<<"SetupDiGetClassDevs a generer une erreur "<<endl;
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
// Insert error handling here.
break;
}
}
_tprintf(TEXT("Device Number %i is: %s\n"),i, buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return false;
}
InterfaceNumber = 0; // this just returns the first one, you can iterate on this
for (InterfaceNumber=0;InterfaceNumber<8;InterfaceNumber++)
{
DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);
if (SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&GUID,InterfaceNumber,&DeviceInterfaceData))
{
printf("\nGot interface");
FunctionClassDeviceData.cbSize=5;
success=SetupDiGetDeviceInterfaceDetail(hDevInfo,&DeviceInterfaceData,(PSP_DEVICE_INTERFACE_DETAIL_DATA)(&FunctionClassDeviceData),256,&nbBytesReturned,NULL);
cout<<"SetupDiGetDeviceInterfaceDetail returns "<<success<< "codeError: "<< GetLastError()<<endl;
cout<<"nbBytesReturned= "<< nbBytesReturned<<endl;
cout<<"devicePath:"<<FunctionClassDeviceData.DevicePath<<endl;
if(success){
HIDHandle=CreateFile(FunctionClassDeviceData.DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
cout<<"CreateFile returns "<<HIDHandle<< "codeError: "<< GetLastError()<<endl;
if (HIDHandle!=INVALID_HANDLE_VALUE){
success = HidD_GetAttributes(HIDHandle, &HIDAttributes);
cout<<"HidD_GetAttributes returns "<<success<< "codeError: "<< GetLastError()<<endl;
if (!success){
printf("Could not get HID attributes\n"); return false;
}
else{
if( HIDAttributes.VendorID == 0x4242 && HIDAttributes.ProductID == 0xee01 ){
cout<<"On a le device en USB "<<endl;
h = HIDHandle;
success= WriteFile(h,strData,(DWORD)(sizeof(strData)),&wmWritten,NULL);
cout<<"WriteFile returns "<<success<< " codeError: "<< GetLastError()<<endl;
return true;
}
}
}
}
}
else
{
printf("\nNo interface");
if ( GetLastError() == ERROR_NO_MORE_ITEMS) printf(", since there are no more items found.\n");
else printf(", unknown reason.\n");
}
}
cout<<"\nLe device n'est pas connecté par usb!\n"<<endl;
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return false;
} |
Partager