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