xref: /btstack/example/hid_host_demo.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
1 /*
2  * Copyright (C) 2017 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_host_demo.c"
39 
40 /*
41  * hid_host_demo.c
42  */
43 
44 /* EXAMPLE_START(hid_host_demo): HID Host Demo
45  *
46  * @text This example implements an HID Host. For now, it connnects to a fixed device, queries the HID SDP
47  * record and opens the HID Control + Interrupt channels
48  */
49 
50 #include <stdio.h>
51 
52 #include "btstack_config.h"
53 #include "btstack.h"
54 
55 #define MAX_ATTRIBUTE_VALUE_SIZE 300
56 
57 // SDP
58 static uint8_t            hid_descriptor[MAX_ATTRIBUTE_VALUE_SIZE];
59 static uint16_t           hid_descriptor_len;
60 
61 static uint16_t           hid_control_psm;
62 static uint16_t           hid_interrupt_psm;
63 
64 static uint8_t            attribute_value[MAX_ATTRIBUTE_VALUE_SIZE];
65 static const unsigned int attribute_value_buffer_size = MAX_ATTRIBUTE_VALUE_SIZE;
66 
67 // L2CAP
68 static uint16_t            l2cap_hid_control_cid;
69 static uint16_t            l2cap_hid_interrupt_cid;
70 
71 // MBP 2016
72 static const char * remote_addr_string = "F4-0F-24-3B-1B-E1";
73 // iMpulse static const char * remote_addr_string = "64:6E:6C:C1:AA:B5";
74 
75 static bd_addr_t remote_addr;
76 
77 static btstack_packet_callback_registration_t hci_event_callback_registration;
78 
79 
80 /* @section Main application configuration
81  *
82  * @text In the application configuration, L2CAP is initialized
83  */
84 
85 /* LISTING_START(PanuSetup): Panu setup */
86 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
87 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
88 
89 static void hid_host_setup(void){
90 
91     // register for HCI events
92     hci_event_callback_registration.callback = &packet_handler;
93     hci_add_event_handler(&hci_event_callback_registration);
94 
95     // Initialize L2CAP
96     l2cap_init();
97 }
98 /* LISTING_END */
99 
100 /* @section SDP parser callback
101  *
102  * @text The SDP parsers retrieves the BNEP PAN UUID as explained in
103  * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}.
104  */
105 
106 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
107 
108     UNUSED(packet_type);
109     UNUSED(channel);
110     UNUSED(size);
111 
112     des_iterator_t attribute_list_it;
113     des_iterator_t additional_des_it;
114     des_iterator_t prot_it;
115     uint8_t       *des_element;
116     uint8_t       *element;
117     uint32_t       uuid;
118     uint8_t        status;
119 
120     switch (hci_event_packet_get_type(packet)){
121         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
122             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
123                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
124                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
125                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
126                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
127                             for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) {
128                                 if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue;
129                                 des_element = des_iterator_get_element(&attribute_list_it);
130                                 des_iterator_init(&prot_it, des_element);
131                                 element = des_iterator_get_element(&prot_it);
132                                 if (de_get_element_type(element) != DE_UUID) continue;
133                                 uuid = de_get_uuid32(element);
134                                 switch (uuid){
135                                     case BLUETOOTH_PROTOCOL_L2CAP:
136                                         if (!des_iterator_has_more(&prot_it)) continue;
137                                         des_iterator_next(&prot_it);
138                                         de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_control_psm);
139                                         printf("HID Control PSM: 0x%04x\n", (int) hid_control_psm);
140                                         break;
141                                     default:
142                                         break;
143                                 }
144                             }
145                             break;
146                         case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
147                             for (des_iterator_init(&attribute_list_it, attribute_value); des_iterator_has_more(&attribute_list_it); des_iterator_next(&attribute_list_it)) {
148                                 if (des_iterator_get_type(&attribute_list_it) != DE_DES) continue;
149                                 des_element = des_iterator_get_element(&attribute_list_it);
150                                 for (des_iterator_init(&additional_des_it, des_element); des_iterator_has_more(&additional_des_it); des_iterator_next(&additional_des_it)) {
151                                     if (des_iterator_get_type(&additional_des_it) != DE_DES) continue;
152                                     des_element = des_iterator_get_element(&additional_des_it);
153                                     des_iterator_init(&prot_it, des_element);
154                                     element = des_iterator_get_element(&prot_it);
155                                     if (de_get_element_type(element) != DE_UUID) continue;
156                                     uuid = de_get_uuid32(element);
157                                     switch (uuid){
158                                         case BLUETOOTH_PROTOCOL_L2CAP:
159                                             if (!des_iterator_has_more(&prot_it)) continue;
160                                             des_iterator_next(&prot_it);
161                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &hid_interrupt_psm);
162                                             printf("HID Interrupt PSM: 0x%04x\n", (int) hid_interrupt_psm);
163                                             break;
164                                         default:
165                                             break;
166                                     }
167                                 }
168                             }
169                             break;
170                         case BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST:
171                             hid_descriptor_len = sdp_event_query_attribute_byte_get_attribute_length(packet);
172                             memcpy(hid_descriptor, attribute_value, hid_descriptor_len);
173                             printf("HID Descriptor:\n");
174                             printf_hexdump(hid_descriptor, hid_descriptor_len);
175                             break;
176                         default:
177                             break;
178                     }
179                 }
180             } else {
181                 fprintf(stderr, "SDP attribute value buffer size exceeded: available %d, required %d\n", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet));
182             }
183             break;
184 
185         case SDP_EVENT_QUERY_COMPLETE:
186             if (!hid_control_psm) {
187                 printf("HID Control PSM missing\n");
188                 break;
189             }
190             if (!hid_interrupt_psm) {
191                 printf("HID Interrupt PSM missing\n");
192                 break;
193             }
194             printf("Setup HID\n");
195             status = l2cap_create_channel(packet_handler, remote_addr, hid_control_psm, 48, &l2cap_hid_control_cid);
196             if (status){
197                 printf("Connecting to HID Control failed: 0x%02x\n", status);
198             }
199             break;
200     }
201 }
202 
203 /*
204  * @section Packet Handler
205  *
206  * @text The packet handler responds to various HCI Events.
207  */
208 
209 /* LISTING_START(packetHandler): Packet Handler */
210 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
211 {
212     /* LISTING_PAUSE */
213     uint8_t   event;
214     bd_addr_t event_addr;
215     uint8_t   status;
216     uint16_t  l2cap_cid;
217 
218     /* LISTING_RESUME */
219     switch (packet_type) {
220 		case HCI_EVENT_PACKET:
221             event = hci_event_packet_get_type(packet);
222             switch (event) {
223                 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
224                  * is received and the example is started in client mode, the remote SDP HID query is started.
225                  */
226                 case BTSTACK_EVENT_STATE:
227                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
228                         printf("Start SDP HID query for remote HID Device.\n");
229                         sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE);
230                     }
231                     break;
232 
233                 /* LISTING_PAUSE */
234                 case HCI_EVENT_PIN_CODE_REQUEST:
235 					// inform about pin code request
236                     printf("Pin code request - using '0000'\n");
237                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
238                     gap_pin_code_response(event_addr, "0000");
239 					break;
240 
241                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
242                     // inform about user confirmation request
243                     printf("SSP User Confirmation Request with numeric value '%06u'\n", little_endian_read_32(packet, 8));
244                     printf("SSP User Confirmation Auto accept\n");
245                     break;
246 
247                 /* LISTING_RESUME */
248 
249                 case L2CAP_EVENT_CHANNEL_OPENED:
250                     status = packet[2];
251                     if (status){
252                         printf("L2CAP Connection failed: 0x%02x\n", status);
253                         break;
254                     }
255                     l2cap_cid  = little_endian_read_16(packet, 13);
256                     if (!l2cap_cid) break;
257                     if (l2cap_cid == l2cap_hid_control_cid){
258                         status = l2cap_create_channel(packet_handler, remote_addr, hid_interrupt_psm, 48, &l2cap_hid_interrupt_cid);
259                         if (status){
260                             printf("Connecting to HID Control failed: 0x%02x\n", status);
261                             break;
262                         }
263                     }
264                     if (l2cap_cid == l2cap_hid_interrupt_cid){
265                         printf("HID Connection established\n");
266                     }
267                     break;
268                 default:
269                     break;
270             }
271             break;
272         case L2CAP_DATA_PACKET:
273             // for now, just dump incoming data
274             if (channel == l2cap_hid_interrupt_cid){
275                 printf("HID Interrupt: ");
276             } else if (channel == l2cap_hid_control_cid){
277                 printf("HID Control: ");
278             } else {
279                 break;
280             }
281             printf_hexdump(packet, size);
282         default:
283             break;
284     }
285 }
286 /* LISTING_END */
287 
288 int btstack_main(int argc, const char * argv[]);
289 int btstack_main(int argc, const char * argv[]){
290 
291     (void)argc;
292     (void)argv;
293 
294     hid_host_setup();
295 
296     // parse human readable Bluetooth address
297     sscanf_bd_addr(remote_addr_string, remote_addr);
298 
299     // Turn on the device
300     hci_power_control(HCI_POWER_ON);
301     return 0;
302 }
303 
304 /* EXAMPLE_END */
305