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