3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2014 Google Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/ioctl.h>
36 #include "lib/bluetooth.h"
39 #include "lib/sdp_lib.h"
42 #include "gdbus/gdbus.h"
44 #include "btio/btio.h"
46 #include "src/adapter.h"
47 #include "src/device.h"
48 #include "src/profile.h"
49 #include "src/service.h"
50 #include "src/storage.h"
51 #include "src/dbus-common.h"
52 #include "src/error.h"
53 #include "src/sdp-client.h"
54 #include "src/shared/uhid.h"
57 #include "hidp_defs.h"
59 #define INPUT_INTERFACE "org.bluez.Input1"
61 enum reconnect_mode_t {
69 struct btd_service *service;
70 struct btd_device *device;
80 struct hidp_connadd_req *req;
82 enum reconnect_mode_t reconnect_mode;
83 guint reconnect_timer;
84 uint32_t reconnect_attempt;
87 uint8_t report_req_pending;
88 guint report_req_timer;
89 uint32_t report_rsp_id;
92 static int idle_timeout = 0;
93 static bool uhid_enabled = false;
95 void input_set_idle_timeout(int timeout)
97 idle_timeout = timeout;
100 void input_enable_userspace_hid(bool state)
102 uhid_enabled = state;
105 static void input_device_enter_reconnect_mode(struct input_device *idev);
106 static int connection_disconnect(struct input_device *idev, uint32_t flags);
108 static void input_device_free(struct input_device *idev)
110 bt_uhid_unref(idev->uhid);
111 btd_service_unref(idev->service);
112 btd_device_unref(idev->device);
115 if (idev->ctrl_watch > 0)
116 g_source_remove(idev->ctrl_watch);
118 if (idev->intr_watch > 0)
119 g_source_remove(idev->intr_watch);
121 if (idev->sec_watch > 0)
122 g_source_remove(idev->sec_watch);
125 g_io_channel_unref(idev->intr_io);
128 g_io_channel_unref(idev->ctrl_io);
131 g_free(idev->req->rd_data);
135 if (idev->reconnect_timer > 0)
136 g_source_remove(idev->reconnect_timer);
138 if (idev->report_req_timer > 0)
139 g_source_remove(idev->report_req_timer);
144 static bool hidp_send_message(GIOChannel *chan, uint8_t hdr,
145 const uint8_t *data, size_t size)
149 uint8_t msg[size + 1];
152 error("BT socket not connected");
161 memcpy(&msg[1], data, size);
164 fd = g_io_channel_unix_get_fd(chan);
166 len = write(fd, msg, size);
168 error("BT socket write error: %s (%d)", strerror(errno), errno);
172 if ((size_t) len < size) {
173 error("BT socket write error: partial write (%zd of %zu bytes)",
181 static bool hidp_send_ctrl_message(struct input_device *idev, uint8_t hdr,
182 const uint8_t *data, size_t size)
184 return hidp_send_message(idev->ctrl_io, hdr, data, size);
187 static bool hidp_send_intr_message(struct input_device *idev, uint8_t hdr,
188 const uint8_t *data, size_t size)
190 return hidp_send_message(idev->intr_io, hdr, data, size);
193 static bool uhid_send_feature_answer(struct input_device *idev,
194 const uint8_t *data, size_t size,
195 uint32_t id, uint16_t err)
197 struct uhid_event ev;
203 if (size > sizeof(ev.u.feature_answer.data))
204 size = sizeof(ev.u.feature_answer.data);
206 if (!idev->uhid_created) {
207 DBG("HID report (%zu bytes) dropped", size);
211 memset(&ev, 0, sizeof(ev));
212 ev.type = UHID_FEATURE_ANSWER;
213 ev.u.feature_answer.id = id;
214 ev.u.feature_answer.err = err;
215 ev.u.feature_answer.size = size;
218 memcpy(ev.u.feature_answer.data, data, size);
220 ret = bt_uhid_send(idev->uhid, &ev);
222 error("bt_uhid_send: %s (%d)", strerror(-ret), -ret);
226 DBG("HID report (%zu bytes)", size);
231 static bool uhid_send_input_report(struct input_device *idev,
232 const uint8_t *data, size_t size)
234 struct uhid_event ev;
240 if (size > sizeof(ev.u.input.data))
241 size = sizeof(ev.u.input.data);
243 if (!idev->uhid_created) {
244 DBG("HID report (%zu bytes) dropped", size);
248 memset(&ev, 0, sizeof(ev));
249 ev.type = UHID_INPUT;
250 ev.u.input.size = size;
253 memcpy(ev.u.input.data, data, size);
255 err = bt_uhid_send(idev->uhid, &ev);
257 error("bt_uhid_send: %s (%d)", strerror(-err), -err);
261 DBG("HID report (%zu bytes)", size);
266 static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev)
271 uint8_t data[UHID_DATA_MAX + 1];
273 fd = g_io_channel_unix_get_fd(chan);
275 len = read(fd, data, sizeof(data));
277 error("BT socket read error: %s (%d)", strerror(errno), errno);
282 DBG("BT socket read returned 0 bytes");
287 if (hdr != (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
288 DBG("unsupported HIDP protocol header 0x%02x", hdr);
293 DBG("received empty HID report");
297 uhid_send_input_report(idev, data + 1, len - 1);
302 static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
304 struct input_device *idev = data;
307 if (cond & G_IO_IN) {
308 if (hidp_recv_intr_data(chan, idev) && (cond == G_IO_IN))
312 ba2str(&idev->dst, address);
314 DBG("Device %s disconnected", address);
316 /* Checking for ctrl_watch avoids a double g_io_channel_shutdown since
317 * it's likely that ctrl_watch_cb has been queued for dispatching in
318 * this mainloop iteration */
319 if ((cond & (G_IO_HUP | G_IO_ERR)) && idev->ctrl_watch)
320 g_io_channel_shutdown(chan, TRUE, NULL);
322 idev->intr_watch = 0;
325 g_io_channel_unref(idev->intr_io);
326 idev->intr_io = NULL;
329 /* Close control channel */
330 if (idev->ctrl_io && !(cond & G_IO_NVAL))
331 g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
333 btd_service_disconnecting_complete(idev->service, 0);
335 /* Enter the auto-reconnect mode if needed */
336 input_device_enter_reconnect_mode(idev);
341 static void hidp_recv_ctrl_handshake(struct input_device *idev, uint8_t param)
343 bool pending_req_complete = false;
344 uint8_t pending_req_type;
348 pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
351 case HIDP_HSHK_SUCCESSFUL:
352 if (pending_req_type == HIDP_TRANS_SET_REPORT) {
353 DBG("SET_REPORT successful");
354 pending_req_complete = true;
356 DBG("Spurious HIDP_HSHK_SUCCESSFUL");
359 case HIDP_HSHK_NOT_READY:
360 case HIDP_HSHK_ERR_INVALID_REPORT_ID:
361 case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
362 case HIDP_HSHK_ERR_INVALID_PARAMETER:
363 case HIDP_HSHK_ERR_UNKNOWN:
364 case HIDP_HSHK_ERR_FATAL:
365 if (pending_req_type == HIDP_TRANS_GET_REPORT) {
366 DBG("GET_REPORT failed (%u)", param);
367 uhid_send_feature_answer(idev, NULL, 0,
368 idev->report_rsp_id, EIO);
369 pending_req_complete = true;
370 } else if (pending_req_type == HIDP_TRANS_SET_REPORT) {
371 DBG("SET_REPORT failed (%u)", param);
372 pending_req_complete = true;
374 DBG("Spurious HIDP_HSHK_ERR");
376 if (param == HIDP_HSHK_ERR_FATAL)
377 hidp_send_ctrl_message(idev, HIDP_TRANS_HID_CONTROL |
378 HIDP_CTRL_SOFT_RESET, NULL, 0);
382 hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
383 HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
387 if (pending_req_complete) {
388 idev->report_req_pending = 0;
389 if (idev->report_req_timer > 0) {
390 g_source_remove(idev->report_req_timer);
391 idev->report_req_timer = 0;
393 idev->report_rsp_id = 0;
397 static void hidp_recv_ctrl_hid_control(struct input_device *idev, uint8_t param)
401 if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG)
402 connection_disconnect(idev, 0);
405 static void hidp_recv_ctrl_data(struct input_device *idev, uint8_t param,
406 const uint8_t *data, size_t size)
408 uint8_t pending_req_type;
409 uint8_t pending_req_param;
413 pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
414 if (pending_req_type != HIDP_TRANS_GET_REPORT) {
415 DBG("Spurious DATA on control channel");
419 pending_req_param = idev->report_req_pending & HIDP_HEADER_PARAM_MASK;
420 if (pending_req_param != param) {
421 DBG("Received DATA RTYPE doesn't match pending request RTYPE");
426 case HIDP_DATA_RTYPE_FEATURE:
427 case HIDP_DATA_RTYPE_INPUT:
428 case HIDP_DATA_RTYPE_OUPUT:
429 uhid_send_feature_answer(idev, data + 1, size - 1,
430 idev->report_rsp_id, 0);
433 case HIDP_DATA_RTYPE_OTHER:
434 DBG("Received DATA_RTYPE_OTHER");
438 hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
439 HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
443 idev->report_req_pending = 0;
444 if (idev->report_req_timer > 0) {
445 g_source_remove(idev->report_req_timer);
446 idev->report_req_timer = 0;
448 idev->report_rsp_id = 0;
451 static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev)
455 uint8_t hdr, type, param;
456 uint8_t data[UHID_DATA_MAX + 1];
458 fd = g_io_channel_unix_get_fd(chan);
460 len = read(fd, data, sizeof(data));
462 error("BT socket read error: %s (%d)", strerror(errno), errno);
467 DBG("BT socket read returned 0 bytes");
472 type = hdr & HIDP_HEADER_TRANS_MASK;
473 param = hdr & HIDP_HEADER_PARAM_MASK;
476 case HIDP_TRANS_HANDSHAKE:
477 hidp_recv_ctrl_handshake(idev, param);
479 case HIDP_TRANS_HID_CONTROL:
480 hidp_recv_ctrl_hid_control(idev, param);
482 case HIDP_TRANS_DATA:
483 hidp_recv_ctrl_data(idev, param, data, len);
486 error("unsupported HIDP control message");
487 hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
488 HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
495 static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
497 struct input_device *idev = data;
500 if (cond & G_IO_IN) {
501 if (hidp_recv_ctrl_message(chan, idev) && (cond == G_IO_IN))
505 ba2str(&idev->dst, address);
507 DBG("Device %s disconnected", address);
509 /* Checking for intr_watch avoids a double g_io_channel_shutdown since
510 * it's likely that intr_watch_cb has been queued for dispatching in
511 * this mainloop iteration */
512 if ((cond & (G_IO_HUP | G_IO_ERR)) && idev->intr_watch)
513 g_io_channel_shutdown(chan, TRUE, NULL);
515 idev->ctrl_watch = 0;
518 g_io_channel_unref(idev->ctrl_io);
519 idev->ctrl_io = NULL;
522 /* Close interrupt channel */
523 if (idev->intr_io && !(cond & G_IO_NVAL))
524 g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
529 #define REPORT_REQ_TIMEOUT 3
531 static gboolean hidp_report_req_timeout(gpointer data)
533 struct input_device *idev = data;
534 uint8_t pending_req_type;
535 const char *req_type_str;
538 ba2str(&idev->dst, address);
539 pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
541 switch (pending_req_type) {
542 case HIDP_TRANS_GET_REPORT:
543 req_type_str = "GET_REPORT";
545 case HIDP_TRANS_SET_REPORT:
546 req_type_str = "SET_REPORT";
549 /* Should never happen */
550 req_type_str = "OTHER_TRANS";
554 DBG("Device %s HIDP %s request timed out", address, req_type_str);
556 idev->report_req_pending = 0;
557 idev->report_req_timer = 0;
558 idev->report_rsp_id = 0;
563 static void hidp_send_set_report(struct uhid_event *ev, void *user_data)
565 struct input_device *idev = user_data;
571 switch (ev->u.output.rtype) {
572 case UHID_FEATURE_REPORT:
573 /* Send SET_REPORT on control channel */
574 if (idev->report_req_pending) {
575 DBG("Old GET_REPORT or SET_REPORT still pending");
579 hdr = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
580 sent = hidp_send_ctrl_message(idev, hdr, ev->u.output.data,
583 idev->report_req_pending = hdr;
584 idev->report_req_timer =
585 g_timeout_add_seconds(REPORT_REQ_TIMEOUT,
586 hidp_report_req_timeout, idev);
589 case UHID_OUTPUT_REPORT:
590 /* Send DATA on interrupt channel */
591 hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
592 hidp_send_intr_message(idev, hdr, ev->u.output.data,
596 DBG("Unsupported HID report type %u", ev->u.output.rtype);
601 static void hidp_send_get_report(struct uhid_event *ev, void *user_data)
603 struct input_device *idev = user_data;
609 if (idev->report_req_pending) {
610 DBG("Old GET_REPORT or SET_REPORT still pending");
611 uhid_send_feature_answer(idev, NULL, 0, ev->u.feature.id,
616 /* Send GET_REPORT on control channel */
617 switch (ev->u.feature.rtype) {
618 case UHID_FEATURE_REPORT:
619 hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
621 case UHID_INPUT_REPORT:
622 hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
624 case UHID_OUTPUT_REPORT:
625 hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUPUT;
628 DBG("Unsupported HID report type %u", ev->u.feature.rtype);
632 sent = hidp_send_ctrl_message(idev, hdr, &ev->u.feature.rnum,
633 sizeof(ev->u.feature.rnum));
635 idev->report_req_pending = hdr;
636 idev->report_req_timer =
637 g_timeout_add_seconds(REPORT_REQ_TIMEOUT,
638 hidp_report_req_timeout, idev);
639 idev->report_rsp_id = ev->u.feature.id;
643 static void epox_endian_quirk(unsigned char *data, int size)
645 /* USAGE_PAGE (Keyboard) 05 07
646 * USAGE_MINIMUM (0) 19 00
647 * USAGE_MAXIMUM (65280) 2A 00 FF <= must be FF 00
648 * LOGICAL_MINIMUM (0) 15 00
649 * LOGICAL_MAXIMUM (65280) 26 00 FF <= must be FF 00
651 unsigned char pattern[] = { 0x05, 0x07, 0x19, 0x00, 0x2a, 0x00, 0xff,
652 0x15, 0x00, 0x26, 0x00, 0xff };
658 for (i = 0; i < size - sizeof(pattern); i++) {
659 if (!memcmp(data + i, pattern, sizeof(pattern))) {
668 static int create_hid_dev_name(sdp_record_t *rec, struct hidp_connadd_req *req)
670 char sdesc[sizeof(req->name)];
672 if (sdp_get_service_desc(rec, sdesc, sizeof(sdesc)) == 0) {
673 char pname[sizeof(req->name)];
675 if (sdp_get_provider_name(rec, pname, sizeof(pname)) == 0 &&
676 strncmp(sdesc, pname, 5) != 0)
677 snprintf(req->name, sizeof(req->name), "%s %s", pname,
680 snprintf(req->name, sizeof(req->name), "%s", sdesc);
682 return sdp_get_service_name(rec, req->name, sizeof(req->name));
688 /* See HID profile specification v1.0, "7.11.6 HIDDescriptorList" for details
689 * on the attribute format. */
690 static int extract_hid_desc_data(sdp_record_t *rec,
691 struct hidp_connadd_req *req)
695 d = sdp_data_get(rec, SDP_ATTR_HID_DESCRIPTOR_LIST);
699 if (!SDP_IS_SEQ(d->dtd))
702 /* First HIDDescriptor */
704 if (!SDP_IS_SEQ(d->dtd))
707 /* ClassDescriptorType */
709 if (d->dtd != SDP_UINT8)
712 /* ClassDescriptorData */
714 if (!d || !SDP_IS_TEXT_STR(d->dtd))
717 req->rd_data = g_try_malloc0(d->unitSize);
719 memcpy(req->rd_data, d->val.str, d->unitSize);
720 req->rd_size = d->unitSize;
721 epox_endian_quirk(req->rd_data, req->rd_size);
727 error("Missing or invalid HIDDescriptorList SDP attribute");
731 static int extract_hid_record(sdp_record_t *rec, struct hidp_connadd_req *req)
737 err = create_hid_dev_name(rec, req);
739 DBG("No valid Service Name or Service Description found");
741 pdlist = sdp_data_get(rec, SDP_ATTR_HID_PARSER_VERSION);
742 req->parser = pdlist ? pdlist->val.uint16 : 0x0100;
744 pdlist = sdp_data_get(rec, SDP_ATTR_HID_DEVICE_SUBCLASS);
745 req->subclass = pdlist ? pdlist->val.uint8 : 0;
747 pdlist = sdp_data_get(rec, SDP_ATTR_HID_COUNTRY_CODE);
748 req->country = pdlist ? pdlist->val.uint8 : 0;
750 pdlist = sdp_data_get(rec, SDP_ATTR_HID_VIRTUAL_CABLE);
751 attr_val = pdlist ? pdlist->val.uint8 : 0;
753 req->flags |= (1 << HIDP_VIRTUAL_CABLE_UNPLUG);
755 pdlist = sdp_data_get(rec, SDP_ATTR_HID_BOOT_DEVICE);
756 attr_val = pdlist ? pdlist->val.uint8 : 0;
758 req->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
760 err = extract_hid_desc_data(rec, req);
767 static int ioctl_connadd(struct hidp_connadd_req *req)
771 ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
775 if (ioctl(ctl, HIDPCONNADD, req) < 0)
783 static bool ioctl_is_connected(struct input_device *idev)
785 struct hidp_conninfo ci;
789 ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
791 error("Can't open HIDP control socket");
795 memset(&ci, 0, sizeof(ci));
796 bacpy(&ci.bdaddr, &idev->dst);
797 if (ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) {
798 error("Can't get HIDP connection info");
805 if (ci.state != BT_CONNECTED)
811 static int ioctl_disconnect(struct input_device *idev, uint32_t flags)
813 struct hidp_conndel_req req;
814 struct hidp_conninfo ci;
817 ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
819 error("Can't open HIDP control socket");
823 memset(&ci, 0, sizeof(ci));
824 bacpy(&ci.bdaddr, &idev->dst);
825 if ((ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) ||
826 (ci.state != BT_CONNECTED)) {
831 memset(&req, 0, sizeof(req));
832 bacpy(&req.bdaddr, &idev->dst);
834 if (ioctl(ctl, HIDPCONNDEL, &req) < 0) {
836 error("Can't delete the HID device: %s (%d)",
837 strerror(-err), -err);
845 static int uhid_connadd(struct input_device *idev, struct hidp_connadd_req *req)
848 struct uhid_event ev;
850 if (idev->uhid_created)
853 /* create uHID device */
854 memset(&ev, 0, sizeof(ev));
855 ev.type = UHID_CREATE;
856 strncpy((char *) ev.u.create.name, req->name,
857 sizeof(ev.u.create.name) - 1);
858 ba2str(&idev->src, (char *) ev.u.create.phys);
859 ba2str(&idev->dst, (char *) ev.u.create.uniq);
860 ev.u.create.vendor = req->vendor;
861 ev.u.create.product = req->product;
862 ev.u.create.version = req->version;
863 ev.u.create.country = req->country;
864 ev.u.create.bus = BUS_BLUETOOTH;
865 ev.u.create.rd_data = req->rd_data;
866 ev.u.create.rd_size = req->rd_size;
868 err = bt_uhid_send(idev->uhid, &ev);
870 error("bt_uhid_send: %s", strerror(-err));
874 bt_uhid_register(idev->uhid, UHID_OUTPUT, hidp_send_set_report, idev);
875 bt_uhid_register(idev->uhid, UHID_FEATURE, hidp_send_get_report, idev);
877 idev->uhid_created = true;
882 static gboolean encrypt_notify(GIOChannel *io, GIOCondition condition,
885 struct input_device *idev = data;
891 err = uhid_connadd(idev, idev->req);
893 err = ioctl_connadd(idev->req);
896 error("ioctl_connadd(): %s (%d)", strerror(-err), -err);
899 g_io_channel_shutdown(idev->ctrl_io, FALSE, NULL);
900 g_io_channel_unref(idev->ctrl_io);
901 idev->ctrl_io = NULL;
905 g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
906 g_io_channel_unref(idev->intr_io);
907 idev->intr_io = NULL;
913 g_free(idev->req->rd_data);
920 static int hidp_add_connection(struct input_device *idev)
922 struct hidp_connadd_req *req;
924 char src_addr[18], dst_addr[18];
925 char filename[PATH_MAX];
927 char handle[11], *str;
931 req = g_new0(struct hidp_connadd_req, 1);
932 req->ctrl_sock = g_io_channel_unix_get_fd(idev->ctrl_io);
933 req->intr_sock = g_io_channel_unix_get_fd(idev->intr_io);
935 req->idle_to = idle_timeout;
937 ba2str(&idev->src, src_addr);
938 ba2str(&idev->dst, dst_addr);
940 snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", src_addr,
942 sprintf(handle, "0x%8.8X", idev->handle);
944 key_file = g_key_file_new();
945 g_key_file_load_from_file(key_file, filename, 0, NULL);
946 str = g_key_file_get_string(key_file, "ServiceRecords", handle, NULL);
947 g_key_file_free(key_file);
950 error("Rejected connection from unknown device %s", dst_addr);
955 rec = record_from_string(str);
958 err = extract_hid_record(rec, req);
959 sdp_record_free(rec);
961 error("Could not parse HID SDP record: %s (%d)", strerror(-err),
966 req->vendor = btd_device_get_vendor(idev->device);
967 req->product = btd_device_get_product(idev->device);
968 req->version = btd_device_get_version(idev->device);
970 if (device_name_known(idev->device))
971 device_get_name(idev->device, req->name, sizeof(req->name));
973 /* Encryption is mandatory for keyboards */
974 if (req->subclass & 0x40) {
975 if (!bt_io_set(idev->intr_io, &gerr,
976 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
977 BT_IO_OPT_INVALID)) {
978 error("btio: %s", gerr->message);
985 idev->sec_watch = g_io_add_watch(idev->intr_io, G_IO_OUT,
986 encrypt_notify, idev);
992 err = uhid_connadd(idev, req);
994 err = ioctl_connadd(req);
997 g_free(req->rd_data);
1003 static bool is_connected(struct input_device *idev)
1006 return (idev->intr_io != NULL && idev->ctrl_io != NULL);
1008 return ioctl_is_connected(idev);
1011 static int connection_disconnect(struct input_device *idev, uint32_t flags)
1013 if (!is_connected(idev))
1016 /* Standard HID disconnect */
1018 g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
1020 g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
1025 return ioctl_disconnect(idev, flags);
1028 static int input_device_connected(struct input_device *idev)
1032 if (idev->intr_io == NULL || idev->ctrl_io == NULL)
1035 err = hidp_add_connection(idev);
1039 btd_service_connecting_complete(idev->service, 0);
1044 static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
1047 struct input_device *idev = user_data;
1048 GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
1056 err = input_device_connected(idev);
1063 idev->intr_watch = g_io_add_watch(idev->intr_io, cond, intr_watch_cb,
1069 btd_service_connecting_complete(idev->service, err);
1071 /* So we guarantee the interrupt channel is closed before the
1072 * control channel (if we only do unref GLib will close it only
1073 * after returning control to the mainloop */
1075 g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
1077 g_io_channel_unref(idev->intr_io);
1078 idev->intr_io = NULL;
1080 if (idev->ctrl_io) {
1081 g_io_channel_unref(idev->ctrl_io);
1082 idev->ctrl_io = NULL;
1086 static void control_connect_cb(GIOChannel *chan, GError *conn_err,
1089 struct input_device *idev = user_data;
1090 GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
1095 error("%s", conn_err->message);
1099 /* Connect to the HID interrupt channel */
1100 io = bt_io_connect(interrupt_connect_cb, idev,
1102 BT_IO_OPT_SOURCE_BDADDR, &idev->src,
1103 BT_IO_OPT_DEST_BDADDR, &idev->dst,
1104 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
1105 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
1108 error("%s", err->message);
1118 idev->ctrl_watch = g_io_add_watch(idev->ctrl_io, cond, ctrl_watch_cb,
1124 btd_service_connecting_complete(idev->service, -EIO);
1125 g_io_channel_unref(idev->ctrl_io);
1126 idev->ctrl_io = NULL;
1129 static int dev_connect(struct input_device *idev)
1134 if (idev->disable_sdp)
1135 bt_clear_cached_session(&idev->src, &idev->dst);
1137 io = bt_io_connect(control_connect_cb, idev,
1139 BT_IO_OPT_SOURCE_BDADDR, &idev->src,
1140 BT_IO_OPT_DEST_BDADDR, &idev->dst,
1141 BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
1142 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
1149 error("%s", err->message);
1155 static gboolean input_device_auto_reconnect(gpointer user_data)
1157 struct input_device *idev = user_data;
1159 DBG("path=%s, attempt=%d", idev->path, idev->reconnect_attempt);
1161 /* Stop the recurrent reconnection attempts if the device is
1162 * reconnected or is marked for removal.
1164 if (device_is_temporary(idev->device) ||
1165 btd_device_is_connected(idev->device))
1168 /* Only attempt an auto-reconnect for at most 3 minutes (6 * 30s). */
1169 if (idev->reconnect_attempt >= 6)
1172 /* Check if the profile is already connected. */
1176 if (is_connected(idev))
1179 idev->reconnect_attempt++;
1185 static const char * const _reconnect_mode_str[] = {
1192 static const char *reconnect_mode_to_string(const enum reconnect_mode_t mode)
1194 return _reconnect_mode_str[mode];
1197 static void input_device_enter_reconnect_mode(struct input_device *idev)
1199 DBG("path=%s reconnect_mode=%s", idev->path,
1200 reconnect_mode_to_string(idev->reconnect_mode));
1202 /* Only attempt an auto-reconnect when the device is required to
1203 * accept reconnections from the host.
1205 if (idev->reconnect_mode != RECONNECT_ANY &&
1206 idev->reconnect_mode != RECONNECT_HOST)
1209 /* If the device is temporary we are not required to reconnect
1210 * with the device. This is likely the case of a removing device.
1212 if (device_is_temporary(idev->device) ||
1213 btd_device_is_connected(idev->device))
1216 if (idev->reconnect_timer > 0)
1217 g_source_remove(idev->reconnect_timer);
1219 DBG("registering auto-reconnect");
1220 idev->reconnect_attempt = 0;
1221 idev->reconnect_timer = g_timeout_add_seconds(30,
1222 input_device_auto_reconnect, idev);
1226 int input_device_connect(struct btd_service *service)
1228 struct input_device *idev;
1232 idev = btd_service_get_user_data(service);
1237 if (is_connected(idev))
1240 return dev_connect(idev);
1243 int input_device_disconnect(struct btd_service *service)
1245 struct input_device *idev;
1250 idev = btd_service_get_user_data(service);
1252 flags = device_is_temporary(idev->device) ?
1253 (1 << HIDP_VIRTUAL_CABLE_UNPLUG) : 0;
1255 err = connection_disconnect(idev, flags);
1262 static bool is_device_sdp_disable(const sdp_record_t *rec)
1266 data = sdp_data_get(rec, SDP_ATTR_HID_SDP_DISABLE);
1268 return data && data->val.uint8;
1271 static enum reconnect_mode_t hid_reconnection_mode(bool reconnect_initiate,
1272 bool normally_connectable)
1274 if (!reconnect_initiate && !normally_connectable)
1275 return RECONNECT_NONE;
1276 else if (!reconnect_initiate && normally_connectable)
1277 return RECONNECT_HOST;
1278 else if (reconnect_initiate && !normally_connectable)
1279 return RECONNECT_DEVICE;
1280 else /* (reconnect_initiate && normally_connectable) */
1281 return RECONNECT_ANY;
1284 static void extract_hid_props(struct input_device *idev,
1285 const sdp_record_t *rec)
1287 /* Extract HID connectability */
1288 bool reconnect_initiate, normally_connectable;
1291 /* HIDNormallyConnectable is optional and assumed FALSE
1292 * if not present. */
1293 pdlist = sdp_data_get(rec, SDP_ATTR_HID_RECONNECT_INITIATE);
1294 reconnect_initiate = pdlist ? pdlist->val.uint8 : TRUE;
1296 pdlist = sdp_data_get(rec, SDP_ATTR_HID_NORMALLY_CONNECTABLE);
1297 normally_connectable = pdlist ? pdlist->val.uint8 : FALSE;
1299 /* Update local values */
1300 idev->reconnect_mode =
1301 hid_reconnection_mode(reconnect_initiate, normally_connectable);
1304 static struct input_device *input_device_new(struct btd_service *service)
1306 struct btd_device *device = btd_service_get_device(service);
1307 struct btd_profile *p = btd_service_get_profile(service);
1308 const char *path = device_get_path(device);
1309 const sdp_record_t *rec = btd_device_get_record(device, p->remote_uuid);
1310 struct btd_adapter *adapter = device_get_adapter(device);
1311 struct input_device *idev;
1316 idev = g_new0(struct input_device, 1);
1317 bacpy(&idev->src, btd_adapter_get_address(adapter));
1318 bacpy(&idev->dst, device_get_address(device));
1319 idev->service = btd_service_ref(service);
1320 idev->device = btd_device_ref(device);
1321 idev->path = g_strdup(path);
1322 idev->handle = rec->handle;
1323 idev->disable_sdp = is_device_sdp_disable(rec);
1325 /* Initialize device properties */
1326 extract_hid_props(idev, rec);
1331 static gboolean property_get_reconnect_mode(
1332 const GDBusPropertyTable *property,
1333 DBusMessageIter *iter, void *data)
1335 struct input_device *idev = data;
1336 const char *str_mode = reconnect_mode_to_string(idev->reconnect_mode);
1338 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str_mode);
1343 static const GDBusPropertyTable input_properties[] = {
1344 { "ReconnectMode", "s", property_get_reconnect_mode },
1348 int input_device_register(struct btd_service *service)
1350 struct btd_device *device = btd_service_get_device(service);
1351 const char *path = device_get_path(device);
1352 struct input_device *idev;
1356 idev = input_device_new(service);
1361 idev->uhid = bt_uhid_new_default();
1363 error("bt_uhid_new_default: failed");
1364 input_device_free(idev);
1369 if (g_dbus_register_interface(btd_get_dbus_connection(),
1370 idev->path, INPUT_INTERFACE,
1372 input_properties, idev,
1374 error("Unable to register %s interface", INPUT_INTERFACE);
1375 input_device_free(idev);
1379 btd_service_set_user_data(service, idev);
1384 static struct input_device *find_device(const bdaddr_t *src,
1385 const bdaddr_t *dst)
1387 struct btd_device *device;
1388 struct btd_service *service;
1390 device = btd_adapter_find_device(adapter_find(src), dst, BDADDR_BREDR);
1394 service = btd_device_get_service(device, HID_UUID);
1395 if (service == NULL)
1398 return btd_service_get_user_data(service);
1401 void input_device_unregister(struct btd_service *service)
1403 struct btd_device *device = btd_service_get_device(service);
1404 const char *path = device_get_path(device);
1405 struct input_device *idev = btd_service_get_user_data(service);
1409 g_dbus_unregister_interface(btd_get_dbus_connection(),
1410 idev->path, INPUT_INTERFACE);
1412 input_device_free(idev);
1415 static int input_device_connadd(struct input_device *idev)
1419 err = input_device_connected(idev);
1423 if (idev->ctrl_io) {
1424 g_io_channel_shutdown(idev->ctrl_io, FALSE, NULL);
1425 g_io_channel_unref(idev->ctrl_io);
1426 idev->ctrl_io = NULL;
1429 if (idev->intr_io) {
1430 g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
1431 g_io_channel_unref(idev->intr_io);
1432 idev->intr_io = NULL;
1438 bool input_device_exists(const bdaddr_t *src, const bdaddr_t *dst)
1440 if (find_device(src, dst))
1446 int input_device_set_channel(const bdaddr_t *src, const bdaddr_t *dst, int psm,
1449 struct input_device *idev = find_device(src, dst);
1450 GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
1452 DBG("idev %p psm %d", idev, psm);
1461 case L2CAP_PSM_HIDP_CTRL:
1464 idev->ctrl_io = g_io_channel_ref(io);
1465 idev->ctrl_watch = g_io_add_watch(idev->ctrl_io, cond,
1466 ctrl_watch_cb, idev);
1468 case L2CAP_PSM_HIDP_INTR:
1471 idev->intr_io = g_io_channel_ref(io);
1472 idev->intr_watch = g_io_add_watch(idev->intr_io, cond,
1473 intr_watch_cb, idev);
1477 if (idev->intr_io && idev->ctrl_io)
1478 input_device_connadd(idev);
1483 int input_device_close_channels(const bdaddr_t *src, const bdaddr_t *dst)
1485 struct input_device *idev = find_device(src, dst);
1491 g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
1494 g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);