device.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  7. * Copyright (C) 2014 Google Inc.
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdlib.h>
  16. #include <stdbool.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <sys/ioctl.h>
  21. #include "lib/bluetooth.h"
  22. #include "lib/hidp.h"
  23. #include "lib/sdp.h"
  24. #include "lib/sdp_lib.h"
  25. #include "lib/uuid.h"
  26. #include "gdbus/gdbus.h"
  27. #include "btio/btio.h"
  28. #include "src/log.h"
  29. #include "src/adapter.h"
  30. #include "src/device.h"
  31. #include "src/profile.h"
  32. #include "src/service.h"
  33. #include "src/storage.h"
  34. #include "src/dbus-common.h"
  35. #include "src/error.h"
  36. #include "src/sdp-client.h"
  37. #include "src/shared/timeout.h"
  38. #include "src/shared/uhid.h"
  39. #include "device.h"
  40. #include "hidp_defs.h"
  41. #define INPUT_INTERFACE "org.bluez.Input1"
  42. enum reconnect_mode_t {
  43. RECONNECT_NONE = 0,
  44. RECONNECT_DEVICE,
  45. RECONNECT_HOST,
  46. RECONNECT_ANY
  47. };
  48. struct input_device {
  49. struct btd_service *service;
  50. struct btd_device *device;
  51. char *path;
  52. bdaddr_t src;
  53. bdaddr_t dst;
  54. uint32_t handle;
  55. GIOChannel *ctrl_io;
  56. GIOChannel *intr_io;
  57. guint ctrl_watch;
  58. guint intr_watch;
  59. guint sec_watch;
  60. struct hidp_connadd_req *req;
  61. bool disable_sdp;
  62. enum reconnect_mode_t reconnect_mode;
  63. unsigned int reconnect_timer;
  64. uint32_t reconnect_attempt;
  65. struct bt_uhid *uhid;
  66. bool uhid_created;
  67. uint8_t report_req_pending;
  68. unsigned int report_req_timer;
  69. uint32_t report_rsp_id;
  70. bool virtual_cable_unplug;
  71. };
  72. static int idle_timeout = 0;
  73. static bool uhid_enabled = false;
  74. static bool classic_bonded_only = false;
  75. void input_set_idle_timeout(int timeout)
  76. {
  77. idle_timeout = timeout;
  78. }
  79. void input_enable_userspace_hid(bool state)
  80. {
  81. uhid_enabled = state;
  82. }
  83. void input_set_classic_bonded_only(bool state)
  84. {
  85. classic_bonded_only = state;
  86. }
  87. bool input_get_classic_bonded_only(void)
  88. {
  89. return classic_bonded_only;
  90. }
  91. static void input_device_enter_reconnect_mode(struct input_device *idev);
  92. static int connection_disconnect(struct input_device *idev, uint32_t flags);
  93. static int uhid_disconnect(struct input_device *idev);
  94. static bool input_device_bonded(struct input_device *idev)
  95. {
  96. return device_is_bonded(idev->device,
  97. btd_device_get_bdaddr_type(idev->device));
  98. }
  99. static void input_device_free(struct input_device *idev)
  100. {
  101. bt_uhid_unref(idev->uhid);
  102. btd_service_unref(idev->service);
  103. btd_device_unref(idev->device);
  104. g_free(idev->path);
  105. if (idev->ctrl_watch > 0)
  106. g_source_remove(idev->ctrl_watch);
  107. if (idev->intr_watch > 0)
  108. g_source_remove(idev->intr_watch);
  109. if (idev->sec_watch > 0)
  110. g_source_remove(idev->sec_watch);
  111. if (idev->intr_io)
  112. g_io_channel_unref(idev->intr_io);
  113. if (idev->ctrl_io)
  114. g_io_channel_unref(idev->ctrl_io);
  115. if (idev->req) {
  116. g_free(idev->req->rd_data);
  117. g_free(idev->req);
  118. }
  119. if (idev->reconnect_timer > 0)
  120. timeout_remove(idev->reconnect_timer);
  121. if (idev->report_req_timer > 0)
  122. timeout_remove(idev->report_req_timer);
  123. g_free(idev);
  124. }
  125. static void virtual_cable_unplug(struct input_device *idev)
  126. {
  127. device_remove_bonding(idev->device,
  128. btd_device_get_bdaddr_type(idev->device));
  129. idev->virtual_cable_unplug = false;
  130. }
  131. static bool hidp_send_message(GIOChannel *chan, uint8_t hdr,
  132. const uint8_t *data, size_t size)
  133. {
  134. int fd;
  135. ssize_t len;
  136. uint8_t msg[size + 1];
  137. if (!chan) {
  138. error("BT socket not connected");
  139. return false;
  140. }
  141. if (data == NULL)
  142. size = 0;
  143. msg[0] = hdr;
  144. if (size > 0)
  145. memcpy(&msg[1], data, size);
  146. ++size;
  147. fd = g_io_channel_unix_get_fd(chan);
  148. len = write(fd, msg, size);
  149. if (len < 0) {
  150. error("BT socket write error: %s (%d)", strerror(errno), errno);
  151. return false;
  152. }
  153. if ((size_t) len < size) {
  154. error("BT socket write error: partial write (%zd of %zu bytes)",
  155. len, size);
  156. return false;
  157. }
  158. return true;
  159. }
  160. static bool hidp_send_ctrl_message(struct input_device *idev, uint8_t hdr,
  161. const uint8_t *data, size_t size)
  162. {
  163. if (hdr == (HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG))
  164. idev->virtual_cable_unplug = true;
  165. return hidp_send_message(idev->ctrl_io, hdr, data, size);
  166. }
  167. static bool hidp_send_intr_message(struct input_device *idev, uint8_t hdr,
  168. const uint8_t *data, size_t size)
  169. {
  170. return hidp_send_message(idev->intr_io, hdr, data, size);
  171. }
  172. static bool uhid_send_get_report_reply(struct input_device *idev,
  173. const uint8_t *data, size_t size,
  174. uint32_t id, uint16_t err)
  175. {
  176. struct uhid_event ev;
  177. int ret;
  178. if (data == NULL)
  179. size = 0;
  180. if (size > sizeof(ev.u.get_report_reply.data))
  181. size = sizeof(ev.u.get_report_reply.data);
  182. if (!idev->uhid_created) {
  183. DBG("HID report (%zu bytes) dropped", size);
  184. return false;
  185. }
  186. memset(&ev, 0, sizeof(ev));
  187. ev.type = UHID_GET_REPORT_REPLY;
  188. ev.u.get_report_reply.id = id;
  189. ev.u.get_report_reply.err = err;
  190. ev.u.get_report_reply.size = size;
  191. if (size > 0)
  192. memcpy(ev.u.get_report_reply.data, data, size);
  193. ret = bt_uhid_send(idev->uhid, &ev);
  194. if (ret < 0) {
  195. error("bt_uhid_send: %s (%d)", strerror(-ret), -ret);
  196. return false;
  197. }
  198. DBG("HID report (%zu bytes)", size);
  199. return true;
  200. }
  201. static bool uhid_send_set_report_reply(struct input_device *idev,
  202. uint32_t id, uint16_t err)
  203. {
  204. struct uhid_event ev;
  205. int ret;
  206. if (!idev->uhid_created)
  207. return false;
  208. memset(&ev, 0, sizeof(ev));
  209. ev.type = UHID_SET_REPORT_REPLY;
  210. ev.u.set_report_reply.id = id;
  211. ev.u.set_report_reply.err = err;
  212. ret = bt_uhid_send(idev->uhid, &ev);
  213. if (ret < 0) {
  214. error("bt_uhid_send: %s (%d)", strerror(-ret), -ret);
  215. return false;
  216. }
  217. return true;
  218. }
  219. static bool uhid_send_input_report(struct input_device *idev,
  220. const uint8_t *data, size_t size)
  221. {
  222. struct uhid_event ev;
  223. int err;
  224. if (data == NULL)
  225. size = 0;
  226. if (size > sizeof(ev.u.input.data))
  227. size = sizeof(ev.u.input.data);
  228. if (!idev->uhid_created) {
  229. DBG("HID report (%zu bytes) dropped", size);
  230. return false;
  231. }
  232. memset(&ev, 0, sizeof(ev));
  233. ev.type = UHID_INPUT;
  234. ev.u.input.size = size;
  235. if (size > 0)
  236. memcpy(ev.u.input.data, data, size);
  237. err = bt_uhid_send(idev->uhid, &ev);
  238. if (err < 0) {
  239. error("bt_uhid_send: %s (%d)", strerror(-err), -err);
  240. return false;
  241. }
  242. DBG("HID report (%zu bytes)", size);
  243. return true;
  244. }
  245. static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev)
  246. {
  247. int fd;
  248. ssize_t len;
  249. uint8_t hdr;
  250. uint8_t data[UHID_DATA_MAX + 1];
  251. fd = g_io_channel_unix_get_fd(chan);
  252. len = read(fd, data, sizeof(data));
  253. if (len < 0) {
  254. error("BT socket read error: %s (%d)", strerror(errno), errno);
  255. return false;
  256. }
  257. if (len == 0) {
  258. DBG("BT socket read returned 0 bytes");
  259. return true;
  260. }
  261. hdr = data[0];
  262. if (hdr != (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) {
  263. DBG("unsupported HIDP protocol header 0x%02x", hdr);
  264. return true;
  265. }
  266. if (len < 2) {
  267. DBG("received empty HID report");
  268. return true;
  269. }
  270. uhid_send_input_report(idev, data + 1, len - 1);
  271. return true;
  272. }
  273. static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
  274. {
  275. struct input_device *idev = data;
  276. char address[18];
  277. if (cond & G_IO_IN) {
  278. if (hidp_recv_intr_data(chan, idev) && (cond == G_IO_IN))
  279. return TRUE;
  280. }
  281. ba2str(&idev->dst, address);
  282. DBG("Device %s disconnected", address);
  283. /* Checking for ctrl_watch avoids a double g_io_channel_shutdown since
  284. * it's likely that ctrl_watch_cb has been queued for dispatching in
  285. * this mainloop iteration */
  286. if ((cond & (G_IO_HUP | G_IO_ERR)) && idev->ctrl_watch)
  287. g_io_channel_shutdown(chan, TRUE, NULL);
  288. idev->intr_watch = 0;
  289. if (idev->intr_io) {
  290. g_io_channel_unref(idev->intr_io);
  291. idev->intr_io = NULL;
  292. }
  293. /* Close control channel if the closing of interrupt channel is not
  294. * initiated by the other party
  295. */
  296. if (idev->ctrl_io && !(cond & (G_IO_NVAL | G_IO_ERR)))
  297. g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
  298. btd_service_disconnecting_complete(idev->service, 0);
  299. /* Enter the auto-reconnect mode if needed */
  300. input_device_enter_reconnect_mode(idev);
  301. if (!idev->ctrl_io && idev->virtual_cable_unplug)
  302. virtual_cable_unplug(idev);
  303. /* If connection abruptly ended, uhid might be not yet disconnected */
  304. if (idev->uhid_created)
  305. uhid_disconnect(idev);
  306. return FALSE;
  307. }
  308. static void hidp_recv_ctrl_handshake(struct input_device *idev, uint8_t param)
  309. {
  310. bool pending_req_complete = false;
  311. uint8_t pending_req_type;
  312. DBG("");
  313. pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
  314. switch (param) {
  315. case HIDP_HSHK_SUCCESSFUL:
  316. if (pending_req_type == HIDP_TRANS_SET_REPORT) {
  317. DBG("SET_REPORT successful");
  318. pending_req_complete = true;
  319. } else
  320. DBG("Spurious HIDP_HSHK_SUCCESSFUL");
  321. break;
  322. case HIDP_HSHK_NOT_READY:
  323. case HIDP_HSHK_ERR_INVALID_REPORT_ID:
  324. case HIDP_HSHK_ERR_UNSUPPORTED_REQUEST:
  325. case HIDP_HSHK_ERR_INVALID_PARAMETER:
  326. case HIDP_HSHK_ERR_UNKNOWN:
  327. case HIDP_HSHK_ERR_FATAL:
  328. if (pending_req_type == HIDP_TRANS_GET_REPORT) {
  329. DBG("GET_REPORT failed (%u)", param);
  330. uhid_send_get_report_reply(idev, NULL, 0,
  331. idev->report_rsp_id, EIO);
  332. pending_req_complete = true;
  333. } else if (pending_req_type == HIDP_TRANS_SET_REPORT) {
  334. DBG("SET_REPORT failed (%u)", param);
  335. uhid_send_set_report_reply(idev, idev->report_rsp_id,
  336. EIO);
  337. pending_req_complete = true;
  338. } else
  339. DBG("Spurious HIDP_HSHK_ERR");
  340. if (param == HIDP_HSHK_ERR_FATAL)
  341. hidp_send_ctrl_message(idev, HIDP_TRANS_HID_CONTROL |
  342. HIDP_CTRL_SOFT_RESET, NULL, 0);
  343. break;
  344. default:
  345. hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
  346. HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
  347. break;
  348. }
  349. if (pending_req_complete) {
  350. idev->report_req_pending = 0;
  351. if (idev->report_req_timer > 0) {
  352. timeout_remove(idev->report_req_timer);
  353. idev->report_req_timer = 0;
  354. }
  355. idev->report_rsp_id = 0;
  356. }
  357. }
  358. static void hidp_recv_ctrl_hid_control(struct input_device *idev, uint8_t param)
  359. {
  360. DBG("");
  361. if (param == HIDP_CTRL_VIRTUAL_CABLE_UNPLUG)
  362. connection_disconnect(idev, (1 << HIDP_VIRTUAL_CABLE_UNPLUG));
  363. }
  364. static void hidp_recv_ctrl_data(struct input_device *idev, uint8_t param,
  365. const uint8_t *data, size_t size)
  366. {
  367. uint8_t pending_req_type;
  368. uint8_t pending_req_param;
  369. DBG("");
  370. pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
  371. if (pending_req_type != HIDP_TRANS_GET_REPORT &&
  372. pending_req_type != HIDP_TRANS_SET_REPORT) {
  373. DBG("Spurious DATA on control channel");
  374. return;
  375. }
  376. pending_req_param = idev->report_req_pending & HIDP_HEADER_PARAM_MASK;
  377. if (pending_req_param != param) {
  378. DBG("Received DATA RTYPE doesn't match pending request RTYPE");
  379. return;
  380. }
  381. switch (param) {
  382. case HIDP_DATA_RTYPE_FEATURE:
  383. case HIDP_DATA_RTYPE_INPUT:
  384. case HIDP_DATA_RTYPE_OUTPUT:
  385. if (pending_req_type == HIDP_TRANS_GET_REPORT)
  386. uhid_send_get_report_reply(idev, data + 1, size - 1,
  387. idev->report_rsp_id, 0);
  388. else
  389. uhid_send_set_report_reply(idev, idev->report_rsp_id,
  390. 0);
  391. break;
  392. case HIDP_DATA_RTYPE_OTHER:
  393. DBG("Received DATA_RTYPE_OTHER");
  394. break;
  395. default:
  396. hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
  397. HIDP_HSHK_ERR_INVALID_PARAMETER, NULL, 0);
  398. break;
  399. }
  400. idev->report_req_pending = 0;
  401. if (idev->report_req_timer > 0) {
  402. timeout_remove(idev->report_req_timer);
  403. idev->report_req_timer = 0;
  404. }
  405. idev->report_rsp_id = 0;
  406. }
  407. static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev)
  408. {
  409. int fd;
  410. ssize_t len;
  411. uint8_t hdr, type, param;
  412. uint8_t data[UHID_DATA_MAX + 1];
  413. fd = g_io_channel_unix_get_fd(chan);
  414. len = read(fd, data, sizeof(data));
  415. if (len < 0) {
  416. error("BT socket read error: %s (%d)", strerror(errno), errno);
  417. return false;
  418. }
  419. if (len == 0) {
  420. DBG("BT socket read returned 0 bytes");
  421. return true;
  422. }
  423. hdr = data[0];
  424. type = hdr & HIDP_HEADER_TRANS_MASK;
  425. param = hdr & HIDP_HEADER_PARAM_MASK;
  426. switch (type) {
  427. case HIDP_TRANS_HANDSHAKE:
  428. hidp_recv_ctrl_handshake(idev, param);
  429. break;
  430. case HIDP_TRANS_HID_CONTROL:
  431. hidp_recv_ctrl_hid_control(idev, param);
  432. break;
  433. case HIDP_TRANS_DATA:
  434. hidp_recv_ctrl_data(idev, param, data, len);
  435. break;
  436. default:
  437. error("unsupported HIDP control message");
  438. hidp_send_ctrl_message(idev, HIDP_TRANS_HANDSHAKE |
  439. HIDP_HSHK_ERR_UNSUPPORTED_REQUEST, NULL, 0);
  440. break;
  441. }
  442. return true;
  443. }
  444. static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
  445. {
  446. struct input_device *idev = data;
  447. char address[18];
  448. if (cond & G_IO_IN) {
  449. if (hidp_recv_ctrl_message(chan, idev) && (cond == G_IO_IN))
  450. return TRUE;
  451. }
  452. ba2str(&idev->dst, address);
  453. DBG("Device %s disconnected", address);
  454. /* Checking for intr_watch avoids a double g_io_channel_shutdown since
  455. * it's likely that intr_watch_cb has been queued for dispatching in
  456. * this mainloop iteration */
  457. if ((cond & (G_IO_HUP | G_IO_ERR)) && idev->intr_watch)
  458. g_io_channel_shutdown(chan, TRUE, NULL);
  459. idev->ctrl_watch = 0;
  460. if (idev->ctrl_io) {
  461. g_io_channel_unref(idev->ctrl_io);
  462. idev->ctrl_io = NULL;
  463. }
  464. /* Close interrupt channel */
  465. if (idev->intr_io && !(cond & G_IO_NVAL))
  466. g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
  467. if (!idev->intr_io && idev->virtual_cable_unplug)
  468. virtual_cable_unplug(idev);
  469. return FALSE;
  470. }
  471. #define REPORT_REQ_TIMEOUT 3
  472. static bool hidp_report_req_timeout(gpointer data)
  473. {
  474. struct input_device *idev = data;
  475. uint8_t pending_req_type;
  476. const char *req_type_str;
  477. char address[18];
  478. ba2str(&idev->dst, address);
  479. pending_req_type = idev->report_req_pending & HIDP_HEADER_TRANS_MASK;
  480. switch (pending_req_type) {
  481. case HIDP_TRANS_GET_REPORT:
  482. req_type_str = "GET_REPORT";
  483. uhid_send_get_report_reply(idev, NULL, 0, idev->report_rsp_id,
  484. ETIMEDOUT);
  485. break;
  486. case HIDP_TRANS_SET_REPORT:
  487. req_type_str = "SET_REPORT";
  488. uhid_send_set_report_reply(idev, idev->report_rsp_id,
  489. ETIMEDOUT);
  490. break;
  491. default:
  492. /* Should never happen */
  493. req_type_str = "OTHER_TRANS";
  494. break;
  495. }
  496. DBG("Device %s HIDP %s request timed out", address, req_type_str);
  497. idev->report_req_pending = 0;
  498. idev->report_req_timer = 0;
  499. idev->report_rsp_id = 0;
  500. return FALSE;
  501. }
  502. static void hidp_send_output(struct uhid_event *ev, void *user_data)
  503. {
  504. struct input_device *idev = user_data;
  505. uint8_t hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUTPUT;
  506. DBG("");
  507. hidp_send_intr_message(idev, hdr, ev->u.output.data,
  508. ev->u.output.size);
  509. }
  510. static void hidp_send_set_report(struct uhid_event *ev, void *user_data)
  511. {
  512. struct input_device *idev = user_data;
  513. uint8_t hdr;
  514. bool sent;
  515. DBG("");
  516. switch (ev->u.set_report.rtype) {
  517. case UHID_FEATURE_REPORT:
  518. hdr = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE;
  519. break;
  520. case UHID_INPUT_REPORT:
  521. hdr = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_INPUT;
  522. break;
  523. case UHID_OUTPUT_REPORT:
  524. hdr = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_OUTPUT;
  525. break;
  526. default:
  527. DBG("Unsupported HID report type %u", ev->u.set_report.rtype);
  528. return;
  529. }
  530. if (idev->report_req_pending) {
  531. DBG("Old GET_REPORT or SET_REPORT still pending");
  532. uhid_send_set_report_reply(idev, ev->u.set_report.id, EBUSY);
  533. return;
  534. }
  535. sent = hidp_send_ctrl_message(idev, hdr, ev->u.set_report.data,
  536. ev->u.set_report.size);
  537. if (sent) {
  538. idev->report_req_pending = hdr;
  539. idev->report_req_timer =
  540. timeout_add_seconds(REPORT_REQ_TIMEOUT,
  541. hidp_report_req_timeout, idev, NULL);
  542. idev->report_rsp_id = ev->u.set_report.id;
  543. } else
  544. uhid_send_set_report_reply(idev, ev->u.set_report.id, EIO);
  545. }
  546. static void hidp_send_get_report(struct uhid_event *ev, void *user_data)
  547. {
  548. struct input_device *idev = user_data;
  549. uint8_t hdr;
  550. bool sent;
  551. DBG("");
  552. if (idev->report_req_pending) {
  553. DBG("Old GET_REPORT or SET_REPORT still pending");
  554. uhid_send_get_report_reply(idev, NULL, 0, ev->u.get_report.id,
  555. EBUSY);
  556. return;
  557. }
  558. /* Send GET_REPORT on control channel */
  559. switch (ev->u.get_report.rtype) {
  560. case UHID_FEATURE_REPORT:
  561. hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_FEATURE;
  562. break;
  563. case UHID_INPUT_REPORT:
  564. hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_INPUT;
  565. break;
  566. case UHID_OUTPUT_REPORT:
  567. hdr = HIDP_TRANS_GET_REPORT | HIDP_DATA_RTYPE_OUTPUT;
  568. break;
  569. default:
  570. DBG("Unsupported HID report type %u", ev->u.get_report.rtype);
  571. return;
  572. }
  573. sent = hidp_send_ctrl_message(idev, hdr, &ev->u.get_report.rnum,
  574. sizeof(ev->u.get_report.rnum));
  575. if (sent) {
  576. idev->report_req_pending = hdr;
  577. idev->report_req_timer =
  578. timeout_add_seconds(REPORT_REQ_TIMEOUT,
  579. hidp_report_req_timeout, idev,
  580. NULL);
  581. idev->report_rsp_id = ev->u.get_report.id;
  582. } else
  583. uhid_send_get_report_reply(idev, NULL, 0, ev->u.get_report.id,
  584. EIO);
  585. }
  586. static void epox_endian_quirk(unsigned char *data, int size)
  587. {
  588. /* USAGE_PAGE (Keyboard) 05 07
  589. * USAGE_MINIMUM (0) 19 00
  590. * USAGE_MAXIMUM (65280) 2A 00 FF <= must be FF 00
  591. * LOGICAL_MINIMUM (0) 15 00
  592. * LOGICAL_MAXIMUM (65280) 26 00 FF <= must be FF 00
  593. */
  594. unsigned char pattern[] = { 0x05, 0x07, 0x19, 0x00, 0x2a, 0x00, 0xff,
  595. 0x15, 0x00, 0x26, 0x00, 0xff };
  596. unsigned int i;
  597. if (!data)
  598. return;
  599. for (i = 0; i < size - sizeof(pattern); i++) {
  600. if (!memcmp(data + i, pattern, sizeof(pattern))) {
  601. data[i + 5] = 0xff;
  602. data[i + 6] = 0x00;
  603. data[i + 10] = 0xff;
  604. data[i + 11] = 0x00;
  605. }
  606. }
  607. }
  608. static int create_hid_dev_name(sdp_record_t *rec, struct hidp_connadd_req *req)
  609. {
  610. char sdesc[sizeof(req->name) / 2];
  611. if (sdp_get_service_desc(rec, sdesc, sizeof(sdesc)) == 0) {
  612. char pname[sizeof(req->name) / 2];
  613. if (sdp_get_provider_name(rec, pname, sizeof(pname)) == 0 &&
  614. strncmp(sdesc, pname, 5) != 0)
  615. snprintf(req->name, sizeof(req->name), "%s %s",
  616. pname, sdesc);
  617. else
  618. snprintf(req->name, sizeof(req->name), "%s", sdesc);
  619. } else {
  620. return sdp_get_service_name(rec, req->name, sizeof(req->name));
  621. }
  622. return 0;
  623. }
  624. /* See HID profile specification v1.0, "7.11.6 HIDDescriptorList" for details
  625. * on the attribute format. */
  626. static int extract_hid_desc_data(sdp_record_t *rec,
  627. struct hidp_connadd_req *req)
  628. {
  629. sdp_data_t *d;
  630. d = sdp_data_get(rec, SDP_ATTR_HID_DESCRIPTOR_LIST);
  631. if (!d)
  632. goto invalid_desc;
  633. if (!SDP_IS_SEQ(d->dtd))
  634. goto invalid_desc;
  635. /* First HIDDescriptor */
  636. d = d->val.dataseq;
  637. if (!SDP_IS_SEQ(d->dtd))
  638. goto invalid_desc;
  639. /* ClassDescriptorType */
  640. d = d->val.dataseq;
  641. if (d->dtd != SDP_UINT8)
  642. goto invalid_desc;
  643. /* ClassDescriptorData */
  644. d = d->next;
  645. if (!d || !SDP_IS_TEXT_STR(d->dtd))
  646. goto invalid_desc;
  647. req->rd_data = g_try_malloc0(d->unitSize);
  648. if (req->rd_data) {
  649. memcpy(req->rd_data, d->val.str, d->unitSize);
  650. req->rd_size = d->unitSize;
  651. epox_endian_quirk(req->rd_data, req->rd_size);
  652. }
  653. return 0;
  654. invalid_desc:
  655. error("Missing or invalid HIDDescriptorList SDP attribute");
  656. return -EINVAL;
  657. }
  658. static int extract_hid_record(sdp_record_t *rec, struct hidp_connadd_req *req)
  659. {
  660. sdp_data_t *pdlist;
  661. uint8_t attr_val;
  662. int err;
  663. err = create_hid_dev_name(rec, req);
  664. if (err < 0)
  665. DBG("No valid Service Name or Service Description found");
  666. pdlist = sdp_data_get(rec, SDP_ATTR_HID_PARSER_VERSION);
  667. req->parser = pdlist ? pdlist->val.uint16 : 0x0100;
  668. pdlist = sdp_data_get(rec, SDP_ATTR_HID_DEVICE_SUBCLASS);
  669. req->subclass = pdlist ? pdlist->val.uint8 : 0;
  670. pdlist = sdp_data_get(rec, SDP_ATTR_HID_COUNTRY_CODE);
  671. req->country = pdlist ? pdlist->val.uint8 : 0;
  672. pdlist = sdp_data_get(rec, SDP_ATTR_HID_VIRTUAL_CABLE);
  673. attr_val = pdlist ? pdlist->val.uint8 : 0;
  674. if (attr_val)
  675. req->flags |= (1 << HIDP_VIRTUAL_CABLE_UNPLUG);
  676. pdlist = sdp_data_get(rec, SDP_ATTR_HID_BOOT_DEVICE);
  677. attr_val = pdlist ? pdlist->val.uint8 : 0;
  678. if (attr_val)
  679. req->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
  680. err = extract_hid_desc_data(rec, req);
  681. if (err < 0)
  682. return err;
  683. return 0;
  684. }
  685. static int ioctl_connadd(struct hidp_connadd_req *req)
  686. {
  687. int ctl, err = 0;
  688. ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
  689. if (ctl < 0)
  690. return -errno;
  691. if (ioctl(ctl, HIDPCONNADD, req) < 0)
  692. err = -errno;
  693. close(ctl);
  694. return err;
  695. }
  696. static bool ioctl_is_connected(struct input_device *idev)
  697. {
  698. struct hidp_conninfo ci;
  699. int ctl;
  700. /* Standard HID */
  701. ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
  702. if (ctl < 0) {
  703. error("Can't open HIDP control socket");
  704. return false;
  705. }
  706. memset(&ci, 0, sizeof(ci));
  707. bacpy(&ci.bdaddr, &idev->dst);
  708. if (ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) {
  709. error("Can't get HIDP connection info");
  710. close(ctl);
  711. return false;
  712. }
  713. close(ctl);
  714. if (ci.state != BT_CONNECTED)
  715. return false;
  716. return true;
  717. }
  718. static int ioctl_disconnect(struct input_device *idev, uint32_t flags)
  719. {
  720. struct hidp_conndel_req req;
  721. struct hidp_conninfo ci;
  722. int ctl, err = 0;
  723. ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HIDP);
  724. if (ctl < 0) {
  725. error("Can't open HIDP control socket");
  726. return -errno;
  727. }
  728. memset(&ci, 0, sizeof(ci));
  729. bacpy(&ci.bdaddr, &idev->dst);
  730. if ((ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) ||
  731. (ci.state != BT_CONNECTED)) {
  732. close(ctl);
  733. return -ENOTCONN;
  734. }
  735. memset(&req, 0, sizeof(req));
  736. bacpy(&req.bdaddr, &idev->dst);
  737. req.flags = flags;
  738. if (ioctl(ctl, HIDPCONNDEL, &req) < 0) {
  739. err = -errno;
  740. error("Can't delete the HID device: %s (%d)",
  741. strerror(-err), -err);
  742. }
  743. close(ctl);
  744. return err;
  745. }
  746. static int uhid_connadd(struct input_device *idev, struct hidp_connadd_req *req)
  747. {
  748. int err;
  749. struct uhid_event ev;
  750. if (idev->uhid_created)
  751. return 0;
  752. /* create uHID device */
  753. memset(&ev, 0, sizeof(ev));
  754. ev.type = UHID_CREATE;
  755. strncpy((char *) ev.u.create.name, req->name, sizeof(ev.u.create.name));
  756. ba2strlc(&idev->src, (char *) ev.u.create.phys);
  757. ba2strlc(&idev->dst, (char *) ev.u.create.uniq);
  758. ev.u.create.vendor = req->vendor;
  759. ev.u.create.product = req->product;
  760. ev.u.create.version = req->version;
  761. ev.u.create.country = req->country;
  762. ev.u.create.bus = BUS_BLUETOOTH;
  763. ev.u.create.rd_data = req->rd_data;
  764. ev.u.create.rd_size = req->rd_size;
  765. err = bt_uhid_send(idev->uhid, &ev);
  766. if (err < 0) {
  767. error("bt_uhid_send: %s", strerror(-err));
  768. return err;
  769. }
  770. bt_uhid_register(idev->uhid, UHID_OUTPUT, hidp_send_output, idev);
  771. bt_uhid_register(idev->uhid, UHID_GET_REPORT, hidp_send_get_report,
  772. idev);
  773. bt_uhid_register(idev->uhid, UHID_SET_REPORT, hidp_send_set_report,
  774. idev);
  775. idev->uhid_created = true;
  776. return err;
  777. }
  778. static int uhid_disconnect(struct input_device *idev)
  779. {
  780. int err;
  781. struct uhid_event ev;
  782. if (!idev->uhid_created)
  783. return 0;
  784. bt_uhid_unregister_all(idev->uhid);
  785. memset(&ev, 0, sizeof(ev));
  786. ev.type = UHID_DESTROY;
  787. err = bt_uhid_send(idev->uhid, &ev);
  788. if (err < 0) {
  789. error("bt_uhid_send: %s", strerror(-err));
  790. return err;
  791. }
  792. idev->uhid_created = false;
  793. return err;
  794. }
  795. static gboolean encrypt_notify(GIOChannel *io, GIOCondition condition,
  796. gpointer data)
  797. {
  798. struct input_device *idev = data;
  799. int err;
  800. DBG("");
  801. if (idev->uhid)
  802. err = uhid_connadd(idev, idev->req);
  803. else
  804. err = ioctl_connadd(idev->req);
  805. if (err < 0) {
  806. error("ioctl_connadd(): %s (%d)", strerror(-err), -err);
  807. if (idev->ctrl_io) {
  808. g_io_channel_shutdown(idev->ctrl_io, FALSE, NULL);
  809. g_io_channel_unref(idev->ctrl_io);
  810. idev->ctrl_io = NULL;
  811. }
  812. if (idev->intr_io) {
  813. g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
  814. g_io_channel_unref(idev->intr_io);
  815. idev->intr_io = NULL;
  816. }
  817. }
  818. idev->sec_watch = 0;
  819. g_free(idev->req->rd_data);
  820. g_free(idev->req);
  821. idev->req = NULL;
  822. return FALSE;
  823. }
  824. static int hidp_add_connection(struct input_device *idev)
  825. {
  826. struct hidp_connadd_req *req;
  827. sdp_record_t *rec;
  828. char src_addr[18], dst_addr[18];
  829. char filename[PATH_MAX];
  830. GKeyFile *key_file;
  831. char handle[11], *str;
  832. GError *gerr = NULL;
  833. int err;
  834. req = g_new0(struct hidp_connadd_req, 1);
  835. req->ctrl_sock = g_io_channel_unix_get_fd(idev->ctrl_io);
  836. req->intr_sock = g_io_channel_unix_get_fd(idev->intr_io);
  837. req->flags = 0;
  838. req->idle_to = idle_timeout;
  839. ba2str(&idev->src, src_addr);
  840. ba2str(&idev->dst, dst_addr);
  841. snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", src_addr,
  842. dst_addr);
  843. sprintf(handle, "0x%8.8X", idev->handle);
  844. key_file = g_key_file_new();
  845. g_key_file_load_from_file(key_file, filename, 0, NULL);
  846. str = g_key_file_get_string(key_file, "ServiceRecords", handle, NULL);
  847. g_key_file_free(key_file);
  848. if (!str) {
  849. error("Rejected connection from unknown device %s", dst_addr);
  850. err = -EPERM;
  851. goto cleanup;
  852. }
  853. rec = record_from_string(str);
  854. g_free(str);
  855. err = extract_hid_record(rec, req);
  856. sdp_record_free(rec);
  857. if (err < 0) {
  858. error("Could not parse HID SDP record: %s (%d)", strerror(-err),
  859. -err);
  860. goto cleanup;
  861. }
  862. req->vendor = btd_device_get_vendor(idev->device);
  863. req->product = btd_device_get_product(idev->device);
  864. req->version = btd_device_get_version(idev->device);
  865. if (device_name_known(idev->device))
  866. device_get_name(idev->device, req->name, sizeof(req->name));
  867. /* Make sure the device is bonded if required */
  868. if (classic_bonded_only && !input_device_bonded(idev)) {
  869. error("Rejected connection from !bonded device %s", dst_addr);
  870. goto cleanup;
  871. }
  872. /* Encryption is mandatory for keyboards */
  873. /* Some platforms may choose to require encryption for all devices */
  874. /* Note that this only matters for pre 2.1 devices as otherwise the */
  875. /* device is encrypted by default by the lower layers */
  876. if (classic_bonded_only || req->subclass & 0x40) {
  877. if (!bt_io_set(idev->intr_io, &gerr,
  878. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
  879. BT_IO_OPT_INVALID)) {
  880. error("btio: %s", gerr->message);
  881. g_error_free(gerr);
  882. err = -EFAULT;
  883. goto cleanup;
  884. }
  885. idev->req = req;
  886. idev->sec_watch = g_io_add_watch(idev->intr_io, G_IO_OUT,
  887. encrypt_notify, idev);
  888. return 0;
  889. }
  890. if (idev->uhid)
  891. err = uhid_connadd(idev, req);
  892. else
  893. err = ioctl_connadd(req);
  894. cleanup:
  895. g_free(req->rd_data);
  896. g_free(req);
  897. return err;
  898. }
  899. static bool is_connected(struct input_device *idev)
  900. {
  901. if (idev->uhid)
  902. return (idev->intr_io != NULL && idev->ctrl_io != NULL);
  903. else
  904. return ioctl_is_connected(idev);
  905. }
  906. static int connection_disconnect(struct input_device *idev, uint32_t flags)
  907. {
  908. int sock;
  909. if (!is_connected(idev))
  910. return -ENOTCONN;
  911. /* Standard HID disconnect
  912. * Intr channel must be disconnected before ctrl channel, so only
  913. * disconnect intr here, ctrl is disconnected in intr_watch_cb.
  914. */
  915. if (idev->intr_io) {
  916. sock = g_io_channel_unix_get_fd(idev->intr_io);
  917. shutdown(sock, SHUT_WR);
  918. }
  919. if (flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG))
  920. idev->virtual_cable_unplug = true;
  921. if (idev->uhid)
  922. return uhid_disconnect(idev);
  923. else
  924. return ioctl_disconnect(idev, flags);
  925. }
  926. static int input_device_connected(struct input_device *idev)
  927. {
  928. int err;
  929. if (idev->intr_io == NULL || idev->ctrl_io == NULL)
  930. return -ENOTCONN;
  931. err = hidp_add_connection(idev);
  932. if (err < 0)
  933. return err;
  934. btd_service_connecting_complete(idev->service, 0);
  935. return 0;
  936. }
  937. static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
  938. gpointer user_data)
  939. {
  940. struct input_device *idev = user_data;
  941. GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  942. int err;
  943. if (conn_err) {
  944. err = -EIO;
  945. goto failed;
  946. }
  947. err = input_device_connected(idev);
  948. if (err < 0)
  949. goto failed;
  950. if (idev->uhid)
  951. cond |= G_IO_IN;
  952. idev->intr_watch = g_io_add_watch(idev->intr_io, cond, intr_watch_cb,
  953. idev);
  954. return;
  955. failed:
  956. btd_service_connecting_complete(idev->service, err);
  957. /* So we guarantee the interrupt channel is closed before the
  958. * control channel (if we only do unref GLib will close it only
  959. * after returning control to the mainloop */
  960. if (!conn_err)
  961. g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
  962. g_io_channel_unref(idev->intr_io);
  963. idev->intr_io = NULL;
  964. if (idev->ctrl_io) {
  965. g_io_channel_unref(idev->ctrl_io);
  966. idev->ctrl_io = NULL;
  967. }
  968. }
  969. static void control_connect_cb(GIOChannel *chan, GError *conn_err,
  970. gpointer user_data)
  971. {
  972. struct input_device *idev = user_data;
  973. GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  974. GIOChannel *io;
  975. GError *err = NULL;
  976. if (conn_err) {
  977. error("%s", conn_err->message);
  978. goto failed;
  979. }
  980. /* Connect to the HID interrupt channel */
  981. io = bt_io_connect(interrupt_connect_cb, idev,
  982. NULL, &err,
  983. BT_IO_OPT_SOURCE_BDADDR, &idev->src,
  984. BT_IO_OPT_DEST_BDADDR, &idev->dst,
  985. BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
  986. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  987. BT_IO_OPT_INVALID);
  988. if (!io) {
  989. error("%s", err->message);
  990. g_error_free(err);
  991. goto failed;
  992. }
  993. idev->intr_io = io;
  994. if (idev->uhid)
  995. cond |= G_IO_IN;
  996. idev->ctrl_watch = g_io_add_watch(idev->ctrl_io, cond, ctrl_watch_cb,
  997. idev);
  998. return;
  999. failed:
  1000. btd_service_connecting_complete(idev->service, -EIO);
  1001. g_io_channel_unref(idev->ctrl_io);
  1002. idev->ctrl_io = NULL;
  1003. }
  1004. static int dev_connect(struct input_device *idev)
  1005. {
  1006. GError *err = NULL;
  1007. GIOChannel *io;
  1008. BtIOSecLevel sec_level;
  1009. if (idev->disable_sdp)
  1010. bt_clear_cached_session(&idev->src, &idev->dst);
  1011. /* encrypt connection if device is bonded */
  1012. if (input_device_bonded(idev))
  1013. sec_level = BT_IO_SEC_MEDIUM;
  1014. else
  1015. sec_level = BT_IO_SEC_LOW;
  1016. io = bt_io_connect(control_connect_cb, idev,
  1017. NULL, &err,
  1018. BT_IO_OPT_SOURCE_BDADDR, &idev->src,
  1019. BT_IO_OPT_DEST_BDADDR, &idev->dst,
  1020. BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
  1021. BT_IO_OPT_SEC_LEVEL, sec_level,
  1022. BT_IO_OPT_INVALID);
  1023. idev->ctrl_io = io;
  1024. if (err == NULL)
  1025. return 0;
  1026. error("%s", err->message);
  1027. g_error_free(err);
  1028. return -EIO;
  1029. }
  1030. static bool input_device_auto_reconnect(gpointer user_data)
  1031. {
  1032. struct input_device *idev = user_data;
  1033. DBG("path=%s, attempt=%d", idev->path, idev->reconnect_attempt);
  1034. /* Stop the recurrent reconnection attempts if the device is
  1035. * reconnected or is marked for removal.
  1036. */
  1037. if (device_is_temporary(idev->device) ||
  1038. btd_device_is_connected(idev->device))
  1039. goto bail;
  1040. /* Only attempt an auto-reconnect for at most 3 minutes (6 * 30s). */
  1041. if (idev->reconnect_attempt >= 6)
  1042. goto bail;
  1043. /* Check if the profile is already connected. */
  1044. if (idev->ctrl_io)
  1045. goto bail;
  1046. if (is_connected(idev))
  1047. goto bail;
  1048. idev->reconnect_attempt++;
  1049. dev_connect(idev);
  1050. return TRUE;
  1051. bail:
  1052. idev->reconnect_timer = 0;
  1053. return FALSE;
  1054. }
  1055. static const char * const _reconnect_mode_str[] = {
  1056. "none",
  1057. "device",
  1058. "host",
  1059. "any"
  1060. };
  1061. static const char *reconnect_mode_to_string(const enum reconnect_mode_t mode)
  1062. {
  1063. return _reconnect_mode_str[mode];
  1064. }
  1065. static void input_device_enter_reconnect_mode(struct input_device *idev)
  1066. {
  1067. DBG("path=%s reconnect_mode=%s", idev->path,
  1068. reconnect_mode_to_string(idev->reconnect_mode));
  1069. /* Make sure the device is bonded if required */
  1070. if (classic_bonded_only && !input_device_bonded(idev))
  1071. return;
  1072. /* Only attempt an auto-reconnect when the device is required to
  1073. * accept reconnections from the host.
  1074. */
  1075. if (idev->reconnect_mode != RECONNECT_ANY &&
  1076. idev->reconnect_mode != RECONNECT_HOST)
  1077. return;
  1078. /* If the device is temporary we are not required to reconnect
  1079. * with the device. This is likely the case of a removing device.
  1080. */
  1081. if (device_is_temporary(idev->device) ||
  1082. btd_device_is_connected(idev->device))
  1083. return;
  1084. if (idev->reconnect_timer > 0)
  1085. timeout_remove(idev->reconnect_timer);
  1086. DBG("registering auto-reconnect");
  1087. idev->reconnect_attempt = 0;
  1088. idev->reconnect_timer = timeout_add_seconds(30,
  1089. input_device_auto_reconnect, idev,
  1090. NULL);
  1091. }
  1092. int input_device_connect(struct btd_service *service)
  1093. {
  1094. struct input_device *idev;
  1095. DBG("");
  1096. idev = btd_service_get_user_data(service);
  1097. if (idev->ctrl_io)
  1098. return -EBUSY;
  1099. if (is_connected(idev))
  1100. return -EALREADY;
  1101. return dev_connect(idev);
  1102. }
  1103. int input_device_disconnect(struct btd_service *service)
  1104. {
  1105. struct input_device *idev;
  1106. int err, flags;
  1107. DBG("");
  1108. idev = btd_service_get_user_data(service);
  1109. flags = device_is_temporary(idev->device) ?
  1110. (1 << HIDP_VIRTUAL_CABLE_UNPLUG) : 0;
  1111. err = connection_disconnect(idev, flags);
  1112. if (err < 0)
  1113. return err;
  1114. return 0;
  1115. }
  1116. static bool is_device_sdp_disable(const sdp_record_t *rec)
  1117. {
  1118. sdp_data_t *data;
  1119. data = sdp_data_get(rec, SDP_ATTR_HID_SDP_DISABLE);
  1120. return data && data->val.uint8;
  1121. }
  1122. static enum reconnect_mode_t hid_reconnection_mode(bool reconnect_initiate,
  1123. bool normally_connectable)
  1124. {
  1125. if (!reconnect_initiate && !normally_connectable)
  1126. return RECONNECT_NONE;
  1127. else if (!reconnect_initiate && normally_connectable)
  1128. return RECONNECT_HOST;
  1129. else if (reconnect_initiate && !normally_connectable)
  1130. return RECONNECT_DEVICE;
  1131. else /* (reconnect_initiate && normally_connectable) */
  1132. return RECONNECT_ANY;
  1133. }
  1134. static void extract_hid_props(struct input_device *idev,
  1135. const sdp_record_t *rec)
  1136. {
  1137. /* Extract HID connectability */
  1138. bool reconnect_initiate, normally_connectable;
  1139. sdp_data_t *pdlist;
  1140. /* HIDNormallyConnectable is optional and assumed FALSE
  1141. * if not present. */
  1142. pdlist = sdp_data_get(rec, SDP_ATTR_HID_RECONNECT_INITIATE);
  1143. reconnect_initiate = pdlist ? pdlist->val.uint8 : TRUE;
  1144. pdlist = sdp_data_get(rec, SDP_ATTR_HID_NORMALLY_CONNECTABLE);
  1145. normally_connectable = pdlist ? pdlist->val.uint8 : FALSE;
  1146. /* Update local values */
  1147. idev->reconnect_mode =
  1148. hid_reconnection_mode(reconnect_initiate, normally_connectable);
  1149. }
  1150. static struct input_device *input_device_new(struct btd_service *service)
  1151. {
  1152. struct btd_device *device = btd_service_get_device(service);
  1153. struct btd_profile *p = btd_service_get_profile(service);
  1154. const char *path = device_get_path(device);
  1155. const sdp_record_t *rec = btd_device_get_record(device, p->remote_uuid);
  1156. struct btd_adapter *adapter = device_get_adapter(device);
  1157. struct input_device *idev;
  1158. if (!rec)
  1159. return NULL;
  1160. idev = g_new0(struct input_device, 1);
  1161. bacpy(&idev->src, btd_adapter_get_address(adapter));
  1162. bacpy(&idev->dst, device_get_address(device));
  1163. idev->service = btd_service_ref(service);
  1164. idev->device = btd_device_ref(device);
  1165. idev->path = g_strdup(path);
  1166. idev->handle = rec->handle;
  1167. idev->disable_sdp = is_device_sdp_disable(rec);
  1168. /* Initialize device properties */
  1169. extract_hid_props(idev, rec);
  1170. if (idev->disable_sdp)
  1171. device_set_refresh_discovery(device, false);
  1172. return idev;
  1173. }
  1174. static gboolean property_get_reconnect_mode(
  1175. const GDBusPropertyTable *property,
  1176. DBusMessageIter *iter, void *data)
  1177. {
  1178. struct input_device *idev = data;
  1179. const char *str_mode = reconnect_mode_to_string(idev->reconnect_mode);
  1180. dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str_mode);
  1181. return TRUE;
  1182. }
  1183. static const GDBusPropertyTable input_properties[] = {
  1184. { "ReconnectMode", "s", property_get_reconnect_mode },
  1185. { }
  1186. };
  1187. int input_device_register(struct btd_service *service)
  1188. {
  1189. struct btd_device *device = btd_service_get_device(service);
  1190. const char *path = device_get_path(device);
  1191. struct input_device *idev;
  1192. DBG("%s", path);
  1193. idev = input_device_new(service);
  1194. if (!idev)
  1195. return -EINVAL;
  1196. if (uhid_enabled) {
  1197. idev->uhid = bt_uhid_new_default();
  1198. if (!idev->uhid) {
  1199. error("bt_uhid_new_default: failed");
  1200. input_device_free(idev);
  1201. return -EIO;
  1202. }
  1203. }
  1204. if (g_dbus_register_interface(btd_get_dbus_connection(),
  1205. idev->path, INPUT_INTERFACE,
  1206. NULL, NULL,
  1207. input_properties, idev,
  1208. NULL) == FALSE) {
  1209. error("Unable to register %s interface", INPUT_INTERFACE);
  1210. input_device_free(idev);
  1211. return -EINVAL;
  1212. }
  1213. btd_service_set_user_data(service, idev);
  1214. device_set_wake_support(device, true);
  1215. return 0;
  1216. }
  1217. static struct input_device *find_device(const bdaddr_t *src,
  1218. const bdaddr_t *dst)
  1219. {
  1220. struct btd_device *device;
  1221. struct btd_service *service;
  1222. device = btd_adapter_find_device(adapter_find(src), dst, BDADDR_BREDR);
  1223. if (device == NULL)
  1224. return NULL;
  1225. service = btd_device_get_service(device, HID_UUID);
  1226. if (service == NULL)
  1227. return NULL;
  1228. return btd_service_get_user_data(service);
  1229. }
  1230. void input_device_unregister(struct btd_service *service)
  1231. {
  1232. struct btd_device *device = btd_service_get_device(service);
  1233. const char *path = device_get_path(device);
  1234. struct input_device *idev = btd_service_get_user_data(service);
  1235. DBG("%s", path);
  1236. g_dbus_unregister_interface(btd_get_dbus_connection(),
  1237. idev->path, INPUT_INTERFACE);
  1238. input_device_free(idev);
  1239. }
  1240. static int input_device_connadd(struct input_device *idev)
  1241. {
  1242. int err;
  1243. err = input_device_connected(idev);
  1244. if (err == 0)
  1245. return 0;
  1246. if (idev->ctrl_io) {
  1247. g_io_channel_shutdown(idev->ctrl_io, FALSE, NULL);
  1248. g_io_channel_unref(idev->ctrl_io);
  1249. idev->ctrl_io = NULL;
  1250. }
  1251. if (idev->intr_io) {
  1252. g_io_channel_shutdown(idev->intr_io, FALSE, NULL);
  1253. g_io_channel_unref(idev->intr_io);
  1254. idev->intr_io = NULL;
  1255. }
  1256. return err;
  1257. }
  1258. bool input_device_exists(const bdaddr_t *src, const bdaddr_t *dst)
  1259. {
  1260. if (find_device(src, dst))
  1261. return true;
  1262. return false;
  1263. }
  1264. int input_device_set_channel(const bdaddr_t *src, const bdaddr_t *dst, int psm,
  1265. GIOChannel *io)
  1266. {
  1267. struct input_device *idev = find_device(src, dst);
  1268. GIOCondition cond = G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1269. DBG("idev %p psm %d", idev, psm);
  1270. if (!idev)
  1271. return -ENOENT;
  1272. if (uhid_enabled)
  1273. cond |= G_IO_IN;
  1274. switch (psm) {
  1275. case L2CAP_PSM_HIDP_CTRL:
  1276. if (idev->ctrl_io)
  1277. return -EALREADY;
  1278. idev->ctrl_io = g_io_channel_ref(io);
  1279. idev->ctrl_watch = g_io_add_watch(idev->ctrl_io, cond,
  1280. ctrl_watch_cb, idev);
  1281. break;
  1282. case L2CAP_PSM_HIDP_INTR:
  1283. if (idev->intr_io)
  1284. return -EALREADY;
  1285. idev->intr_io = g_io_channel_ref(io);
  1286. idev->intr_watch = g_io_add_watch(idev->intr_io, cond,
  1287. intr_watch_cb, idev);
  1288. break;
  1289. }
  1290. if (idev->intr_io && idev->ctrl_io)
  1291. input_device_connadd(idev);
  1292. return 0;
  1293. }
  1294. int input_device_close_channels(const bdaddr_t *src, const bdaddr_t *dst)
  1295. {
  1296. struct input_device *idev = find_device(src, dst);
  1297. if (!idev)
  1298. return -ENOENT;
  1299. if (idev->intr_io)
  1300. g_io_channel_shutdown(idev->intr_io, TRUE, NULL);
  1301. if (idev->ctrl_io)
  1302. g_io_channel_shutdown(idev->ctrl_io, TRUE, NULL);
  1303. return 0;
  1304. }