hci: set role only on create and role changed

This commit is contained in:
Matthias Ringwald 2023-05-10 13:55:01 +02:00
parent 6d4050fb8a
commit 88f925b138
3 changed files with 16 additions and 18 deletions

View File

@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Fixed ## Fixed
- HCI: fix set extended scan response - HCI: fix set extended scan response
- HCI: fix report of extended advertisements with data len > 31 - HCI: fix report of extended advertisements with data len > 31
- HCI: fix CTKD in Initiator role over BR/EDR SC when using dedicated bonding
- SM: fix value in SM_EVENT_NUMERIC_COMPARISON_REQUEST - SM: fix value in SM_EVENT_NUMERIC_COMPARISON_REQUEST
- btstack_stdin_embedded: use timer to poll RTT input, fix for tickless RTOS - btstack_stdin_embedded: use timer to poll RTT input, fix for tickless RTOS
- gatt_client: return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER for invalid connection handle - gatt_client: return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER for invalid connection handle

View File

@ -311,7 +311,8 @@ static void hci_connection_init(hci_connection_t * conn){
* *
* @return connection OR NULL, if no memory left * @return connection OR NULL, if no memory left
*/ */
static hci_connection_t * create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ static hci_connection_t *
create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type, hci_role_t role) {
log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
hci_connection_t * conn = btstack_memory_hci_connection_get(); hci_connection_t * conn = btstack_memory_hci_connection_get();
@ -321,7 +322,7 @@ static hci_connection_t * create_connection_for_bd_addr_and_type(const bd_addr_t
bd_addr_copy(conn->address, addr); bd_addr_copy(conn->address, addr);
conn->address_type = addr_type; conn->address_type = addr_type;
conn->con_handle = HCI_CON_HANDLE_INVALID; conn->con_handle = HCI_CON_HANDLE_INVALID;
conn->role = HCI_ROLE_INVALID; conn->role = role;
#ifdef ENABLE_LE_PERIODIC_ADVERTISING #ifdef ENABLE_LE_PERIODIC_ADVERTISING
conn->le_past_sync_handle = HCI_CON_HANDLE_INVALID; conn->le_past_sync_handle = HCI_CON_HANDLE_INVALID;
#endif #endif
@ -3130,10 +3131,10 @@ static void event_handle_le_connection_complete(const uint8_t * packet){
// read fields // read fields
uint8_t status = packet[3]; uint8_t status = packet[3];
uint8_t role = packet[6]; hci_role_t role = (hci_role_t) packet[6];
uint16_t conn_interval;
// support different connection complete events // support different connection complete events
uint16_t conn_interval;
switch (packet[2]){ switch (packet[2]){
case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet);
@ -3202,7 +3203,7 @@ static void event_handle_le_connection_complete(const uint8_t * packet){
// LE connections are auto-accepted, so just create a connection if there isn't one already // LE connections are auto-accepted, so just create a connection if there isn't one already
if (!conn){ if (!conn){
conn = create_connection_for_bd_addr_and_type(addr, addr_type); conn = create_connection_for_bd_addr_and_type(addr, addr_type, role);
} }
// no memory, sorry. // no memory, sorry.
@ -3211,7 +3212,6 @@ static void event_handle_le_connection_complete(const uint8_t * packet){
} }
conn->state = OPEN; conn->state = OPEN;
conn->role = role;
conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
conn->le_connection_interval = conn_interval; conn->le_connection_interval = conn_interval;
@ -3500,7 +3500,7 @@ static void event_handler(uint8_t *packet, uint16_t size){
addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO;
conn = hci_connection_for_bd_addr_and_type(addr, addr_type); conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
if (!conn) { if (!conn) {
conn = create_connection_for_bd_addr_and_type(addr, addr_type); conn = create_connection_for_bd_addr_and_type(addr, addr_type, HCI_ROLE_SLAVE);
} }
if (!conn) { if (!conn) {
// CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
@ -3510,7 +3510,6 @@ static void event_handler(uint8_t *packet, uint16_t size){
// avoid event to higher layer // avoid event to higher layer
return; return;
} }
conn->role = HCI_ROLE_SLAVE;
conn->state = RECEIVED_CONNECTION_REQUEST; conn->state = RECEIVED_CONNECTION_REQUEST;
// store info about eSCO // store info about eSCO
if (link_type == HCI_LINK_TYPE_ESCO){ if (link_type == HCI_LINK_TYPE_ESCO){
@ -4002,7 +4001,7 @@ static void event_handler(uint8_t *packet, uint16_t size){
addr_type = BD_ADDR_TYPE_ACL; addr_type = BD_ADDR_TYPE_ACL;
conn = hci_connection_for_bd_addr_and_type(addr, addr_type); conn = hci_connection_for_bd_addr_and_type(addr, addr_type);
if (!conn) break; if (!conn) break;
conn->role = packet[9]; conn->role = (hci_role_t) packet[9];
break; break;
#endif #endif
@ -6834,7 +6833,6 @@ static bool hci_run_general_pending_commands(void){
#ifdef ENABLE_CLASSIC #ifdef ENABLE_CLASSIC
case RECEIVED_CONNECTION_REQUEST: case RECEIVED_CONNECTION_REQUEST:
connection->role = HCI_ROLE_SLAVE;
if (connection->address_type == BD_ADDR_TYPE_ACL){ if (connection->address_type == BD_ADDR_TYPE_ACL){
log_info("sending hci_accept_connection_request"); log_info("sending hci_accept_connection_request");
connection->state = ACCEPTED_CONNECTION_REQUEST; connection->state = ACCEPTED_CONNECTION_REQUEST;
@ -7259,14 +7257,13 @@ uint8_t hci_send_cmd_packet(uint8_t *packet, int size){
conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL);
if (!conn) { if (!conn) {
conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL, HCI_ROLE_MASTER);
if (!conn) { if (!conn) {
// notify client that alloc failed // notify client that alloc failed
hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);
return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller
} }
conn->state = SEND_CREATE_CONNECTION; conn->state = SEND_CREATE_CONNECTION;
conn->role = HCI_ROLE_MASTER;
} }
log_info("conn state %u", conn->state); log_info("conn state %u", conn->state);
@ -7315,11 +7312,11 @@ uint8_t hci_send_cmd_packet(uint8_t *packet, int size){
return ERROR_CODE_COMMAND_DISALLOWED; return ERROR_CODE_COMMAND_DISALLOWED;
} }
// allocate connection struct // allocate connection struct
conn = create_connection_for_bd_addr_and_type(conn->address, BD_ADDR_TYPE_SCO); conn = create_connection_for_bd_addr_and_type(conn->address, BD_ADDR_TYPE_SCO,
HCI_ROLE_MASTER);
if (!conn) { if (!conn) {
return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
} }
conn->role = HCI_ROLE_MASTER;
break; break;
case BD_ADDR_TYPE_SCO: case BD_ADDR_TYPE_SCO:
// update of existing SCO connection // update of existing SCO connection
@ -7952,7 +7949,7 @@ void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_
int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
// create connection state machine // create connection state machine
hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL, HCI_ROLE_MASTER);
if (!connection){ if (!connection){
return BTSTACK_MEMORY_ALLOC_FAILED; return BTSTACK_MEMORY_ALLOC_FAILED;
@ -8035,7 +8032,7 @@ uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){
} }
log_info("gap_connect: no connection exists yet, creating context"); log_info("gap_connect: no connection exists yet, creating context");
conn = create_connection_for_bd_addr_and_type(addr, addr_type); conn = create_connection_for_bd_addr_and_type(addr, addr_type, HCI_ROLE_MASTER);
if (!conn){ if (!conn){
// notify client that alloc failed // notify client that alloc failed
hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED);

View File

@ -533,7 +533,7 @@ typedef struct {
bd_addr_type_t address_type; bd_addr_type_t address_type;
// role: 0 - master, 1 - slave // role: 0 - master, 1 - slave
uint8_t role; hci_role_t role;
// connection state // connection state
CONNECTION_STATE state; CONNECTION_STATE state;