xref: /btstack/example/sm_pairing_peripheral.c (revision d7ee901afc7e69dda611280634e1de34a0c0349a)
1*d7ee901aSMatthias Ringwald /*
2*d7ee901aSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*d7ee901aSMatthias Ringwald  *
4*d7ee901aSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*d7ee901aSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*d7ee901aSMatthias Ringwald  * are met:
7*d7ee901aSMatthias Ringwald  *
8*d7ee901aSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*d7ee901aSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*d7ee901aSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*d7ee901aSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*d7ee901aSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*d7ee901aSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*d7ee901aSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*d7ee901aSMatthias Ringwald  *    from this software without specific prior written permission.
16*d7ee901aSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*d7ee901aSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*d7ee901aSMatthias Ringwald  *    monetary gain.
19*d7ee901aSMatthias Ringwald  *
20*d7ee901aSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*d7ee901aSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*d7ee901aSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*d7ee901aSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*d7ee901aSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*d7ee901aSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*d7ee901aSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*d7ee901aSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*d7ee901aSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*d7ee901aSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*d7ee901aSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*d7ee901aSMatthias Ringwald  * SUCH DAMAGE.
32*d7ee901aSMatthias Ringwald  *
33*d7ee901aSMatthias Ringwald  * Please inquire about commercial licensing options at
34*d7ee901aSMatthias Ringwald  * [email protected]
35*d7ee901aSMatthias Ringwald  *
36*d7ee901aSMatthias Ringwald  */
37*d7ee901aSMatthias Ringwald 
38*d7ee901aSMatthias Ringwald // *****************************************************************************
39*d7ee901aSMatthias Ringwald /* EXAMPLE_START(sm_pairing_peripheral): LE Peripheral - Test pairing combinations
40*d7ee901aSMatthias Ringwald  *
41*d7ee901aSMatthias Ringwald  * @text Depending on the Authentication requiremens and IO Capabilities,
42*d7ee901aSMatthias Ringwald  * the pairing process uses different short and long term key generation method.
43*d7ee901aSMatthias Ringwald  * This example helps explore the different options incl. LE Secure Connections.
44*d7ee901aSMatthias Ringwald  */
45*d7ee901aSMatthias Ringwald  // *****************************************************************************
46*d7ee901aSMatthias Ringwald 
47*d7ee901aSMatthias Ringwald #include <stdint.h>
48*d7ee901aSMatthias Ringwald #include <stdio.h>
49*d7ee901aSMatthias Ringwald #include <stdlib.h>
50*d7ee901aSMatthias Ringwald #include <string.h>
51*d7ee901aSMatthias Ringwald 
52*d7ee901aSMatthias Ringwald #include "sm_pairing_peripheral.h"
53*d7ee901aSMatthias Ringwald #include "btstack.h"
54*d7ee901aSMatthias Ringwald 
55*d7ee901aSMatthias Ringwald /* @section Main Application Setup
56*d7ee901aSMatthias Ringwald  *
57*d7ee901aSMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
58*d7ee901aSMatthias Ringwald  * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
59*d7ee901aSMatthias Ringwald  * ATT Database generated from $sm_pairing_peripheral.gatt$. Finally, it configures the advertisements
60*d7ee901aSMatthias Ringwald  * and boots the Bluetooth stack.
61*d7ee901aSMatthias Ringwald  * In this example, the Advertisement contains the Flags attribute and the device name.
62*d7ee901aSMatthias Ringwald  * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
63*d7ee901aSMatthias Ringwald  * Various examples for IO Capabilites and Authentication Requirements are given below.
64*d7ee901aSMatthias Ringwald  */
65*d7ee901aSMatthias Ringwald 
66*d7ee901aSMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */
67*d7ee901aSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
68*d7ee901aSMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
69*d7ee901aSMatthias Ringwald // static hci_con_handle_t con_handle;
70*d7ee901aSMatthias Ringwald 
71*d7ee901aSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
72*d7ee901aSMatthias Ringwald 
73*d7ee901aSMatthias Ringwald const uint8_t adv_data[] = {
74*d7ee901aSMatthias Ringwald     // Flags general discoverable, BR/EDR not supported
75*d7ee901aSMatthias Ringwald     0x02, 0x01, 0x06,
76*d7ee901aSMatthias Ringwald     // Name
77*d7ee901aSMatthias Ringwald     0x0b, 0x09, 'S', 'M', ' ', 'P', 'a', 'i', 'r', 'i', 'n', 'g',
78*d7ee901aSMatthias Ringwald };
79*d7ee901aSMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data);
80*d7ee901aSMatthias Ringwald 
81*d7ee901aSMatthias Ringwald static void sm_peripheral_setup(void){
82*d7ee901aSMatthias Ringwald 
83*d7ee901aSMatthias Ringwald     // register for HCI events
84*d7ee901aSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
85*d7ee901aSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
86*d7ee901aSMatthias Ringwald 
87*d7ee901aSMatthias Ringwald     l2cap_init();
88*d7ee901aSMatthias Ringwald 
89*d7ee901aSMatthias Ringwald     // setup le device db
90*d7ee901aSMatthias Ringwald     le_device_db_init();
91*d7ee901aSMatthias Ringwald 
92*d7ee901aSMatthias Ringwald     // setup SM: Display only
93*d7ee901aSMatthias Ringwald     sm_init();
94*d7ee901aSMatthias Ringwald     sm_event_callback_registration.callback = &packet_handler;
95*d7ee901aSMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
96*d7ee901aSMatthias Ringwald 
97*d7ee901aSMatthias Ringwald     /**
98*d7ee901aSMatthias Ringwald      * Choose ONE of the following configurations
99*d7ee901aSMatthias Ringwald      */
100*d7ee901aSMatthias Ringwald 
101*d7ee901aSMatthias Ringwald     // LE Legacy Pairing, Just Works
102*d7ee901aSMatthias Ringwald     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
103*d7ee901aSMatthias Ringwald     sm_set_authentication_requirements(0);
104*d7ee901aSMatthias Ringwald 
105*d7ee901aSMatthias Ringwald     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
106*d7ee901aSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
107*d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_MITM_PROTECTION);
108*d7ee901aSMatthias Ringwald 
109*d7ee901aSMatthias Ringwald #ifdef ENABLE_LE_SECURE_CONNECTIONS
110*d7ee901aSMatthias Ringwald     // LE Secure Connetions, Just Works
111*d7ee901aSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
112*d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION);
113*d7ee901aSMatthias Ringwald 
114*d7ee901aSMatthias Ringwald     // LE Secure Connections, Numeric Comparison
115*d7ee901aSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
116*d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
117*d7ee901aSMatthias Ringwald 
118*d7ee901aSMatthias Ringwald     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
119*d7ee901aSMatthias Ringwald     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
120*d7ee901aSMatthias Ringwald     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
121*d7ee901aSMatthias Ringwald #endif
122*d7ee901aSMatthias Ringwald 
123*d7ee901aSMatthias Ringwald     // setup ATT server
124*d7ee901aSMatthias Ringwald     att_server_init(profile_data, NULL, NULL);
125*d7ee901aSMatthias Ringwald     att_server_register_packet_handler(packet_handler);
126*d7ee901aSMatthias Ringwald 
127*d7ee901aSMatthias Ringwald     // setup advertisements
128*d7ee901aSMatthias Ringwald     uint16_t adv_int_min = 0x0030;
129*d7ee901aSMatthias Ringwald     uint16_t adv_int_max = 0x0030;
130*d7ee901aSMatthias Ringwald     uint8_t adv_type = 0;
131*d7ee901aSMatthias Ringwald     bd_addr_t null_addr;
132*d7ee901aSMatthias Ringwald     memset(null_addr, 0, 6);
133*d7ee901aSMatthias Ringwald     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
134*d7ee901aSMatthias Ringwald     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
135*d7ee901aSMatthias Ringwald     gap_advertisements_enable(1);
136*d7ee901aSMatthias Ringwald }
137*d7ee901aSMatthias Ringwald 
138*d7ee901aSMatthias Ringwald /* LISTING_END */
139*d7ee901aSMatthias Ringwald 
140*d7ee901aSMatthias Ringwald /*
141*d7ee901aSMatthias Ringwald  * @section Packet Handler
142*d7ee901aSMatthias Ringwald  *
143*d7ee901aSMatthias Ringwald  * @text The packet handler is used to:
144*d7ee901aSMatthias Ringwald  *        - stop the counter after a disconnect
145*d7ee901aSMatthias Ringwald  *        - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received
146*d7ee901aSMatthias Ringwald  */
147*d7ee901aSMatthias Ringwald 
148*d7ee901aSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */
149*d7ee901aSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
150*d7ee901aSMatthias Ringwald     switch (packet_type) {
151*d7ee901aSMatthias Ringwald         case HCI_EVENT_PACKET:
152*d7ee901aSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
153*d7ee901aSMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
154*d7ee901aSMatthias Ringwald                     printf("Just Works requested\n");
155*d7ee901aSMatthias Ringwald                     sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
156*d7ee901aSMatthias Ringwald                     break;
157*d7ee901aSMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
158*d7ee901aSMatthias Ringwald                     printf("Confirming numeric comparison: %u\n", sm_event_numeric_comparison_request_get_passkey(packet));
159*d7ee901aSMatthias Ringwald                     sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
160*d7ee901aSMatthias Ringwald                     break;
161*d7ee901aSMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
162*d7ee901aSMatthias Ringwald                     printf("Display Passkey: %u\n", sm_event_passkey_display_number_get_passkey(packet));
163*d7ee901aSMatthias Ringwald                     break;
164*d7ee901aSMatthias Ringwald             }
165*d7ee901aSMatthias Ringwald             break;
166*d7ee901aSMatthias Ringwald     }
167*d7ee901aSMatthias Ringwald }
168*d7ee901aSMatthias Ringwald /* LISTING_END */
169*d7ee901aSMatthias Ringwald 
170*d7ee901aSMatthias Ringwald int btstack_main(void);
171*d7ee901aSMatthias Ringwald int btstack_main(void)
172*d7ee901aSMatthias Ringwald {
173*d7ee901aSMatthias Ringwald     sm_peripheral_setup();
174*d7ee901aSMatthias Ringwald 
175*d7ee901aSMatthias Ringwald     // turn on!
176*d7ee901aSMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
177*d7ee901aSMatthias Ringwald 
178*d7ee901aSMatthias Ringwald     return 0;
179*d7ee901aSMatthias Ringwald }
180*d7ee901aSMatthias Ringwald /* EXAMPLE_END */
181