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