Skip to content

Commit

Permalink
bluetooth: services: pass information on report id in callbacks
Browse files Browse the repository at this point in the history
Add information about report id as argument to the callbacks.
The old callback format is kept for backwards compatibility.

Signed-off-by: Pawel Dunaj <[email protected]>
  • Loading branch information
pdunaj committed Dec 20, 2024
1 parent 3665a25 commit fe1e80f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
37 changes: 35 additions & 2 deletions include/bluetooth/services/hids.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ struct bt_hids_rep {
*/
typedef void (*bt_hids_notify_handler_t) (enum bt_hids_notify_evt evt);

/** @brief HID notification event handler, with report identification.
*
* @param report_id Report ID defined in the HIDS Report Map.
* @param evt Notification event.
*/
typedef void (*bt_hids_notify_ext_handler_t) (uint8_t report_id, enum bt_hids_notify_evt evt);

/** @brief HID Report event handler.
*
* @param rep Pointer to the report descriptor.
Expand All @@ -177,6 +184,18 @@ typedef void (*bt_hids_rep_handler_t) (struct bt_hids_rep *rep,
struct bt_conn *conn,
bool write);

/** @brief HID Report event handler, with report identification.
*
* @param report_id Report ID defined in the HIDS Report Map.
* @param rep Pointer to the report descriptor.
* @param conn Pointer to Connection Object.
* @param write @c true if handler is called for report write.
*/
typedef void (*bt_hids_rep_ext_handler_t) (uint8_t report_id,
struct bt_hids_rep *rep,
struct bt_conn *conn,
bool write);

/** @brief Input Report.
*/
struct bt_hids_inp_rep {
Expand Down Expand Up @@ -225,8 +244,15 @@ struct bt_hids_inp_rep {
*/
const uint8_t *rep_mask;

/** Callback with the notification event. */
/** Callback with the notification event.
* Used if set and extended callback is not set.
*/
bt_hids_notify_handler_t handler;

/** Extended callback with the notification event.
* Has preference over the normal callback.
*/
bt_hids_notify_ext_handler_t handler_ext;
};


Expand Down Expand Up @@ -263,8 +289,15 @@ struct bt_hids_outp_feat_rep {
*/
uint8_t perm;

/** Callback with updated report data. */
/** Callback with updated report data.
* Used if set and extended callback is not set.
*/
bt_hids_rep_handler_t handler;

/** Extended callback with updated report data.
* Has preference over the normal callback.
*/
bt_hids_rep_ext_handler_t handler_ext;
};

/** @brief Boot Mouse Input Report.
Expand Down
41 changes: 25 additions & 16 deletions subsys/bluetooth/services/hids.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,13 @@ static ssize_t hids_outp_rep_read(struct bt_conn *conn,
ret_len = bt_gatt_attr_read(conn, attr, buf, len, offset, rep_data,
rep->size);

if (rep->handler) {
struct bt_hids_rep report = {
.data = buf,
.size = rep->size,
};
struct bt_hids_rep report = {
.data = buf,
.size = rep->size,
};
if (rep->handler_ext) {
rep->handler_ext(rep->id, &report, conn, false);
} else if (rep->handler) {
rep->handler(&report, conn, false);
}

Expand Down Expand Up @@ -331,11 +333,13 @@ static ssize_t hids_outp_rep_write(struct bt_conn *conn,
}
memcpy(rep_data + offset, buf, len);

if (rep->handler) {
struct bt_hids_rep report = {
.data = rep_data,
.size = rep->size,
};
struct bt_hids_rep report = {
.data = rep_data,
.size = rep->size,
};
if (rep->handler_ext) {
rep->handler_ext(rep->id, &report, conn, true);
} else if (rep->handler) {
rep->handler(&report, conn, true);
}
release_ctx:
Expand Down Expand Up @@ -473,16 +477,21 @@ static void hids_input_report_ccc_changed(struct bt_gatt_attr const *attr,
CONTAINER_OF((struct _bt_gatt_ccc *)attr->user_data,
struct bt_hids_inp_rep, ccc);

uint8_t report_id = inp_rep->id;
enum bt_hids_notify_evt evt;

if (value == BT_GATT_CCC_NOTIFY) {
LOG_DBG("Notification has been turned on");
if (inp_rep->handler != NULL) {
inp_rep->handler(BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
}
evt = BT_HIDS_CCCD_EVT_NOTIFY_ENABLED;
} else {
LOG_DBG("Notification has been turned off");
if (inp_rep->handler != NULL) {
inp_rep->handler(BT_HIDS_CCCD_EVT_NOTIFY_DISABLED);
}
evt = BT_HIDS_CCCD_EVT_NOTIFY_DISABLED;
}

if (inp_rep->handler_ext != NULL) {
inp_rep->handler_ext(report_id, evt);
} else if (inp_rep->handler != NULL) {
inp_rep->handler(evt);
}
}

Expand Down

0 comments on commit fe1e80f

Please sign in to comment.