hidhost.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <ctype.h>
  19. #include <glib.h>
  20. #include "btio/btio.h"
  21. #include "lib/bluetooth.h"
  22. #include "lib/sdp.h"
  23. #include "lib/sdp_lib.h"
  24. #include "lib/uuid.h"
  25. #include "src/shared/mgmt.h"
  26. #include "src/shared/util.h"
  27. #include "src/shared/uhid.h"
  28. #include "src/shared/queue.h"
  29. #include "src/shared/att.h"
  30. #include "src/shared/gatt-db.h"
  31. #include "src/sdp-client.h"
  32. #include "src/uuid-helper.h"
  33. #include "src/log.h"
  34. #include "profiles/input/hog-lib.h"
  35. #include "hal-msg.h"
  36. #include "ipc-common.h"
  37. #include "ipc.h"
  38. #include "bluetooth.h"
  39. #include "gatt.h"
  40. #include "hidhost.h"
  41. #include "utils.h"
  42. #define L2CAP_PSM_HIDP_CTRL 0x11
  43. #define L2CAP_PSM_HIDP_INTR 0x13
  44. /* HID message types */
  45. #define HID_MSG_HANDSHAKE 0x00
  46. #define HID_MSG_CONTROL 0x10
  47. #define HID_MSG_GET_REPORT 0x40
  48. #define HID_MSG_SET_REPORT 0x50
  49. #define HID_MSG_GET_PROTOCOL 0x60
  50. #define HID_MSG_SET_PROTOCOL 0x70
  51. #define HID_MSG_DATA 0xa0
  52. #define HID_MSG_TYPE_MASK 0xf0
  53. /* HID data types */
  54. #define HID_DATA_TYPE_INPUT 0x01
  55. #define HID_DATA_TYPE_OUTPUT 0x02
  56. #define HID_DATA_TYPE_FEATURE 0x03
  57. /* HID protocol header parameters */
  58. #define HID_PROTO_BOOT 0x00
  59. #define HID_PROTO_REPORT 0x01
  60. /* HID GET REPORT Size Field */
  61. #define HID_GET_REPORT_SIZE_FIELD 0x08
  62. /* HID Virtual Cable Unplug */
  63. #define HID_VIRTUAL_CABLE_UNPLUG 0x05
  64. static bdaddr_t adapter_addr;
  65. static GIOChannel *ctrl_io = NULL;
  66. static GIOChannel *intr_io = NULL;
  67. static GSList *devices = NULL;
  68. static unsigned int hog_app = 0;
  69. static struct ipc *hal_ipc = NULL;
  70. struct hid_device {
  71. bdaddr_t dst;
  72. uint8_t state;
  73. uint8_t subclass;
  74. uint16_t vendor;
  75. uint16_t product;
  76. uint16_t version;
  77. uint8_t country;
  78. int rd_size;
  79. void *rd_data;
  80. uint8_t boot_dev;
  81. GIOChannel *ctrl_io;
  82. GIOChannel *intr_io;
  83. guint ctrl_watch;
  84. guint intr_watch;
  85. struct bt_uhid *uhid;
  86. uint8_t last_hid_msg;
  87. struct bt_hog *hog;
  88. int sec_level;
  89. };
  90. static int device_cmp(gconstpointer s, gconstpointer user_data)
  91. {
  92. const struct hid_device *dev = s;
  93. const bdaddr_t *dst = user_data;
  94. return bacmp(&dev->dst, dst);
  95. }
  96. static void hid_device_free(void *data)
  97. {
  98. struct hid_device *dev = data;
  99. if (dev->ctrl_watch > 0)
  100. g_source_remove(dev->ctrl_watch);
  101. if (dev->intr_watch > 0)
  102. g_source_remove(dev->intr_watch);
  103. if (dev->intr_io)
  104. g_io_channel_unref(dev->intr_io);
  105. if (dev->ctrl_io)
  106. g_io_channel_unref(dev->ctrl_io);
  107. if (dev->uhid)
  108. bt_uhid_unref(dev->uhid);
  109. if (dev->hog)
  110. bt_hog_unref(dev->hog);
  111. g_free(dev->rd_data);
  112. g_free(dev);
  113. }
  114. static void hid_device_remove(struct hid_device *dev)
  115. {
  116. devices = g_slist_remove(devices, dev);
  117. hid_device_free(dev);
  118. }
  119. static struct hid_device *hid_device_new(const bdaddr_t *addr)
  120. {
  121. struct hid_device *dev;
  122. dev = g_new0(struct hid_device, 1);
  123. bacpy(&dev->dst, addr);
  124. dev->state = HAL_HIDHOST_STATE_DISCONNECTED;
  125. dev->sec_level = BT_IO_SEC_LOW;
  126. devices = g_slist_append(devices, dev);
  127. return dev;
  128. }
  129. static bool hex2buf(const uint8_t *hex, uint8_t *buf, int buf_size)
  130. {
  131. int i, j;
  132. char c;
  133. uint8_t b;
  134. for (i = 0, j = 0; i < buf_size; i++, j++) {
  135. c = toupper(hex[j]);
  136. if (c >= '0' && c <= '9')
  137. b = c - '0';
  138. else if (c >= 'A' && c <= 'F')
  139. b = 10 + c - 'A';
  140. else
  141. return false;
  142. j++;
  143. c = toupper(hex[j]);
  144. if (c >= '0' && c <= '9')
  145. b = b * 16 + c - '0';
  146. else if (c >= 'A' && c <= 'F')
  147. b = b * 16 + 10 + c - 'A';
  148. else
  149. return false;
  150. buf[i] = b;
  151. }
  152. return true;
  153. }
  154. static void handle_uhid_output(struct uhid_event *event, void *user_data)
  155. {
  156. struct uhid_output_req *output = &event->u.output;
  157. struct hid_device *dev = user_data;
  158. int fd, req_size;
  159. uint8_t *req;
  160. if (!dev->ctrl_io)
  161. return;
  162. req_size = 1 + output->size;
  163. req = malloc0(req_size);
  164. if (!req)
  165. return;
  166. req[0] = HID_MSG_SET_REPORT | output->rtype;
  167. memcpy(req + 1, output->data, req_size - 1);
  168. fd = g_io_channel_unix_get_fd(dev->ctrl_io);
  169. if (write(fd, req, req_size) < 0)
  170. error("hidhost: error writing set_report: %s (%d)",
  171. strerror(errno), errno);
  172. free(req);
  173. }
  174. static gboolean intr_io_watch_cb(GIOChannel *chan, gpointer data)
  175. {
  176. struct hid_device *dev = data;
  177. uint8_t buf[UHID_DATA_MAX];
  178. struct uhid_event ev;
  179. int fd, bread, err;
  180. /* Wait uHID if not ready */
  181. if (!dev->uhid)
  182. return TRUE;
  183. fd = g_io_channel_unix_get_fd(chan);
  184. bread = read(fd, buf, sizeof(buf));
  185. if (bread < 0) {
  186. error("hidhost: read from interrupt failed: %s(%d)",
  187. strerror(errno), -errno);
  188. return TRUE;
  189. }
  190. /* Discard non-data packets */
  191. if (bread == 0 || buf[0] != (HID_MSG_DATA | HID_DATA_TYPE_INPUT))
  192. return TRUE;
  193. /* send data to uHID device skipping HIDP header byte */
  194. memset(&ev, 0, sizeof(ev));
  195. ev.type = UHID_INPUT;
  196. ev.u.input.size = bread - 1;
  197. memcpy(ev.u.input.data, &buf[1], ev.u.input.size);
  198. err = bt_uhid_send(dev->uhid, &ev);
  199. if (err < 0)
  200. DBG("bt_uhid_send: %s (%d)", strerror(-err), -err);
  201. return TRUE;
  202. }
  203. static void bt_hid_notify_state(struct hid_device *dev, uint8_t state)
  204. {
  205. struct hal_ev_hidhost_conn_state ev;
  206. char address[18];
  207. if (dev->state == state)
  208. return;
  209. dev->state = state;
  210. ba2str(&dev->dst, address);
  211. DBG("device %s state %u", address, state);
  212. bdaddr2android(&dev->dst, ev.bdaddr);
  213. ev.state = state;
  214. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  215. HAL_EV_HIDHOST_CONN_STATE, sizeof(ev), &ev);
  216. }
  217. static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond,
  218. gpointer data)
  219. {
  220. struct hid_device *dev = data;
  221. if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
  222. goto error;
  223. if (cond & G_IO_IN)
  224. return intr_io_watch_cb(chan, data);
  225. error:
  226. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  227. /*
  228. * Checking for ctrl_watch avoids a double g_io_channel_shutdown since
  229. * it's likely that ctrl_watch_cb has been queued for dispatching in
  230. * this mainloop iteration
  231. */
  232. if ((cond & (G_IO_HUP | G_IO_ERR)) && dev->ctrl_watch)
  233. g_io_channel_shutdown(chan, TRUE, NULL);
  234. /* Close control channel */
  235. if (dev->ctrl_io && !(cond & G_IO_NVAL))
  236. g_io_channel_shutdown(dev->ctrl_io, TRUE, NULL);
  237. hid_device_remove(dev);
  238. return FALSE;
  239. }
  240. static void bt_hid_notify_proto_mode(struct hid_device *dev, uint8_t *buf,
  241. int len)
  242. {
  243. struct hal_ev_hidhost_proto_mode ev;
  244. char address[18];
  245. ba2str(&dev->dst, address);
  246. DBG("device %s", address);
  247. memset(&ev, 0, sizeof(ev));
  248. bdaddr2android(&dev->dst, ev.bdaddr);
  249. if (buf[0] == HID_MSG_DATA) {
  250. ev.status = HAL_HIDHOST_STATUS_OK;
  251. if (buf[1] == HID_PROTO_REPORT)
  252. ev.mode = HAL_HIDHOST_REPORT_PROTOCOL;
  253. else if (buf[1] == HID_PROTO_BOOT)
  254. ev.mode = HAL_HIDHOST_BOOT_PROTOCOL;
  255. else
  256. ev.mode = HAL_HIDHOST_UNSUPPORTED_PROTOCOL;
  257. } else {
  258. ev.status = buf[0];
  259. ev.mode = HAL_HIDHOST_UNSUPPORTED_PROTOCOL;
  260. }
  261. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  262. HAL_EV_HIDHOST_PROTO_MODE, sizeof(ev), &ev);
  263. }
  264. static void bt_hid_notify_get_report(struct hid_device *dev, uint8_t *buf,
  265. int len)
  266. {
  267. struct hal_ev_hidhost_get_report *ev;
  268. int ev_len;
  269. char address[18];
  270. ba2str(&dev->dst, address);
  271. DBG("device %s", address);
  272. ev_len = sizeof(*ev);
  273. if (!((buf[0] == (HID_MSG_DATA | HID_DATA_TYPE_INPUT)) ||
  274. (buf[0] == (HID_MSG_DATA | HID_DATA_TYPE_OUTPUT)) ||
  275. (buf[0] == (HID_MSG_DATA | HID_DATA_TYPE_FEATURE)))) {
  276. ev = g_malloc0(ev_len);
  277. ev->status = buf[0];
  278. bdaddr2android(&dev->dst, ev->bdaddr);
  279. goto send;
  280. }
  281. /*
  282. * Report porotocol mode reply contains id after hdr, in boot
  283. * protocol mode id doesn't exist
  284. */
  285. ev_len += (dev->boot_dev) ? (len - 1) : (len - 2);
  286. ev = g_malloc0(ev_len);
  287. ev->status = HAL_HIDHOST_STATUS_OK;
  288. bdaddr2android(&dev->dst, ev->bdaddr);
  289. /*
  290. * Report porotocol mode reply contains id after hdr, in boot
  291. * protocol mode id doesn't exist
  292. */
  293. if (dev->boot_dev) {
  294. ev->len = len - 1;
  295. memcpy(ev->data, buf + 1, ev->len);
  296. } else {
  297. ev->len = len - 2;
  298. memcpy(ev->data, buf + 2, ev->len);
  299. }
  300. send:
  301. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  302. HAL_EV_HIDHOST_GET_REPORT, ev_len, ev);
  303. g_free(ev);
  304. }
  305. static void bt_hid_notify_handshake(struct hid_device *dev, uint8_t *buf,
  306. int len)
  307. {
  308. struct hal_ev_hidhost_handshake ev;
  309. bdaddr2android(&dev->dst, ev.bdaddr);
  310. /* crop result code to handshake status range from HAL */
  311. ev.status = buf[0];
  312. if (ev.status > HAL_HIDHOST_HS_ERROR)
  313. ev.status = HAL_HIDHOST_HS_ERROR;
  314. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  315. HAL_EV_HIDHOST_HANDSHAKE, sizeof(ev), &ev);
  316. }
  317. static void bt_hid_notify_virtual_unplug(struct hid_device *dev,
  318. uint8_t *buf, int len)
  319. {
  320. struct hal_ev_hidhost_virtual_unplug ev;
  321. char address[18];
  322. ba2str(&dev->dst, address);
  323. DBG("device %s", address);
  324. bdaddr2android(&dev->dst, ev.bdaddr);
  325. ev.status = HAL_HIDHOST_GENERAL_ERROR;
  326. /* Wait either channels to HUP */
  327. if (dev->intr_io && dev->ctrl_io) {
  328. g_io_channel_shutdown(dev->intr_io, TRUE, NULL);
  329. g_io_channel_shutdown(dev->ctrl_io, TRUE, NULL);
  330. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTING);
  331. ev.status = HAL_HIDHOST_STATUS_OK;
  332. }
  333. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  334. HAL_EV_HIDHOST_VIRTUAL_UNPLUG, sizeof(ev), &ev);
  335. }
  336. static gboolean ctrl_io_watch_cb(GIOChannel *chan, gpointer data)
  337. {
  338. struct hid_device *dev = data;
  339. int fd, bread;
  340. uint8_t buf[UHID_DATA_MAX];
  341. DBG("");
  342. fd = g_io_channel_unix_get_fd(chan);
  343. bread = read(fd, buf, sizeof(buf));
  344. if (bread < 0) {
  345. error("hidhost: read from control failed: %s(%d)",
  346. strerror(errno), -errno);
  347. return TRUE;
  348. }
  349. switch (dev->last_hid_msg) {
  350. case HID_MSG_GET_PROTOCOL:
  351. case HID_MSG_SET_PROTOCOL:
  352. bt_hid_notify_proto_mode(dev, buf, bread);
  353. break;
  354. case HID_MSG_GET_REPORT:
  355. bt_hid_notify_get_report(dev, buf, bread);
  356. break;
  357. }
  358. switch (buf[0] & HID_MSG_TYPE_MASK) {
  359. case HID_MSG_HANDSHAKE:
  360. bt_hid_notify_handshake(dev, buf, bread);
  361. break;
  362. case HID_MSG_CONTROL:
  363. if ((buf[0] & ~HID_MSG_TYPE_MASK) == HID_VIRTUAL_CABLE_UNPLUG)
  364. bt_hid_notify_virtual_unplug(dev, buf, bread);
  365. break;
  366. default:
  367. break;
  368. }
  369. /* reset msg type request */
  370. dev->last_hid_msg = 0;
  371. return TRUE;
  372. }
  373. static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond,
  374. gpointer data)
  375. {
  376. struct hid_device *dev = data;
  377. if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
  378. goto error;
  379. if (cond & G_IO_IN)
  380. return ctrl_io_watch_cb(chan, data);
  381. error:
  382. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  383. /*
  384. * Checking for intr_watch avoids a double g_io_channel_shutdown since
  385. * it's likely that intr_watch_cb has been queued for dispatching in
  386. * this mainloop iteration
  387. */
  388. if ((cond & (G_IO_HUP | G_IO_ERR)) && dev->intr_watch)
  389. g_io_channel_shutdown(chan, TRUE, NULL);
  390. if (dev->intr_io && !(cond & G_IO_NVAL))
  391. g_io_channel_shutdown(dev->intr_io, TRUE, NULL);
  392. hid_device_remove(dev);
  393. return FALSE;
  394. }
  395. static void bt_hid_set_info(struct hid_device *dev)
  396. {
  397. struct hal_ev_hidhost_info ev;
  398. DBG("");
  399. bdaddr2android(&dev->dst, ev.bdaddr);
  400. ev.attr = 0; /* TODO: Check what is this field */
  401. ev.subclass = dev->subclass;
  402. ev.app_id = 0; /* TODO: Check what is this field */
  403. ev.vendor = dev->vendor;
  404. ev.product = dev->product;
  405. ev.version = dev->version;
  406. ev.country = dev->country;
  407. ev.descr_len = dev->rd_size;
  408. memset(ev.descr, 0, sizeof(ev.descr));
  409. memcpy(ev.descr, dev->rd_data, ev.descr_len);
  410. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_EV_HIDHOST_INFO,
  411. sizeof(ev), &ev);
  412. }
  413. static int uhid_create(struct hid_device *dev)
  414. {
  415. struct uhid_event ev;
  416. int err;
  417. dev->uhid = bt_uhid_new_default();
  418. if (!dev->uhid) {
  419. err = -errno;
  420. error("hidhost: Failed to create bt_uhid instance");
  421. return err;
  422. }
  423. memset(&ev, 0, sizeof(ev));
  424. ev.type = UHID_CREATE;
  425. strcpy((char *) ev.u.create.name, "bluez-input-device");
  426. ev.u.create.bus = BUS_BLUETOOTH;
  427. ev.u.create.vendor = dev->vendor;
  428. ev.u.create.product = dev->product;
  429. ev.u.create.version = dev->version;
  430. ev.u.create.country = dev->country;
  431. ev.u.create.rd_size = dev->rd_size;
  432. ev.u.create.rd_data = dev->rd_data;
  433. err = bt_uhid_send(dev->uhid, &ev);
  434. if (err < 0) {
  435. error("hidhost: Failed to create uHID device: %s",
  436. strerror(-err));
  437. bt_uhid_unref(dev->uhid);
  438. dev->uhid = NULL;
  439. return err;
  440. }
  441. bt_uhid_register(dev->uhid, UHID_OUTPUT, handle_uhid_output, dev);
  442. bt_hid_set_info(dev);
  443. return 0;
  444. }
  445. static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
  446. gpointer user_data)
  447. {
  448. struct hid_device *dev = user_data;
  449. uint8_t state;
  450. DBG("");
  451. if (conn_err) {
  452. error("hidhost: Failed to connect interrupt channel (%s)",
  453. conn_err->message);
  454. state = HAL_HIDHOST_STATE_FAILED;
  455. goto failed;
  456. }
  457. if (uhid_create(dev) < 0) {
  458. state = HAL_HIDHOST_STATE_NO_HID;
  459. goto failed;
  460. }
  461. dev->intr_watch = g_io_add_watch(dev->intr_io,
  462. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  463. intr_watch_cb, dev);
  464. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTED);
  465. return;
  466. failed:
  467. bt_hid_notify_state(dev, state);
  468. hid_device_remove(dev);
  469. }
  470. static void control_connect_cb(GIOChannel *chan, GError *conn_err,
  471. gpointer user_data)
  472. {
  473. struct hid_device *dev = user_data;
  474. GError *err = NULL;
  475. DBG("");
  476. if (conn_err) {
  477. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  478. error("hidhost: Failed to connect control channel (%s)",
  479. conn_err->message);
  480. goto failed;
  481. }
  482. /* Connect to the HID interrupt channel */
  483. dev->intr_io = bt_io_connect(interrupt_connect_cb, dev, NULL, &err,
  484. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  485. BT_IO_OPT_DEST_BDADDR, &dev->dst,
  486. BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
  487. BT_IO_OPT_SEC_LEVEL, dev->sec_level,
  488. BT_IO_OPT_INVALID);
  489. if (!dev->intr_io) {
  490. error("hidhost: Failed to connect interrupt channel (%s)",
  491. err->message);
  492. g_error_free(err);
  493. goto failed;
  494. }
  495. dev->ctrl_watch = g_io_add_watch(dev->ctrl_io,
  496. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  497. ctrl_watch_cb, dev);
  498. return;
  499. failed:
  500. hid_device_remove(dev);
  501. }
  502. static void hid_sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
  503. {
  504. struct hid_device *dev = data;
  505. sdp_list_t *list;
  506. GError *gerr = NULL;
  507. DBG("");
  508. if (err < 0) {
  509. error("hidhost: Unable to get SDP record: %s", strerror(-err));
  510. goto fail;
  511. }
  512. if (!recs || !recs->data) {
  513. error("hidhost: No SDP records found");
  514. goto fail;
  515. }
  516. for (list = recs; list != NULL; list = list->next) {
  517. sdp_record_t *rec = list->data;
  518. sdp_data_t *data;
  519. data = sdp_data_get(rec, SDP_ATTR_HID_COUNTRY_CODE);
  520. if (data)
  521. dev->country = data->val.uint8;
  522. data = sdp_data_get(rec, SDP_ATTR_HID_DEVICE_SUBCLASS);
  523. if (data) {
  524. dev->subclass = data->val.uint8;
  525. /* Encryption is mandatory for keyboards */
  526. if (dev->subclass & 0x40)
  527. dev->sec_level = BT_IO_SEC_MEDIUM;
  528. }
  529. data = sdp_data_get(rec, SDP_ATTR_HID_BOOT_DEVICE);
  530. if (data)
  531. dev->boot_dev = data->val.uint8;
  532. data = sdp_data_get(rec, SDP_ATTR_HID_DESCRIPTOR_LIST);
  533. if (data) {
  534. if (!SDP_IS_SEQ(data->dtd))
  535. goto fail;
  536. /* First HIDDescriptor */
  537. data = data->val.dataseq;
  538. if (!SDP_IS_SEQ(data->dtd))
  539. goto fail;
  540. /* ClassDescriptorType */
  541. data = data->val.dataseq;
  542. if (data->dtd != SDP_UINT8)
  543. goto fail;
  544. /* ClassDescriptorData */
  545. data = data->next;
  546. if (!data || !SDP_IS_TEXT_STR(data->dtd))
  547. goto fail;
  548. dev->rd_size = data->unitSize;
  549. dev->rd_data = g_memdup(data->val.str, data->unitSize);
  550. }
  551. }
  552. if (dev->ctrl_io) {
  553. /* Raise the security level for this device if needed. */
  554. if ((dev->sec_level > BT_IO_SEC_LOW) &&
  555. !bt_io_set(dev->ctrl_io, &gerr,
  556. BT_IO_OPT_SEC_LEVEL, dev->sec_level,
  557. BT_IO_OPT_INVALID)) {
  558. error("hidhost: Cannot raise security level: %s",
  559. gerr->message);
  560. g_error_free(gerr);
  561. goto fail;
  562. }
  563. if (uhid_create(dev) < 0)
  564. goto fail;
  565. return;
  566. }
  567. dev->ctrl_io = bt_io_connect(control_connect_cb, dev, NULL, &gerr,
  568. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  569. BT_IO_OPT_DEST_BDADDR, &dev->dst,
  570. BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
  571. BT_IO_OPT_SEC_LEVEL, dev->sec_level,
  572. BT_IO_OPT_INVALID);
  573. if (gerr) {
  574. error("hidhost: Failed to connect control channel (%s)",
  575. gerr->message);
  576. g_error_free(gerr);
  577. goto fail;
  578. }
  579. return;
  580. fail:
  581. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  582. hid_device_remove(dev);
  583. }
  584. static void hid_sdp_did_search_cb(sdp_list_t *recs, int err, gpointer data)
  585. {
  586. struct hid_device *dev = data;
  587. sdp_list_t *list;
  588. uuid_t uuid;
  589. DBG("");
  590. if (err < 0) {
  591. error("hidhost: Unable to get Device ID SDP record: %s",
  592. strerror(-err));
  593. goto fail;
  594. }
  595. if (!recs || !recs->data) {
  596. error("hidhost: No Device ID SDP records found");
  597. goto fail;
  598. }
  599. for (list = recs; list; list = list->next) {
  600. sdp_record_t *rec = list->data;
  601. sdp_data_t *data;
  602. data = sdp_data_get(rec, SDP_ATTR_VENDOR_ID);
  603. if (data)
  604. dev->vendor = data->val.uint16;
  605. data = sdp_data_get(rec, SDP_ATTR_PRODUCT_ID);
  606. if (data)
  607. dev->product = data->val.uint16;
  608. data = sdp_data_get(rec, SDP_ATTR_VERSION);
  609. if (data)
  610. dev->version = data->val.uint16;
  611. }
  612. sdp_uuid16_create(&uuid, HID_SVCLASS_ID);
  613. if (bt_search_service(&adapter_addr, &dev->dst, &uuid,
  614. hid_sdp_search_cb, dev, NULL, 0) < 0) {
  615. error("hidhost: Failed to search SDP details");
  616. goto fail;
  617. }
  618. return;
  619. fail:
  620. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  621. hid_device_remove(dev);
  622. }
  623. static void hog_conn_cb(const bdaddr_t *addr, int err, void *attrib)
  624. {
  625. GSList *l;
  626. struct hid_device *dev;
  627. l = g_slist_find_custom(devices, addr, device_cmp);
  628. dev = l ? l->data : NULL;
  629. if (err < 0) {
  630. if (!dev)
  631. return;
  632. if (dev->hog) {
  633. bt_hid_notify_state(dev,
  634. HAL_HIDHOST_STATE_DISCONNECTED);
  635. bt_hog_detach(dev->hog);
  636. return;
  637. }
  638. goto fail;
  639. }
  640. if (!dev)
  641. dev = hid_device_new(addr);
  642. if (!dev->hog) {
  643. /* TODO: Get device details and primary */
  644. dev->hog = bt_hog_new_default("bluez-input-device", dev->vendor,
  645. dev->product, dev->version, NULL);
  646. if (!dev->hog) {
  647. error("HoG: unable to create session");
  648. goto fail;
  649. }
  650. }
  651. if (!bt_hog_attach(dev->hog, attrib)) {
  652. error("HoG: unable to attach");
  653. goto fail;
  654. }
  655. if (!bt_gatt_set_security(addr, BT_IO_SEC_MEDIUM)) {
  656. error("Failed to set security level");
  657. goto fail;
  658. }
  659. DBG("");
  660. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTED);
  661. if (!bt_gatt_add_autoconnect(hog_app, &dev->dst))
  662. error("hidhost: Could not add to autoconnect list");
  663. return;
  664. fail:
  665. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  666. hid_device_remove(dev);
  667. }
  668. static bool hog_connect(struct hid_device *dev)
  669. {
  670. DBG("");
  671. if (hog_app)
  672. return bt_gatt_connect_app(hog_app, &dev->dst);
  673. hog_app = bt_gatt_register_app(HOG_UUID, GATT_CLIENT, hog_conn_cb);
  674. if (!hog_app) {
  675. error("hidhost: bt_gatt_register_app failed");
  676. return false;
  677. }
  678. return bt_gatt_connect_app(hog_app, &dev->dst);
  679. }
  680. static void bt_hid_connect(const void *buf, uint16_t len)
  681. {
  682. const struct hal_cmd_hidhost_connect *cmd = buf;
  683. struct hid_device *dev;
  684. uint8_t status;
  685. char addr[18];
  686. bdaddr_t dst;
  687. GSList *l;
  688. uuid_t uuid;
  689. DBG("");
  690. android2bdaddr(&cmd->bdaddr, &dst);
  691. l = g_slist_find_custom(devices, &dst, device_cmp);
  692. if (l)
  693. dev = l->data;
  694. else
  695. dev = hid_device_new(&dst);
  696. if (dev->state != HAL_HIDHOST_STATE_DISCONNECTED)
  697. goto done;
  698. ba2str(&dev->dst, addr);
  699. DBG("connecting to %s", addr);
  700. if (bt_device_last_seen_bearer(&dev->dst) != BDADDR_BREDR) {
  701. if (!hog_connect(dev)) {
  702. status = HAL_STATUS_FAILED;
  703. hid_device_remove(dev);
  704. goto failed;
  705. }
  706. goto done;
  707. }
  708. sdp_uuid16_create(&uuid, PNP_INFO_SVCLASS_ID);
  709. if (bt_search_service(&adapter_addr, &dev->dst, &uuid,
  710. hid_sdp_did_search_cb, dev, NULL, 0) < 0) {
  711. error("hidhost: Failed to search DeviceID SDP details");
  712. hid_device_remove(dev);
  713. status = HAL_STATUS_FAILED;
  714. goto failed;
  715. }
  716. done:
  717. if (dev->state == HAL_HIDHOST_STATE_DISCONNECTED)
  718. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTING);
  719. status = HAL_STATUS_SUCCESS;
  720. failed:
  721. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_CONNECT,
  722. status);
  723. }
  724. static bool hog_disconnect(struct hid_device *dev)
  725. {
  726. DBG("");
  727. if (dev->state == HAL_HIDHOST_STATE_DISCONNECTED)
  728. return false;
  729. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTING);
  730. if (!bt_gatt_disconnect_app(hog_app, &dev->dst)) {
  731. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
  732. hid_device_remove(dev);
  733. }
  734. return true;
  735. }
  736. static void bt_hid_disconnect(const void *buf, uint16_t len)
  737. {
  738. const struct hal_cmd_hidhost_disconnect *cmd = buf;
  739. struct hid_device *dev;
  740. uint8_t status;
  741. GSList *l;
  742. bdaddr_t dst;
  743. DBG("");
  744. android2bdaddr(&cmd->bdaddr, &dst);
  745. l = g_slist_find_custom(devices, &dst, device_cmp);
  746. if (!l) {
  747. status = HAL_STATUS_FAILED;
  748. goto failed;
  749. }
  750. dev = l->data;
  751. if (bt_is_device_le(&dst)) {
  752. if (!hog_disconnect(dev)) {
  753. status = HAL_STATUS_FAILED;
  754. goto failed;
  755. }
  756. goto done;
  757. }
  758. /* Wait either channels to HUP */
  759. if (dev->intr_io)
  760. g_io_channel_shutdown(dev->intr_io, TRUE, NULL);
  761. if (dev->ctrl_io)
  762. g_io_channel_shutdown(dev->ctrl_io, TRUE, NULL);
  763. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTING);
  764. done:
  765. status = HAL_STATUS_SUCCESS;
  766. failed:
  767. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_DISCONNECT,
  768. status);
  769. }
  770. static bool bt_hid_write_virtual_unplug(GIOChannel *chan)
  771. {
  772. uint8_t hdr = HID_MSG_CONTROL | HID_VIRTUAL_CABLE_UNPLUG;
  773. int fd = g_io_channel_unix_get_fd(chan);
  774. if (write(fd, &hdr, sizeof(hdr)) == sizeof(hdr))
  775. return true;
  776. error("hidhost: Error writing virtual unplug command: %s (%d)",
  777. strerror(errno), errno);
  778. return false;
  779. }
  780. static void bt_hid_virtual_unplug(const void *buf, uint16_t len)
  781. {
  782. const struct hal_cmd_hidhost_virtual_unplug *cmd = buf;
  783. struct hid_device *dev;
  784. GSList *l;
  785. uint8_t status;
  786. bdaddr_t dst;
  787. DBG("");
  788. android2bdaddr(&cmd->bdaddr, &dst);
  789. l = g_slist_find_custom(devices, &dst, device_cmp);
  790. if (!l) {
  791. status = HAL_STATUS_FAILED;
  792. goto failed;
  793. }
  794. dev = l->data;
  795. if (!(dev->ctrl_io)) {
  796. status = HAL_STATUS_FAILED;
  797. goto failed;
  798. }
  799. if (!bt_hid_write_virtual_unplug(dev->ctrl_io)) {
  800. status = HAL_STATUS_FAILED;
  801. goto failed;
  802. }
  803. /* Wait either channels to HUP */
  804. if (dev->intr_io)
  805. g_io_channel_shutdown(dev->intr_io, TRUE, NULL);
  806. if (dev->ctrl_io)
  807. g_io_channel_shutdown(dev->ctrl_io, TRUE, NULL);
  808. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTING);
  809. status = HAL_STATUS_SUCCESS;
  810. failed:
  811. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  812. HAL_OP_HIDHOST_VIRTUAL_UNPLUG, status);
  813. }
  814. static void bt_hid_info(const void *buf, uint16_t len)
  815. {
  816. const struct hal_cmd_hidhost_set_info *cmd = buf;
  817. if (len != sizeof(*cmd) + cmd->descr_len) {
  818. error("Invalid hid set info size (%u bytes), terminating", len);
  819. raise(SIGTERM);
  820. return;
  821. }
  822. /*
  823. * Data from hal_cmd_hidhost_set_info is usefull only when we create
  824. * UHID device. Once device is created all the transactions will be
  825. * done through the fd. There is no way to use this information
  826. * once device is created with HID internals.
  827. */
  828. DBG("Not supported");
  829. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SET_INFO,
  830. HAL_STATUS_UNSUPPORTED);
  831. }
  832. static void bt_hid_get_protocol(const void *buf, uint16_t len)
  833. {
  834. const struct hal_cmd_hidhost_get_protocol *cmd = buf;
  835. struct hid_device *dev;
  836. GSList *l;
  837. bdaddr_t dst;
  838. int fd;
  839. uint8_t hdr;
  840. uint8_t status;
  841. DBG("");
  842. switch (cmd->mode) {
  843. case HAL_HIDHOST_REPORT_PROTOCOL:
  844. case HAL_HIDHOST_BOOT_PROTOCOL:
  845. break;
  846. default:
  847. status = HAL_STATUS_INVALID;
  848. goto failed;
  849. }
  850. android2bdaddr(&cmd->bdaddr, &dst);
  851. l = g_slist_find_custom(devices, &dst, device_cmp);
  852. if (!l) {
  853. status = HAL_STATUS_FAILED;
  854. goto failed;
  855. }
  856. dev = l->data;
  857. hdr = HID_MSG_GET_PROTOCOL | cmd->mode;
  858. fd = g_io_channel_unix_get_fd(dev->ctrl_io);
  859. if (write(fd, &hdr, sizeof(hdr)) < 0) {
  860. error("hidhost: Error writing device_get_protocol: %s (%d)",
  861. strerror(errno), errno);
  862. status = HAL_STATUS_FAILED;
  863. goto failed;
  864. }
  865. dev->last_hid_msg = HID_MSG_GET_PROTOCOL;
  866. status = HAL_STATUS_SUCCESS;
  867. failed:
  868. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  869. HAL_OP_HIDHOST_GET_PROTOCOL, status);
  870. }
  871. static void bt_hid_set_protocol(const void *buf, uint16_t len)
  872. {
  873. const struct hal_cmd_hidhost_set_protocol *cmd = buf;
  874. struct hid_device *dev;
  875. GSList *l;
  876. bdaddr_t dst;
  877. int fd;
  878. uint8_t hdr;
  879. uint8_t status;
  880. DBG("");
  881. switch (cmd->mode) {
  882. case HAL_HIDHOST_REPORT_PROTOCOL:
  883. case HAL_HIDHOST_BOOT_PROTOCOL:
  884. break;
  885. default:
  886. status = HAL_STATUS_INVALID;
  887. goto failed;
  888. }
  889. android2bdaddr(&cmd->bdaddr, &dst);
  890. l = g_slist_find_custom(devices, &dst, device_cmp);
  891. if (!l) {
  892. status = HAL_STATUS_FAILED;
  893. goto failed;
  894. }
  895. dev = l->data;
  896. hdr = HID_MSG_SET_PROTOCOL | cmd->mode;
  897. fd = g_io_channel_unix_get_fd(dev->ctrl_io);
  898. if (write(fd, &hdr, sizeof(hdr)) < 0) {
  899. error("hidhost: error writing device_set_protocol: %s (%d)",
  900. strerror(errno), errno);
  901. status = HAL_STATUS_FAILED;
  902. goto failed;
  903. }
  904. dev->last_hid_msg = HID_MSG_SET_PROTOCOL;
  905. status = HAL_STATUS_SUCCESS;
  906. failed:
  907. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST,
  908. HAL_OP_HIDHOST_SET_PROTOCOL, status);
  909. }
  910. static void bt_hid_get_report(const void *buf, uint16_t len)
  911. {
  912. const struct hal_cmd_hidhost_get_report *cmd = buf;
  913. struct hid_device *dev;
  914. GSList *l;
  915. bdaddr_t dst;
  916. int fd;
  917. uint8_t *req;
  918. uint8_t req_size;
  919. uint8_t status;
  920. DBG("");
  921. switch (cmd->type) {
  922. case HAL_HIDHOST_INPUT_REPORT:
  923. case HAL_HIDHOST_OUTPUT_REPORT:
  924. case HAL_HIDHOST_FEATURE_REPORT:
  925. break;
  926. default:
  927. status = HAL_STATUS_INVALID;
  928. goto failed;
  929. }
  930. android2bdaddr(&cmd->bdaddr, &dst);
  931. l = g_slist_find_custom(devices, &dst, device_cmp);
  932. if (!l) {
  933. status = HAL_STATUS_FAILED;
  934. goto failed;
  935. }
  936. dev = l->data;
  937. req_size = (cmd->buf_size > 0) ? 4 : 2;
  938. req = g_try_malloc0(req_size);
  939. if (!req) {
  940. status = HAL_STATUS_NOMEM;
  941. goto failed;
  942. }
  943. req[0] = HID_MSG_GET_REPORT | cmd->type;
  944. req[1] = cmd->id;
  945. if (cmd->buf_size > 0) {
  946. req[0] = req[0] | HID_GET_REPORT_SIZE_FIELD;
  947. put_le16(cmd->buf_size, &req[2]);
  948. }
  949. fd = g_io_channel_unix_get_fd(dev->ctrl_io);
  950. if (write(fd, req, req_size) < 0) {
  951. error("hidhost: error writing hid_get_report: %s (%d)",
  952. strerror(errno), errno);
  953. g_free(req);
  954. status = HAL_STATUS_FAILED;
  955. goto failed;
  956. }
  957. dev->last_hid_msg = HID_MSG_GET_REPORT;
  958. g_free(req);
  959. status = HAL_STATUS_SUCCESS;
  960. failed:
  961. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_GET_REPORT,
  962. status);
  963. }
  964. static void bt_hid_set_report(const void *buf, uint16_t len)
  965. {
  966. const struct hal_cmd_hidhost_set_report *cmd = buf;
  967. struct hid_device *dev;
  968. GSList *l;
  969. bdaddr_t dst;
  970. int fd;
  971. uint8_t *req = NULL;
  972. uint8_t req_size;
  973. uint8_t status;
  974. DBG("");
  975. if (len != sizeof(*cmd) + cmd->len) {
  976. error("Invalid hid set report size (%u bytes), terminating",
  977. len);
  978. raise(SIGTERM);
  979. return;
  980. }
  981. switch (cmd->type) {
  982. case HAL_HIDHOST_INPUT_REPORT:
  983. case HAL_HIDHOST_OUTPUT_REPORT:
  984. case HAL_HIDHOST_FEATURE_REPORT:
  985. break;
  986. default:
  987. status = HAL_STATUS_INVALID;
  988. goto failed;
  989. }
  990. android2bdaddr(&cmd->bdaddr, &dst);
  991. l = g_slist_find_custom(devices, &dst, device_cmp);
  992. if (!l) {
  993. status = HAL_STATUS_FAILED;
  994. goto failed;
  995. }
  996. dev = l->data;
  997. if (!dev->ctrl_io && !dev->hog) {
  998. status = HAL_STATUS_FAILED;
  999. goto failed;
  1000. }
  1001. req_size = 1 + (cmd->len / 2);
  1002. req = g_try_malloc0(req_size);
  1003. if (!req) {
  1004. status = HAL_STATUS_NOMEM;
  1005. goto failed;
  1006. }
  1007. req[0] = HID_MSG_SET_REPORT | cmd->type;
  1008. /*
  1009. * Report data coming to HAL is in ascii format, HAL sends
  1010. * data in hex to daemon, so convert to binary.
  1011. */
  1012. if (!hex2buf(cmd->data, req + 1, req_size - 1)) {
  1013. status = HAL_STATUS_INVALID;
  1014. goto failed;
  1015. }
  1016. if (dev->hog) {
  1017. if (bt_hog_send_report(dev->hog, req + 1, req_size - 1,
  1018. cmd->type) < 0) {
  1019. status = HAL_STATUS_FAILED;
  1020. goto failed;
  1021. }
  1022. goto done;
  1023. }
  1024. fd = g_io_channel_unix_get_fd(dev->ctrl_io);
  1025. if (write(fd, req, req_size) < 0) {
  1026. error("hidhost: error writing hid_set_report: %s (%d)",
  1027. strerror(errno), errno);
  1028. status = HAL_STATUS_FAILED;
  1029. goto failed;
  1030. }
  1031. dev->last_hid_msg = HID_MSG_SET_REPORT;
  1032. done:
  1033. status = HAL_STATUS_SUCCESS;
  1034. failed:
  1035. g_free(req);
  1036. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SET_REPORT,
  1037. status);
  1038. }
  1039. static void bt_hid_send_data(const void *buf, uint16_t len)
  1040. {
  1041. const struct hal_cmd_hidhost_send_data *cmd = buf;
  1042. struct hid_device *dev;
  1043. GSList *l;
  1044. bdaddr_t dst;
  1045. int fd;
  1046. uint8_t *req = NULL;
  1047. uint8_t req_size;
  1048. uint8_t status;
  1049. DBG("");
  1050. if (len != sizeof(*cmd) + cmd->len) {
  1051. error("Invalid hid send data size (%u bytes), terminating",
  1052. len);
  1053. raise(SIGTERM);
  1054. return;
  1055. }
  1056. android2bdaddr(&cmd->bdaddr, &dst);
  1057. l = g_slist_find_custom(devices, &dst, device_cmp);
  1058. if (!l) {
  1059. status = HAL_STATUS_FAILED;
  1060. goto failed;
  1061. }
  1062. dev = l->data;
  1063. if (!(dev->intr_io)) {
  1064. status = HAL_STATUS_FAILED;
  1065. goto failed;
  1066. }
  1067. req_size = 1 + (cmd->len / 2);
  1068. req = g_try_malloc0(req_size);
  1069. if (!req) {
  1070. status = HAL_STATUS_NOMEM;
  1071. goto failed;
  1072. }
  1073. req[0] = HID_MSG_DATA | HID_DATA_TYPE_OUTPUT;
  1074. /*
  1075. * Report data coming to HAL is in ascii format, HAL sends
  1076. * data in hex to daemon, so convert to binary.
  1077. */
  1078. if (!hex2buf(cmd->data, req + 1, req_size - 1)) {
  1079. status = HAL_STATUS_INVALID;
  1080. goto failed;
  1081. }
  1082. fd = g_io_channel_unix_get_fd(dev->intr_io);
  1083. if (write(fd, req, req_size) < 0) {
  1084. error("hidhost: error writing data to HID device: %s (%d)",
  1085. strerror(errno), errno);
  1086. status = HAL_STATUS_FAILED;
  1087. goto failed;
  1088. }
  1089. status = HAL_STATUS_SUCCESS;
  1090. failed:
  1091. g_free(req);
  1092. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SEND_DATA,
  1093. status);
  1094. }
  1095. static const struct ipc_handler cmd_handlers[] = {
  1096. /* HAL_OP_HIDHOST_CONNECT */
  1097. { bt_hid_connect, false, sizeof(struct hal_cmd_hidhost_connect) },
  1098. /* HAL_OP_HIDHOST_DISCONNECT */
  1099. { bt_hid_disconnect, false, sizeof(struct hal_cmd_hidhost_disconnect) },
  1100. /* HAL_OP_HIDHOST_VIRTUAL_UNPLUG */
  1101. { bt_hid_virtual_unplug, false,
  1102. sizeof(struct hal_cmd_hidhost_virtual_unplug) },
  1103. /* HAL_OP_HIDHOST_SET_INFO */
  1104. { bt_hid_info, true, sizeof(struct hal_cmd_hidhost_set_info) },
  1105. /* HAL_OP_HIDHOST_GET_PROTOCOL */
  1106. { bt_hid_get_protocol, false,
  1107. sizeof(struct hal_cmd_hidhost_get_protocol) },
  1108. /* HAL_OP_HIDHOST_SET_PROTOCOL */
  1109. { bt_hid_set_protocol, false,
  1110. sizeof(struct hal_cmd_hidhost_get_protocol) },
  1111. /* HAL_OP_HIDHOST_GET_REPORT */
  1112. { bt_hid_get_report, false, sizeof(struct hal_cmd_hidhost_get_report) },
  1113. /* HAL_OP_HIDHOST_SET_REPORT */
  1114. { bt_hid_set_report, true, sizeof(struct hal_cmd_hidhost_set_report) },
  1115. /* HAL_OP_HIDHOST_SEND_DATA */
  1116. { bt_hid_send_data, true, sizeof(struct hal_cmd_hidhost_send_data) },
  1117. };
  1118. static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
  1119. {
  1120. struct hid_device *dev;
  1121. bdaddr_t dst;
  1122. char address[18];
  1123. uint16_t psm;
  1124. GError *gerr = NULL;
  1125. GSList *l;
  1126. uuid_t uuid;
  1127. if (err) {
  1128. error("hidhost: Connect failed (%s)", err->message);
  1129. return;
  1130. }
  1131. bt_io_get(chan, &gerr,
  1132. BT_IO_OPT_DEST_BDADDR, &dst,
  1133. BT_IO_OPT_PSM, &psm,
  1134. BT_IO_OPT_INVALID);
  1135. if (gerr) {
  1136. error("hidhost: Failed to read remote address (%s)",
  1137. gerr->message);
  1138. g_io_channel_shutdown(chan, TRUE, NULL);
  1139. g_error_free(gerr);
  1140. return;
  1141. }
  1142. ba2str(&dst, address);
  1143. DBG("Incoming connection from %s on PSM %d", address, psm);
  1144. if (!bt_device_is_bonded(&dst)) {
  1145. warn("hidhost: Rejecting connection from unknown device %s",
  1146. address);
  1147. if (psm == L2CAP_PSM_HIDP_CTRL)
  1148. bt_hid_write_virtual_unplug(chan);
  1149. g_io_channel_shutdown(chan, TRUE, NULL);
  1150. return;
  1151. }
  1152. switch (psm) {
  1153. case L2CAP_PSM_HIDP_CTRL:
  1154. l = g_slist_find_custom(devices, &dst, device_cmp);
  1155. if (l)
  1156. return;
  1157. dev = hid_device_new(&dst);
  1158. dev->ctrl_io = g_io_channel_ref(chan);
  1159. sdp_uuid16_create(&uuid, PNP_INFO_SVCLASS_ID);
  1160. if (bt_search_service(&adapter_addr, &dev->dst, &uuid,
  1161. hid_sdp_did_search_cb, dev, NULL, 0) < 0) {
  1162. error("hidhost: Failed to search DID SDP details");
  1163. hid_device_remove(dev);
  1164. return;
  1165. }
  1166. dev->ctrl_watch = g_io_add_watch(dev->ctrl_io,
  1167. G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  1168. ctrl_watch_cb, dev);
  1169. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTING);
  1170. break;
  1171. case L2CAP_PSM_HIDP_INTR:
  1172. l = g_slist_find_custom(devices, &dst, device_cmp);
  1173. if (!l)
  1174. return;
  1175. dev = l->data;
  1176. dev->intr_io = g_io_channel_ref(chan);
  1177. dev->intr_watch = g_io_add_watch(dev->intr_io,
  1178. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  1179. intr_watch_cb, dev);
  1180. bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTED);
  1181. break;
  1182. }
  1183. }
  1184. static void hid_unpaired_cb(const bdaddr_t *addr)
  1185. {
  1186. GSList *l;
  1187. struct hid_device *dev;
  1188. char address[18];
  1189. l = g_slist_find_custom(devices, addr, device_cmp);
  1190. if (!l)
  1191. return;
  1192. dev = l->data;
  1193. ba2str(addr, address);
  1194. DBG("Unpaired device %s", address);
  1195. if (hog_app)
  1196. bt_gatt_remove_autoconnect(hog_app, addr);
  1197. hid_device_remove(dev);
  1198. }
  1199. bool bt_hid_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
  1200. {
  1201. GError *err = NULL;
  1202. DBG("");
  1203. if (!bt_unpaired_register(hid_unpaired_cb)) {
  1204. error("hidhost: Could not register unpaired callback");
  1205. return false;
  1206. }
  1207. bacpy(&adapter_addr, addr);
  1208. ctrl_io = bt_io_listen(connect_cb, NULL, NULL, NULL, &err,
  1209. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1210. BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
  1211. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  1212. BT_IO_OPT_INVALID);
  1213. if (!ctrl_io) {
  1214. error("hidhost: Failed to listen on control channel: %s",
  1215. err->message);
  1216. g_error_free(err);
  1217. return false;
  1218. }
  1219. intr_io = bt_io_listen(connect_cb, NULL, NULL, NULL, &err,
  1220. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1221. BT_IO_OPT_PSM, L2CAP_PSM_HIDP_INTR,
  1222. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
  1223. BT_IO_OPT_INVALID);
  1224. if (!intr_io) {
  1225. error("hidhost: Failed to listen on interrupt channel: %s",
  1226. err->message);
  1227. g_error_free(err);
  1228. g_io_channel_shutdown(ctrl_io, TRUE, NULL);
  1229. g_io_channel_unref(ctrl_io);
  1230. ctrl_io = NULL;
  1231. return false;
  1232. }
  1233. hal_ipc = ipc;
  1234. ipc_register(hal_ipc, HAL_SERVICE_ID_HIDHOST, cmd_handlers,
  1235. G_N_ELEMENTS(cmd_handlers));
  1236. return true;
  1237. }
  1238. void bt_hid_unregister(void)
  1239. {
  1240. DBG("");
  1241. if (hog_app > 0)
  1242. bt_gatt_unregister_app(hog_app);
  1243. g_slist_free_full(devices, hid_device_free);
  1244. devices = NULL;
  1245. if (ctrl_io) {
  1246. g_io_channel_shutdown(ctrl_io, TRUE, NULL);
  1247. g_io_channel_unref(ctrl_io);
  1248. ctrl_io = NULL;
  1249. }
  1250. if (intr_io) {
  1251. g_io_channel_shutdown(intr_io, TRUE, NULL);
  1252. g_io_channel_unref(intr_io);
  1253. intr_io = NULL;
  1254. }
  1255. ipc_unregister(hal_ipc, HAL_SERVICE_ID_HIDHOST);
  1256. hal_ipc = NULL;
  1257. bt_unpaired_unregister(hid_unpaired_cb);
  1258. }