1*aa29f587SMatthias Ringwald /* 2*aa29f587SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*aa29f587SMatthias Ringwald * 4*aa29f587SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*aa29f587SMatthias Ringwald * modification, are permitted provided that the following conditions 6*aa29f587SMatthias Ringwald * are met: 7*aa29f587SMatthias Ringwald * 8*aa29f587SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*aa29f587SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*aa29f587SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*aa29f587SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*aa29f587SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*aa29f587SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*aa29f587SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*aa29f587SMatthias Ringwald * from this software without specific prior written permission. 16*aa29f587SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*aa29f587SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*aa29f587SMatthias Ringwald * monetary gain. 19*aa29f587SMatthias Ringwald * 20*aa29f587SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*aa29f587SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*aa29f587SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*aa29f587SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*aa29f587SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*aa29f587SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*aa29f587SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*aa29f587SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*aa29f587SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*aa29f587SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*aa29f587SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*aa29f587SMatthias Ringwald * SUCH DAMAGE. 32*aa29f587SMatthias Ringwald * 33*aa29f587SMatthias Ringwald * Please inquire about commercial licensing options at 34*aa29f587SMatthias Ringwald * [email protected] 35*aa29f587SMatthias Ringwald * 36*aa29f587SMatthias Ringwald */ 37*aa29f587SMatthias Ringwald 38*aa29f587SMatthias Ringwald #define __BTSTACK_FILE__ "hid_mouse_demo.c" 39*aa29f587SMatthias Ringwald 40*aa29f587SMatthias Ringwald // ***************************************************************************** 41*aa29f587SMatthias Ringwald /* EXAMPLE_START(hid_device_demo): HID Device (Server) Demo 42*aa29f587SMatthias Ringwald * 43*aa29f587SMatthias Ringwald * Status: Basic implementation. HID Request from Host are not answered yet. Works with iOS. 44*aa29f587SMatthias Ringwald * 45*aa29f587SMatthias Ringwald * @text This HID Device example demonstrates how to implement 46*aa29f587SMatthias Ringwald * an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent 47*aa29f587SMatthias Ringwald * If HAVE_BTSTACK_STDIN is defined, you can type from the terminal 48*aa29f587SMatthias Ringwald */ 49*aa29f587SMatthias Ringwald // ***************************************************************************** 50*aa29f587SMatthias Ringwald 51*aa29f587SMatthias Ringwald 52*aa29f587SMatthias Ringwald #include <stdint.h> 53*aa29f587SMatthias Ringwald #include <stdio.h> 54*aa29f587SMatthias Ringwald #include <stdlib.h> 55*aa29f587SMatthias Ringwald #include <string.h> 56*aa29f587SMatthias Ringwald #include <inttypes.h> 57*aa29f587SMatthias Ringwald 58*aa29f587SMatthias Ringwald #include "btstack.h" 59*aa29f587SMatthias Ringwald 60*aa29f587SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 61*aa29f587SMatthias Ringwald #include "btstack_stdin.h" 62*aa29f587SMatthias Ringwald #endif 63*aa29f587SMatthias Ringwald 64*aa29f587SMatthias Ringwald // to enable demo text on POSIX systems 65*aa29f587SMatthias Ringwald // #undef HAVE_BTSTACK_STDIN 66*aa29f587SMatthias Ringwald 67*aa29f587SMatthias Ringwald static uint8_t hid_service_buffer[250]; 68*aa29f587SMatthias Ringwald static const char hid_device_name[] = "BTstack HID Mouse"; 69*aa29f587SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 70*aa29f587SMatthias Ringwald static uint16_t hid_cid; 71*aa29f587SMatthias Ringwald 72*aa29f587SMatthias Ringwald // from USB HID Specification 1.1, Appendix B.2 73*aa29f587SMatthias Ringwald const uint8_t hid_descriptor_mouse_boot_mode[] = { 74*aa29f587SMatthias Ringwald 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 75*aa29f587SMatthias Ringwald 0x09, 0x02, // USAGE (Mouse) 76*aa29f587SMatthias Ringwald 0xa1, 0x01, // COLLECTION (Application) 77*aa29f587SMatthias Ringwald 78*aa29f587SMatthias Ringwald 0x09, 0x01, // USAGE (Pointer) 79*aa29f587SMatthias Ringwald 0xa1, 0x00, // COLLECTION (Physical) 80*aa29f587SMatthias Ringwald 81*aa29f587SMatthias Ringwald 0x05, 0x09, // USAGE_PAGE (Button) 82*aa29f587SMatthias Ringwald 0x19, 0x01, // USAGE_MINIMUM (Button 1) 83*aa29f587SMatthias Ringwald 0x29, 0x03, // USAGE_MAXIMUM (Button 3) 84*aa29f587SMatthias Ringwald 0x15, 0x00, // LOGICAL_MINIMUM (0) 85*aa29f587SMatthias Ringwald 0x25, 0x01, // LOGICAL_MAXIMUM (1) 86*aa29f587SMatthias Ringwald 0x95, 0x03, // REPORT_COUNT (3) 87*aa29f587SMatthias Ringwald 0x75, 0x01, // REPORT_SIZE (1) 88*aa29f587SMatthias Ringwald 0x81, 0x02, // INPUT (Data,Var,Abs) 89*aa29f587SMatthias Ringwald 0x95, 0x01, // REPORT_COUNT (1) 90*aa29f587SMatthias Ringwald 0x75, 0x05, // REPORT_SIZE (5) 91*aa29f587SMatthias Ringwald 0x81, 0x03, // INPUT (Cnst,Var,Abs) 92*aa29f587SMatthias Ringwald 93*aa29f587SMatthias Ringwald 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 94*aa29f587SMatthias Ringwald 0x09, 0x30, // USAGE (X) 95*aa29f587SMatthias Ringwald 0x09, 0x31, // USAGE (Y) 96*aa29f587SMatthias Ringwald 0x15, 0x81, // LOGICAL_MINIMUM (-127) 97*aa29f587SMatthias Ringwald 0x25, 0x7f, // LOGICAL_MAXIMUM (127) 98*aa29f587SMatthias Ringwald 0x75, 0x08, // REPORT_SIZE (8) 99*aa29f587SMatthias Ringwald 0x95, 0x02, // REPORT_COUNT (2) 100*aa29f587SMatthias Ringwald 0x81, 0x06, // INPUT (Data,Var,Rel) 101*aa29f587SMatthias Ringwald 102*aa29f587SMatthias Ringwald 0xc0, // END_COLLECTION 103*aa29f587SMatthias Ringwald 0xc0 // END_COLLECTION 104*aa29f587SMatthias Ringwald }; 105*aa29f587SMatthias Ringwald 106*aa29f587SMatthias Ringwald // HID Report sending 107*aa29f587SMatthias Ringwald static void send_report(uint8_t buttons, int8_t dx, int8_t dy){ 108*aa29f587SMatthias Ringwald uint8_t report[] = { 0xa1, buttons, (uint8_t) dx, (uint8_t) dy}; 109*aa29f587SMatthias Ringwald hid_device_send_interrupt_message(hid_cid, &report[0], sizeof(report)); 110*aa29f587SMatthias Ringwald printf("Mouse: %d/%d - buttons: %02x\n", dx, dy, buttons); 111*aa29f587SMatthias Ringwald } 112*aa29f587SMatthias Ringwald 113*aa29f587SMatthias Ringwald static int dx; 114*aa29f587SMatthias Ringwald static int dy; 115*aa29f587SMatthias Ringwald static uint8_t buttons; 116*aa29f587SMatthias Ringwald 117*aa29f587SMatthias Ringwald static void mousing_can_send_now(void){ 118*aa29f587SMatthias Ringwald send_report(buttons, dx, dy); 119*aa29f587SMatthias Ringwald // reset 120*aa29f587SMatthias Ringwald dx = 0; 121*aa29f587SMatthias Ringwald dy = 0; 122*aa29f587SMatthias Ringwald if (buttons){ 123*aa29f587SMatthias Ringwald buttons = 0; 124*aa29f587SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 125*aa29f587SMatthias Ringwald } 126*aa29f587SMatthias Ringwald } 127*aa29f587SMatthias Ringwald 128*aa29f587SMatthias Ringwald // Demo Application 129*aa29f587SMatthias Ringwald 130*aa29f587SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 131*aa29f587SMatthias Ringwald 132*aa29f587SMatthias Ringwald static const int MOUSE_SPEED = 30; 133*aa29f587SMatthias Ringwald 134*aa29f587SMatthias Ringwald // On systems with STDIN, we can directly type on the console 135*aa29f587SMatthias Ringwald 136*aa29f587SMatthias Ringwald static void stdin_process(char character){ 137*aa29f587SMatthias Ringwald 138*aa29f587SMatthias Ringwald if (!hid_cid) { 139*aa29f587SMatthias Ringwald printf("Mouse not connected, ignoring '%c'\n", character); 140*aa29f587SMatthias Ringwald return; 141*aa29f587SMatthias Ringwald } 142*aa29f587SMatthias Ringwald 143*aa29f587SMatthias Ringwald switch (character){ 144*aa29f587SMatthias Ringwald case 'a': 145*aa29f587SMatthias Ringwald dx -= MOUSE_SPEED; 146*aa29f587SMatthias Ringwald break; 147*aa29f587SMatthias Ringwald case 's': 148*aa29f587SMatthias Ringwald dy += MOUSE_SPEED; 149*aa29f587SMatthias Ringwald break; 150*aa29f587SMatthias Ringwald case 'd': 151*aa29f587SMatthias Ringwald dx += MOUSE_SPEED; 152*aa29f587SMatthias Ringwald break; 153*aa29f587SMatthias Ringwald case 'w': 154*aa29f587SMatthias Ringwald dy -= MOUSE_SPEED; 155*aa29f587SMatthias Ringwald break; 156*aa29f587SMatthias Ringwald case 'l': 157*aa29f587SMatthias Ringwald buttons |= 1; 158*aa29f587SMatthias Ringwald break; 159*aa29f587SMatthias Ringwald case 'r': 160*aa29f587SMatthias Ringwald buttons |= 2; 161*aa29f587SMatthias Ringwald break; 162*aa29f587SMatthias Ringwald default: 163*aa29f587SMatthias Ringwald return; 164*aa29f587SMatthias Ringwald } 165*aa29f587SMatthias Ringwald hid_device_request_can_send_now_event(hid_cid); 166*aa29f587SMatthias Ringwald } 167*aa29f587SMatthias Ringwald 168*aa29f587SMatthias Ringwald #else 169*aa29f587SMatthias Ringwald 170*aa29f587SMatthias Ringwald // On embedded systems, simulate clicking on 4 corners of a square 171*aa29f587SMatthias Ringwald 172*aa29f587SMatthias Ringwald #define MOUSE_PERIOD_MS 15 173*aa29f587SMatthias Ringwald 174*aa29f587SMatthias Ringwald static int step; 175*aa29f587SMatthias Ringwald static const int STEPS_PER_DIRECTION = 50; 176*aa29f587SMatthias Ringwald static const int MOUSE_SPEED = 10; 177*aa29f587SMatthias Ringwald 178*aa29f587SMatthias Ringwald static struct { 179*aa29f587SMatthias Ringwald int dx; 180*aa29f587SMatthias Ringwald int dy; 181*aa29f587SMatthias Ringwald } directions[] = { 182*aa29f587SMatthias Ringwald { 1, 0 }, 183*aa29f587SMatthias Ringwald { 0, 1 }, 184*aa29f587SMatthias Ringwald { -1, 0 }, 185*aa29f587SMatthias Ringwald { 0, -1 }, 186*aa29f587SMatthias Ringwald }; 187*aa29f587SMatthias Ringwald 188*aa29f587SMatthias Ringwald static btstack_timer_source_t mousing_timer; 189*aa29f587SMatthias Ringwald 190*aa29f587SMatthias Ringwald static void mousing_timer_handler(btstack_timer_source_t * ts){ 191*aa29f587SMatthias Ringwald 192*aa29f587SMatthias Ringwald if (con_handle == HCI_CON_HANDLE_INVALID) return; 193*aa29f587SMatthias Ringwald 194*aa29f587SMatthias Ringwald // simulate left click when corner reached 195*aa29f587SMatthias Ringwald if (step % STEPS_PER_DIRECTION == 0){ 196*aa29f587SMatthias Ringwald buttons |= 1; 197*aa29f587SMatthias Ringwald } 198*aa29f587SMatthias Ringwald // simulate move 199*aa29f587SMatthias Ringwald int direction_index = step / STEPS_PER_DIRECTION; 200*aa29f587SMatthias Ringwald dx += directions[direction_index].dx * MOUSE_SPEED; 201*aa29f587SMatthias Ringwald dy += directions[direction_index].dy * MOUSE_SPEED; 202*aa29f587SMatthias Ringwald 203*aa29f587SMatthias Ringwald // next 204*aa29f587SMatthias Ringwald step++; 205*aa29f587SMatthias Ringwald if (step >= STEPS_PER_DIRECTION * 4) { 206*aa29f587SMatthias Ringwald step = 0; 207*aa29f587SMatthias Ringwald } 208*aa29f587SMatthias Ringwald 209*aa29f587SMatthias Ringwald // trigger send 210*aa29f587SMatthias Ringwald hids_device_request_can_send_now_event(con_handle); 211*aa29f587SMatthias Ringwald 212*aa29f587SMatthias Ringwald // set next timer 213*aa29f587SMatthias Ringwald btstack_run_loop_set_timer(ts, MOUSE_PERIOD_MS); 214*aa29f587SMatthias Ringwald btstack_run_loop_add_timer(ts); 215*aa29f587SMatthias Ringwald } 216*aa29f587SMatthias Ringwald 217*aa29f587SMatthias Ringwald static void hid_embedded_start_mousing(void){ 218*aa29f587SMatthias Ringwald printf("Start mousing..\n"); 219*aa29f587SMatthias Ringwald 220*aa29f587SMatthias Ringwald step = 0; 221*aa29f587SMatthias Ringwald 222*aa29f587SMatthias Ringwald // set one-shot timer 223*aa29f587SMatthias Ringwald mousing_timer.process = &mousing_timer_handler; 224*aa29f587SMatthias Ringwald btstack_run_loop_set_timer(&mousing_timer, MOUSE_PERIOD_MS); 225*aa29f587SMatthias Ringwald btstack_run_loop_add_timer(&mousing_timer); 226*aa29f587SMatthias Ringwald } 227*aa29f587SMatthias Ringwald #endif 228*aa29f587SMatthias Ringwald 229*aa29f587SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ 230*aa29f587SMatthias Ringwald UNUSED(channel); 231*aa29f587SMatthias Ringwald UNUSED(packet_size); 232*aa29f587SMatthias Ringwald switch (packet_type){ 233*aa29f587SMatthias Ringwald case HCI_EVENT_PACKET: 234*aa29f587SMatthias Ringwald switch (packet[0]){ 235*aa29f587SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 236*aa29f587SMatthias Ringwald // ssp: inform about user confirmation request 237*aa29f587SMatthias Ringwald log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); 238*aa29f587SMatthias Ringwald log_info("SSP User Confirmation Auto accept\n"); 239*aa29f587SMatthias Ringwald break; 240*aa29f587SMatthias Ringwald 241*aa29f587SMatthias Ringwald case HCI_EVENT_HID_META: 242*aa29f587SMatthias Ringwald switch (hci_event_hid_meta_get_subevent_code(packet)){ 243*aa29f587SMatthias Ringwald case HID_SUBEVENT_CONNECTION_OPENED: 244*aa29f587SMatthias Ringwald if (hid_subevent_connection_opened_get_status(packet)) return; 245*aa29f587SMatthias Ringwald hid_cid = hid_subevent_connection_opened_get_hid_cid(packet); 246*aa29f587SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 247*aa29f587SMatthias Ringwald printf("HID Connected, control mouse using 'a','s',''d', 'w' keys for movement and 'l' and 'r' for buttons...\n"); 248*aa29f587SMatthias Ringwald #else 249*aa29f587SMatthias Ringwald printf("HID Connected, simulating mouse movements...\n"); 250*aa29f587SMatthias Ringwald hid_embedded_start_typing(); 251*aa29f587SMatthias Ringwald #endif 252*aa29f587SMatthias Ringwald break; 253*aa29f587SMatthias Ringwald case HID_SUBEVENT_CONNECTION_CLOSED: 254*aa29f587SMatthias Ringwald printf("HID Disconnected\n"); 255*aa29f587SMatthias Ringwald hid_cid = 0; 256*aa29f587SMatthias Ringwald break; 257*aa29f587SMatthias Ringwald case HID_SUBEVENT_CAN_SEND_NOW: 258*aa29f587SMatthias Ringwald mousing_can_send_now(); 259*aa29f587SMatthias Ringwald break; 260*aa29f587SMatthias Ringwald default: 261*aa29f587SMatthias Ringwald break; 262*aa29f587SMatthias Ringwald } 263*aa29f587SMatthias Ringwald break; 264*aa29f587SMatthias Ringwald default: 265*aa29f587SMatthias Ringwald break; 266*aa29f587SMatthias Ringwald } 267*aa29f587SMatthias Ringwald break; 268*aa29f587SMatthias Ringwald default: 269*aa29f587SMatthias Ringwald break; 270*aa29f587SMatthias Ringwald } 271*aa29f587SMatthias Ringwald } 272*aa29f587SMatthias Ringwald 273*aa29f587SMatthias Ringwald /* @section Main Application Setup 274*aa29f587SMatthias Ringwald * 275*aa29f587SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 276*aa29f587SMatthias Ringwald * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 277*aa29f587SMatthias Ringwald * At the end the Bluetooth stack is started. 278*aa29f587SMatthias Ringwald */ 279*aa29f587SMatthias Ringwald 280*aa29f587SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HID Device */ 281*aa29f587SMatthias Ringwald 282*aa29f587SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 283*aa29f587SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 284*aa29f587SMatthias Ringwald (void)argc; 285*aa29f587SMatthias Ringwald (void)argv; 286*aa29f587SMatthias Ringwald 287*aa29f587SMatthias Ringwald // register for HCI events 288*aa29f587SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 289*aa29f587SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 290*aa29f587SMatthias Ringwald hci_register_sco_packet_handler(&packet_handler); 291*aa29f587SMatthias Ringwald 292*aa29f587SMatthias Ringwald gap_discoverable_control(1); 293*aa29f587SMatthias Ringwald gap_set_class_of_device(0x2540); 294*aa29f587SMatthias Ringwald gap_set_local_name("HID Mouse Demo 00:00:00:00:00:00"); 295*aa29f587SMatthias Ringwald 296*aa29f587SMatthias Ringwald // L2CAP 297*aa29f587SMatthias Ringwald l2cap_init(); 298*aa29f587SMatthias Ringwald 299*aa29f587SMatthias Ringwald // SDP Server 300*aa29f587SMatthias Ringwald sdp_init(); 301*aa29f587SMatthias Ringwald memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 302*aa29f587SMatthias Ringwald // hid sevice subclass 2540 Keyboard, hid counntry code 33 US, hid virtual cable off, hid reconnect initiate off, hid boot device off 303*aa29f587SMatthias Ringwald hid_create_sdp_record(hid_service_buffer, 0x10001, 0x2540, 33, 0, 0, 0, hid_descriptor_mouse_boot_mode, sizeof(hid_descriptor_mouse_boot_mode), hid_device_name); 304*aa29f587SMatthias Ringwald printf("SDP service record size: %u\n", de_get_len( hid_service_buffer)); 305*aa29f587SMatthias Ringwald sdp_register_service(hid_service_buffer); 306*aa29f587SMatthias Ringwald 307*aa29f587SMatthias Ringwald // HID Device 308*aa29f587SMatthias Ringwald hid_device_init(); 309*aa29f587SMatthias Ringwald hid_device_register_packet_handler(&packet_handler); 310*aa29f587SMatthias Ringwald 311*aa29f587SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 312*aa29f587SMatthias Ringwald btstack_stdin_setup(stdin_process); 313*aa29f587SMatthias Ringwald #endif 314*aa29f587SMatthias Ringwald // turn on! 315*aa29f587SMatthias Ringwald hci_power_control(HCI_POWER_ON); 316*aa29f587SMatthias Ringwald return 0; 317*aa29f587SMatthias Ringwald } 318*aa29f587SMatthias Ringwald /* LISTING_END */ 319*aa29f587SMatthias Ringwald /* EXAMPLE_END */ 320