xref: /btstack/example/sm_pairing_peripheral.c (revision f9f2075ceac5e9dc08e9abea437e43d733a3a0ea)
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 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 // *****************************************************************************
39 /* EXAMPLE_START(sm_pairing_peripheral): LE Peripheral - Test pairing combinations
40  *
41  * @text Depending on the Authentication requiremens and IO Capabilities,
42  * the pairing process uses different short and long term key generation method.
43  * This example helps explore the different options incl. LE Secure Connections.
44  */
45  // *****************************************************************************
46 
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "sm_pairing_peripheral.h"
53 #include "btstack.h"
54 
55 /* @section Main Application Setup
56  *
57  * @text Listing MainConfiguration shows main application code.
58  * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
59  * ATT Database generated from $sm_pairing_peripheral.gatt$. Finally, it configures the advertisements
60  * and boots the Bluetooth stack.
61  * In this example, the Advertisement contains the Flags attribute and the device name.
62  * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
63  * Various examples for IO Capabilites and Authentication Requirements are given below.
64  */
65 
66 /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */
67 static btstack_packet_callback_registration_t hci_event_callback_registration;
68 static btstack_packet_callback_registration_t sm_event_callback_registration;
69 // static hci_con_handle_t con_handle;
70 
71 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
72 
73 const uint8_t adv_data[] = {
74     // Flags general discoverable, BR/EDR not supported
75     0x02, 0x01, 0x06,
76     // Name
77     0x0b, 0x09, 'S', 'M', ' ', 'P', 'a', 'i', 'r', 'i', 'n', 'g',
78 };
79 const uint8_t adv_data_len = sizeof(adv_data);
80 
81 static void sm_peripheral_setup(void){
82 
83     // register for HCI events
84     hci_event_callback_registration.callback = &packet_handler;
85     hci_add_event_handler(&hci_event_callback_registration);
86 
87     l2cap_init();
88 
89     // setup le device db
90     le_device_db_init();
91 
92     // setup SM: Display only
93     sm_init();
94     sm_event_callback_registration.callback = &packet_handler;
95     sm_add_event_handler(&sm_event_callback_registration);
96 
97     /**
98      * Choose ONE of the following configurations
99      */
100 
101     // LE Legacy Pairing, Just Works
102     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
103     sm_set_authentication_requirements(0);
104 
105     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
106     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
107     // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION);
108 
109 #ifdef ENABLE_LE_SECURE_CONNECTIONS
110     // LE Secure Connetions, Just Works
111     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
112     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION);
113 
114     // LE Secure Connections, Numeric Comparison
115     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
116     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
117 
118     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
119     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
120     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
121 #endif
122 
123     // setup ATT server
124     att_server_init(profile_data, NULL, NULL);
125     att_server_register_packet_handler(packet_handler);
126 
127     // setup advertisements
128     uint16_t adv_int_min = 0x0030;
129     uint16_t adv_int_max = 0x0030;
130     uint8_t adv_type = 0;
131     bd_addr_t null_addr;
132     memset(null_addr, 0, 6);
133     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
134     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
135     gap_advertisements_enable(1);
136 }
137 
138 /* LISTING_END */
139 
140 /*
141  * @section Packet Handler
142  *
143  * @text The packet handler is used to:
144  *        - stop the counter after a disconnect
145  *        - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received
146  */
147 
148 /* LISTING_START(packetHandler): Packet Handler */
149 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
150     UNUSED(channel);
151     UNUSED(size);
152 
153     bd_addr_t addr;
154     switch (packet_type) {
155         case HCI_EVENT_PACKET:
156             switch (hci_event_packet_get_type(packet)) {
157                 case SM_EVENT_JUST_WORKS_REQUEST:
158                     printf("Just Works requested\n");
159                     sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
160                     break;
161                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
162                     printf("Confirming numeric comparison: %u\n", sm_event_numeric_comparison_request_get_passkey(packet));
163                     sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
164                     break;
165                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
166                     printf("Display Passkey: %u\n", sm_event_passkey_display_number_get_passkey(packet));
167                     break;
168                 case SM_EVENT_IDENTITY_CREATED:
169                     sm_event_identity_created_get_identity_address(packet, addr);
170                     printf("Identity created: type %u address %s\n", sm_event_identity_created_get_identity_addr_type(packet), bd_addr_to_str(addr));
171                     break;
172                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
173                     sm_event_identity_resolving_succeeded_get_identity_address(packet, addr);
174                     printf("Identity resolved: type %u address %s\n", sm_event_identity_resolving_succeeded_get_identity_addr_type(packet), bd_addr_to_str(addr));
175                     break;
176                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
177                     sm_event_identity_created_get_address(packet, addr);
178                     printf("Identity resolving failed\n");
179                     break;
180             }
181             break;
182     }
183 }
184 /* LISTING_END */
185 
186 int btstack_main(void);
187 int btstack_main(void)
188 {
189     sm_peripheral_setup();
190 
191     // turn on!
192 	hci_power_control(HCI_POWER_ON);
193 
194     return 0;
195 }
196 /* EXAMPLE_END */
197