xref: /btstack/src/ble/gatt-service/bond_management_service_server.c (revision 6fad2c376995e272e1924e97046a38cb37cd48b1)
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 #define BTSTACK_FILE__ "bond_management_service_server.c"
39 
40 #include "bluetooth.h"
41 #include "btstack_defines.h"
42 #include "hci.h"
43 #include "gap.h"
44 #include "btstack_util.h"
45 #include "btstack_debug.h"
46 
47 #include "bluetooth_gatt.h"
48 #include "ble/att_db.h"
49 #include "ble/att_server.h"
50 #include "ble/le_device_db.h"
51 
52 #include "ble/gatt-service/bond_management_service_server.h"
53 
54 // characteristic:  Control Point
55 static uint16_t bm_control_point_value_handle;
56 
57 static const char * bm_authorization_string;
58 
59 // characteristic: Feature
60 static uint16_t bm_supported_features_value_handle;
61 static uint32_t bm_supported_features;
62 
63 static att_service_handler_t bond_management_service;
64 
65 #ifdef ENABLE_CLASSIC
66 static void bond_management_delete_bonding_information_classic(hci_connection_t * connection, bool delete_own_bonding, bool delete_all_bonding_but_active){
67     bd_addr_t entry_address;
68     link_key_t link_key;
69     link_key_type_t type;
70     btstack_link_key_iterator_t it;
71 
72     int ok = gap_link_key_iterator_init(&it);
73     if (!ok) {
74         log_error("could not initialize iterator");
75         return;
76     }
77 
78     while (gap_link_key_iterator_get_next(&it, entry_address, link_key, &type)){
79         if (memcmp(connection->address, entry_address, 6) == 0){
80             if (delete_own_bonding){
81                 gap_drop_link_key_for_bd_addr(entry_address);
82             }
83         } else {
84             if (delete_all_bonding_but_active){
85                 gap_drop_link_key_for_bd_addr(entry_address);
86             }
87         }
88     }
89     gap_link_key_iterator_done(&it);
90 
91 }
92 #endif
93 
94 static void bond_management_delete_bonding_information_le(hci_connection_t * connection, bool delete_own_bonding, bool delete_all_bonding_but_active){
95     bd_addr_t entry_address;
96     bd_addr_type_t device_address_type = connection->address_type;
97 
98     uint16_t i;
99     for (i=0; i < le_device_db_max_count(); i++){
100         int entry_address_type = (int) BD_ADDR_TYPE_UNKNOWN;
101         le_device_db_info(i, &entry_address_type, entry_address, NULL);
102         // skip unused entries
103         if (entry_address_type == (int) BD_ADDR_TYPE_UNKNOWN) continue;
104 
105         if ((entry_address_type == (int) device_address_type) && (memcmp(entry_address, connection->address, 6) == 0)){
106             if (delete_own_bonding){
107                 gap_delete_bonding((bd_addr_type_t)entry_address_type, entry_address);
108             }
109         } else {
110             if (delete_all_bonding_but_active){
111                 gap_delete_bonding((bd_addr_type_t)entry_address_type, entry_address);
112             }
113         }
114     }
115 }
116 
117 static uint16_t bond_management_service_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
118     UNUSED(con_handle);
119     UNUSED(attribute_handle);
120     UNUSED(offset);
121     UNUSED(buffer_size);
122 
123     if (attribute_handle == bm_supported_features_value_handle){
124         uint16_t relevant_octets = 0;
125 
126         // The server shall only include the number of octets needed for returning the highest set feature bit
127         if (bm_supported_features > 0xFFFF){
128             relevant_octets = 3;
129         } else if (bm_supported_features > 0xFF) {
130             relevant_octets = 2;
131         } else if (bm_supported_features > 0x00){
132             relevant_octets = 1;
133         }
134 
135         uint8_t feature_buffer[3];
136         if (buffer != NULL){
137             little_endian_store_24(feature_buffer, 0, bm_supported_features);
138             (void) memcpy(buffer, feature_buffer, relevant_octets);
139         }
140         return relevant_octets;
141     }
142 
143     return 0;
144 }
145 
146 static int bond_management_service_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
147     UNUSED(transaction_mode);
148     UNUSED(offset);
149     UNUSED(buffer_size);
150 
151     hci_connection_t * connection = hci_connection_for_handle(con_handle);
152     btstack_assert(connection != NULL);
153 
154     if (attribute_handle == bm_control_point_value_handle){
155         if (buffer_size == 0){
156             return BOND_MANAGEMENT_CONTROL_POINT_OPCODE_NOT_SUPPORTED;
157         }
158 
159         uint8_t cmd = buffer[0];
160         // check if command/auth is supported
161         if (cmd > BOND_MANAGEMENT_CMD_DELETE_ALL_BUT_ACTIVE_BOND_LE) {
162             return BOND_MANAGEMENT_CONTROL_POINT_OPCODE_NOT_SUPPORTED;
163         }
164         uint16_t authorisation_code_size = buffer_size - 1;
165         if (authorisation_code_size > 511){
166             return BOND_MANAGEMENT_OPERATION_FAILED;
167         }
168 
169         uint8_t  auth_provided = authorisation_code_size > 0 ? 1 : 0;
170         uint32_t requested_feature_mask = 1UL << (2*(cmd-1) + auth_provided);
171 
172         if ((bm_supported_features & requested_feature_mask) == 0){
173             // abort, feature not allowed
174             return BOND_MANAGEMENT_CONTROL_POINT_OPCODE_NOT_SUPPORTED;
175         }
176 
177         if (auth_provided == 1){
178             if (!bm_authorization_string){
179                 return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
180             }
181             if (strlen(bm_authorization_string) != authorisation_code_size){
182                 return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
183             }
184             if (memcmp(bm_authorization_string, (const char *)&buffer[1], authorisation_code_size) != 0){
185                 return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
186             }
187         }
188 
189         switch (cmd){
190 #ifdef ENABLE_CLASSIC
191             case BOND_MANAGEMENT_CMD_DELETE_ACTIVE_BOND_CLASSIC_AND_LE:
192                 bond_management_delete_bonding_information_classic(connection, true, false);
193                 bond_management_delete_bonding_information_le(connection, true, false);
194                 break;
195             case BOND_MANAGEMENT_CMD_DELETE_ACTIVE_BOND_CLASSIC:
196                 bond_management_delete_bonding_information_classic(connection, true, false);
197                 break;
198             case BOND_MANAGEMENT_CMD_DELETE_ALL_BONDS_CLASSIC_AND_LE:
199                 bond_management_delete_bonding_information_classic(connection, true, true);
200                 bond_management_delete_bonding_information_le(connection, true, true);
201                 break;
202             case BOND_MANAGEMENT_CMD_DELETE_ALL_BONDS_CLASSIC:
203                 bond_management_delete_bonding_information_classic(connection, true, true);
204                 break;
205             case BOND_MANAGEMENT_CMD_DELETE_ALL_BUT_ACTIVE_BOND_CLASSIC_AND_LE:
206                 bond_management_delete_bonding_information_classic(connection, false, true);
207                 bond_management_delete_bonding_information_le(connection, false, true);
208                 break;
209             case BOND_MANAGEMENT_CMD_DELETE_ALL_BUT_ACTIVE_BOND_CLASSIC:
210                 bond_management_delete_bonding_information_classic(connection, false, true);
211                 break;
212 #endif
213             case BOND_MANAGEMENT_CMD_DELETE_ACTIVE_BOND_LE:
214                 bond_management_delete_bonding_information_le(connection, true, false);
215                 break;
216             case BOND_MANAGEMENT_CMD_DELETE_ALL_BONDS_LE:
217                 bond_management_delete_bonding_information_le(connection, true, true);
218                 break;
219             case BOND_MANAGEMENT_CMD_DELETE_ALL_BUT_ACTIVE_BOND_LE:
220                 bond_management_delete_bonding_information_le(connection, false, true);
221                 break;
222             default:
223                 return BOND_MANAGEMENT_CONTROL_POINT_OPCODE_NOT_SUPPORTED;
224         }
225 
226         return 0;
227     }
228     return 0;
229 }
230 
231 // buffer for authorisation conde
232 void bond_management_service_server_init(uint32_t supported_features){
233     // get service handle range
234     uint16_t start_handle = 0;
235     uint16_t end_handle   = 0xffff;
236     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BOND_MANAGEMENT, &start_handle, &end_handle);
237 	btstack_assert(service_found != 0);
238 	UNUSED(service_found);
239 
240     bm_control_point_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOND_MANAGEMENT_CONTROL_POINT);
241     bm_supported_features_value_handle       = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOND_MANAGEMENT_FEATURE);
242     bm_supported_features = supported_features;
243 
244     log_info("Control Point value handle 0x%02x", bm_control_point_value_handle);
245     log_info("Feature       value handle 0x%02x", bm_supported_features_value_handle);
246     // register service with ATT Server
247     bond_management_service.start_handle   = start_handle;
248     bond_management_service.end_handle     = end_handle;
249     bond_management_service.read_callback  = &bond_management_service_read_callback;
250     bond_management_service.write_callback = &bond_management_service_write_callback;
251 
252     att_server_register_service_handler(&bond_management_service);
253 }
254 
255 void bond_management_service_server_set_authorisation_string(const char * authorisation_string){
256     bm_authorization_string = authorisation_string;
257 }
258