From 287a0c9d4380fac1853f66715d64af6370affc32 Mon Sep 17 00:00:00 2001 From: Maxim Kulkin Date: Mon, 15 Aug 2022 20:46:28 -0400 Subject: [PATCH] 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 a bitfield that stores flag if report with particular ID was received and updates get_last_received_report() to match its documentation. --- shared-module/usb_hid/Device.c | 18 ++++++++++++++++++ shared-module/usb_hid/Device.h | 1 + 2 files changed, 19 insertions(+) diff --git a/shared-module/usb_hid/Device.c b/shared-module/usb_hid/Device.c index f92c93baa3540..0bc65036d836e 100644 --- a/shared-module/usb_hid/Device.c +++ b/shared-module/usb_hid/Device.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include #include #include "py/gc.h" @@ -121,6 +122,18 @@ static const uint8_t mouse_report_descriptor[] = { 0xC0, // End Collection }; +static inline bool bitset_is_set(const uint8_t* bitset, int index) { + return (bitset[index >> 3] & (1 << (index & 0x7))) != 0; +} + +static inline void bitset_set(uint8_t* bitset, int index) { + bitset[index >> 3] |= (1 << (index & 0x7)); +} + +static inline void bitset_clear(uint8_t* bitset, int index) { + bitset[index >> 3] &= ~(1 << (index & 0x7)); +} + const usb_hid_device_obj_t usb_hid_device_mouse_obj = { .base = { .type = &usb_hid_device_type, @@ -241,6 +254,9 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t * mp_obj_t common_hal_usb_hid_device_get_last_received_report(usb_hid_device_obj_t *self, uint8_t report_id) { // report_id has already been validated for this device. size_t id_idx = get_report_id_idx(self, report_id); + if (!bitset_is_set(self->out_report_buffers_updated, id_idx)) + return mp_const_none; + bitset_clear(self->out_report_buffers_updated, id_idx); return mp_obj_new_bytes(self->out_report_buffers[id_idx], self->out_report_lengths[id_idx]); } @@ -258,6 +274,7 @@ void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self) { ? gc_alloc(self->out_report_lengths[i], false, true /*long-lived*/) : NULL; } + memset(self->out_report_buffers_updated, 0, sizeof(self->out_report_buffers_updated)); } @@ -304,6 +321,7 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t rep hid_device->out_report_buffers[id_idx] && hid_device->out_report_lengths[id_idx] >= bufsize) { memcpy(hid_device->out_report_buffers[id_idx], buffer, bufsize); + bitset_set(hid_device->out_report_buffers_updated, id_idx); } } } diff --git a/shared-module/usb_hid/Device.h b/shared-module/usb_hid/Device.h index f265712be41e3..8254eb72a8a0e 100644 --- a/shared-module/usb_hid/Device.h +++ b/shared-module/usb_hid/Device.h @@ -38,6 +38,7 @@ typedef struct { const uint8_t *report_descriptor; uint8_t *in_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; uint8_t *out_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; + uint8_t out_report_buffers_updated[(CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR + 7) / 8]; uint16_t report_descriptor_length; uint8_t report_ids[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; uint8_t in_report_lengths[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy