From d34c10725ba59217821893dd3950c2d1c13e0567 Mon Sep 17 00:00:00 2001 From: Carles Cufi Date: Fri, 5 May 2017 13:33:48 +0200 Subject: [PATCH] Bluetooth: Enable events based on features The event mask population used to let the Controller know which events are relevant to the Host needs to take into account the features supported by the Controller itself, in order to only enable those that are indeed valid. Jira: ZEP-2050 Signed-off-by: Carles Cufi --- subsys/bluetooth/host/hci_core.c | 88 +++++++++++++++++--------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 98b0d20723d..fc91d230b7a 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -3080,13 +3080,55 @@ static int common_init(void) return 0; } +static int le_set_event_mask(void) +{ + struct bt_hci_cp_le_set_event_mask *cp_mask; + struct net_buf *buf; + u64_t mask = 0; + + /* Set LE event mask */ + buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EVENT_MASK, sizeof(*cp_mask)); + if (!buf) { + return -ENOBUFS; + } + + cp_mask = net_buf_add(buf, sizeof(*cp_mask)); + + mask |= BT_EVT_MASK_LE_ADVERTISING_REPORT; + + if (IS_ENABLED(CONFIG_BLUETOOTH_CONN)) { + mask |= BT_EVT_MASK_LE_CONN_COMPLETE; + mask |= BT_EVT_MASK_LE_CONN_UPDATE_COMPLETE; + mask |= BT_EVT_MASK_LE_REMOTE_FEAT_COMPLETE; + if (BT_FEAT_LE_CONN_PARAM_REQ_PROC(bt_dev.le.features)) { + mask |= BT_EVT_MASK_LE_CONN_PARAM_REQ; + } + } + + if (IS_ENABLED(CONFIG_BLUETOOTH_SMP) && + BT_FEAT_LE_ENCR(bt_dev.le.features)) { + mask |= BT_EVT_MASK_LE_LTK_REQUEST; + } + + /* + * If "LE Read Local P-256 Public Key" and "LE Generate DH Key" are + * supported we need to enable events generated by those commands. + */ + if ((bt_dev.supported_commands[34] & 0x02) && + (bt_dev.supported_commands[34] & 0x04)) { + mask |= BT_EVT_MASK_LE_P256_PUBLIC_KEY_COMPLETE; + mask |= BT_EVT_MASK_LE_GENERATE_DHKEY_COMPLETE; + } + + sys_put_le64(mask, cp_mask->events); + return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EVENT_MASK, buf, NULL); +} + static int le_init(void) { struct bt_hci_cp_write_le_host_supp *cp_le; - struct bt_hci_cp_le_set_event_mask *cp_mask; struct net_buf *buf; struct net_buf *rsp; - u64_t mask = 0; int err; /* For now we only support LE capable controllers */ @@ -3145,44 +3187,7 @@ static int le_init(void) net_buf_unref(rsp); } - /* Set LE event mask */ - buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EVENT_MASK, sizeof(*cp_mask)); - if (!buf) { - return -ENOBUFS; - } - - cp_mask = net_buf_add(buf, sizeof(*cp_mask)); - - mask |= BT_EVT_MASK_LE_ADVERTISING_REPORT; - - if (IS_ENABLED(CONFIG_BLUETOOTH_CONN)) { - mask |= BT_EVT_MASK_LE_CONN_COMPLETE; - mask |= BT_EVT_MASK_LE_CONN_UPDATE_COMPLETE; - mask |= BT_EVT_MASK_LE_REMOTE_FEAT_COMPLETE; - mask |= BT_EVT_MASK_LE_CONN_PARAM_REQ; - } - - if (IS_ENABLED(CONFIG_BLUETOOTH_SMP)) { - mask |= BT_EVT_MASK_LE_LTK_REQUEST; - } - - /* - * If "LE Read Local P-256 Public Key" and "LE Generate DH Key" are - * supported we need to enable events generated by those commands. - */ - if ((bt_dev.supported_commands[34] & 0x02) && - (bt_dev.supported_commands[34] & 0x04)) { - mask |= BT_EVT_MASK_LE_P256_PUBLIC_KEY_COMPLETE; - mask |= BT_EVT_MASK_LE_GENERATE_DHKEY_COMPLETE; - } - - sys_put_le64(mask, cp_mask->events); - err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EVENT_MASK, buf, NULL); - if (err) { - return err; - } - - return 0; + return le_set_event_mask(); } #if defined(CONFIG_BLUETOOTH_BREDR) @@ -3408,6 +3413,9 @@ static int set_event_mask(void) ev = net_buf_add(buf, sizeof(*ev)); if (IS_ENABLED(CONFIG_BLUETOOTH_BREDR)) { + /* Since we require LE support, we can count on a + * Bluetooth 4.0 feature set + */ mask |= BT_EVT_MASK_INQUIRY_COMPLETE; mask |= BT_EVT_MASK_CONN_COMPLETE; mask |= BT_EVT_MASK_CONN_REQUEST;