From 480c3386b44b966537e1e8a5a01241a81222c5ef Mon Sep 17 00:00:00 2001 From: Pawel Dunaj Date: Tue, 10 Dec 2024 10:44:48 +0100 Subject: [PATCH] subsys: bluetooth: User data in HID service callback Allow user to set argument with which notification completion callback is called. Signed-off-by: Pawel Dunaj --- include/bluetooth/services/hids.h | 30 +++++++++++++++++++++++++++--- subsys/bluetooth/services/hids.c | 9 +++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/include/bluetooth/services/hids.h b/include/bluetooth/services/hids.h index abe2f8fa787a..c53405d0e7e2 100644 --- a/include/bluetooth/services/hids.h +++ b/include/bluetooth/services/hids.h @@ -532,6 +532,26 @@ int bt_hids_connected(struct bt_hids *hids_obj, struct bt_conn *conn); */ int bt_hids_disconnected(struct bt_hids *hids_obj, struct bt_conn *conn); +/** @brief Send Input Report, operation complete callback has user data as argument. + * + * @note The function is not thread safe. + * It cannot be called from multiple threads at the same time. + * + * @param hids_obj Pointer to HIDS instance. + * @param conn Pointer to Connection Object. + * @param rep_index Index of report descriptor. + * @param rep Pointer to the report data. + * @param len Length of report data. + * @param cb Notification complete callback (can be NULL). + * @param userdata Argument passed to notificaion complete callback. + * + * @return 0 If the operation was successful. Otherwise, a (negative) error + * code is returned. + */ +int bt_hids_inp_rep_send_userdata(struct bt_hids *hids_obj, struct bt_conn *conn, + uint8_t rep_index, uint8_t const *rep, uint8_t len, + bt_gatt_complete_func_t cb, void *userdata); + /** @brief Send Input Report. * * @note The function is not thread safe. @@ -543,13 +563,17 @@ int bt_hids_disconnected(struct bt_hids *hids_obj, struct bt_conn *conn); * @param rep Pointer to the report data. * @param len Length of report data. * @param cb Notification complete callback (can be NULL). + * @param userdata Argument passed to notificaion complete callback. * * @return 0 If the operation was successful. Otherwise, a (negative) error * code is returned. */ -int bt_hids_inp_rep_send(struct bt_hids *hids_obj, struct bt_conn *conn, - uint8_t rep_index, uint8_t const *rep, uint8_t len, - bt_gatt_complete_func_t cb); +static inline int bt_hids_inp_rep_send(struct bt_hids *hids_obj, struct bt_conn *conn, + uint8_t rep_index, uint8_t const *rep, uint8_t len, + bt_gatt_complete_func_t cb) +{ + return bt_hids_inp_rep_send_userdata(hids_obj, conn, rep_index, rep, len, cb, NULL); +} /** @brief Send Boot Mouse Input Report. * diff --git a/subsys/bluetooth/services/hids.c b/subsys/bluetooth/services/hids.c index c62f6bb37ea4..1a2371268f55 100644 --- a/subsys/bluetooth/services/hids.c +++ b/subsys/bluetooth/services/hids.c @@ -1106,10 +1106,10 @@ static int inp_rep_notify_all(struct bt_hids *hids_obj, } } -int bt_hids_inp_rep_send(struct bt_hids *hids_obj, - struct bt_conn *conn, uint8_t rep_index, - uint8_t const *rep, uint8_t len, - bt_gatt_complete_func_t cb) +int bt_hids_inp_rep_send_userdata(struct bt_hids *hids_obj, + struct bt_conn *conn, uint8_t rep_index, + uint8_t const *rep, uint8_t len, + bt_gatt_complete_func_t cb, void *userdata) { struct bt_hids_inp_rep *hids_inp_rep = &hids_obj->inp_rep_group.reports[rep_index]; @@ -1147,6 +1147,7 @@ int bt_hids_inp_rep_send(struct bt_hids *hids_obj, params.data = rep; params.len = hids_inp_rep->size; params.func = cb; + params.user_data = userdata; int err = bt_gatt_notify_cb(conn, ¶ms);