Bluetooth: Refactor AD data HCI commands to helper function
The advertising data and scan response data HCI commands are almost identical so we can save some code size by having a helper function for them. Change-Id: I9f80477ab7837c0a8efa0f9219552ea1426978d4 Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
parent
2d5dc34c94
commit
02740a8083
1 changed files with 33 additions and 46 deletions
|
@ -2342,25 +2342,13 @@ static bool valid_adv_param(const struct bt_le_adv_param *param)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_le_adv_start(const struct bt_le_adv_param *param,
|
static int set_ad(uint16_t hci_op, const struct bt_data *ad, size_t ad_len)
|
||||||
const struct bt_data *ad, size_t ad_len,
|
|
||||||
const struct bt_data *sd, size_t sd_len)
|
|
||||||
{
|
{
|
||||||
struct net_buf *buf;
|
|
||||||
struct bt_hci_cp_le_set_adv_data *set_data;
|
struct bt_hci_cp_le_set_adv_data *set_data;
|
||||||
struct bt_hci_cp_le_set_adv_parameters *set_param;
|
struct net_buf *buf;
|
||||||
uint8_t adv_enable;
|
int i;
|
||||||
int i, err;
|
|
||||||
|
|
||||||
if (!valid_adv_param(param)) {
|
buf = bt_hci_cmd_create(hci_op, sizeof(*set_data));
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (atomic_test_bit(bt_dev.flags, BT_DEV_ADVERTISING)) {
|
|
||||||
return -EALREADY;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_DATA, sizeof(*set_data));
|
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
return -ENOBUFS;
|
return -ENOBUFS;
|
||||||
}
|
}
|
||||||
|
@ -2379,47 +2367,46 @@ int bt_le_adv_start(const struct bt_le_adv_param *param,
|
||||||
set_data->data[set_data->len++] = ad[i].type;
|
set_data->data[set_data->len++] = ad[i].type;
|
||||||
|
|
||||||
memcpy(&set_data->data[set_data->len], ad[i].data,
|
memcpy(&set_data->data[set_data->len], ad[i].data,
|
||||||
ad[i].data_len);
|
ad[i].data_len);
|
||||||
set_data->len += ad[i].data_len;
|
set_data->len += ad[i].data_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_DATA, buf);
|
return bt_hci_cmd_send(hci_op, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bt_le_adv_start(const struct bt_le_adv_param *param,
|
||||||
|
const struct bt_data *ad, size_t ad_len,
|
||||||
|
const struct bt_data *sd, size_t sd_len)
|
||||||
|
{
|
||||||
|
struct net_buf *buf;
|
||||||
|
struct bt_hci_cp_le_set_adv_parameters *set_param;
|
||||||
|
uint8_t adv_enable;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!valid_adv_param(param)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (atomic_test_bit(bt_dev.flags, BT_DEV_ADVERTISING)) {
|
||||||
|
return -EALREADY;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = set_ad(BT_HCI_OP_LE_SET_ADV_DATA, ad, ad_len);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Don't bother with scan response if the advertising type isn't
|
* Don't bother with scan response if the advertising type isn't
|
||||||
* a scannable one.
|
* a scannable one.
|
||||||
*/
|
*/
|
||||||
if (param->type != BT_LE_ADV_IND && param->type != BT_LE_ADV_SCAN_IND) {
|
if (param->type == BT_LE_ADV_IND || param->type == BT_LE_ADV_SCAN_IND) {
|
||||||
goto send_set_param;
|
err = set_ad(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, sd, sd_len);
|
||||||
}
|
if (err) {
|
||||||
|
return err;
|
||||||
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_RSP_DATA,
|
|
||||||
sizeof(*set_data));
|
|
||||||
if (!buf) {
|
|
||||||
return -ENOBUFS;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_data = net_buf_add(buf, sizeof(*set_data));
|
|
||||||
|
|
||||||
memset(set_data, 0, sizeof(*set_data));
|
|
||||||
|
|
||||||
for (i = 0; i < sd_len; i++) {
|
|
||||||
/* Check if ad fit in the remaining buffer */
|
|
||||||
if (set_data->len + sd[i].data_len + 2 > 31) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
set_data->data[set_data->len++] = sd[i].data_len + 1;
|
|
||||||
set_data->data[set_data->len++] = sd[i].type;
|
|
||||||
|
|
||||||
memcpy(&set_data->data[set_data->len], sd[i].data,
|
|
||||||
sd[i].data_len);
|
|
||||||
set_data->len += sd[i].data_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bt_hci_cmd_send(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, buf);
|
|
||||||
|
|
||||||
send_set_param:
|
|
||||||
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAMETERS,
|
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAMETERS,
|
||||||
sizeof(*set_param));
|
sizeof(*set_param));
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue