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__ "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 "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 #endif 69 70 #ifndef NVN_NUM_GATT_SERVER_CCC 71 #define NVN_NUM_GATT_SERVER_CCC 20 72 #endif 73 74 static void att_run_for_context(att_server_t * att_server); 75 static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 76 static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle); 77 static void att_server_persistent_ccc_restore(att_server_t * att_server); 78 static void att_server_persistent_ccc_clear(att_server_t * att_server); 79 static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size); 80 81 typedef enum { 82 ATT_SERVER_RUN_PHASE_1_REQUESTS, 83 ATT_SERVER_RUN_PHASE_2_INDICATIONS, 84 ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS, 85 } att_server_run_phase_t; 86 87 // 88 typedef struct { 89 uint32_t seq_nr; 90 uint16_t att_handle; 91 uint8_t value; 92 uint8_t device_index; 93 } persistent_ccc_entry_t; 94 95 // global 96 static btstack_packet_callback_registration_t hci_event_callback_registration; 97 static btstack_packet_callback_registration_t sm_event_callback_registration; 98 static btstack_packet_handler_t att_client_packet_handler = NULL; 99 static btstack_linked_list_t service_handlers; 100 static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration; 101 102 static att_read_callback_t att_server_client_read_callback; 103 static att_write_callback_t att_server_client_write_callback; 104 105 // round robin 106 static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID; 107 108 static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){ 109 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 110 if (!hci_connection) return NULL; 111 return &hci_connection->att_server; 112 } 113 114 #ifdef ENABLE_GATT_OVER_CLASSIC 115 static att_server_t * att_server_for_l2cap_cid(uint16_t l2cap_cid){ 116 btstack_linked_list_iterator_t it; 117 hci_connections_get_iterator(&it); 118 while(btstack_linked_list_iterator_has_next(&it)){ 119 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 120 att_server_t * att_server = &connection->att_server; 121 if (att_server->l2cap_cid == l2cap_cid) return att_server; 122 } 123 return NULL; 124 } 125 #endif 126 127 #ifdef ENABLE_LE_SIGNED_WRITE 128 static att_server_t * att_server_for_state(att_server_state_t state){ 129 btstack_linked_list_iterator_t it; 130 hci_connections_get_iterator(&it); 131 while(btstack_linked_list_iterator_has_next(&it)){ 132 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 133 att_server_t * att_server = &connection->att_server; 134 if (att_server->state == state) return att_server; 135 } 136 return NULL; 137 } 138 #endif 139 140 static void att_server_request_can_send_now(att_server_t * att_server){ 141 #ifdef ENABLE_GATT_OVER_CLASSIC 142 if (att_server->l2cap_cid != 0){ 143 l2cap_request_can_send_now_event(att_server->l2cap_cid); 144 return; 145 } 146 #endif 147 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 148 } 149 150 static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 151 btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 152 if (!packet_handler) return; 153 154 uint8_t event[7]; 155 int pos = 0; 156 event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 157 event[pos++] = sizeof(event) - 2; 158 event[pos++] = status; 159 little_endian_store_16(event, pos, client_handle); 160 pos += 2; 161 little_endian_store_16(event, pos, attribute_handle); 162 (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 163 } 164 165 static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 166 // dispatch to app level handler 167 if (att_client_packet_handler){ 168 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 169 } 170 171 // dispatch to service handlers 172 btstack_linked_list_iterator_t it; 173 btstack_linked_list_iterator_init(&it, &service_handlers); 174 while (btstack_linked_list_iterator_has_next(&it)){ 175 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 176 if (!handler->packet_handler) continue; 177 (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 178 } 179 } 180 181 static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 182 uint8_t event[6]; 183 int pos = 0; 184 event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 185 event[pos++] = sizeof(event) - 2; 186 little_endian_store_16(event, pos, con_handle); 187 pos += 2; 188 little_endian_store_16(event, pos, mtu); 189 190 // also dispatch to GATT Clients 191 att_dispatch_server_mtu_exchanged(con_handle, mtu); 192 193 // dispatch to app level handler and service handlers 194 att_emit_event_to_all(&event[0], sizeof(event)); 195 } 196 197 static void att_emit_can_send_now_event(void * context){ 198 UNUSED(context); 199 if (!att_client_packet_handler) return; 200 201 uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 202 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 203 } 204 205 static void att_emit_connected_event(att_server_t * att_server){ 206 uint8_t event[11]; 207 int pos = 0; 208 event[pos++] = ATT_EVENT_CONNECTED; 209 event[pos++] = sizeof(event) - 2; 210 event[pos++] = att_server->peer_addr_type; 211 reverse_bd_addr(att_server->peer_address, &event[pos]); 212 pos += 6; 213 little_endian_store_16(event, pos, att_server->connection.con_handle); 214 pos += 2; 215 216 // dispatch to app level handler and service handlers 217 att_emit_event_to_all(&event[0], sizeof(event)); 218 } 219 220 221 static void att_emit_disconnected_event(uint16_t con_handle){ 222 uint8_t event[4]; 223 int pos = 0; 224 event[pos++] = ATT_EVENT_DISCONNECTED; 225 event[pos++] = sizeof(event) - 2; 226 little_endian_store_16(event, pos, con_handle); 227 pos += 2; 228 229 // dispatch to app level handler and service handlers 230 att_emit_event_to_all(&event[0], sizeof(event)); 231 } 232 233 static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 234 void * context = btstack_run_loop_get_timer_context(ts); 235 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 236 att_server_t * att_server = att_server_for_handle(con_handle); 237 if (!att_server) return; 238 // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer 239 // (that's why we don't reset the value_indication_handle) 240 uint16_t att_handle = att_server->value_indication_handle; 241 att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 242 } 243 244 static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 245 246 UNUSED(channel); // ok: there is no channel 247 UNUSED(size); // ok: handling own l2cap events 248 249 att_server_t * att_server; 250 hci_con_handle_t con_handle; 251 #ifdef ENABLE_GATT_OVER_CLASSIC 252 bd_addr_t address; 253 #endif 254 255 switch (packet_type) { 256 257 case HCI_EVENT_PACKET: 258 switch (hci_event_packet_get_type(packet)) { 259 260 #ifdef ENABLE_GATT_OVER_CLASSIC 261 case L2CAP_EVENT_INCOMING_CONNECTION: 262 l2cap_event_incoming_connection_get_address(packet, address); 263 l2cap_accept_connection(channel); 264 printf("Accept incoming connection from %s\n", bd_addr_to_str(address)); 265 break; 266 case L2CAP_EVENT_CHANNEL_OPENED: 267 con_handle = l2cap_event_channel_opened_get_handle(packet); 268 att_server = att_server_for_handle(con_handle); 269 if (!att_server) break; 270 // store connection info 271 att_server->peer_addr_type = BD_ADDR_TYPE_CLASSIC; 272 l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 273 att_server->connection.con_handle = con_handle; 274 att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 275 // reset connection properties 276 att_server->state = ATT_SERVER_IDLE; 277 att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 278 att_server->connection.max_mtu = l2cap_max_mtu(); 279 if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 280 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 281 } 282 // TODO: use security level from underlying HCI connection 283 att_server->connection.encryption_key_size = 0; 284 att_server->connection.authenticated = 0; 285 att_server->connection.authorized = 0; 286 // TODO: what to do about le device db? 287 att_server->pairing_active = 0; 288 printf("Connection opened %s, l2cap cid %04x, \n", bd_addr_to_str(address), att_server->l2cap_cid); 289 break; 290 #endif 291 case HCI_EVENT_LE_META: 292 switch (packet[2]) { 293 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 294 con_handle = little_endian_read_16(packet, 4); 295 att_server = att_server_for_handle(con_handle); 296 if (!att_server) break; 297 // store connection info 298 att_server->peer_addr_type = packet[7]; 299 reverse_bd_addr(&packet[8], att_server->peer_address); 300 att_server->connection.con_handle = con_handle; 301 // reset connection properties 302 att_server->state = ATT_SERVER_IDLE; 303 att_server->connection.mtu = ATT_DEFAULT_MTU; 304 att_server->connection.max_mtu = l2cap_max_le_mtu(); 305 if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 306 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 307 } 308 att_server->connection.encryption_key_size = 0; 309 att_server->connection.authenticated = 0; 310 att_server->connection.authorized = 0; 311 // workaround: identity resolving can already be complete, at least store result 312 att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 313 att_server->ir_lookup_active = 0; 314 att_server->pairing_active = 0; 315 // notify all - old 316 att_emit_event_to_all(packet, size); 317 // notify all - new 318 att_emit_connected_event(att_server); 319 break; 320 321 default: 322 break; 323 } 324 break; 325 326 case HCI_EVENT_ENCRYPTION_CHANGE: 327 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 328 // check handle 329 con_handle = little_endian_read_16(packet, 3); 330 att_server = att_server_for_handle(con_handle); 331 if (!att_server) break; 332 att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle); 333 att_server->connection.authenticated = gap_authenticated(con_handle); 334 att_server->connection.secure_connection = gap_secure_connection(con_handle); 335 log_info("encrypted key size %u, authenticated %u, secure connectipon %u", 336 att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection); 337 if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 338 // restore CCC values when encrypted 339 if (hci_event_encryption_change_get_encryption_enabled(packet)){ 340 att_server_persistent_ccc_restore(att_server); 341 } 342 } 343 att_run_for_context(att_server); 344 break; 345 346 case HCI_EVENT_DISCONNECTION_COMPLETE: 347 // check handle 348 con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 349 att_server = att_server_for_handle(con_handle); 350 if (!att_server) break; 351 att_clear_transaction_queue(&att_server->connection); 352 att_server->connection.con_handle = 0; 353 att_server->pairing_active = 0; 354 att_server->state = ATT_SERVER_IDLE; 355 if (att_server->value_indication_handle){ 356 btstack_run_loop_remove_timer(&att_server->value_indication_timer); 357 uint16_t att_handle = att_server->value_indication_handle; 358 att_server->value_indication_handle = 0; // reset error state 359 att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle); 360 } 361 // notify all - new 362 att_emit_disconnected_event(con_handle); 363 // notify all - old 364 att_emit_event_to_all(packet, size); 365 break; 366 367 // Identity Resolving 368 case SM_EVENT_IDENTITY_RESOLVING_STARTED: 369 con_handle = sm_event_identity_resolving_started_get_handle(packet); 370 att_server = att_server_for_handle(con_handle); 371 if (!att_server) break; 372 log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 373 att_server->ir_lookup_active = 1; 374 break; 375 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 376 con_handle = sm_event_identity_created_get_handle(packet); 377 att_server = att_server_for_handle(con_handle); 378 if (!att_server) return; 379 att_server->ir_lookup_active = 0; 380 att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 381 log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 382 att_run_for_context(att_server); 383 break; 384 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 385 con_handle = sm_event_identity_resolving_failed_get_handle(packet); 386 att_server = att_server_for_handle(con_handle); 387 if (!att_server) break; 388 log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 389 att_server->ir_lookup_active = 0; 390 att_server->ir_le_device_db_index = -1; 391 att_run_for_context(att_server); 392 break; 393 394 // Pairing started - delete stored CCC values 395 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 396 // - assumes that all events have the con handle as the first field 397 case SM_EVENT_JUST_WORKS_REQUEST: 398 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 399 case SM_EVENT_PASSKEY_INPUT_NUMBER: 400 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 401 con_handle = sm_event_just_works_request_get_handle(packet); 402 att_server = att_server_for_handle(con_handle); 403 if (!att_server) break; 404 att_server->pairing_active = 1; 405 log_info("SM Pairing started"); 406 if (att_server->ir_le_device_db_index < 0) break; 407 att_server_persistent_ccc_clear(att_server); 408 // index not valid anymore 409 att_server->ir_le_device_db_index = -1; 410 break; 411 412 // Bonding completed 413 case SM_EVENT_IDENTITY_CREATED: 414 con_handle = sm_event_identity_created_get_handle(packet); 415 att_server = att_server_for_handle(con_handle); 416 if (!att_server) return; 417 att_server->pairing_active = 0; 418 att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 419 att_run_for_context(att_server); 420 break; 421 422 // Pairing complete (with/without bonding=storing of pairing information) 423 case SM_EVENT_PAIRING_COMPLETE: 424 con_handle = sm_event_pairing_complete_get_handle(packet); 425 att_server = att_server_for_handle(con_handle); 426 if (!att_server) return; 427 att_server->pairing_active = 0; 428 att_run_for_context(att_server); 429 break; 430 431 // Authorization 432 case SM_EVENT_AUTHORIZATION_RESULT: { 433 con_handle = sm_event_authorization_result_get_handle(packet); 434 att_server = att_server_for_handle(con_handle); 435 if (!att_server) break; 436 att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 437 att_server_request_can_send_now(att_server); 438 break; 439 } 440 default: 441 break; 442 } 443 break; 444 #ifdef ENABLE_GATT_OVER_CLASSIC 445 case L2CAP_DATA_PACKET: 446 att_server = att_server_for_l2cap_cid(channel); 447 if (!att_server) break; 448 449 att_server_handle_att_pdu(att_server, packet, size); 450 break; 451 #endif 452 453 default: 454 break; 455 } 456 } 457 458 #ifdef ENABLE_LE_SIGNED_WRITE 459 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 460 461 att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 462 if (!att_server) return; 463 464 uint8_t hash_flipped[8]; 465 reverse_64(hash, hash_flipped); 466 if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 467 log_info("ATT Signed Write, invalid signature"); 468 att_server->state = ATT_SERVER_IDLE; 469 return; 470 } 471 log_info("ATT Signed Write, valid signature"); 472 473 // update sequence number 474 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 475 le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 476 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 477 att_server_request_can_send_now(att_server); 478 } 479 #endif 480 481 // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 482 // pre: can send now 483 // returns: 1 if packet was sent 484 static int att_server_process_validated_request(att_server_t * att_server){ 485 486 l2cap_reserve_packet_buffer(); 487 uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 488 uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 489 490 #ifdef ENABLE_ATT_DELAYED_RESPONSE 491 if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){ 492 // update state 493 att_server->state = ATT_SERVER_RESPONSE_PENDING; 494 495 // callback with handle ATT_READ_RESPONSE_PENDING for reads 496 if (att_response_size == ATT_READ_RESPONSE_PENDING){ 497 att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 498 } 499 500 // free reserved buffer 501 l2cap_release_packet_buffer(); 502 return 0; 503 } 504 #endif 505 506 // intercept "insufficient authorization" for authenticated connections to allow for user authorization 507 if ((att_response_size >= 4) 508 && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 509 && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 510 && (att_server->connection.authenticated)){ 511 512 switch (gap_authorization_state(att_server->connection.con_handle)){ 513 case AUTHORIZATION_UNKNOWN: 514 l2cap_release_packet_buffer(); 515 sm_request_pairing(att_server->connection.con_handle); 516 return 0; 517 case AUTHORIZATION_PENDING: 518 l2cap_release_packet_buffer(); 519 return 0; 520 default: 521 break; 522 } 523 } 524 525 att_server->state = ATT_SERVER_IDLE; 526 if (att_response_size == 0) { 527 l2cap_release_packet_buffer(); 528 return 0; 529 } 530 531 l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 532 533 // notify client about MTU exchange result 534 if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 535 att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 536 } 537 return 1; 538 } 539 540 #ifdef ENABLE_ATT_DELAYED_RESPONSE 541 int att_server_response_ready(hci_con_handle_t con_handle){ 542 att_server_t * att_server = att_server_for_handle(con_handle); 543 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 544 if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 545 546 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 547 att_server_request_can_send_now(att_server); 548 return ERROR_CODE_SUCCESS; 549 } 550 #endif 551 552 static void att_run_for_context(att_server_t * att_server){ 553 switch (att_server->state){ 554 case ATT_SERVER_REQUEST_RECEIVED: 555 556 // wait until re-encryption as central is complete 557 if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break; 558 559 // wait until pairing is complete 560 if (att_server->pairing_active) break; 561 562 #ifdef ENABLE_LE_SIGNED_WRITE 563 if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 564 log_info("ATT Signed Write!"); 565 if (!sm_cmac_ready()) { 566 log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 567 att_server->state = ATT_SERVER_IDLE; 568 return; 569 } 570 if (att_server->request_size < (3 + 12)) { 571 log_info("ATT Signed Write, request to short. Abort."); 572 att_server->state = ATT_SERVER_IDLE; 573 return; 574 } 575 if (att_server->ir_lookup_active){ 576 return; 577 } 578 if (att_server->ir_le_device_db_index < 0){ 579 log_info("ATT Signed Write, CSRK not available"); 580 att_server->state = ATT_SERVER_IDLE; 581 return; 582 } 583 584 // check counter 585 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 586 uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 587 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 588 if (counter_packet < counter_db){ 589 log_info("ATT Signed Write, db reports higher counter, abort"); 590 att_server->state = ATT_SERVER_IDLE; 591 return; 592 } 593 594 // signature is { sequence counter, secure hash } 595 sm_key_t csrk; 596 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 597 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 598 log_info("Orig Signature: "); 599 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 600 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 601 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); 602 return; 603 } 604 #endif 605 // move on 606 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 607 att_server_request_can_send_now(att_server); 608 break; 609 610 default: 611 break; 612 } 613 } 614 615 static int att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 616 switch (phase){ 617 case ATT_SERVER_RUN_PHASE_1_REQUESTS: 618 return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 619 case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 620 return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0); 621 case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 622 return (!btstack_linked_list_empty(&att_server->notification_requests)); 623 } 624 // avoid warning 625 return 0; 626 } 627 628 static void att_server_trigger_send_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 629 btstack_context_callback_registration_t * client; 630 switch (phase){ 631 case ATT_SERVER_RUN_PHASE_1_REQUESTS: 632 att_server_process_validated_request(att_server); 633 break; 634 case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 635 client = (btstack_context_callback_registration_t*) att_server->indication_requests; 636 btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 637 client->callback(client->context); 638 break; 639 case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 640 client = (btstack_context_callback_registration_t*) att_server->notification_requests; 641 btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 642 client->callback(client->context); 643 break; 644 } 645 } 646 647 static void att_server_handle_can_send_now(void){ 648 649 hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 650 att_server_t * request_att_server = NULL; 651 int can_send_now = 1; 652 int phase_index; 653 654 for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 655 att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 656 hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 657 while (1){ 658 btstack_linked_list_iterator_t it; 659 hci_connections_get_iterator(&it); 660 while(btstack_linked_list_iterator_has_next(&it)){ 661 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 662 att_server_t * att_server = &connection->att_server; 663 664 int data_ready = att_server_data_ready_for_phase(att_server, phase); 665 666 // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_server->connection.con_handle, skip_connections_until, data_ready); 667 668 // skip until last sender found (which is also skipped) 669 if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 670 if (data_ready && request_att_server == NULL){ 671 request_att_server = att_server; 672 } 673 if (skip_connections_until == att_server->connection.con_handle){ 674 skip_connections_until = HCI_CON_HANDLE_INVALID; 675 } 676 continue; 677 }; 678 679 if (data_ready){ 680 if (can_send_now){ 681 att_server_trigger_send_for_phase(att_server, phase); 682 last_send_con_handle = att_server->connection.con_handle; 683 can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle); 684 data_ready = att_server_data_ready_for_phase(att_server, phase); 685 if (data_ready && request_att_server == NULL){ 686 request_att_server = att_server; 687 } 688 } else { 689 request_att_server = att_server; 690 break; 691 } 692 } 693 } 694 695 // stop skipping (handles disconnect by last send connection) 696 skip_connections_until = HCI_CON_HANDLE_INVALID; 697 698 // Exit loop, if we cannot send 699 if (!can_send_now) break; 700 701 // Exit loop, if we can send but there are also no further request 702 if (request_att_server == NULL) break; 703 704 // Finally, if we still can send and there are requests, just try again 705 request_att_server = NULL; 706 } 707 // update last send con handle for round robin 708 if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 709 att_server_last_can_send_now = last_send_con_handle; 710 } 711 } 712 713 if (request_att_server == NULL) return; 714 att_server_request_can_send_now(request_att_server); 715 } 716 717 static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size){ 718 719 // handle value indication confirms 720 if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){ 721 btstack_run_loop_remove_timer(&att_server->value_indication_timer); 722 uint16_t att_handle = att_server->value_indication_handle; 723 att_server->value_indication_handle = 0; 724 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle); 725 att_server_request_can_send_now(att_server); 726 return; 727 } 728 729 // directly process command 730 // note: signed write cannot be handled directly as authentication needs to be verified 731 if (packet[0] == ATT_WRITE_COMMAND){ 732 att_handle_request(&att_server->connection, packet, size, 0); 733 return; 734 } 735 736 // check size 737 if (size > sizeof(att_server->request_buffer)) { 738 log_info("drop att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer)); 739 return; 740 } 741 742 #ifdef ENABLE_LE_SIGNED_WRITE 743 // abort signed write validation if a new request comes in (but finish previous signed write if possible) 744 if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 745 if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 746 log_info("skip new signed write request as previous is in validation"); 747 return; 748 } else { 749 log_info("abort signed write validation to process new request"); 750 att_server->state = ATT_SERVER_IDLE; 751 } 752 } 753 #endif 754 // last request still in processing? 755 if (att_server->state != ATT_SERVER_IDLE){ 756 log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 757 return; 758 } 759 760 // store request 761 att_server->state = ATT_SERVER_REQUEST_RECEIVED; 762 att_server->request_size = size; 763 memcpy(att_server->request_buffer, packet, size); 764 765 att_run_for_context(att_server); 766 } 767 768 static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 769 att_server_t * att_server; 770 771 switch (packet_type){ 772 case HCI_EVENT_PACKET: 773 switch (packet[0]){ 774 case L2CAP_EVENT_CAN_SEND_NOW: 775 att_server_handle_can_send_now(); 776 break; 777 case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 778 // GATT client has negotiated the mtu for this connection 779 att_server = att_server_for_handle(handle); 780 if (!att_server) break; 781 att_server->connection.mtu = little_endian_read_16(packet, 4); 782 break; 783 default: 784 break; 785 } 786 break; 787 788 case ATT_DATA_PACKET: 789 log_debug("ATT Packet, handle 0x%04x", handle); 790 att_server = att_server_for_handle(handle); 791 if (!att_server) break; 792 793 att_server_handle_att_pdu(att_server, packet, size); 794 break; 795 } 796 } 797 798 // --------------------- 799 // persistent CCC writes 800 static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 801 return 'B' << 24 | 'T' << 16 | 'C' << 8 | index; 802 } 803 804 static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 805 // lookup att_server instance 806 att_server_t * att_server = att_server_for_handle(con_handle); 807 if (!att_server) return; 808 int le_device_index = att_server->ir_le_device_db_index; 809 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); 810 811 // check if bonded 812 if (le_device_index < 0) return; 813 814 // get btstack_tlv 815 const btstack_tlv_t * tlv_impl = NULL; 816 void * tlv_context; 817 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 818 if (!tlv_impl) return; 819 820 // update ccc tag 821 int index; 822 uint32_t highest_seq_nr = 0; 823 uint32_t lowest_seq_nr = 0; 824 uint32_t tag_for_lowest_seq_nr = 0; 825 uint32_t tag_for_empty = 0; 826 persistent_ccc_entry_t entry; 827 for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 828 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 829 int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 830 831 // empty/invalid tag 832 if (len != sizeof(persistent_ccc_entry_t)){ 833 tag_for_empty = tag; 834 continue; 835 } 836 // update highest seq nr 837 if (entry.seq_nr > highest_seq_nr){ 838 highest_seq_nr = entry.seq_nr; 839 } 840 // find entry with lowest seq nr 841 if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){ 842 tag_for_lowest_seq_nr = tag; 843 lowest_seq_nr = entry.seq_nr; 844 } 845 846 if (entry.device_index != le_device_index) continue; 847 if (entry.att_handle != att_handle) continue; 848 849 // found matching entry 850 if (value){ 851 // update 852 if (entry.value == value) { 853 log_info("CCC Index %u: Up-to-date", index); 854 return; 855 } 856 entry.value = value; 857 entry.seq_nr = highest_seq_nr + 1; 858 log_info("CCC Index %u: Store", index); 859 tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 860 } else { 861 // delete 862 log_info("CCC Index %u: Delete", index); 863 tlv_impl->delete_tag(tlv_context, tag); 864 } 865 return; 866 } 867 868 log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 869 870 if (value == 0){ 871 // done 872 return; 873 } 874 875 uint32_t tag_to_use = 0; 876 if (tag_for_empty){ 877 tag_to_use = tag_for_empty; 878 } else if (tag_for_lowest_seq_nr){ 879 tag_to_use = tag_for_lowest_seq_nr; 880 } else { 881 // should not happen 882 return; 883 } 884 // store ccc tag 885 entry.seq_nr = highest_seq_nr + 1; 886 entry.device_index = le_device_index; 887 entry.att_handle = att_handle; 888 entry.value = value; 889 tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 890 } 891 892 static void att_server_persistent_ccc_clear(att_server_t * att_server){ 893 if (!att_server) return; 894 int le_device_index = att_server->ir_le_device_db_index; 895 log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 896 // check if bonded 897 if (le_device_index < 0) return; 898 // get btstack_tlv 899 const btstack_tlv_t * tlv_impl = NULL; 900 void * tlv_context; 901 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 902 if (!tlv_impl) return; 903 // get all ccc tag 904 int index; 905 persistent_ccc_entry_t entry; 906 for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 907 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 908 int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 909 if (len != sizeof(persistent_ccc_entry_t)) continue; 910 if (entry.device_index != le_device_index) continue; 911 // delete entry 912 log_info("CCC Index %u: Delete", index); 913 tlv_impl->delete_tag(tlv_context, tag); 914 } 915 } 916 917 static void att_server_persistent_ccc_restore(att_server_t * att_server){ 918 if (!att_server) return; 919 int le_device_index = att_server->ir_le_device_db_index; 920 log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 921 // check if bonded 922 if (le_device_index < 0) return; 923 // get btstack_tlv 924 const btstack_tlv_t * tlv_impl = NULL; 925 void * tlv_context; 926 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 927 if (!tlv_impl) return; 928 // get all ccc tag 929 int index; 930 persistent_ccc_entry_t entry; 931 for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 932 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 933 int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 934 if (len != sizeof(persistent_ccc_entry_t)) continue; 935 if (entry.device_index != le_device_index) continue; 936 // simulate write callback 937 uint16_t attribute_handle = entry.att_handle; 938 uint8_t value[2]; 939 little_endian_store_16(value, 0, entry.value); 940 att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 941 if (!callback) continue; 942 log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 943 (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 944 } 945 } 946 947 // persistent CCC writes 948 // --------------------- 949 950 // gatt service management 951 static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 952 btstack_linked_list_iterator_t it; 953 btstack_linked_list_iterator_init(&it, &service_handlers); 954 while (btstack_linked_list_iterator_has_next(&it)){ 955 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 956 if (handler->start_handle > handle) continue; 957 if (handler->end_handle < handle) continue; 958 return handler; 959 } 960 return NULL; 961 } 962 static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 963 att_service_handler_t * handler = att_service_handler_for_handle(handle); 964 if (handler) return handler->read_callback; 965 return att_server_client_read_callback; 966 } 967 968 static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 969 att_service_handler_t * handler = att_service_handler_for_handle(handle); 970 if (handler) return handler->write_callback; 971 return att_server_client_write_callback; 972 } 973 974 static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 975 att_service_handler_t * handler = att_service_handler_for_handle(handle); 976 if (handler) return handler->packet_handler; 977 return att_client_packet_handler; 978 } 979 980 static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 981 // notify all callbacks 982 btstack_linked_list_iterator_t it; 983 btstack_linked_list_iterator_init(&it, &service_handlers); 984 while (btstack_linked_list_iterator_has_next(&it)){ 985 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 986 if (!handler->write_callback) continue; 987 (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 988 } 989 if (!att_server_client_write_callback) return; 990 (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 991 } 992 993 // returns first reported error or 0 994 static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 995 btstack_linked_list_iterator_t it; 996 btstack_linked_list_iterator_init(&it, &service_handlers); 997 while (btstack_linked_list_iterator_has_next(&it)){ 998 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 999 if (!handler->write_callback) continue; 1000 uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 1001 if (error_code) return error_code; 1002 } 1003 if (!att_server_client_write_callback) return 0; 1004 return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 1005 } 1006 1007 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){ 1008 att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 1009 if (!callback) return 0; 1010 return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 1011 } 1012 1013 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){ 1014 switch (transaction_mode){ 1015 case ATT_TRANSACTION_MODE_VALIDATE: 1016 return att_validate_prepared_write(con_handle); 1017 case ATT_TRANSACTION_MODE_EXECUTE: 1018 case ATT_TRANSACTION_MODE_CANCEL: 1019 att_notify_write_callbacks(con_handle, transaction_mode); 1020 return 0; 1021 default: 1022 break; 1023 } 1024 1025 // track CCC writes 1026 if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){ 1027 att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1028 } 1029 1030 att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 1031 if (!callback) return 0; 1032 return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 1033 } 1034 1035 /** 1036 * @brief register read/write callbacks for specific handle range 1037 * @param att_service_handler_t 1038 */ 1039 void att_server_register_service_handler(att_service_handler_t * handler){ 1040 if (att_service_handler_for_handle(handler->start_handle) || 1041 att_service_handler_for_handle(handler->end_handle)){ 1042 log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 1043 return; 1044 } 1045 btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 1046 } 1047 1048 void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 1049 1050 // store callbacks 1051 att_server_client_read_callback = read_callback; 1052 att_server_client_write_callback = write_callback; 1053 1054 // register for HCI Events 1055 hci_event_callback_registration.callback = &att_event_packet_handler; 1056 hci_add_event_handler(&hci_event_callback_registration); 1057 1058 // register for SM events 1059 sm_event_callback_registration.callback = &att_event_packet_handler; 1060 sm_add_event_handler(&sm_event_callback_registration); 1061 1062 // and L2CAP ATT Server PDUs 1063 att_dispatch_register_server(att_packet_handler); 1064 1065 #ifdef ENABLE_GATT_OVER_CLASSIC 1066 // setup l2cap service 1067 l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2); 1068 #endif 1069 1070 att_set_db(db); 1071 att_set_read_callback(att_server_read_callback); 1072 att_set_write_callback(att_server_write_callback); 1073 } 1074 1075 void att_server_register_packet_handler(btstack_packet_handler_t handler){ 1076 att_client_packet_handler = handler; 1077 } 1078 1079 1080 // to be deprecated 1081 int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 1082 return att_dispatch_server_can_send_now(con_handle); 1083 } 1084 1085 int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1086 return att_server_request_to_send_notification(callback_registration, con_handle); 1087 } 1088 1089 void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1090 att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1091 att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1092 } 1093 // end of deprecated 1094 1095 int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1096 att_server_t * att_server = att_server_for_handle(con_handle); 1097 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1098 btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 1099 att_server_request_can_send_now(att_server); 1100 return ERROR_CODE_SUCCESS; 1101 } 1102 1103 int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1104 att_server_t * att_server = att_server_for_handle(con_handle); 1105 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1106 btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 1107 att_server_request_can_send_now(att_server); 1108 return ERROR_CODE_SUCCESS; 1109 } 1110 1111 int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 1112 att_server_t * att_server = att_server_for_handle(con_handle); 1113 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1114 1115 if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 1116 1117 l2cap_reserve_packet_buffer(); 1118 uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1119 uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 1120 return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 1121 } 1122 1123 int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 1124 att_server_t * att_server = att_server_for_handle(con_handle); 1125 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1126 1127 if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 1128 if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 1129 1130 // track indication 1131 att_server->value_indication_handle = attribute_handle; 1132 btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 1133 btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 1134 btstack_run_loop_add_timer(&att_server->value_indication_timer); 1135 1136 l2cap_reserve_packet_buffer(); 1137 uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1138 uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 1139 l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 1140 return 0; 1141 } 1142 1143 uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 1144 att_server_t * att_server = att_server_for_handle(con_handle); 1145 if (!att_server) return 0; 1146 return att_server->connection.mtu; 1147 } 1148