xref: /btstack/example/gap_dedicated_bonding.c (revision e708f863ff0efb99975c34c0c09b6ff827f4c12c)
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 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__ "gap_dedicated_bonding.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 
47 static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};
48 
49 static btstack_packet_callback_registration_t hci_event_callback_registration;
50 
51 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
52     UNUSED(channel);
53     UNUSED(size);
54 
55     if (packet_type != HCI_EVENT_PACKET) return;
56 
57     switch (hci_event_packet_get_type(packet)) {
58         case BTSTACK_EVENT_STATE:
59             // BTstack activated, get started
60             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
61                 gap_dedicated_bonding(remote, 1);
62             }
63             break;
64         case GAP_EVENT_DEDICATED_BONDING_COMPLETED:
65             printf("GAP Dedicated Bonding Complete, status 0x%02x\n", packet[2]);
66             break;
67         default:
68             break;
69     }
70 }
71 
72 int btstack_main(int argc, const char * argv[]);
73 int btstack_main(int argc, const char * argv[]){
74     (void)argc;
75     (void)argv;
76 
77     hci_event_callback_registration.callback = &packet_handler;
78     hci_add_event_handler(&hci_event_callback_registration);
79 
80     l2cap_init();
81 
82 #ifdef ENABLE_BLE
83     // Initialize LE Security Manager. Needed for cross-transport key derivation
84     sm_init();
85 #endif
86 
87     // turn on!
88     hci_power_control(HCI_POWER_ON);
89 
90     return 0;
91 }
92