From 693b19d63e45a12c2c12f5db2ca7bc764eb3972b Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Mon, 26 Oct 2015 21:41:09 +0100 Subject: [PATCH] track HCI Connection roles for BR/EDR and LE --- include/btstack/hci_cmds.h | 8 ++++++++ src/hci.c | 13 ++++++++++++- src/hci.h | 9 ++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/btstack/hci_cmds.h b/include/btstack/hci_cmds.h index 6802c3857..10c612a63 100644 --- a/include/btstack/hci_cmds.h +++ b/include/btstack/hci_cmds.h @@ -197,7 +197,15 @@ extern "C" { #define HCI_EVENT_HARDWARE_ERROR 0x10 #define HCI_EVENT_FLUSH_OCCURED 0x11 + +/** + * @format 1B1 + * @param status + * @param bd_addr + * @param role + */ #define HCI_EVENT_ROLE_CHANGE 0x12 + #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13 #define HCI_EVENT_MODE_CHANGE_EVENT 0x14 #define HCI_EVENT_RETURN_LINK_KEYS 0x15 diff --git a/src/hci.c b/src/hci.c index 5b56ce9d9..8f94e85dd 100644 --- a/src/hci.c +++ b/src/hci.c @@ -1313,6 +1313,7 @@ static void event_handler(uint8_t *packet, int size){ BD_ADDR_COPY(hci_stack->decline_addr, addr); break; } + conn->role = HCI_ROLE_SLAVE; conn->state = RECEIVED_CONNECTION_REQUEST; hci_run(); break; @@ -1550,6 +1551,14 @@ static void event_handler(uint8_t *packet, int size){ } break; + case HCI_EVENT_ROLE_CHANGE: + if (packet[2]) break; // status != 0 + handle = READ_BT_16(packet, 3); + conn = hci_connection_for_handle(handle); + if (!conn) break; // no conn + conn->role = packet[9]; + break; + case DAEMON_EVENT_HCI_PACKET_SENT: // release packet buffer only for asynchronous transport and if there are not further fragements if (hci_transport_synchronous()) { @@ -1590,7 +1599,7 @@ static void event_handler(uint8_t *packet, int size){ break; } // on success, both hosts receive connection complete event - if (packet[6] == 0){ + if (packet[6] == HCI_ROLE_MASTER){ // if we're master, it was an outgoing connection and we're done with it hci_stack->le_connecting_state = LE_CONNECTING_IDLE; } else { @@ -1607,6 +1616,7 @@ static void event_handler(uint8_t *packet, int size){ } conn->state = OPEN; + conn->role = packet[6]; conn->con_handle = READ_BT_16(packet, 4); // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock @@ -2286,6 +2296,7 @@ void hci_run(void){ case RECEIVED_CONNECTION_REQUEST: log_info("sending hci_accept_connection_request"); connection->state = ACCEPTED_CONNECTION_REQUEST; + connection->role = HCI_ROLE_SLAVE; if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ hci_send_cmd(&hci_accept_connection_request, connection->address, 1); } else { diff --git a/src/hci.h b/src/hci.h index 7e0f82cdd..4f8e1ea33 100644 --- a/src/hci.h +++ b/src/hci.h @@ -61,7 +61,11 @@ #if defined __cplusplus extern "C" { #endif - + +// HCI roles +#define HCI_ROLE_MASTER 0 +#define HCI_ROLE_SLAVE 1 + // packet header sizes #define HCI_CMD_HEADER_SIZE 3 #define HCI_ACL_HEADER_SIZE 4 @@ -493,6 +497,9 @@ typedef struct { // le public, le random, classic bd_addr_type_t address_type; + // role: 0 - master, 1 - slave + uint8_t role; + // connection state CONNECTION_STATE state;