Bluetooth: Add proper defines for HCI connection role
Subsystems (2)
PATCH 1/3 Bluetooth: Add proper defines for HCI connection role
From: Johan Hedberg <[email protected]>
All HCI commands and events, including LE ones, use 0x00 for master role and 0x01 for slave role. It makes therefore sense to add generic defines for these instead of the current LE_CONN_ROLE_MASTER. Having clean defines will also make it possible to provide simpler internal APIs.
--- include/net/bluetooth/hci.h | 6 +++--- net/bluetooth/hci_event.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
include/net/bluetooth/hci.h +3 -3
@@ -401,6 +401,9 @@ enum {/* The core spec defines 127 as the "not available" value */ #define HCI_TX_POWER_INVALID 127 +#define HCI_ROLE_MASTER 0x00 +#define HCI_ROLE_SLAVE 0x01 +/* Extended Inquiry Response field types */ #define EIR_FLAGS 0x01/* flags */ #define EIR_UUID16_SOME 0x02/* 16-bit UUID, more available */
@@ -1713,9 +1716,6 @@ struct hci_ev_sync_train_complete {#define HCI_EV_SLAVE_PAGE_RESP_TIMEOUT 0x54 -/* Low energy meta events */ -#define LE_CONN_ROLE_MASTER 0x00 -#define HCI_EV_LE_CONN_COMPLETE 0x01struct hci_ev_le_conn_complete {__u8 status;
net/bluetooth/hci_event.c +1 -1
@@ -4116,7 +4116,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) conn->dst_type = ev->bdaddr_type; -if (ev->role ==LE_CONN_ROLE_MASTER ) { +if (ev->role ==HCI_ROLE_MASTER ) { conn->out = true;set_bit (HCI_CONN_MASTER , &conn->flags); }
Signature · 1 line
1.9.3
PATCH 2/3 Bluetooth: Convert HCI_CONN_MASTER flag to a conn->role variable
From: Johan Hedberg <[email protected]>
Having a dedicated u8 role variable in the hci_conn struct greatly simplifies tracking of the role, since this is the native way that it's represented on the HCI level.
--- include/net/bluetooth/hci_core.h | 2 +- net/bluetooth/hci_conn.c | 13 ++++++------- net/bluetooth/hci_event.c | 27 +++++++++------------------ net/bluetooth/l2cap_core.c | 4 ++-- net/bluetooth/smp.c | 12 ++++++------ 5 files changed, 24 insertions(+), 34 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
@@ -371,6 +371,7 @@ struct hci_conn {__u16 state;__u8 mode;__u8 type; +__u8 role;bool out;__u8 attempt;__u8 dev_class[3 ];
@@ -540,7 +541,6 @@ enum {HCI_CONN_POWER_SAVE ,HCI_CONN_REMOTE_OOB ,HCI_CONN_FLUSH_KEY , -HCI_CONN_MASTER ,HCI_CONN_ENCRYPT ,HCI_CONN_AUTH , HCI_CONN_SECURE,
net/bluetooth/hci_conn.c +6 -7
@@ -66,8 +66,7 @@ static void hci_acl_create_connection(struct hci_conn *conn) conn->state =BT_CONNECT ; conn->out = true; - -set_bit (HCI_CONN_MASTER , &conn->flags); + conn->role =HCI_ROLE_MASTER ; conn->attempt++;
@@ -335,7 +334,7 @@ static void hci_conn_timeout(struct work_struct *work)* event handling and hci_clock_offset_evt function. */ if (conn->type ==ACL_LINK && -test_bit (HCI_CONN_MASTER , &conn->flags)) { + conn->role ==HCI_ROLE_MASTER ) {struct hci_dev *hdev = conn->hdev;struct hci_cp_read_clock_offset cp;
@@ -786,8 +785,8 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, goto create_conn; } - conn->out = true; -set_bit (HCI_CONN_MASTER , &conn->flags); + conn->out = true; + conn->role =HCI_ROLE_MASTER ; params =hci_conn_params_lookup (hdev, &conn->dst, conn->dst_type);if (params) {
@@ -1076,7 +1075,7 @@ int hci_conn_switch_role(struct hci_conn *conn, __u8 role) {BT_DBG ("hcon %p" , conn); -if (!role &&test_bit (HCI_CONN_MASTER , &conn->flags)) +if (role == conn->role)return 1 ;if (!test_and_set_bit (HCI_CONN_RSWITCH_PEND , &conn->flags)) {
@@ -1151,7 +1150,7 @@ static u32 get_link_mode(struct hci_conn *conn) {u32 link_mode =0 ; -if (test_bit (HCI_CONN_MASTER , &conn->flags)) +if (conn->role ==HCI_ROLE_MASTER ) link_mode |=HCI_LM_MASTER ;if (test_bit (HCI_CONN_ENCRYPT , &conn->flags))
net/bluetooth/hci_event.c +9 -18
@@ -101,12 +101,8 @@ static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)hci_dev_lock (hdev); conn =hci_conn_hash_lookup_handle (hdev,__le16_to_cpu (rp->handle)); -if (conn) { -if (rp->role) -clear_bit (HCI_CONN_MASTER , &conn->flags); -else -set_bit (HCI_CONN_MASTER , &conn->flags); - } +if (conn) + conn->role = rp->role;hci_dev_unlock (hdev); }
@@ -1420,8 +1416,8 @@ static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)if (!conn) { conn =hci_conn_add (hdev,ACL_LINK , &cp->bdaddr);if (conn) { - conn->out = true; -set_bit (HCI_CONN_MASTER , &conn->flags); + conn->out = true; + conn->role =HCI_ROLE_MASTER ; }else BT_ERR ("No memory for new connection" ); }
@@ -2924,12 +2920,8 @@ static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb) conn =hci_conn_hash_lookup_ba (hdev,ACL_LINK , &ev->bdaddr);if (conn) { -if (!ev->status) { -if (ev->role) -clear_bit (HCI_CONN_MASTER , &conn->flags); -else -set_bit (HCI_CONN_MASTER , &conn->flags); - } +if (!ev->status) + conn->role = ev->role;clear_bit (HCI_CONN_RSWITCH_PEND , &conn->flags);
@@ -4116,10 +4108,9 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) conn->dst_type = ev->bdaddr_type; -if (ev->role ==HCI_ROLE_MASTER ) { + conn->role = ev->role; +if (conn->role ==HCI_ROLE_MASTER ) conn->out = true; -set_bit (HCI_CONN_MASTER , &conn->flags); - }/* If we didn't have a hci_conn object previously * but we're in master role this must be something
@@ -4527,7 +4518,7 @@ static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,return send_conn_param_neg_reply (hdev, handle,HCI_ERROR_INVALID_LL_PARAMS ); -if (test_bit (HCI_CONN_MASTER , &hcon->flags)) { +if (hcon->role ==HCI_ROLE_MASTER ) {struct hci_conn_params *params;u8 store_hint;
net/bluetooth/l2cap_core.c +2 -2
@@ -1487,7 +1487,7 @@ static void l2cap_le_conn_ready(struct l2cap_conn *conn)* been configured for this connection. If not, then trigger * the connection update procedure. */ -if (!test_bit (HCI_CONN_MASTER , &hcon->flags) && +if (hcon->role ==HCI_ROLE_SLAVE && (hcon->le_conn_interval < hcon->le_conn_min_interval || hcon->le_conn_interval > hcon->le_conn_max_interval)) {struct l2cap_conn_param_update_req req;
@@ -5227,7 +5227,7 @@ static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,u16 min, max, latency, to_multiplier;int err; -if (!test_bit (HCI_CONN_MASTER , &hcon->flags)) +if (hcon->role !=HCI_ROLE_MASTER )return -EINVAL ;if (cmd_len !=sizeof (struct l2cap_conn_param_update_req ))
net/bluetooth/smp.c +6 -6
@@ -445,7 +445,7 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,* Confirms and the slave Enters the passkey. */ if (method ==OVERLAP ) { -if (test_bit (HCI_CONN_MASTER , &hcon->flags)) +if (hcon->role ==HCI_ROLE_MASTER ) method =CFM_PASSKEY ;else method =REQ_PASSKEY ;
@@ -686,7 +686,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*req))return SMP_INVALID_PARAMS ; -if (test_bit (HCI_CONN_MASTER , &conn->hcon->flags)) +if (conn->hcon->role !=HCI_ROLE_SLAVE )return SMP_CMD_NOTSUPP ;if (!test_and_set_bit (HCI_CONN_LE_SMP_PEND , &conn->hcon->flags))
@@ -755,7 +755,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*rsp))return SMP_INVALID_PARAMS ; -if (!test_bit (HCI_CONN_MASTER , &conn->hcon->flags)) +if (conn->hcon->role !=HCI_ROLE_MASTER )return SMP_CMD_NOTSUPP ;skb_pull (skb,sizeof (*rsp));
@@ -903,7 +903,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*rp))return SMP_INVALID_PARAMS ; -if (!test_bit (HCI_CONN_MASTER , &conn->hcon->flags)) +if (hcon->role !=HCI_ROLE_MASTER )return SMP_CMD_NOTSUPP ; sec_level =authreq_to_seclevel (rp->auth_req);
@@ -961,7 +961,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)if (sec_level > hcon->pending_sec_level) hcon->pending_sec_level = sec_level; -if (test_bit (HCI_CONN_MASTER , &hcon->flags)) +if (hcon->role ==HCI_ROLE_MASTER )if (smp_ltk_encrypt (conn, hcon->pending_sec_level))return 0 ;
@@ -981,7 +981,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) hcon->pending_sec_level > BT_SECURITY_MEDIUM) authreq |=SMP_AUTH_MITM ; -if (test_bit (HCI_CONN_MASTER , &hcon->flags)) { +if (hcon->role ==HCI_ROLE_MASTER ) {struct smp_cmd_pairing cp;build_pairing_cmd (conn, &cp,NULL , authreq);
Signature · 1 line
1.9.3
PATCH 3/3 Bluetooth: Use explicit role instead of a bool in function parameters
In reply to [email protected]From: Johan Hedberg <[email protected]>
To make the code more understandable it makes sense to use the new HCI defines for connection role instead of a "bool master" parameter. This makes it immediately clear when looking at the function calls what the last parameter is describing.
--- include/net/bluetooth/hci_core.h | 6 +++--- net/bluetooth/hci_conn.c | 7 ++++--- net/bluetooth/hci_core.c | 19 +++++++++++-------- net/bluetooth/hci_event.c | 5 ++--- net/bluetooth/l2cap_core.c | 9 ++++++--- net/bluetooth/mgmt.c | 4 ++-- net/bluetooth/smp.c | 4 ++-- 7 files changed, 30 insertions(+), 24 deletions(-)
include/net/bluetooth/hci_core.h +3 -3
@@ -707,7 +707,7 @@ struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);struct hci_conn *hci_connect_le (struct hci_dev *hdev,bdaddr_t *dst,u8 dst_type,u8 sec_level,u16 conn_timeout, -bool master); +u8 role);struct hci_conn *hci_connect_acl (struct hci_dev *hdev,bdaddr_t *dst,u8 sec_level,u8 auth_type); struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
@@ -881,12 +881,12 @@ struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len, bool *persistent);struct smp_ltk *hci_find_ltk (struct hci_dev *hdev,__le16 ediv,__le64 rand, -bool master); +u8 role);struct smp_ltk *hci_add_ltk (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 addr_type,u8 type,u8 authenticated,u8 tk[16 ],u8 enc_size,__le16 ediv,__le64 rand);struct smp_ltk *hci_find_ltk_by_addr (struct hci_dev *hdev,bdaddr_t *bdaddr, -u8 addr_type,bool master); +u8 addr_type,u8 role);int hci_remove_ltk (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 bdaddr_type);void hci_smp_ltks_clear (struct hci_dev *hdev);int hci_remove_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr);
net/bluetooth/hci_conn.c +4 -3
@@ -697,7 +697,7 @@ static void hci_req_directed_advertising(struct hci_request *req,struct hci_conn *hci_connect_le (struct hci_dev *hdev,bdaddr_t *dst,u8 dst_type,u8 sec_level,u16 conn_timeout, -bool master) +u8 role) {struct hci_conn_params *params;struct hci_conn *conn;
@@ -769,8 +769,10 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, &enable); } + conn->role = role; +/* If requested to connect as slave use directed advertising */ -if (!master) { +if (conn->role ==HCI_ROLE_SLAVE ) {/* If we're active scanning most controllers are unable * to initiate advertising. Simply reject the attempt. */
@@ -786,7 +788,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, } conn->out = true; - conn->role =HCI_ROLE_MASTER ; params =hci_conn_params_lookup (hdev, &conn->dst, conn->dst_type);if (params) {
net/bluetooth/hci_core.c +11 -8
@@ -3121,13 +3121,16 @@ static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,return false; } -static bool ltk_type_master (u8 type) +static u8 ltk_role (u8 type) { -return (type ==SMP_LTK ); +if (type ==SMP_LTK ) +return HCI_ROLE_MASTER ; + +return HCI_ROLE_SLAVE ; }struct smp_ltk *hci_find_ltk (struct hci_dev *hdev,__le16 ediv,__le64 rand, -bool master) +u8 role) {struct smp_ltk *k;
@@ -3135,7 +3138,7 @@ struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand,if (k->ediv != ediv || k->rand != rand)continue ; -if (ltk_type_master (k->type) != master) +if (ltk_role (k->type) != role)continue ;return k;
@@ -3145,14 +3148,14 @@ struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand, }struct smp_ltk *hci_find_ltk_by_addr (struct hci_dev *hdev,bdaddr_t *bdaddr, -u8 addr_type,bool master) +u8 addr_type,u8 role) {struct smp_ltk *k;list_for_each_entry (k, &hdev->long_term_keys, list)if (addr_type == k->bdaddr_type &&bacmp (bdaddr, &k->bdaddr) ==0 && -ltk_type_master (k->type) == master) +ltk_role (k->type) == role)return k;return NULL ;
@@ -3247,9 +3250,9 @@ struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand) { struct smp_ltk *key, *old_key; -bool master =ltk_type_master (type); +u8 role =ltk_role (type); - old_key =hci_find_ltk_by_addr (hdev, bdaddr, addr_type, master); + old_key =hci_find_ltk_by_addr (hdev, bdaddr, addr_type, role);if (old_key) key = old_key;else {
net/bluetooth/hci_event.c +2 -3
@@ -4263,9 +4263,8 @@ static void check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,return ; connect: -/* Request connection in master = true role */ conn =hci_connect_le (hdev, addr, addr_type,BT_SECURITY_LOW , -HCI_LE_AUTOCONN_TIMEOUT , true); +HCI_LE_AUTOCONN_TIMEOUT ,HCI_ROLE_MASTER );if (!IS_ERR (conn))return ;
@@ -4443,7 +4442,7 @@ static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)if (conn ==NULL ) goto not_found; - ltk =hci_find_ltk (hdev, ev->ediv, ev->rand, conn->out); + ltk =hci_find_ltk (hdev, ev->ediv, ev->rand, conn->role);if (ltk ==NULL ) goto not_found;
net/bluetooth/l2cap_core.c +6 -3
@@ -7128,7 +7128,7 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, chan->dcid = cid;if (bdaddr_type_is_le (dst_type)) { -bool master; +u8 role;/* Convert from L2CAP channel address type to HCI address type */
@@ -7137,10 +7137,13 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,else dst_type =ADDR_LE_DEV_RANDOM ; - master = !test_bit (HCI_ADVERTISING , &hdev->dev_flags); +if (test_bit (HCI_ADVERTISING , &hdev->dev_flags)) + role =HCI_ROLE_SLAVE ; +else + role =HCI_ROLE_MASTER ; hcon =hci_connect_le (hdev, dst, dst_type, chan->sec_level, -HCI_LE_CONN_TIMEOUT , master); +HCI_LE_CONN_TIMEOUT , role); }else {u8 auth_type =l2cap_get_auth_type (chan); hcon =hci_connect_acl (hdev, dst, chan->sec_level, auth_type);
net/bluetooth/mgmt.c +2 -2
@@ -3154,9 +3154,9 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,*/ hci_conn_params_add (hdev, &cp->addr.bdaddr, addr_type); -/* Request a connection with master = true role */ conn =hci_connect_le (hdev, &cp->addr.bdaddr, addr_type, - sec_level,HCI_LE_CONN_TIMEOUT , true); + sec_level,HCI_LE_CONN_TIMEOUT , +HCI_ROLE_MASTER ); }if (IS_ERR (conn)) {
net/bluetooth/smp.c +2 -2
@@ -849,7 +849,7 @@ static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)struct hci_conn *hcon = conn->hcon; key =hci_find_ltk_by_addr (hcon->hdev, &hcon->dst, hcon->dst_type, - hcon->out); + hcon->role);if (!key)return false;
@@ -881,7 +881,7 @@ bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level)*/ if (test_bit (HCI_CONN_STK_ENCRYPT , &hcon->flags) &&hci_find_ltk_by_addr (hcon->hdev, &hcon->dst, hcon->dst_type, - hcon->out)) + hcon->role))return false;if (hcon->sec_level >= sec_level)
Signature · 1 line
1.9.3
Marcel Holtmann
maintainerRe: [PATCH 3/3] Bluetooth: Use explicit role instead of a bool in function parameters
Hi Johan,
Quoted 7 lines
To make the code more understandable it makes sense to use the new HCI defines for connection role instead of a "bool master" parameter. This makes it immediately clear when looking at the function calls what the last parameter is describing.
Signed-off-by Johan Hedberg <[email protected]>---
Quoted 8 lines
include/net/bluetooth/hci_core.h | 6 +++--- net/bluetooth/hci_conn.c | 7 ++++--- net/bluetooth/hci_core.c | 19 +++++++++++-------- net/bluetooth/hci_event.c | 5 ++--- net/bluetooth/l2cap_core.c | 9 ++++++--- net/bluetooth/mgmt.c | 4 ++-- net/bluetooth/smp.c | 4 ++-- 7 files changed, 30 insertions(+), 24 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 2/3] Bluetooth: Convert HCI_CONN_MASTER flag to a conn->role variable
In reply to [email protected]Hi Johan,
Quoted 4 lines
Having a dedicated u8 role variable in the
hci_connstruct greatly simplifies tracking of the role, since this is the native way that it's represented on the HCI level.
Quoted 8 lines
Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci_core.h | 2 +- net/bluetooth/hci_conn.c | 13 ++++++------- net/bluetooth/hci_event.c | 27 +++++++++------------------ net/bluetooth/l2cap_core.c | 4 ++-- net/bluetooth/smp.c | 12 ++++++------ 5 files changed, 24 insertions(+), 34 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel
Marcel Holtmann
maintainerHi Johan,
Quoted 10 lines
All HCI commands and events, including LE ones, use
0x00for master role and0x01for slave role. It makes therefore sense to add generic defines for these instead of the currentLE_CONN_ROLE_MASTER. Having clean defines will also make it possible to provide simpler internal APIs.Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci.h| 6 +++---net/bluetooth/hci_event.c| 2 +-2 files changed, 4 insertions(+), 4 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel