Skip to content

Commit f6774d5

Browse files
Tails86hathach
authored andcommitted
Implemented tuh_hid_send_report
1 parent be21413 commit f6774d5

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/class/hid/hid_host.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* The MIT License (MIT)
33
*
44
* Copyright (c) 2019 Ha Thach (tinyusb.org)
@@ -274,7 +274,49 @@ bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance)
274274
// return !usbh_edpt_busy(dev_addr, hid_itf->ep_in);
275275
//}
276276

277-
//void tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len);
277+
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len)
278+
{
279+
TU_LOG2("HID Send Report %d\r\n", report_id);
280+
281+
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
282+
283+
if (hid_itf->ep_out == 0)
284+
{
285+
// This HID does not have an out endpoint (other than control)
286+
return false;
287+
}
288+
else if (len > CFG_TUH_HID_EPOUT_BUFSIZE
289+
|| (report_id != 0 && len > (CFG_TUH_HID_EPOUT_BUFSIZE - 1)))
290+
{
291+
// ep_out buffer is not large enough to hold contents
292+
return false;
293+
}
294+
295+
// claim endpoint
296+
TU_VERIFY( usbh_edpt_claim(dev_addr, hid_itf->ep_out) );
297+
298+
if (report_id == 0)
299+
{
300+
// No report ID in transmission
301+
memcpy(&hid_itf->epout_buf[0], report, len);
302+
}
303+
else
304+
{
305+
hid_itf->epout_buf[0] = report_id;
306+
memcpy(&hid_itf->epout_buf[1], report, len);
307+
++len; // 1 more byte for report_id
308+
}
309+
310+
TU_LOG3_MEM(hid_itf->epout_buf, len, 2);
311+
312+
if ( !usbh_edpt_xfer(dev_addr, hid_itf->ep_out, hid_itf->epout_buf, len) )
313+
{
314+
usbh_edpt_release(dev_addr, hid_itf->ep_out);
315+
return false;
316+
}
317+
318+
return true;
319+
}
278320

279321
//--------------------------------------------------------------------+
280322
// USBH API
@@ -349,7 +391,7 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
349391
hidh_device_t* hid_dev = get_dev(dev_addr);
350392
TU_ASSERT(hid_dev->inst_count < CFG_TUH_HID, 0);
351393

352-
hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
394+
hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
353395

354396
//------------- Endpoint Descriptors -------------//
355397
p_desc = tu_desc_next(p_desc);

src/class/hid/hid_host.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* The MIT License (MIT)
33
*
44
* Copyright (c) 2019 Ha Thach (tinyusb.org)
@@ -106,7 +106,7 @@ bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance);
106106

107107
// Send report using interrupt endpoint
108108
// If report_id > 0 (composite), it will be sent as 1st byte, then report contents. Otherwise only report content is sent.
109-
//void tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len);
109+
bool tuh_hid_send_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t const* report, uint16_t len);
110110

111111
//--------------------------------------------------------------------+
112112
// Callbacks (Weak is optional)

0 commit comments

Comments
 (0)