Skip to content

Commit

Permalink
samples: hid-mouse: protect report buffer
Browse files Browse the repository at this point in the history
Use the message queue to pass the new report from the input callback,
and use a semaphore to protect the report buffer until it is transferred
to the host.

(cherry picked from commit 67a31ef)

Original-Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
GitOrigin-RevId: 67a31ef
Change-Id: I1cd577c19818038ad57b0a96a3cfd95cfe8f8bfa
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5688009
Tested-by: Yuval Peress <peress@google.com>
Reviewed-by: Yuval Peress <peress@google.com>
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Commit-Queue: Yuval Peress <peress@google.com>
  • Loading branch information
jfischer-no authored and Chromeos LUCI committed Jul 9, 2024
1 parent 9ef2c9d commit 27365ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions samples/subsys/usb/hid-mouse/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y

CONFIG_GPIO=y
CONFIG_INPUT=y
CONFIG_INPUT_MODE_SYNCHRONOUS=y
45 changes: 27 additions & 18 deletions samples/subsys/usb/hid-mouse/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ enum mouse_report_idx {
MOUSE_REPORT_COUNT = 4,
};

static uint8_t __aligned(sizeof(void *)) report[MOUSE_REPORT_COUNT];
static K_SEM_DEFINE(report_sem, 0, 1);
K_MSGQ_DEFINE(mouse_msgq, MOUSE_REPORT_COUNT, 2, 1);
static K_SEM_DEFINE(ep_write_sem, 0, 1);

static inline void status_cb(enum usb_dc_status_code status, const uint8_t *param)
{
Expand All @@ -57,9 +57,7 @@ static ALWAYS_INLINE void rwup_if_suspended(void)

static void input_cb(struct input_event *evt)
{
uint8_t tmp[MOUSE_REPORT_COUNT];

(void)memcpy(tmp, report, sizeof(tmp));
static uint8_t tmp[MOUSE_REPORT_COUNT];

switch (evt->code) {
case INPUT_KEY_0:
Expand Down Expand Up @@ -88,10 +86,13 @@ static void input_cb(struct input_event *evt)
return;
}

if (memcmp(tmp, report, sizeof(tmp))) {
memcpy(report, tmp, sizeof(report));
k_sem_give(&report_sem);
if (k_msgq_put(&mouse_msgq, tmp, K_NO_WAIT) != 0) {
LOG_ERR("Failed to put new input event");
}

tmp[MOUSE_X_REPORT_IDX] = 0U;
tmp[MOUSE_Y_REPORT_IDX] = 0U;

}

INPUT_CALLBACK_DEFINE(NULL, input_cb);
Expand Down Expand Up @@ -120,6 +121,16 @@ static int enable_usb_device_next(void)
}
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */

static void int_in_ready_cb(const struct device *dev)
{
ARG_UNUSED(dev);
k_sem_give(&ep_write_sem);
}

static const struct hid_ops ops = {
.int_in_ready = int_in_ready_cb,
};

int main(void)
{
const struct device *hid_dev;
Expand Down Expand Up @@ -148,7 +159,7 @@ int main(void)

usb_hid_register_device(hid_dev,
hid_report_desc, sizeof(hid_report_desc),
NULL);
&ops);

usb_hid_init(hid_dev);

Expand All @@ -163,19 +174,17 @@ int main(void)
}

while (true) {
k_sem_take(&report_sem, K_FOREVER);
uint8_t __aligned(sizeof(void *)) report[MOUSE_REPORT_COUNT];

k_msgq_get(&mouse_msgq, &report, K_FOREVER);

ret = hid_int_ep_write(hid_dev, report, sizeof(report), NULL);
report[MOUSE_X_REPORT_IDX] = 0U;
report[MOUSE_Y_REPORT_IDX] = 0U;
if (ret) {
LOG_ERR("HID write error, %d", ret);
}

/* Toggle LED on sent report */
ret = gpio_pin_toggle(led0.port, led0.pin);
if (ret < 0) {
LOG_ERR("Failed to toggle the LED pin, error: %d", ret);
} else {
k_sem_take(&ep_write_sem, K_FOREVER);
/* Toggle LED on sent report */
(void)gpio_pin_toggle(led0.port, led0.pin);
}
}
return 0;
Expand Down

0 comments on commit 27365ac

Please sign in to comment.