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