xref: /btstack/example/gap_dedicated_bonding.c (revision 1284e348c4a9af0a7045ffa643836aadd5c64b93)
1bcf00d8fSMatthias Ringwald /*
2bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bcf00d8fSMatthias Ringwald  *
4bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bcf00d8fSMatthias Ringwald  * are met:
7bcf00d8fSMatthias Ringwald  *
8bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bcf00d8fSMatthias Ringwald  *    monetary gain.
19bcf00d8fSMatthias Ringwald  *
20bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bcf00d8fSMatthias 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,
25bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32bcf00d8fSMatthias Ringwald  *
33bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34bcf00d8fSMatthias Ringwald  * [email protected]
35bcf00d8fSMatthias Ringwald  *
36bcf00d8fSMatthias Ringwald  */
37bcf00d8fSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gap_dedicated_bonding.c"
39ab2c6ae4SMatthias Ringwald 
40bcf00d8fSMatthias Ringwald #include <stdint.h>
41bcf00d8fSMatthias Ringwald #include <stdio.h>
42bcf00d8fSMatthias Ringwald #include <stdlib.h>
43bcf00d8fSMatthias Ringwald #include <string.h>
44bcf00d8fSMatthias Ringwald 
450e2df43fSMatthias Ringwald #include "btstack.h"
46bcf00d8fSMatthias Ringwald 
47*1284e348SMatthias Ringwald static const char * device_addr_string = "00:1A:7D:DA:71:03";
48*1284e348SMatthias Ringwald static bd_addr_t device_addr;
49*1284e348SMatthias Ringwald 
50*1284e348SMatthias Ringwald static const int mitm_protection_required = 0;
51bcf00d8fSMatthias Ringwald 
52bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
53bcf00d8fSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)54bcf00d8fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
559ec2630cSMatthias Ringwald     UNUSED(channel);
569ec2630cSMatthias Ringwald     UNUSED(size);
579ec2630cSMatthias Ringwald 
58bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
59bcf00d8fSMatthias Ringwald 
600e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
61bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
62d356a6daSMatthias Ringwald             // BTstack activated, get started
63fb42b6e5SMilanka Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
64*1284e348SMatthias Ringwald                 printf("GAP Dedicated Bonding to %s\n", bd_addr_to_str(device_addr));
65*1284e348SMatthias Ringwald                 gap_dedicated_bonding(device_addr, mitm_protection_required);
66bcf00d8fSMatthias Ringwald             }
67bcf00d8fSMatthias Ringwald             break;
68bcf00d8fSMatthias Ringwald         case GAP_EVENT_DEDICATED_BONDING_COMPLETED:
69fcd55a0bSMilanka Ringwald             printf("GAP Dedicated Bonding Complete, status 0x%02x\n", packet[2]);
706058cb0dSMatthias Ringwald             break;
71bcf00d8fSMatthias Ringwald         default:
72bcf00d8fSMatthias Ringwald             break;
73bcf00d8fSMatthias Ringwald     }
74bcf00d8fSMatthias Ringwald }
75bcf00d8fSMatthias Ringwald 
76bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])77bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
789ec2630cSMatthias Ringwald     (void)argc;
799ec2630cSMatthias Ringwald     (void)argv;
80bcf00d8fSMatthias Ringwald 
81bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
82bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
83bcf00d8fSMatthias Ringwald 
848c9bb29eSMatthias Ringwald     l2cap_init();
858c9bb29eSMatthias Ringwald 
868c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE
878c9bb29eSMatthias Ringwald     // Initialize LE Security Manager. Needed for cross-transport key derivation
888c9bb29eSMatthias Ringwald     sm_init();
898c9bb29eSMatthias Ringwald #endif
908c9bb29eSMatthias Ringwald 
91*1284e348SMatthias Ringwald     // parse human readable device address
92*1284e348SMatthias Ringwald     sscanf_bd_addr(device_addr_string, device_addr);
93*1284e348SMatthias Ringwald 
94bcf00d8fSMatthias Ringwald     // turn on!
95bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
96bcf00d8fSMatthias Ringwald 
97bcf00d8fSMatthias Ringwald     return 0;
98bcf00d8fSMatthias Ringwald }
99