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__ "att_server.c" 39 40 41 // 42 // ATT Server Globals 43 // 44 45 #include <stdint.h> 46 #include <string.h> 47 #include <inttypes.h> 48 49 #include "btstack_config.h" 50 51 #include "ble/att_dispatch.h" 52 #include "ble/att_db.h" 53 #include "ble/att_server.h" 54 #include "ble/core.h" 55 #include "ble/le_device_db.h" 56 #include "ble/sm.h" 57 #include "btstack_debug.h" 58 #include "btstack_event.h" 59 #include "btstack_memory.h" 60 #include "btstack_run_loop.h" 61 #include "gap.h" 62 #include "hci.h" 63 #include "hci_dump.h" 64 #include "l2cap.h" 65 #include "btstack_tlv.h" 66 #ifdef ENABLE_LE_SIGNED_WRITE 67 #include "ble/sm.h" 68 #include "bluetooth_psm.h" 69 70 #endif 71 72 #ifdef ENABLE_TESTING_SUPPORT 73 #include <stdio.h> 74 #endif 75 76 #ifndef NVN_NUM_GATT_SERVER_CCC 77 #define NVN_NUM_GATT_SERVER_CCC 20 78 #endif 79 80 static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection); 81 static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 82 static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle); 83 static void att_server_handle_can_send_now(void); 84 static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection); 85 static void att_server_persistent_ccc_clear(att_server_t * att_server); 86 static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size); 87 88 typedef enum { 89 ATT_SERVER_RUN_PHASE_1_REQUESTS = 0, 90 ATT_SERVER_RUN_PHASE_2_INDICATIONS, 91 ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS, 92 } att_server_run_phase_t; 93 94 // 95 typedef struct { 96 uint32_t seq_nr; 97 uint16_t att_handle; 98 uint8_t value; 99 uint8_t device_index; 100 } persistent_ccc_entry_t; 101 102 // global 103 static btstack_packet_callback_registration_t hci_event_callback_registration; 104 static btstack_packet_callback_registration_t sm_event_callback_registration; 105 static btstack_packet_handler_t att_client_packet_handler; 106 static btstack_linked_list_t service_handlers; 107 static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration; 108 109 static att_read_callback_t att_server_client_read_callback; 110 static att_write_callback_t att_server_client_write_callback; 111 112 // round robin 113 static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID; 114 115 #ifdef ENABLE_GATT_OVER_EATT 116 typedef struct { 117 btstack_linked_item_t item; 118 att_server_t att_server; 119 att_connection_t att_connection; 120 uint8_t * receive_buffer; 121 uint8_t * send_buffer; 122 } att_server_eatt_bearer_t; 123 static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle); 124 static btstack_linked_list_t att_server_eatt_bearer_pool; 125 static btstack_linked_list_t att_server_eatt_bearer_active; 126 #endif 127 128 #ifdef ENABLE_LE_SIGNED_WRITE 129 static bool att_server_connections_for_state(att_server_state_t state, att_server_t ** att_server_out, att_connection_t ** att_connection_out){ 130 btstack_linked_list_iterator_t it; 131 hci_connections_get_iterator(&it); 132 while(btstack_linked_list_iterator_has_next(&it)){ 133 hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 134 if (hci_connection->att_server.state == state) { 135 *att_server_out = &hci_connection->att_server; 136 *att_connection_out = &hci_connection->att_connection; 137 return true; 138 } 139 } 140 141 #ifdef ENABLE_GATT_OVER_EATT 142 btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 143 while(btstack_linked_list_iterator_has_next(&it)){ 144 att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 145 if (eatt_bearer->att_server.state == state) { 146 *att_server_out = &eatt_bearer->att_server; 147 *att_connection_out = &eatt_bearer->att_connection; 148 return true; 149 } 150 } 151 #endif 152 153 return false; 154 } 155 #endif 156 157 static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){ 158 switch (att_server->bearer_type){ 159 case ATT_BEARER_UNENHANCED_LE: 160 #ifdef ENABLE_GATT_OVER_CLASSIC 161 case ATT_BEARER_UNENHANCED_CLASSIC: 162 /* fall through */ 163 #endif 164 att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 165 break; 166 #ifdef ENABLE_GATT_OVER_EATT 167 case ATT_BEARER_ENHANCED_LE: 168 l2cap_request_can_send_now_event(att_server->l2cap_cid); 169 break; 170 #endif 171 default: 172 btstack_unreachable(); 173 break; 174 } 175 } 176 177 static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){ 178 switch (att_server->bearer_type) { 179 case ATT_BEARER_UNENHANCED_LE: 180 #ifdef ENABLE_GATT_OVER_CLASSIC 181 case ATT_BEARER_UNENHANCED_CLASSIC: 182 /* fall through */ 183 #endif 184 return att_dispatch_server_can_send_now(att_connection->con_handle); 185 #ifdef ENABLE_GATT_OVER_EATT 186 case ATT_BEARER_ENHANCED_LE: 187 return l2cap_can_send_packet_now(att_server->l2cap_cid); 188 #endif 189 default: 190 btstack_unreachable(); 191 break; 192 } 193 return false; 194 } 195 196 static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 197 btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 198 if (!packet_handler) return; 199 200 uint8_t event[7]; 201 int pos = 0; 202 event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 203 event[pos++] = sizeof(event) - 2u; 204 event[pos++] = status; 205 little_endian_store_16(event, pos, client_handle); 206 pos += 2; 207 little_endian_store_16(event, pos, attribute_handle); 208 (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 209 } 210 211 static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 212 // dispatch to app level handler 213 if (att_client_packet_handler != NULL){ 214 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 215 } 216 217 // dispatch to service handlers 218 btstack_linked_list_iterator_t it; 219 btstack_linked_list_iterator_init(&it, &service_handlers); 220 while (btstack_linked_list_iterator_has_next(&it)){ 221 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 222 if (!handler->packet_handler) continue; 223 (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 224 } 225 } 226 227 static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 228 uint8_t event[6]; 229 int pos = 0; 230 event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 231 event[pos++] = sizeof(event) - 2u; 232 little_endian_store_16(event, pos, con_handle); 233 pos += 2; 234 little_endian_store_16(event, pos, mtu); 235 236 // also dispatch to GATT Clients 237 att_dispatch_server_mtu_exchanged(con_handle, mtu); 238 239 // dispatch to app level handler and service handlers 240 att_emit_event_to_all(&event[0], sizeof(event)); 241 } 242 243 static void att_emit_can_send_now_event(void * context){ 244 UNUSED(context); 245 if (!att_client_packet_handler) return; 246 247 uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 248 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 249 } 250 251 static void att_emit_connected_event(att_server_t * att_server, att_connection_t * att_connection){ 252 uint8_t event[11]; 253 int pos = 0; 254 event[pos++] = ATT_EVENT_CONNECTED; 255 event[pos++] = sizeof(event) - 2u; 256 event[pos++] = att_server->peer_addr_type; 257 reverse_bd_addr(att_server->peer_address, &event[pos]); 258 pos += 6; 259 little_endian_store_16(event, pos, att_connection->con_handle); 260 pos += 2; 261 262 // dispatch to app level handler and service handlers 263 att_emit_event_to_all(&event[0], sizeof(event)); 264 } 265 266 267 static void att_emit_disconnected_event(uint16_t con_handle){ 268 uint8_t event[4]; 269 int pos = 0; 270 event[pos++] = ATT_EVENT_DISCONNECTED; 271 event[pos++] = sizeof(event) - 2u; 272 little_endian_store_16(event, pos, con_handle); 273 pos += 2; 274 275 // dispatch to app level handler and service handlers 276 att_emit_event_to_all(&event[0], sizeof(event)); 277 } 278 279 static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 280 void * context = btstack_run_loop_get_timer_context(ts); 281 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 282 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 283 if (!hci_connection) return; 284 // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 285 // (that's why we don't reset the value_indication_handle) 286 att_server_t * att_server = &hci_connection->att_server; 287 uint16_t att_handle = att_server->value_indication_handle; 288 att_connection_t * att_connection = &hci_connection->att_connection; 289 att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 290 } 291 292 static void att_server_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 293 294 UNUSED(channel); // ok: there is no channel 295 UNUSED(size); // ok: handling own l2cap events 296 297 att_server_t * att_server; 298 att_connection_t * att_connection; 299 hci_con_handle_t con_handle; 300 hci_connection_t * hci_connection; 301 302 switch (packet_type) { 303 304 case HCI_EVENT_PACKET: 305 switch (hci_event_packet_get_type(packet)) { 306 case HCI_EVENT_META_GAP: 307 switch (hci_event_gap_meta_get_subevent_code(packet)) { 308 case GAP_SUBEVENT_LE_CONNECTION_COMPLETE: 309 con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 310 hci_connection = hci_connection_for_handle(con_handle); 311 if (!hci_connection) break; 312 att_server = &hci_connection->att_server; 313 // store connection info 314 att_server->peer_addr_type = gap_subevent_le_connection_complete_get_peer_address_type(packet); 315 gap_subevent_le_connection_complete_get_peer_address(packet, att_server->peer_address); 316 att_connection = &hci_connection->att_connection; 317 att_connection->con_handle = con_handle; 318 // reset connection properties 319 att_server->state = ATT_SERVER_IDLE; 320 att_server->bearer_type = ATT_BEARER_UNENHANCED_LE; 321 att_connection->mtu = ATT_DEFAULT_MTU; 322 att_connection->max_mtu = l2cap_max_le_mtu(); 323 if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 324 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 325 } 326 att_connection->encryption_key_size = 0u; 327 att_connection->authenticated = 0u; 328 att_connection->authorized = 0u; 329 // workaround: identity resolving can already be complete, at least store result 330 att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 331 att_server->ir_lookup_active = false; 332 att_server->pairing_active = false; 333 // notify all - new 334 att_emit_connected_event(att_server, att_connection); 335 break; 336 default: 337 break; 338 } 339 break; 340 341 case HCI_EVENT_LE_META: 342 switch (hci_event_le_meta_get_subevent_code(packet)) { 343 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 344 // forward LE Connection Complete event to keep backward compatibility 345 // deprecated: please register hci handler with hci_add_event_handler or handle ATT_EVENT_CONNECTED 346 att_emit_event_to_all(packet, size); 347 break; 348 default: 349 break; 350 } 351 break; 352 353 case HCI_EVENT_ENCRYPTION_CHANGE: 354 case HCI_EVENT_ENCRYPTION_CHANGE_V2: 355 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 356 // check handle 357 con_handle = little_endian_read_16(packet, 3); 358 hci_connection = hci_connection_for_handle(con_handle); 359 if (!hci_connection) break; 360 if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 361 // update security params 362 att_server = &hci_connection->att_server; 363 att_connection = &hci_connection->att_connection; 364 att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 365 att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 366 att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0; 367 log_info("encrypted key size %u, authenticated %u, secure connection %u", 368 att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 369 if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 370 // restore CCC values when encrypted for LE Connections 371 if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){ 372 att_server_persistent_ccc_restore(att_server, att_connection); 373 } 374 } 375 att_run_for_context(att_server, att_connection); 376 break; 377 378 case HCI_EVENT_DISCONNECTION_COMPLETE: 379 // check handle 380 con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 381 hci_connection = hci_connection_for_handle(con_handle); 382 if (!hci_connection) break; 383 att_server = &hci_connection->att_server; 384 att_connection = &hci_connection->att_connection; 385 att_clear_transaction_queue(att_connection); 386 att_connection->con_handle = 0; 387 att_server->pairing_active = false; 388 att_server->state = ATT_SERVER_IDLE; 389 if (att_server->value_indication_handle != 0u){ 390 btstack_run_loop_remove_timer(&att_server->value_indication_timer); 391 uint16_t att_handle = att_server->value_indication_handle; 392 att_server->value_indication_handle = 0u; // reset error state 393 att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 394 } 395 // notify all - new 396 att_emit_disconnected_event(con_handle); 397 // notify all - old 398 att_emit_event_to_all(packet, size); 399 break; 400 401 // Identity Resolving 402 case SM_EVENT_IDENTITY_RESOLVING_STARTED: 403 con_handle = sm_event_identity_resolving_started_get_handle(packet); 404 hci_connection = hci_connection_for_handle(con_handle); 405 if (!hci_connection) break; 406 att_server = &hci_connection->att_server; 407 log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 408 att_server->ir_lookup_active = true; 409 break; 410 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 411 con_handle = sm_event_identity_created_get_handle(packet); 412 hci_connection = hci_connection_for_handle(con_handle); 413 if (!hci_connection) return; 414 att_connection = &hci_connection->att_connection; 415 att_server = &hci_connection->att_server; 416 att_server->ir_lookup_active = false; 417 att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 418 log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 419 att_run_for_context(att_server, att_connection); 420 break; 421 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 422 con_handle = sm_event_identity_resolving_failed_get_handle(packet); 423 hci_connection = hci_connection_for_handle(con_handle); 424 if (!hci_connection) break; 425 att_connection = &hci_connection->att_connection; 426 att_server = &hci_connection->att_server; 427 log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 428 att_server->ir_lookup_active = false; 429 att_server->ir_le_device_db_index = -1; 430 att_run_for_context(att_server, att_connection); 431 break; 432 433 // Pairing started - delete stored CCC values 434 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 435 // - assumes that all events have the con handle as the first field 436 case SM_EVENT_JUST_WORKS_REQUEST: 437 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 438 case SM_EVENT_PASSKEY_INPUT_NUMBER: 439 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 440 con_handle = sm_event_just_works_request_get_handle(packet); 441 hci_connection = hci_connection_for_handle(con_handle); 442 if (!hci_connection) break; 443 att_server = &hci_connection->att_server; 444 log_info("SM Pairing started"); 445 att_server->pairing_active = true; 446 if (att_server->ir_le_device_db_index < 0) break; 447 att_server_persistent_ccc_clear(att_server); 448 // index not valid anymore 449 att_server->ir_le_device_db_index = -1; 450 break; 451 452 // Bonding completed 453 case SM_EVENT_IDENTITY_CREATED: 454 con_handle = sm_event_identity_created_get_handle(packet); 455 hci_connection = hci_connection_for_handle(con_handle); 456 if (!hci_connection) return; 457 att_connection = &hci_connection->att_connection; 458 att_server = &hci_connection->att_server; 459 att_server->pairing_active = false; 460 att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 461 att_run_for_context(att_server, att_connection); 462 break; 463 464 // Pairing complete (with/without bonding=storing of pairing information) 465 case SM_EVENT_PAIRING_COMPLETE: 466 con_handle = sm_event_pairing_complete_get_handle(packet); 467 hci_connection = hci_connection_for_handle(con_handle); 468 if (!hci_connection) return; 469 att_connection = &hci_connection->att_connection; 470 att_server = &hci_connection->att_server; 471 att_server->pairing_active = false; 472 att_run_for_context(att_server, att_connection); 473 break; 474 475 // Authorization 476 case SM_EVENT_AUTHORIZATION_RESULT: { 477 con_handle = sm_event_authorization_result_get_handle(packet); 478 hci_connection = hci_connection_for_handle(con_handle); 479 if (!hci_connection) break; 480 att_connection = &hci_connection->att_connection; 481 att_server = &hci_connection->att_server; 482 att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 483 att_server_request_can_send_now(att_server, att_connection); 484 break; 485 } 486 default: 487 break; 488 } 489 break; 490 default: 491 break; 492 } 493 } 494 495 static uint8_t 496 att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer, 497 uint16_t size) { 498 UNUSED(buffer); 499 uint8_t status = ERROR_CODE_SUCCESS; 500 switch (att_server->bearer_type) { 501 case ATT_BEARER_UNENHANCED_LE: 502 status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 503 break; 504 #ifdef ENABLE_GATT_OVER_CLASSIC 505 case ATT_BEARER_UNENHANCED_CLASSIC: 506 status = l2cap_send_prepared(att_server->l2cap_cid, size); 507 break; 508 #endif 509 #ifdef ENABLE_GATT_OVER_EATT 510 case ATT_BEARER_ENHANCED_LE: 511 btstack_assert(buffer != NULL); 512 status = l2cap_send(att_server->l2cap_cid, buffer, size); 513 break; 514 #endif 515 default: 516 btstack_unreachable(); 517 break; 518 } 519 return status; 520 } 521 522 #ifdef ENABLE_LE_SIGNED_WRITE 523 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 524 att_server_t * att_server = NULL; 525 att_connection_t * att_connection = NULL; 526 bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection); 527 528 if (found == false){ 529 return; 530 } 531 532 uint8_t hash_flipped[8]; 533 reverse_64(hash, hash_flipped); 534 if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){ 535 log_info("ATT Signed Write, invalid signature"); 536 #ifdef ENABLE_TESTING_SUPPORT 537 printf("ATT Signed Write, invalid signature\n"); 538 #endif 539 att_server->state = ATT_SERVER_IDLE; 540 return; 541 } 542 log_info("ATT Signed Write, valid signature"); 543 #ifdef ENABLE_TESTING_SUPPORT 544 printf("ATT Signed Write, valid signature\n"); 545 #endif 546 547 // update sequence number 548 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 549 le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 550 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 551 att_server_request_can_send_now(att_server, att_connection); 552 } 553 #endif 554 555 // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 556 // pre: can send now 557 // uses l2cap outgoing buffer if no eatt_buffer provided 558 // returns: 1 if packet was sent 559 static int 560 att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) { 561 562 uint8_t * att_response_buffer; 563 if (eatt_buffer != NULL){ 564 att_response_buffer = eatt_buffer; 565 } else { 566 l2cap_reserve_packet_buffer(); 567 att_response_buffer = l2cap_get_outgoing_buffer(); 568 } 569 570 uint16_t att_response_size = 0; 571 // Send Error Response for MTU Request over connection-oriented channel 572 if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){ 573 att_response_size = 5; 574 att_response_buffer[0] = ATT_ERROR_RESPONSE; 575 att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST; 576 att_response_buffer[2] = 0; 577 att_response_buffer[3] = 0; 578 att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED; 579 } else { 580 att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 581 } 582 583 #ifdef ENABLE_ATT_DELAYED_RESPONSE 584 if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 585 // update state 586 att_server->state = ATT_SERVER_RESPONSE_PENDING; 587 588 // callback with handle ATT_READ_RESPONSE_PENDING for reads 589 if (att_response_size == ATT_READ_RESPONSE_PENDING){ 590 att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 591 } 592 593 // free reserved buffer 594 if (eatt_buffer == NULL){ 595 l2cap_release_packet_buffer(); 596 } 597 return 0; 598 } 599 #endif 600 601 // intercept "insufficient authorization" for authenticated connections to allow for user authorization 602 if ((att_response_size >= 4u) 603 && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 604 && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 605 && (att_connection->authenticated != 0u)){ 606 607 switch (gap_authorization_state(att_connection->con_handle)){ 608 case AUTHORIZATION_UNKNOWN: 609 if (eatt_buffer == NULL){ 610 l2cap_release_packet_buffer(); 611 } 612 sm_request_pairing(att_connection->con_handle); 613 return 0; 614 case AUTHORIZATION_PENDING: 615 if (eatt_buffer == NULL){ 616 l2cap_release_packet_buffer(); 617 } 618 return 0; 619 default: 620 break; 621 } 622 } 623 624 att_server->state = ATT_SERVER_IDLE; 625 if (att_response_size == 0u) { 626 if (eatt_buffer == NULL){ 627 l2cap_release_packet_buffer(); 628 } 629 return 0; 630 } 631 632 (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size); 633 634 // notify client about MTU exchange result 635 if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 636 att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 637 } 638 return 1; 639 } 640 641 #ifdef ENABLE_ATT_DELAYED_RESPONSE 642 uint8_t att_server_response_ready(hci_con_handle_t con_handle){ 643 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 644 if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 645 att_server_t * att_server = &hci_connection->att_server; 646 att_connection_t * att_connection = &hci_connection->att_connection; 647 if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 648 649 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 650 att_server_request_can_send_now(att_server, att_connection); 651 return ERROR_CODE_SUCCESS; 652 } 653 #endif 654 655 static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){ 656 switch (att_server->state){ 657 case ATT_SERVER_REQUEST_RECEIVED: 658 switch (att_server->bearer_type){ 659 case ATT_BEARER_UNENHANCED_LE: 660 // wait until re-encryption as central is complete 661 if (gap_reconnect_security_setup_active(att_connection->con_handle)) { 662 return; 663 }; 664 break; 665 #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 666 #ifdef ENABLE_GATT_OVER_CLASSIC 667 case ATT_BEARER_UNENHANCED_CLASSIC: 668 /* fall through */ 669 #endif 670 #ifdef ENABLE_GATT_OVER_EATT 671 case ATT_BEARER_ENHANCED_LE: 672 #endif 673 // ok 674 break; 675 #endif 676 default: 677 btstack_unreachable(); 678 break; 679 } 680 681 // wait until pairing is complete 682 if (att_server->pairing_active) break; 683 684 #ifdef ENABLE_LE_SIGNED_WRITE 685 if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 686 log_info("ATT Signed Write!"); 687 if (!sm_cmac_ready()) { 688 log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 689 att_server->state = ATT_SERVER_IDLE; 690 return; 691 } 692 if (att_server->request_size < (3 + 12)) { 693 log_info("ATT Signed Write, request to short. Abort."); 694 att_server->state = ATT_SERVER_IDLE; 695 return; 696 } 697 if (att_server->ir_lookup_active){ 698 return; 699 } 700 if (att_server->ir_le_device_db_index < 0){ 701 log_info("ATT Signed Write, CSRK not available"); 702 att_server->state = ATT_SERVER_IDLE; 703 return; 704 } 705 706 // check counter 707 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 708 uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 709 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 710 if (counter_packet < counter_db){ 711 log_info("ATT Signed Write, db reports higher counter, abort"); 712 att_server->state = ATT_SERVER_IDLE; 713 return; 714 } 715 716 // signature is { sequence counter, secure hash } 717 sm_key_t csrk; 718 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 719 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 720 log_info("Orig Signature: "); 721 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 722 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 723 sm_cmac_signed_write_start(csrk, att_server->request_buffer[0], attribute_handle, att_server->request_size - 15, &att_server->request_buffer[3], counter_packet, att_signed_write_handle_cmac_result); 724 return; 725 } 726 #endif 727 // move on 728 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 729 att_server_request_can_send_now(att_server, att_connection); 730 break; 731 732 default: 733 break; 734 } 735 } 736 737 static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 738 switch (phase){ 739 case ATT_SERVER_RUN_PHASE_1_REQUESTS: 740 return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 741 case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 742 return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 743 case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 744 return (!btstack_linked_list_empty(&att_server->notification_requests)); 745 default: 746 btstack_assert(false); 747 return false; 748 } 749 } 750 751 static void att_server_trigger_send_for_phase(att_server_t * att_server, att_connection_t * att_connection, att_server_run_phase_t phase){ 752 btstack_context_callback_registration_t * client; 753 switch (phase){ 754 case ATT_SERVER_RUN_PHASE_1_REQUESTS: 755 att_server_process_validated_request(att_server, att_connection, NULL); 756 break; 757 case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 758 client = (btstack_context_callback_registration_t*) att_server->indication_requests; 759 btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 760 client->callback(client->context); 761 break; 762 case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 763 client = (btstack_context_callback_registration_t*) att_server->notification_requests; 764 btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 765 client->callback(client->context); 766 break; 767 default: 768 btstack_assert(false); 769 break; 770 } 771 } 772 773 static void att_server_handle_can_send_now(void){ 774 775 hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 776 hci_connection_t * request_hci_connection = NULL; 777 bool can_send_now = true; 778 uint8_t phase_index; 779 780 for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 781 att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 782 hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 783 while (true){ 784 btstack_linked_list_iterator_t it; 785 hci_connections_get_iterator(&it); 786 while(btstack_linked_list_iterator_has_next(&it)){ 787 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 788 att_server_t * att_server = &connection->att_server; 789 att_connection_t * att_connection = &connection->att_connection; 790 791 bool data_ready = att_server_data_ready_for_phase(att_server, phase); 792 793 // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_connection->con_handle, skip_connections_until, data_ready); 794 795 // skip until last sender found (which is also skipped) 796 if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 797 if (data_ready && (request_hci_connection == NULL)){ 798 request_hci_connection = connection; 799 } 800 if (skip_connections_until == att_connection->con_handle){ 801 skip_connections_until = HCI_CON_HANDLE_INVALID; 802 } 803 continue; 804 }; 805 806 if (data_ready){ 807 if (can_send_now){ 808 att_server_trigger_send_for_phase(att_server, att_connection, phase); 809 last_send_con_handle = att_connection->con_handle; 810 can_send_now = att_server_can_send_packet(att_server, att_connection); 811 data_ready = att_server_data_ready_for_phase(att_server, phase); 812 if (data_ready && (request_hci_connection == NULL)){ 813 request_hci_connection = connection; 814 } 815 } else { 816 request_hci_connection = connection; 817 break; 818 } 819 } 820 } 821 822 // stop skipping (handles disconnect by last send connection) 823 skip_connections_until = HCI_CON_HANDLE_INVALID; 824 825 // Exit loop, if we cannot send 826 if (!can_send_now) break; 827 828 // Exit loop, if we can send but there are also no further request 829 if (request_hci_connection == NULL) break; 830 831 // Finally, if we still can send and there are requests, just try again 832 request_hci_connection = NULL; 833 } 834 // update last send con handle for round robin 835 if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 836 att_server_last_can_send_now = last_send_con_handle; 837 } 838 } 839 840 if (request_hci_connection == NULL) return; 841 842 att_server_t * att_server = &request_hci_connection->att_server; 843 att_connection_t * att_connection = &request_hci_connection->att_connection; 844 att_server_request_can_send_now(att_server, att_connection); 845 } 846 847 static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){ 848 849 uint8_t opcode = packet[0u]; 850 uint8_t method = opcode & 0x03fu; 851 bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 852 bool command = (opcode & 0x40u) != 0u; 853 854 // ignore invalid commands 855 if (invalid && command){ 856 return; 857 } 858 859 // handle value indication confirms 860 if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 861 btstack_run_loop_remove_timer(&att_server->value_indication_timer); 862 uint16_t att_handle = att_server->value_indication_handle; 863 att_server->value_indication_handle = 0u; 864 att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 865 att_server_request_can_send_now(att_server, att_connection); 866 return; 867 } 868 869 // directly process command 870 // note: signed write cannot be handled directly as authentication needs to be verified 871 if (opcode == ATT_WRITE_COMMAND){ 872 att_handle_request(att_connection, packet, size, NULL); 873 return; 874 } 875 876 // check size 877 if (size > sizeof(att_server->request_buffer)) { 878 log_info("drop att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer)); 879 return; 880 } 881 882 #ifdef ENABLE_LE_SIGNED_WRITE 883 // abort signed write validation if a new request comes in (but finish previous signed write if possible) 884 if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 885 if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 886 log_info("skip new signed write request as previous is in validation"); 887 return; 888 } else { 889 log_info("abort signed write validation to process new request"); 890 att_server->state = ATT_SERVER_IDLE; 891 } 892 } 893 #endif 894 // last request still in processing? 895 if (att_server->state != ATT_SERVER_IDLE){ 896 log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 897 return; 898 } 899 900 // store request 901 att_server->state = ATT_SERVER_REQUEST_RECEIVED; 902 att_server->request_size = size; 903 (void)memcpy(att_server->request_buffer, packet, size); 904 905 att_run_for_context(att_server, att_connection); 906 } 907 908 static void att_server_dispatch_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 909 hci_connection_t * hci_connection; 910 att_connection_t * att_connection; 911 att_server_t * att_server; 912 #ifdef ENABLE_GATT_OVER_CLASSIC 913 hci_con_handle_t con_handle; 914 bd_addr_t address; 915 btstack_linked_list_iterator_t it; 916 #endif 917 918 switch (packet_type){ 919 case HCI_EVENT_PACKET: 920 switch (packet[0]){ 921 #ifdef ENABLE_GATT_OVER_CLASSIC 922 case L2CAP_EVENT_CHANNEL_OPENED: 923 con_handle = l2cap_event_channel_opened_get_handle(packet); 924 hci_connection = hci_connection_for_handle(con_handle); 925 if (!hci_connection) break; 926 att_server = &hci_connection->att_server; 927 // store connection info 928 att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 929 att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 930 l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 931 att_connection = &hci_connection->att_connection; 932 att_connection->con_handle = con_handle; 933 // reset connection properties 934 att_server->state = ATT_SERVER_IDLE; 935 att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 936 att_connection->max_mtu = l2cap_max_mtu(); 937 if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 938 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 939 } 940 941 log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 942 943 // update security params 944 att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 945 att_connection->authenticated = gap_authenticated(con_handle); 946 att_connection->secure_connection = gap_secure_connection(con_handle); 947 log_info("encrypted key size %u, authenticated %u, secure connection %u", 948 att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 949 950 // notify connection opened 951 att_emit_connected_event(att_server, att_connection); 952 953 // restore persisten ccc if encrypted 954 if ( gap_security_level(con_handle) >= LEVEL_2){ 955 att_server_persistent_ccc_restore(att_server, att_connection); 956 } 957 // TODO: what to do about le device db? 958 att_server->pairing_active = 0; 959 break; 960 #endif 961 case L2CAP_EVENT_CAN_SEND_NOW: 962 att_server_handle_can_send_now(); 963 break; 964 case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 965 // GATT client has negotiated the mtu for this connection 966 hci_connection = hci_connection_for_handle(channel); 967 if (!hci_connection) break; 968 att_connection = &hci_connection->att_connection; 969 att_connection->mtu = little_endian_read_16(packet, 4); 970 break; 971 default: 972 break; 973 } 974 break; 975 976 case ATT_DATA_PACKET: 977 log_debug("ATT Packet, handle 0x%04x", channel); 978 hci_connection = hci_connection_for_handle(channel); 979 if (!hci_connection) break; 980 981 att_server = &hci_connection->att_server; 982 att_connection = &hci_connection->att_connection; 983 att_server_handle_att_pdu(att_server, att_connection, packet, size); 984 break; 985 986 #ifdef ENABLE_GATT_OVER_CLASSIC 987 case L2CAP_DATA_PACKET: 988 hci_connections_get_iterator(&it); 989 while(btstack_linked_list_iterator_has_next(&it)){ 990 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 991 att_server = &hci_connection->att_server; 992 att_connection = &hci_connection->att_connection; 993 if (att_server->l2cap_cid == channel) { 994 att_server_handle_att_pdu(att_server, att_connection, packet, size); 995 break; 996 } 997 } 998 break; 999 #endif 1000 1001 default: 1002 break; 1003 } 1004 } 1005 1006 // --------------------- 1007 // persistent CCC writes 1008 static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 1009 return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 1010 } 1011 1012 static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 1013 // lookup att_server instance 1014 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1015 if (!hci_connection) return; 1016 att_server_t * att_server = &hci_connection->att_server; 1017 int le_device_index = att_server->ir_le_device_db_index; 1018 log_info("Store CCC value 0x%04x for handle 0x%04x of remote %s, le device id %d", value, att_handle, bd_addr_to_str(att_server->peer_address), le_device_index); 1019 1020 // check if bonded 1021 if (le_device_index < 0) return; 1022 1023 // get btstack_tlv 1024 const btstack_tlv_t * tlv_impl = NULL; 1025 void * tlv_context; 1026 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1027 if (!tlv_impl) return; 1028 1029 // update ccc tag 1030 int index; 1031 uint32_t highest_seq_nr = 0; 1032 uint32_t lowest_seq_nr = 0; 1033 uint32_t tag_for_lowest_seq_nr = 0; 1034 uint32_t tag_for_empty = 0; 1035 persistent_ccc_entry_t entry; 1036 for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 1037 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 1038 int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 1039 1040 // empty/invalid tag 1041 if (len != sizeof(persistent_ccc_entry_t)){ 1042 tag_for_empty = tag; 1043 continue; 1044 } 1045 // update highest seq nr 1046 if (entry.seq_nr > highest_seq_nr){ 1047 highest_seq_nr = entry.seq_nr; 1048 } 1049 // find entry with lowest seq nr 1050 if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 1051 tag_for_lowest_seq_nr = tag; 1052 lowest_seq_nr = entry.seq_nr; 1053 } 1054 1055 if (entry.device_index != le_device_index) continue; 1056 if (entry.att_handle != att_handle) continue; 1057 1058 // found matching entry 1059 if (value != 0){ 1060 // update 1061 if (entry.value == value) { 1062 log_info("CCC Index %u: Up-to-date", index); 1063 return; 1064 } 1065 entry.value = (uint8_t) value; 1066 entry.seq_nr = highest_seq_nr + 1u; 1067 log_info("CCC Index %u: Store", index); 1068 int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 1069 if (result != 0){ 1070 log_error("Store tag index %u failed", index); 1071 } 1072 } else { 1073 // delete 1074 log_info("CCC Index %u: Delete", index); 1075 tlv_impl->delete_tag(tlv_context, tag); 1076 } 1077 return; 1078 } 1079 1080 log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 1081 1082 if (value == 0u){ 1083 // done 1084 return; 1085 } 1086 1087 uint32_t tag_to_use = 0u; 1088 if (tag_for_empty != 0u){ 1089 tag_to_use = tag_for_empty; 1090 } else if (tag_for_lowest_seq_nr != 0){ 1091 tag_to_use = tag_for_lowest_seq_nr; 1092 } else { 1093 // should not happen 1094 return; 1095 } 1096 // store ccc tag 1097 entry.seq_nr = highest_seq_nr + 1u; 1098 entry.device_index = le_device_index; 1099 entry.att_handle = att_handle; 1100 entry.value = (uint8_t) value; 1101 int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 1102 if (result != 0){ 1103 log_error("Store tag index %u failed", index); 1104 } 1105 } 1106 1107 static void att_server_persistent_ccc_clear(att_server_t * att_server){ 1108 int le_device_index = att_server->ir_le_device_db_index; 1109 log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1110 // check if bonded 1111 if (le_device_index < 0) return; 1112 // get btstack_tlv 1113 const btstack_tlv_t * tlv_impl = NULL; 1114 void * tlv_context; 1115 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1116 if (!tlv_impl) return; 1117 // get all ccc tag 1118 int index; 1119 persistent_ccc_entry_t entry; 1120 for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1121 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 1122 int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 1123 if (len != sizeof(persistent_ccc_entry_t)) continue; 1124 if (entry.device_index != le_device_index) continue; 1125 // delete entry 1126 log_info("CCC Index %u: Delete", index); 1127 tlv_impl->delete_tag(tlv_context, tag); 1128 } 1129 } 1130 1131 static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){ 1132 int le_device_index = att_server->ir_le_device_db_index; 1133 log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1134 // check if bonded 1135 if (le_device_index < 0) return; 1136 // get btstack_tlv 1137 const btstack_tlv_t * tlv_impl = NULL; 1138 void * tlv_context; 1139 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1140 if (!tlv_impl) return; 1141 // get all ccc tag 1142 int index; 1143 persistent_ccc_entry_t entry; 1144 for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1145 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 1146 int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 1147 if (len != sizeof(persistent_ccc_entry_t)) continue; 1148 if (entry.device_index != le_device_index) continue; 1149 // simulate write callback 1150 uint16_t attribute_handle = entry.att_handle; 1151 uint8_t value[2]; 1152 little_endian_store_16(value, 0, entry.value); 1153 att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 1154 if (!callback) continue; 1155 log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1156 (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 1157 } 1158 } 1159 1160 // persistent CCC writes 1161 // --------------------- 1162 1163 // gatt service management 1164 static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 1165 btstack_linked_list_iterator_t it; 1166 btstack_linked_list_iterator_init(&it, &service_handlers); 1167 while (btstack_linked_list_iterator_has_next(&it)){ 1168 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 1169 if (handler->start_handle > handle) continue; 1170 if (handler->end_handle < handle) continue; 1171 return handler; 1172 } 1173 return NULL; 1174 } 1175 static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 1176 att_service_handler_t * handler = att_service_handler_for_handle(handle); 1177 if (handler != NULL) return handler->read_callback; 1178 return att_server_client_read_callback; 1179 } 1180 1181 static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 1182 att_service_handler_t * handler = att_service_handler_for_handle(handle); 1183 if (handler != NULL) return handler->write_callback; 1184 return att_server_client_write_callback; 1185 } 1186 1187 static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 1188 att_service_handler_t * handler = att_service_handler_for_handle(handle); 1189 if (handler != NULL) return handler->packet_handler; 1190 return att_client_packet_handler; 1191 } 1192 1193 static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 1194 // notify all callbacks 1195 btstack_linked_list_iterator_t it; 1196 btstack_linked_list_iterator_init(&it, &service_handlers); 1197 while (btstack_linked_list_iterator_has_next(&it)){ 1198 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 1199 if (!handler->write_callback) continue; 1200 (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 1201 } 1202 if (!att_server_client_write_callback) return; 1203 (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 1204 } 1205 1206 // returns first reported error or 0 1207 static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 1208 btstack_linked_list_iterator_t it; 1209 btstack_linked_list_iterator_init(&it, &service_handlers); 1210 while (btstack_linked_list_iterator_has_next(&it)){ 1211 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 1212 if (!handler->write_callback) continue; 1213 uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 1214 if (error_code != 0u) return error_code; 1215 } 1216 if (!att_server_client_write_callback) return 0; 1217 return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 1218 } 1219 1220 static uint16_t att_server_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 1221 att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 1222 if (!callback) return 0; 1223 return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 1224 } 1225 1226 static int att_server_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){ 1227 switch (transaction_mode){ 1228 case ATT_TRANSACTION_MODE_VALIDATE: 1229 return att_validate_prepared_write(con_handle); 1230 case ATT_TRANSACTION_MODE_EXECUTE: 1231 case ATT_TRANSACTION_MODE_CANCEL: 1232 att_notify_write_callbacks(con_handle, transaction_mode); 1233 return 0; 1234 default: 1235 break; 1236 } 1237 1238 // track CCC writes 1239 if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1240 att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1241 } 1242 1243 att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 1244 if (!callback) return 0; 1245 return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 1246 } 1247 1248 /** 1249 * @brief register read/write callbacks for specific handle range 1250 * @param att_service_handler_t 1251 */ 1252 void att_server_register_service_handler(att_service_handler_t * handler){ 1253 bool att_server_registered = false; 1254 if (att_service_handler_for_handle(handler->start_handle) != NULL){ 1255 att_server_registered = true; 1256 } 1257 1258 if (att_service_handler_for_handle(handler->end_handle) != NULL){ 1259 att_server_registered = true; 1260 } 1261 1262 if (att_server_registered){ 1263 log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 1264 return; 1265 } 1266 btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 1267 } 1268 1269 void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 1270 1271 // store callbacks 1272 att_server_client_read_callback = read_callback; 1273 att_server_client_write_callback = write_callback; 1274 1275 // register for HCI Events 1276 hci_event_callback_registration.callback = &att_server_event_packet_handler; 1277 hci_add_event_handler(&hci_event_callback_registration); 1278 1279 // register for SM events 1280 sm_event_callback_registration.callback = &att_server_event_packet_handler; 1281 sm_add_event_handler(&sm_event_callback_registration); 1282 1283 // and L2CAP ATT Server PDUs 1284 att_dispatch_register_server(att_server_dispatch_packet_handler); 1285 1286 #ifdef ENABLE_GATT_OVER_CLASSIC 1287 // setup l2cap service 1288 att_dispatch_classic_register_service(); 1289 #endif 1290 1291 att_set_db(db); 1292 att_set_read_callback(att_server_read_callback); 1293 att_set_write_callback(att_server_write_callback); 1294 } 1295 1296 void att_server_register_packet_handler(btstack_packet_handler_t handler){ 1297 att_client_packet_handler = handler; 1298 } 1299 1300 1301 // to be deprecated 1302 int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 1303 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1304 if (!hci_connection) return 0; 1305 att_server_t * att_server = &hci_connection->att_server; 1306 att_connection_t * att_connection = &hci_connection->att_connection; 1307 return att_server_can_send_packet(att_server, att_connection); 1308 } 1309 1310 uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1311 return att_server_request_to_send_notification(callback_registration, con_handle); 1312 } 1313 1314 void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1315 att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1316 att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1317 } 1318 // end of deprecated 1319 1320 uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1321 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1322 if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1323 att_server_t * att_server = &hci_connection->att_server; 1324 att_connection_t * att_connection = &hci_connection->att_connection; 1325 bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 1326 att_server_request_can_send_now(att_server, att_connection); 1327 if (added){ 1328 return ERROR_CODE_SUCCESS; 1329 } else { 1330 return ERROR_CODE_COMMAND_DISALLOWED; 1331 } 1332 } 1333 1334 uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1335 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1336 if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1337 att_server_t * att_server = &hci_connection->att_server; 1338 att_connection_t * att_connection = &hci_connection->att_connection; 1339 bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 1340 att_server_request_can_send_now(att_server, att_connection); 1341 if (added){ 1342 return ERROR_CODE_SUCCESS; 1343 } else { 1344 return ERROR_CODE_COMMAND_DISALLOWED; 1345 } 1346 } 1347 1348 static uint8_t att_server_prepare_server_message(hci_con_handle_t con_handle, att_server_t ** out_att_server, att_connection_t ** out_att_connection, uint8_t ** out_packet_buffer){ 1349 1350 att_server_t * att_server = NULL; 1351 att_connection_t * att_connection = NULL; 1352 uint8_t * packet_buffer = NULL; 1353 1354 // prefer enhanced bearer 1355 #ifdef ENABLE_GATT_OVER_EATT 1356 att_server_eatt_bearer_t * eatt_bearer = att_server_eatt_bearer_for_con_handle(con_handle); 1357 if (eatt_bearer != NULL){ 1358 att_server = &eatt_bearer->att_server; 1359 att_connection = &eatt_bearer->att_connection; 1360 packet_buffer = eatt_bearer->send_buffer; 1361 } else 1362 #endif 1363 { 1364 hci_connection_t *hci_connection = hci_connection_for_handle(con_handle); 1365 if (hci_connection != NULL) { 1366 att_server = &hci_connection->att_server; 1367 att_connection = &hci_connection->att_connection; 1368 } 1369 } 1370 1371 if (att_server == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1372 if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 1373 1374 if (packet_buffer == NULL){ 1375 l2cap_reserve_packet_buffer(); 1376 packet_buffer = l2cap_get_outgoing_buffer(); 1377 } 1378 1379 *out_att_connection = att_connection; 1380 *out_att_server = att_server; 1381 *out_packet_buffer = packet_buffer; 1382 return ERROR_CODE_SUCCESS; 1383 } 1384 1385 uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 1386 att_server_t * att_server = NULL; 1387 att_connection_t * att_connection = NULL; 1388 uint8_t * packet_buffer = NULL; 1389 1390 uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer); 1391 if (status != ERROR_CODE_SUCCESS){ 1392 return status; 1393 } 1394 1395 uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 1396 1397 return att_server_send_prepared(att_server, att_connection, NULL, size); 1398 } 1399 1400 /** 1401 * @brief notify client about multiple attribute value changes 1402 * @param con_handle 1403 * @param num_attributes 1404 * @param attribute_handles[] 1405 * @param values_data[] 1406 * @param values_len[] 1407 * @return 0 if ok, error otherwise 1408 */ 1409 uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes, 1410 const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len){ 1411 1412 att_server_t * att_server = NULL; 1413 att_connection_t * att_connection = NULL; 1414 uint8_t * packet_buffer = NULL; 1415 1416 uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer); 1417 if (status != ERROR_CODE_SUCCESS){ 1418 return status; 1419 } 1420 1421 uint16_t size = att_prepare_handle_value_multiple_notification(att_connection, num_attributes, attribute_handles, values_data, values_len, packet_buffer); 1422 1423 return att_server_send_prepared(att_server, att_connection, packet_buffer, size); 1424 } 1425 1426 uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 1427 1428 att_server_t * att_server = NULL; 1429 att_connection_t * att_connection = NULL; 1430 uint8_t * packet_buffer = NULL; 1431 1432 uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer); 1433 if (status != ERROR_CODE_SUCCESS){ 1434 return status; 1435 } 1436 1437 if (att_server->value_indication_handle != 0u) { 1438 // free reserved packet buffer 1439 if (att_server->bearer_type == ATT_BEARER_ENHANCED_LE){ 1440 l2cap_release_packet_buffer(); 1441 } 1442 return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 1443 } 1444 1445 // track indication 1446 att_server->value_indication_handle = attribute_handle; 1447 btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 1448 btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 1449 btstack_run_loop_add_timer(&att_server->value_indication_timer); 1450 1451 uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 1452 1453 return att_server_send_prepared(att_server, att_connection, NULL, size); 1454 } 1455 1456 uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 1457 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1458 if (!hci_connection) return 0; 1459 att_connection_t * att_connection = &hci_connection->att_connection; 1460 return att_connection->mtu; 1461 } 1462 1463 void att_server_deinit(void){ 1464 att_server_client_read_callback = NULL; 1465 att_server_client_write_callback = NULL; 1466 att_client_packet_handler = NULL; 1467 service_handlers = NULL; 1468 } 1469 1470 #ifdef ENABLE_GATT_OVER_EATT 1471 1472 #define MAX_NR_EATT_CHANNELS 5 1473 1474 static uint16_t att_server_eatt_receive_buffer_size; 1475 static uint16_t att_server_eatt_send_buffer_size; 1476 1477 static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){ 1478 btstack_linked_list_iterator_t it; 1479 btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1480 while(btstack_linked_list_iterator_has_next(&it)){ 1481 att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1482 if (eatt_bearer->att_server.l2cap_cid == cid) { 1483 return eatt_bearer; 1484 } 1485 } 1486 return NULL; 1487 } 1488 1489 static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle){ 1490 btstack_linked_list_iterator_t it; 1491 btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1492 while(btstack_linked_list_iterator_has_next(&it)){ 1493 att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1494 if (eatt_bearer->att_connection.con_handle == con_handle) { 1495 return eatt_bearer; 1496 } 1497 } 1498 return NULL; 1499 } 1500 1501 static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1502 uint16_t cid; 1503 uint8_t status; 1504 uint16_t remote_mtu; 1505 1506 uint8_t i; 1507 uint8_t num_requested_bearers; 1508 uint8_t num_accepted_bearers; 1509 uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS; 1510 uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS]; 1511 uint16_t cids[MAX_NR_EATT_CHANNELS]; 1512 att_server_eatt_bearer_t * eatt_bearers[MAX_NR_EATT_CHANNELS]; 1513 att_server_eatt_bearer_t * eatt_bearer; 1514 att_server_t * att_server; 1515 att_connection_t * att_connection; 1516 hci_con_handle_t con_handle; 1517 hci_connection_t * hci_connection; 1518 1519 switch (packet_type) { 1520 1521 case L2CAP_DATA_PACKET: 1522 eatt_bearer = att_server_eatt_bearer_for_cid(channel); 1523 btstack_assert(eatt_bearer != NULL); 1524 att_server = &eatt_bearer->att_server; 1525 att_connection = &eatt_bearer->att_connection; 1526 att_server_handle_att_pdu(att_server, att_connection, packet, size); 1527 break; 1528 1529 case HCI_EVENT_PACKET: 1530 switch (packet[0]) { 1531 1532 case L2CAP_EVENT_CAN_SEND_NOW: 1533 cid = l2cap_event_packet_sent_get_local_cid(packet); 1534 eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1535 btstack_assert(eatt_bearer != NULL); 1536 att_server = &eatt_bearer->att_server; 1537 att_connection = &eatt_bearer->att_connection; 1538 // only used for EATT request responses 1539 btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED); 1540 att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer); 1541 break; 1542 1543 case L2CAP_EVENT_PACKET_SENT: 1544 cid = l2cap_event_packet_sent_get_local_cid(packet); 1545 eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1546 btstack_assert(eatt_bearer != NULL); 1547 att_server = &eatt_bearer->att_server; 1548 att_connection = &eatt_bearer->att_connection; 1549 att_server_handle_att_pdu(att_server, att_connection, packet, size); 1550 break; 1551 1552 case L2CAP_EVENT_ECBM_INCOMING_CONNECTION: 1553 cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet); 1554 1555 // reject if outgoing l2cap connection active, L2CAP/TIM/BV-01-C 1556 con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 1557 hci_connection = hci_connection_for_handle(con_handle); 1558 btstack_assert(hci_connection != NULL); 1559 if (hci_connection->att_server.eatt_outgoing_active) { 1560 hci_connection->att_server.incoming_connection_request = true; 1561 l2cap_ecbm_decline_channels(cid, L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE ); 1562 log_info("Decline incoming connection from %s", bd_addr_to_str(hci_connection->address)); 1563 } else { 1564 num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet); 1565 for (i = 0; i < num_requested_bearers; i++){ 1566 eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool); 1567 if (eatt_bearers[i] == NULL) { 1568 break; 1569 } 1570 eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 1571 eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE; 1572 receive_buffers[i] = eatt_bearers[i]->receive_buffer; 1573 btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]); 1574 } 1575 num_accepted_bearers = i; 1576 status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids); 1577 btstack_assert(status == ERROR_CODE_SUCCESS); 1578 log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers); 1579 for (i=0;i<num_accepted_bearers;i++){ 1580 log_info("eatt l2cap cid: 0x%04x", cids[i]); 1581 eatt_bearers[i]->att_server.l2cap_cid = cids[i]; 1582 } 1583 } 1584 break; 1585 1586 case L2CAP_EVENT_ECBM_CHANNEL_OPENED: 1587 cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet); 1588 status = l2cap_event_ecbm_channel_opened_get_status(packet); 1589 remote_mtu = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet); 1590 eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1591 btstack_assert(eatt_bearer != NULL); 1592 eatt_bearer->att_connection.mtu_exchanged = true; 1593 eatt_bearer->att_connection.mtu = remote_mtu; 1594 eatt_bearer->att_connection.max_mtu = remote_mtu; 1595 log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status); 1596 break; 1597 1598 case L2CAP_EVENT_ECBM_RECONFIGURED: 1599 break; 1600 1601 case L2CAP_EVENT_CHANNEL_CLOSED: 1602 eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet)); 1603 btstack_assert(eatt_bearers != NULL); 1604 1605 // TODO: finalize - abort queued writes 1606 1607 btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearer); 1608 btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 1609 break; 1610 default: 1611 break; 1612 } 1613 break; 1614 default: 1615 btstack_unreachable(); 1616 break; 1617 } 1618 } 1619 1620 // create eatt bearers 1621 uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){ 1622 uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t); 1623 if (storage_size < size_for_structs) { 1624 return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 1625 } 1626 1627 // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets. 1628 1629 memset(storage_buffer, 0, storage_size); 1630 uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers); 1631 att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2; 1632 att_server_eatt_send_buffer_size = buffer_size_per_bearer / 2; 1633 uint8_t * bearer_buffer = &storage_buffer[size_for_structs]; 1634 uint8_t i; 1635 att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer; 1636 log_info("%u EATT bearers with receive buffer size %u", 1637 num_eatt_bearers, att_server_eatt_receive_buffer_size); 1638 for (i=0;i<num_eatt_bearers;i++){ 1639 eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID; 1640 eatt_bearer->receive_buffer = bearer_buffer; 1641 bearer_buffer += att_server_eatt_receive_buffer_size; 1642 eatt_bearer->send_buffer = bearer_buffer; 1643 bearer_buffer += att_server_eatt_send_buffer_size; 1644 btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 1645 eatt_bearer++; 1646 } 1647 // TODO: define minimum EATT MTU 1648 l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0, false); 1649 1650 return 0; 1651 } 1652 #endif 1653