From 395ffaa790e396130c40c6d2e4842effa36d8f19 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Fri, 5 Jan 2024 18:57:52 +0000 Subject: [PATCH] fix(ble): Properly send mouse HoG using worker. * Properly send mouse HoG reports using our worker to avoid thread issues. --- app/src/hog.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/app/src/hog.c b/app/src/hog.c index b82f38f422a..204609145c2 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -373,26 +373,25 @@ void send_mouse_report_callback(struct k_work *work) { } }; -int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) { - struct bt_conn *conn = destination_connection(); - if (conn == NULL) { - return 1; - } +K_WORK_DEFINE(hog_mouse_work, send_mouse_report_callback); - struct bt_gatt_notify_params notify_params = { - .attr = &hog_svc.attrs[13], - .data = report, - .len = sizeof(*report), - }; - - int err = bt_gatt_notify_cb(conn, ¬ify_params); - if (err == -EPERM) { - bt_conn_set_security(conn, BT_SECURITY_L2); - } else if (err) { - LOG_DBG("Error notifying %d", err); +int zmk_hog_send_mouse_report(struct zmk_hid_mouse_report_body *report) { + int err = k_msgq_put(&zmk_hog_mouse_msgq, report, K_MSEC(100)); + if (err) { + switch (err) { + case -EAGAIN: { + LOG_WRN("Consumer message queue full, popping first message and queueing again"); + struct zmk_hid_mouse_report_body discarded_report; + k_msgq_get(&zmk_hog_mouse_msgq, &discarded_report, K_NO_WAIT); + return zmk_hog_send_mouse_report(report); + } + default: + LOG_WRN("Failed to queue mouse report to send (%d)", err); + return err; + } } - bt_conn_unref(conn); + k_work_submit_to_queue(&hog_work_q, &hog_mouse_work); return 0; };