Skip to content

Commit c5c7238

Browse files
committed
Added a rudimentary mechanism for reacting to messages from the host
to the HID device. Fixes: #6930
1 parent cd798ab commit c5c7238

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

hardware/arduino/avr/libraries/HID/src/HID.cpp

+22-5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ int HID_::SendReport(uint8_t id, const void* data, int len)
9595
return ret + ret2;
9696
}
9797

98+
int HID_::RegisterReportReceiver(uint8_t id, void(*reportReceiver)(const void *data, int len))
99+
{
100+
this->receiverId = id;
101+
this->reportReceiver = reportReceiver;
102+
}
103+
98104
bool HID_::setup(USBSetup& setup)
99105
{
100106
if (pluggedInterface != setup.wIndex) {
@@ -133,13 +139,23 @@ bool HID_::setup(USBSetup& setup)
133139
}
134140
if (request == HID_SET_REPORT)
135141
{
136-
//uint8_t reportID = setup.wValueL;
137-
//uint16_t length = setup.wLength;
138-
//uint8_t data[length];
142+
if (this->reportReceiver == NULL)
143+
{
144+
return false;
145+
}
146+
uint8_t reportID = setup.wValueL;
147+
uint16_t length = setup.wLength;
148+
uint8_t data[16];
139149
// Make sure to not read more data than USB_EP_SIZE.
140150
// You can read multiple times through a loop.
141151
// The first byte (may!) contain the reportID on a multreport.
142-
//USB_RecvControl(data, length);
152+
int numRead = USB_RecvControl(data, length);
153+
if (numRead < 1 || data[0] != this->receiverId )
154+
{
155+
return false;
156+
}
157+
158+
(*this->reportReceiver)(data + 1, length - 1);
143159
}
144160
}
145161

@@ -148,7 +164,8 @@ bool HID_::setup(USBSetup& setup)
148164

149165
HID_::HID_(void) : PluggableUSBModule(1, 1, epType),
150166
rootNode(NULL), descriptorSize(0),
151-
protocol(HID_REPORT_PROTOCOL), idle(1)
167+
protocol(HID_REPORT_PROTOCOL), idle(1),
168+
receiverId(0), reportReceiver(NULL)
152169
{
153170
epType[0] = EP_TYPE_INTERRUPT_IN;
154171
PluggableUSB().plug(this);

hardware/arduino/avr/libraries/HID/src/HID.h

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class HID_ : public PluggableUSBModule
9696
int SendReport(uint8_t id, const void* data, int len);
9797
void AppendDescriptor(HIDSubDescriptor* node);
9898

99+
// Registers a callback for when a report with the given id occurs. You can only have
100+
// one callback registered. If you need to un-register a callback, supply 0 and NULL
101+
// to this method.
102+
int RegisterReportReceiver(uint8_t id, void(*reportReceiver)(const void *data, int len));
103+
99104
protected:
100105
// Implementation of the PluggableUSBModule
101106
int getInterface(uint8_t* interfaceCount);
@@ -111,6 +116,8 @@ class HID_ : public PluggableUSBModule
111116

112117
uint8_t protocol;
113118
uint8_t idle;
119+
uint8_t receiverId;
120+
void(*reportReceiver)(const void *data, int len);
114121
};
115122

116123
// Replacement for global singleton.

0 commit comments

Comments
 (0)