xref: /btstack/example/hog_host_demo.c (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
1 /*
2  * Copyright (C) 2020 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 BLUEKITCHEN
24  * GMBH 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__ "hog_host_demo.c"
39 
40 /*
41  * hog_host_demo.c
42  */
43 
44 /* EXAMPLE_START(hog_host_demo): HID Host LE
45  *
46  * @text This example implements a minimal HID-over-GATT Host. It scans for LE HID devices, connects to it,
47  * discovers the Characteristics relevant for the HID Service and enables Notifications on them.
48  * It then dumps all Boot Keyboard and Mouse Input Reports
49  */
50 
51 #include <inttypes.h>
52 #include <stdio.h>
53 #include <btstack_tlv.h>
54 
55 #include "btstack_config.h"
56 #include "btstack.h"
57 
58 // hog_host_demo.gatt contains the declaration of the provided GATT Services + Characteristics
59 // hog_host_demo.h    contains the binary representation of gatt_browser.gatt
60 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py hog_host_demo.gatt hog_host_demo.h
61 // it needs to be regenerated when the GATT Database declared in gatt_browser.gatt file is modified
62 #include "hog_host_demo.h"
63 
64 // TAG to store remote device address and type in TLV
65 #define TLV_TAG_HOGD ((((uint32_t) 'H') << 24 ) | (((uint32_t) 'O') << 16) | (((uint32_t) 'G') << 8) | 'D')
66 
67 typedef struct {
68     bd_addr_t addr;
69     bd_addr_type_t addr_type;
70 } le_device_addr_t;
71 
72 static enum {
73     W4_WORKING,
74     W4_HID_DEVICE_FOUND,
75     W4_CONNECTED,
76     W4_ENCRYPTED,
77     W4_HID_CLIENT_CONNECTED,
78     READY,
79     W4_TIMEOUT_THEN_SCAN,
80     W4_TIMEOUT_THEN_RECONNECT,
81 } app_state;
82 
83 static le_device_addr_t remote_device;
84 static hci_con_handle_t connection_handle;
85 static uint16_t hids_cid;
86 static hid_protocol_mode_t protocol_mode = HID_PROTOCOL_MODE_REPORT;
87 
88 // SDP
89 static uint8_t hid_descriptor_storage[500];
90 
91 // used to implement connection timeout and reconnect timer
92 static btstack_timer_source_t connection_timer;
93 
94 // register for events from HCI/GAP and SM
95 static btstack_packet_callback_registration_t hci_event_callback_registration;
96 static btstack_packet_callback_registration_t sm_event_callback_registration;
97 
98 // used to store remote device in TLV
99 static const btstack_tlv_t * btstack_tlv_singleton_impl;
100 static void *                btstack_tlv_singleton_context;
101 
102 // Simplified US Keyboard with Shift modifier
103 
104 #define CHAR_ILLEGAL     0xff
105 #define CHAR_RETURN     '\n'
106 #define CHAR_ESCAPE      27
107 #define CHAR_TAB         '\t'
108 #define CHAR_BACKSPACE   0x7f
109 
110 /**
111  * English (US)
112  */
113 static const uint8_t keytable_us_none [] = {
114         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*   0-3 */
115         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',                   /*  4-13 */
116         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',                   /* 14-23 */
117         'u', 'v', 'w', 'x', 'y', 'z',                                       /* 24-29 */
118         '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',                   /* 30-39 */
119         CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
120         '-', '=', '[', ']', '\\', CHAR_ILLEGAL, ';', '\'', 0x60, ',',       /* 45-54 */
121         '.', '/', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
122         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
123         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
124         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
125         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
126         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
127         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
128         '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
129         '6', '7', '8', '9', '0', '.', 0xa7,                                 /* 97-100 */
130 };
131 
132 static const uint8_t keytable_us_shift[] = {
133         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /*  0-3  */
134         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',                   /*  4-13 */
135         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',                   /* 14-23 */
136         'U', 'V', 'W', 'X', 'Y', 'Z',                                       /* 24-29 */
137         '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',                   /* 30-39 */
138         CHAR_RETURN, CHAR_ESCAPE, CHAR_BACKSPACE, CHAR_TAB, ' ',            /* 40-44 */
139         '_', '+', '{', '}', '|', CHAR_ILLEGAL, ':', '"', 0x7E, '<',         /* 45-54 */
140         '>', '?', CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,   /* 55-60 */
141         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 61-64 */
142         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 65-68 */
143         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 69-72 */
144         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 73-76 */
145         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 77-80 */
146         CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL, CHAR_ILLEGAL,             /* 81-84 */
147         '*', '-', '+', '\n', '1', '2', '3', '4', '5',                       /* 85-97 */
148         '6', '7', '8', '9', '0', '.', 0xb1,                                 /* 97-100 */
149 };
150 
151 
152 
153 #define NUM_KEYS 6
154 static uint8_t last_keys[NUM_KEYS];
155 static void hid_handle_input_report(uint8_t service_index, const uint8_t * report, uint16_t report_len){
156     // check if HID Input Report
157 
158     if (report_len < 1) return;
159 
160     btstack_hid_parser_t parser;
161 
162     switch (protocol_mode){
163         case HID_PROTOCOL_MODE_BOOT:
164             btstack_hid_parser_init(&parser,
165                                     btstack_hid_get_boot_descriptor_data(),
166                                     btstack_hid_get_boot_descriptor_len(),
167                                     HID_REPORT_TYPE_INPUT, report, report_len);
168             break;
169 
170         default:
171             btstack_hid_parser_init(&parser,
172                 hids_client_descriptor_storage_get_descriptor_data(hids_cid, service_index),
173                 hids_client_descriptor_storage_get_descriptor_len(hids_cid, service_index),
174                 HID_REPORT_TYPE_INPUT, report, report_len);
175             break;
176 
177     }
178 
179     int shift = 0;
180     uint8_t new_keys[NUM_KEYS];
181     memset(new_keys, 0, sizeof(new_keys));
182     int     new_keys_count = 0;
183     while (btstack_hid_parser_has_more(&parser)){
184         uint16_t usage_page;
185         uint16_t usage;
186         int32_t  value;
187         btstack_hid_parser_get_field(&parser, &usage_page, &usage, &value);
188         if (usage_page != 0x07) continue;
189         switch (usage){
190             case 0xe1:
191             case 0xe6:
192                 if (value){
193                     shift = 1;
194                 }
195                 continue;
196             case 0x00:
197                 continue;
198             default:
199                 break;
200         }
201         if (usage >= sizeof(keytable_us_none)) continue;
202 
203         // store new keys
204         new_keys[new_keys_count++] = (uint8_t) usage;
205 
206         // check if usage was used last time (and ignore in that case)
207         int i;
208         for (i=0;i<NUM_KEYS;i++){
209             if (usage == last_keys[i]){
210                 usage = 0;
211             }
212         }
213         if (usage == 0) continue;
214 
215         uint8_t key;
216         if (shift){
217             key = keytable_us_shift[usage];
218         } else {
219             key = keytable_us_none[usage];
220         }
221         if (key == CHAR_ILLEGAL) continue;
222         if (key == CHAR_BACKSPACE){
223             printf("\b \b");    // go back one char, print space, go back one char again
224             continue;
225         }
226         printf("%c", key);
227     }
228     memcpy(last_keys, new_keys, NUM_KEYS);
229 }
230 
231 /**
232  * @section Test if advertisement contains HID UUID
233  * @param packet
234  * @param size
235  * @returns true if it does
236  */
237 static bool adv_event_contains_hid_service(const uint8_t * packet){
238     const uint8_t * ad_data = gap_event_advertising_report_get_data(packet);
239     uint8_t ad_len = gap_event_advertising_report_get_data_length(packet);
240     return ad_data_contains_uuid16(ad_len, ad_data, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE);
241 }
242 
243 /**
244  * Start scanning
245  */
246 static void hog_start_scan(void){
247     printf("Scanning for LE HID devices...\n");
248     app_state = W4_HID_DEVICE_FOUND;
249     // Passive scanning, 100% (scan interval = scan window)
250     gap_set_scan_parameters(0,48,48);
251     gap_start_scan();
252 }
253 
254 /**
255  * Handle timeout for outgoing connection
256  * @param ts
257  */
258 static void hog_connection_timeout(btstack_timer_source_t * ts){
259     UNUSED(ts);
260     printf("Timeout - abort connection\n");
261     gap_connect_cancel();
262     hog_start_scan();
263 }
264 
265 
266 /**
267  * Connect to remote device but set timer for timeout
268  */
269 static void hog_connect(void) {
270     // set timer
271     btstack_run_loop_set_timer(&connection_timer, 10000);
272     btstack_run_loop_set_timer_handler(&connection_timer, &hog_connection_timeout);
273     btstack_run_loop_add_timer(&connection_timer);
274     app_state = W4_CONNECTED;
275     gap_connect(remote_device.addr, remote_device.addr_type);
276 }
277 
278 /**
279  * Handle timer event to trigger reconnect
280  * @param ts
281  */
282 static void hog_reconnect_timeout(btstack_timer_source_t * ts){
283     UNUSED(ts);
284     switch (app_state){
285         case W4_TIMEOUT_THEN_RECONNECT:
286             hog_connect();
287             break;
288         case W4_TIMEOUT_THEN_SCAN:
289             hog_start_scan();
290             break;
291         default:
292             break;
293     }
294 }
295 
296 /**
297  * Start connecting after boot up: connect to last used device if possible, start scan otherwise
298  */
299 static void hog_start_connect(void){
300     // check if we have a bonded device
301     btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context);
302     if (btstack_tlv_singleton_impl){
303         int len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (uint8_t *) &remote_device, sizeof(remote_device));
304         if (len == sizeof(remote_device)){
305             printf("Bonded, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , bd_addr_to_str(remote_device.addr));
306             hog_connect();
307             return;
308         }
309     }
310     // otherwise, scan for HID devices
311     hog_start_scan();
312 }
313 
314 /**
315  * In case of error, disconnect and start scanning again
316  */
317 static void handle_outgoing_connection_error(void){
318     printf("Error occurred, disconnect and start over\n");
319     gap_disconnect(connection_handle);
320     hog_start_scan();
321 }
322 
323 /**
324  * Handle GATT Client Events dependent on current state
325  *
326  * @param packet_type
327  * @param channel
328  * @param packet
329  * @param size
330  */
331 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
332     UNUSED(packet_type);
333     UNUSED(channel);
334     UNUSED(size);
335 
336     uint8_t status;
337 
338     if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META){
339         return;
340     }
341 
342     switch (hci_event_gattservice_meta_get_subevent_code(packet)){
343         case GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED:
344             status = gattservice_subevent_hid_service_connected_get_status(packet);
345             switch (status){
346                 case ERROR_CODE_SUCCESS:
347                     printf("HID service client connected, found %d services\n",
348                         gattservice_subevent_hid_service_connected_get_num_instances(packet));
349 
350                                         // store device as bonded
351                     if (btstack_tlv_singleton_impl){
352                         btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (const uint8_t *) &remote_device, sizeof(remote_device));
353                     }
354                     // done
355                     printf("Ready - please start typing or mousing..\n");
356                     app_state = READY;
357                     break;
358                 default:
359                     printf("HID service client connection failed, status 0x%02x.\n", status);
360                     handle_outgoing_connection_error();
361                     break;
362             }
363             break;
364 
365         case GATTSERVICE_SUBEVENT_HID_REPORT:
366             hid_handle_input_report(
367                 gattservice_subevent_hid_report_get_service_index(packet),
368                 gattservice_subevent_hid_report_get_report(packet),
369                 gattservice_subevent_hid_report_get_report_len(packet));
370             break;
371 
372         default:
373             break;
374     }
375 }
376 
377 /* LISTING_START(packetHandler): Packet Handler */
378 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
379     /* LISTING_PAUSE */
380     UNUSED(channel);
381     UNUSED(size);
382     uint8_t event;
383     /* LISTING_RESUME */
384     switch (packet_type) {
385         case HCI_EVENT_PACKET:
386             event = hci_event_packet_get_type(packet);
387             switch (event) {
388                 case BTSTACK_EVENT_STATE:
389                     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
390                     btstack_assert(app_state == W4_WORKING);
391 
392                     hog_start_connect();
393                     break;
394                 case GAP_EVENT_ADVERTISING_REPORT:
395                     if (app_state != W4_HID_DEVICE_FOUND) break;
396                     if (adv_event_contains_hid_service(packet) == false) break;
397                     // stop scan
398                     gap_stop_scan();
399                     // store remote device address and type
400                     gap_event_advertising_report_get_address(packet, remote_device.addr);
401                     remote_device.addr_type = gap_event_advertising_report_get_address_type(packet);
402                     // connect
403                     printf("Found, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , bd_addr_to_str(remote_device.addr));
404                     hog_connect();
405                     break;
406                 case HCI_EVENT_DISCONNECTION_COMPLETE:
407                     if (app_state != READY) break;
408                     connection_handle = HCI_CON_HANDLE_INVALID;
409                     switch (app_state){
410                         case READY:
411                             printf("\nDisconnected, try to reconnect...\n");
412                             app_state = W4_TIMEOUT_THEN_RECONNECT;
413                             break;
414                         default:
415                             printf("\nDisconnected, start over...\n");
416                             app_state = W4_TIMEOUT_THEN_SCAN;
417                             break;
418                     }
419                     // set timer
420                     btstack_run_loop_set_timer(&connection_timer, 100);
421                     btstack_run_loop_set_timer_handler(&connection_timer, &hog_reconnect_timeout);
422                     btstack_run_loop_add_timer(&connection_timer);
423                     break;
424                 case HCI_EVENT_META_GAP:
425                     // wait for connection complete
426                     if (hci_event_gap_meta_get_subevent_code(packet) != GAP_SUBEVENT_LE_CONNECTION_COMPLETE) break;
427                     if (app_state != W4_CONNECTED) return;
428                     btstack_run_loop_remove_timer(&connection_timer);
429                     connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
430                     // request security
431                     app_state = W4_ENCRYPTED;
432                     sm_request_pairing(connection_handle);
433                     break;
434                 default:
435                     break;
436             }
437             break;
438         default:
439             break;
440     }
441 }
442 /* LISTING_END */
443 
444 /* @section HCI packet handler
445  *
446  * @text The SM packet handler receives Security Manager Events required for pairing.
447  * It also receives events generated during Identity Resolving
448  * see Listing SMPacketHandler.
449  */
450 
451 /* LISTING_START(SMPacketHandler): Scanning and receiving advertisements */
452 
453 static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
454     UNUSED(channel);
455     UNUSED(size);
456 
457     if (packet_type != HCI_EVENT_PACKET) return;
458 
459     bool connect_to_service = false;
460 
461     switch (hci_event_packet_get_type(packet)) {
462         case SM_EVENT_JUST_WORKS_REQUEST:
463             printf("Just works requested\n");
464             sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
465             break;
466         case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
467             printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
468             sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
469             break;
470         case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
471             printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
472             break;
473         case SM_EVENT_PAIRING_COMPLETE:
474             switch (sm_event_pairing_complete_get_status(packet)){
475                 case ERROR_CODE_SUCCESS:
476                     printf("Pairing complete, success\n");
477                     connect_to_service = true;
478                     break;
479                 case ERROR_CODE_CONNECTION_TIMEOUT:
480                     printf("Pairing failed, timeout\n");
481                     break;
482                 case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
483                     printf("Pairing failed, disconnected\n");
484                     break;
485                 case ERROR_CODE_AUTHENTICATION_FAILURE:
486                     printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet));
487                     break;
488                 default:
489                     break;
490             }
491             break;
492         case SM_EVENT_REENCRYPTION_COMPLETE:
493             printf("Re-encryption complete, success\n");
494             connect_to_service = true;
495             break;
496         default:
497             break;
498     }
499 
500     if (connect_to_service){
501         // continue - query primary services
502         printf("Search for HID service.\n");
503         app_state = W4_HID_CLIENT_CONNECTED;
504         hids_client_connect(connection_handle, handle_gatt_client_event, protocol_mode, &hids_cid);
505     }
506 }
507 /* LISTING_END */
508 
509 int btstack_main(int argc, const char * argv[]);
510 int btstack_main(int argc, const char * argv[]){
511 
512     (void)argc;
513     (void)argv;
514 
515     /* LISTING_START(HogBootHostSetup): HID-over-GATT Host Setup */
516 
517     l2cap_init();
518 
519     // setup SM: Display only
520     sm_init();
521     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
522     sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
523 
524     //
525     gatt_client_init();
526 
527     // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android and iOS
528     att_server_init(profile_data, NULL, NULL);
529 
530     hids_client_init(hid_descriptor_storage, sizeof(hid_descriptor_storage));
531 
532     // register for events from HCI
533     hci_event_callback_registration.callback = &packet_handler;
534     hci_add_event_handler(&hci_event_callback_registration);
535 
536     // register for events from Security Manager
537     sm_event_callback_registration.callback = &sm_packet_handler;
538     sm_add_event_handler(&sm_event_callback_registration);
539     sm_set_authentication_requirements( SM_AUTHREQ_BONDING);
540 
541     /* LISTING_END */
542 
543     // Disable stdout buffering
544 	setvbuf(stdin, NULL, _IONBF, 0);
545 
546     app_state = W4_WORKING;
547 
548     // Turn on the device
549     hci_power_control(HCI_POWER_ON);
550     return 0;
551 }
552 
553 /* EXAMPLE_END */
554