hog_keyboard_demo.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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__ "hog_keyboard_demo.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(hog_keyboard_demo): HID-over-GATT Keyboard
  40. */
  41. // *****************************************************************************
  42. #include <stdint.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <inttypes.h>
  47. #include "hog_keyboard_demo.h"
  48. #include "btstack.h"
  49. #include "ble/gatt-service/battery_service_server.h"
  50. #include "ble/gatt-service/device_information_service_server.h"
  51. #include "ble/gatt-service/hids_device.h"
  52. // from USB HID Specification 1.1, Appendix B.1
  53. const uint8_t hid_descriptor_keyboard_boot_mode[] = {
  54. 0x05, 0x01, // Usage Page (Generic Desktop)
  55. 0x09, 0x06, // Usage (Keyboard)
  56. 0xa1, 0x01, // Collection (Application)
  57. 0x85, 0x01, // Report ID 1
  58. // Modifier byte
  59. 0x75, 0x01, // Report Size (1)
  60. 0x95, 0x08, // Report Count (8)
  61. 0x05, 0x07, // Usage Page (Key codes)
  62. 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl)
  63. 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI)
  64. 0x15, 0x00, // Logical Minimum (0)
  65. 0x25, 0x01, // Logical Maximum (1)
  66. 0x81, 0x02, // Input (Data, Variable, Absolute)
  67. // Reserved byte
  68. 0x75, 0x01, // Report Size (1)
  69. 0x95, 0x08, // Report Count (8)
  70. 0x81, 0x03, // Input (Constant, Variable, Absolute)
  71. // LED report + padding
  72. 0x95, 0x05, // Report Count (5)
  73. 0x75, 0x01, // Report Size (1)
  74. 0x05, 0x08, // Usage Page (LEDs)
  75. 0x19, 0x01, // Usage Minimum (Num Lock)
  76. 0x29, 0x05, // Usage Maxium (Kana)
  77. 0x91, 0x02, // Output (Data, Variable, Absolute)
  78. 0x95, 0x01, // Report Count (1)
  79. 0x75, 0x03, // Report Size (3)
  80. 0x91, 0x03, // Output (Constant, Variable, Absolute)
  81. // Keycodes
  82. 0x95, 0x06, // Report Count (6)
  83. 0x75, 0x08, // Report Size (8)
  84. 0x15, 0x00, // Logical Minimum (0)
  85. 0x25, 0xff, // Logical Maximum (1)
  86. 0x05, 0x07, // Usage Page (Key codes)
  87. 0x19, 0x00, // Usage Minimum (Reserved (no event indicated))
  88. 0x29, 0xff, // Usage Maxium (Reserved)
  89. 0x81, 0x00, // Input (Data, Array)
  90. 0xc0, // End collection
  91. };
  92. //
  93. #define CHAR_ILLEGAL 0xff
  94. #define CHAR_RETURN '\n'
  95. #define CHAR_ESCAPE 27
  96. #define CHAR_TAB '\t'
  97. #define CHAR_BACKSPACE 0x7f
  98. // Simplified US Keyboard with Shift modifier
  99. /**
  100. * English (US)
  101. */
  102. static const uint8_t keytable_us_none [] = {
  103. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */
  104. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', /* 4-13 */
  105. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 14-23 */
  106. 'u', 'v', 'w', 'x', 'y', 'z', /* 24-29 */
  107. '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', /* 30-39 */
  108. CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */
  109. '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',', /* 45-54 */
  110. '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */
  111. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */
  112. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */
  113. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */
  114. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */
  115. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */
  116. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */
  117. '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
  118. '6', '7', '8', '9', '0', '.', 0xa7, /* 97-100 */
  119. };
  120. static const uint8_t keytable_us_shift[] = {
  121. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 0-3 */
  122. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', /* 4-13 */
  123. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 14-23 */
  124. 'U', 'V', 'W', 'X', 'Y', 'Z', /* 24-29 */
  125. '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', /* 30-39 */
  126. CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ', /* 40-44 */
  127. '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<', /* 45-54 */
  128. '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 55-60 */
  129. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 61-64 */
  130. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 65-68 */
  131. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 69-72 */
  132. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 73-76 */
  133. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 77-80 */
  134. CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, /* 81-84 */
  135. '*', '-', '+', '\n', '1', '2', '3', '4', '5', /* 85-97 */
  136. '6', '7', '8', '9', '0', '.', 0xb1, /* 97-100 */
  137. };
  138. // static btstack_timer_source_t heartbeat;
  139. static btstack_packet_callback_registration_t hci_event_callback_registration;
  140. static btstack_packet_callback_registration_t sm_event_callback_registration;
  141. static uint8_t battery = 100;
  142. static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
  143. static uint8_t protocol_mode = 1;
  144. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  145. const uint8_t adv_data[] = {
  146. // Flags general discoverable, BR/EDR not supported
  147. 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  148. // Name
  149. 0x0d, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'H', 'I', 'D', ' ', 'K', 'e', 'y', 'b', 'o', 'a', 'r', 'd',
  150. // 16-bit Service UUIDs
  151. 0x03, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE & 0xff, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE >> 8,
  152. // Appearance HID - Keyboard (Category 15, Sub-Category 1)
  153. 0x03, BLUETOOTH_DATA_TYPE_APPEARANCE, 0xC1, 0x03,
  154. };
  155. const uint8_t adv_data_len = sizeof(adv_data);
  156. static void le_keyboard_setup(void){
  157. l2cap_init();
  158. // setup le device db
  159. le_device_db_init();
  160. // setup SM: Display only
  161. sm_init();
  162. sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
  163. sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
  164. // setup ATT server
  165. att_server_init(profile_data, NULL, NULL);
  166. // setup battery service
  167. battery_service_server_init(battery);
  168. // setup device information service
  169. device_information_service_server_init();
  170. // setup HID Device service
  171. hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode));
  172. // setup advertisements
  173. uint16_t adv_int_min = 0x0030;
  174. uint16_t adv_int_max = 0x0030;
  175. uint8_t adv_type = 0;
  176. bd_addr_t null_addr;
  177. memset(null_addr, 0, 6);
  178. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  179. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  180. gap_advertisements_enable(1);
  181. // register for HCI events
  182. hci_event_callback_registration.callback = &packet_handler;
  183. hci_add_event_handler(&hci_event_callback_registration);
  184. // register for SM events
  185. sm_event_callback_registration.callback = &packet_handler;
  186. sm_add_event_handler(&sm_event_callback_registration);
  187. // register for HIDS
  188. hids_device_register_packet_handler(packet_handler);
  189. }
  190. // HID Keyboard lookup
  191. static int lookup_keycode(uint8_t character, const uint8_t * table, int size, uint8_t * keycode){
  192. int i;
  193. for (i=0;i<size;i++){
  194. if (table[i] != character) continue;
  195. *keycode = i;
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. static int keycode_and_modifer_us_for_character(uint8_t character, uint8_t * keycode, uint8_t * modifier){
  201. int found;
  202. found = lookup_keycode(character, keytable_us_none, sizeof(keytable_us_none), keycode);
  203. if (found) {
  204. *modifier = 0; // none
  205. return 1;
  206. }
  207. found = lookup_keycode(character, keytable_us_shift, sizeof(keytable_us_shift), keycode);
  208. if (found) {
  209. *modifier = 2; // shift
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. // HID Report sending
  215. static void send_report(int modifier, int keycode){
  216. uint8_t report[] = { modifier, 0, keycode, 0, 0, 0, 0, 0};
  217. switch (protocol_mode){
  218. case 0:
  219. hids_device_send_boot_keyboard_input_report(con_handle, report, sizeof(report));
  220. break;
  221. case 1:
  222. hids_device_send_input_report(con_handle, report, sizeof(report));
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. // Demo Application
  229. #ifdef HAVE_BTSTACK_STDIN
  230. // On systems with STDIN, we can directly type on the console
  231. static enum {
  232. W4_INPUT,
  233. W4_CAN_SEND_FROM_BUFFER,
  234. W4_CAN_SEND_KEY_UP,
  235. } state;
  236. // Buffer for 20 characters
  237. static uint8_t ascii_input_storage[20];
  238. static btstack_ring_buffer_t ascii_input_buffer;
  239. static void typing_can_send_now(void){
  240. switch (state){
  241. case W4_CAN_SEND_FROM_BUFFER:
  242. while (1){
  243. uint8_t c;
  244. uint32_t num_bytes_read;
  245. btstack_ring_buffer_read(&ascii_input_buffer, &c, 1, &num_bytes_read);
  246. if (num_bytes_read == 0){
  247. state = W4_INPUT;
  248. break;
  249. }
  250. uint8_t modifier;
  251. uint8_t keycode;
  252. int found = keycode_and_modifer_us_for_character(c, &keycode, &modifier);
  253. if (!found) continue;
  254. printf("sending: %c\n", c);
  255. send_report(modifier, keycode);
  256. state = W4_CAN_SEND_KEY_UP;
  257. hids_device_request_can_send_now_event(con_handle);
  258. break;
  259. }
  260. break;
  261. case W4_CAN_SEND_KEY_UP:
  262. send_report(0, 0);
  263. if (btstack_ring_buffer_bytes_available(&ascii_input_buffer)){
  264. state = W4_CAN_SEND_FROM_BUFFER;
  265. hids_device_request_can_send_now_event(con_handle);
  266. } else {
  267. state = W4_INPUT;
  268. }
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. static void stdin_process(char character){
  275. uint8_t c = character;
  276. btstack_ring_buffer_write(&ascii_input_buffer, &c, 1);
  277. // start sending
  278. if (state == W4_INPUT && con_handle != HCI_CON_HANDLE_INVALID){
  279. state = W4_CAN_SEND_FROM_BUFFER;
  280. hids_device_request_can_send_now_event(con_handle);
  281. }
  282. }
  283. #else
  284. // On embedded systems, send constant demo text with fixed period
  285. #define TYPING_PERIOD_MS 50
  286. static const char * demo_text = "\n\nHello World!\n\nThis is the BTstack HID Keyboard Demo running on an Embedded Device.\n\n";
  287. static int demo_pos;
  288. static btstack_timer_source_t typing_timer;
  289. static int send_keycode;
  290. static int send_modifier;
  291. static int send_keyup;
  292. static void send_key(int modifier, int keycode){
  293. send_keycode = keycode;
  294. send_modifier = modifier;
  295. hids_device_request_can_send_now_event(con_handle);
  296. }
  297. static void typing_can_send_now(void){
  298. send_report(send_modifier, send_keycode);
  299. }
  300. static void typing_timer_handler(btstack_timer_source_t * ts){
  301. if (send_keyup){
  302. // just send key up
  303. send_keyup = 0;
  304. send_key(0, 0);
  305. } else {
  306. // get next character
  307. uint8_t character = demo_text[demo_pos++];
  308. if (demo_text[demo_pos] == 0){
  309. demo_pos = 0;
  310. }
  311. // get keycode and send
  312. uint8_t modifier;
  313. uint8_t keycode;
  314. int found = keycode_and_modifer_us_for_character(character, &keycode, &modifier);
  315. if (found){
  316. printf("%c\n", character);
  317. send_key(modifier, keycode);
  318. send_keyup = 1;
  319. }
  320. }
  321. // set next timer
  322. btstack_run_loop_set_timer(ts, TYPING_PERIOD_MS);
  323. btstack_run_loop_add_timer(ts);
  324. }
  325. static void hid_embedded_start_typing(void){
  326. printf("Start typing..\n");
  327. demo_pos = 0;
  328. // set one-shot timer
  329. typing_timer.process = &typing_timer_handler;
  330. btstack_run_loop_set_timer(&typing_timer, TYPING_PERIOD_MS);
  331. btstack_run_loop_add_timer(&typing_timer);
  332. }
  333. #endif
  334. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  335. UNUSED(channel);
  336. UNUSED(size);
  337. switch (packet_type) {
  338. case HCI_EVENT_PACKET:
  339. switch (hci_event_packet_get_type(packet)) {
  340. case HCI_EVENT_DISCONNECTION_COMPLETE:
  341. con_handle = HCI_CON_HANDLE_INVALID;
  342. printf("Disconnected\n");
  343. break;
  344. case SM_EVENT_JUST_WORKS_REQUEST:
  345. printf("Just Works requested\n");
  346. sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
  347. break;
  348. case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
  349. printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
  350. sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
  351. break;
  352. case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
  353. printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
  354. break;
  355. case HCI_EVENT_HIDS_META:
  356. switch (hci_event_hids_meta_get_subevent_code(packet)){
  357. case HIDS_SUBEVENT_INPUT_REPORT_ENABLE:
  358. con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
  359. printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet));
  360. #ifndef HAVE_BTSTACK_STDIN
  361. hid_embedded_start_typing();
  362. #endif
  363. break;
  364. case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE:
  365. con_handle = hids_subevent_boot_keyboard_input_report_enable_get_con_handle(packet);
  366. printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet));
  367. break;
  368. case HIDS_SUBEVENT_PROTOCOL_MODE:
  369. protocol_mode = hids_subevent_protocol_mode_get_protocol_mode(packet);
  370. printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot");
  371. break;
  372. case HIDS_SUBEVENT_CAN_SEND_NOW:
  373. typing_can_send_now();
  374. break;
  375. default:
  376. break;
  377. }
  378. }
  379. break;
  380. }
  381. }
  382. int btstack_main(void);
  383. int btstack_main(void)
  384. {
  385. le_keyboard_setup();
  386. #ifdef HAVE_BTSTACK_STDIN
  387. btstack_ring_buffer_init(&ascii_input_buffer, ascii_input_storage, sizeof(ascii_input_storage));
  388. btstack_stdin_setup(stdin_process);
  389. #endif
  390. // turn on!
  391. hci_power_control(HCI_POWER_ON);
  392. return 0;
  393. }
  394. /* EXAMPLE_END */