int att_server_notify(uint16_t handle, uint8_t *value, uint16_t value_len){ if (!l2cap_can_send_fixed_channel_packet_now(att_connection.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, handle, value, value_len, packet_buffer); return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); }
int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 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_server->connection, attribute_handle, value, value_len, packet_buffer); return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); }
void send_ucd_packet(void){ l2cap_reserve_packet_buffer(); int ucd_size = 50; uint8_t * ucd_buffer = l2cap_get_outgoing_buffer(); bt_store_16(ucd_buffer, 0, 0x2211); int i; for (i=2; i< ucd_size ; i++){ ucd_buffer[i] = i; } l2cap_send_prepared_connectionless(handle, L2CAP_CID_CONNECTIONLESS_CHANNEL, ucd_size); }
// 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(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_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE if (att_response_size == ATT_READ_RESPONSE_PENDING){ // update state att_server->state = ATT_SERVER_READ_RESPONSE_PENDING; // callback with handle ATT_READ_RESPONSE_PENDING att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); // free reserved buffer l2cap_release_packet_buffer(); return 0; } #endif // 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_server->connection.authenticated)){ switch (gap_authorization_state(att_server->connection.con_handle)){ case AUTHORIZATION_UNKNOWN: l2cap_release_packet_buffer(); sm_request_pairing(att_server->connection.con_handle); return 0; case AUTHORIZATION_PENDING: l2cap_release_packet_buffer(); return 0; default: break; } } att_server->state = ATT_SERVER_IDLE; if (att_response_size == 0) { l2cap_release_packet_buffer(); return 0; } 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_server->connection.con_handle, att_server->connection.mtu); } return 1; }
int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ log_info("l2cap_send_internal cid %u, cannot send\n", cid); return BTSTACK_ACL_BUFFERS_FULL; } uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); memcpy(&acl_buffer[8], data, len); return l2cap_send_prepared_connectionless(handle, cid, len); }
int att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len){ if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS; if (!l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; // track indication att_handle_value_indication_handle = handle; run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout); run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); run_loop_add_timer(&att_handle_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, handle, value, value_len, packet_buffer); l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); return 0; }
int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 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_PROGRESS; if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; // track indication 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_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; }
static void att_run(void){ switch (att_server_state){ case ATT_SERVER_IDLE: case ATT_SERVER_W4_SIGNED_WRITE_VALIDATION: return; case ATT_SERVER_REQUEST_RECEIVED: if (att_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; return; } if (att_request_size < (3 + 12)) { log_info("ATT Signed Write, request to short. Abort."); att_server_state = ATT_SERVER_IDLE; return; } if (att_ir_lookup_active){ return; } if (att_ir_le_device_db_index < 0){ log_info("ATT Signed Write, CSRK not available"); att_server_state = ATT_SERVER_IDLE; return; } // check counter uint32_t counter_packet = READ_BT_32(att_request_buffer, att_request_size-12); uint32_t counter_db = le_device_db_remote_counter_get(att_ir_le_device_db_index); log_info("ATT Signed Write, DB counter %u, packet counter %u", 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; return; } // signature is { sequence counter, secure hash } sm_key_t csrk; le_device_db_csrk_get(att_ir_le_device_db_index, csrk); att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; log_info("Orig Signature: "); hexdump( &att_request_buffer[att_request_size-8], 8); sm_cmac_start(csrk, att_request_size - 12, att_request_buffer, counter_packet, att_signed_write_handle_cmac_result); return; } // NOTE: fall through for regular commands case ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED: if (!l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle)) return; 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); // 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)){ switch (sm_authorization_state(att_client_addr_type, att_client_address)){ case AUTHORIZATION_UNKNOWN: l2cap_release_packet_buffer(); sm_request_authorization(att_client_addr_type, att_client_address); return; case AUTHORIZATION_PENDING: l2cap_release_packet_buffer(); return; default: break; } } att_server_state = ATT_SERVER_IDLE; if (att_response_size == 0) { l2cap_release_packet_buffer(); return; } l2cap_send_prepared_connectionless(att_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); } break; } }