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_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 141 btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 142 if (!packet_handler) return; 143 144 uint8_t event[7]; 145 int pos = 0; 146 event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 147 event[pos++] = sizeof(event) - 2; 148 event[pos++] = status; 149 little_endian_store_16(event, pos, client_handle); 150 pos += 2; 151 little_endian_store_16(event, pos, attribute_handle); 152 (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 153 } 154 155 static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 156 // dispatch to app level handler 157 if (att_client_packet_handler){ 158 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 159 } 160 161 // dispatch to service handlers 162 btstack_linked_list_iterator_t it; 163 btstack_linked_list_iterator_init(&it, &service_handlers); 164 while (btstack_linked_list_iterator_has_next(&it)){ 165 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 166 if (!handler->packet_handler) continue; 167 (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 168 } 169 } 170 171 static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 172 uint8_t event[6]; 173 int pos = 0; 174 event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 175 event[pos++] = sizeof(event) - 2; 176 little_endian_store_16(event, pos, con_handle); 177 pos += 2; 178 little_endian_store_16(event, pos, mtu); 179 180 // also dispatch to GATT Clients 181 att_dispatch_server_mtu_exchanged(con_handle, mtu); 182 183 // dispatch to app level handler and service handlers 184 att_emit_event_to_all(&event[0], sizeof(event)); 185 } 186 187 static void att_emit_can_send_now_event(void * context){ 188 UNUSED(context); 189 if (!att_client_packet_handler) return; 190 191 uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 192 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 193 } 194 195 static void att_emit_connected_event(att_server_t * att_server){ 196 uint8_t event[11]; 197 int pos = 0; 198 event[pos++] = ATT_EVENT_CONNECTED; 199 event[pos++] = sizeof(event) - 2; 200 event[pos++] = att_server->peer_addr_type; 201 reverse_bd_addr(att_server->peer_address, &event[pos]); 202 pos += 6; 203 little_endian_store_16(event, pos, att_server->connection.con_handle); 204 pos += 2; 205 206 // dispatch to app level handler and service handlers 207 att_emit_event_to_all(&event[0], sizeof(event)); 208 } 209 210 211 static void att_emit_disconnected_event(uint16_t con_handle){ 212 uint8_t event[4]; 213 int pos = 0; 214 event[pos++] = ATT_EVENT_DISCONNECTED; 215 event[pos++] = sizeof(event) - 2; 216 little_endian_store_16(event, pos, con_handle); 217 pos += 2; 218 219 // dispatch to app level handler and service handlers 220 att_emit_event_to_all(&event[0], sizeof(event)); 221 } 222 223 static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 224 void * context = btstack_run_loop_get_timer_context(ts); 225 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 226 att_server_t * att_server = att_server_for_handle(con_handle); 227 if (!att_server) return; 228 // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer 229 // (that's why we don't reset the value_indication_handle) 230 uint16_t att_handle = att_server->value_indication_handle; 231 att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 232 } 233 234 static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 235 236 UNUSED(channel); // ok: there is no channel 237 UNUSED(size); // ok: handling own l2cap events 238 239 att_server_t * att_server; 240 hci_con_handle_t con_handle; 241 #ifdef ENABLE_GATT_OVER_CLASSIC 242 bd_addr_t address; 243 #endif 244 245 switch (packet_type) { 246 247 case HCI_EVENT_PACKET: 248 switch (hci_event_packet_get_type(packet)) { 249 250 #ifdef ENABLE_GATT_OVER_CLASSIC 251 case L2CAP_EVENT_INCOMING_CONNECTION: 252 l2cap_event_incoming_connection_get_address(packet, address); 253 l2cap_accept_connection(channel); 254 printf("Accept incoming connection from %s\n", bd_addr_to_str(address)); 255 break; 256 case L2CAP_EVENT_CHANNEL_OPENED: 257 con_handle = l2cap_event_channel_opened_get_handle(packet); 258 att_server = att_server_for_handle(con_handle); 259 if (!att_server) break; 260 // store connection info 261 att_server->peer_addr_type = BD_ADDR_TYPE_CLASSIC; 262 l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 263 att_server->connection.con_handle = con_handle; 264 att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 265 // reset connection properties 266 att_server->state = ATT_SERVER_IDLE; 267 att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 268 att_server->connection.max_mtu = l2cap_max_mtu(); 269 if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 270 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 271 } 272 // TODO: use security level from underlying HCI connection 273 att_server->connection.encryption_key_size = 0; 274 att_server->connection.authenticated = 0; 275 att_server->connection.authorized = 0; 276 // TODO: what to do about le device db? 277 att_server->pairing_active = 0; 278 printf("Connection opened %s, l2cap cid %04x, \n", bd_addr_to_str(address), att_server->l2cap_cid); 279 break; 280 #endif 281 case HCI_EVENT_LE_META: 282 switch (packet[2]) { 283 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 284 con_handle = little_endian_read_16(packet, 4); 285 att_server = att_server_for_handle(con_handle); 286 if (!att_server) break; 287 // store connection info 288 att_server->peer_addr_type = packet[7]; 289 reverse_bd_addr(&packet[8], att_server->peer_address); 290 att_server->connection.con_handle = con_handle; 291 // reset connection properties 292 att_server->state = ATT_SERVER_IDLE; 293 att_server->connection.mtu = ATT_DEFAULT_MTU; 294 att_server->connection.max_mtu = l2cap_max_le_mtu(); 295 if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 296 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 297 } 298 att_server->connection.encryption_key_size = 0; 299 att_server->connection.authenticated = 0; 300 att_server->connection.authorized = 0; 301 // workaround: identity resolving can already be complete, at least store result 302 att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 303 att_server->ir_lookup_active = 0; 304 att_server->pairing_active = 0; 305 // notify all - old 306 att_emit_event_to_all(packet, size); 307 // notify all - new 308 att_emit_connected_event(att_server); 309 break; 310 311 default: 312 break; 313 } 314 break; 315 316 case HCI_EVENT_ENCRYPTION_CHANGE: 317 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 318 // check handle 319 con_handle = little_endian_read_16(packet, 3); 320 att_server = att_server_for_handle(con_handle); 321 if (!att_server) break; 322 att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle); 323 att_server->connection.authenticated = gap_authenticated(con_handle); 324 att_server->connection.secure_connection = gap_secure_connection(con_handle); 325 log_info("encrypted key size %u, authenticated %u, secure connectipon %u", 326 att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection); 327 if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 328 // restore CCC values when encrypted 329 if (hci_event_encryption_change_get_encryption_enabled(packet)){ 330 att_server_persistent_ccc_restore(att_server); 331 } 332 } 333 att_run_for_context(att_server); 334 break; 335 336 case HCI_EVENT_DISCONNECTION_COMPLETE: 337 // check handle 338 con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 339 att_server = att_server_for_handle(con_handle); 340 if (!att_server) break; 341 att_clear_transaction_queue(&att_server->connection); 342 att_server->connection.con_handle = 0; 343 att_server->pairing_active = 0; 344 att_server->state = ATT_SERVER_IDLE; 345 if (att_server->value_indication_handle){ 346 btstack_run_loop_remove_timer(&att_server->value_indication_timer); 347 uint16_t att_handle = att_server->value_indication_handle; 348 att_server->value_indication_handle = 0; // reset error state 349 att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle); 350 } 351 // notify all - new 352 att_emit_disconnected_event(con_handle); 353 // notify all - old 354 att_emit_event_to_all(packet, size); 355 break; 356 357 // Identity Resolving 358 case SM_EVENT_IDENTITY_RESOLVING_STARTED: 359 con_handle = sm_event_identity_resolving_started_get_handle(packet); 360 att_server = att_server_for_handle(con_handle); 361 if (!att_server) break; 362 log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 363 att_server->ir_lookup_active = 1; 364 break; 365 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 366 con_handle = sm_event_identity_created_get_handle(packet); 367 att_server = att_server_for_handle(con_handle); 368 if (!att_server) return; 369 att_server->ir_lookup_active = 0; 370 att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 371 log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 372 att_run_for_context(att_server); 373 break; 374 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 375 con_handle = sm_event_identity_resolving_failed_get_handle(packet); 376 att_server = att_server_for_handle(con_handle); 377 if (!att_server) break; 378 log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 379 att_server->ir_lookup_active = 0; 380 att_server->ir_le_device_db_index = -1; 381 att_run_for_context(att_server); 382 break; 383 384 // Pairing started - delete stored CCC values 385 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 386 // - assumes that all events have the con handle as the first field 387 case SM_EVENT_JUST_WORKS_REQUEST: 388 case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 389 case SM_EVENT_PASSKEY_INPUT_NUMBER: 390 case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 391 con_handle = sm_event_just_works_request_get_handle(packet); 392 att_server = att_server_for_handle(con_handle); 393 if (!att_server) break; 394 att_server->pairing_active = 1; 395 log_info("SM Pairing started"); 396 if (att_server->ir_le_device_db_index < 0) break; 397 att_server_persistent_ccc_clear(att_server); 398 // index not valid anymore 399 att_server->ir_le_device_db_index = -1; 400 break; 401 402 // Bonding completed 403 case SM_EVENT_IDENTITY_CREATED: 404 con_handle = sm_event_identity_created_get_handle(packet); 405 att_server = att_server_for_handle(con_handle); 406 if (!att_server) return; 407 att_server->pairing_active = 0; 408 att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 409 att_run_for_context(att_server); 410 break; 411 412 // Pairing complete (with/without bonding=storing of pairing information) 413 case SM_EVENT_PAIRING_COMPLETE: 414 con_handle = sm_event_pairing_complete_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_run_for_context(att_server); 419 break; 420 421 // Authorization 422 case SM_EVENT_AUTHORIZATION_RESULT: { 423 con_handle = sm_event_authorization_result_get_handle(packet); 424 att_server = att_server_for_handle(con_handle); 425 if (!att_server) break; 426 att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 427 att_dispatch_server_request_can_send_now_event(con_handle); 428 break; 429 } 430 default: 431 break; 432 } 433 break; 434 #ifdef ENABLE_GATT_OVER_CLASSIC 435 case L2CAP_DATA_PACKET: 436 att_server = att_server_for_l2cap_cid(channel); 437 if (!att_server) break; 438 439 att_server_handle_att_pdu(att_server, packet, size); 440 break; 441 #endif 442 443 default: 444 break; 445 } 446 } 447 448 #ifdef ENABLE_LE_SIGNED_WRITE 449 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 450 451 att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 452 if (!att_server) return; 453 454 uint8_t hash_flipped[8]; 455 reverse_64(hash, hash_flipped); 456 if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 457 log_info("ATT Signed Write, invalid signature"); 458 att_server->state = ATT_SERVER_IDLE; 459 return; 460 } 461 log_info("ATT Signed Write, valid signature"); 462 463 // update sequence number 464 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 465 le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 466 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 467 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 468 } 469 #endif 470 471 // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 472 // pre: can send now 473 // returns: 1 if packet was sent 474 static int att_server_process_validated_request(att_server_t * att_server){ 475 476 l2cap_reserve_packet_buffer(); 477 uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 478 uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 479 480 #ifdef ENABLE_ATT_DELAYED_RESPONSE 481 if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){ 482 // update state 483 att_server->state = ATT_SERVER_RESPONSE_PENDING; 484 485 // callback with handle ATT_READ_RESPONSE_PENDING for reads 486 if (att_response_size == ATT_READ_RESPONSE_PENDING){ 487 att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 488 } 489 490 // free reserved buffer 491 l2cap_release_packet_buffer(); 492 return 0; 493 } 494 #endif 495 496 // intercept "insufficient authorization" for authenticated connections to allow for user authorization 497 if ((att_response_size >= 4) 498 && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 499 && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 500 && (att_server->connection.authenticated)){ 501 502 switch (gap_authorization_state(att_server->connection.con_handle)){ 503 case AUTHORIZATION_UNKNOWN: 504 l2cap_release_packet_buffer(); 505 sm_request_pairing(att_server->connection.con_handle); 506 return 0; 507 case AUTHORIZATION_PENDING: 508 l2cap_release_packet_buffer(); 509 return 0; 510 default: 511 break; 512 } 513 } 514 515 att_server->state = ATT_SERVER_IDLE; 516 if (att_response_size == 0) { 517 l2cap_release_packet_buffer(); 518 return 0; 519 } 520 521 l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 522 523 // notify client about MTU exchange result 524 if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 525 att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 526 } 527 return 1; 528 } 529 530 #ifdef ENABLE_ATT_DELAYED_RESPONSE 531 int att_server_response_ready(hci_con_handle_t con_handle){ 532 att_server_t * att_server = att_server_for_handle(con_handle); 533 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 534 if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 535 536 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 537 att_dispatch_server_request_can_send_now_event(con_handle); 538 return ERROR_CODE_SUCCESS; 539 } 540 #endif 541 542 static void att_server_request_can_send_now(att_server_t * att_server){ 543 #ifdef ENABLE_GATT_OVER_CLASSIC 544 if (att_server->l2cap_cid != 0){ 545 l2cap_request_can_send_now_event(att_server->l2cap_cid); 546 return; 547 } 548 #endif 549 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 550 } 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 request_con_handle = HCI_CON_HANDLE_INVALID; 650 hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 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_con_handle == HCI_CON_HANDLE_INVALID){ 671 request_con_handle = att_server->connection.con_handle; 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_con_handle == HCI_CON_HANDLE_INVALID){ 686 request_con_handle = att_server->connection.con_handle; 687 } 688 } else { 689 request_con_handle = att_server->connection.con_handle; 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_con_handle == HCI_CON_HANDLE_INVALID) break; 703 704 // Finally, if we still can send and there are requests, just try again 705 request_con_handle = HCI_CON_HANDLE_INVALID; 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_con_handle == HCI_CON_HANDLE_INVALID) return; 714 att_dispatch_server_request_can_send_now_event(request_con_handle); 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_dispatch_server_request_can_send_now_event(con_handle); 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_dispatch_server_request_can_send_now_event(con_handle); 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