8000 shared-module/usb_hid: Fix behavior of Device.get_last_received_report() · squix78/circuitpython@429c722 · GitHub
[go: up one dir, main page]

Skip to content

Commit 429c722

Browse files
committed
shared-module/usb_hid: Fix behavior of Device.get_last_received_report()
Documentation states that get_last_received_report() function should return None if there was no report received previously, otherwise it should return report. Moreover, same report should be returned only once. That makes it possible to reliably process incoming OUT/Feature reports. This patch adds an array that stores flags if report with particular ID was received and updates get_last_received_report() to match its documentation.
1 parent 4a5790b commit 429c722

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"
@@ -246,6 +247,10 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t *
246247
mp_obj_t common_hal_usb_hid_device_get_last_received_report(usb_hid_device_obj_t *self, uint8_t report_id) {
247248
// report_id has already been validated for this device.
248249
size_t id_idx = get_report_id_idx(self, report_id);
250+
if (!self->out_report_buffers_updated[id_idx]) {
251+
return mp_const_none;
252+
}
253+
self->out_report_buffers_updated[id_idx] = false;
249254
return mp_obj_new_bytes(self->out_report_buffers[id_idx], self->out_report_lengths[id_idx]);
250255
}
251256

@@ -263,6 +268,7 @@ void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self) {
263268
? gc_alloc(self->out_report_lengths[i], false, true /*long-lived*/)
264269
: NULL;
265270
}
271+
memset(self->out_report_buffers_updated, 0, sizeof(self->out_report_buffers_updated));
266272
}
267273

268274

@@ -309,6 +315,7 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep
309315
hid_device->out_report_buffers[id_idx] &&
310316
hid_device->out_report_lengths[id_idx] >= bufsize) {
311317
memcpy(hid_device->out_report_buffers[id_idx], buffer, bufsize);
318+
hid_device->out_report_buffers_updated[id_idx] = true;
312319
}
313320
}
314321
}

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)
0