hog_mouse_demo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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__ "hog_mouse_demo.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(hog_mouse_demo): HID-over-GATT Mouse
  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_mouse_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.2
  53. const uint8_t hid_descriptor_mouse_boot_mode[] = {
  54. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  55. 0x09, 0x02, // USAGE (Mouse)
  56. 0xa1, 0x01, // COLLECTION (Application)
  57. 0x85, 0x01, // Report ID 1
  58. 0x09, 0x01, // USAGE (Pointer)
  59. 0xa1, 0x00, // COLLECTION (Physical)
  60. #if 1
  61. 0x05, 0x09, // USAGE_PAGE (Button)
  62. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  63. 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
  64. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  65. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  66. 0x95, 0x03, // REPORT_COUNT (3)
  67. 0x75, 0x01, // REPORT_SIZE (1)
  68. 0x81, 0x02, // INPUT (Data,Var,Abs)
  69. 0x95, 0x01, // REPORT_COUNT (1)
  70. 0x75, 0x05, // REPORT_SIZE (5)
  71. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  72. #endif
  73. #if 1
  74. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  75. 0x09, 0x30, // USAGE (X)
  76. 0x09, 0x31, // USAGE (Y)
  77. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  78. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  79. 0x75, 0x08, // REPORT_SIZE (8)
  80. 0x95, 0x02, // REPORT_COUNT (2)
  81. 0x81, 0x06, // INPUT (Data,Var,Rel)
  82. #endif
  83. 0xc0, // END_COLLECTION
  84. 0xc0 // END_COLLECTION
  85. };
  86. static btstack_packet_callback_registration_t hci_event_callback_registration;
  87. static btstack_packet_callback_registration_t sm_event_callback_registration;
  88. static uint8_t battery = 100;
  89. static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
  90. static uint8_t protocol_mode = 1;
  91. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  92. const uint8_t adv_data[] = {
  93. // Flags general discoverable, BR/EDR not supported
  94. 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  95. // Name
  96. 0x0a, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'H', 'I', 'D', ' ', 'M', 'o', 'u', 's', 'e',
  97. // 16-bit Service UUIDs
  98. 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,
  99. // Appearance HID - Mouse (Category 15, Sub-Category 2)
  100. 0x03, BLUETOOTH_DATA_TYPE_APPEARANCE, 0xC2, 0x03,
  101. };
  102. const uint8_t adv_data_len = sizeof(adv_data);
  103. static void hog_mouse_setup(void){
  104. // setup l2cap and register for connection parameter updates
  105. l2cap_init();
  106. l2cap_register_packet_handler(&packet_handler);
  107. // setup le device db
  108. le_device_db_init();
  109. // setup SM: Display only
  110. sm_init();
  111. sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
  112. // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
  113. sm_set_authentication_requirements(SM_AUTHREQ_BONDING);
  114. // setup ATT server
  115. att_server_init(profile_data, NULL, NULL);
  116. // setup battery service
  117. battery_service_server_init(battery);
  118. // setup device information service
  119. device_information_service_server_init();
  120. // setup HID Device service
  121. hids_device_init(0, hid_descriptor_mouse_boot_mode, sizeof(hid_descriptor_mouse_boot_mode));
  122. // setup advertisements
  123. uint16_t adv_int_min = 0x0030;
  124. uint16_t adv_int_max = 0x0030;
  125. uint8_t adv_type = 0;
  126. bd_addr_t null_addr;
  127. memset(null_addr, 0, 6);
  128. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  129. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  130. gap_advertisements_enable(1);
  131. // register for events
  132. hci_event_callback_registration.callback = &packet_handler;
  133. hci_add_event_handler(&hci_event_callback_registration);
  134. sm_event_callback_registration.callback = &packet_handler;
  135. sm_add_event_handler(&sm_event_callback_registration);
  136. hids_device_register_packet_handler(packet_handler);
  137. }
  138. // HID Report sending
  139. static void send_report(uint8_t buttons, int8_t dx, int8_t dy){
  140. uint8_t report[] = { buttons, (uint8_t) dx, (uint8_t) dy, 0};
  141. switch (protocol_mode){
  142. case 0:
  143. hids_device_send_boot_mouse_input_report(con_handle, report, sizeof(report));
  144. case 1:
  145. hids_device_send_input_report(con_handle, report, sizeof(report));
  146. break;
  147. default:
  148. break;
  149. }
  150. printf("Mouse: %d/%d - buttons: %02x\n", dx, dy, buttons);
  151. }
  152. static int dx;
  153. static int dy;
  154. static uint8_t buttons;
  155. static void mousing_can_send_now(void){
  156. send_report(buttons, dx, dy);
  157. // reset
  158. dx = 0;
  159. dy = 0;
  160. if (buttons){
  161. buttons = 0;
  162. hids_device_request_can_send_now_event(con_handle);
  163. }
  164. }
  165. // Demo Application
  166. #ifdef HAVE_BTSTACK_STDIN
  167. static const int MOUSE_SPEED = 30;
  168. // On systems with STDIN, we can directly type on the console
  169. static void stdin_process(char character){
  170. if (con_handle == HCI_CON_HANDLE_INVALID) {
  171. printf("Mouse not connected, ignoring '%c'\n", character);
  172. return;
  173. }
  174. switch (character){
  175. case 'a':
  176. dx -= MOUSE_SPEED;
  177. break;
  178. case 's':
  179. dy += MOUSE_SPEED;
  180. break;
  181. case 'd':
  182. dx += MOUSE_SPEED;
  183. break;
  184. case 'w':
  185. dy -= MOUSE_SPEED;
  186. break;
  187. case 'l':
  188. buttons |= 1;
  189. break;
  190. case 'r':
  191. buttons |= 2;
  192. break;
  193. default:
  194. return;
  195. }
  196. hids_device_request_can_send_now_event(con_handle);
  197. }
  198. #else
  199. // On embedded systems, simulate clicking on 4 corners of a square
  200. #define MOUSE_PERIOD_MS 15
  201. static const int STEPS_PER_DIRECTION = 50;
  202. static const int MOUSE_SPEED = 10;
  203. static btstack_timer_source_t mousing_timer;
  204. static int mousing_active = 0;
  205. static int step;
  206. static struct {
  207. int dx;
  208. int dy;
  209. } directions[] = {
  210. { 1, 0 },
  211. { 0, 1 },
  212. { -1, 0 },
  213. { 0, -1 },
  214. };
  215. static void mousing_timer_handler(btstack_timer_source_t * ts){
  216. if (con_handle == HCI_CON_HANDLE_INVALID) {
  217. mousing_active = 0;
  218. return;
  219. }
  220. // simulate left click when corner reached
  221. if (step % STEPS_PER_DIRECTION == 0){
  222. buttons |= 1;
  223. }
  224. // simulate move
  225. int direction_index = step / STEPS_PER_DIRECTION;
  226. dx += directions[direction_index].dx * MOUSE_SPEED;
  227. dy += directions[direction_index].dy * MOUSE_SPEED;
  228. // next
  229. step++;
  230. if (step >= STEPS_PER_DIRECTION * 4) {
  231. step = 0;
  232. }
  233. // trigger send
  234. hids_device_request_can_send_now_event(con_handle);
  235. // set next timer
  236. btstack_run_loop_set_timer(ts, MOUSE_PERIOD_MS);
  237. btstack_run_loop_add_timer(ts);
  238. }
  239. static void hid_embedded_start_mousing(void){
  240. if (mousing_active) return;
  241. mousing_active = 1;
  242. printf("Start mousing..\n");
  243. step = 0;
  244. // set one-shot timer
  245. mousing_timer.process = &mousing_timer_handler;
  246. btstack_run_loop_set_timer(&mousing_timer, MOUSE_PERIOD_MS);
  247. btstack_run_loop_add_timer(&mousing_timer);
  248. }
  249. #endif
  250. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  251. UNUSED(channel);
  252. UNUSED(size);
  253. uint16_t conn_interval;
  254. switch (packet_type) {
  255. case HCI_EVENT_PACKET:
  256. switch (hci_event_packet_get_type(packet)) {
  257. case HCI_EVENT_DISCONNECTION_COMPLETE:
  258. con_handle = HCI_CON_HANDLE_INVALID;
  259. printf("Disconnected\n");
  260. break;
  261. case SM_EVENT_JUST_WORKS_REQUEST:
  262. printf("Just Works requested\n");
  263. sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
  264. break;
  265. case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
  266. printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
  267. sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
  268. break;
  269. case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
  270. printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
  271. break;
  272. case L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE:
  273. printf("L2CAP Connection Parameter Update Complete, response: %x\n", l2cap_event_connection_parameter_update_response_get_result(packet));
  274. break;
  275. case HCI_EVENT_LE_META:
  276. switch (hci_event_le_meta_get_subevent_code(packet)) {
  277. case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
  278. // print connection parameters (without using float operations)
  279. conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet);
  280. printf("LE Connection Complete:\n");
  281. printf("- Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
  282. printf("- Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet));
  283. break;
  284. case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
  285. // print connection parameters (without using float operations)
  286. conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
  287. printf("LE Connection Update:\n");
  288. printf("- Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
  289. printf("- Connection Latency: %u\n", hci_subevent_le_connection_update_complete_get_conn_latency(packet));
  290. break;
  291. default:
  292. break;
  293. }
  294. break;
  295. case HCI_EVENT_HIDS_META:
  296. switch (hci_event_hids_meta_get_subevent_code(packet)){
  297. case HIDS_SUBEVENT_INPUT_REPORT_ENABLE:
  298. con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
  299. printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet));
  300. #ifndef HAVE_BTSTACK_STDIN
  301. hid_embedded_start_mousing();
  302. #endif
  303. // request connection param update via L2CAP following Apple Bluetooth Design Guidelines
  304. // gap_request_connection_parameter_update(con_handle, 12, 12, 4, 100); // 15 ms, 4, 1s
  305. // directly update connection params via HCI following Apple Bluetooth Design Guidelines
  306. // gap_update_connection_parameters(con_handle, 12, 12, 4, 100); // 60-75 ms, 4, 1s
  307. break;
  308. case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE:
  309. con_handle = hids_subevent_boot_keyboard_input_report_enable_get_con_handle(packet);
  310. printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet));
  311. break;
  312. case HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE:
  313. con_handle = hids_subevent_boot_mouse_input_report_enable_get_con_handle(packet);
  314. printf("Boot Mouse Characteristic Subscribed %u\n", hids_subevent_boot_mouse_input_report_enable_get_enable(packet));
  315. break;
  316. case HIDS_SUBEVENT_PROTOCOL_MODE:
  317. protocol_mode = hids_subevent_protocol_mode_get_protocol_mode(packet);
  318. printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot");
  319. break;
  320. case HIDS_SUBEVENT_CAN_SEND_NOW:
  321. mousing_can_send_now();
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. break;
  328. }
  329. }
  330. int btstack_main(void);
  331. int btstack_main(void)
  332. {
  333. hog_mouse_setup();
  334. #ifdef HAVE_BTSTACK_STDIN
  335. btstack_stdin_setup(stdin_process);
  336. #endif
  337. // turn on!
  338. hci_power_control(HCI_POWER_ON);
  339. return 0;
  340. }