hog-lib.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 Intel Corporation.
  7. * Copyright (C) 2012 Marcel Holtmann <marcel@holtmann.org>
  8. * Copyright (C) 2012 Nordic Semiconductor Inc.
  9. * Copyright (C) 2012 Instituto Nokia de Tecnologia - INdT
  10. *
  11. *
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include <config.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <stdbool.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <ctype.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <glib.h>
  25. #include "lib/bluetooth.h"
  26. #include "lib/sdp.h"
  27. #include "lib/uuid.h"
  28. #include "src/shared/util.h"
  29. #include "src/shared/uhid.h"
  30. #include "src/shared/queue.h"
  31. #include "src/shared/att.h"
  32. #include "src/shared/gatt-db.h"
  33. #include "src/log.h"
  34. #include "attrib/att.h"
  35. #include "attrib/gattrib.h"
  36. #include "attrib/gatt.h"
  37. #include "btio/btio.h"
  38. #include "profiles/scanparam/scpp.h"
  39. #include "profiles/deviceinfo/dis.h"
  40. #include "profiles/battery/bas.h"
  41. #include "profiles/input/hog-lib.h"
  42. #define HOG_UUID16 0x1812
  43. #define HOG_INFO_UUID 0x2A4A
  44. #define HOG_REPORT_MAP_UUID 0x2A4B
  45. #define HOG_REPORT_UUID 0x2A4D
  46. #define HOG_PROTO_MODE_UUID 0x2A4E
  47. #define HOG_CONTROL_POINT_UUID 0x2A4C
  48. #define HOG_REPORT_TYPE_INPUT 1
  49. #define HOG_REPORT_TYPE_OUTPUT 2
  50. #define HOG_REPORT_TYPE_FEATURE 3
  51. #define HOG_PROTO_MODE_BOOT 0
  52. #define HOG_PROTO_MODE_REPORT 1
  53. #define HOG_REPORT_MAP_MAX_SIZE 512
  54. #define HID_INFO_SIZE 4
  55. #define ATT_NOTIFICATION_HEADER_SIZE 3
  56. struct bt_hog {
  57. int ref_count;
  58. char *name;
  59. uint16_t vendor;
  60. uint16_t product;
  61. uint16_t version;
  62. struct gatt_db_attribute *attr;
  63. struct gatt_primary *primary;
  64. GAttrib *attrib;
  65. GSList *reports;
  66. struct bt_uhid *uhid;
  67. int uhid_fd;
  68. bool uhid_created;
  69. gboolean has_report_id;
  70. uint16_t bcdhid;
  71. uint8_t bcountrycode;
  72. uint16_t proto_mode_handle;
  73. uint16_t ctrlpt_handle;
  74. uint8_t flags;
  75. unsigned int getrep_att;
  76. uint16_t getrep_id;
  77. unsigned int setrep_att;
  78. uint16_t setrep_id;
  79. struct bt_scpp *scpp;
  80. struct bt_dis *dis;
  81. struct queue *bas;
  82. GSList *instances;
  83. struct queue *gatt_op;
  84. struct gatt_db *gatt_db;
  85. struct gatt_db_attribute *report_map_attr;
  86. };
  87. struct report_map {
  88. uint8_t value[HOG_REPORT_MAP_MAX_SIZE];
  89. size_t length;
  90. };
  91. struct report {
  92. struct bt_hog *hog;
  93. uint8_t id;
  94. uint8_t type;
  95. uint16_t handle;
  96. uint16_t value_handle;
  97. uint8_t properties;
  98. uint16_t ccc_handle;
  99. guint notifyid;
  100. uint16_t len;
  101. uint8_t *value;
  102. };
  103. struct gatt_request {
  104. unsigned int id;
  105. struct bt_hog *hog;
  106. void *user_data;
  107. };
  108. static struct gatt_request *create_request(struct bt_hog *hog,
  109. void *user_data)
  110. {
  111. struct gatt_request *req;
  112. req = new0(struct gatt_request, 1);
  113. if (!req)
  114. return NULL;
  115. req->user_data = user_data;
  116. req->hog = bt_hog_ref(hog);
  117. return req;
  118. }
  119. static bool set_and_store_gatt_req(struct bt_hog *hog,
  120. struct gatt_request *req,
  121. unsigned int id)
  122. {
  123. req->id = id;
  124. return queue_push_head(hog->gatt_op, req);
  125. }
  126. static void destroy_gatt_req(struct gatt_request *req)
  127. {
  128. queue_remove(req->hog->gatt_op, req);
  129. bt_hog_unref(req->hog);
  130. free(req);
  131. }
  132. static void write_char(struct bt_hog *hog, GAttrib *attrib, uint16_t handle,
  133. const uint8_t *value, size_t vlen,
  134. GAttribResultFunc func,
  135. gpointer user_data)
  136. {
  137. struct gatt_request *req;
  138. unsigned int id;
  139. req = create_request(hog, user_data);
  140. if (!req)
  141. return;
  142. id = gatt_write_char(attrib, handle, value, vlen, func, req);
  143. if (!id) {
  144. error("hog: Could not write char");
  145. return;
  146. }
  147. if (!set_and_store_gatt_req(hog, req, id)) {
  148. error("hog: Failed to queue write char req");
  149. g_attrib_cancel(attrib, id);
  150. free(req);
  151. }
  152. }
  153. static void read_char(struct bt_hog *hog, GAttrib *attrib, uint16_t handle,
  154. GAttribResultFunc func, gpointer user_data)
  155. {
  156. struct gatt_request *req;
  157. unsigned int id;
  158. req = create_request(hog, user_data);
  159. if (!req)
  160. return;
  161. id = gatt_read_char(attrib, handle, func, req);
  162. if (!id) {
  163. error("hog: Could not read char");
  164. return;
  165. }
  166. if (!set_and_store_gatt_req(hog, req, id)) {
  167. error("hog: Failed to queue read char req");
  168. g_attrib_cancel(attrib, id);
  169. free(req);
  170. }
  171. }
  172. static void discover_desc(struct bt_hog *hog, GAttrib *attrib,
  173. uint16_t start, uint16_t end, gatt_cb_t func,
  174. gpointer user_data)
  175. {
  176. struct gatt_request *req;
  177. unsigned int id;
  178. req = create_request(hog, user_data);
  179. if (!req)
  180. return;
  181. id = gatt_discover_desc(attrib, start, end, NULL, func, req);
  182. if (!id) {
  183. error("hog: Could not discover descriptors");
  184. return;
  185. }
  186. if (!set_and_store_gatt_req(hog, req, id)) {
  187. error("hog: Failed to queue discover descriptors req");
  188. g_attrib_cancel(attrib, id);
  189. free(req);
  190. }
  191. }
  192. static void discover_char(struct bt_hog *hog, GAttrib *attrib,
  193. uint16_t start, uint16_t end,
  194. bt_uuid_t *uuid, gatt_cb_t func,
  195. gpointer user_data)
  196. {
  197. struct gatt_request *req;
  198. unsigned int id;
  199. req = create_request(hog, user_data);
  200. if (!req)
  201. return;
  202. id = gatt_discover_char(attrib, start, end, uuid, func, req);
  203. if (!id) {
  204. error("hog: Could not discover characteristic");
  205. return;
  206. }
  207. if (!set_and_store_gatt_req(hog, req, id)) {
  208. error("hog: Failed to queue discover characteristic req");
  209. g_attrib_cancel(attrib, id);
  210. free(req);
  211. }
  212. }
  213. static void discover_primary(struct bt_hog *hog, GAttrib *attrib,
  214. bt_uuid_t *uuid, gatt_cb_t func,
  215. gpointer user_data)
  216. {
  217. struct gatt_request *req;
  218. unsigned int id;
  219. req = create_request(hog, user_data);
  220. if (!req)
  221. return;
  222. id = gatt_discover_primary(attrib, uuid, func, req);
  223. if (!id) {
  224. error("hog: Could not send discover primary");
  225. return;
  226. }
  227. if (!set_and_store_gatt_req(hog, req, id)) {
  228. error("hog: Failed to queue discover primary req");
  229. g_attrib_cancel(attrib, id);
  230. free(req);
  231. }
  232. }
  233. static void find_included(struct bt_hog *hog, GAttrib *attrib,
  234. uint16_t start, uint16_t end,
  235. gatt_cb_t func, gpointer user_data)
  236. {
  237. struct gatt_request *req;
  238. unsigned int id;
  239. req = create_request(hog, user_data);
  240. if (!req)
  241. return;
  242. id = gatt_find_included(attrib, start, end, func, req);
  243. if (!id) {
  244. error("hog: Could not find included");
  245. return;
  246. }
  247. if (!set_and_store_gatt_req(hog, req, id)) {
  248. error("hog: Failed to queue find included req");
  249. g_attrib_cancel(attrib, id);
  250. free(req);
  251. }
  252. }
  253. static void report_value_cb(const guint8 *pdu, guint16 len, gpointer user_data)
  254. {
  255. struct report *report = user_data;
  256. struct bt_hog *hog = report->hog;
  257. struct uhid_event ev;
  258. uint8_t *buf;
  259. int err;
  260. if (len < ATT_NOTIFICATION_HEADER_SIZE) {
  261. error("Malformed ATT notification");
  262. return;
  263. }
  264. pdu += ATT_NOTIFICATION_HEADER_SIZE;
  265. len -= ATT_NOTIFICATION_HEADER_SIZE;
  266. memset(&ev, 0, sizeof(ev));
  267. ev.type = UHID_INPUT;
  268. buf = ev.u.input.data;
  269. if (hog->has_report_id) {
  270. buf[0] = report->id;
  271. len = MIN(len, sizeof(ev.u.input.data) - 1);
  272. memcpy(buf + 1, pdu, len);
  273. ev.u.input.size = ++len;
  274. } else {
  275. len = MIN(len, sizeof(ev.u.input.data));
  276. memcpy(buf, pdu, len);
  277. ev.u.input.size = len;
  278. }
  279. err = bt_uhid_send(hog->uhid, &ev);
  280. if (err < 0) {
  281. error("bt_uhid_send: %s (%d)", strerror(-err), -err);
  282. return;
  283. }
  284. }
  285. static void report_ccc_written_cb(guint8 status, const guint8 *pdu,
  286. guint16 plen, gpointer user_data)
  287. {
  288. struct gatt_request *req = user_data;
  289. struct report *report = req->user_data;
  290. struct bt_hog *hog = report->hog;
  291. destroy_gatt_req(req);
  292. if (status != 0) {
  293. error("Write report characteristic descriptor failed: %s",
  294. att_ecode2str(status));
  295. return;
  296. }
  297. if (report->notifyid)
  298. return;
  299. report->notifyid = g_attrib_register(hog->attrib,
  300. ATT_OP_HANDLE_NOTIFY,
  301. report->value_handle,
  302. report_value_cb, report, NULL);
  303. DBG("Report characteristic descriptor written: notifications enabled");
  304. }
  305. static void write_ccc(struct bt_hog *hog, GAttrib *attrib, uint16_t handle,
  306. void *user_data)
  307. {
  308. uint8_t value[2];
  309. put_le16(GATT_CLIENT_CHARAC_CFG_NOTIF_BIT, value);
  310. write_char(hog, attrib, handle, value, sizeof(value),
  311. report_ccc_written_cb, user_data);
  312. }
  313. static void ccc_read_cb(guint8 status, const guint8 *pdu, guint16 len,
  314. gpointer user_data)
  315. {
  316. struct gatt_request *req = user_data;
  317. struct report *report = req->user_data;
  318. destroy_gatt_req(req);
  319. if (status != 0) {
  320. error("Error reading CCC value: %s", att_ecode2str(status));
  321. return;
  322. }
  323. write_ccc(report->hog, report->hog->attrib, report->ccc_handle, report);
  324. }
  325. static const char *type_to_string(uint8_t type)
  326. {
  327. switch (type) {
  328. case HOG_REPORT_TYPE_INPUT:
  329. return "input";
  330. case HOG_REPORT_TYPE_OUTPUT:
  331. return "output";
  332. case HOG_REPORT_TYPE_FEATURE:
  333. return "feature";
  334. }
  335. return NULL;
  336. }
  337. static void report_reference_cb(guint8 status, const guint8 *pdu,
  338. guint16 plen, gpointer user_data)
  339. {
  340. struct gatt_request *req = user_data;
  341. struct report *report = req->user_data;
  342. destroy_gatt_req(req);
  343. if (status != 0) {
  344. error("Read Report Reference descriptor failed: %s",
  345. att_ecode2str(status));
  346. return;
  347. }
  348. if (plen != 3) {
  349. error("Malformed ATT read response");
  350. return;
  351. }
  352. report->id = pdu[1];
  353. report->type = pdu[2];
  354. DBG("Report 0x%04x: id 0x%02x type %s", report->value_handle,
  355. report->id, type_to_string(report->type));
  356. /* Enable notifications only for Input Reports */
  357. if (report->type == HOG_REPORT_TYPE_INPUT)
  358. read_char(report->hog, report->hog->attrib, report->ccc_handle,
  359. ccc_read_cb, report);
  360. }
  361. static void external_report_reference_cb(guint8 status, const guint8 *pdu,
  362. guint16 plen, gpointer user_data);
  363. static void discover_external_cb(uint8_t status, GSList *descs, void *user_data)
  364. {
  365. struct gatt_request *req = user_data;
  366. struct bt_hog *hog = req->user_data;
  367. destroy_gatt_req(req);
  368. if (status != 0) {
  369. error("Discover external descriptors failed: %s",
  370. att_ecode2str(status));
  371. return;
  372. }
  373. for ( ; descs; descs = descs->next) {
  374. struct gatt_desc *desc = descs->data;
  375. read_char(hog, hog->attrib, desc->handle,
  376. external_report_reference_cb,
  377. hog);
  378. }
  379. }
  380. static void discover_external(struct bt_hog *hog, GAttrib *attrib,
  381. uint16_t start, uint16_t end,
  382. gpointer user_data)
  383. {
  384. bt_uuid_t uuid;
  385. if (start > end)
  386. return;
  387. bt_uuid16_create(&uuid, GATT_EXTERNAL_REPORT_REFERENCE);
  388. discover_desc(hog, attrib, start, end, discover_external_cb,
  389. user_data);
  390. }
  391. static void discover_report_cb(uint8_t status, GSList *descs, void *user_data)
  392. {
  393. struct gatt_request *req = user_data;
  394. struct report *report = req->user_data;
  395. struct bt_hog *hog = report->hog;
  396. destroy_gatt_req(req);
  397. if (status != 0) {
  398. error("Discover report descriptors failed: %s",
  399. att_ecode2str(status));
  400. return;
  401. }
  402. for ( ; descs; descs = descs->next) {
  403. struct gatt_desc *desc = descs->data;
  404. switch (desc->uuid16) {
  405. case GATT_CLIENT_CHARAC_CFG_UUID:
  406. report->ccc_handle = desc->handle;
  407. break;
  408. case GATT_REPORT_REFERENCE:
  409. read_char(hog, hog->attrib, desc->handle,
  410. report_reference_cb, report);
  411. break;
  412. }
  413. }
  414. }
  415. static void discover_report(struct bt_hog *hog, GAttrib *attrib,
  416. uint16_t start, uint16_t end,
  417. gpointer user_data)
  418. {
  419. if (start > end)
  420. return;
  421. discover_desc(hog, attrib, start, end, discover_report_cb, user_data);
  422. }
  423. static void report_read_cb(guint8 status, const guint8 *pdu, guint16 len,
  424. gpointer user_data)
  425. {
  426. struct gatt_request *req = user_data;
  427. struct report *report = req->user_data;
  428. destroy_gatt_req(req);
  429. if (status != 0) {
  430. error("Error reading Report value: %s", att_ecode2str(status));
  431. return;
  432. }
  433. if (report->value)
  434. g_free(report->value);
  435. report->value = g_memdup(pdu, len);
  436. report->len = len;
  437. }
  438. static int report_chrc_cmp(const void *data, const void *user_data)
  439. {
  440. const struct report *report = data;
  441. const struct gatt_char *decl = user_data;
  442. return report->handle - decl->handle;
  443. }
  444. static struct report *report_new(struct bt_hog *hog, struct gatt_char *chr)
  445. {
  446. struct report *report;
  447. GSList *l;
  448. /* Skip if report already exists */
  449. l = g_slist_find_custom(hog->reports, chr, report_chrc_cmp);
  450. if (l)
  451. return l->data;
  452. report = g_new0(struct report, 1);
  453. report->hog = hog;
  454. report->handle = chr->handle;
  455. report->value_handle = chr->value_handle;
  456. report->properties = chr->properties;
  457. hog->reports = g_slist_append(hog->reports, report);
  458. read_char(hog, hog->attrib, chr->value_handle, report_read_cb, report);
  459. return report;
  460. }
  461. static void external_service_char_cb(uint8_t status, GSList *chars,
  462. void *user_data)
  463. {
  464. struct gatt_request *req = user_data;
  465. struct bt_hog *hog = req->user_data;
  466. struct gatt_primary *primary = hog->primary;
  467. struct report *report;
  468. GSList *l;
  469. destroy_gatt_req(req);
  470. if (status != 0) {
  471. const char *str = att_ecode2str(status);
  472. DBG("Discover external service characteristic failed: %s", str);
  473. return;
  474. }
  475. for (l = chars; l; l = g_slist_next(l)) {
  476. struct gatt_char *chr, *next;
  477. uint16_t start, end;
  478. chr = l->data;
  479. next = l->next ? l->next->data : NULL;
  480. DBG("0x%04x UUID: %s properties: %02x",
  481. chr->handle, chr->uuid, chr->properties);
  482. report = report_new(hog, chr);
  483. start = chr->value_handle + 1;
  484. end = (next ? next->handle - 1 : primary->range.end);
  485. discover_report(hog, hog->attrib, start, end, report);
  486. }
  487. }
  488. static void external_report_reference_cb(guint8 status, const guint8 *pdu,
  489. guint16 plen, gpointer user_data)
  490. {
  491. struct gatt_request *req = user_data;
  492. struct bt_hog *hog = req->user_data;
  493. uint16_t uuid16;
  494. bt_uuid_t uuid;
  495. destroy_gatt_req(req);
  496. if (status != 0) {
  497. error("Read External Report Reference descriptor failed: %s",
  498. att_ecode2str(status));
  499. return;
  500. }
  501. if (plen != 3) {
  502. error("Malformed ATT read response");
  503. return;
  504. }
  505. uuid16 = get_le16(&pdu[1]);
  506. DBG("External report reference read, external report characteristic "
  507. "UUID: 0x%04x", uuid16);
  508. /* Do not discover if is not a Report */
  509. if (uuid16 != HOG_REPORT_UUID)
  510. return;
  511. bt_uuid16_create(&uuid, uuid16);
  512. discover_char(hog, hog->attrib, 0x0001, 0xffff, &uuid,
  513. external_service_char_cb, hog);
  514. }
  515. static int report_cmp(gconstpointer a, gconstpointer b)
  516. {
  517. const struct report *ra = a, *rb = b;
  518. /* sort by type first.. */
  519. if (ra->type != rb->type)
  520. return ra->type - rb->type;
  521. /* skip id check in case of report id 0 */
  522. if (!rb->id)
  523. return 0;
  524. /* ..then by id */
  525. return ra->id - rb->id;
  526. }
  527. static struct report *find_report(struct bt_hog *hog, uint8_t type, uint8_t id)
  528. {
  529. struct report cmp;
  530. GSList *l;
  531. cmp.type = type;
  532. cmp.id = hog->has_report_id ? id : 0;
  533. l = g_slist_find_custom(hog->reports, &cmp, report_cmp);
  534. return l ? l->data : NULL;
  535. }
  536. static struct report *find_report_by_rtype(struct bt_hog *hog, uint8_t rtype,
  537. uint8_t id)
  538. {
  539. uint8_t type;
  540. switch (rtype) {
  541. case UHID_FEATURE_REPORT:
  542. type = HOG_REPORT_TYPE_FEATURE;
  543. break;
  544. case UHID_OUTPUT_REPORT:
  545. type = HOG_REPORT_TYPE_OUTPUT;
  546. break;
  547. case UHID_INPUT_REPORT:
  548. type = HOG_REPORT_TYPE_INPUT;
  549. break;
  550. default:
  551. return NULL;
  552. }
  553. return find_report(hog, type, id);
  554. }
  555. static void output_written_cb(guint8 status, const guint8 *pdu,
  556. guint16 plen, gpointer user_data)
  557. {
  558. struct gatt_request *req = user_data;
  559. destroy_gatt_req(req);
  560. if (status != 0) {
  561. error("Write output report failed: %s", att_ecode2str(status));
  562. return;
  563. }
  564. }
  565. static void forward_report(struct uhid_event *ev, void *user_data)
  566. {
  567. struct bt_hog *hog = user_data;
  568. struct report *report;
  569. void *data;
  570. int size;
  571. report = find_report_by_rtype(hog, ev->u.output.rtype,
  572. ev->u.output.data[0]);
  573. if (!report)
  574. return;
  575. data = ev->u.output.data;
  576. size = ev->u.output.size;
  577. if (hog->has_report_id && size > 0) {
  578. data++;
  579. --size;
  580. }
  581. DBG("Sending report type %d ID %d to handle 0x%X", report->type,
  582. report->id, report->value_handle);
  583. if (hog->attrib == NULL)
  584. return;
  585. if (report->properties & GATT_CHR_PROP_WRITE)
  586. write_char(hog, hog->attrib, report->value_handle,
  587. data, size, output_written_cb, hog);
  588. else if (report->properties & GATT_CHR_PROP_WRITE_WITHOUT_RESP)
  589. gatt_write_cmd(hog->attrib, report->value_handle,
  590. data, size, NULL, NULL);
  591. }
  592. static void set_report_cb(guint8 status, const guint8 *pdu,
  593. guint16 plen, gpointer user_data)
  594. {
  595. struct bt_hog *hog = user_data;
  596. struct uhid_event rsp;
  597. int err;
  598. hog->setrep_att = 0;
  599. memset(&rsp, 0, sizeof(rsp));
  600. rsp.type = UHID_SET_REPORT_REPLY;
  601. rsp.u.set_report_reply.id = hog->setrep_id;
  602. rsp.u.set_report_reply.err = status;
  603. if (status != 0)
  604. error("Error setting Report value: %s", att_ecode2str(status));
  605. err = bt_uhid_send(hog->uhid, &rsp);
  606. if (err < 0)
  607. error("bt_uhid_send: %s", strerror(-err));
  608. }
  609. static void set_report(struct uhid_event *ev, void *user_data)
  610. {
  611. struct bt_hog *hog = user_data;
  612. struct report *report;
  613. void *data;
  614. int size;
  615. int err;
  616. /* uhid never sends reqs in parallel; if there's a req, it timed out */
  617. if (hog->setrep_att) {
  618. g_attrib_cancel(hog->attrib, hog->setrep_att);
  619. hog->setrep_att = 0;
  620. }
  621. hog->setrep_id = ev->u.set_report.id;
  622. report = find_report_by_rtype(hog, ev->u.set_report.rtype,
  623. ev->u.set_report.rnum);
  624. if (!report) {
  625. err = ENOTSUP;
  626. goto fail;
  627. }
  628. data = ev->u.set_report.data;
  629. size = ev->u.set_report.size;
  630. if (hog->has_report_id && size > 0) {
  631. data++;
  632. --size;
  633. }
  634. DBG("Sending report type %d ID %d to handle 0x%X", report->type,
  635. report->id, report->value_handle);
  636. if (hog->attrib == NULL)
  637. return;
  638. hog->setrep_att = gatt_write_char(hog->attrib,
  639. report->value_handle,
  640. data, size, set_report_cb,
  641. hog);
  642. if (!hog->setrep_att) {
  643. err = ENOMEM;
  644. goto fail;
  645. }
  646. return;
  647. fail:
  648. /* cancel the request on failure */
  649. set_report_cb(err, NULL, 0, hog);
  650. }
  651. static void report_reply(struct bt_hog *hog, uint8_t status, uint8_t id,
  652. uint16_t len, const uint8_t *data)
  653. {
  654. struct uhid_event rsp;
  655. int err;
  656. hog->getrep_att = 0;
  657. memset(&rsp, 0, sizeof(rsp));
  658. rsp.type = UHID_GET_REPORT_REPLY;
  659. rsp.u.get_report_reply.id = hog->getrep_id;
  660. if (status)
  661. goto done;
  662. if (hog->has_report_id && len > 0) {
  663. rsp.u.get_report_reply.size = len + 1;
  664. rsp.u.get_report_reply.data[0] = id;
  665. memcpy(&rsp.u.get_report_reply.data[1], data, len);
  666. } else {
  667. rsp.u.get_report_reply.size = len;
  668. memcpy(rsp.u.get_report_reply.data, data, len);
  669. }
  670. done:
  671. rsp.u.get_report_reply.err = status;
  672. err = bt_uhid_send(hog->uhid, &rsp);
  673. if (err < 0)
  674. error("bt_uhid_send: %s", strerror(-err));
  675. }
  676. static void get_report_cb(guint8 status, const guint8 *pdu, guint16 len,
  677. gpointer user_data)
  678. {
  679. struct report *report = user_data;
  680. struct bt_hog *hog = report->hog;
  681. if (status != 0) {
  682. error("Error reading Report value: %s", att_ecode2str(status));
  683. goto exit;
  684. }
  685. if (len == 0) {
  686. error("Error reading Report, length %d", len);
  687. status = EIO;
  688. goto exit;
  689. }
  690. if (pdu[0] != 0x0b) {
  691. error("Error reading Report, invalid response: %02x", pdu[0]);
  692. status = EPROTO;
  693. goto exit;
  694. }
  695. --len;
  696. ++pdu;
  697. exit:
  698. report_reply(hog, status, report->id, len, pdu);
  699. }
  700. static void get_report(struct uhid_event *ev, void *user_data)
  701. {
  702. struct bt_hog *hog = user_data;
  703. struct report *report;
  704. guint8 err;
  705. /* uhid never sends reqs in parallel; if there's a req, it timed out */
  706. if (hog->getrep_att) {
  707. g_attrib_cancel(hog->attrib, hog->getrep_att);
  708. hog->getrep_att = 0;
  709. }
  710. hog->getrep_id = ev->u.get_report.id;
  711. report = find_report_by_rtype(hog, ev->u.get_report.rtype,
  712. ev->u.get_report.rnum);
  713. if (!report) {
  714. err = ENOTSUP;
  715. goto fail;
  716. }
  717. hog->getrep_att = gatt_read_char(hog->attrib,
  718. report->value_handle,
  719. get_report_cb, report);
  720. if (!hog->getrep_att) {
  721. err = ENOMEM;
  722. goto fail;
  723. }
  724. return;
  725. fail:
  726. /* reply with an error on failure */
  727. report_reply(hog, err, 0, 0, NULL);
  728. }
  729. static bool get_descriptor_item_info(uint8_t *buf, ssize_t blen, ssize_t *len,
  730. bool *is_long)
  731. {
  732. if (!blen)
  733. return false;
  734. *is_long = (buf[0] == 0xfe);
  735. if (*is_long) {
  736. if (blen < 3)
  737. return false;
  738. /*
  739. * long item:
  740. * byte 0 -> 0xFE
  741. * byte 1 -> data size
  742. * byte 2 -> tag
  743. * + data
  744. */
  745. *len = buf[1] + 3;
  746. } else {
  747. uint8_t b_size;
  748. /*
  749. * short item:
  750. * byte 0[1..0] -> data size (=0, 1, 2, 4)
  751. * byte 0[3..2] -> type
  752. * byte 0[7..4] -> tag
  753. * + data
  754. */
  755. b_size = buf[0] & 0x03;
  756. *len = (b_size ? 1 << (b_size - 1) : 0) + 1;
  757. }
  758. /* item length should be no more than input buffer length */
  759. return *len <= blen;
  760. }
  761. static char *item2string(char *str, uint8_t *buf, uint8_t len)
  762. {
  763. char *p = str;
  764. int i;
  765. /*
  766. * Since long item tags are not defined except for vendor ones, we
  767. * just ensure that short items are printed properly (up to 5 bytes).
  768. */
  769. for (i = 0; i < 6 && i < len; i++)
  770. p += sprintf(p, " %02x", buf[i]);
  771. /*
  772. * If there are some data left, just add continuation mark to indicate
  773. * this.
  774. */
  775. if (i < len)
  776. sprintf(p, " ...");
  777. return str;
  778. }
  779. static void uhid_create(struct bt_hog *hog, uint8_t *report_map,
  780. ssize_t report_map_len)
  781. {
  782. uint8_t *value = report_map;
  783. struct uhid_event ev;
  784. ssize_t vlen = report_map_len;
  785. char itemstr[20]; /* 5x3 (data) + 4 (continuation) + 1 (null) */
  786. int i, err;
  787. GError *gerr = NULL;
  788. DBG("Report MAP:");
  789. for (i = 0; i < vlen;) {
  790. ssize_t ilen = 0;
  791. bool long_item = false;
  792. if (get_descriptor_item_info(&value[i], vlen - i, &ilen,
  793. &long_item)) {
  794. /* Report ID is short item with prefix 100001xx */
  795. if (!long_item && (value[i] & 0xfc) == 0x84)
  796. hog->has_report_id = TRUE;
  797. DBG("\t%s", item2string(itemstr, &value[i], ilen));
  798. i += ilen;
  799. } else {
  800. error("Report Map parsing failed at %d", i);
  801. /* Just print remaining items at once and break */
  802. DBG("\t%s", item2string(itemstr, &value[i], vlen - i));
  803. break;
  804. }
  805. }
  806. /* create uHID device */
  807. memset(&ev, 0, sizeof(ev));
  808. ev.type = UHID_CREATE;
  809. bt_io_get(g_attrib_get_channel(hog->attrib), &gerr,
  810. BT_IO_OPT_SOURCE, ev.u.create.phys,
  811. BT_IO_OPT_DEST, ev.u.create.uniq,
  812. BT_IO_OPT_INVALID);
  813. /* Phys + uniq are the same size (hw address type) */
  814. for (i = 0;
  815. i < (int)sizeof(ev.u.create.phys) && ev.u.create.phys[i] != 0;
  816. ++i) {
  817. ev.u.create.phys[i] = tolower(ev.u.create.phys[i]);
  818. ev.u.create.uniq[i] = tolower(ev.u.create.uniq[i]);
  819. }
  820. if (gerr) {
  821. error("Failed to connection details: %s", gerr->message);
  822. g_error_free(gerr);
  823. return;
  824. }
  825. strncpy((char *) ev.u.create.name, hog->name,
  826. sizeof(ev.u.create.name) - 1);
  827. ev.u.create.vendor = hog->vendor;
  828. ev.u.create.product = hog->product;
  829. ev.u.create.version = hog->version;
  830. ev.u.create.country = hog->bcountrycode;
  831. ev.u.create.bus = BUS_BLUETOOTH;
  832. ev.u.create.rd_data = value;
  833. ev.u.create.rd_size = vlen;
  834. err = bt_uhid_send(hog->uhid, &ev);
  835. if (err < 0) {
  836. error("bt_uhid_send: %s", strerror(-err));
  837. return;
  838. }
  839. bt_uhid_register(hog->uhid, UHID_OUTPUT, forward_report, hog);
  840. bt_uhid_register(hog->uhid, UHID_GET_REPORT, get_report, hog);
  841. bt_uhid_register(hog->uhid, UHID_SET_REPORT, set_report, hog);
  842. hog->uhid_created = true;
  843. DBG("HoG created uHID device");
  844. }
  845. static void db_report_map_write_value_cb(struct gatt_db_attribute *attr,
  846. int err, void *user_data)
  847. {
  848. if (err)
  849. error("Error writing report map value to gatt db");
  850. }
  851. static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
  852. gpointer user_data)
  853. {
  854. struct gatt_request *req = user_data;
  855. struct bt_hog *hog = req->user_data;
  856. uint8_t value[HOG_REPORT_MAP_MAX_SIZE];
  857. ssize_t vlen;
  858. destroy_gatt_req(req);
  859. DBG("HoG inspecting report map");
  860. if (status != 0) {
  861. error("Report Map read failed: %s", att_ecode2str(status));
  862. return;
  863. }
  864. vlen = dec_read_resp(pdu, plen, value, sizeof(value));
  865. if (vlen < 0) {
  866. error("ATT protocol error");
  867. return;
  868. }
  869. uhid_create(hog, value, vlen);
  870. /* Cache the report map if gatt_db is available */
  871. if (hog->report_map_attr) {
  872. gatt_db_attribute_write(hog->report_map_attr, 0, value, vlen, 0,
  873. NULL, db_report_map_write_value_cb,
  874. NULL);
  875. }
  876. }
  877. static void info_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
  878. gpointer user_data)
  879. {
  880. struct gatt_request *req = user_data;
  881. struct bt_hog *hog = req->user_data;
  882. uint8_t value[HID_INFO_SIZE];
  883. ssize_t vlen;
  884. destroy_gatt_req(req);
  885. if (status != 0) {
  886. error("HID Information read failed: %s",
  887. att_ecode2str(status));
  888. return;
  889. }
  890. vlen = dec_read_resp(pdu, plen, value, sizeof(value));
  891. if (vlen != 4) {
  892. error("ATT protocol error");
  893. return;
  894. }
  895. hog->bcdhid = get_le16(&value[0]);
  896. hog->bcountrycode = value[2];
  897. hog->flags = value[3];
  898. DBG("bcdHID: 0x%04X bCountryCode: 0x%02X Flags: 0x%02X",
  899. hog->bcdhid, hog->bcountrycode, hog->flags);
  900. }
  901. static void proto_mode_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
  902. gpointer user_data)
  903. {
  904. struct gatt_request *req = user_data;
  905. struct bt_hog *hog = req->user_data;
  906. uint8_t value;
  907. ssize_t vlen;
  908. destroy_gatt_req(req);
  909. if (status != 0) {
  910. error("Protocol Mode characteristic read failed: %s",
  911. att_ecode2str(status));
  912. return;
  913. }
  914. vlen = dec_read_resp(pdu, plen, &value, sizeof(value));
  915. if (vlen < 0) {
  916. error("ATT protocol error");
  917. return;
  918. }
  919. if (value == HOG_PROTO_MODE_BOOT) {
  920. uint8_t nval = HOG_PROTO_MODE_REPORT;
  921. DBG("HoG is operating in Boot Procotol Mode");
  922. gatt_write_cmd(hog->attrib, hog->proto_mode_handle, &nval,
  923. sizeof(nval), NULL, NULL);
  924. } else if (value == HOG_PROTO_MODE_REPORT)
  925. DBG("HoG is operating in Report Protocol Mode");
  926. }
  927. static void char_discovered_cb(uint8_t status, GSList *chars, void *user_data)
  928. {
  929. struct gatt_request *req = user_data;
  930. struct bt_hog *hog = req->user_data;
  931. struct gatt_primary *primary = hog->primary;
  932. bt_uuid_t report_uuid, report_map_uuid, info_uuid;
  933. bt_uuid_t proto_mode_uuid, ctrlpt_uuid;
  934. struct report *report;
  935. GSList *l;
  936. uint16_t info_handle = 0, proto_mode_handle = 0;
  937. destroy_gatt_req(req);
  938. DBG("HoG inspecting characteristics");
  939. if (status != 0) {
  940. const char *str = att_ecode2str(status);
  941. DBG("Discover all characteristics failed: %s", str);
  942. return;
  943. }
  944. bt_uuid16_create(&report_uuid, HOG_REPORT_UUID);
  945. bt_uuid16_create(&report_map_uuid, HOG_REPORT_MAP_UUID);
  946. bt_uuid16_create(&info_uuid, HOG_INFO_UUID);
  947. bt_uuid16_create(&proto_mode_uuid, HOG_PROTO_MODE_UUID);
  948. bt_uuid16_create(&ctrlpt_uuid, HOG_CONTROL_POINT_UUID);
  949. for (l = chars; l; l = g_slist_next(l)) {
  950. struct gatt_char *chr, *next;
  951. bt_uuid_t uuid;
  952. uint16_t start, end;
  953. chr = l->data;
  954. next = l->next ? l->next->data : NULL;
  955. DBG("0x%04x UUID: %s properties: %02x",
  956. chr->handle, chr->uuid, chr->properties);
  957. bt_string_to_uuid(&uuid, chr->uuid);
  958. start = chr->value_handle + 1;
  959. end = (next ? next->handle - 1 : primary->range.end);
  960. if (bt_uuid_cmp(&uuid, &report_uuid) == 0) {
  961. report = report_new(hog, chr);
  962. discover_report(hog, hog->attrib, start, end, report);
  963. } else if (bt_uuid_cmp(&uuid, &report_map_uuid) == 0) {
  964. DBG("HoG discovering report map");
  965. read_char(hog, hog->attrib, chr->value_handle,
  966. report_map_read_cb, hog);
  967. discover_external(hog, hog->attrib, start, end, hog);
  968. } else if (bt_uuid_cmp(&uuid, &info_uuid) == 0)
  969. info_handle = chr->value_handle;
  970. else if (bt_uuid_cmp(&uuid, &proto_mode_uuid) == 0)
  971. proto_mode_handle = chr->value_handle;
  972. else if (bt_uuid_cmp(&uuid, &ctrlpt_uuid) == 0)
  973. hog->ctrlpt_handle = chr->value_handle;
  974. }
  975. if (proto_mode_handle) {
  976. hog->proto_mode_handle = proto_mode_handle;
  977. read_char(hog, hog->attrib, proto_mode_handle,
  978. proto_mode_read_cb, hog);
  979. }
  980. if (info_handle)
  981. read_char(hog, hog->attrib, info_handle, info_read_cb, hog);
  982. }
  983. static void report_free(void *data)
  984. {
  985. struct report *report = data;
  986. g_free(report->value);
  987. g_free(report);
  988. }
  989. static void cancel_gatt_req(struct gatt_request *req)
  990. {
  991. if (g_attrib_cancel(req->hog->attrib, req->id))
  992. destroy_gatt_req(req);
  993. }
  994. static void hog_free(void *data)
  995. {
  996. struct bt_hog *hog = data;
  997. bt_hog_detach(hog);
  998. queue_destroy(hog->bas, (void *) bt_bas_unref);
  999. g_slist_free_full(hog->instances, hog_free);
  1000. bt_scpp_unref(hog->scpp);
  1001. bt_dis_unref(hog->dis);
  1002. bt_uhid_unref(hog->uhid);
  1003. g_slist_free_full(hog->reports, report_free);
  1004. g_free(hog->name);
  1005. g_free(hog->primary);
  1006. queue_destroy(hog->gatt_op, (void *) destroy_gatt_req);
  1007. if (hog->gatt_db)
  1008. gatt_db_unref(hog->gatt_db);
  1009. g_free(hog);
  1010. }
  1011. struct bt_hog *bt_hog_new_default(const char *name, uint16_t vendor,
  1012. uint16_t product, uint16_t version,
  1013. struct gatt_db *db)
  1014. {
  1015. return bt_hog_new(-1, name, vendor, product, version, db);
  1016. }
  1017. static void foreach_hog_report(struct gatt_db_attribute *attr, void *user_data)
  1018. {
  1019. struct report *report = user_data;
  1020. struct bt_hog *hog = report->hog;
  1021. const bt_uuid_t *uuid;
  1022. bt_uuid_t ref_uuid, ccc_uuid;
  1023. uint16_t handle;
  1024. handle = gatt_db_attribute_get_handle(attr);
  1025. uuid = gatt_db_attribute_get_type(attr);
  1026. bt_uuid16_create(&ref_uuid, GATT_REPORT_REFERENCE);
  1027. if (!bt_uuid_cmp(&ref_uuid, uuid)) {
  1028. read_char(hog, hog->attrib, handle, report_reference_cb,
  1029. report);
  1030. return;
  1031. }
  1032. bt_uuid16_create(&ccc_uuid, GATT_CLIENT_CHARAC_CFG_UUID);
  1033. if (!bt_uuid_cmp(&ccc_uuid, uuid))
  1034. report->ccc_handle = handle;
  1035. }
  1036. static int report_attr_cmp(const void *data, const void *user_data)
  1037. {
  1038. const struct report *report = data;
  1039. const struct gatt_db_attribute *attr = user_data;
  1040. return report->handle - gatt_db_attribute_get_handle(attr);
  1041. }
  1042. static struct report *report_add(struct bt_hog *hog,
  1043. struct gatt_db_attribute *attr)
  1044. {
  1045. struct report *report;
  1046. GSList *l;
  1047. /* Skip if report already exists */
  1048. l = g_slist_find_custom(hog->reports, attr, report_attr_cmp);
  1049. if (l)
  1050. return l->data;
  1051. report = g_new0(struct report, 1);
  1052. report->hog = hog;
  1053. gatt_db_attribute_get_char_data(attr, &report->handle,
  1054. &report->value_handle,
  1055. &report->properties,
  1056. NULL, NULL);
  1057. hog->reports = g_slist_append(hog->reports, report);
  1058. read_char(hog, hog->attrib, report->value_handle, report_read_cb,
  1059. report);
  1060. return report;
  1061. }
  1062. static void foreach_hog_external(struct gatt_db_attribute *attr,
  1063. void *user_data)
  1064. {
  1065. struct bt_hog *hog = user_data;
  1066. const bt_uuid_t *uuid;
  1067. bt_uuid_t ext_uuid;
  1068. uint16_t handle;
  1069. handle = gatt_db_attribute_get_handle(attr);
  1070. uuid = gatt_db_attribute_get_type(attr);
  1071. bt_uuid16_create(&ext_uuid, GATT_EXTERNAL_REPORT_REFERENCE);
  1072. if (!bt_uuid_cmp(&ext_uuid, uuid))
  1073. read_char(hog, hog->attrib, handle,
  1074. external_report_reference_cb, hog);
  1075. }
  1076. static void db_report_map_read_value_cb(struct gatt_db_attribute *attrib,
  1077. int err, const uint8_t *value,
  1078. size_t length, void *user_data)
  1079. {
  1080. struct report_map *map = user_data;
  1081. if (err) {
  1082. error("Error reading report map from gatt db %s",
  1083. strerror(-err));
  1084. return;
  1085. }
  1086. if (!length)
  1087. return;
  1088. map->length = length < sizeof(map->value) ? length : sizeof(map->value);
  1089. memcpy(map->value, value, map->length);
  1090. }
  1091. static void foreach_hog_chrc(struct gatt_db_attribute *attr, void *user_data)
  1092. {
  1093. struct bt_hog *hog = user_data;
  1094. bt_uuid_t uuid, report_uuid, report_map_uuid, info_uuid;
  1095. bt_uuid_t proto_mode_uuid, ctrlpt_uuid;
  1096. uint16_t handle, value_handle;
  1097. struct report_map report_map = {0};
  1098. gatt_db_attribute_get_char_data(attr, &handle, &value_handle, NULL,
  1099. NULL, &uuid);
  1100. bt_uuid16_create(&report_uuid, HOG_REPORT_UUID);
  1101. if (!bt_uuid_cmp(&report_uuid, &uuid)) {
  1102. struct report *report = report_add(hog, attr);
  1103. gatt_db_service_foreach_desc(attr, foreach_hog_report, report);
  1104. return;
  1105. }
  1106. bt_uuid16_create(&report_map_uuid, HOG_REPORT_MAP_UUID);
  1107. if (!bt_uuid_cmp(&report_map_uuid, &uuid)) {
  1108. if (hog->gatt_db) {
  1109. /* Try to read the cache of report map if available */
  1110. hog->report_map_attr = gatt_db_get_attribute(
  1111. hog->gatt_db,
  1112. value_handle);
  1113. gatt_db_attribute_read(hog->report_map_attr, 0,
  1114. BT_ATT_OP_READ_REQ, NULL,
  1115. db_report_map_read_value_cb,
  1116. &report_map);
  1117. }
  1118. if (report_map.length) {
  1119. /* Report map found in the cache, straight to creating
  1120. * UHID to optimize reconnection.
  1121. */
  1122. uhid_create(hog, report_map.value, report_map.length);
  1123. } else {
  1124. read_char(hog, hog->attrib, value_handle,
  1125. report_map_read_cb, hog);
  1126. }
  1127. gatt_db_service_foreach_desc(attr, foreach_hog_external, hog);
  1128. return;
  1129. }
  1130. bt_uuid16_create(&info_uuid, HOG_INFO_UUID);
  1131. if (!bt_uuid_cmp(&info_uuid, &uuid)) {
  1132. read_char(hog, hog->attrib, value_handle, info_read_cb, hog);
  1133. return;
  1134. }
  1135. bt_uuid16_create(&proto_mode_uuid, HOG_PROTO_MODE_UUID);
  1136. if (!bt_uuid_cmp(&proto_mode_uuid, &uuid)) {
  1137. hog->proto_mode_handle = value_handle;
  1138. read_char(hog, hog->attrib, value_handle, proto_mode_read_cb,
  1139. hog);
  1140. }
  1141. bt_uuid16_create(&ctrlpt_uuid, HOG_CONTROL_POINT_UUID);
  1142. if (!bt_uuid_cmp(&ctrlpt_uuid, &uuid))
  1143. hog->ctrlpt_handle = value_handle;
  1144. }
  1145. static struct bt_hog *hog_new(int fd, const char *name, uint16_t vendor,
  1146. uint16_t product, uint16_t version,
  1147. struct gatt_db_attribute *attr)
  1148. {
  1149. struct bt_hog *hog;
  1150. hog = g_try_new0(struct bt_hog, 1);
  1151. if (!hog)
  1152. return NULL;
  1153. hog->gatt_op = queue_new();
  1154. hog->bas = queue_new();
  1155. if (fd < 0)
  1156. hog->uhid = bt_uhid_new_default();
  1157. else
  1158. hog->uhid = bt_uhid_new(fd);
  1159. hog->uhid_fd = fd;
  1160. if (!hog->gatt_op || !hog->bas || !hog->uhid) {
  1161. hog_free(hog);
  1162. return NULL;
  1163. }
  1164. hog->name = g_strdup(name);
  1165. hog->vendor = vendor;
  1166. hog->product = product;
  1167. hog->version = version;
  1168. hog->attr = attr;
  1169. return hog;
  1170. }
  1171. static void hog_attach_instance(struct bt_hog *hog,
  1172. struct gatt_db_attribute *attr)
  1173. {
  1174. struct bt_hog *instance;
  1175. if (!hog->attr) {
  1176. hog->attr = attr;
  1177. return;
  1178. }
  1179. instance = hog_new(hog->uhid_fd, hog->name, hog->vendor,
  1180. hog->product, hog->version, attr);
  1181. if (!instance)
  1182. return;
  1183. hog->instances = g_slist_append(hog->instances, bt_hog_ref(instance));
  1184. }
  1185. static void foreach_hog_service(struct gatt_db_attribute *attr, void *user_data)
  1186. {
  1187. struct bt_hog *hog = user_data;
  1188. hog_attach_instance(hog, attr);
  1189. }
  1190. static void dis_notify(uint8_t source, uint16_t vendor, uint16_t product,
  1191. uint16_t version, void *user_data)
  1192. {
  1193. struct bt_hog *hog = user_data;
  1194. GSList *l;
  1195. hog->vendor = vendor;
  1196. hog->product = product;
  1197. hog->version = version;
  1198. for (l = hog->instances; l; l = l->next) {
  1199. struct bt_hog *instance = l->data;
  1200. instance->vendor = vendor;
  1201. instance->product = product;
  1202. instance->version = version;
  1203. }
  1204. }
  1205. struct bt_hog *bt_hog_new(int fd, const char *name, uint16_t vendor,
  1206. uint16_t product, uint16_t version,
  1207. struct gatt_db *db)
  1208. {
  1209. struct bt_hog *hog;
  1210. hog = hog_new(fd, name, vendor, product, version, NULL);
  1211. if (!hog)
  1212. return NULL;
  1213. if (db) {
  1214. bt_uuid_t uuid;
  1215. /* Handle the HID services */
  1216. bt_uuid16_create(&uuid, HOG_UUID16);
  1217. gatt_db_foreach_service(db, &uuid, foreach_hog_service, hog);
  1218. if (!hog->attr) {
  1219. hog_free(hog);
  1220. return NULL;
  1221. }
  1222. /* Try creating a DIS instance in case pid/vid are not set */
  1223. if (!vendor && !product) {
  1224. hog->dis = bt_dis_new(db);
  1225. bt_dis_set_notification(hog->dis, dis_notify, hog);
  1226. }
  1227. hog->gatt_db = gatt_db_ref(db);
  1228. }
  1229. return bt_hog_ref(hog);
  1230. }
  1231. struct bt_hog *bt_hog_ref(struct bt_hog *hog)
  1232. {
  1233. if (!hog)
  1234. return NULL;
  1235. __sync_fetch_and_add(&hog->ref_count, 1);
  1236. return hog;
  1237. }
  1238. void bt_hog_unref(struct bt_hog *hog)
  1239. {
  1240. if (!hog)
  1241. return;
  1242. if (__sync_sub_and_fetch(&hog->ref_count, 1))
  1243. return;
  1244. hog_free(hog);
  1245. }
  1246. static void find_included_cb(uint8_t status, GSList *services, void *user_data)
  1247. {
  1248. struct gatt_request *req = user_data;
  1249. GSList *l;
  1250. DBG("");
  1251. destroy_gatt_req(req);
  1252. if (status) {
  1253. const char *str = att_ecode2str(status);
  1254. DBG("Find included failed: %s", str);
  1255. return;
  1256. }
  1257. for (l = services; l; l = l->next) {
  1258. struct gatt_included *include = l->data;
  1259. DBG("included: handle %x, uuid %s",
  1260. include->handle, include->uuid);
  1261. }
  1262. }
  1263. static void hog_attach_scpp(struct bt_hog *hog, struct gatt_primary *primary)
  1264. {
  1265. if (hog->scpp) {
  1266. bt_scpp_attach(hog->scpp, hog->attrib);
  1267. return;
  1268. }
  1269. hog->scpp = bt_scpp_new(primary);
  1270. if (hog->scpp)
  1271. bt_scpp_attach(hog->scpp, hog->attrib);
  1272. }
  1273. static void hog_attach_dis(struct bt_hog *hog, struct gatt_primary *primary)
  1274. {
  1275. if (hog->dis) {
  1276. bt_dis_attach(hog->dis, hog->attrib);
  1277. return;
  1278. }
  1279. hog->dis = bt_dis_new_primary(primary);
  1280. if (hog->dis) {
  1281. bt_dis_set_notification(hog->dis, dis_notify, hog);
  1282. bt_dis_attach(hog->dis, hog->attrib);
  1283. }
  1284. }
  1285. static void hog_attach_bas(struct bt_hog *hog, struct gatt_primary *primary)
  1286. {
  1287. struct bt_bas *instance;
  1288. instance = bt_bas_new(primary);
  1289. bt_bas_attach(instance, hog->attrib);
  1290. queue_push_head(hog->bas, instance);
  1291. }
  1292. static void hog_attach_hog(struct bt_hog *hog, struct gatt_primary *primary)
  1293. {
  1294. struct bt_hog *instance;
  1295. if (!hog->primary) {
  1296. hog->primary = g_memdup(primary, sizeof(*primary));
  1297. discover_char(hog, hog->attrib, primary->range.start,
  1298. primary->range.end, NULL,
  1299. char_discovered_cb, hog);
  1300. find_included(hog, hog->attrib, primary->range.start,
  1301. primary->range.end, find_included_cb, hog);
  1302. return;
  1303. }
  1304. instance = bt_hog_new(hog->uhid_fd, hog->name, hog->vendor,
  1305. hog->product, hog->version, NULL);
  1306. if (!instance)
  1307. return;
  1308. instance->primary = g_memdup(primary, sizeof(*primary));
  1309. find_included(instance, hog->attrib, primary->range.start,
  1310. primary->range.end, find_included_cb, instance);
  1311. bt_hog_attach(instance, hog->attrib);
  1312. hog->instances = g_slist_append(hog->instances, instance);
  1313. }
  1314. static void primary_cb(uint8_t status, GSList *services, void *user_data)
  1315. {
  1316. struct gatt_request *req = user_data;
  1317. struct bt_hog *hog = req->user_data;
  1318. struct gatt_primary *primary;
  1319. GSList *l;
  1320. DBG("");
  1321. destroy_gatt_req(req);
  1322. if (status) {
  1323. const char *str = att_ecode2str(status);
  1324. DBG("Discover primary failed: %s", str);
  1325. return;
  1326. }
  1327. if (!services) {
  1328. DBG("No primary service found");
  1329. return;
  1330. }
  1331. for (l = services; l; l = l->next) {
  1332. primary = l->data;
  1333. if (strcmp(primary->uuid, SCAN_PARAMETERS_UUID) == 0) {
  1334. hog_attach_scpp(hog, primary);
  1335. continue;
  1336. }
  1337. if (strcmp(primary->uuid, DEVICE_INFORMATION_UUID) == 0) {
  1338. hog_attach_dis(hog, primary);
  1339. continue;
  1340. }
  1341. if (strcmp(primary->uuid, BATTERY_UUID) == 0) {
  1342. hog_attach_bas(hog, primary);
  1343. continue;
  1344. }
  1345. if (strcmp(primary->uuid, HOG_UUID) == 0)
  1346. hog_attach_hog(hog, primary);
  1347. }
  1348. }
  1349. bool bt_hog_attach(struct bt_hog *hog, void *gatt)
  1350. {
  1351. GSList *l;
  1352. if (hog->attrib)
  1353. return false;
  1354. hog->attrib = g_attrib_ref(gatt);
  1355. if (!hog->attr && !hog->primary) {
  1356. discover_primary(hog, hog->attrib, NULL, primary_cb, hog);
  1357. return true;
  1358. }
  1359. if (hog->scpp)
  1360. bt_scpp_attach(hog->scpp, gatt);
  1361. if (hog->dis)
  1362. bt_dis_attach(hog->dis, gatt);
  1363. queue_foreach(hog->bas, (void *) bt_bas_attach, gatt);
  1364. for (l = hog->instances; l; l = l->next) {
  1365. struct bt_hog *instance = l->data;
  1366. bt_hog_attach(instance, gatt);
  1367. }
  1368. if (!hog->uhid_created) {
  1369. DBG("HoG discovering characteristics");
  1370. if (hog->attr)
  1371. gatt_db_service_foreach_char(hog->attr,
  1372. foreach_hog_chrc, hog);
  1373. else
  1374. discover_char(hog, hog->attrib,
  1375. hog->primary->range.start,
  1376. hog->primary->range.end, NULL,
  1377. char_discovered_cb, hog);
  1378. }
  1379. if (!hog->uhid_created)
  1380. return true;
  1381. /* If UHID is already created, set up the report value handlers to
  1382. * optimize reconnection.
  1383. */
  1384. for (l = hog->reports; l; l = l->next) {
  1385. struct report *r = l->data;
  1386. if (r->notifyid)
  1387. continue;
  1388. r->notifyid = g_attrib_register(hog->attrib,
  1389. ATT_OP_HANDLE_NOTIFY,
  1390. r->value_handle,
  1391. report_value_cb, r, NULL);
  1392. }
  1393. return true;
  1394. }
  1395. static void uhid_destroy(struct bt_hog *hog)
  1396. {
  1397. int err;
  1398. struct uhid_event ev;
  1399. if (!hog->uhid_created)
  1400. return;
  1401. bt_uhid_unregister_all(hog->uhid);
  1402. memset(&ev, 0, sizeof(ev));
  1403. ev.type = UHID_DESTROY;
  1404. err = bt_uhid_send(hog->uhid, &ev);
  1405. if (err < 0) {
  1406. error("bt_uhid_send: %s", strerror(-err));
  1407. return;
  1408. }
  1409. hog->uhid_created = false;
  1410. }
  1411. void bt_hog_detach(struct bt_hog *hog)
  1412. {
  1413. GSList *l;
  1414. if (!hog->attrib)
  1415. return;
  1416. queue_foreach(hog->bas, (void *) bt_bas_detach, NULL);
  1417. for (l = hog->instances; l; l = l->next) {
  1418. struct bt_hog *instance = l->data;
  1419. bt_hog_detach(instance);
  1420. }
  1421. for (l = hog->reports; l; l = l->next) {
  1422. struct report *r = l->data;
  1423. if (r->notifyid > 0) {
  1424. g_attrib_unregister(hog->attrib, r->notifyid);
  1425. r->notifyid = 0;
  1426. }
  1427. }
  1428. if (hog->scpp)
  1429. bt_scpp_detach(hog->scpp);
  1430. if (hog->dis)
  1431. bt_dis_detach(hog->dis);
  1432. queue_foreach(hog->gatt_op, (void *) cancel_gatt_req, NULL);
  1433. g_attrib_unref(hog->attrib);
  1434. hog->attrib = NULL;
  1435. uhid_destroy(hog);
  1436. }
  1437. int bt_hog_set_control_point(struct bt_hog *hog, bool suspend)
  1438. {
  1439. uint8_t value = suspend ? 0x00 : 0x01;
  1440. if (hog->attrib == NULL)
  1441. return -ENOTCONN;
  1442. if (hog->ctrlpt_handle == 0)
  1443. return -ENOTSUP;
  1444. gatt_write_cmd(hog->attrib, hog->ctrlpt_handle, &value,
  1445. sizeof(value), NULL, NULL);
  1446. return 0;
  1447. }
  1448. int bt_hog_send_report(struct bt_hog *hog, void *data, size_t size, int type)
  1449. {
  1450. struct report *report;
  1451. GSList *l;
  1452. if (!hog)
  1453. return -EINVAL;
  1454. if (!hog->attrib)
  1455. return -ENOTCONN;
  1456. report = find_report(hog, type, 0);
  1457. if (!report)
  1458. return -ENOTSUP;
  1459. DBG("hog: Write report, handle 0x%X", report->value_handle);
  1460. if (report->properties & GATT_CHR_PROP_WRITE)
  1461. write_char(hog, hog->attrib, report->value_handle,
  1462. data, size, output_written_cb, hog);
  1463. if (report->properties & GATT_CHR_PROP_WRITE_WITHOUT_RESP)
  1464. gatt_write_cmd(hog->attrib, report->value_handle,
  1465. data, size, NULL, NULL);
  1466. for (l = hog->instances; l; l = l->next) {
  1467. struct bt_hog *instance = l->data;
  1468. bt_hog_send_report(instance, data, size, type);
  1469. }
  1470. return 0;
  1471. }