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 <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <inttypes.h> 50 51 #include "btstack_config.h" 52 53 #include "att_dispatch.h" 54 #include "ble/att_db.h" 55 #include "ble/att_server.h" 56 #include "ble/core.h" 57 #include "ble/le_device_db.h" 58 #include "ble/sm.h" 59 #include "btstack_debug.h" 60 #include "btstack_event.h" 61 #include "btstack_memory.h" 62 #include "btstack_run_loop.h" 63 #include "gap.h" 64 #include "hci.h" 65 #include "hci_dump.h" 66 #include "l2cap.h" 67 #include "btstack_tlv.h" 68 69 #ifndef GATT_SERVER_NUM_PERSISTENT_CCC 70 #define GATT_SERVER_NUM_PERSISTENT_CCC 20 71 #endif 72 73 static void att_run_for_context(att_server_t * att_server); 74 75 // global 76 static btstack_packet_callback_registration_t hci_event_callback_registration; 77 static btstack_packet_callback_registration_t sm_event_callback_registration; 78 static btstack_packet_handler_t att_client_packet_handler = NULL; 79 static btstack_linked_list_t can_send_now_clients; 80 static btstack_linked_list_t service_handlers; 81 static uint8_t att_client_waiting_for_can_send; 82 83 static att_read_callback_t att_server_client_read_callback; 84 static att_write_callback_t att_server_client_write_callback; 85 86 // track CCC 1-entry cache 87 // static att_server_t * att_persistent_ccc_server; 88 // static hci_con_handle_t att_persistent_ccc_con_handle; 89 90 static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){ 91 hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 92 if (!hci_connection) return NULL; 93 return &hci_connection->att_server; 94 } 95 96 #ifdef ENABLE_LE_SIGNED_WRITE 97 static att_server_t * att_server_for_state(att_server_state_t state){ 98 btstack_linked_list_iterator_t it; 99 hci_connections_get_iterator(&it); 100 while(btstack_linked_list_iterator_has_next(&it)){ 101 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 102 att_server_t * att_server = &connection->att_server; 103 if (att_server->state == state) return att_server; 104 } 105 return NULL; 106 } 107 #endif 108 109 static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 110 if (!att_client_packet_handler) return; 111 112 uint8_t event[7]; 113 int pos = 0; 114 event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 115 event[pos++] = sizeof(event) - 2; 116 event[pos++] = status; 117 little_endian_store_16(event, pos, client_handle); 118 pos += 2; 119 little_endian_store_16(event, pos, attribute_handle); 120 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 121 } 122 123 static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 124 if (!att_client_packet_handler) return; 125 126 uint8_t event[6]; 127 int pos = 0; 128 event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 129 event[pos++] = sizeof(event) - 2; 130 little_endian_store_16(event, pos, con_handle); 131 pos += 2; 132 little_endian_store_16(event, pos, mtu); 133 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 134 } 135 136 static void att_emit_can_send_now_event(void){ 137 if (!att_client_packet_handler) return; 138 139 uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 140 (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 141 } 142 143 static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 144 void * context = btstack_run_loop_get_timer_context(ts); 145 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 146 att_server_t * att_server = att_server_for_handle(con_handle); 147 if (!att_server) return; 148 uint16_t att_handle = att_server->value_indication_handle; 149 att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 150 } 151 152 static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 153 154 UNUSED(channel); // ok: there is no channel 155 UNUSED(size); // ok: handling own l2cap events 156 157 att_server_t * att_server; 158 hci_con_handle_t con_handle; 159 160 switch (packet_type) { 161 162 case HCI_EVENT_PACKET: 163 switch (hci_event_packet_get_type(packet)) { 164 165 case HCI_EVENT_LE_META: 166 switch (packet[2]) { 167 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 168 con_handle = little_endian_read_16(packet, 4); 169 att_server = att_server_for_handle(con_handle); 170 if (!att_server) break; 171 // store connection info 172 att_server->peer_addr_type = packet[7]; 173 reverse_bd_addr(&packet[8], att_server->peer_address); 174 att_server->connection.con_handle = con_handle; 175 // reset connection properties 176 att_server->state = ATT_SERVER_IDLE; 177 att_server->connection.mtu = ATT_DEFAULT_MTU; 178 att_server->connection.max_mtu = l2cap_max_le_mtu(); 179 if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 180 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 181 } 182 att_server->connection.encryption_key_size = 0; 183 att_server->connection.authenticated = 0; 184 att_server->connection.authorized = 0; 185 att_server->ir_le_device_db_index = -1; 186 break; 187 188 default: 189 break; 190 } 191 break; 192 193 case HCI_EVENT_ENCRYPTION_CHANGE: 194 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 195 // check handle 196 con_handle = little_endian_read_16(packet, 3); 197 att_server = att_server_for_handle(con_handle); 198 if (!att_server) break; 199 att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle); 200 att_server->connection.authenticated = sm_authenticated(con_handle); 201 break; 202 203 case HCI_EVENT_DISCONNECTION_COMPLETE: 204 // check handle 205 con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 206 att_server = att_server_for_handle(con_handle); 207 if (!att_server) break; 208 att_clear_transaction_queue(&att_server->connection); 209 att_server->connection.con_handle = 0; 210 att_server->value_indication_handle = 0; // reset error state 211 att_server->state = ATT_SERVER_IDLE; 212 break; 213 214 case SM_EVENT_IDENTITY_RESOLVING_STARTED: 215 con_handle = sm_event_identity_resolving_started_get_handle(packet); 216 att_server = att_server_for_handle(con_handle); 217 if (!att_server) break; 218 log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 219 att_server->ir_lookup_active = 1; 220 break; 221 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 222 con_handle = sm_event_identity_resolving_succeeded_get_handle(packet); 223 att_server = att_server_for_handle(con_handle); 224 if (!att_server) break; 225 att_server->ir_lookup_active = 0; 226 att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 227 log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_server->ir_le_device_db_index); 228 att_run_for_context(att_server); 229 break; 230 case SM_EVENT_IDENTITY_RESOLVING_FAILED: 231 con_handle = sm_event_identity_resolving_failed_get_handle(packet); 232 att_server = att_server_for_handle(con_handle); 233 if (!att_server) break; 234 log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 235 att_server->ir_lookup_active = 0; 236 att_server->ir_le_device_db_index = -1; 237 att_run_for_context(att_server); 238 break; 239 case SM_EVENT_AUTHORIZATION_RESULT: { 240 con_handle = sm_event_authorization_result_get_handle(packet); 241 att_server = att_server_for_handle(con_handle); 242 if (!att_server) break; 243 att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 244 att_dispatch_server_request_can_send_now_event(con_handle); 245 break; 246 } 247 default: 248 break; 249 } 250 break; 251 default: 252 break; 253 } 254 } 255 256 #ifdef ENABLE_LE_SIGNED_WRITE 257 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 258 259 att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 260 if (!att_server) return; 261 262 uint8_t hash_flipped[8]; 263 reverse_64(hash, hash_flipped); 264 if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 265 log_info("ATT Signed Write, invalid signature"); 266 att_server->state = ATT_SERVER_IDLE; 267 return; 268 } 269 log_info("ATT Signed Write, valid signature"); 270 271 // update sequence number 272 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 273 le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 274 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 275 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 276 } 277 #endif 278 279 // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 280 // pre: can send now 281 // returns: 1 if packet was sent 282 static int att_server_process_validated_request(att_server_t * att_server){ 283 284 l2cap_reserve_packet_buffer(); 285 uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 286 uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 287 288 // intercept "insufficient authorization" for authenticated connections to allow for user authorization 289 if ((att_response_size >= 4) 290 && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 291 && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 292 && (att_server->connection.authenticated)){ 293 294 switch (sm_authorization_state(att_server->connection.con_handle)){ 295 case AUTHORIZATION_UNKNOWN: 296 l2cap_release_packet_buffer(); 297 sm_request_pairing(att_server->connection.con_handle); 298 return 0; 299 case AUTHORIZATION_PENDING: 300 l2cap_release_packet_buffer(); 301 return 0; 302 default: 303 break; 304 } 305 } 306 307 att_server->state = ATT_SERVER_IDLE; 308 if (att_response_size == 0) { 309 l2cap_release_packet_buffer(); 310 return 0; 311 } 312 313 l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 314 315 // notify client about MTU exchange result 316 if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 317 att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 318 } 319 return 1; 320 } 321 322 static void att_run_for_context(att_server_t * att_server){ 323 switch (att_server->state){ 324 case ATT_SERVER_REQUEST_RECEIVED: 325 #ifdef ENABLE_LE_SIGNED_WRITE 326 if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 327 log_info("ATT Signed Write!"); 328 if (!sm_cmac_ready()) { 329 log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 330 att_server->state = ATT_SERVER_IDLE; 331 return; 332 } 333 if (att_server->request_size < (3 + 12)) { 334 log_info("ATT Signed Write, request to short. Abort."); 335 att_server->state = ATT_SERVER_IDLE; 336 return; 337 } 338 if (att_server->ir_lookup_active){ 339 return; 340 } 341 if (att_server->ir_le_device_db_index < 0){ 342 log_info("ATT Signed Write, CSRK not available"); 343 att_server->state = ATT_SERVER_IDLE; 344 return; 345 } 346 347 // check counter 348 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 349 uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 350 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 351 if (counter_packet < counter_db){ 352 log_info("ATT Signed Write, db reports higher counter, abort"); 353 att_server->state = ATT_SERVER_IDLE; 354 return; 355 } 356 357 // signature is { sequence counter, secure hash } 358 sm_key_t csrk; 359 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 360 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 361 log_info("Orig Signature: "); 362 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 363 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 364 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); 365 return; 366 } 367 #endif 368 // move on 369 att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 370 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 371 break; 372 373 default: 374 break; 375 } 376 } 377 378 static void att_server_handle_can_send_now(void){ 379 380 // NOTE: we get l2cap fixed channel instead of con_handle 381 382 btstack_linked_list_iterator_t it; 383 hci_connections_get_iterator(&it); 384 while(btstack_linked_list_iterator_has_next(&it)){ 385 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 386 att_server_t * att_server = &connection->att_server; 387 if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){ 388 int sent = att_server_process_validated_request(att_server); 389 if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){ 390 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 391 return; 392 } 393 } 394 } 395 396 while (!btstack_linked_list_empty(&can_send_now_clients)){ 397 // handle first client 398 btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients; 399 hci_con_handle_t con_handle = (uintptr_t) client->context; 400 btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client); 401 client->callback(client->context); 402 403 // request again if needed 404 if (!att_dispatch_server_can_send_now(con_handle)){ 405 if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){ 406 att_dispatch_server_request_can_send_now_event(con_handle); 407 } 408 return; 409 } 410 } 411 412 if (att_client_waiting_for_can_send){ 413 att_client_waiting_for_can_send = 0; 414 att_emit_can_send_now_event(); 415 } 416 } 417 418 static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 419 420 att_server_t * att_server; 421 422 switch (packet_type){ 423 424 case HCI_EVENT_PACKET: 425 if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; 426 att_server_handle_can_send_now(); 427 break; 428 429 case ATT_DATA_PACKET: 430 log_info("ATT Packet, handle 0x%04x", handle); 431 att_server = att_server_for_handle(handle); 432 if (!att_server) break; 433 434 // handle value indication confirms 435 if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){ 436 btstack_run_loop_remove_timer(&att_server->value_indication_timer); 437 uint16_t att_handle = att_server->value_indication_handle; 438 att_server->value_indication_handle = 0; 439 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle); 440 return; 441 } 442 443 // directly process command 444 // note: signed write cannot be handled directly as authentication needs to be verified 445 if (packet[0] == ATT_WRITE_COMMAND){ 446 att_handle_request(&att_server->connection, packet, size, 0); 447 return; 448 } 449 450 // check size 451 if (size > sizeof(att_server->request_buffer)) { 452 log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer)); 453 return; 454 } 455 456 // last request still in processing? 457 if (att_server->state != ATT_SERVER_IDLE){ 458 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 459 return; 460 } 461 462 // store request 463 att_server->state = ATT_SERVER_REQUEST_RECEIVED; 464 att_server->request_size = size; 465 memcpy(att_server->request_buffer, packet, size); 466 467 att_run_for_context(att_server); 468 break; 469 } 470 } 471 472 // --------------------- 473 // persistent CCC writes 474 // TLV value format: le_device_index(8), attribute_handle(16), value (8) 475 static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 476 return 'B' << 24 | 'T' << 16 | 'C' << 8 | index; 477 } 478 479 static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 480 // lookup att_server instance 481 att_server_t * att_server = att_server_for_handle(con_handle); 482 if (!att_server) return; 483 int le_device_index = att_server->ir_le_device_db_index; 484 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); 485 // check if bonded 486 if (le_device_index < 0) return; 487 // get btstack_tlv 488 const btstack_tlv_t * tlv_impl = NULL; 489 void * tlv_context; 490 btstack_tlv_get_instance(&tlv_impl, &tlv_context); 491 if (!tlv_impl) return; 492 // update ccc tag 493 int index; 494 uint8_t buffer[4]; 495 int free_index = -1; 496 for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){ 497 uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 498 int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer)); 499 if (len == 0){ 500 free_index = index; 501 continue; 502 } 503 if (len != sizeof(buffer)) continue; 504 if (buffer[0] != le_device_index) continue; 505 if (little_endian_read_16(buffer, 1) != att_handle) continue; 506 // update 507 buffer[3] = value; 508 tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer)); 509 return; 510 } 511 if (free_index < 0) return; 512 // store ccc tag 513 buffer[0] = le_device_index; 514 little_endian_store_16(buffer, 1, att_handle); 515 buffer[3] = value; 516 uint32_t tag = att_server_persistent_ccc_tag_for_index(free_index); 517 tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer)); 518 } 519 520 // persistent CCC writes 521 // --------------------- 522 523 // gatt service management 524 static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 525 btstack_linked_list_iterator_t it; 526 btstack_linked_list_iterator_init(&it, &service_handlers); 527 while (btstack_linked_list_iterator_has_next(&it)){ 528 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 529 if (handler->start_handle > handle) continue; 530 if (handler->end_handle < handle) continue; 531 return handler; 532 } 533 return NULL; 534 } 535 static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 536 att_service_handler_t * handler = att_service_handler_for_handle(handle); 537 if (handler) return handler->read_callback; 538 return att_server_client_read_callback; 539 } 540 541 static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 542 att_service_handler_t * handler = att_service_handler_for_handle(handle); 543 if (handler) return handler->write_callback; 544 return att_server_client_write_callback; 545 } 546 547 static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 548 // notify all callbacks 549 btstack_linked_list_iterator_t it; 550 btstack_linked_list_iterator_init(&it, &service_handlers); 551 while (btstack_linked_list_iterator_has_next(&it)){ 552 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 553 if (!handler->write_callback) continue; 554 (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 555 } 556 if (!att_server_client_write_callback) return; 557 (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 558 } 559 560 // returns first reported error or 0 561 static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 562 btstack_linked_list_iterator_t it; 563 btstack_linked_list_iterator_init(&it, &service_handlers); 564 while (btstack_linked_list_iterator_has_next(&it)){ 565 att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 566 if (!handler->write_callback) continue; 567 uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 568 if (error_code) return error_code; 569 } 570 if (!att_server_client_write_callback) return 0; 571 return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 572 } 573 574 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){ 575 att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 576 return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 577 } 578 579 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){ 580 switch (transaction_mode){ 581 case ATT_TRANSACTION_MODE_VALIDATE: 582 return att_validate_prepared_write(con_handle); 583 case ATT_TRANSACTION_MODE_EXECUTE: 584 case ATT_TRANSACTION_MODE_CANCEL: 585 att_notify_write_callbacks(con_handle, transaction_mode); 586 return 0; 587 default: 588 break; 589 } 590 att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 591 592 // track CCC writes 593 if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){ 594 att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 595 } 596 597 return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 598 } 599 600 /** 601 * @brief register read/write callbacks for specific handle range 602 * @param att_service_handler_t 603 */ 604 void att_server_register_service_handler(att_service_handler_t * handler){ 605 if (att_service_handler_for_handle(handler->start_handle) || 606 att_service_handler_for_handle(handler->end_handle)){ 607 log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 608 return; 609 } 610 btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 611 } 612 613 void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 614 615 // store callbacks 616 att_server_client_read_callback = read_callback; 617 att_server_client_write_callback = write_callback; 618 619 // register for HCI Events 620 hci_event_callback_registration.callback = &att_event_packet_handler; 621 hci_add_event_handler(&hci_event_callback_registration); 622 623 // register for SM events 624 sm_event_callback_registration.callback = &att_event_packet_handler; 625 sm_add_event_handler(&sm_event_callback_registration); 626 627 // and L2CAP ATT Server PDUs 628 att_dispatch_register_server(att_packet_handler); 629 630 att_set_db(db); 631 att_set_read_callback(att_server_read_callback); 632 att_set_write_callback(att_server_write_callback); 633 634 } 635 636 void att_server_register_packet_handler(btstack_packet_handler_t handler){ 637 att_client_packet_handler = handler; 638 } 639 640 int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 641 return att_dispatch_server_can_send_now(con_handle); 642 } 643 644 void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 645 log_debug("att_server_request_can_send_now_event 0x%04x", con_handle); 646 att_client_waiting_for_can_send = 1; 647 att_dispatch_server_request_can_send_now_event(con_handle); 648 } 649 650 void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 651 // check if can send already 652 if (att_dispatch_server_can_send_now(con_handle)){ 653 callback_registration->callback(callback_registration->context); 654 return; 655 } 656 callback_registration->context = (void*)(uintptr_t) con_handle; 657 btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration); 658 att_dispatch_server_request_can_send_now_event(con_handle); 659 } 660 661 662 int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 663 att_server_t * att_server = att_server_for_handle(con_handle); 664 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 665 666 if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 667 668 l2cap_reserve_packet_buffer(); 669 uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 670 uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 671 return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 672 } 673 674 int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 675 att_server_t * att_server = att_server_for_handle(con_handle); 676 if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 677 678 if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 679 if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 680 681 // track indication 682 att_server->value_indication_handle = attribute_handle; 683 btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 684 btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 685 btstack_run_loop_add_timer(&att_server->value_indication_timer); 686 687 l2cap_reserve_packet_buffer(); 688 uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 689 uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 690 l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 691 return 0; 692 } 693