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 BLUEKITCHEN 24 * GMBH 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__ "hid_mouse_demo.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(hid_mouse_demo): HID Mouse Classic 42 * 43 * @text This HID Device example demonstrates how to implement 44 * an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent 45 * If HAVE_BTSTACK_STDIN is defined, you can type from the terminal 46 */ 47 // ***************************************************************************** 48 49 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <inttypes.h> 55 56 #include "btstack.h" 57 58 #ifdef HAVE_BTSTACK_STDIN 59 #include "btstack_stdin.h" 60 #endif 61 62 // to enable demo text on POSIX systems 63 // #undef HAVE_BTSTACK_STDIN 64 65 static uint8_t hid_service_buffer[250]; 66 static const char hid_device_name[] = "BTstack HID Mouse"; 67 static btstack_packet_callback_registration_t hci_event_callback_registration; 68 static uint16_t hid_cid; 69 70 // from USB HID Specification 1.1, Appendix B.2 71 const uint8_t hid_descriptor_mouse_boot_mode[] = { 72 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 73 0x09, 0x02, // USAGE (Mouse) 74 0xa1, 0x01, // COLLECTION (Application) 75 76 0x09, 0x01, // USAGE (Pointer) 77 0xa1, 0x00, // COLLECTION (Physical) 78 79 0x05, 0x09, // USAGE_PAGE (Button) 80 0x19, 0x01, // USAGE_MINIMUM (Button 1) 81 0x29, 0x03, // USAGE_MAXIMUM (Button 3) 82 0x15, 0x00, // LOGICAL_MINIMUM (0) 83 0x25, 0x01, // LOGICAL_MAXIMUM (1) 84 0x95, 0x03, // REPORT_COUNT (3) 85 0x75, 0x01, // REPORT_SIZE (1) 86 0x81, 0x02, // INPUT (Data,Var,Abs) 87 0x95, 0x01, // REPORT_COUNT (1) 88 0x75, 0x05, // REPORT_SIZE (5) 89 0x81, 0x03, // INPUT (Cnst,Var,Abs) 90 91 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 92 0x09, 0x30, // USAGE (X) 93 0x09, 0x31, // USAGE (Y) 94 0x15, 0x81, // LOGICAL_MINIMUM (-127) 95 0x25, 0x7f, // LOGICAL_MAXIMUM (127) 96 0x75, 0x08, // REPORT_SIZE (8) 97 0x95, 0x02, // REPORT_COUNT (2) 98 0x81, 0x06, // INPUT (Data,Var,Rel) 99 100 0xc0, // END_COLLECTION 101 0xc0 // END_COLLECTION 102 }; 103 104 // HID Report sending 105 static void send_report(uint8_t buttons, int8_t dx, int8_t dy){ 106 uint8_t report[] = { 0xa1, buttons, (uint8_t) dx, (uint8_t) dy}; 107 hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report)); 108 printf("Mouse: %d/%d - buttons: %02x\n", dx, dy, buttons); 109 } 110 111 static int dx; 112 static int dy; 113 static uint8_t buttons; 114 static int hid_boot_device = 0; 115 116 static void mousing_can_send_now(void){ 117 send_report(buttons, dx, dy); 118 // reset 119 dx = 0; 120 dy = 0; 121 if (buttons){ 122 buttons = 0; 123 hid_device_request_can_send_now_event(hid_cid); 124 } 125 } 126 127 // Demo Application 128 129 #ifdef HAVE_BTSTACK_STDIN 130 131 static const int MOUSE_SPEED = 30; 132 133 // On systems with STDIN, we can directly type on the console 134 135 static void stdin_process(char character){ 136 137 if (!hid_cid) { 138 printf("Mouse not connected, ignoring '%c'\n", character); 139 return; 140 } 141 142 switch (character){ 143 case 'a': 144 dx -= MOUSE_SPEED; 145 break; 146 case 's': 147 dy += MOUSE_SPEED; 148 break; 149 case 'd': 150 dx += MOUSE_SPEED; 151 break; 152 case 'w': 153 dy -= MOUSE_SPEED; 154 break; 155 case 'l': 156 buttons |= 1; 157 break; 158 case 'r': 159 buttons |= 2; 160 break; 161 default: 162 return; 163 } 164 hid_device_request_can_send_now_event(hid_cid); 165 } 166 167 #else 168 169 // On embedded systems, simulate clicking on 4 corners of a square 170 171 #define MOUSE_PERIOD_MS 15 172 173 static int step; 174 static const int STEPS_PER_DIRECTION = 50; 175 static const int MOUSE_SPEED = 10; 176 177 static struct { 178 int dx; 179 int dy; 180 } directions[] = { 181 { 1, 0 }, 182 { 0, 1 }, 183 { -1, 0 }, 184 { 0, -1 }, 185 }; 186 187 static btstack_timer_source_t mousing_timer; 188 189 static void mousing_timer_handler(btstack_timer_source_t * ts){ 190 191 if (!hid_cid) return; 192 193 // simulate left click when corner reached 194 if (step % STEPS_PER_DIRECTION == 0){ 195 buttons |= 1; 196 } 197 // simulate move 198 int direction_index = step / STEPS_PER_DIRECTION; 199 dx += directions[direction_index].dx * MOUSE_SPEED; 200 dy += directions[direction_index].dy * MOUSE_SPEED; 201 202 // next 203 step++; 204 if (step >= STEPS_PER_DIRECTION * 4) { 205 step = 0; 206 } 207 208 // trigger send 209 hid_device_request_can_send_now_event(hid_cid); 210 211 // set next timer 212 btstack_run_loop_set_timer(ts, MOUSE_PERIOD_MS); 213 btstack_run_loop_add_timer(ts); 214 } 215 216 static void hid_embedded_start_mousing(void){ 217 printf("Start mousing..\n"); 218 219 step = 0; 220 221 // set one-shot timer 222 mousing_timer.process = &mousing_timer_handler; 223 btstack_run_loop_set_timer(&mousing_timer, MOUSE_PERIOD_MS); 224 btstack_run_loop_add_timer(&mousing_timer); 225 } 226 #endif 227 228 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 229 UNUSED(channel); 230 UNUSED(packet_size); 231 switch (packet_type){ 232 case HCI_EVENT_PACKET: 233 switch (hci_event_packet_get_type(packet)){ 234 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 235 // ssp: inform about user confirmation request 236 log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); 237 log_info("SSP User Confirmation Auto accept\n"); 238 break; 239 240 case HCI_EVENT_HID_META: 241 switch (hci_event_hid_meta_get_subevent_code(packet)){ 242 case HID_SUBEVENT_CONNECTION_OPENED: 243 if (hid_subevent_connection_opened_get_status(packet) != ERROR_CODE_SUCCESS) return; 244 hid_cid = hid_subevent_connection_opened_get_hid_cid(packet); 245 #ifdef HAVE_BTSTACK_STDIN 246 printf("HID Connected, control mouse using 'a','s',''d', 'w' keys for movement and 'l' and 'r' for buttons...\n"); 247 #else 248 printf("HID Connected, simulating mouse movements...\n"); 249 hid_embedded_start_mousing(); 250 #endif 251 break; 252 case HID_SUBEVENT_CONNECTION_CLOSED: 253 printf("HID Disconnected\n"); 254 hid_cid = 0; 255 break; 256 case HID_SUBEVENT_CAN_SEND_NOW: 257 mousing_can_send_now(); 258 break; 259 default: 260 break; 261 } 262 break; 263 default: 264 break; 265 } 266 break; 267 default: 268 break; 269 } 270 } 271 272 /* @section Main Application Setup 273 * 274 * @text Listing MainConfiguration shows main application code. 275 * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 276 * At the end the Bluetooth stack is started. 277 */ 278 279 /* LISTING_START(MainConfiguration): Setup HID Device */ 280 281 int btstack_main(int argc, const char * argv[]); 282 int btstack_main(int argc, const char * argv[]){ 283 (void)argc; 284 (void)argv; 285 286 // allow to get found by inquiry 287 gap_discoverable_control(1); 288 // use Limited Discoverable Mode; Peripheral; Pointing Device as CoD 289 gap_set_class_of_device(0x2580); 290 // set local name to be identified - zeroes will be replaced by actual BD ADDR 291 gap_set_local_name("HID Mouse Demo 00:00:00:00:00:00"); 292 // allow for role switch in general and sniff mode 293 gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE ); 294 // allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it 295 gap_set_allow_role_switch(true); 296 297 // L2CAP 298 l2cap_init(); 299 300 #ifdef ENABLE_BLE 301 // Initialize LE Security Manager. Needed for cross-transport key derivation 302 sm_init(); 303 #endif 304 305 // SDP Server 306 sdp_init(); 307 memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 308 309 uint8_t hid_virtual_cable = 0; 310 uint8_t hid_remote_wake = 1; 311 uint8_t hid_reconnect_initiate = 1; 312 uint8_t hid_normally_connectable = 1; 313 314 hid_sdp_record_t hid_params = { 315 // hid sevice subclass 2580 Mouse, hid counntry code 33 US 316 0x2580, 33, 317 hid_virtual_cable, hid_remote_wake, 318 hid_reconnect_initiate, hid_normally_connectable, 319 hid_boot_device, 320 0xFFFF, 0xFFFF, 3200, 321 hid_descriptor_mouse_boot_mode, 322 sizeof(hid_descriptor_mouse_boot_mode), 323 hid_device_name 324 }; 325 326 hid_create_sdp_record(hid_service_buffer, 0x10001, &hid_params); 327 328 printf("SDP service record size: %u\n", de_get_len( hid_service_buffer)); 329 sdp_register_service(hid_service_buffer); 330 331 // HID Device 332 hid_device_init(hid_boot_device, sizeof(hid_descriptor_mouse_boot_mode), hid_descriptor_mouse_boot_mode); 333 // register for HCI events 334 hci_event_callback_registration.callback = &packet_handler; 335 hci_add_event_handler(&hci_event_callback_registration); 336 337 // register for HID 338 hid_device_register_packet_handler(&packet_handler); 339 340 #ifdef HAVE_BTSTACK_STDIN 341 btstack_stdin_setup(stdin_process); 342 #endif 343 // turn on! 344 hci_power_control(HCI_POWER_ON); 345 return 0; 346 } 347 /* LISTING_END */ 348 /* EXAMPLE_END */ 349