hid_keyboard_demo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (C) 2014 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_keyboard_demo.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(hid_keyboard_demo): HID Keyboard (Server) Demo
  40. *
  41. * @text This HID Device example demonstrates how to implement
  42. * an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent
  43. * If HAVE_BTSTACK_STDIN is defined, you can type from the terminal
  44. *
  45. * @text Status: Basic implementation. HID Request from Host are not answered yet. Works with iOS.
  46. */
  47. // *****************************************************************************
  48. #include <stdint.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <inttypes.h>
  53. #include "btstack.h"
  54. #ifdef HAVE_BTSTACK_STDIN
  55. #include "btstack_stdin.h"
  56. #endif
  57. // to enable demo text on POSIX systems
  58. // #undef HAVE_BTSTACK_STDIN
  59. // from USB HID Specification 1.1, Appendix B.1
  60. const uint8_t hid_descriptor_keyboard_boot_mode[] = {
  61. 0x05, 0x01, // Usage Page (Generic Desktop)
  62. 0x09, 0x06, // Usage (Keyboard)
  63. 0xa1, 0x01, // Collection (Application)
  64. // Modifier byte
  65. 0x75, 0x01, // Report Size (1)
  66. 0x95, 0x08, // Report Count (8)
  67. 0x05, 0x07, // Usage Page (Key codes)
  68. 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl)
  69. 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI)
  70. 0x15, 0x00, // Logical Minimum (0)
  71. 0x25, 0x01, // Logical Maximum (1)
  72. 0x81, 0x02, // Input (Data, Variable, Absolute)
  73. // Reserved byte
  74. 0x75, 0x01, // Report Size (1)
  75. 0x95, 0x08, // Report Count (8)
  76. 0x81, 0x03, // Input (Constant, Variable, Absolute)
  77. // LED report + padding
  78. 0x95, 0x05, // Report Count (5)
  79. 0x75, 0x01, // Report Size (1)
  80. 0x05, 0x08, // Usage Page (LEDs)
  81. 0x19, 0x01, // Usage Minimum (Num Lock)
  82. 0x29, 0x05, // Usage Maxium (Kana)
  83. 0x91, 0x02, // Output (Data, Variable, Absolute)
  84. 0x95, 0x01, // Report Count (1)
  85. 0x75, 0x03, // Report Size (3)
  86. 0x91, 0x03, // Output (Constant, Variable, Absolute)
  87. // Keycodes
  88. 0x95, 0x06, // Report Count (6)
  89. 0x75, 0x08, // Report Size (8)
  90. 0x15, 0x00, // Logical Minimum (0)
  91. 0x25, 0xff, // Logical Maximum (1)
  92. 0x05, 0x07, // Usage Page (Key codes)
  93. 0x19, 0x00, // Usage Minimum (Reserved (no event indicated))
  94. 0x29, 0xff, // Usage Maxium (Reserved)
  95. 0x81, 0x00, // Input (Data, Array)
  96. 0xc0, // End collection
  97. };
  98. //
  99. #define CHAR_ILLEGAL 0xff
  100. #define CHAR_RETURN '\n'
  101. #define CHAR_ESCAPE 27
  102. #define CHAR_TAB '\t'
  103. #define CHAR_BACKSPACE 0x7f
  104. // Simplified US Keyboard with Shift modifier
  105. /**
  106. * English (US)
  107. */
  108. static const uint8_t keytable_us_none [] = {
  109. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */
  110. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */
  111. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */
  112. 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */
  113. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */
  114. CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */
  115. '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */
  116. '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */
  117. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */
  118. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */
  119. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */
  120. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */
  121. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */
  122. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */
  123. '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
  124. '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */
  125. };
  126. static const uint8_t keytable_us_shift[] = {
  127. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */
  128. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */
  129. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */
  130. 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */
  131. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */
  132. CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */
  133. '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */
  134. '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */
  135. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */
  136. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */
  137. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */
  138. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */
  139. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */
  140. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */
  141. '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
  142. '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */
  143. };
  144. // STATE
  145. static uint8_t hid_service_buffer[250];
  146. static uint8_t device_id_sdp_service_buffer[100];
  147. static const char hid_device_name[] = "BTstack HID Keyboard";
  148. static btstack_packet_callback_registration_t hci_event_callback_registration;
  149. static uint16_t hid_cid;
  150. static bd_addr_t device_addr;
  151. static uint8_t hid_boot_device = 0;
  152. #ifdef HAVE_BTSTACK_STDIN
  153. static const char * device_addr_string = "BC:EC:5D:E6:15:03";
  154. #endif
  155. static enum {
  156. APP_BOOTING,
  157. APP_NOT_CONNECTED,
  158. APP_CONNECTING,
  159. APP_CONNECTED
  160. } app_state = APP_BOOTING;
  161. // HID Keyboard lookup
  162. static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){
  163. int i;
  164. for (i=0;i<size;i++){
  165. if (table[i] != character) continue;
  166. *keycode = i;
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){
  172. int found;
  173. found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode);
  174. if (found) {
  175. *modifier = 0; // none
  176. return 1;
  177. }
  178. found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode);
  179. if (found) {
  180. *modifier = 2; // shift
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. // HID Report sending
  186. static int send_keycode;
  187. static int send_modifier;
  188. static void send_key(int modifier, int keycode){
  189. send_keycode = keycode;
  190. send_modifier = modifier;
  191. hid_device_request_can_send_now_event(hid_cid);
  192. }
  193. static void send_report(int modifier, int keycode){
  194. uint8_t report[] = { 0xa1, modifier, 0, 0, keycode, 0, 0, 0, 0, 0};
  195. hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report));
  196. }
  197. // Demo Application
  198. #ifdef HAVE_BTSTACK_STDIN
  199. // On systems with STDIN, we can directly type on the console
  200. static void stdin_process(char character){
  201. uint8_t modifier;
  202. uint8_t keycode;
  203. int found;
  204. switch (app_state){
  205. case APP_BOOTING:
  206. case APP_CONNECTING:
  207. // ignore
  208. break;
  209. case APP_CONNECTED:
  210. // send keyu
  211. found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
  212. if (found){
  213. send_key(modifier, keycode);
  214. return;
  215. }
  216. break;
  217. case APP_NOT_CONNECTED:
  218. printf("Connecting to %s...\n", bd_addr_to_str(device_addr));
  219. hid_device_connect(device_addr, &hid_cid);
  220. break;
  221. }
  222. }
  223. #else
  224. // On embedded systems, send constant demo text with fixed period
  225. #define TYPING_PERIOD_MS 100
  226. static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n";
  227. static int demo_pos;
  228. static btstack_timer_source_t typing_timer;
  229. static void typing_timer_handler(btstack_timer_source_t * ts){
  230. // abort if not connected
  231. if (!hid_cid) return;
  232. // get next character
  233. uint8_t character = demo_text[demo_pos++];
  234. if (demo_text[demo_pos] == 0){
  235. demo_pos = 0;
  236. }
  237. // get keycodeand send
  238. uint8_t modifier;
  239. uint8_t keycode;
  240. int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
  241. if (found){
  242. send_key(modifier, keycode);
  243. }
  244. // set next timer
  245. btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS);
  246. btstack_run_loop_add_timer(ts);
  247. }
  248. static void hid_embedded_start_typing(void){
  249. demo_pos = 0;
  250. // set one-shot timer
  251. typing_timer.process = &typing_timer_handler;
  252. btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS);
  253. btstack_run_loop_add_timer(&typing_timer);
  254. }
  255. #endif
  256. static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){
  257. UNUSED(channel);
  258. UNUSED(packet_size);
  259. uint8_t status;
  260. switch (packet_type){
  261. case HCI_EVENT_PACKET:
  262. switch (packet[0]){
  263. case BTSTACK_EVENT_STATE:
  264. if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
  265. app_state = APP_NOT_CONNECTED;
  266. break;
  267. case HCI_EVENT_USER_CONFIRMATION_REQUEST:
  268. // ssp: inform about user confirmation request
  269. log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet));
  270. log_info("SSP User Confirmation Auto accept\n");
  271. break;
  272. case HCI_EVENT_HID_META:
  273. switch (hci_event_hid_meta_get_subevent_code(packet)){
  274. case HID_SUBEVENT_CONNECTION_OPENED:
  275. status = hid_subevent_connection_opened_get_status(packet);
  276. if (status) {
  277. // outgoing connection failed
  278. printf("Connection failed, status 0x%x\n", status);
  279. app_state = APP_NOT_CONNECTED;
  280. hid_cid = 0;
  281. return;
  282. }
  283. app_state = APP_CONNECTED;
  284. hid_cid = hid_subevent_connection_opened_get_hid_cid(packet);
  285. #ifdef HAVE_BTSTACK_STDIN
  286. printf("HID Connected, please start typing...\n");
  287. #else
  288. printf("HID Connected, sending demo text...\n");
  289. hid_embedded_start_typing();
  290. #endif
  291. break;
  292. case HID_SUBEVENT_CONNECTION_CLOSED:
  293. printf("HID Disconnected\n");
  294. app_state = APP_NOT_CONNECTED;
  295. hid_cid = 0;
  296. break;
  297. case HID_SUBEVENT_CAN_SEND_NOW:
  298. if (send_keycode){
  299. send_report(send_modifier, send_keycode);
  300. send_keycode = 0;
  301. send_modifier = 0;
  302. hid_device_request_can_send_now_event(hid_cid);
  303. } else {
  304. send_report(0, 0);
  305. }
  306. break;
  307. default:
  308. break;
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. /* @section Main Application Setup
  320. *
  321. * @text Listing MainConfiguration shows main application code.
  322. * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it.
  323. * At the end the Bluetooth stack is started.
  324. */
  325. /* LISTING_START(MainConfiguration): Setup HID Device */
  326. int btstack_main(int argc, const char * argv[]);
  327. int btstack_main(int argc, const char * argv[]){
  328. (void)argc;
  329. (void)argv;
  330. // allow to get found by inquiry
  331. gap_discoverable_control(1);
  332. // use Limited Discoverable Mode; Peripheral; Keyboard as CoD
  333. gap_set_class_of_device(0x2540);
  334. // set local name to be identified - zeroes will be replaced by actual BD ADDR
  335. gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00");
  336. // allow for role switch in general and sniff mode
  337. gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
  338. // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it
  339. gap_set_allow_role_switch(true);
  340. // L2CAP
  341. l2cap_init();
  342. // SDP Server
  343. sdp_init();
  344. memset(hid_service_buffer, 0, sizeof(hid_service_buffer));
  345. // hid sevice subclass 2540 Keyboard, hid counntry code 33 US, hid virtual cable off, hid reconnect initiate off, hid boot device off
  346. hid_create_sdp_record(hid_service_buffer, 0x10001, 0x2540, 33, 0, 0, hid_boot_device, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode), hid_device_name);
  347. printf("HID service record size: %u\n", de_get_len( hid_service_buffer));
  348. sdp_register_service(hid_service_buffer);
  349. // See https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers if you don't have a USB Vendor ID and need a Bluetooth Vendor ID
  350. // device info: BlueKitchen GmbH, product 1, version 1
  351. device_id_create_sdp_record(device_id_sdp_service_buffer, 0x10003, DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1);
  352. printf("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer));
  353. sdp_register_service(device_id_sdp_service_buffer);
  354. // HID Device
  355. hid_device_init(hid_boot_device, sizeof(hid_descriptor_keyboard_boot_mode), hid_descriptor_keyboard_boot_mode);
  356. // register for HCI events
  357. hci_event_callback_registration.callback = &packet_handler;
  358. hci_add_event_handler(&hci_event_callback_registration);
  359. // register for HID events
  360. hid_device_register_packet_handler(&packet_handler);
  361. #ifdef HAVE_BTSTACK_STDIN
  362. sscanf_bd_addr(device_addr_string, device_addr);
  363. btstack_stdin_setup(stdin_process);
  364. #endif
  365. // turn on!
  366. hci_power_control(HCI_POWER_ON);
  367. return 0;
  368. }
  369. /* LISTING_END */
  370. /* EXAMPLE_END */