att_server: move state into hci_connection_t to handle multiple ATT clients

This commit is contained in:
Matthias Ringwald 2016-11-21 16:29:39 +01:00
parent 3bdbb0eac8
commit 18707219a5
2 changed files with 215 additions and 143 deletions

View File

@ -63,39 +63,31 @@
#include "hci_dump.h"
#include "l2cap.h"
static void att_run(void);
static void att_run_for_context(att_server_t * att_server);
// max ATT request matches L2CAP PDU -- allow to use smaller buffer
#ifndef ATT_REQUEST_BUFFER_SIZE
#define ATT_REQUEST_BUFFER_SIZE HCI_ACL_PAYLOAD_SIZE
#endif
typedef enum {
ATT_SERVER_IDLE,
ATT_SERVER_REQUEST_RECEIVED,
ATT_SERVER_W4_SIGNED_WRITE_VALIDATION,
ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED,
} att_server_state_t;
static att_connection_t att_connection;
static att_server_state_t att_server_state;
static uint8_t att_client_addr_type;
static bd_addr_t att_client_address;
static uint8_t att_client_waiting_for_can_send;
static uint16_t att_request_size = 0;
static uint8_t att_request_buffer[ATT_REQUEST_BUFFER_SIZE];
static int att_ir_le_device_db_index = -1;
static int att_ir_lookup_active = 0;
static int att_handle_value_indication_handle = 0;
static btstack_timer_source_t att_handle_value_indication_timer;
// global
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t sm_event_callback_registration;
static btstack_packet_handler_t att_client_packet_handler = NULL;
static btstack_packet_handler_t att_client_packet_handler = NULL;
static btstack_linked_list_t can_send_now_clients;
static uint8_t att_client_waiting_for_can_send;
static btstack_linked_list_t can_send_now_clients;
static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (!hci_connection) return NULL;
return &hci_connection->att_server;
}
static att_server_t * att_server_for_state(att_server_state_t state){
btstack_linked_list_iterator_t it;
hci_connections_get_iterator(&it);
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
att_server_t * att_server = &connection->att_server;
if (att_server->state == state) return att_server;
}
return NULL;
}
static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
if (!att_client_packet_handler) return;
@ -132,13 +124,18 @@ static void att_emit_can_send_now_event(void){
}
static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
uint16_t att_handle = att_handle_value_indication_handle;
att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection.con_handle, att_handle);
hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts);
att_server_t * att_server = att_server_for_handle(con_handle);
if (!att_server) return;
uint16_t att_handle = att_server->value_indication_handle;
att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
}
static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t event_address;
att_server_t * att_server;
hci_con_handle_t con_handle;
switch (packet_type) {
case HCI_EVENT_PACKET:
@ -147,19 +144,24 @@ static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uin
case HCI_EVENT_LE_META:
switch (packet[2]) {
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
con_handle = little_endian_read_16(packet, 4);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
// store connection info
att_client_addr_type = packet[7];
reverse_bd_addr(&packet[8], att_client_address);
att_server->peer_addr_type = packet[7];
reverse_bd_addr(&packet[8], att_server->peer_address);
att_server->connection.con_handle = con_handle;
// reset connection properties
att_connection.con_handle = little_endian_read_16(packet, 4);
att_connection.mtu = ATT_DEFAULT_MTU;
att_connection.max_mtu = l2cap_max_le_mtu();
if (att_connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
att_connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
att_server->state = ATT_SERVER_IDLE;
att_server->connection.mtu = ATT_DEFAULT_MTU;
att_server->connection.max_mtu = l2cap_max_le_mtu();
if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
}
att_connection.encryption_key_size = 0;
att_connection.authenticated = 0;
att_connection.authorized = 0;
att_server->connection.encryption_key_size = 0;
att_server->connection.authenticated = 0;
att_server->connection.authorized = 0;
att_server->ir_le_device_db_index = -1;
break;
default:
@ -170,42 +172,55 @@ static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uin
case HCI_EVENT_ENCRYPTION_CHANGE:
case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
// check handle
if (att_connection.con_handle != little_endian_read_16(packet, 3)) break;
att_connection.encryption_key_size = sm_encryption_key_size(att_connection.con_handle);
att_connection.authenticated = sm_authenticated(att_connection.con_handle);
con_handle = little_endian_read_16(packet, 3);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle);
att_server->connection.authenticated = sm_authenticated(con_handle);
break;
case HCI_EVENT_DISCONNECTION_COMPLETE:
// check handle
if (att_connection.con_handle != hci_event_disconnection_complete_get_connection_handle(packet)) break;
att_clear_transaction_queue(&att_connection);
att_connection.con_handle = 0;
att_handle_value_indication_handle = 0; // reset error state
att_server_state = ATT_SERVER_IDLE;
con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
att_clear_transaction_queue(&att_server->connection);
att_server->connection.con_handle = 0;
att_server->value_indication_handle = 0; // reset error state
att_server->state = ATT_SERVER_IDLE;
break;
case SM_EVENT_IDENTITY_RESOLVING_STARTED:
con_handle = sm_event_identity_resolving_started_get_handle(packet);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
att_ir_lookup_active = 1;
att_server->ir_lookup_active = 1;
break;
case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
att_ir_lookup_active = 0;
att_ir_le_device_db_index = little_endian_read_16(packet, 11);
log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index);
att_run();
con_handle = sm_event_identity_resolving_succeeded_get_handle(packet);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
att_server->ir_lookup_active = 0;
att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index_internal(packet);
log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_server->ir_le_device_db_index);
att_run_for_context(att_server);
break;
case SM_EVENT_IDENTITY_RESOLVING_FAILED:
con_handle = sm_event_identity_resolving_failed_get_handle(packet);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
att_ir_lookup_active = 0;
att_ir_le_device_db_index = -1;
att_run();
att_server->ir_lookup_active = 0;
att_server->ir_le_device_db_index = -1;
att_run_for_context(att_server);
break;
case SM_EVENT_AUTHORIZATION_RESULT: {
if (packet[4] != att_client_addr_type) break;
reverse_bd_addr(&packet[5], event_address);
if (memcmp(event_address, att_client_address, 6) != 0) break;
att_connection.authorized = packet[11];
att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
con_handle = sm_event_authorization_result_get_handle(packet);
att_server = att_server_for_handle(con_handle);
if (!att_server) break;
att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
att_dispatch_server_request_can_send_now_event(con_handle);
break;
}
default:
@ -219,48 +234,45 @@ static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uin
static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
if (att_server_state != ATT_SERVER_W4_SIGNED_WRITE_VALIDATION) return;
att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
if (!att_server) return;
uint8_t hash_flipped[8];
reverse_64(hash, hash_flipped);
if (memcmp(hash_flipped, &att_request_buffer[att_request_size-8], 8)){
if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
log_info("ATT Signed Write, invalid signature");
att_server_state = ATT_SERVER_IDLE;
att_server->state = ATT_SERVER_IDLE;
return;
}
log_info("ATT Signed Write, valid signature");
// update sequence number
uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12);
le_device_db_remote_counter_set(att_ir_le_device_db_index, counter_packet+1);
att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
}
#if 0
static int att_server_ready_to_send(att_connection_t * connection){
}
#endif
// pre: att_server_state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
// pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
// pre: can send now
// returns: 1 if packet was sent
static int att_server_process_validated_request(hci_con_handle_t con_handle){
static int att_server_process_validated_request(att_server_t * att_server){
l2cap_reserve_packet_buffer();
uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
uint16_t att_response_size = att_handle_request(&att_connection, att_request_buffer, att_request_size, att_response_buffer);
uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
// intercept "insufficient authorization" for authenticated connections to allow for user authorization
if ((att_response_size >= 4)
&& (att_response_buffer[0] == ATT_ERROR_RESPONSE)
&& (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
&& (att_connection.authenticated)){
&& (att_server->connection.authenticated)){
switch (sm_authorization_state(att_connection.con_handle)){
switch (sm_authorization_state(att_server->connection.con_handle)){
case AUTHORIZATION_UNKNOWN:
l2cap_release_packet_buffer();
sm_request_pairing(att_connection.con_handle);
sm_request_pairing(att_server->connection.con_handle);
return 0;
case AUTHORIZATION_PENDING:
l2cap_release_packet_buffer();
@ -270,99 +282,105 @@ static int att_server_process_validated_request(hci_con_handle_t con_handle){
}
}
att_server_state = ATT_SERVER_IDLE;
att_server->state = ATT_SERVER_IDLE;
if (att_response_size == 0) {
l2cap_release_packet_buffer();
return 0;
}
l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
// notify client about MTU exchange result
if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
att_emit_mtu_event(att_connection.con_handle, att_connection.mtu);
att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
}
return 1;
}
static void att_run(void){
switch (att_server_state){
static void att_run_for_context(att_server_t * att_server){
switch (att_server->state){
case ATT_SERVER_REQUEST_RECEIVED:
if (att_request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
log_info("ATT Signed Write!");
if (!sm_cmac_ready()) {
log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
att_server_state = ATT_SERVER_IDLE;
att_server->state = ATT_SERVER_IDLE;
return;
}
if (att_request_size < (3 + 12)) {
if (att_server->request_size < (3 + 12)) {
log_info("ATT Signed Write, request to short. Abort.");
att_server_state = ATT_SERVER_IDLE;
att_server->state = ATT_SERVER_IDLE;
return;
}
if (att_ir_lookup_active){
if (att_server->ir_lookup_active){
return;
}
if (att_ir_le_device_db_index < 0){
if (att_server->ir_le_device_db_index < 0){
log_info("ATT Signed Write, CSRK not available");
att_server_state = ATT_SERVER_IDLE;
att_server->state = ATT_SERVER_IDLE;
return;
}
// check counter
uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12);
uint32_t counter_db = le_device_db_remote_counter_get(att_ir_le_device_db_index);
uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
if (counter_packet < counter_db){
log_info("ATT Signed Write, db reports higher counter, abort");
att_server_state = ATT_SERVER_IDLE;
att_server->state = ATT_SERVER_IDLE;
return;
}
// signature is { sequence counter, secure hash }
sm_key_t csrk;
le_device_db_remote_csrk_get(att_ir_le_device_db_index, csrk);
att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
log_info("Orig Signature: ");
log_info_hexdump( &att_request_buffer[att_request_size-8], 8);
uint16_t attribute_handle = little_endian_read_16(att_request_buffer, 1);
sm_cmac_signed_write_start(csrk, att_request_buffer[0], attribute_handle, att_request_size - 15, &att_request_buffer[3], counter_packet, att_signed_write_handle_cmac_result);
log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
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);
return;
}
// move on
att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
break;
default:
break;
}
}
}
static void att_server_handle_can_send_now(void){
// NOTE: we get l2cap fixed channel instead of con_handle
// TODO: get con_handle
if (att_server_state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
int sent = att_server_process_validated_request(att_connection.con_handle);
if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
return;
btstack_linked_list_iterator_t it;
hci_connections_get_iterator(&it);
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
att_server_t * att_server = &connection->att_server;
if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
int sent = att_server_process_validated_request(att_server);
if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
return;
}
}
}
while (!btstack_linked_list_empty(&can_send_now_clients)){
// handle first client
btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
hci_con_handle_t con_handle = (uintptr_t) client->context;
btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
client->callback(client->context);
// request again if needed
if (!att_dispatch_server_can_send_now(att_connection.con_handle)){
if (!att_dispatch_server_can_send_now(con_handle)){
if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
att_dispatch_server_request_can_send_now_event(con_handle);
}
return;
}
@ -375,6 +393,9 @@ static void att_server_handle_can_send_now(void){
}
static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
att_server_t * att_server;
switch (packet_type){
case HCI_EVENT_PACKET:
@ -383,40 +404,44 @@ static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pa
break;
case ATT_DATA_PACKET:
log_info("ATT Packet, handle 0x%04x", handle);
att_server = att_server_for_handle(handle);
if (!att_server) break;
// handle value indication confirms
if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_handle_value_indication_handle){
btstack_run_loop_remove_timer(&att_handle_value_indication_timer);
uint16_t att_handle = att_handle_value_indication_handle;
att_handle_value_indication_handle = 0;
att_handle_value_indication_notify_client(0, att_connection.con_handle, att_handle);
if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
btstack_run_loop_remove_timer(&att_server->value_indication_timer);
uint16_t att_handle = att_server->value_indication_handle;
att_server->value_indication_handle = 0;
att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
return;
}
// directly process command
// note: signed write cannot be handled directly as authentication needs to be verified
if (packet[0] == ATT_WRITE_COMMAND){
att_handle_request(&att_connection, packet, size, 0);
att_handle_request(&att_server->connection, packet, size, 0);
return;
}
// check size
if (size > sizeof(att_request_buffer)) {
log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_request_buffer %u", packet[0], size, (int) sizeof(att_request_buffer));
if (size > sizeof(att_server->request_buffer)) {
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));
return;
}
// last request still in processing?
if (att_server_state != ATT_SERVER_IDLE){
log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server_state);
if (att_server->state != ATT_SERVER_IDLE){
log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
return;
}
// store request
att_server_state = ATT_SERVER_REQUEST_RECEIVED;
att_request_size = size;
memcpy(att_request_buffer, packet, size);
att_server->state = ATT_SERVER_REQUEST_RECEIVED;
att_server->request_size = size;
memcpy(att_server->request_buffer, packet, size);
att_run();
att_run_for_context(att_server);
break;
}
}
@ -434,7 +459,6 @@ void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_
// and L2CAP ATT Server PDUs
att_dispatch_register_server(att_packet_handler);
att_server_state = ATT_SERVER_IDLE;
att_set_db(db);
att_set_read_callback(read_callback);
att_set_write_callback(write_callback);
@ -445,17 +469,14 @@ void att_server_register_packet_handler(btstack_packet_handler_t handler){
att_client_packet_handler = handler;
}
// NOTE: con_handle is ignored for now as only a single connection is supported
// TODO: support multiple connections
int att_server_can_send_packet_now(hci_con_handle_t con_handle){
if (att_connection.con_handle == 0) return 0;
return att_dispatch_server_can_send_now(att_connection.con_handle);
return att_dispatch_server_can_send_now(con_handle);
}
void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
log_info("att_server_request_can_send_now_event 0x%04x", con_handle);
att_client_waiting_for_can_send = 1;
att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
att_dispatch_server_request_can_send_now_event(con_handle);
}
void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
@ -464,32 +485,40 @@ void att_server_register_can_send_now_callback(btstack_context_callback_registra
callback_registration->callback(callback_registration->context);
return;
}
callback_registration->context = (void*)(uintptr_t) con_handle;
btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
att_dispatch_server_request_can_send_now_event(con_handle);
}
int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
att_server_t * att_server = att_server_for_handle(con_handle);
if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
l2cap_reserve_packet_buffer();
uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
uint16_t size = att_prepare_handle_value_notification(&att_connection, attribute_handle, value, value_len, packet_buffer);
return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
}
int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
att_server_t * att_server = att_server_for_handle(con_handle);
if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
// track indication
att_handle_value_indication_handle = attribute_handle;
btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout);
btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
btstack_run_loop_add_timer(&att_handle_value_indication_timer);
att_server->value_indication_handle = attribute_handle;
btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
btstack_run_loop_add_timer(&att_server->value_indication_timer);
l2cap_reserve_packet_buffer();
uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
uint16_t size = att_prepare_handle_value_indication(&att_connection, attribute_handle, value, value_len, packet_buffer);
l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
return 0;
}

View File

@ -56,6 +56,10 @@
#include "gap.h"
#include "hci_transport.h"
#ifdef ENABLE_BLE
#include "ble/att_db.h"
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
@ -378,6 +382,42 @@ typedef struct sm_connection {
int sm_le_db_index;
} sm_connection_t;
//
// ATT Server
//
// max ATT request matches L2CAP PDU -- allow to use smaller buffer
#ifndef ATT_REQUEST_BUFFER_SIZE
#define ATT_REQUEST_BUFFER_SIZE HCI_ACL_PAYLOAD_SIZE
#endif
typedef enum {
ATT_SERVER_IDLE,
ATT_SERVER_REQUEST_RECEIVED,
ATT_SERVER_W4_SIGNED_WRITE_VALIDATION,
ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED,
} att_server_state_t;
typedef struct {
att_server_state_t state;
uint8_t peer_addr_type;
bd_addr_t peer_address;
int ir_le_device_db_index;
int ir_lookup_active;
int value_indication_handle;
btstack_timer_source_t value_indication_timer;
att_connection_t connection;
uint16_t request_size;
uint8_t request_buffer[ATT_REQUEST_BUFFER_SIZE];
} att_server_t;
//
typedef struct {
// linked list - assert: first field
btstack_linked_item_t item;
@ -437,6 +477,9 @@ typedef struct {
#ifdef ENABLE_BLE
// LE Security Manager
sm_connection_t sm_connection;
// ATT Server
att_server_t att_server;
#endif
} hci_connection_t;