1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """A HID-class echo device.
7 This module provides a HID feature and HID device that can be used as an
8 echo test for HID drivers. The device exposes vendor-specific input, output
9 and feature usages that transmit 8 bytes of data. Data written sent as an
10 output report is echoed as an input report. The value of the feature report
11 can be written and read with control transfers.
17 import hid_descriptors
22 class EchoFeature(hid_gadget
.HidFeature
):
24 REPORT_DESC
= hid_descriptors
.ReportDescriptor(
25 hid_descriptors
.UsagePage(0xFF00), # Vendor Defined
26 hid_descriptors
.Usage(0),
27 hid_descriptors
.Collection(
28 hid_constants
.CollectionType
.APPLICATION
,
29 hid_descriptors
.LogicalMinimum(0, force_length
=1),
30 hid_descriptors
.LogicalMaximum(255, force_length
=2),
31 hid_descriptors
.ReportSize(8),
32 hid_descriptors
.ReportCount(8),
33 hid_descriptors
.Usage(0),
34 hid_descriptors
.Input(hid_descriptors
.Data
,
35 hid_descriptors
.Variable
,
36 hid_descriptors
.Absolute
),
37 hid_descriptors
.Usage(0),
38 hid_descriptors
.Output(hid_descriptors
.Data
,
39 hid_descriptors
.Variable
,
40 hid_descriptors
.Absolute
),
41 hid_descriptors
.Usage(0),
42 hid_descriptors
.Feature(hid_descriptors
.Data
,
43 hid_descriptors
.Variable
,
44 hid_descriptors
.Absolute
)
49 super(EchoFeature
, self
).__init
__()
50 self
._input
_output
_report
= 0
51 self
._feature
_report
= 0
53 def SetInputReport(self
, data
):
54 self
._input
_output
_report
, = struct
.unpack('<Q', data
)
55 self
.SendReport(struct
.pack('<Q', self
._input
_output
_report
))
58 def SetOutputReport(self
, data
):
59 self
._input
_output
_report
, = struct
.unpack('<Q', data
)
60 self
.SendReport(struct
.pack('<Q', self
._input
_output
_report
))
63 def SetFeatureReport(self
, data
):
64 self
._feature
_report
, = struct
.unpack('<Q', data
)
67 def GetInputReport(self
):
68 return struct
.pack('<Q', self
._input
_output
_report
)
70 def GetOutputReport(self
):
71 return struct
.pack('<Q', self
._input
_output
_report
)
73 def GetFeatureReport(self
):
74 return struct
.pack('<Q', self
._feature
_report
)
77 class EchoGadget(hid_gadget
.HidGadget
):
80 self
._feature
= EchoFeature()
81 super(EchoGadget
, self
).__init
__(
82 report_desc
=EchoFeature
.REPORT_DESC
,
83 features
={0: self
._feature
},
87 vendor_id
=usb_constants
.VendorID
.GOOGLE
,
88 product_id
=usb_constants
.ProductID
.GOOGLE_HID_ECHO_GADGET
,
89 device_version
=0x0100)
90 self
.AddStringDescriptor(1, 'Google Inc.')
91 self
.AddStringDescriptor(2, 'HID Echo Gadget')
94 def RegisterHandlers():
95 from tornado
import web
97 class WebConfigureHandler(web
.RequestHandler
):
100 server
.SwitchGadget(EchoGadget())
103 server
.app
.add_handlers('.*$', [
104 (r
'/hid_echo/configure', WebConfigureHandler
),