xref: /btstack/example/hog_mouse_demo.c (revision 4cf49978783c0210e70100601dff72ad36d7de21)
1fbf7b2f3SMatthias Ringwald /*
2fbf7b2f3SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3fbf7b2f3SMatthias Ringwald  *
4fbf7b2f3SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5fbf7b2f3SMatthias Ringwald  * modification, are permitted provided that the following conditions
6fbf7b2f3SMatthias Ringwald  * are met:
7fbf7b2f3SMatthias Ringwald  *
8fbf7b2f3SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9fbf7b2f3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10fbf7b2f3SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11fbf7b2f3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12fbf7b2f3SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13fbf7b2f3SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14fbf7b2f3SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15fbf7b2f3SMatthias Ringwald  *    from this software without specific prior written permission.
16fbf7b2f3SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17fbf7b2f3SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18fbf7b2f3SMatthias Ringwald  *    monetary gain.
19fbf7b2f3SMatthias Ringwald  *
20fbf7b2f3SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21fbf7b2f3SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22fbf7b2f3SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25fbf7b2f3SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26fbf7b2f3SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27fbf7b2f3SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28fbf7b2f3SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29fbf7b2f3SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30fbf7b2f3SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31fbf7b2f3SMatthias Ringwald  * SUCH DAMAGE.
32fbf7b2f3SMatthias Ringwald  *
33fbf7b2f3SMatthias Ringwald  * Please inquire about commercial licensing options at
34fbf7b2f3SMatthias Ringwald  * [email protected]
35fbf7b2f3SMatthias Ringwald  *
36fbf7b2f3SMatthias Ringwald  */
37fbf7b2f3SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hog_mouse_demo.c"
39fbf7b2f3SMatthias Ringwald 
40fbf7b2f3SMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(hog_mouse_demo): HID Mouse LE
42fbf7b2f3SMatthias Ringwald  */
43fbf7b2f3SMatthias Ringwald // *****************************************************************************
44fbf7b2f3SMatthias Ringwald 
45fbf7b2f3SMatthias Ringwald #include <stdint.h>
46fbf7b2f3SMatthias Ringwald #include <stdio.h>
47fbf7b2f3SMatthias Ringwald #include <stdlib.h>
48fbf7b2f3SMatthias Ringwald #include <string.h>
49fbf7b2f3SMatthias Ringwald #include <inttypes.h>
50fbf7b2f3SMatthias Ringwald 
51fbf7b2f3SMatthias Ringwald #include "hog_mouse_demo.h"
52fbf7b2f3SMatthias Ringwald 
53fbf7b2f3SMatthias Ringwald #include "btstack.h"
54fbf7b2f3SMatthias Ringwald 
55fbf7b2f3SMatthias Ringwald #include "ble/gatt-service/battery_service_server.h"
56fbf7b2f3SMatthias Ringwald #include "ble/gatt-service/device_information_service_server.h"
57fbf7b2f3SMatthias Ringwald #include "ble/gatt-service/hids_device.h"
58fbf7b2f3SMatthias Ringwald 
59fbf7b2f3SMatthias Ringwald // from USB HID Specification 1.1, Appendix B.2
60fbf7b2f3SMatthias Ringwald const uint8_t hid_descriptor_mouse_boot_mode[] = {
61fbf7b2f3SMatthias Ringwald     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
62fbf7b2f3SMatthias Ringwald     0x09, 0x02,                    // USAGE (Mouse)
63fbf7b2f3SMatthias Ringwald     0xa1, 0x01,                    // COLLECTION (Application)
64fbf7b2f3SMatthias Ringwald 
65fbf7b2f3SMatthias Ringwald     0x85,  0x01,                    // Report ID 1
66fbf7b2f3SMatthias Ringwald 
67fbf7b2f3SMatthias Ringwald     0x09, 0x01,                    //   USAGE (Pointer)
68fbf7b2f3SMatthias Ringwald 
69fbf7b2f3SMatthias Ringwald     0xa1, 0x00,                    //   COLLECTION (Physical)
70fbf7b2f3SMatthias Ringwald 
71fbf7b2f3SMatthias Ringwald #if 1
72fbf7b2f3SMatthias Ringwald     0x05, 0x09,                    //     USAGE_PAGE (Button)
73fbf7b2f3SMatthias Ringwald     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
74fbf7b2f3SMatthias Ringwald     0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
75fbf7b2f3SMatthias Ringwald     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
76fbf7b2f3SMatthias Ringwald     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
77fbf7b2f3SMatthias Ringwald     0x95, 0x03,                    //     REPORT_COUNT (3)
78fbf7b2f3SMatthias Ringwald     0x75, 0x01,                    //     REPORT_SIZE (1)
79fbf7b2f3SMatthias Ringwald     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
80fbf7b2f3SMatthias Ringwald     0x95, 0x01,                    //     REPORT_COUNT (1)
81fbf7b2f3SMatthias Ringwald     0x75, 0x05,                    //     REPORT_SIZE (5)
82fbf7b2f3SMatthias Ringwald     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
83fbf7b2f3SMatthias Ringwald #endif
84fbf7b2f3SMatthias Ringwald 
85fbf7b2f3SMatthias Ringwald #if 1
86fbf7b2f3SMatthias Ringwald     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
87fbf7b2f3SMatthias Ringwald     0x09, 0x30,                    //     USAGE (X)
88fbf7b2f3SMatthias Ringwald     0x09, 0x31,                    //     USAGE (Y)
89fbf7b2f3SMatthias Ringwald     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
90fbf7b2f3SMatthias Ringwald     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
91fbf7b2f3SMatthias Ringwald     0x75, 0x08,                    //     REPORT_SIZE (8)
92fbf7b2f3SMatthias Ringwald     0x95, 0x02,                    //     REPORT_COUNT (2)
93fbf7b2f3SMatthias Ringwald     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
94fbf7b2f3SMatthias Ringwald #endif
95fbf7b2f3SMatthias Ringwald 
96fbf7b2f3SMatthias Ringwald     0xc0,                          //   END_COLLECTION
97fbf7b2f3SMatthias Ringwald     0xc0                           // END_COLLECTION
98fbf7b2f3SMatthias Ringwald };
99fbf7b2f3SMatthias Ringwald 
100fbf7b2f3SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
101441a7ef1SMatthias Ringwald static btstack_packet_callback_registration_t l2cap_event_callback_registration;
102fbf7b2f3SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
103fbf7b2f3SMatthias Ringwald static uint8_t battery = 100;
104fbf7b2f3SMatthias Ringwald static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
10550304921SMatthias Ringwald static uint8_t protocol_mode = 1;
106fbf7b2f3SMatthias Ringwald 
107fbf7b2f3SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
108fbf7b2f3SMatthias Ringwald 
109fbf7b2f3SMatthias Ringwald const uint8_t adv_data[] = {
110fbf7b2f3SMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
111fbf7b2f3SMatthias Ringwald     0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
112fbf7b2f3SMatthias Ringwald     // Name
113fbf7b2f3SMatthias Ringwald     0x0a, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'H', 'I', 'D', ' ', 'M', 'o', 'u', 's', 'e',
114fbf7b2f3SMatthias Ringwald     // 16-bit Service UUIDs
115fbf7b2f3SMatthias Ringwald     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,
116ae856536SMatthias Ringwald     // Appearance HID - Mouse (Category 15, Sub-Category 2)
117ae856536SMatthias Ringwald     0x03, BLUETOOTH_DATA_TYPE_APPEARANCE, 0xC2, 0x03,
118fbf7b2f3SMatthias Ringwald };
119fbf7b2f3SMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
120fbf7b2f3SMatthias Ringwald 
hog_mouse_setup(void)121fbf7b2f3SMatthias Ringwald static void hog_mouse_setup(void){
122fbf7b2f3SMatthias Ringwald 
123441a7ef1SMatthias Ringwald     // setup l2cap and
124fbf7b2f3SMatthias Ringwald     l2cap_init();
125fbf7b2f3SMatthias Ringwald 
126fbf7b2f3SMatthias Ringwald     // setup SM: Display only
127fbf7b2f3SMatthias Ringwald     sm_init();
128*4cf49978SMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
129*4cf49978SMatthias Ringwald     sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
130fbf7b2f3SMatthias Ringwald 
131fbf7b2f3SMatthias Ringwald     // setup ATT server
132fbf7b2f3SMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
133fbf7b2f3SMatthias Ringwald 
134fbf7b2f3SMatthias Ringwald     // setup battery service
135fbf7b2f3SMatthias Ringwald     battery_service_server_init(battery);
136fbf7b2f3SMatthias Ringwald 
137fbf7b2f3SMatthias Ringwald     // setup device information service
138fbf7b2f3SMatthias Ringwald     device_information_service_server_init();
139fbf7b2f3SMatthias Ringwald 
140fbf7b2f3SMatthias Ringwald     // setup HID Device service
141fbf7b2f3SMatthias Ringwald     hids_device_init(0, hid_descriptor_mouse_boot_mode, sizeof(hid_descriptor_mouse_boot_mode));
142fbf7b2f3SMatthias Ringwald 
143fbf7b2f3SMatthias Ringwald     // setup advertisements
144fbf7b2f3SMatthias Ringwald     uint16_t adv_int_min = 0x0030;
145fbf7b2f3SMatthias Ringwald     uint16_t adv_int_max = 0x0030;
146fbf7b2f3SMatthias Ringwald     uint8_t adv_type = 0;
147fbf7b2f3SMatthias Ringwald     bd_addr_t null_addr;
148fbf7b2f3SMatthias Ringwald     memset(null_addr, 0, 6);
149fbf7b2f3SMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
150fbf7b2f3SMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
151fbf7b2f3SMatthias Ringwald     gap_advertisements_enable(1);
152a4fe6467SMatthias Ringwald 
153a4fe6467SMatthias Ringwald     // register for events
154a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
155a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
156a4fe6467SMatthias Ringwald 
157441a7ef1SMatthias Ringwald     // register for connection parameter updates
158441a7ef1SMatthias Ringwald     l2cap_event_callback_registration.callback = &packet_handler;
159441a7ef1SMatthias Ringwald     l2cap_add_event_handler(&l2cap_event_callback_registration);
160441a7ef1SMatthias Ringwald 
161a4fe6467SMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
162a4fe6467SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
163a4fe6467SMatthias Ringwald 
164a4fe6467SMatthias Ringwald     hids_device_register_packet_handler(packet_handler);
165fbf7b2f3SMatthias Ringwald }
166fbf7b2f3SMatthias Ringwald 
167fbf7b2f3SMatthias Ringwald // HID Report sending
send_report(uint8_t buttons,int8_t dx,int8_t dy)168fbf7b2f3SMatthias Ringwald static void send_report(uint8_t buttons, int8_t dx, int8_t dy){
1692abb034cSMatthias Ringwald     uint8_t report[] = { buttons, (uint8_t) dx, (uint8_t) dy };
17050304921SMatthias Ringwald     switch (protocol_mode){
17150304921SMatthias Ringwald         case 0:
17250304921SMatthias Ringwald             hids_device_send_boot_mouse_input_report(con_handle, report, sizeof(report));
173555f545dSMatthias Ringwald             break;
17450304921SMatthias Ringwald         case 1:
175fbf7b2f3SMatthias Ringwald             hids_device_send_input_report(con_handle, report, sizeof(report));
17650304921SMatthias Ringwald             break;
17750304921SMatthias Ringwald         default:
17850304921SMatthias Ringwald             break;
17950304921SMatthias Ringwald     }
180fbf7b2f3SMatthias Ringwald     printf("Mouse: %d/%d - buttons: %02x\n", dx, dy, buttons);
181fbf7b2f3SMatthias Ringwald }
182fbf7b2f3SMatthias Ringwald 
183fbf7b2f3SMatthias Ringwald static int dx;
184fbf7b2f3SMatthias Ringwald static int dy;
185fbf7b2f3SMatthias Ringwald static uint8_t buttons;
186fbf7b2f3SMatthias Ringwald 
mousing_can_send_now(void)187fbf7b2f3SMatthias Ringwald static void mousing_can_send_now(void){
188fbf7b2f3SMatthias Ringwald     send_report(buttons, dx, dy);
189fbf7b2f3SMatthias Ringwald     // reset
190fbf7b2f3SMatthias Ringwald     dx = 0;
191fbf7b2f3SMatthias Ringwald     dy = 0;
192fbf7b2f3SMatthias Ringwald     if (buttons){
193fbf7b2f3SMatthias Ringwald         buttons = 0;
194fbf7b2f3SMatthias Ringwald         hids_device_request_can_send_now_event(con_handle);
195fbf7b2f3SMatthias Ringwald     }
196fbf7b2f3SMatthias Ringwald }
197fbf7b2f3SMatthias Ringwald 
198fbf7b2f3SMatthias Ringwald // Demo Application
199fbf7b2f3SMatthias Ringwald 
200fbf7b2f3SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
201fbf7b2f3SMatthias Ringwald 
202fbf7b2f3SMatthias Ringwald static const int MOUSE_SPEED = 30;
203fbf7b2f3SMatthias Ringwald 
204fbf7b2f3SMatthias Ringwald // On systems with STDIN, we can directly type on the console
205fbf7b2f3SMatthias Ringwald 
stdin_process(char character)206fbf7b2f3SMatthias Ringwald static void stdin_process(char character){
207fbf7b2f3SMatthias Ringwald 
208fbf7b2f3SMatthias Ringwald     if (con_handle == HCI_CON_HANDLE_INVALID) {
209fbf7b2f3SMatthias Ringwald         printf("Mouse not connected, ignoring '%c'\n", character);
210fbf7b2f3SMatthias Ringwald         return;
211fbf7b2f3SMatthias Ringwald     }
212fbf7b2f3SMatthias Ringwald 
213fbf7b2f3SMatthias Ringwald     switch (character){
214fbf7b2f3SMatthias Ringwald         case 'a':
215fbf7b2f3SMatthias Ringwald             dx -= MOUSE_SPEED;
216fbf7b2f3SMatthias Ringwald             break;
217fbf7b2f3SMatthias Ringwald         case 's':
218fbf7b2f3SMatthias Ringwald             dy += MOUSE_SPEED;
219fbf7b2f3SMatthias Ringwald             break;
220fbf7b2f3SMatthias Ringwald         case 'd':
221fbf7b2f3SMatthias Ringwald             dx += MOUSE_SPEED;
222fbf7b2f3SMatthias Ringwald             break;
223fbf7b2f3SMatthias Ringwald         case 'w':
224fbf7b2f3SMatthias Ringwald             dy -= MOUSE_SPEED;
225fbf7b2f3SMatthias Ringwald             break;
226fbf7b2f3SMatthias Ringwald         case 'l':
227fbf7b2f3SMatthias Ringwald             buttons |= 1;
228fbf7b2f3SMatthias Ringwald             break;
229fbf7b2f3SMatthias Ringwald         case 'r':
230fbf7b2f3SMatthias Ringwald             buttons |= 2;
231fbf7b2f3SMatthias Ringwald             break;
232fbf7b2f3SMatthias Ringwald         default:
233fbf7b2f3SMatthias Ringwald             return;
234fbf7b2f3SMatthias Ringwald     }
235fbf7b2f3SMatthias Ringwald     hids_device_request_can_send_now_event(con_handle);
236fbf7b2f3SMatthias Ringwald }
237fbf7b2f3SMatthias Ringwald 
238fbf7b2f3SMatthias Ringwald #else
239fbf7b2f3SMatthias Ringwald 
240fbf7b2f3SMatthias Ringwald // On embedded systems, simulate clicking on 4 corners of a square
241fbf7b2f3SMatthias Ringwald 
242fbf7b2f3SMatthias Ringwald #define MOUSE_PERIOD_MS 15
243fbf7b2f3SMatthias Ringwald 
244fbf7b2f3SMatthias Ringwald static const int STEPS_PER_DIRECTION = 50;
245fbf7b2f3SMatthias Ringwald static const int MOUSE_SPEED = 10;
246fbf7b2f3SMatthias Ringwald 
2475b950925SMatthias Ringwald static btstack_timer_source_t mousing_timer;
2485b950925SMatthias Ringwald static int mousing_active = 0;
2495b950925SMatthias Ringwald static int step;
2505b950925SMatthias Ringwald 
251fbf7b2f3SMatthias Ringwald static struct {
252fbf7b2f3SMatthias Ringwald     int dx;
253fbf7b2f3SMatthias Ringwald     int dy;
254fbf7b2f3SMatthias Ringwald } directions[] = {
255fbf7b2f3SMatthias Ringwald     {  1,  0 },
256fbf7b2f3SMatthias Ringwald     {  0,  1 },
257fbf7b2f3SMatthias Ringwald     { -1,  0 },
258fbf7b2f3SMatthias Ringwald     {  0, -1 },
259fbf7b2f3SMatthias Ringwald };
260fbf7b2f3SMatthias Ringwald 
261fbf7b2f3SMatthias Ringwald 
mousing_timer_handler(btstack_timer_source_t * ts)262fbf7b2f3SMatthias Ringwald static void mousing_timer_handler(btstack_timer_source_t * ts){
263fbf7b2f3SMatthias Ringwald 
2645b950925SMatthias Ringwald     if (con_handle == HCI_CON_HANDLE_INVALID) {
2655b950925SMatthias Ringwald         mousing_active = 0;
2665b950925SMatthias Ringwald         return;
2675b950925SMatthias Ringwald     }
268fbf7b2f3SMatthias Ringwald 
269fbf7b2f3SMatthias Ringwald     // simulate left click when corner reached
270fbf7b2f3SMatthias Ringwald     if (step % STEPS_PER_DIRECTION == 0){
271fbf7b2f3SMatthias Ringwald         buttons |= 1;
272fbf7b2f3SMatthias Ringwald     }
273fbf7b2f3SMatthias Ringwald     // simulate move
274fbf7b2f3SMatthias Ringwald     int direction_index = step / STEPS_PER_DIRECTION;
275fbf7b2f3SMatthias Ringwald     dx += directions[direction_index].dx * MOUSE_SPEED;
276fbf7b2f3SMatthias Ringwald     dy += directions[direction_index].dy * MOUSE_SPEED;
277fbf7b2f3SMatthias Ringwald 
278fbf7b2f3SMatthias Ringwald     // next
279fbf7b2f3SMatthias Ringwald     step++;
280fbf7b2f3SMatthias Ringwald     if (step >= STEPS_PER_DIRECTION * 4) {
281fbf7b2f3SMatthias Ringwald         step = 0;
282fbf7b2f3SMatthias Ringwald     }
283fbf7b2f3SMatthias Ringwald 
284fbf7b2f3SMatthias Ringwald     // trigger send
285fbf7b2f3SMatthias Ringwald     hids_device_request_can_send_now_event(con_handle);
286fbf7b2f3SMatthias Ringwald 
287fbf7b2f3SMatthias Ringwald     // set next timer
288fbf7b2f3SMatthias Ringwald     btstack_run_loop_set_timer(ts, MOUSE_PERIOD_MS);
289fbf7b2f3SMatthias Ringwald     btstack_run_loop_add_timer(ts);
290fbf7b2f3SMatthias Ringwald }
291fbf7b2f3SMatthias Ringwald 
hid_embedded_start_mousing(void)292fbf7b2f3SMatthias Ringwald static void hid_embedded_start_mousing(void){
2935b950925SMatthias Ringwald     if (mousing_active) return;
2945b950925SMatthias Ringwald     mousing_active = 1;
2955b950925SMatthias Ringwald 
296fbf7b2f3SMatthias Ringwald     printf("Start mousing..\n");
297fbf7b2f3SMatthias Ringwald 
298fbf7b2f3SMatthias Ringwald     step = 0;
299fbf7b2f3SMatthias Ringwald 
300fbf7b2f3SMatthias Ringwald     // set one-shot timer
301fbf7b2f3SMatthias Ringwald     mousing_timer.process = &mousing_timer_handler;
302fbf7b2f3SMatthias Ringwald     btstack_run_loop_set_timer(&mousing_timer, MOUSE_PERIOD_MS);
303fbf7b2f3SMatthias Ringwald     btstack_run_loop_add_timer(&mousing_timer);
304fbf7b2f3SMatthias Ringwald }
305fbf7b2f3SMatthias Ringwald #endif
306fbf7b2f3SMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)307fbf7b2f3SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
308fbf7b2f3SMatthias Ringwald     UNUSED(channel);
309fbf7b2f3SMatthias Ringwald     UNUSED(size);
3100830ab8fSMatthias Ringwald     uint16_t conn_interval;
311fbf7b2f3SMatthias Ringwald 
3127bbeb3adSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
3137bbeb3adSMilanka Ringwald 
314fbf7b2f3SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
315fbf7b2f3SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
316d241e50bSMatthias Ringwald             con_handle = HCI_CON_HANDLE_INVALID;
317fbf7b2f3SMatthias Ringwald             printf("Disconnected\n");
318fbf7b2f3SMatthias Ringwald             break;
319fbf7b2f3SMatthias Ringwald         case SM_EVENT_JUST_WORKS_REQUEST:
320fbf7b2f3SMatthias Ringwald             printf("Just Works requested\n");
321fbf7b2f3SMatthias Ringwald             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
322fbf7b2f3SMatthias Ringwald             break;
323fbf7b2f3SMatthias Ringwald         case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
324fbf7b2f3SMatthias Ringwald             printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
325fbf7b2f3SMatthias Ringwald             sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
326fbf7b2f3SMatthias Ringwald             break;
327fbf7b2f3SMatthias Ringwald         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
328fbf7b2f3SMatthias Ringwald             printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
329fbf7b2f3SMatthias Ringwald             break;
3300830ab8fSMatthias Ringwald         case L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE:
3310830ab8fSMatthias Ringwald             printf("L2CAP Connection Parameter Update Complete, response: %x\n", l2cap_event_connection_parameter_update_response_get_result(packet));
3320830ab8fSMatthias Ringwald             break;
333bba48196SMatthias Ringwald         case HCI_EVENT_META_GAP:
334bba48196SMatthias Ringwald             switch (hci_event_gap_meta_get_subevent_code(packet)) {
335bba48196SMatthias Ringwald                 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
3360830ab8fSMatthias Ringwald                     // print connection parameters (without using float operations)
337bba48196SMatthias Ringwald                     conn_interval = gap_subevent_le_connection_complete_get_conn_interval(packet);
3380830ab8fSMatthias Ringwald                     printf("LE Connection Complete:\n");
3390830ab8fSMatthias Ringwald                     printf("- Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
340bba48196SMatthias Ringwald                     printf("- Connection Latency: %u\n", gap_subevent_le_connection_complete_get_conn_latency(packet));
3410830ab8fSMatthias Ringwald                     break;
342bba48196SMatthias Ringwald                 default:
343bba48196SMatthias Ringwald                     break;
344bba48196SMatthias Ringwald             }
345bba48196SMatthias Ringwald             break;
346bba48196SMatthias Ringwald         case HCI_EVENT_LE_META:
347bba48196SMatthias Ringwald             switch (hci_event_le_meta_get_subevent_code(packet)) {
3480830ab8fSMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
3490830ab8fSMatthias Ringwald                     // print connection parameters (without using float operations)
3500830ab8fSMatthias Ringwald                     conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet);
3510830ab8fSMatthias Ringwald                     printf("LE Connection Update:\n");
3520830ab8fSMatthias Ringwald                     printf("- Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3));
3530830ab8fSMatthias Ringwald                     printf("- Connection Latency: %u\n", hci_subevent_le_connection_update_complete_get_conn_latency(packet));
3540830ab8fSMatthias Ringwald                     break;
3550830ab8fSMatthias Ringwald                 default:
3560830ab8fSMatthias Ringwald                     break;
3570830ab8fSMatthias Ringwald             }
3580830ab8fSMatthias Ringwald             break;
359fbf7b2f3SMatthias Ringwald         case HCI_EVENT_HIDS_META:
360fbf7b2f3SMatthias Ringwald             switch (hci_event_hids_meta_get_subevent_code(packet)){
361fbf7b2f3SMatthias Ringwald                 case HIDS_SUBEVENT_INPUT_REPORT_ENABLE:
362fbf7b2f3SMatthias Ringwald                     con_handle = hids_subevent_input_report_enable_get_con_handle(packet);
363fbf7b2f3SMatthias Ringwald                     printf("Report Characteristic Subscribed %u\n", hids_subevent_input_report_enable_get_enable(packet));
364fbf7b2f3SMatthias Ringwald #ifndef HAVE_BTSTACK_STDIN
365fbf7b2f3SMatthias Ringwald                     hid_embedded_start_mousing();
366fbf7b2f3SMatthias Ringwald #endif
3670830ab8fSMatthias Ringwald                     // request connection param update via L2CAP following Apple Bluetooth Design Guidelines
3680830ab8fSMatthias Ringwald                     // gap_request_connection_parameter_update(con_handle, 12, 12, 4, 100);    // 15 ms, 4, 1s
3690830ab8fSMatthias Ringwald 
3700830ab8fSMatthias Ringwald                     // directly update connection params via HCI following Apple Bluetooth Design Guidelines
3710830ab8fSMatthias Ringwald                     // gap_update_connection_parameters(con_handle, 12, 12, 4, 100);    // 60-75 ms, 4, 1s
3720830ab8fSMatthias Ringwald 
373fbf7b2f3SMatthias Ringwald                     break;
374fbf7b2f3SMatthias Ringwald                 case HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE:
37531eaaa99SMatthias Ringwald                     con_handle = hids_subevent_boot_keyboard_input_report_enable_get_con_handle(packet);
376fbf7b2f3SMatthias Ringwald                     printf("Boot Keyboard Characteristic Subscribed %u\n", hids_subevent_boot_keyboard_input_report_enable_get_enable(packet));
377fbf7b2f3SMatthias Ringwald                     break;
378fbf7b2f3SMatthias Ringwald                 case HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE:
37931eaaa99SMatthias Ringwald                     con_handle = hids_subevent_boot_mouse_input_report_enable_get_con_handle(packet);
380fbf7b2f3SMatthias Ringwald                     printf("Boot Mouse Characteristic Subscribed %u\n", hids_subevent_boot_mouse_input_report_enable_get_enable(packet));
381fbf7b2f3SMatthias Ringwald                     break;
382fbf7b2f3SMatthias Ringwald                 case HIDS_SUBEVENT_PROTOCOL_MODE:
38350304921SMatthias Ringwald                     protocol_mode = hids_subevent_protocol_mode_get_protocol_mode(packet);
384fbf7b2f3SMatthias Ringwald                     printf("Protocol Mode: %s mode\n", hids_subevent_protocol_mode_get_protocol_mode(packet) ? "Report" : "Boot");
385fbf7b2f3SMatthias Ringwald                     break;
386fbf7b2f3SMatthias Ringwald                 case HIDS_SUBEVENT_CAN_SEND_NOW:
387fbf7b2f3SMatthias Ringwald                     mousing_can_send_now();
388fbf7b2f3SMatthias Ringwald                     break;
389fbf7b2f3SMatthias Ringwald                 default:
390fbf7b2f3SMatthias Ringwald                     break;
391fbf7b2f3SMatthias Ringwald             }
3927bbeb3adSMilanka Ringwald             break;
3937bbeb3adSMilanka Ringwald 
3947bbeb3adSMilanka Ringwald         default:
395fbf7b2f3SMatthias Ringwald             break;
396fbf7b2f3SMatthias Ringwald     }
397fbf7b2f3SMatthias Ringwald }
398fbf7b2f3SMatthias Ringwald 
399fbf7b2f3SMatthias Ringwald int btstack_main(void);
btstack_main(void)400fbf7b2f3SMatthias Ringwald int btstack_main(void)
401fbf7b2f3SMatthias Ringwald {
402fbf7b2f3SMatthias Ringwald     hog_mouse_setup();
403fbf7b2f3SMatthias Ringwald 
404fbf7b2f3SMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
405fbf7b2f3SMatthias Ringwald     btstack_stdin_setup(stdin_process);
406fbf7b2f3SMatthias Ringwald #endif
407fbf7b2f3SMatthias Ringwald 
408fbf7b2f3SMatthias Ringwald     // turn on!
409fbf7b2f3SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
410fbf7b2f3SMatthias Ringwald 
411fbf7b2f3SMatthias Ringwald     return 0;
412fbf7b2f3SMatthias Ringwald }
413