2012-07-10 19:16:52

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 00/13] Rebased remaining HoG patches

Rebasing against upstream after the input directory move.

Additionally there are two patches from Vinicius to improve re-connection time,
only discovering characteristics when probing the HoG device.

Claudio Takahasi (4):
hog: Use real values for vendor and product IDs
hog: Add read Report Reference descriptor
hog: Add HID Information Characteristic read
hog: Use hardware country code

João Paulo Rechi Vita (6):
hog: HID I/O driver
hog: Prepend Report ID to the HID report
hog: Add support for uHID events
hog: Handle output reports
hog: Handle output events
hog: Handle feature reports

Paulo Alcantara (1):
hog: Handle HID devices operating in Boot Protocol Mode

Vinicius Costa Gomes (2):
hog: Fix re-discovering HoG characteristics when reconnecting
hog: Fix destroying the uhid device when disconnecting

acinclude.m4 | 9 +-
configure.ac | 2 +
profiles/input/hog_device.c | 416 +++++++++++++++++++++++++++++++++++++++----
profiles/input/main.c | 2 +
profiles/input/manager.c | 2 +
5 files changed, 393 insertions(+), 38 deletions(-)

--
1.7.10.4



2012-07-13 14:02:25

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ 00/13] Rebased remaining HoG patches

Hi Jo?o Paulo,

On Tue, Jul 10, 2012, Jo?o Paulo Rechi Vita wrote:
> Rebasing against upstream after the input directory move.
>
> Additionally there are two patches from Vinicius to improve re-connection time,
> only discovering characteristics when probing the HoG device.
>
> Claudio Takahasi (4):
> hog: Use real values for vendor and product IDs
> hog: Add read Report Reference descriptor
> hog: Add HID Information Characteristic read
> hog: Use hardware country code
>
> Jo?o Paulo Rechi Vita (6):
> hog: HID I/O driver
> hog: Prepend Report ID to the HID report
> hog: Add support for uHID events
> hog: Handle output reports
> hog: Handle output events
> hog: Handle feature reports
>
> Paulo Alcantara (1):
> hog: Handle HID devices operating in Boot Protocol Mode
>
> Vinicius Costa Gomes (2):
> hog: Fix re-discovering HoG characteristics when reconnecting
> hog: Fix destroying the uhid device when disconnecting
>
> acinclude.m4 | 9 +-
> configure.ac | 2 +
> profiles/input/hog_device.c | 416 +++++++++++++++++++++++++++++++++++++++----
> profiles/input/main.c | 2 +
> profiles/input/manager.c | 2 +
> 5 files changed, 393 insertions(+), 38 deletions(-)

All patches from this set are now upstream.

Johan

2012-07-10 21:50:57

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ] hog: HID I/O driver

Hi Jo?o Paulo,

On Tue, Jul 10, 2012, Jo?o Paulo Rechi Vita wrote:
> uHID is HID I/O driver that makes possible to implement HID I/O drivers in
> user-space. It works similar to the uinput but it is initialized with a HID
> descriptor and deals with raw HID reports.
>
> This commit uses uHID to create a HID device for the remote HoG device and
> to tranfers HID reports to HID subsystem.
> ---
> profiles/input/hog_device.c | 89 ++++++++++++++++++++++++++++--------
> profiles/input/uhid-copy.h | 104 +++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 174 insertions(+), 19 deletions(-)
> create mode 100644 profiles/input/uhid-copy.h

Applied (after several tweaks). Thanks.

Johan

2012-07-10 21:26:51

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: Re: [PATCH BlueZ] hog: HID I/O driver

On Tue, Jul 10, 2012 at 6:24 PM, João Paulo Rechi Vita
<[email protected]> wrote:
> uHID is HID I/O driver that makes possible to implement HID I/O drivers in
> user-space. It works similar to the uinput but it is initialized with a HID
> descriptor and deals with raw HID reports.
>
> This commit uses uHID to create a HID device for the remote HoG device and
> to tranfers HID reports to HID subsystem.
> ---
> profiles/input/hog_device.c | 89 ++++++++++++++++++++++++++++--------
> profiles/input/uhid-copy.h | 104 +++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 174 insertions(+), 19 deletions(-)
> create mode 100644 profiles/input/uhid-copy.h

This revision address the comments that Marcel and Johan made in
person on the BlueZ meeting.

--
João Paulo Rechi Vita
Openbossa Labs - INdT

2012-07-10 21:24:46

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ] hog: HID I/O driver

uHID is HID I/O driver that makes possible to implement HID I/O drivers in
user-space. It works similar to the uinput but it is initialized with a HID
descriptor and deals with raw HID reports.

This commit uses uHID to create a HID device for the remote HoG device and
to tranfers HID reports to HID subsystem.
---
profiles/input/hog_device.c | 89 ++++++++++++++++++++++++++++--------
profiles/input/uhid-copy.h | 104 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 174 insertions(+), 19 deletions(-)
create mode 100644 profiles/input/uhid-copy.h

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 8e5e758..b0fa9da 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -29,6 +29,10 @@
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include "uhid-copy.h"

#include <bluetooth/bluetooth.h>
#include <bluetooth/uuid.h>
@@ -49,6 +53,7 @@

#define HOG_REPORT_MAP_UUID 0x2A4B
#define HOG_REPORT_UUID 0x2A4D
+#define UHID_DEVICE_FILE "/dev/uhid"

#define HOG_REPORT_MAP_MAX_SIZE 512

@@ -64,21 +69,32 @@ struct hog_device {
guint report_cb_id;
struct gatt_primary *hog_primary;
GSList *reports;
+ int uhid_fd;
};

static GSList *devices = NULL;

static void report_value_cb(const uint8_t *pdu, uint16_t len, gpointer user_data)
{
- uint16_t handle;
+ struct hog_device *hogdev = user_data;
+ struct uhid_event ev;
+ uint16_t report_size = len - 3;

if (len < 3) { /* 1-byte opcode + 2-byte handle */
error("Malformed ATT notification");
return;
}

- handle = att_get_u16(&pdu[1]);
- DBG("Report notification on handle 0x%04x", handle);
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_INPUT;
+ ev.u.input.size = MIN(report_size, UHID_DATA_MAX);
+ memcpy(ev.u.input.data, &pdu[3], MIN(report_size, UHID_DATA_MAX));
+
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("uHID write failed: %s", strerror(errno));
+ else
+ DBG("Report from HoG device %s written to uHID fd %d",
+ hogdev->path, hogdev->uhid_fd);
}

static void report_ccc_written_cb(guint8 status, const guint8 *pdu,
@@ -157,7 +173,9 @@ static void discover_descriptor(GAttrib *attrib, struct gatt_char *chr,
static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
gpointer user_data)
{
+ struct hog_device *hogdev = user_data;
uint8_t value[HOG_REPORT_MAP_MAX_SIZE];
+ struct uhid_event ev;
ssize_t vlen;
int i;

@@ -179,6 +197,22 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
else
DBG("\t %02x %02x", value[i], value[i + 1]);
}
+
+ /* create uHID device */
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_CREATE;
+ /* TODO: get info from DIS */
+ strcpy((char *)ev.u.create.name, "bluez-hog-device");
+ ev.u.create.vendor = 0xBEBA;
+ ev.u.create.product = 0xCAFE;
+ ev.u.create.version = 0;
+ ev.u.create.country = 0;
+ ev.u.create.bus = BUS_BLUETOOTH;
+ ev.u.create.rd_data = value;
+ ev.u.create.rd_size = vlen;
+
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("Failed to create uHID device: %s", strerror(errno));
}

static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
@@ -239,6 +273,12 @@ static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
static void attio_disconnected_cb(gpointer user_data)
{
struct hog_device *hogdev = user_data;
+ struct uhid_event ev;
+
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_DESTROY;
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("Failed to destroy uHID device: %s", strerror(errno));

g_attrib_unregister(hogdev->attrib, hogdev->report_cb_id);
hogdev->report_cb_id = 0;
@@ -293,6 +333,22 @@ static struct gatt_primary *load_hog_primary(struct btd_device *device)
return (l ? l->data : NULL);
}

+static void report_free(void *data)
+{
+ struct report *report = data;
+ g_free(report->decl);
+ g_free(report);
+}
+
+static void hog_device_free(struct hog_device *hogdev)
+{
+ btd_device_unref(hogdev->device);
+ g_slist_free_full(hogdev->reports, report_free);
+ g_free(hogdev->path);
+ g_free(hogdev->hog_primary);
+ g_free(hogdev);
+}
+
int hog_device_register(struct btd_device *device, const char *path)
{
struct hog_device *hogdev;
@@ -310,6 +366,13 @@ int hog_device_register(struct btd_device *device, const char *path)
if (!hogdev)
return -ENOMEM;

+ hogdev->uhid_fd = open(UHID_DEVICE_FILE, O_RDWR | O_CLOEXEC);
+ if (hogdev->uhid_fd < 0) {
+ error("Failed to open uHID device: %s", strerror(errno));
+ hog_device_free(hogdev);
+ return -errno;
+ }
+
hogdev->hog_primary = g_memdup(prim, sizeof(*prim));

hogdev->attioid = btd_device_add_attio_callback(device,
@@ -323,22 +386,6 @@ int hog_device_register(struct btd_device *device, const char *path)
return 0;
}

-static void report_free(void *data)
-{
- struct report *report = data;
- g_free(report->decl);
- g_free(report);
-}
-
-static void hog_device_free(struct hog_device *hogdev)
-{
- btd_device_unref(hogdev->device);
- g_slist_free_full(hogdev->reports, report_free);
- g_free(hogdev->path);
- g_free(hogdev->hog_primary);
- g_free(hogdev);
-}
-
int hog_device_unregister(const char *path)
{
struct hog_device *hogdev;
@@ -348,6 +395,10 @@ int hog_device_unregister(const char *path)
return -EINVAL;

btd_device_remove_attio_callback(hogdev->device, hogdev->attioid);
+
+ close(hogdev->uhid_fd);
+ hogdev->uhid_fd = -1;
+
devices = g_slist_remove(devices, hogdev);
hog_device_free(hogdev);

diff --git a/profiles/input/uhid-copy.h b/profiles/input/uhid-copy.h
new file mode 100644
index 0000000..381b062
--- /dev/null
+++ b/profiles/input/uhid-copy.h
@@ -0,0 +1,104 @@
+#ifndef __UHID_H_
+#define __UHID_H_
+
+/*
+ * User-space I/O driver support for HID subsystem
+ * Copyright (c) 2012 David Herrmann
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ */
+
+/*
+ * Public header for user-space communication. We try to keep every structure
+ * aligned but to be safe we also use __attribute__((__packed__)). Therefore,
+ * the communication should be ABI compatible even between architectures.
+ */
+
+#include <linux/input.h>
+#include <linux/types.h>
+
+enum uhid_event_type {
+ UHID_CREATE,
+ UHID_DESTROY,
+ UHID_START,
+ UHID_STOP,
+ UHID_OPEN,
+ UHID_CLOSE,
+ UHID_OUTPUT,
+ UHID_OUTPUT_EV,
+ UHID_INPUT,
+ UHID_FEATURE,
+ UHID_FEATURE_ANSWER,
+};
+
+struct uhid_create_req {
+ __u8 name[128];
+ __u8 phys[64];
+ __u8 uniq[64];
+ __u8 *rd_data;
+ __u16 rd_size;
+
+ __u16 bus;
+ __u32 vendor;
+ __u32 product;
+ __u32 version;
+ __u32 country;
+} __attribute__((__packed__));
+
+#define UHID_DATA_MAX 4096
+
+enum uhid_report_type {
+ UHID_FEATURE_REPORT,
+ UHID_OUTPUT_REPORT,
+ UHID_INPUT_REPORT,
+};
+
+struct uhid_input_req {
+ __u8 data[UHID_DATA_MAX];
+ __u16 size;
+} __attribute__((__packed__));
+
+struct uhid_output_req {
+ __u8 data[UHID_DATA_MAX];
+ __u16 size;
+ __u8 rtype;
+} __attribute__((__packed__));
+
+struct uhid_output_ev_req {
+ __u16 type;
+ __u16 code;
+ __s32 value;
+} __attribute__((__packed__));
+
+struct uhid_feature_req {
+ __u32 id;
+ __u8 rnum;
+ __u8 rtype;
+} __attribute__((__packed__));
+
+struct uhid_feature_answer_req {
+ __u32 id;
+ __u16 err;
+ __u16 size;
+ __u8 data[UHID_DATA_MAX];
+};
+
+struct uhid_event {
+ __u32 type;
+
+ union {
+ struct uhid_create_req create;
+ struct uhid_input_req input;
+ struct uhid_output_req output;
+ struct uhid_output_ev_req output_ev;
+ struct uhid_feature_req feature;
+ struct uhid_feature_answer_req feature_answer;
+ } u;
+} __attribute__((__packed__));
+
+#endif /* __UHID_H_ */
--
1.7.10.4


2012-07-10 19:17:05

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 13/13] hog: Fix destroying the uhid device when disconnecting

From: Vinicius Costa Gomes <[email protected]>

The uhid device should have the lifetime of the hog_device structure.
So, only destroy the uhid device when hog_device is destroyed.
---
profiles/input/hog_device.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 7f4cf2d..10bc622 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -550,12 +550,6 @@ static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
static void attio_disconnected_cb(gpointer user_data)
{
struct hog_device *hogdev = user_data;
- struct uhid_event ev;
-
- memset(&ev, 0, sizeof(ev));
- ev.type = UHID_DESTROY;
- if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
- error("Failed to destroy uHID device: %s", strerror(errno));

g_attrib_unregister(hogdev->attrib, hogdev->report_cb_id);
hogdev->report_cb_id = 0;
@@ -675,6 +669,7 @@ int hog_device_register(struct btd_device *device, const char *path)
int hog_device_unregister(const char *path)
{
struct hog_device *hogdev;
+ struct uhid_event ev;

hogdev = find_device_by_path(devices, path);
if (hogdev == NULL)
@@ -687,6 +682,11 @@ int hog_device_unregister(const char *path)
hogdev->uhid_watch_id = 0;
}

+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_DESTROY;
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("Failed to destroy uHID device: %s", strerror(errno));
+
close(hogdev->uhid_fd);
hogdev->uhid_fd = -1;

--
1.7.10.4


2012-07-10 19:17:04

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 12/13] hog: Fix re-discovering HoG characteristics when reconnecting

From: Vinicius Costa Gomes <[email protected]>

If the reports list is already present there's no need rediscovering all
the characteristics. This should also make reconnections faster.
---
profiles/input/hog_device.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 18854b8..7f4cf2d 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -536,12 +536,15 @@ static void attio_connected_cb(GAttrib *attrib, gpointer user_data)

hogdev->attrib = g_attrib_ref(attrib);

- gatt_discover_char(hogdev->attrib, prim->range.start, prim->range.end,
- NULL, char_discovered_cb, hogdev);
-
hogdev->report_cb_id = g_attrib_register(hogdev->attrib,
ATT_OP_HANDLE_NOTIFY, report_value_cb,
hogdev, NULL);
+
+ if (hogdev->reports == NULL) {
+ gatt_discover_char(hogdev->attrib, prim->range.start,
+ prim->range.end, NULL,
+ char_discovered_cb, hogdev);
+ }
}

static void attio_disconnected_cb(gpointer user_data)
--
1.7.10.4


2012-07-10 19:17:03

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 11/13] hog: Handle HID devices operating in Boot Protocol Mode

From: Paulo Alcantara <[email protected]>

BlueZ does not support HID devices operating in Boot Protocol Mode, so we
need to set it back to Report Protocol Mode through the Protocol Mode
characteristic.

This patch takes cares of changing from Boot Protocol Mode to Report
Protocol Mode by writing the Protocol Mode characteristic value to 1,
which is value for Report Protocol Mode on HID devices operating in
Boot Protocol Mode.
---
profiles/input/hog_device.c | 50 +++++++++++++++++++++++++++++++++++++++++--
1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 7e6c4b1..18854b8 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -54,11 +54,15 @@
#define HOG_INFO_UUID 0x2A4A
#define HOG_REPORT_MAP_UUID 0x2A4B
#define HOG_REPORT_UUID 0x2A4D
+#define HOG_PROTO_MODE_UUID 0x2A4E

#define HOG_REPORT_TYPE_INPUT 1
#define HOG_REPORT_TYPE_OUTPUT 2
#define HOG_REPORT_TYPE_FEATURE 3

+#define HOG_PROTO_MODE_BOOT 0
+#define HOG_PROTO_MODE_REPORT 1
+
#define UHID_DEVICE_FILE "/dev/uhid"

#define HOG_REPORT_MAP_MAX_SIZE 512
@@ -77,6 +81,7 @@ struct hog_device {
guint uhid_watch_id;
uint16_t bcdhid;
uint8_t bcountrycode;
+ uint16_t proto_mode_handle;
uint8_t flags;
};

@@ -328,13 +333,45 @@ static void info_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
hogdev->bcdhid, hogdev->bcountrycode, hogdev->flags);
}

+static void proto_mode_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
+ gpointer user_data)
+{
+ struct hog_device *hogdev = user_data;
+ uint8_t value;
+ ssize_t vlen;
+
+ if (status != 0) {
+ error("Protocol Mode characteristic read failed: %s",
+ att_ecode2str(status));
+ return;
+ }
+
+ vlen = dec_read_resp(pdu, plen, &value, sizeof(value));
+ if (vlen < 0) {
+ error("ATT protocol error");
+ return;
+ }
+
+ if (value == HOG_PROTO_MODE_BOOT) {
+ uint8_t nval = HOG_PROTO_MODE_REPORT;
+
+ DBG("HoG device %s is operating in Boot Procotol Mode",
+ hogdev->path);
+
+ gatt_write_char(hogdev->attrib, hogdev->proto_mode_handle, &nval,
+ sizeof(nval), NULL, NULL);
+ } else if (value == HOG_PROTO_MODE_REPORT)
+ DBG("HoG device %s is operating in Report Protocol Mode",
+ hogdev->path);
+}
+
static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
{
struct hog_device *hogdev = user_data;
- bt_uuid_t report_uuid, report_map_uuid, info_uuid;
+ bt_uuid_t report_uuid, report_map_uuid, info_uuid, proto_mode_uuid;
struct report *report;
GSList *l;
- uint16_t map_handle = 0, info_handle = 0;
+ uint16_t map_handle = 0, info_handle = 0, proto_mode_handle = 0;

if (status != 0) {
const char *str = att_ecode2str(status);
@@ -345,6 +382,7 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
bt_uuid16_create(&report_uuid, HOG_REPORT_UUID);
bt_uuid16_create(&report_map_uuid, HOG_REPORT_MAP_UUID);
bt_uuid16_create(&info_uuid, HOG_INFO_UUID);
+ bt_uuid16_create(&proto_mode_uuid, HOG_PROTO_MODE_UUID);

for (l = chars; l; l = g_slist_next(l)) {
struct gatt_char *chr, *next;
@@ -369,6 +407,14 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
map_handle = chr->value_handle;
else if (bt_uuid_cmp(&uuid, &info_uuid) == 0)
info_handle = chr->value_handle;
+ else if (bt_uuid_cmp(&uuid, &proto_mode_uuid) == 0)
+ proto_mode_handle = chr->value_handle;
+ }
+
+ if (proto_mode_handle) {
+ hogdev->proto_mode_handle = proto_mode_handle;
+ gatt_read_char(hogdev->attrib, proto_mode_handle, 0,
+ proto_mode_read_cb, hogdev);
}

if (info_handle)
--
1.7.10.4


2012-07-10 19:17:02

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 10/13] hog: Use hardware country code

From: Claudio Takahasi <[email protected]>

bCountryCode is a 8-bits integer identifying hardware target country.

The order of the characteristic declarations may be different on each
implementation. Since GATT/ATT requests need to be serialized, HID
information will be returned before report map characteristic value.
---
profiles/input/hog_device.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 778a367..7e6c4b1 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -292,7 +292,7 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
ev.u.create.vendor = vendor;
ev.u.create.product = product;
ev.u.create.version = version;
- ev.u.create.country = 0; /* get this info from the right place */
+ ev.u.create.country = hogdev->bcountrycode;
ev.u.create.bus = BUS_BLUETOOTH;
ev.u.create.rd_data = value;
ev.u.create.rd_size = vlen;
@@ -334,6 +334,7 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
bt_uuid_t report_uuid, report_map_uuid, info_uuid;
struct report *report;
GSList *l;
+ uint16_t map_handle = 0, info_handle = 0;

if (status != 0) {
const char *str = att_ecode2str(status);
@@ -365,12 +366,18 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
report);
discover_descriptor(hogdev->attrib, chr, next, report);
} else if (bt_uuid_cmp(&uuid, &report_map_uuid) == 0)
- gatt_read_char(hogdev->attrib, chr->value_handle, 0,
- report_map_read_cb, hogdev);
+ map_handle = chr->value_handle;
else if (bt_uuid_cmp(&uuid, &info_uuid) == 0)
- gatt_read_char(hogdev->attrib, chr->value_handle, 0,
- info_read_cb, hogdev);
+ info_handle = chr->value_handle;
}
+
+ if (info_handle)
+ gatt_read_char(hogdev->attrib, info_handle, 0,
+ info_read_cb, hogdev);
+
+ if (map_handle)
+ gatt_read_char(hogdev->attrib, map_handle, 0,
+ report_map_read_cb, hogdev);
}

static void output_written_cb(guint8 status, const guint8 *pdu,
--
1.7.10.4


2012-07-10 19:17:01

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 09/13] hog: Add HID Information Characteristic read

From: Claudio Takahasi <[email protected]>

This patch adds the characteristic value read for HID Information
Characteristic. It's information contains HID Device's HID Attributes.
---
profiles/input/hog_device.c | 38 +++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 00127c3..778a367 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -51,6 +51,7 @@
#include "attio.h"
#include "gatt.h"

+#define HOG_INFO_UUID 0x2A4A
#define HOG_REPORT_MAP_UUID 0x2A4B
#define HOG_REPORT_UUID 0x2A4D

@@ -61,6 +62,7 @@
#define UHID_DEVICE_FILE "/dev/uhid"

#define HOG_REPORT_MAP_MAX_SIZE 512
+#define HID_INFO_SIZE 4

struct hog_device {
char *path;
@@ -73,6 +75,9 @@ struct hog_device {
int uhid_fd;
gboolean prepend_id;
guint uhid_watch_id;
+ uint16_t bcdhid;
+ uint8_t bcountrycode;
+ uint8_t flags;
};

struct report {
@@ -296,10 +301,37 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
error("Failed to create uHID device: %s", strerror(errno));
}

+static void info_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
+ gpointer user_data)
+{
+ struct hog_device *hogdev = user_data;
+ uint8_t value[HID_INFO_SIZE];
+ ssize_t vlen;
+
+ if (status != 0) {
+ error("HID Information read failed: %s",
+ att_ecode2str(status));
+ return;
+ }
+
+ vlen = dec_read_resp(pdu, plen, value, sizeof(value));
+ if (vlen != 4) {
+ error("ATT protocol error");
+ return;
+ }
+
+ hogdev->bcdhid = att_get_u16(&value[0]);
+ hogdev->bcountrycode = value[2];
+ hogdev->flags = value[3];
+
+ DBG("bcdHID: 0x%04X bCountryCode: 0x%02X Flags: 0x%02X",
+ hogdev->bcdhid, hogdev->bcountrycode, hogdev->flags);
+}
+
static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
{
struct hog_device *hogdev = user_data;
- bt_uuid_t report_uuid, report_map_uuid;
+ bt_uuid_t report_uuid, report_map_uuid, info_uuid;
struct report *report;
GSList *l;

@@ -311,6 +343,7 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)

bt_uuid16_create(&report_uuid, HOG_REPORT_UUID);
bt_uuid16_create(&report_map_uuid, HOG_REPORT_MAP_UUID);
+ bt_uuid16_create(&info_uuid, HOG_INFO_UUID);

for (l = chars; l; l = g_slist_next(l)) {
struct gatt_char *chr, *next;
@@ -334,6 +367,9 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
} else if (bt_uuid_cmp(&uuid, &report_map_uuid) == 0)
gatt_read_char(hogdev->attrib, chr->value_handle, 0,
report_map_read_cb, hogdev);
+ else if (bt_uuid_cmp(&uuid, &info_uuid) == 0)
+ gatt_read_char(hogdev->attrib, chr->value_handle, 0,
+ info_read_cb, hogdev);
}
}

--
1.7.10.4


2012-07-10 19:17:00

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 08/13] hog: Handle feature reports

This patch writes the feature reports coming from the HID host on the
device's Feature Report characteristic.
---
profiles/input/hog_device.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 236745a..00127c3 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -56,6 +56,7 @@

#define HOG_REPORT_TYPE_INPUT 1
#define HOG_REPORT_TYPE_OUTPUT 2
+#define HOG_REPORT_TYPE_FEATURE 3

#define UHID_DEVICE_FILE "/dev/uhid"

@@ -362,10 +363,20 @@ static void forward_report(struct hog_device *hogdev,
int size;
guint type;

- type = HOG_REPORT_TYPE_OUTPUT;
data = ev->u.output.data;
size = ev->u.output.size;

+ switch (ev->type) {
+ case UHID_OUTPUT:
+ type = HOG_REPORT_TYPE_OUTPUT;
+ break;
+ case UHID_FEATURE:
+ type = HOG_REPORT_TYPE_FEATURE;
+ break;
+ default:
+ return;
+ }
+
l = g_slist_find_custom(hogdev->reports, GUINT_TO_POINTER(type),
report_type_cmp);
if (!l)
@@ -409,6 +420,7 @@ static gboolean uhid_event_cb(GIOChannel *io, GIOCondition cond,

switch (ev.type) {
case UHID_OUTPUT:
+ case UHID_FEATURE:
forward_report(hogdev, &ev);
break;
case UHID_OUTPUT_EV:
--
1.7.10.4


2012-07-10 19:16:59

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 07/13] hog: Handle output events

This patch prints the output events coming from the HID host for debug
purposes.
---
profiles/input/hog_device.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index d8d67ae..236745a 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -411,6 +411,11 @@ static gboolean uhid_event_cb(GIOChannel *io, GIOCondition cond,
case UHID_OUTPUT:
forward_report(hogdev, &ev);
break;
+ case UHID_OUTPUT_EV:
+ DBG("uHID output event: type %d code %d value %d",
+ ev.u.output_ev.type, ev.u.output_ev.code,
+ ev.u.output_ev.value);
+ break;
default:
warn("unexpected uHID event");
break;
--
1.7.10.4


2012-07-10 19:16:58

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 06/13] hog: Handle output reports

This patch writes the output reports coming from the HID host on the
device's Output Report characteristic.
---
profiles/input/hog_device.c | 60 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index b812ed5..d8d67ae 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -54,6 +54,9 @@
#define HOG_REPORT_MAP_UUID 0x2A4B
#define HOG_REPORT_UUID 0x2A4D

+#define HOG_REPORT_TYPE_INPUT 1
+#define HOG_REPORT_TYPE_OUTPUT 2
+
#define UHID_DEVICE_FILE "/dev/uhid"

#define HOG_REPORT_MAP_MAX_SIZE 512
@@ -333,6 +336,54 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
}
}

+static void output_written_cb(guint8 status, const guint8 *pdu,
+ guint16 plen, gpointer user_data)
+{
+ if (status != 0) {
+ error("Write output report failed: %s", att_ecode2str(status));
+ return;
+ }
+}
+
+static gint report_type_cmp(gconstpointer a, gconstpointer b)
+{
+ const struct report *report = a;
+ uint8_t type = GPOINTER_TO_UINT(b);
+
+ return report->type - type;
+}
+
+static void forward_report(struct hog_device *hogdev,
+ struct uhid_event *ev)
+{
+ struct report *report;
+ GSList *l;
+ void *data;
+ int size;
+ guint type;
+
+ type = HOG_REPORT_TYPE_OUTPUT;
+ data = ev->u.output.data;
+ size = ev->u.output.size;
+
+ l = g_slist_find_custom(hogdev->reports, GUINT_TO_POINTER(type),
+ report_type_cmp);
+ if (!l)
+ return;
+
+ report = l->data;
+
+ DBG("Sending report type %d to device %s handle 0x%X", type,
+ hogdev->path, report->decl->value_handle);
+
+ if (report->decl->properties & ATT_CHAR_PROPER_WRITE)
+ gatt_write_char(hogdev->attrib, report->decl->value_handle,
+ data, size, output_written_cb, hogdev);
+ else if (report->decl->properties & ATT_CHAR_PROPER_WRITE_WITHOUT_RESP)
+ gatt_write_char(hogdev->attrib, report->decl->value_handle,
+ data, size, NULL, NULL);
+}
+
static gboolean uhid_event_cb(GIOChannel *io, GIOCondition cond,
gpointer user_data)
{
@@ -356,6 +407,15 @@ static gboolean uhid_event_cb(GIOChannel *io, GIOCondition cond,

DBG("uHID event type %d received", ev.type);

+ switch (ev.type) {
+ case UHID_OUTPUT:
+ forward_report(hogdev, &ev);
+ break;
+ default:
+ warn("unexpected uHID event");
+ break;
+ }
+
return TRUE;

failed:
--
1.7.10.4


2012-07-10 19:16:57

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 05/13] hog: Add support for uHID events

This patch adds the GLib GIOChannel watcher to monitor uhid events.
---
profiles/input/hog_device.c | 46 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 53b4746..b812ed5 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -53,6 +53,7 @@

#define HOG_REPORT_MAP_UUID 0x2A4B
#define HOG_REPORT_UUID 0x2A4D
+
#define UHID_DEVICE_FILE "/dev/uhid"

#define HOG_REPORT_MAP_MAX_SIZE 512
@@ -67,6 +68,7 @@ struct hog_device {
GSList *reports;
int uhid_fd;
gboolean prepend_id;
+ guint uhid_watch_id;
};

struct report {
@@ -331,6 +333,36 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
}
}

+static gboolean uhid_event_cb(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ struct hog_device *hogdev = user_data;
+ struct uhid_event ev;
+ ssize_t bread;
+ int fd;
+
+ if (cond & (G_IO_ERR | G_IO_NVAL))
+ goto failed;
+
+ fd = g_io_channel_unix_get_fd(io);
+ memset(&ev, 0, sizeof(ev));
+
+ bread = read(fd, &ev, sizeof(ev));
+ if (bread < 0) {
+ int err = -errno;
+ DBG("uhid-dev read: %s(%d)", strerror(err), err);
+ goto failed;
+ }
+
+ DBG("uHID event type %d received", ev.type);
+
+ return TRUE;
+
+failed:
+ hogdev->uhid_watch_id = 0;
+ return FALSE;
+}
+
static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
{
struct hog_device *hogdev = user_data;
@@ -429,6 +461,8 @@ int hog_device_register(struct btd_device *device, const char *path)
{
struct hog_device *hogdev;
struct gatt_primary *prim;
+ GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_NVAL;
+ GIOChannel *io;

hogdev = find_device_by_path(devices, path);
if (hogdev)
@@ -449,12 +483,19 @@ int hog_device_register(struct btd_device *device, const char *path)
return -errno;
}

+ io = g_io_channel_unix_new(hogdev->uhid_fd);
+ g_io_channel_set_encoding(io, NULL, NULL);
+ hogdev->uhid_watch_id = g_io_add_watch(io, cond, uhid_event_cb,
+ hogdev);
+ g_io_channel_unref(io);
+
hogdev->hog_primary = g_memdup(prim, sizeof(*prim));

hogdev->attioid = btd_device_add_attio_callback(device,
attio_connected_cb,
attio_disconnected_cb,
hogdev);
+
device_set_auto_connect(device, TRUE);

devices = g_slist_append(devices, hogdev);
@@ -472,6 +513,11 @@ int hog_device_unregister(const char *path)

btd_device_remove_attio_callback(hogdev->device, hogdev->attioid);

+ if (hogdev->uhid_watch_id) {
+ g_source_remove(hogdev->uhid_watch_id);
+ hogdev->uhid_watch_id = 0;
+ }
+
close(hogdev->uhid_fd);
hogdev->uhid_fd = -1;

--
1.7.10.4


2012-07-10 19:16:56

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 04/13] hog: Prepend Report ID to the HID report

If the report descriptor has a Report ID tag it has to be prepended
to the report data to construct the HID report itself.
---
profiles/input/hog_device.c | 78 +++++++++++++++++++++++++++++++++++--------
1 file changed, 64 insertions(+), 14 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 823d7a4..53b4746 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -57,10 +57,6 @@

#define HOG_REPORT_MAP_MAX_SIZE 512

-struct report {
- struct gatt_char *decl;
-};
-
struct hog_device {
char *path;
struct btd_device *device;
@@ -70,25 +66,64 @@ struct hog_device {
struct gatt_primary *hog_primary;
GSList *reports;
int uhid_fd;
+ gboolean prepend_id;
+};
+
+struct report {
+ uint8_t id;
+ uint8_t type;
+ struct gatt_char *decl;
+ struct hog_device *hogdev;
};

static GSList *devices = NULL;

+static gint report_handle_cmp(gconstpointer a, gconstpointer b)
+{
+ const struct report *report = a;
+ uint16_t handle = GPOINTER_TO_UINT(b);
+
+ return report->decl->value_handle - handle;
+}
+
static void report_value_cb(const uint8_t *pdu, uint16_t len, gpointer user_data)
{
struct hog_device *hogdev = user_data;
struct uhid_event ev;
uint16_t report_size = len - 3;
+ guint handle;
+ GSList *l;
+ struct report *report;
+ uint8_t *buf;

if (len < 3) { /* 1-byte opcode + 2-byte handle */
error("Malformed ATT notification");
return;
}

+ handle = att_get_u16(&pdu[1]);
+
+ l = g_slist_find_custom(hogdev->reports, GUINT_TO_POINTER(handle),
+ report_handle_cmp);
+ if (!l) {
+ error("Invalid report");
+ return;
+ }
+
+ report = l->data;
+
memset(&ev, 0, sizeof(ev));
ev.type = UHID_INPUT;
ev.u.input.size = MIN(report_size, UHID_DATA_MAX);
- memcpy(ev.u.input.data, &pdu[3], MIN(report_size, UHID_DATA_MAX));
+
+ buf = ev.u.input.data;
+ if (hogdev->prepend_id) {
+ *buf = report->id;
+ buf++;
+ ev.u.input.size++;
+ }
+
+ memcpy(buf, &pdu[3], MIN(report_size, UHID_DATA_MAX));

if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
error("uHID write failed: %s", strerror(errno));
@@ -121,6 +156,8 @@ static void write_ccc(uint16_t handle, gpointer user_data)
static void report_reference_cb(guint8 status, const guint8 *pdu,
guint16 plen, gpointer user_data)
{
+ struct report *report = user_data;
+
if (status != 0) {
error("Read Report Reference descriptor failed: %s",
att_ecode2str(status));
@@ -132,13 +169,16 @@ static void report_reference_cb(guint8 status, const guint8 *pdu,
return;
}

+ report->id = pdu[1];
+ report->type = pdu[2];
DBG("Report ID: 0x%02x Report type: 0x%02x", pdu[1], pdu[2]);
}

static void discover_descriptor_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
- struct hog_device *hogdev = user_data;
+ struct report *report = user_data;
+ struct hog_device *hogdev = report->hogdev;
struct att_data_list *list;
uint8_t format;
int i;
@@ -165,10 +205,10 @@ static void discover_descriptor_cb(guint8 status, const guint8 *pdu,
uuid16 = att_get_u16(&value[2]);

if (uuid16 == GATT_CLIENT_CHARAC_CFG_UUID)
- write_ccc(handle, user_data);
+ write_ccc(handle, hogdev);
else if (uuid16 == GATT_REPORT_REFERENCE)
gatt_read_char(hogdev->attrib, handle, 0,
- report_reference_cb, hogdev);
+ report_reference_cb, report);
}

done:
@@ -211,11 +251,20 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
}

DBG("Report MAP:");
- for (i = 0; i < vlen; i += 2) {
- if (i + 1 == vlen)
- DBG("\t %02x", value[i]);
- else
- DBG("\t %02x %02x", value[i], value[i + 1]);
+ for (i = 0; i < vlen; i++) {
+ switch (value[i]) {
+ case 0x85:
+ case 0x86:
+ case 0x87:
+ hogdev->prepend_id = TRUE;
+ }
+
+ if (i % 2 == 0) {
+ if (i + 1 == vlen)
+ DBG("\t %02x", value[i]);
+ else
+ DBG("\t %02x %02x", value[i], value[i + 1]);
+ }
}

vendor_src = btd_device_get_vendor_src(hogdev->device);
@@ -271,10 +320,11 @@ static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)

if (bt_uuid_cmp(&uuid, &report_uuid) == 0) {
report = g_new0(struct report, 1);
+ report->hogdev = hogdev;
report->decl = g_memdup(chr, sizeof(*chr));
hogdev->reports = g_slist_append(hogdev->reports,
report);
- discover_descriptor(hogdev->attrib, chr, next, hogdev);
+ discover_descriptor(hogdev->attrib, chr, next, report);
} else if (bt_uuid_cmp(&uuid, &report_map_uuid) == 0)
gatt_read_char(hogdev->attrib, chr->value_handle, 0,
report_map_read_cb, hogdev);
--
1.7.10.4


2012-07-10 19:16:55

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 03/13] hog: Add read Report Reference descriptor

From: Claudio Takahasi <[email protected]>

This patch adds the GATT operation to read the value of the Report
Reference descriptor of the Report characteristic.
---
profiles/input/hog_device.c | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 547bd6c..823d7a4 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -118,9 +118,27 @@ static void write_ccc(uint16_t handle, gpointer user_data)
report_ccc_written_cb, hogdev);
}

+static void report_reference_cb(guint8 status, const guint8 *pdu,
+ guint16 plen, gpointer user_data)
+{
+ if (status != 0) {
+ error("Read Report Reference descriptor failed: %s",
+ att_ecode2str(status));
+ return;
+ }
+
+ if (plen != 3) {
+ error("Malformed ATT read response");
+ return;
+ }
+
+ DBG("Report ID: 0x%02x Report type: 0x%02x", pdu[1], pdu[2]);
+}
+
static void discover_descriptor_cb(guint8 status, const guint8 *pdu,
guint16 len, gpointer user_data)
{
+ struct hog_device *hogdev = user_data;
struct att_data_list *list;
uint8_t format;
int i;
@@ -146,10 +164,11 @@ static void discover_descriptor_cb(guint8 status, const guint8 *pdu,
handle = att_get_u16(value);
uuid16 = att_get_u16(&value[2]);

- if (uuid16 != GATT_CLIENT_CHARAC_CFG_UUID)
- continue;
-
- write_ccc(handle, user_data);
+ if (uuid16 == GATT_CLIENT_CHARAC_CFG_UUID)
+ write_ccc(handle, user_data);
+ else if (uuid16 == GATT_REPORT_REFERENCE)
+ gatt_read_char(hogdev->attrib, handle, 0,
+ report_reference_cb, hogdev);
}

done:
--
1.7.10.4


2012-07-10 19:16:54

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 02/13] hog: Use real values for vendor and product IDs

From: Claudio Takahasi <[email protected]>

This patch replaces the hard-code values for vendor and product IDs
by the values obtained from the device core functions. Vendor and
product IDs are read from the remote's Device Information Service.
---
profiles/input/hog_device.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 5dd9cf1..547bd6c 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -176,6 +176,7 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
struct hog_device *hogdev = user_data;
uint8_t value[HOG_REPORT_MAP_MAX_SIZE];
struct uhid_event ev;
+ uint16_t vendor_src, vendor, product, version;
ssize_t vlen;
int i;

@@ -198,15 +199,21 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
DBG("\t %02x %02x", value[i], value[i + 1]);
}

+ vendor_src = btd_device_get_vendor_src(hogdev->device);
+ vendor = btd_device_get_vendor(hogdev->device);
+ product = btd_device_get_product(hogdev->device);
+ version = btd_device_get_version(hogdev->device);
+ DBG("DIS information: vendor_src=0x%X, vendor=0x%X, product=0x%X, "
+ "version=0x%X", vendor_src, vendor, product, version);
+
/* create uHID device */
memset(&ev, 0, sizeof(ev));
ev.type = UHID_CREATE;
- /* TODO: get info from DIS */
strcpy((char *)ev.u.create.name, "bluez-hog-device");
- ev.u.create.vendor = 0xBEBA;
- ev.u.create.product = 0xCAFE;
- ev.u.create.version = 0;
- ev.u.create.country = 0;
+ ev.u.create.vendor = vendor;
+ ev.u.create.product = product;
+ ev.u.create.version = version;
+ ev.u.create.country = 0; /* get this info from the right place */
ev.u.create.bus = BUS_BLUETOOTH;
ev.u.create.rd_data = value;
ev.u.create.rd_size = vlen;
--
1.7.10.4


2012-07-10 19:16:53

by Joao Paulo Rechi Vita

[permalink] [raw]
Subject: [PATCH BlueZ 01/13] hog: HID I/O driver

uHID is HID I/O driver that makes possible to implement HID I/O drivers in
user-space. It works similar to the uinput but it is initialized with a HID
descriptor and deals with raw HID reports.

This commit uses uHID to create a HID device for the remote HoG device and
to tranfers HID reports to HID subsystem.
---
acinclude.m4 | 9 ++++-
configure.ac | 2 +
profiles/input/hog_device.c | 89 ++++++++++++++++++++++++++++++++++---------
profiles/input/main.c | 2 +
profiles/input/manager.c | 2 +
5 files changed, 84 insertions(+), 20 deletions(-)

diff --git a/acinclude.m4 b/acinclude.m4
index 39b0a18..e04cbe3 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -161,6 +161,13 @@ AC_DEFUN([AC_PATH_OUI], [
AC_DEFINE_UNQUOTED(OUIFILE, ["$ac_with_ouifile"], [Define the OUI file path])
])

+AC_DEFUN([AC_PATH_UHID], [
+ AC_CHECK_HEADERS(linux/uhid.h,
+ uhid_found=yes,
+ uhid_found=no
+ )
+])
+
AC_DEFUN([AC_ARG_BLUEZ], [
debug_enable=no
optimization_enable=yes
@@ -342,5 +349,5 @@ AC_DEFUN([AC_ARG_BLUEZ], [
AM_CONDITIONAL(DBUSOOBPLUGIN, test "${dbusoob_enable}" = "yes")
AM_CONDITIONAL(WIIMOTEPLUGIN, test "${wiimote_enable}" = "yes")
AM_CONDITIONAL(GATTMODULES, test "${gatt_enable}" = "yes")
- AM_CONDITIONAL(HOGPLUGIN, test "${gatt_enable}" = "yes" && test "${input_enable}" = "yes")
+ AM_CONDITIONAL(HOGPLUGIN, test "${gatt_enable}" = "yes" && test "${input_enable}" = "yes" && test "${uhid_found}" = "yes")
])
diff --git a/configure.ac b/configure.ac
index 7f331ae..150171f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -39,6 +39,7 @@ AC_CHECK_HEADER([sys/inotify.h],
[AC_DEFINE([HAVE_SYS_INOTIFY_H], 1,
[Define to 1 if you have <sys/inotify.h>.])],
[AC_MSG_ERROR(inotify headers are required and missing)])
+
AC_PATH_DBUS
AC_PATH_GLIB
AC_PATH_GSTREAMER
@@ -48,6 +49,7 @@ AC_PATH_SNDFILE
AC_PATH_OUI
AC_PATH_READLINE
AC_PATH_CHECK
+AC_PATH_UHID

AC_ARG_BLUEZ

diff --git a/profiles/input/hog_device.c b/profiles/input/hog_device.c
index 8e5e758..5dd9cf1 100644
--- a/profiles/input/hog_device.c
+++ b/profiles/input/hog_device.c
@@ -29,6 +29,10 @@
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <linux/uhid.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/uuid.h>
@@ -49,6 +53,7 @@

#define HOG_REPORT_MAP_UUID 0x2A4B
#define HOG_REPORT_UUID 0x2A4D
+#define UHID_DEVICE_FILE "/dev/uhid"

#define HOG_REPORT_MAP_MAX_SIZE 512

@@ -64,21 +69,32 @@ struct hog_device {
guint report_cb_id;
struct gatt_primary *hog_primary;
GSList *reports;
+ int uhid_fd;
};

static GSList *devices = NULL;

static void report_value_cb(const uint8_t *pdu, uint16_t len, gpointer user_data)
{
- uint16_t handle;
+ struct hog_device *hogdev = user_data;
+ struct uhid_event ev;
+ uint16_t report_size = len - 3;

if (len < 3) { /* 1-byte opcode + 2-byte handle */
error("Malformed ATT notification");
return;
}

- handle = att_get_u16(&pdu[1]);
- DBG("Report notification on handle 0x%04x", handle);
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_INPUT;
+ ev.u.input.size = MIN(report_size, UHID_DATA_MAX);
+ memcpy(ev.u.input.data, &pdu[3], MIN(report_size, UHID_DATA_MAX));
+
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("uHID write failed: %s", strerror(errno));
+ else
+ DBG("Report from HoG device %s written to uHID fd %d",
+ hogdev->path, hogdev->uhid_fd);
}

static void report_ccc_written_cb(guint8 status, const guint8 *pdu,
@@ -157,7 +173,9 @@ static void discover_descriptor(GAttrib *attrib, struct gatt_char *chr,
static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
gpointer user_data)
{
+ struct hog_device *hogdev = user_data;
uint8_t value[HOG_REPORT_MAP_MAX_SIZE];
+ struct uhid_event ev;
ssize_t vlen;
int i;

@@ -179,6 +197,22 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
else
DBG("\t %02x %02x", value[i], value[i + 1]);
}
+
+ /* create uHID device */
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_CREATE;
+ /* TODO: get info from DIS */
+ strcpy((char *)ev.u.create.name, "bluez-hog-device");
+ ev.u.create.vendor = 0xBEBA;
+ ev.u.create.product = 0xCAFE;
+ ev.u.create.version = 0;
+ ev.u.create.country = 0;
+ ev.u.create.bus = BUS_BLUETOOTH;
+ ev.u.create.rd_data = value;
+ ev.u.create.rd_size = vlen;
+
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("Failed to create uHID device: %s", strerror(errno));
}

static void char_discovered_cb(GSList *chars, guint8 status, gpointer user_data)
@@ -239,6 +273,12 @@ static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
static void attio_disconnected_cb(gpointer user_data)
{
struct hog_device *hogdev = user_data;
+ struct uhid_event ev;
+
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_DESTROY;
+ if (write(hogdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ error("Failed to destroy uHID device: %s", strerror(errno));

g_attrib_unregister(hogdev->attrib, hogdev->report_cb_id);
hogdev->report_cb_id = 0;
@@ -293,6 +333,22 @@ static struct gatt_primary *load_hog_primary(struct btd_device *device)
return (l ? l->data : NULL);
}

+static void report_free(void *data)
+{
+ struct report *report = data;
+ g_free(report->decl);
+ g_free(report);
+}
+
+static void hog_device_free(struct hog_device *hogdev)
+{
+ btd_device_unref(hogdev->device);
+ g_slist_free_full(hogdev->reports, report_free);
+ g_free(hogdev->path);
+ g_free(hogdev->hog_primary);
+ g_free(hogdev);
+}
+
int hog_device_register(struct btd_device *device, const char *path)
{
struct hog_device *hogdev;
@@ -310,6 +366,13 @@ int hog_device_register(struct btd_device *device, const char *path)
if (!hogdev)
return -ENOMEM;

+ hogdev->uhid_fd = open(UHID_DEVICE_FILE, O_RDWR | O_CLOEXEC);
+ if (hogdev->uhid_fd < 0) {
+ error("Failed to open uHID device: %s", strerror(errno));
+ hog_device_free(hogdev);
+ return -errno;
+ }
+
hogdev->hog_primary = g_memdup(prim, sizeof(*prim));

hogdev->attioid = btd_device_add_attio_callback(device,
@@ -323,22 +386,6 @@ int hog_device_register(struct btd_device *device, const char *path)
return 0;
}

-static void report_free(void *data)
-{
- struct report *report = data;
- g_free(report->decl);
- g_free(report);
-}
-
-static void hog_device_free(struct hog_device *hogdev)
-{
- btd_device_unref(hogdev->device);
- g_slist_free_full(hogdev->reports, report_free);
- g_free(hogdev->path);
- g_free(hogdev->hog_primary);
- g_free(hogdev);
-}
-
int hog_device_unregister(const char *path)
{
struct hog_device *hogdev;
@@ -348,6 +395,10 @@ int hog_device_unregister(const char *path)
return -EINVAL;

btd_device_remove_attio_callback(hogdev->device, hogdev->attioid);
+
+ close(hogdev->uhid_fd);
+ hogdev->uhid_fd = -1;
+
devices = g_slist_remove(devices, hogdev);
hog_device_free(hogdev);

diff --git a/profiles/input/main.c b/profiles/input/main.c
index 722bc49..d1623ec 100644
--- a/profiles/input/main.c
+++ b/profiles/input/main.c
@@ -86,6 +86,7 @@ static void input_exit(void)
BLUETOOTH_PLUGIN_DEFINE(input, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
input_init, input_exit)

+#ifdef HAVE_LINUX_UHID_H
static int hog_init(void)
{
if (!main_opts.gatt_enabled) {
@@ -106,3 +107,4 @@ static void hog_exit(void)

BLUETOOTH_PLUGIN_DEFINE(hog, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
hog_init, hog_exit)
+#endif
diff --git a/profiles/input/manager.c b/profiles/input/manager.c
index 928a2f5..696569f 100644
--- a/profiles/input/manager.c
+++ b/profiles/input/manager.c
@@ -194,6 +194,7 @@ void input_manager_exit(void)
connection = NULL;
}

+#ifdef HAVE_LINUX_UHID_H
static int hog_device_probe(struct btd_device *device, GSList *uuids)
{
const char *path = device_get_path(device);
@@ -228,3 +229,4 @@ void hog_manager_exit(void)
{
btd_unregister_device_driver(&hog_driver);
}
+#endif
--
1.7.10.4