From cdddd7e69e0694a8819a3067bb29ba4b7785fe90 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Fri, 19 Dec 2014 10:10:15 -0200 Subject: [PATCH] android/hog: implement get_report functionality When UHID_GET_REPORT is received, hog has to update the current value of the report and send it back to the caller. This function should be synchronous, so the answer is sent in get_report_cb(). --- android/hog.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/android/hog.c b/android/hog.c index bb770c88b..1a9aedf81 100644 --- a/android/hog.c +++ b/android/hog.c @@ -93,6 +93,8 @@ struct bt_hog { uint16_t proto_mode_handle; uint16_t ctrlpt_handle; uint8_t flags; + unsigned int getrep_att; + uint16_t getrep_id; struct bt_scpp *scpp; struct bt_dis *dis; struct bt_bas *bas; @@ -662,7 +664,7 @@ static void forward_report(struct uhid_event *ev, void *user_data) data, size, NULL, NULL); } -static void get_report(struct uhid_event *ev, void *user_data) +static void get_feature(struct uhid_event *ev, void *user_data) { struct bt_hog *hog = user_data; struct report *report; @@ -693,6 +695,89 @@ done: error("bt_uhid_send: %s", strerror(-err)); } +static void get_report_cb(guint8 status, const guint8 *pdu, guint16 len, + gpointer user_data) +{ + struct bt_hog *hog = user_data; + struct uhid_event rsp; + int err; + + hog->getrep_att = 0; + + memset(&rsp, 0, sizeof(rsp)); + rsp.type = UHID_GET_REPORT_REPLY; + rsp.u.get_report_reply.id = hog->getrep_id; + + if (status != 0) { + error("Error reading Report value: %s", att_ecode2str(status)); + goto exit; + } + + if (len == 0) { + error("Error reading Report, length %d", len); + status = EIO; + goto exit; + } + + if (pdu[0] != 0x0b) { + error("Error reading Report, invalid response: %02x", pdu[0]); + status = EPROTO; + goto exit; + } + + --len; + ++pdu; + if (hog->has_report_id && len > 0) { + --len; + ++pdu; + } + + rsp.u.get_report_reply.size = len; + memcpy(rsp.u.get_report_reply.data, pdu, len); + +exit: + rsp.u.get_report_reply.err = status; + err = bt_uhid_send(hog->uhid, &rsp); + if (err < 0) + error("bt_uhid_send: %s", strerror(-err)); +} + +static void get_report(struct uhid_event *ev, void *user_data) +{ + struct bt_hog *hog = user_data; + struct report *report; + guint8 err; + + /* uhid never sends reqs in parallel; if there's a req, it timed out */ + if (hog->getrep_att) { + g_attrib_cancel(hog->attrib, hog->getrep_att); + hog->getrep_att = 0; + } + + hog->getrep_id = ev->u.get_report.id; + + report = find_report(hog, ev->u.get_report.rtype, + ev->u.get_report.rnum); + if (!report) { + err = ENOTSUP; + goto fail; + } + + hog->getrep_att = gatt_read_char(hog->attrib, + report->decl->value_handle, + get_report_cb, hog); + if (!hog->getrep_att) { + err = ENOMEM; + goto fail; + } + + return; + +fail: + /* cancel the request on failure */ + get_report_cb(err, NULL, 0, hog); +} + static bool get_descriptor_item_info(uint8_t *buf, ssize_t blen, ssize_t *len, bool *is_long) { @@ -833,7 +918,8 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen, } bt_uhid_register(hog->uhid, UHID_OUTPUT, forward_report, hog); - bt_uhid_register(hog->uhid, UHID_FEATURE, get_report, hog); + bt_uhid_register(hog->uhid, UHID_FEATURE, get_feature, hog); + bt_uhid_register(hog->uhid, UHID_GET_REPORT, get_report, hog); } static void info_read_cb(guint8 status, const guint8 *pdu, guint16 plen, -- 2.47.3