Witam,
Próbuje stworzyć urządzenie USB, które w zaprogramowanych odstępach czasu spowodowało by wciśnięcie zaprogramowanej sekwencji klawiszy, oraz aplikację w C# która umożliwiała by zmianą parametrów urządzenia.
Chodzi o to aby ktoś z aplikacją mógł odpowiednio skonfigurować urządzenie a potem ktokolwiek kto tylko podłączy je do swojego komputera mógł już się cieszyć użytkowaniem.
Mam już w zasadzie gotowe urządzenie - manipuluje już tylko deskryptorami.
Urządzenie pojawia się jako: "Urządzenie wejściowe USB" w skład którego wchodzą "Urządzenie klawiatury HID" oraz "Urządzenie zgodne z HID".
Problem polega na tym, że nie potrafię z poziomu aplikacji ( C# ), komunikować się z tym urządzeniem.
Część odpowiedzialna za symulację wciśniętych klawiszy działa dobrze.
Gdy wyrzucę z deskryptorów część odpowiedzialną za klawiaturę ( czyli zostanie samo "Urządzenie zgodne z HID"), to komunikacja z aplikacją działa bez zarzutu.
Korzystam z biblioteki HIDLibrary.
O to fragment kodu odpowiedzialny za komunikację:
//Try To Enumerate Device
Device = HidDevices.Enumerate(VendorID, ProductID).FirstOrDefault();
if (Device == null)
{
return;
}
//Open Device
Device.OpenDevice();
//if( Device.IsOpen == false )
// return;
Device.Inserted += DeviceAttachedHandler;
Device.Removed += DeviceRemovedHandler;
Device.MonitorDeviceEvents = true;
//Handle Reports
KeyMapRequest(); //Fill Buffer with KeyMapRequest
HidReport report = Device.CreateReport();
report.ReportId = 2;
report.Data = USBBuffer;
succeed = Device.WriteReport(report, 10);
Device.ReadReport(OnReport);
LastDataEventDate = DateTime.Now;
while ((DateTime.Now - LastDataEventDate).TotalMilliseconds < 100)
{
}
Gdyby podczas pracy podejrzeć "Device.IsOpen":
- w przypadku pełnego urządzenia: Device.IsOpen == true
- w przypadku samego "Urządzenie zgodne z HID": Device.IsOpen == false
Czy Windows czasem nie blokuje dostępu do urządzeń typu Klawiatura??
Albo może mam coś nie tak w deskryptorach?
Deskryptory raportu:
//Keyboard Simulator
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x01, // REPORT_ID (1)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0, // END_COLLECTION
/*65*/
//Device Configuration
0x06, 0xFF, 0xA0, // USAGE_PAGE (Vendor-Defined)
0x09, 0x01, // USAGE (Vendor-Defined)
0xA1, 0x01, // COLLECTION (Application)
0x85, 0x02, // REPORT_ID (2)
0x09, 0x03, // USAGE (Vendor-Defined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0x00, 0xFF, // LOGICAL_MAXIMUM (255)
0x95, 0x2D, // REPORT_COUNT (45)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x02, // Input (Data, Variable, Absolute)
0x09, 0x04, // USAGE (Vendor-Defined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0x00, 0xFF, // LOGICAL_MAXIMUM (255)
0x95, 0x2D, // REPORT_COUNT (45)
0x75, 0x08, // REPORT_SIZE (8)
0x91, 0x02, // Output (Data, Variable, Absolute)
0x09, 0x05, // USAGE (Vendor-Defined)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0x00, 0xFF, // LOGICAL_MAXIMUM (255)
0x95, 0x2D, // REPORT_COUNT (45)
0x75, 0x08, // REPORT_SIZE (8)
0xB1, 0x02, // Feature (Data, Variable, Absolute)
0xc0, // END_COLLECTION
deskryptor konfiguracyjny:
0x09, /* bLength: Configuration Descriptor size */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
CUSTOMHID_SIZ_CONFIG_DESC,
/* wTotalLength: Bytes returned */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing
the configuration*/
0xA0, /* bmAttributes: Bus powered */
0xC8, /* MaxPower 100 mA: this current is used for detecting Vbus */
/************** Descriptor of Custom HID interface ****************/
/* 09 */
0x09, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor */
/******************** Descriptor of Custom HID HID ********************/
/* 18 */
0x09, /* bLength: HID Descriptor size */
HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
0x10, /* bcdHID: HID Class Spec release number */
0x01,
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
KeyboardReportDescriptorSize,/* wItemLength: Total length of Report descriptor */
0x00,
/******************** Descriptor of Custom HID endpoints ******************/
/* 27 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x81, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
0x40, /* wMaxPacketSize: 64 Bytes max */
0x00,
0x20, /* bInterval: Polling Interval (10 ms) */
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x01, /* bEndpointAddress: Endpoint Address (OUT) */
0x03, /* bmAttributes: Interrupt endpoint */
0x40, /* wMaxPacketSize: 64 Bytes max */
0x00,
0x20, /* bInterval: Polling Interval (10 ms) */
/* 41 */