if-hh.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <hardware/bluetooth.h>
  11. #include <hardware/bt_hh.h>
  12. #include "if-main.h"
  13. #include "pollhandler.h"
  14. #include "../hal-utils.h"
  15. const bthh_interface_t *if_hh = NULL;
  16. SINTMAP(bthh_protocol_mode_t, -1, "(unknown)")
  17. DELEMENT(BTHH_REPORT_MODE),
  18. DELEMENT(BTHH_BOOT_MODE),
  19. DELEMENT(BTHH_UNSUPPORTED_MODE),
  20. ENDMAP
  21. SINTMAP(bthh_report_type_t, -1, "(unknown)")
  22. DELEMENT(BTHH_INPUT_REPORT),
  23. DELEMENT(BTHH_OUTPUT_REPORT),
  24. DELEMENT(BTHH_FEATURE_REPORT),
  25. ENDMAP
  26. SINTMAP(bthh_connection_state_t, -1, "(unknown)")
  27. DELEMENT(BTHH_CONN_STATE_CONNECTED),
  28. DELEMENT(BTHH_CONN_STATE_CONNECTING),
  29. DELEMENT(BTHH_CONN_STATE_DISCONNECTED),
  30. DELEMENT(BTHH_CONN_STATE_DISCONNECTING),
  31. DELEMENT(BTHH_CONN_STATE_FAILED_MOUSE_FROM_HOST),
  32. DELEMENT(BTHH_CONN_STATE_FAILED_KBD_FROM_HOST),
  33. DELEMENT(BTHH_CONN_STATE_FAILED_TOO_MANY_DEVICES),
  34. DELEMENT(BTHH_CONN_STATE_FAILED_NO_BTHID_DRIVER),
  35. DELEMENT(BTHH_CONN_STATE_FAILED_GENERIC),
  36. DELEMENT(BTHH_CONN_STATE_UNKNOWN),
  37. ENDMAP
  38. SINTMAP(bthh_status_t, -1, "(unknown)")
  39. DELEMENT(BTHH_OK),
  40. DELEMENT(BTHH_HS_HID_NOT_READY),
  41. DELEMENT(BTHH_HS_INVALID_RPT_ID),
  42. DELEMENT(BTHH_HS_TRANS_NOT_SPT),
  43. DELEMENT(BTHH_HS_INVALID_PARAM),
  44. DELEMENT(BTHH_HS_ERROR),
  45. DELEMENT(BTHH_ERR),
  46. DELEMENT(BTHH_ERR_SDP),
  47. DELEMENT(BTHH_ERR_PROTO),
  48. DELEMENT(BTHH_ERR_DB_FULL),
  49. DELEMENT(BTHH_ERR_TOD_UNSPT),
  50. DELEMENT(BTHH_ERR_NO_RES),
  51. DELEMENT(BTHH_ERR_AUTH_FAILED),
  52. DELEMENT(BTHH_ERR_HDL),
  53. ENDMAP
  54. static char connected_device_addr[MAX_ADDR_STR_LEN];
  55. /*
  56. * Callback for connection state change.
  57. * state will have one of the values from bthh_connection_state_t
  58. */
  59. static void connection_state_cb(bt_bdaddr_t *bd_addr,
  60. bthh_connection_state_t state)
  61. {
  62. char addr[MAX_ADDR_STR_LEN];
  63. haltest_info("%s: bd_addr=%s connection_state=%s\n", __func__,
  64. bt_bdaddr_t2str(bd_addr, addr),
  65. bthh_connection_state_t2str(state));
  66. if (state == BTHH_CONN_STATE_CONNECTED)
  67. strcpy(connected_device_addr, addr);
  68. }
  69. /*
  70. * Callback for virtual unplug api.
  71. * the status of the virtual unplug
  72. */
  73. static void virtual_unplug_cb(bt_bdaddr_t *bd_addr, bthh_status_t hh_status)
  74. {
  75. char addr[MAX_ADDR_STR_LEN];
  76. haltest_info("%s: bd_addr=%s hh_status=%s\n", __func__,
  77. bt_bdaddr_t2str(bd_addr, addr),
  78. bthh_status_t2str(hh_status));
  79. }
  80. /* Callback for Android 5.0 handshake api. */
  81. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  82. static void handshake_cb(bt_bdaddr_t *bd_addr, bthh_status_t hh_status)
  83. {
  84. char addr[MAX_ADDR_STR_LEN];
  85. haltest_info("%s: bd_addr=%s hh_status=%s\n", __func__,
  86. bt_bdaddr_t2str(bd_addr, addr),
  87. bthh_status_t2str(hh_status));
  88. }
  89. #endif
  90. /*
  91. * Callback for get hid info
  92. * hid_info will contain attr_mask, sub_class, app_id, vendor_id, product_id,
  93. * version, ctry_code, len
  94. */
  95. static void hid_info_cb(bt_bdaddr_t *bd_addr, bthh_hid_info_t hid_info)
  96. {
  97. char addr[MAX_ADDR_STR_LEN];
  98. /* TODO: bluedroid does not seem to ever call this callback */
  99. haltest_info("%s: bd_addr=%s\n", __func__,
  100. bt_bdaddr_t2str(bd_addr, addr));
  101. }
  102. /*
  103. * Callback for get/set protocol api.
  104. * the protocol mode is one of the value from bthh_protocol_mode_t
  105. */
  106. static void protocol_mode_cb(bt_bdaddr_t *bd_addr, bthh_status_t hh_status,
  107. bthh_protocol_mode_t mode)
  108. {
  109. char addr[MAX_ADDR_STR_LEN];
  110. haltest_info("%s: bd_addr=%s hh_status=%s mode=%s\n", __func__,
  111. bt_bdaddr_t2str(bd_addr, addr),
  112. bthh_status_t2str(hh_status),
  113. bthh_protocol_mode_t2str(mode));
  114. }
  115. /* Callback for get/set_idle_time api. */
  116. static void idle_time_cb(bt_bdaddr_t *bd_addr, bthh_status_t hh_status,
  117. int idle_rate)
  118. {
  119. char addr[MAX_ADDR_STR_LEN];
  120. haltest_info("%s: bd_addr=%s hh_status=%s idle_rate=%d\n", __func__,
  121. bt_bdaddr_t2str(bd_addr, addr),
  122. bthh_status_t2str(hh_status), idle_rate);
  123. }
  124. /*
  125. * Callback for get report api.
  126. * if status is ok rpt_data contains the report data
  127. */
  128. static void get_report_cb(bt_bdaddr_t *bd_addr, bthh_status_t hh_status,
  129. uint8_t *rpt_data, int rpt_size)
  130. {
  131. char addr[MAX_ADDR_STR_LEN];
  132. /* TODO: print actual report */
  133. haltest_info("%s: bd_addr=%s hh_status=%s rpt_size=%d\n", __func__,
  134. bt_bdaddr_t2str(bd_addr, addr),
  135. bthh_status_t2str(hh_status), rpt_size);
  136. }
  137. static bthh_callbacks_t bthh_callbacks = {
  138. .size = sizeof(bthh_callbacks),
  139. .connection_state_cb = connection_state_cb,
  140. .hid_info_cb = hid_info_cb,
  141. .protocol_mode_cb = protocol_mode_cb,
  142. .idle_time_cb = idle_time_cb,
  143. .get_report_cb = get_report_cb,
  144. .virtual_unplug_cb = virtual_unplug_cb,
  145. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  146. .handshake_cb = handshake_cb
  147. #endif
  148. };
  149. /* init */
  150. static void init_p(int argc, const char **argv)
  151. {
  152. RETURN_IF_NULL(if_hh);
  153. EXEC(if_hh->init, &bthh_callbacks);
  154. }
  155. /* connect */
  156. static void connect_c(int argc, const char **argv, enum_func *enum_func,
  157. void **user)
  158. {
  159. if (argc == 3) {
  160. *user = (void *) connected_device_addr;
  161. *enum_func = enum_one_string;
  162. }
  163. }
  164. static void connect_p(int argc, const char **argv)
  165. {
  166. bt_bdaddr_t addr;
  167. RETURN_IF_NULL(if_hh);
  168. VERIFY_ADDR_ARG(2, &addr);
  169. EXEC(if_hh->connect, &addr);
  170. }
  171. /* disconnect */
  172. /* Same completion as connect_c */
  173. #define disconnect_c connect_c
  174. static void disconnect_p(int argc, const char **argv)
  175. {
  176. bt_bdaddr_t addr;
  177. RETURN_IF_NULL(if_hh);
  178. VERIFY_ADDR_ARG(2, &addr);
  179. EXEC(if_hh->disconnect, &addr);
  180. }
  181. /* virtual_unplug */
  182. /* Same completion as connect_c */
  183. #define virtual_unplug_c connect_c
  184. static void virtual_unplug_p(int argc, const char **argv)
  185. {
  186. bt_bdaddr_t addr;
  187. RETURN_IF_NULL(if_hh);
  188. VERIFY_ADDR_ARG(2, &addr);
  189. EXEC(if_hh->virtual_unplug, &addr);
  190. }
  191. /* set_info */
  192. /* Same completion as connect_c */
  193. #define set_info_c connect_c
  194. static void set_info_p(int argc, const char **argv)
  195. {
  196. bt_bdaddr_t addr;
  197. bthh_hid_info_t hid_info;
  198. RETURN_IF_NULL(if_hh);
  199. VERIFY_ADDR_ARG(2, &addr);
  200. memset(&hid_info, 0, sizeof(hid_info));
  201. /*
  202. * This command is intentionally not supported. See comment from
  203. * bt_hid_info() in android/hidhost.c
  204. */
  205. EXEC(if_hh->set_info, &addr, hid_info);
  206. }
  207. /* get_protocol */
  208. static void get_protocol_c(int argc, const char **argv, enum_func *enum_func,
  209. void **user)
  210. {
  211. if (argc == 3) {
  212. *user = connected_device_addr;
  213. *enum_func = enum_one_string;
  214. } else if (argc == 4) {
  215. *user = TYPE_ENUM(bthh_protocol_mode_t);
  216. *enum_func = enum_defines;
  217. }
  218. }
  219. static void get_protocol_p(int argc, const char **argv)
  220. {
  221. bt_bdaddr_t addr;
  222. bthh_protocol_mode_t protocolMode;
  223. RETURN_IF_NULL(if_hh);
  224. VERIFY_ADDR_ARG(2, &addr);
  225. if (argc < 4) {
  226. haltest_error("No protocol mode specified\n");
  227. return;
  228. }
  229. protocolMode = str2bthh_protocol_mode_t(argv[3]);
  230. EXEC(if_hh->get_protocol, &addr, protocolMode);
  231. }
  232. /* set_protocol */
  233. /* Same completion as get_protocol_c */
  234. #define set_protocol_c get_protocol_c
  235. static void set_protocol_p(int argc, const char **argv)
  236. {
  237. bt_bdaddr_t addr;
  238. bthh_protocol_mode_t protocolMode;
  239. RETURN_IF_NULL(if_hh);
  240. VERIFY_ADDR_ARG(2, &addr);
  241. if (argc < 4) {
  242. haltest_error("No protocol mode specified\n");
  243. return;
  244. }
  245. protocolMode = str2bthh_protocol_mode_t(argv[3]);
  246. EXEC(if_hh->set_protocol, &addr, protocolMode);
  247. }
  248. /* get_report */
  249. static void get_report_c(int argc, const char **argv, enum_func *enum_func,
  250. void **user)
  251. {
  252. if (argc == 3) {
  253. *user = connected_device_addr;
  254. *enum_func = enum_one_string;
  255. } else if (argc == 4) {
  256. *user = TYPE_ENUM(bthh_report_type_t);
  257. *enum_func = enum_defines;
  258. }
  259. }
  260. static void get_report_p(int argc, const char **argv)
  261. {
  262. bt_bdaddr_t addr;
  263. bthh_report_type_t reportType;
  264. uint8_t reportId;
  265. int bufferSize;
  266. RETURN_IF_NULL(if_hh);
  267. VERIFY_ADDR_ARG(2, &addr);
  268. if (argc < 4) {
  269. haltest_error("No report type specified\n");
  270. return;
  271. }
  272. reportType = str2bthh_report_type_t(argv[3]);
  273. if (argc < 5) {
  274. haltest_error("No reportId specified\n");
  275. return;
  276. }
  277. reportId = (uint8_t) atoi(argv[4]);
  278. if (argc < 6) {
  279. haltest_error("No bufferSize specified\n");
  280. return;
  281. }
  282. bufferSize = atoi(argv[5]);
  283. EXEC(if_hh->get_report, &addr, reportType, reportId, bufferSize);
  284. }
  285. /* set_report */
  286. static void set_report_c(int argc, const char **argv, enum_func *enum_func,
  287. void **user)
  288. {
  289. if (argc == 3) {
  290. *user = connected_device_addr;
  291. *enum_func = enum_one_string;
  292. } else if (argc == 4) {
  293. *user = TYPE_ENUM(bthh_report_type_t);
  294. *enum_func = enum_defines;
  295. }
  296. }
  297. static void set_report_p(int argc, const char **argv)
  298. {
  299. bt_bdaddr_t addr;
  300. bthh_report_type_t reportType;
  301. RETURN_IF_NULL(if_hh);
  302. VERIFY_ADDR_ARG(2, &addr);
  303. if (argc <= 3) {
  304. haltest_error("No report type specified\n");
  305. return;
  306. }
  307. reportType = str2bthh_report_type_t(argv[3]);
  308. if (argc <= 4) {
  309. haltest_error("No report specified\n");
  310. return;
  311. }
  312. EXEC(if_hh->set_report, &addr, reportType, (char *) argv[4]);
  313. }
  314. /* send_data */
  315. static void send_data_c(int argc, const char **argv, enum_func *enum_func,
  316. void **user)
  317. {
  318. if (argc == 3) {
  319. *user = connected_device_addr;
  320. *enum_func = enum_one_string;
  321. }
  322. }
  323. static void send_data_p(int argc, const char **argv)
  324. {
  325. bt_bdaddr_t addr;
  326. RETURN_IF_NULL(if_hh);
  327. VERIFY_ADDR_ARG(2, &addr);
  328. if (argc <= 3) {
  329. haltest_error("No data to send specified\n");
  330. return;
  331. }
  332. EXEC(if_hh->send_data, &addr, (char *) argv[3]);
  333. }
  334. /* cleanup */
  335. static void cleanup_p(int argc, const char **argv)
  336. {
  337. RETURN_IF_NULL(if_hh);
  338. EXECV(if_hh->cleanup);
  339. }
  340. /* Methods available in bthh_interface_t */
  341. static struct method methods[] = {
  342. STD_METHOD(init),
  343. STD_METHODCH(connect, "<addr>"),
  344. STD_METHODCH(disconnect, "<addr>"),
  345. STD_METHODCH(virtual_unplug, "<addr>"),
  346. STD_METHODCH(set_info, "<addr>"),
  347. STD_METHODCH(get_protocol, "<addr> <mode>"),
  348. STD_METHODCH(set_protocol, "<addr> <mode>"),
  349. STD_METHODCH(get_report, "<addr> <type> <report_id> <size>"),
  350. STD_METHODCH(set_report, "<addr> <type> <hex_encoded_report>"),
  351. STD_METHODCH(send_data, "<addr> <hex_encoded_data>"),
  352. STD_METHOD(cleanup),
  353. END_METHOD
  354. };
  355. const struct interface hh_if = {
  356. .name = "hidhost",
  357. .methods = methods
  358. };