hid_host_demo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (C) 2017 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "hid_host_demo.c"
  38. /*
  39. * hid_host_demo.c
  40. */
  41. /* EXAMPLE_START(hid_host_demo): HID Host Demo
  42. *
  43. * @text This example implements an HID Host. For now, it connnects to a fixed device, queries the HID SDP
  44. * record and opens the HID Control + Interrupt channels
  45. */
  46. #include <inttypes.h>
  47. #include <stdio.h>
  48. #include "btstack_config.h"
  49. #include "btstack.h"
  50. #define MAX_ATTRIBUTE_VALUE_SIZE 300
  51. // SDP
  52. static uint8_t hid_descriptor[MAX_ATTRIBUTE_VALUE_SIZE];
  53. static uint16_t hid_descriptor_len;
  54. static uint16_t hid_control_psm;
  55. static uint16_t hid_interrupt_psm;
  56. static uint8_t attribute_value[MAX_ATTRIBUTE_VALUE_SIZE];
  57. static const unsigned int attribute_value_buffer_size = MAX_ATTRIBUTE_VALUE_SIZE;
  58. // L2CAP
  59. static uint16_t l2cap_hid_control_cid;
  60. static uint16_t l2cap_hid_interrupt_cid;
  61. // MBP 2016
  62. static const char * remote_addr_string = "F4-0F-24-3B-1B-E1";
  63. // iMpulse static const char * remote_addr_string = "64:6E:6C:C1:AA:B5";
  64. static bd_addr_t remote_addr;
  65. static btstack_packet_callback_registration_t hci_event_callback_registration;
  66. // Simplified US Keyboard with Shift modifier
  67. #define CHAR_ILLEGAL 0xff
  68. #define CHAR_RETURN '\n'
  69. #define CHAR_ESCAPE 27
  70. #define CHAR_TAB '\t'
  71. #define CHAR_BACKSPACE 0x7f
  72. /**
  73. * English (US)
  74. */
  75. static const uint8_t keytable_us_none [] = {
  76. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */
  77. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */
  78. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */
  79. 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */
  80. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */
  81. CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */
  82. '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */
  83. '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */
  84. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */
  85. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */
  86. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */
  87. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */
  88. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */
  89. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */
  90. '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
  91. '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */
  92. };
  93. static const uint8_t keytable_us_shift[] = {
  94. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */
  95. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */
  96. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */
  97. 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */
  98. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */
  99. CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */
  100. '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */
  101. '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */
  102. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */
  103. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */
  104. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */
  105. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */
  106. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */
  107. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */
  108. '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
  109. '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */
  110. };
  111. /* @section Main application configuration
  112. *
  113. * @text In the application configuration, L2CAP is initialized
  114. */
  115. /* LISTING_START(PanuSetup): Panu setup */
  116. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  117. static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  118. static void hid_host_setup(void){
  119. // Initialize L2CAP
  120. l2cap_init();
  121. // register L2CAP Services for reconnections
  122. l2cap_register_service(packet_handler, PSM_HID_INTERRUPT, 0xffff, gap_get_security_level());
  123. l2cap_register_service(packet_handler, PSM_HID_CONTROL, 0xffff, gap_get_security_level());
  124. // Allow sniff mode requests by HID device
  125. gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE);
  126. // register for HCI events
  127. hci_event_callback_registration.callback = &packet_handler;
  128. hci_add_event_handler(&hci_event_callback_registration);
  129. // Allow sniff mode requests by HID device
  130. gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE);
  131. // Disable stdout buffering
  132. setbuf(stdout, NULL);
  133. }
  134. /* LISTING_END */
  135. /* @section SDP parser callback
  136. *
  137. * @text The SDP parsers retrieves the BNEP PAN UUID as explained in
  138. * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}.
  139. */
  140. static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
  141. UNUSED(packet_type);
  142. UNUSED(channel);
  143. UNUSED(size);
  144. des_iterator_t attribute_list_it;
  145. des_iterator_t additional_des_it;
  146. des_iterator_t prot_it;
  147. uint8_t *des_element;
  148. uint8_t *element;
  149. uint32_t uuid;
  150. uint8_t status;
  151. switch (hci_event_packet_get_type(packet)){
  152. case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
  153. if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
  154. attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
  155. if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
  156. switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
  157. case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
  158. for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) {
  159. if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue;
  160. des_element = des_iterator_get_element(&attribute_list_it);
  161. des_iterator_init(&prot_it, des_element);
  162. element = des_iterator_get_element(&prot_it);
  163. if (!element) continue;
  164. if (de_get_element_type(element) != DE_UUID) continue;
  165. uuid = de_get_uuid32(element);
  166. des_iterator_next(&prot_it);
  167. switch (uuid){
  168. case BLUETOOTH_PROTOCOL_L2CAP:
  169. if (!des_iterator_has_more(&prot_it)) continue;
  170. de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_control_psm);
  171. printf("HID Control PSM: 0x%04x\n", (int) hid_control_psm);
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. break;
  178. case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
  179. for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) {
  180. if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue;
  181. des_element = des_iterator_get_element(&attribute_list_it);
  182. for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) {
  183. if (des_iterator_get_type(&additional_des_it) != DE_DES) continue;
  184. des_element = des_iterator_get_element(&additional_des_it);
  185. des_iterator_init(&prot_it, des_element);
  186. element = des_iterator_get_element(&prot_it);
  187. if (!element) continue;
  188. if (de_get_element_type(element) != DE_UUID) continue;
  189. uuid = de_get_uuid32(element);
  190. des_iterator_next(&prot_it);
  191. switch (uuid){
  192. case BLUETOOTH_PROTOCOL_L2CAP:
  193. if (!des_iterator_has_more(&prot_it)) continue;
  194. de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_interrupt_psm);
  195. printf("HID Interrupt PSM: 0x%04x\n", (int) hid_interrupt_psm);
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. }
  202. break;
  203. case BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST:
  204. for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) {
  205. if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue;
  206. des_element = des_iterator_get_element(&attribute_list_it);
  207. for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) {
  208. if (des_iterator_get_type(&additional_des_it) != DE_STRING) continue;
  209. element = des_iterator_get_element(&additional_des_it);
  210. const uint8_t * descriptor = de_get_string(element);
  211. hid_descriptor_len = de_get_data_size(element);
  212. memcpy(hid_descriptor, descriptor, hid_descriptor_len);
  213. printf("HID Descriptor:\n");
  214. printf_hexdump(hid_descriptor, hid_descriptor_len);
  215. }
  216. }
  217. break;
  218. default:
  219. break;
  220. }
  221. }
  222. } else {
  223. fprintf(stderr, "SDP attribute value buffer size exceeded: available %d, required %d\n", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet));
  224. }
  225. break;
  226. case SDP_EVENT_QUERY_COMPLETE:
  227. if (sdp_event_query_complete_get_status(packet) != ERROR_CODE_SUCCESS){
  228. printf("SDP Query failed\n");
  229. break;
  230. }
  231. if ((l2cap_hid_interrupt_cid != 0) && (l2cap_hid_control_cid != 0)){
  232. printf("HID device re-connected\n");
  233. break;
  234. }
  235. if (!hid_control_psm) {
  236. hid_control_psm = BLUETOOTH_PSM_HID_CONTROL;
  237. printf("HID Control PSM missing, using default 0x%04x\n", hid_control_psm);
  238. }
  239. if (!hid_interrupt_psm) {
  240. hid_interrupt_psm = BLUETOOTH_PSM_HID_INTERRUPT;
  241. printf("HID Interrupt PSM missing, using default 0x%04x\n", hid_interrupt_psm);
  242. break;
  243. }
  244. printf("Setup HID\n");
  245. status = l2cap_create_channel(packet_handler, remote_addr, hid_control_psm, 48, &l2cap_hid_control_cid);
  246. if (status){
  247. printf("Connecting to HID Control failed: 0x%02x\n", status);
  248. }
  249. break;
  250. }
  251. }
  252. /*
  253. * @section HID Report Handler
  254. *
  255. * @text Use BTstack's compact HID Parser to process incoming HID Report
  256. * Iterate over all fields and process fields with usage page = 0x07 / Keyboard
  257. * Check if SHIFT is down and process first character (don't handle multiple key presses)
  258. *
  259. */
  260. #define NUM_KEYS 6
  261. static uint8_t last_keys[NUM_KEYS];
  262. static void hid_host_handle_interrupt_report(const uint8_t * report, uint16_t report_len){
  263. // check if HID Input Report
  264. if (report_len < 1) return;
  265. if (*report != 0xa1) return;
  266. report++;
  267. report_len--;
  268. btstack_hid_parser_t parser;
  269. btstack_hid_parser_init(&parser, hid_descriptor, hid_descriptor_len, HID_REPORT_TYPE_INPUT, report, report_len);
  270. int shift = 0;
  271. uint8_t new_keys[NUM_KEYS];
  272. memset(new_keys, 0, sizeof(new_keys));
  273. int new_keys_count = 0;
  274. while (btstack_hid_parser_has_more(&parser)){
  275. uint16_t usage_page;
  276. uint16_t usage;
  277. int32_t value;
  278. btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value);
  279. if (usage_page != 0x07) continue;
  280. switch (usage){
  281. case 0xe1:
  282. case 0xe6:
  283. if (value){
  284. shift = 1;
  285. }
  286. continue;
  287. case 0x00:
  288. continue;
  289. default:
  290. break;
  291. }
  292. if (usage >= sizeof(keytable_us_none)) continue;
  293. // store new keys
  294. new_keys[new_keys_count++] = usage;
  295. // check if usage was used last time (and ignore in that case)
  296. int i;
  297. for (i=0;i<NUM_KEYS;i++){
  298. if (usage == last_keys[i]){
  299. usage = 0;
  300. }
  301. }
  302. if (usage == 0) continue;
  303. uint8_t key;
  304. if (shift){
  305. key = keytable_us_shift[usage];
  306. } else {
  307. key = keytable_us_none[usage];
  308. }
  309. if (key == CHAR_ILLEGAL) continue;
  310. if (key == CHAR_BACKSPACE){
  311. printf("\b \b"); // go back one char, print space, go back one char again
  312. continue;
  313. }
  314. printf("%c", key);
  315. }
  316. memcpy(last_keys, new_keys, NUM_KEYS);
  317. }
  318. /*
  319. * @section Packet Handler
  320. *
  321. * @text The packet handler responds to various HCI Events.
  322. */
  323. /* LISTING_START(packetHandler): Packet Handler */
  324. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
  325. {
  326. /* LISTING_PAUSE */
  327. uint8_t event;
  328. bd_addr_t event_addr;
  329. uint8_t status;
  330. uint16_t l2cap_cid;
  331. /* LISTING_RESUME */
  332. switch (packet_type) {
  333. case HCI_EVENT_PACKET:
  334. event = hci_event_packet_get_type(packet);
  335. switch (event) {
  336. /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
  337. * is received and the example is started in client mode, the remote SDP HID query is started.
  338. */
  339. case BTSTACK_EVENT_STATE:
  340. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  341. printf("Start SDP HID query for remote HID Device %s.\n", bd_addr_to_str(remote_addr));
  342. sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE);
  343. }
  344. break;
  345. /* LISTING_PAUSE */
  346. case HCI_EVENT_PIN_CODE_REQUEST:
  347. // inform about pin code request
  348. printf("Pin code request - using '0000'\n");
  349. hci_event_pin_code_request_get_bd_addr(packet, event_addr);
  350. gap_pin_code_response(event_addr, "0000");
  351. break;
  352. case HCI_EVENT_USER_CONFIRMATION_REQUEST:
  353. // inform about user confirmation request
  354. printf("SSP User Confirmation Request with numeric value '%"PRIu32"'\n", little_endian_read_32(packet, 8));
  355. printf("SSP User Confirmation Auto accept\n");
  356. break;
  357. /* LISTING_RESUME */
  358. case L2CAP_EVENT_INCOMING_CONNECTION:
  359. l2cap_cid = l2cap_event_incoming_connection_get_local_cid(packet);
  360. switch (l2cap_event_incoming_connection_get_psm(packet)){
  361. case PSM_HID_CONTROL:
  362. case PSM_HID_INTERRUPT:
  363. l2cap_accept_connection(l2cap_cid);
  364. break;
  365. default:
  366. l2cap_decline_connection(l2cap_cid);
  367. break;
  368. }
  369. break;
  370. case L2CAP_EVENT_CHANNEL_OPENED:
  371. status = packet[2];
  372. if (status){
  373. printf("L2CAP Connection failed: 0x%02x\n", status);
  374. break;
  375. }
  376. l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
  377. switch (l2cap_event_channel_opened_get_psm(packet)){
  378. case PSM_HID_CONTROL:
  379. if (l2cap_event_channel_opened_get_incoming(packet) == 0) {
  380. status = l2cap_create_channel(packet_handler, remote_addr, hid_interrupt_psm, 48, &l2cap_hid_interrupt_cid);
  381. if (status){
  382. printf("Connecting to HID Interrupt failed: 0x%02x\n", status);
  383. break;
  384. }
  385. }
  386. l2cap_hid_control_cid = l2cap_cid;
  387. break;
  388. case PSM_HID_INTERRUPT:
  389. l2cap_hid_interrupt_cid = l2cap_cid;
  390. break;
  391. default:
  392. break;
  393. }
  394. if ((l2cap_hid_control_cid != 0) && (l2cap_hid_interrupt_cid != 0)){
  395. if (hid_descriptor_len == 0){
  396. printf("Start SDP HID query to get HID Descriptor\n");
  397. sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE);
  398. } else {
  399. printf("HID Connection established\n");
  400. }
  401. }
  402. break;
  403. case L2CAP_EVENT_CHANNEL_CLOSED:
  404. if ((l2cap_hid_control_cid != 0) && (l2cap_hid_interrupt_cid != 0)){
  405. printf("HID Connection closed\n");
  406. hid_descriptor_len = 0;
  407. }
  408. l2cap_cid = l2cap_event_channel_closed_get_local_cid(packet);
  409. if (l2cap_cid == l2cap_hid_control_cid){
  410. l2cap_hid_control_cid = 0;
  411. }
  412. if (l2cap_cid == l2cap_hid_interrupt_cid){
  413. l2cap_hid_interrupt_cid = 0;
  414. }
  415. default:
  416. break;
  417. }
  418. break;
  419. case L2CAP_DATA_PACKET:
  420. // for now, just dump incoming data
  421. if (channel == l2cap_hid_interrupt_cid){
  422. hid_host_handle_interrupt_report(packet, size);
  423. } else if (channel == l2cap_hid_control_cid){
  424. printf("HID Control: ");
  425. printf_hexdump(packet, size);
  426. } else {
  427. break;
  428. }
  429. default:
  430. break;
  431. }
  432. }
  433. /* LISTING_END */
  434. int btstack_main(int argc, const char * argv[]);
  435. int btstack_main(int argc, const char * argv[]){
  436. (void)argc;
  437. (void)argv;
  438. hid_host_setup();
  439. // parse human readable Bluetooth address
  440. sscanf_bd_addr(remote_addr_string, remote_addr);
  441. // Turn on the device
  442. hci_power_control(HCI_POWER_ON);
  443. return 0;
  444. }
  445. /* EXAMPLE_END */