Skip to content

Commit b481411

Browse files
authored
Merge pull request #6767 from maximkulkin/usb-hid-get-last-report-fix
shared-module/usb_hid: Fix behavior of Device.get_last_received_report()
2 parents 4ae97be + aab5fac commit b481411

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

shared-module/usb_hid/Device.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdbool.h>
2728
#include <string.h>
2829

2930
#include "py/gc.h"
@@ -241,6 +242,10 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t *
241242
mp_obj_t common_hal_usb_hid_device_get_last_received_report(usb_hid_device_obj_t *self, uint8_t report_id) {
242243
// report_id has already been validated for this device.
243244
size_t id_idx = get_report_id_idx(self, report_id);
245+
if (!self->out_report_buffers_updated[id_idx]) {
246+
return mp_const_none;
247+
}
248+
self->out_report_buffers_updated[id_idx] = false;
244249
return mp_obj_new_bytes(self->out_report_buffers[id_idx], self->out_report_lengths[id_idx]);
245250
}
246251

@@ -258,6 +263,7 @@ void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self) {
258263
? gc_alloc(self->out_report_lengths[i], false, true /*long-lived*/)
259264
: NULL;
260265
}
266+
memset(self->out_report_buffers_updated, 0, sizeof(self->out_report_buffers_updated));
261267
}
262268

263269

@@ -304,6 +310,7 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep
304310
hid_device->out_report_buffers[id_idx] &&
305311
hid_device->out_report_lengths[id_idx] >= bufsize) {
306312
memcpy(hid_device->out_report_buffers[id_idx], buffer, bufsize);
313+
hid_device->out_report_buffers_updated[id_idx] = true;
307314
}
308315
}
309316
}

shared-module/usb_hid/Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct {
3838
const uint8_t *report_descriptor;
3939
uint8_t *in_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
4040
uint8_t *out_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
41+
uint8_t out_report_buffers_updated[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
4142
uint16_t report_descriptor_length;
4243
uint8_t report_ids[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];
4344
uint8_t in_report_lengths[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR];

0 commit comments

Comments
 (0)