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