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 l2cap_init(); 124 125 // setup le device db 126 le_device_db_init(); 127 128 // setup SM: Display only 129 sm_init(); 130 sm_event_callback_registration.callback = &packet_handler; 131 sm_add_event_handler(&sm_event_callback_registration); 132 sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY); 133 // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING); 134 sm_set_authentication_requirements(SM_AUTHREQ_BONDING); 135 136 // setup ATT server 137 att_server_init(profile_data, NULL, NULL); 138 139 // setup battery service 140 battery_service_server_init(battery); 141 142 // setup device information service 143 device_information_service_server_init(); 144 145 // setup HID Device service 146 hids_device_init(0, hid_descriptor_mouse_boot_mode, sizeof(hid_descriptor_mouse_boot_mode)); 147 hids_device_register_packet_handler(packet_handler); 148 149 // setup advertisements 150 uint16_t adv_int_min = 0x0030; 151 uint16_t adv_int_max = 0x0030; 152 uint8_t adv_type = 0; 153 bd_addr_t null_addr; 154 memset(null_addr, 0, 6); 155 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 156 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 157 gap_advertisements_enable(1); 158 } 159 160 // HID Report sending 161 static void send_report(uint8_t buttons, int8_t dx, int8_t dy){ 162 // uint8_t report[] = { (uint8_t) dx, (uint8_t) dy, buttons}; 163 uint8_t report[] = { buttons, (uint8_t) dx, (uint8_t) dy}; 164 hids_device_send_input_report(con_handle, report, sizeof(report)); 165 printf("Mouse: %d/%d - buttons: %02x\n", dx, dy, buttons); 166 } 167 168 static int dx; 169 static int dy; 170 static uint8_t buttons; 171 172 static void mousing_can_send_now(void){ 173 send_report(buttons, dx, dy); 174 // reset 175 dx = 0; 176 dy = 0; 177 if (buttons){ 178 buttons = 0; 179 hids_device_request_can_send_now_event(con_handle); 180 } 181 } 182 183 // Demo Application 184 185 #ifdef HAVE_BTSTACK_STDIN 186 187 static const int MOUSE_SPEED = 30; 188 189 // On systems with STDIN, we can directly type on the console 190 191 static void stdin_process(char character){ 192 193 if (con_handle == HCI_CON_HANDLE_INVALID) { 194 printf("Mouse not connected, ignoring '%c'\n", character); 195 return; 196 } 197 198 switch (character){ 199 case 'a': 200 dx -= MOUSE_SPEED; 201 break; 202 case 's': 203 dy += MOUSE_SPEED; 204 break; 205 case 'd': 206 dx += MOUSE_SPEED; 207 break; 208 case 'w': 209 dy -= MOUSE_SPEED; 210 break; 211 case 'l': 212 buttons |= 1; 213 break; 214 case 'r': 215 buttons |= 2; 216 break; 217 default: 218 return; 219 } 220 hids_device_request_can_send_now_event(con_handle); 221 } 222 223 #else 224 225 // On embedded systems, simulate clicking on 4 corners of a square 226 227 #define MOUSE_PERIOD_MS 15 228 229 static int step; 230 static const int STEPS_PER_DIRECTION = 50; 231 static const int MOUSE_SPEED = 10; 232 233 static struct { 234 int dx; 235 int dy; 236 } directions[] = { 237 { 1, 0 }, 238 { 0, 1 }, 239 { -1, 0 }, 240 { 0, -1 }, 241 }; 242 243 static btstack_timer_source_t mousing_timer; 244 245 static void mousing_timer_handler(btstack_timer_source_t * ts){ 246 247 if (con_handle == HCI_CON_HANDLE_INVALID) return; 248 249 // simulate left click when corner reached 250 if (step % STEPS_PER_DIRECTION == 0){ 251 buttons |= 1; 252 } 253 // simulate move 254 int direction_index = step / STEPS_PER_DIRECTION; 255 dx += directions[direction_index].dx * MOUSE_SPEED; 256 dy += directions[direction_index].dy * MOUSE_SPEED; 257 258 // next 259 step++; 260 if (step >= STEPS_PER_DIRECTION * 4) { 261 step = 0; 262 } 263 264 // trigger send 265 hids_device_request_can_send_now_event(con_handle); 266 267 // set next timer 268 btstack_run_loop_set_timer(ts, MOUSE_PERIOD_MS); 269 btstack_run_loop_add_timer(ts); 270 } 271 272 static void hid_embedded_start_mousing(void){ 273 printf("Start mousing..\n"); 274 275 step = 0; 276 277 // set one-shot timer 278 mousing_timer.process = &mousing_timer_handler; 279 btstack_run_loop_set_timer(&mousing_timer, MOUSE_PERIOD_MS); 280 btstack_run_loop_add_timer(&mousing_timer); 281 } 282 283 #endif 284 285 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 286 UNUSED(channel); 287 UNUSED(size); 288 289 switch (packet_type) { 290 case HCI_EVENT_PACKET: 291 switch (hci_event_packet_get_type(packet)) { 292 case HCI_EVENT_DISCONNECTION_COMPLETE: 293 printf("Disconnected\n"); 294 break; 295 case SM_EVENT_JUST_WORKS_REQUEST: 296 printf("Just Works requested\n"); 297 sm_just_works_confirm(sm_event_just_works_request_get_handle(packet)); 298 break; 299 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 300 printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet)); 301 sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet)); 302 break; 303 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 304 printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet)); 305 break; 306 case HCI_EVENT_HIDS_META: 307 switch (hci_event_hids_meta_get_subevent_code(packet)){ 308 case HIDS_SUBEVENT_INPUT_REPORT_ENABLE: 309 con_handle = hids_subevent_input_report_enable_get_con_handle(packet); 310 printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet)); 311 #ifndef HAVE_BTSTACK_STDIN 312 hid_embedded_start_mousing(); 313 #endif 314 break; 315 case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE: 316 con_handle = hids_subevent_input_report_enable_get_con_handle(packet); 317 printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet)); 318 break; 319 case HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE: 320 con_handle = hids_subevent_input_report_enable_get_con_handle(packet); 321 printf("Boot Mouse Characteristic Subscribed %u\n", hids_subevent_boot_mouse_input_report_enable_get_enable(packet)); 322 break; 323 case HIDS_SUBEVENT_PROTOCOL_MODE: 324 printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot"); 325 break; 326 case HIDS_SUBEVENT_CAN_SEND_NOW: 327 mousing_can_send_now(); 328 break; 329 default: 330 break; 331 } 332 } 333 break; 334 } 335 } 336 337 int btstack_main(void); 338 int btstack_main(void) 339 { 340 hog_mouse_setup(); 341 342 #ifdef HAVE_BTSTACK_STDIN 343 btstack_stdin_setup(stdin_process); 344 #endif 345 346 // turn on! 347 hci_power_control(HCI_POWER_ON); 348 349 return 0; 350 } 351