Microchip® Advanced Software Framework

hci.c File Reference
#include "btstack_config.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_linked_list.h"
#include "btstack_memory.h"
#include "bluetooth_company_id.h"
#include "bluetooth_data_types.h"
#include "gap.h"
#include "hci.h"
#include "hci_cmd.h"
#include "hci_dump.h"
#include "ad_parser.h"

Macros

#define __BTSTACK_FILE__   "hci.c"
 
#define GAP_INQUIRY_DURATION_MAX   0x30
 
#define GAP_INQUIRY_DURATION_MIN   0x01
 
#define GAP_INQUIRY_MAX_NAME_LEN   32
 
#define GAP_INQUIRY_STATE_ACTIVE   0x80
 
#define GAP_INQUIRY_STATE_IDLE   0
 
#define GAP_INQUIRY_STATE_W2_CANCEL   0x81
 
#define GAP_INQUIRY_STATE_W4_CANCELLED   0x82
 
#define GAP_PAIRING_STATE_IDLE   0
 
#define GAP_PAIRING_STATE_SEND_CONFIRMATION   5
 
#define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE   6
 
#define GAP_PAIRING_STATE_SEND_PASSKEY   3
 
#define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE   4
 
#define GAP_PAIRING_STATE_SEND_PIN   1
 
#define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE   2
 
#define GAP_REMOTE_NAME_STATE_IDLE   0
 
#define GAP_REMOTE_NAME_STATE_W2_SEND   1
 
#define GAP_REMOTE_NAME_STATE_W4_COMPLETE   2
 
#define HCI_CONNECTION_TIMEOUT_MS   10000
 
#define HCI_RESET_RESEND_TIMEOUT_MS   200
 

Functions

static void acl_handler (uint8_t *packet, int size)
 
static hci_connection_tcreate_connection_for_bd_addr_and_type (bd_addr_t addr, bd_addr_type_t addr_type)
 create connection for given address More...
 
static void event_handler (uint8_t *packet, int size)
 
uint8_t gap_disconnect (hci_con_handle_t handle)
 Disconnect connection with handle. More...
 
void gap_get_connection_parameter_range (le_connection_parameter_range_t *range)
 get le connection parameter range More...
 
gap_connection_type_t gap_get_connection_type (hci_con_handle_t connection_handle)
 Get connection type. More...
 
void gap_local_bd_addr (bd_addr_t address_buffer)
 Gets local address. More...
 
void gap_set_connection_parameter_range (le_connection_parameter_range_t *range)
 set le connection parameter range More...
 
void gap_set_local_name (const char *local_name)
 Sets local name. More...
 
static int gap_ssp_supported (void)
 
void hci_add_event_handler (btstack_packet_callback_registration_t *callback_handler)
 Add event packet handler. More...
 
int hci_can_send_acl_le_packet_now (void)
 Check hci packet buffer is free and an LE acl packet can be sent to controller. More...
 
int hci_can_send_acl_packet_now (hci_con_handle_t con_handle)
 Check hci packet buffer is free and an acl packet for the given handle can be sent to controller. More...
 
static int hci_can_send_comand_packet_transport (void)
 
int hci_can_send_command_packet_now (void)
 Check if CMD packet can be sent to controller. More...
 
static int hci_can_send_prepared_acl_packet_for_address_type (bd_addr_type_t address_type)
 
int hci_can_send_prepared_acl_packet_now (hci_con_handle_t con_handle)
 Check if acl packet for the given handle can be sent to controller. More...
 
static int hci_classic_supported (void)
 
void hci_close (void)
 Shutdown HCI. More...
 
hci_connection_thci_connection_for_bd_addr_and_type (bd_addr_t addr, bd_addr_type_t addr_type)
 get connection for given address More...
 
hci_connection_thci_connection_for_handle (hci_con_handle_t con_handle)
 get connection for a given handle More...
 
void hci_connections_get_iterator (btstack_linked_list_iterator_t *it)
 get hci connections iterator More...
 
void hci_disconnect_all (void)
 Disconnect all HCI connections. More...
 
void hci_disconnect_security_block (hci_con_handle_t con_handle)
 Disconn because of security block. More...
 
static void hci_emit_acl_packet (uint8_t *packet, uint16_t size)
 
static void hci_emit_dedicated_bonding_result (bd_addr_t address, uint8_t status)
 
static void hci_emit_disconnection_complete (hci_con_handle_t con_handle, uint8_t reason)
 
static void hci_emit_event (uint8_t *event, uint16_t size, int dump)
 
static void hci_emit_hci_open_failed (void)
 
static void hci_emit_nr_connections_changed (void)
 
void hci_emit_state (void)
 Emit current HCI state. More...
 
uint16_t hci_get_manufacturer (void)
 Get Manufactured. More...
 
uint8_t * hci_get_outgoing_packet_buffer (void)
 Get pointer for outgoing packet buffer. More...
 
HCI_STATE hci_get_state (void)
 Get state. More...
 
void hci_init (const hci_transport_t *transport, const void *config)
 Set up HCI. More...
 
static void hci_init_done (void)
 
static void hci_initialization_timeout_handler (btstack_timer_source_t *ds)
 
static void hci_initializing_event_handler (uint8_t *packet, uint16_t size)
 
static void hci_initializing_next_state (void)
 
static void hci_initializing_run (void)
 
static int hci_is_le_connection (hci_connection_t *connection)
 
int hci_is_packet_buffer_reserved (void)
 Check if outgoing packet buffer is reserved. More...
 
static int hci_le_supported (void)
 
uint16_t hci_max_acl_data_packet_length (void)
 Get maximal ACL Classic data packet length based on used buffer size. More...
 
int hci_non_flushable_packet_boundary_flag_supported (void)
 Check if ACL packets marked as non flushable can be sent. More...
 
static int hci_number_free_acl_slots_for_connection_type (bd_addr_type_t address_type)
 
int hci_number_free_acl_slots_for_handle (hci_con_handle_t con_handle)
 Get number of free acl slots for packets of given handle. More...
 
int hci_power_control (HCI_POWER_MODE power_mode)
 Requests the change of BTstack power mode. More...
 
static void hci_power_control_off (void)
 
static int hci_power_control_on (void)
 
static void hci_power_control_sleep (void)
 
static int hci_power_control_wake (void)
 
static void hci_power_transition_to_initializing (void)
 
void hci_register_acl_packet_handler (btstack_packet_handler_t handler)
 Register HCI packet handlers. More...
 
void hci_release_packet_buffer (void)
 Release outgoing packet buffer More...
 
int hci_reserve_packet_buffer (void)
 Reserves outgoing packet buffer. More...
 
static void hci_run (void)
 
int hci_send_acl_packet_buffer (int size)
 Send acl packet prepared in hci packet buffer. More...
 
static int hci_send_acl_packet_fragments (hci_connection_t *connection)
 
int hci_send_cmd (const hci_cmd_t *cmd,...)
 pre: numcmds >= 0 - it's allowed to send a command to the controller More...
 
int hci_send_cmd_packet (uint8_t *packet, int size)
 Send complete CMD packet. More...
 
int hci_send_cmd_va_arg (const hci_cmd_t *cmd, va_list argptr)
 va_list version of hci_send_cmd More...
 
void hci_set_bd_addr (bd_addr_t addr)
 Set Public BD ADDR - passed on to Bluetooth chipset during init if supported in bt_control_h. More...
 
void hci_set_chipset (const btstack_chipset_t *chipset_driver)
 Configure Bluetooth chipset driver. More...
 
void hci_set_control (const btstack_control_t *hardware_control)
 Configure Bluetooth hardware control. More...
 
void hci_set_hardware_error_callback (void(*fn)(uint8_t error))
 Set callback for Bluetooth Hardware Error. More...
 
static void hci_shutdown_connection (hci_connection_t *conn)
 
static void hci_state_reset (void)
 
static int hci_transport_can_send_prepared_packet_now (uint8_t packet_type)
 
static int hci_transport_synchronous (void)
 
static uint32_t hci_transport_uart_get_main_baud_rate (void)
 
static int nr_hci_connections (void)
 count connections More...
 
static void packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size)
 

Variables

static hci_stack_thci_stack = NULL
 
static hci_stack_t hci_stack_static
 

#define __BTSTACK_FILE__   "hci.c"
#define GAP_INQUIRY_DURATION_MAX   0x30

Referenced by hci_run().

#define GAP_INQUIRY_DURATION_MIN   0x01

Referenced by hci_run().

#define GAP_INQUIRY_MAX_NAME_LEN   32
#define GAP_INQUIRY_STATE_ACTIVE   0x80

Referenced by event_handler(), and hci_run().

#define GAP_INQUIRY_STATE_IDLE   0

Referenced by event_handler().

#define GAP_INQUIRY_STATE_W2_CANCEL   0x81

Referenced by hci_run().

#define GAP_INQUIRY_STATE_W4_CANCELLED   0x82

Referenced by event_handler(), and hci_run().

#define GAP_PAIRING_STATE_IDLE   0

Referenced by hci_run().

#define GAP_PAIRING_STATE_SEND_CONFIRMATION   5

Referenced by hci_run().

#define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE   6

Referenced by hci_run().

#define GAP_PAIRING_STATE_SEND_PASSKEY   3

Referenced by hci_run().

#define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE   4

Referenced by hci_run().

#define GAP_PAIRING_STATE_SEND_PIN   1

Referenced by hci_run().

#define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE   2

Referenced by hci_run().

#define GAP_REMOTE_NAME_STATE_IDLE   0

Referenced by event_handler().

#define GAP_REMOTE_NAME_STATE_W2_SEND   1

Referenced by hci_run().

#define GAP_REMOTE_NAME_STATE_W4_COMPLETE   2

Referenced by event_handler(), and hci_run().

#define HCI_CONNECTION_TIMEOUT_MS   10000

Referenced by event_handler().

#define HCI_RESET_RESEND_TIMEOUT_MS   200

Referenced by hci_initializing_run().

static void event_handler ( uint8_t *  packet,
int  size 
)
static

References hci_stack_t::acl_data_packet_length, hci_stack_t::acl_fragmentation_pos, hci_stack_t::acl_fragmentation_total_size, hci_stack_t::acl_packets_total_num, hci_connection_t::address, hci_connection_t::address_type, hci_connection_t::authentication_flags, bd_addr_copy(), bd_addr_to_str(), BD_ADDR_TYPE_CLASSIC, BD_ADDR_TYPE_SCO, hci_stack_t::bondable, BONDING_DEDICATED, BONDING_DISCONNECT_DEDICATED_DONE, BONDING_EMIT_COMPLETE_ON_DISCONNECT, hci_connection_t::bonding_flags, BONDING_RECEIVED_REMOTE_FEATURES, BONDING_REMOTE_SUPPORTS_SSP, BONDING_REQUEST_REMOTE_FEATURES, BONDING_SEND_AUTHENTICATE_REQUEST, BONDING_SEND_ENCRYPTION_REQUEST, hci_connection_t::bonding_status, btstack_linked_list_remove(), btstack_memory_hci_connection_free(), btstack_min(), btstack_run_loop_add_timer(), btstack_run_loop_set_timer(), CHANGED_COMBINATION_KEY, hci_connection_t::con_handle, CONNECTION_ENCRYPTED, hci_stack_t::connections, create_connection_for_bd_addr_and_type(), hci_stack_t::decline_addr, hci_stack_t::decline_reason, btstack_link_key_db_t::delete_link_key, DENY_PIN_CODE_REQUEST, hci_stack_t::discoverable, gap_drop_link_key_for_bd_addr(), GAP_EVENT_INQUIRY_COMPLETE, GAP_INQUIRY_STATE_ACTIVE, GAP_INQUIRY_STATE_IDLE, GAP_INQUIRY_STATE_W4_CANCELLED, GAP_REMOTE_NAME_STATE_IDLE, GAP_REMOTE_NAME_STATE_W4_COMPLETE, gap_security_level_for_link_key_type(), gap_store_link_key_for_bd_addr(), HANDLE_LINK_KEY_REQUEST, hci_stack_t::hardware_error_callback, hci_classic_supported(), hci_connection_for_bd_addr_and_type(), hci_connection_for_handle(), HCI_CONNECTION_TIMEOUT_MS, hci_emit_dedicated_bonding_result(), hci_emit_event(), hci_emit_nr_connections_changed(), HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT, HCI_EVENT_COMMAND_COMPLETE, HCI_EVENT_COMMAND_STATUS, HCI_EVENT_CONNECTION_COMPLETE, HCI_EVENT_CONNECTION_REQUEST, HCI_EVENT_DISCONNECTION_COMPLETE, HCI_EVENT_ENCRYPTION_CHANGE, HCI_EVENT_EXTENDED_INQUIRY_RESPONSE, HCI_EVENT_HARDWARE_ERROR, HCI_EVENT_INQUIRY_COMPLETE, HCI_EVENT_INQUIRY_RESULT, HCI_EVENT_INQUIRY_RESULT_WITH_RSSI, HCI_EVENT_IO_CAPABILITY_REQUEST, HCI_EVENT_IS_COMMAND_COMPLETE, HCI_EVENT_LE_META, HCI_EVENT_LINK_KEY_NOTIFICATION, HCI_EVENT_LINK_KEY_REQUEST, HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS, hci_event_packet_get_type(), HCI_EVENT_PIN_CODE_REQUEST, hci_event_pin_code_request_get_bd_addr(), HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE, HCI_EVENT_ROLE_CHANGE, HCI_EVENT_SCO_CAN_SEND_NOW, HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE, HCI_EVENT_TRANSPORT_PACKET_SENT, HCI_EVENT_USER_CONFIRMATION_REQUEST, HCI_EVENT_USER_PASSKEY_REQUEST, hci_extended_sco_link_supported(), HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE, hci_initializing_event_handler(), hci_initializing_next_state(), hci_inquiry_cancel, hci_is_le_connection(), hci_le_read_buffer_size, hci_le_read_maximum_data_length, hci_le_read_white_list_size, hci_le_supported(), hci_stack_t::hci_packet_buffer, hci_power_control_off(), hci_power_control_on(), hci_read_bd_addr, hci_read_buffer_size, hci_read_local_name, hci_read_local_supported_commands, hci_read_local_supported_features, hci_read_local_version_information, hci_release_packet_buffer(), HCI_ROLE_MASTER, HCI_ROLE_SLAVE, hci_run(), hci_shutdown_connection(), HCI_STATE_FALLING_ASLEEP, HCI_STATE_INITIALIZING, HCI_SUBEVENT_LE_ADVERTISING_REPORT, HCI_SUBEVENT_LE_CONNECTION_COMPLETE, hci_stack_t::hci_transport, hci_transport_synchronous(), hci_write_scan_enable, hci_write_synchronous_flow_control_enable, hci_stack_t::inquiry_state, hci_stack_t::le_acl_packets_total_num, LE_ADVERTISEMENT_TASKS_ENABLE, LE_CONNECTING_IDLE, LE_CONNECTING_WHITELIST, hci_stack_t::le_data_packets_length, LE_SCANNING, LEGACY_PAIRING_ACTIVE, hci_stack_t::link_key_db, hci_connection_t::link_key_type, little_endian_read_16(), hci_stack_t::local_bd_addr, hci_stack_t::local_supported_commands, hci_stack_t::local_supported_features, log_error, log_info, hci_stack_t::manufacturer, hci_connection_t::num_acl_packets_sent, hci_stack_t::num_cmd_packets, hci_connection_t::num_sco_packets_sent, OFFSET_OF_DATA_IN_COMMAND_COMPLETE, OPEN, hci_stack_t::packet_types, READ_ACL_CONNECTION_HANDLE, RECEIVED_CONNECTION_REQUEST, RECEIVED_DISCONNECTION_COMPLETE, RECV_IO_CAPABILITIES_REQUEST, RECV_LINK_KEY_NOTIFICATION, RECV_LINK_KEY_REQUEST, hci_stack_t::remote_name_state, hci_connection_t::remote_supported_feature_eSCO, hci_connection_t::requested_security_level, reverse_bd_addr(), hci_connection_t::role, hci_stack_t::sco_data_packet_length, hci_stack_t::sco_packets_total_num, hci_stack_t::sco_voice_setting_active, SEND_IO_CAPABILITIES_REPLY, SEND_USER_CONFIRM_REPLY, SEND_USER_PASSKEY_REPLY, btstack_link_key_db_t::set_local_bd_addr, hci_transport_t::set_sco_config, hci_stack_t::ssp_auto_accept, SSP_PAIRING_ACTIVE, hci_connection_t::state, hci_stack_t::state, status, hci_stack_t::substate, hci_stack_t::synchronous_flow_control_enabled, and hci_connection_t::timeout.

Referenced by packet_handler().

uint8_t gap_disconnect ( hci_con_handle_t  handle)

Disconnect connection with handle.

Parameters
handle

References hci_connection_for_handle(), hci_emit_disconnection_complete(), hci_run(), SEND_DISCONNECT, and hci_connection_t::state.

void gap_get_connection_parameter_range ( le_connection_parameter_range_t range)

get le connection parameter range

Set accepted connection parameter range.

Returns
le connection parameter range struct

References hci_stack_t::le_connection_parameter_range.

void gap_local_bd_addr ( bd_addr_t  address_buffer)

Gets local address.

References hci_stack_t::local_bd_addr.

Referenced by sm_run().

void gap_set_connection_parameter_range ( le_connection_parameter_range_t range)

set le connection parameter range

Get accepted connection parameter range.

References hci_stack_t::le_connection_parameter_range.

void gap_set_local_name ( const char *  local_name)

Sets local name.

Note
has to be done before stack starts up
Default name is 'BTstack 00:00:00:00:00:00'
'00:00:00:00:00:00' in local_name will be replaced with actual bd addr
Parameters
nameis not copied, make sure memory is accessible during stack startup

References hci_stack_t::local_name.

static int gap_ssp_supported ( void  )
static
int hci_can_send_acl_le_packet_now ( void  )

Check hci packet buffer is free and an LE acl packet can be sent to controller.

References BD_ADDR_TYPE_LE_PUBLIC, hci_can_send_prepared_acl_packet_for_address_type(), and hci_stack_t::hci_packet_buffer_reserved.

Referenced by att_packet_handler(), and l2cap_notify_channel_can_send().

int hci_can_send_acl_packet_now ( hci_con_handle_t  con_handle)

Check hci packet buffer is free and an acl packet for the given handle can be sent to controller.

References hci_can_send_prepared_acl_packet_now(), and hci_stack_t::hci_packet_buffer_reserved.

Referenced by l2cap_can_send_fixed_channel_packet_now(), l2cap_notify_channel_can_send(), l2cap_run(), and l2cap_send_connectionless().

static int hci_can_send_comand_packet_transport ( void  )
static
int hci_can_send_command_packet_now ( void  )
static int hci_can_send_prepared_acl_packet_for_address_type ( bd_addr_type_t  address_type)
static
int hci_can_send_prepared_acl_packet_now ( hci_con_handle_t  con_handle)
static int hci_classic_supported ( void  )
static
hci_connection_t* hci_connection_for_bd_addr_and_type ( bd_addr_t  addr,
bd_addr_type_t  addr_type 
)

get connection for given address

Get internal hci_connection_t for given Bluetooth addres.

Returns
connection OR NULL, if not found

References hci_connection_t::address, hci_connection_t::address_type, btstack_linked_list_iterator_has_next(), btstack_linked_list_iterator_init(), btstack_linked_list_iterator_next(), hci_stack_t::connections, and NULL.

Referenced by event_handler(), and hci_send_cmd_packet().

void hci_connections_get_iterator ( btstack_linked_list_iterator_t it)

get hci connections iterator

Get connection iterator.

Returns
hci connections iterator

References btstack_linked_list_iterator_init(), and hci_stack_t::connections.

Referenced by att_server_handle_can_send_now(), l2cap_run(), and sm_run().

void hci_disconnect_security_block ( hci_con_handle_t  con_handle)

Disconn because of security block.

Called by L2CAP

References BONDING_DISCONNECT_SECURITY_BLOCK, hci_connection_t::bonding_flags, and hci_connection_for_handle().

Referenced by l2cap_hci_event_handler(), and l2cap_run().

static void hci_emit_acl_packet ( uint8_t *  packet,
uint16_t  size 
)
static
static void hci_emit_dedicated_bonding_result ( bd_addr_t  address,
uint8_t  status 
)
static
static void hci_emit_disconnection_complete ( hci_con_handle_t  con_handle,
uint8_t  reason 
)
static
static void hci_emit_hci_open_failed ( void  )
static
static void hci_emit_nr_connections_changed ( void  )
static
void hci_emit_state ( void  )

Emit current HCI state.

Called by daemon

References BTSTACK_EVENT_STATE, hci_emit_event(), and hci_stack_t::state.

Referenced by hci_init_done(), hci_power_control(), and hci_run().

uint16_t hci_get_manufacturer ( void  )

Get Manufactured.

Returns
manufacturer id

References hci_stack_t::manufacturer.

uint8_t* hci_get_outgoing_packet_buffer ( void  )
HCI_STATE hci_get_state ( void  )

Get state.

References hci_stack_t::state.

Referenced by sm_run().

static void hci_init_done ( void  )
static
static void hci_initializing_event_handler ( uint8_t *  packet,
uint16_t  size 
)
static

References BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION, BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION, BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS, btstack_run_loop_remove_timer(), hci_stack_t::chipset, hci_stack_t::config, hci_stack_t::custom_bd_addr_set, gap_ssp_supported(), hci_classic_supported(), HCI_EVENT_COMMAND_COMPLETE, HCI_EVENT_COMMAND_STATUS, hci_event_packet_get_type(), HCI_EVENT_VENDOR_SPECIFIC, HCI_INIT_CUSTOM_INIT, HCI_INIT_DONE, hci_init_done(), HCI_INIT_READ_BD_ADDR, HCI_INIT_READ_BUFFER_SIZE, HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS, HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES, HCI_INIT_SEND_BAUD_CHANGE, HCI_INIT_SEND_BAUD_CHANGE_BCM, HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION, HCI_INIT_SEND_RESET, HCI_INIT_SEND_RESET_CSR_WARM_BOOT, HCI_INIT_SEND_RESET_ST_WARM_BOOT, HCI_INIT_SET_BD_ADDR, HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT, HCI_INIT_W4_CUSTOM_INIT, HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT, HCI_INIT_W4_READ_BD_ADDR, HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS, HCI_INIT_W4_SEND_BAUD_CHANGE, HCI_INIT_W4_SEND_BAUD_CHANGE_BCM, HCI_INIT_W4_SEND_READ_LOCAL_NAME, HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION, HCI_INIT_W4_SEND_RESET, HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT, HCI_INIT_W4_SET_BD_ADDR, HCI_INIT_W4_SET_EVENT_MASK, HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, HCI_INIT_W4_WRITE_SCAN_ENABLE, HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, HCI_INIT_WRITE_PAGE_TIMEOUT, hci_initializing_next_state(), hci_le_supported(), hci_reset, hci_stack_t::hci_transport, hci_transport_uart_get_main_baud_rate(), hci_stack_t::last_cmd_opcode, little_endian_read_16(), hci_stack_t::local_supported_commands, log_debug, log_error, log_info, hci_stack_t::manufacturer, hci_cmd_t::opcode, hci_transport_t::set_baudrate, btstack_chipset_t::set_baudrate_command, btstack_chipset_t::set_bd_addr_command, status, hci_stack_t::substate, hci_stack_t::timeout, and UNUSED.

Referenced by event_handler().

static void hci_initializing_next_state ( void  )
static
static void hci_initializing_run ( void  )
static

References bd_addr_to_str(), BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION, BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO, BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, btstack_run_loop_add_timer(), btstack_run_loop_set_timer(), btstack_run_loop_set_timer_handler(), hci_stack_t::chipset, hci_stack_t::class_of_device, hci_stack_t::config, hci_stack_t::connectable, hci_stack_t::custom_bd_addr, DEVICE_NAME_LEN, hci_stack_t::discoverable, hci_stack_t::eir_data, hci_bcm_write_sco_pcm_int, hci_can_send_command_packet_now(), HCI_CMD_HEADER_SIZE, HCI_COMMAND_DATA_PACKET, hci_dump_packet(), hci_host_buffer_size, HCI_INIT_BCM_WRITE_SCO_PCM_INT, HCI_INIT_CUSTOM_INIT, HCI_INIT_READ_BD_ADDR, HCI_INIT_READ_BUFFER_SIZE, HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS, HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES, HCI_INIT_SEND_BAUD_CHANGE, HCI_INIT_SEND_BAUD_CHANGE_BCM, HCI_INIT_SEND_READ_LOCAL_NAME, HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION, HCI_INIT_SEND_RESET, HCI_INIT_SEND_RESET_CSR_WARM_BOOT, HCI_INIT_SEND_RESET_ST_WARM_BOOT, HCI_INIT_SET_BD_ADDR, HCI_INIT_SET_EVENT_MASK, HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT, HCI_INIT_W4_CUSTOM_INIT, HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY, HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT, HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET, HCI_INIT_W4_READ_BD_ADDR, HCI_INIT_W4_READ_BUFFER_SIZE, HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS, HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES, HCI_INIT_W4_SEND_BAUD_CHANGE, HCI_INIT_W4_SEND_BAUD_CHANGE_BCM, HCI_INIT_W4_SEND_READ_LOCAL_NAME, HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION, HCI_INIT_W4_SEND_RESET, HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT, HCI_INIT_W4_SET_BD_ADDR, HCI_INIT_W4_SET_EVENT_MASK, HCI_INIT_W4_WRITE_CLASS_OF_DEVICE, HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, HCI_INIT_W4_WRITE_EIR_DATA, HCI_INIT_W4_WRITE_INQUIRY_MODE, HCI_INIT_W4_WRITE_LOCAL_NAME, HCI_INIT_W4_WRITE_PAGE_TIMEOUT, HCI_INIT_W4_WRITE_SCAN_ENABLE, HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE, HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, HCI_INIT_WRITE_CLASS_OF_DEVICE, HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, HCI_INIT_WRITE_EIR_DATA, HCI_INIT_WRITE_INQUIRY_MODE, HCI_INIT_WRITE_LOCAL_NAME, HCI_INIT_WRITE_PAGE_TIMEOUT, HCI_INIT_WRITE_SCAN_ENABLE, HCI_INIT_WRITE_SIMPLE_PAIRING_MODE, HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, hci_initialization_timeout_handler(), hci_le_read_buffer_size, hci_le_read_maximum_data_length, hci_le_read_white_list_size, hci_le_set_event_mask, hci_le_set_scan_parameters, hci_le_supported(), hci_le_write_suggested_default_data_length, hci_stack_t::hci_packet_buffer, hci_read_bd_addr, hci_read_buffer_size, hci_read_local_name, hci_read_local_supported_commands, hci_read_local_supported_features, hci_read_local_version_information, hci_reserve_packet_buffer(), hci_reset, HCI_RESET_RESEND_TIMEOUT_MS, hci_send_cmd(), hci_send_cmd_packet(), hci_set_controller_to_host_flow_control, hci_set_event_mask, hci_state_reset(), hci_stack_t::hci_transport, hci_transport_uart_get_main_baud_rate(), hci_write_class_of_device, hci_write_default_erroneous_data_reporting, hci_write_extended_inquiry_response, hci_write_inquiry_mode, hci_write_le_host_supported, hci_write_local_name, hci_write_page_timeout, hci_write_scan_enable, hci_write_simple_pairing_mode, hci_write_synchronous_flow_control_enable, hci_stack_t::inquiry_mode, hci_stack_t::last_cmd_opcode, little_endian_read_16(), hci_stack_t::local_name, log_debug, log_info, hci_stack_t::manufacturer, btstack_chipset_t::next_command, hci_cmd_t::opcode, hci_transport_t::send_packet, hci_transport_t::set_baudrate, btstack_chipset_t::set_baudrate_command, btstack_chipset_t::set_bd_addr_command, hci_stack_t::ssp_enable, hci_stack_t::substate, and hci_stack_t::timeout.

Referenced by hci_run().

static int hci_is_le_connection ( hci_connection_t connection)
static
int hci_is_packet_buffer_reserved ( void  )

Check if outgoing packet buffer is reserved.

Used for internal checks in l2cap.c

References hci_stack_t::hci_packet_buffer_reserved.

Referenced by l2cap_send_prepared_connectionless().

static int hci_le_supported ( void  )
static
uint16_t hci_max_acl_data_packet_length ( void  )

Get maximal ACL Classic data packet length based on used buffer size.

Called by L2CAP

References hci_stack_t::acl_data_packet_length.

Referenced by l2cap_run().

int hci_non_flushable_packet_boundary_flag_supported ( void  )

Check if ACL packets marked as non flushable can be sent.

Called by L2CAP

References hci_stack_t::local_supported_features.

Referenced by l2cap_create_signaling_internal().

int hci_number_free_acl_slots_for_handle ( hci_con_handle_t  con_handle)

Get number of free acl slots for packets of given handle.

Called by daemon

References hci_connection_t::address_type, hci_connection_for_handle(), hci_number_free_acl_slots_for_connection_type(), and log_error.

Referenced by hci_can_send_prepared_acl_packet_now().

static void hci_power_transition_to_initializing ( void  )
static
void hci_register_acl_packet_handler ( btstack_packet_handler_t  handler)

Register HCI packet handlers.

Registers a packet handler for ACL data.

References hci_stack_t::acl_packet_handler.

Referenced by l2cap_init().

void hci_release_packet_buffer ( void  )

Release outgoing packet buffer

Note
only called instead of hci_send_preparared

References hci_stack_t::hci_packet_buffer_reserved.

Referenced by event_handler(), hci_send_acl_packet_buffer(), hci_send_acl_packet_fragments(), and l2cap_release_packet_buffer().

int hci_reserve_packet_buffer ( void  )
static void hci_run ( void  )
static

References ACCEPTED_CONNECTION_REQUEST, hci_stack_t::acl_fragmentation_pos, hci_stack_t::acl_fragmentation_total_size, hci_connection_t::address, whitelist_entry_t::address, hci_connection_t::address_type, whitelist_entry_t::address_type, hci_connection_t::authentication_flags, BD_ADDR_TYPE_CLASSIC, BD_ADDR_TYPE_LE_PUBLIC, hci_stack_t::bondable, BONDING_DEDICATED, BONDING_DISCONNECT_DEDICATED_DONE, BONDING_DISCONNECT_SECURITY_BLOCK, BONDING_EMIT_COMPLETE_ON_DISCONNECT, hci_connection_t::bonding_flags, BONDING_REQUEST_REMOTE_FEATURES, BONDING_SEND_AUTHENTICATE_REQUEST, BONDING_SEND_ENCRYPTION_REQUEST, btstack_linked_list_empty(), btstack_linked_list_iterator_has_next(), btstack_linked_list_iterator_init(), btstack_linked_list_iterator_next(), btstack_linked_list_remove(), hci_connection_t::con_handle, CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS, CON_PARAMETER_UPDATE_NONE, hci_stack_t::connectable, hci_stack_t::connections, hci_stack_t::decline_addr, hci_stack_t::decline_reason, DENY_PIN_CODE_REQUEST, ERROR_CODE_PAIRING_NOT_ALLOWED, GAP_INQUIRY_DURATION_MAX, GAP_INQUIRY_DURATION_MIN, GAP_INQUIRY_STATE_ACTIVE, GAP_INQUIRY_STATE_W2_CANCEL, GAP_INQUIRY_STATE_W4_CANCELLED, gap_mitm_protection_required_for_security_level(), hci_stack_t::gap_pairing_addr, hci_stack_t::gap_pairing_passkey, hci_stack_t::gap_pairing_pin, hci_stack_t::gap_pairing_state, GAP_PAIRING_STATE_IDLE, GAP_PAIRING_STATE_SEND_CONFIRMATION, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE, GAP_PAIRING_STATE_SEND_PASSKEY, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE, GAP_PAIRING_STATE_SEND_PIN, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE, GAP_REMOTE_NAME_STATE_W2_SEND, GAP_REMOTE_NAME_STATE_W4_COMPLETE, gap_security_level_for_link_key_type(), btstack_link_key_db_t::get_link_key, HANDLE_LINK_KEY_REQUEST, hci_accept_connection_request, hci_authentication_requested, hci_can_send_comand_packet_transport(), hci_can_send_command_packet_now(), hci_can_send_prepared_acl_packet_now(), hci_classic_supported(), hci_connection_for_handle(), hci_create_connection, hci_disconnect, hci_emit_disconnection_complete(), hci_emit_state(), HCI_FALLING_ASLEEP_COMPLETE, HCI_FALLING_ASLEEP_DISCONNECT, HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE, hci_initializing_run(), hci_inquiry, hci_inquiry_cancel, HCI_INQUIRY_LAP, hci_io_capability_request_negative_reply, hci_io_capability_request_reply, hci_le_add_device_to_white_list, hci_le_connection_update, hci_le_create_connection, hci_le_create_connection_cancel, hci_le_remove_device_from_white_list, hci_le_set_advertise_enable, hci_le_set_advertising_data, hci_le_set_advertising_parameters, hci_le_set_scan_enable, hci_le_set_scan_parameters, hci_le_set_scan_response_data, hci_link_key_request_negative_reply, hci_link_key_request_reply, hci_stack_t::hci_packet_buffer, hci_pin_code_request_negative_reply, hci_pin_code_request_reply, hci_power_control_off(), hci_power_control_sleep(), hci_read_remote_supported_features_command, hci_reject_connection_request, hci_remote_name_request, HCI_ROLE_SLAVE, hci_send_acl_packet_fragments(), hci_send_cmd(), hci_set_connection_encryption, hci_shutdown_connection(), HCI_STATE_FALLING_ASLEEP, HCI_STATE_HALTING, HCI_STATE_INITIALIZING, HCI_STATE_SLEEPING, HCI_STATE_WORKING, hci_usable_acl_packet_types(), hci_user_confirmation_request_negative_reply, hci_user_confirmation_request_reply, hci_user_passkey_request_negative_reply, hci_user_passkey_request_reply, hci_write_scan_enable, if(), hci_stack_t::inquiry_state, LE_ADVERTISEMENT_TASKS_DISABLE, LE_ADVERTISEMENT_TASKS_ENABLE, LE_ADVERTISEMENT_TASKS_SET_ADV_DATA, LE_ADVERTISEMENT_TASKS_SET_PARAMS, LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA, hci_connection_t::le_con_parameter_update_state, hci_connection_t::le_conn_interval_max, hci_connection_t::le_conn_interval_min, hci_connection_t::le_conn_latency, LE_CONNECTING_IDLE, LE_SCAN_IDLE, LE_SCANNING, LE_START_SCAN, LE_STOP_SCAN, hci_connection_t::le_supervision_timeout, LE_WHITELIST_ADD_TO_CONTROLLER, LE_WHITELIST_ON_CONTROLLER, LE_WHITELIST_REMOVE_FROM_CONTROLLER, hci_stack_t::link_key_db, hci_connection_t::link_key_type, log_info, hci_stack_t::new_scan_enable_value, btstack_linked_item::next, NULL, READ_ACL_CONNECTION_HANDLE, RECEIVED_CONNECTION_REQUEST, hci_stack_t::remote_name_addr, hci_stack_t::remote_name_clock_offset, hci_stack_t::remote_name_page_scan_repetition_mode, hci_stack_t::remote_name_state, hci_connection_t::remote_supported_feature_eSCO, hci_connection_t::requested_security_level, hci_connection_t::role, SEND_CANCEL_CONNECTION, SEND_CREATE_CONNECTION, SEND_DISCONNECT, SEND_IO_CAPABILITIES_REPLY, SEND_USER_CONFIRM_REPLY, SEND_USER_PASSKEY_REPLY, SENT_CANCEL_CONNECTION, SENT_CREATE_CONNECTION, SENT_DISCONNECT, hci_stack_t::ssp_authentication_requirement, SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING, hci_stack_t::ssp_io_capability, SSP_IO_CAPABILITY_UNKNOWN, state, hci_connection_t::state, whitelist_entry_t::state, hci_stack_t::state, and hci_stack_t::substate.

Referenced by acl_handler(), event_handler(), gap_disconnect(), hci_disconnect_all(), hci_init_done(), hci_initialization_timeout_handler(), and hci_power_control().

int hci_send_cmd ( const hci_cmd_t cmd,
  ... 
)

pre: numcmds >= 0 - it's allowed to send a command to the controller

Creates and sends HCI command packets based on a template and a list of parameters.

References hci_send_cmd_va_arg().

Referenced by hci_initialization_timeout_handler(), hci_initializing_run(), hci_run(), l2cap_hci_event_handler(), l2cap_run(), sm_aes128_start(), sm_random_start(), and sm_run().

int hci_send_cmd_packet ( uint8_t *  packet,
int  size 
)

Send complete CMD packet.

Called by daemon

References bd_addr_to_str(), BD_ADDR_TYPE_CLASSIC, BTSTACK_MEMORY_ALLOC_FAILED, hci_connection_t::con_handle, create_connection_for_bd_addr_and_type(), btstack_link_key_db_t::delete_link_key, hci_accept_synchronous_connection, HCI_COMMAND_DATA_PACKET, hci_connection_for_bd_addr_and_type(), hci_create_connection, hci_delete_stored_link_key, hci_dump_packet(), hci_le_create_connection, hci_le_create_connection_cancel, hci_le_set_advertise_enable, hci_le_set_random_address, hci_link_key_request_negative_reply, hci_link_key_request_reply, hci_stack_t::hci_packet_buffer, hci_stack_t::hci_packet_buffer_reserved, hci_pin_code_request_negative_reply, hci_pin_code_request_reply, hci_setup_synchronous_connection, hci_stack_t::hci_transport, hci_transport_synchronous(), hci_user_confirmation_request_negative_reply, hci_user_confirmation_request_reply, hci_user_passkey_request_negative_reply, hci_user_passkey_request_reply, hci_write_loopback_mode, IS_COMMAND, LE_CONNECTING_DIRECT, LE_CONNECTING_IDLE, LE_CONNECTING_WHITELIST, LEGACY_PAIRING_ACTIVE, hci_stack_t::link_key_db, little_endian_read_16(), log_error, log_info, hci_stack_t::loopback_mode, hci_stack_t::num_cmd_packets, OPEN, reverse_bd_addr(), hci_stack_t::sco_voice_setting_active, SEND_CREATE_CONNECTION, hci_transport_t::send_packet, SENT_CREATE_CONNECTION, SENT_LINK_KEY_NEGATIVE_REQUEST, SENT_LINK_KEY_REPLY, SSP_PAIRING_ACTIVE, and hci_connection_t::state.

Referenced by hci_initializing_run(), and hci_send_cmd_va_arg().

void hci_set_bd_addr ( bd_addr_t  addr)

Set Public BD ADDR - passed on to Bluetooth chipset during init if supported in bt_control_h.

References hci_stack_t::custom_bd_addr, and hci_stack_t::custom_bd_addr_set.

void hci_set_chipset ( const btstack_chipset_t chipset_driver)

Configure Bluetooth chipset driver.

Has to be called before power on, or right after receiving the local version information

References hci_stack_t::chipset, hci_stack_t::config, and btstack_chipset_t::init.

Referenced by bt_app_init().

void hci_set_control ( const btstack_control_t hardware_control)

Configure Bluetooth hardware control.

Has to be called after hci_init() but before power on.

References hci_stack_t::config, hci_stack_t::control, and btstack_control_t::init.

void hci_set_hardware_error_callback ( void(*)(uint8_t error)  fn)

Set callback for Bluetooth Hardware Error.

References hci_stack_t::hardware_error_callback.

static int hci_transport_can_send_prepared_packet_now ( uint8_t  packet_type)
static
static int hci_transport_synchronous ( void  )
static
static uint32_t hci_transport_uart_get_main_baud_rate ( void  )
static
static int nr_hci_connections ( void  )
static
static void packet_handler ( uint8_t  packet_type,
uint8_t *  packet,
uint16_t  size 
)
static

hci_stack_t* hci_stack = NULL
static
hci_stack_t hci_stack_static
static

Referenced by hci_init().