diff --git a/drivers/nble/gap.c b/drivers/nble/gap.c index 356ab8c359a..471451412cf 100644 --- a/drivers/nble/gap.c +++ b/drivers/nble/gap.c @@ -24,7 +24,8 @@ int bt_enable(bt_ready_cb_t cb) } int bt_le_adv_start(const struct bt_le_adv_param *param, - const struct bt_eir *ad, const struct bt_eir *sd) + const struct bt_data *ad, size_t ad_len, + const struct bt_data *sd, size_t sd_len) { return -ENOSYS; } diff --git a/include/bluetooth/bluetooth.h b/include/bluetooth/bluetooth.h index 8d16c16a533..457657d1739 100644 --- a/include/bluetooth/bluetooth.h +++ b/include/bluetooth/bluetooth.h @@ -47,11 +47,26 @@ int bt_enable(bt_ready_cb_t cb); /* Advertising API */ -struct bt_eir { - uint8_t len; +/** Description of different data types that can be encoded into + * advertising data. Used to form arrays that are passed to the + * bt_le_adv_start() function. + */ +struct bt_data { uint8_t type; - uint8_t data[29]; -} __packed; + uint8_t data_len; + const uint8_t *data; +}; + +/** Helper to declare inline byte arrays */ +#define BT_BYTES(bytes...) ((uint8_t []) { bytes }) + +/** Helper to declare elements of bt_data arrays */ +#define BT_DATA(_type, _data, _data_len) \ + { \ + .type = _type, \ + .data_len = _data_len, \ + .data = _data, \ + } /** Local advertising address type */ enum { @@ -103,12 +118,15 @@ struct bt_le_adv_param { * * @param param Advertising parameters. * @param ad Data to be used in advertisement packets. + * @param ad_len Number of elements in ad * @param sd Data to be used in scan response packets. + * @param sd_len Number of elements in sd * * @return Zero on success or (negative) error code otherwise. */ int bt_le_adv_start(const struct bt_le_adv_param *param, - const struct bt_eir *ad, const struct bt_eir *sd); + const struct bt_data *ad, size_t ad_len, + const struct bt_data *sd, size_t sd_len); /** @brief Stop advertising * diff --git a/include/bluetooth/hci.h b/include/bluetooth/hci.h index 2a60d58f59d..b7fef15b995 100644 --- a/include/bluetooth/hci.h +++ b/include/bluetooth/hci.h @@ -46,24 +46,25 @@ typedef struct { #define BT_HCI_ERR_PAIRING_NOT_SUPPORTED 0x29 #define BT_HCI_ERR_UNACCEPT_CONN_PARAMS 0x3b -/* EIR/AD definitions */ -#define BT_EIR_FLAGS 0x01 /* AD flags */ -#define BT_EIR_UUID16_SOME 0x02 /* 16-bit UUID, more available */ -#define BT_EIR_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ -#define BT_EIR_UUID32_SOME 0x04 /* 32-bit UUID, more available */ -#define BT_EIR_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ -#define BT_EIR_UUID128_SOME 0x06 /* 128-bit UUID, more available */ -#define BT_EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ -#define BT_EIR_NAME_COMPLETE 0x09 /* Complete name */ -#define BT_EIR_TX_POWER 0x0a /* Tx Power */ -#define BT_EIR_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */ -#define BT_EIR_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */ -#define BT_EIR_SVC_DATA16 0x16 /* Service data, 16-bit UUID */ -#define BT_EIR_GAP_APPEARANCE 0x19 /* GAP appearance */ -#define BT_EIR_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */ -#define BT_EIR_SVC_DATA32 0x20 /* Service data, 32-bit UUID */ -#define BT_EIR_SVC_DATA128 0x21 /* Service data, 128-bit UUID */ -#define BT_EIR_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */ +/* EIR/AD data type definitions */ +#define BT_DATA_FLAGS 0x01 /* AD flags */ +#define BT_DATA_UUID16_SOME 0x02 /* 16-bit UUID, more available */ +#define BT_DATA_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ +#define BT_DATA_UUID32_SOME 0x04 /* 32-bit UUID, more available */ +#define BT_DATA_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ +#define BT_DATA_UUID128_SOME 0x06 /* 128-bit UUID, more available */ +#define BT_DATA_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ +#define BT_DATA_NAME_SHORTENED 0x08 /* Shortened name */ +#define BT_DATA_NAME_COMPLETE 0x09 /* Complete name */ +#define BT_DATA_TX_POWER 0x0a /* Tx Power */ +#define BT_DATA_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */ +#define BT_DATA_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */ +#define BT_DATA_SVC_DATA16 0x16 /* Service data, 16-bit UUID */ +#define BT_DATA_GAP_APPEARANCE 0x19 /* GAP appearance */ +#define BT_DATA_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */ +#define BT_DATA_SVC_DATA32 0x20 /* Service data, 32-bit UUID */ +#define BT_DATA_SVC_DATA128 0x21 /* Service data, 128-bit UUID */ +#define BT_DATA_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */ #define BT_LE_AD_LIMITED 0x01 /* Limited Discoverable */ #define BT_LE_AD_GENERAL 0x02 /* General Discoverable */ diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 2036df7aacb..05ecc2195cb 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -2336,11 +2336,11 @@ static bool valid_adv_param(const struct bt_le_adv_param *param) } int bt_le_adv_start(const struct bt_le_adv_param *param, - const struct bt_eir *ad, const struct bt_eir *sd) + 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 *scan_rsp; struct bt_hci_cp_le_set_adv_parameters *set_param; uint8_t adv_enable; int i, err; @@ -2353,10 +2353,6 @@ int bt_le_adv_start(const struct bt_le_adv_param *param, return -EALREADY; } - if (!ad) { - goto send_scan_rsp; - } - buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_DATA, sizeof(*set_data)); if (!buf) { return -ENOBUFS; @@ -2366,41 +2362,52 @@ int bt_le_adv_start(const struct bt_le_adv_param *param, memset(set_data, 0, sizeof(*set_data)); - for (i = 0; ad[i].len; i++) { + for (i = 0; i < ad_len; i++) { /* Check if ad fit in the remaining buffer */ - if (set_data->len + ad[i].len + 1 > 29) { + if (set_data->len + ad[i].data_len + 2 > 31) { break; } - memcpy(&set_data->data[set_data->len], &ad[i], ad[i].len + 1); - set_data->len += ad[i].len + 1; + set_data->data[set_data->len++] = ad[i].data_len + 1; + set_data->data[set_data->len++] = ad[i].type; + + memcpy(&set_data->data[set_data->len], ad[i].data, + ad[i].data_len); + set_data->len += ad[i].data_len; } bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_DATA, buf); -send_scan_rsp: - if (!sd) { + /* + * Don't bother with scan response if the advertising type isn't + * a scannable one. + */ + if (param->type != BT_LE_ADV_IND && param->type != BT_LE_ADV_SCAN_IND) { goto send_set_param; } buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, - sizeof(*scan_rsp)); + sizeof(*set_data)); if (!buf) { return -ENOBUFS; } - scan_rsp = net_buf_add(buf, sizeof(*scan_rsp)); + set_data = net_buf_add(buf, sizeof(*set_data)); - memset(scan_rsp, 0, sizeof(*scan_rsp)); + memset(set_data, 0, sizeof(*set_data)); - for (i = 0; sd[i].len; i++) { + for (i = 0; i < sd_len; i++) { /* Check if ad fit in the remaining buffer */ - if (scan_rsp->len + sd[i].len + 1 > 29) { + if (set_data->len + sd[i].data_len + 2 > 31) { break; } - memcpy(&scan_rsp->data[scan_rsp->len], &sd[i], sd[i].len + 1); - scan_rsp->len += sd[i].len + 1; + 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); diff --git a/samples/bluetooth/beacon/src/main.c b/samples/bluetooth/beacon/src/main.c index 1c8ad1e72ea..4c17dec7108 100644 --- a/samples/bluetooth/beacon/src/main.c +++ b/samples/bluetooth/beacon/src/main.c @@ -19,34 +19,21 @@ #include #include #include +#include #include #include /* Set Advertisement data */ -static const struct bt_eir ad[] = { - { - .len = 3, - .type = BT_EIR_UUID16_ALL, - .data = { 0xd8, 0xfe }, - }, - { - .len = 9, - .type = BT_EIR_SVC_DATA16, - .data = { 0xd8, 0xfe, 0x00, 0x20, 0x02, '0', '1', - 0x08 }, - }, - { } +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_UUID16_ALL, BT_BYTES(0xd8, 0xfe), 2), + BT_DATA(BT_DATA_SVC_DATA16, + BT_BYTES(0xd8, 0xfe, 0x00, 0x20, 0x02, '0', '1', 0x08), 8) }; /* Set Scan Response data */ -static const struct bt_eir sd[] = { - { - .len = 12, - .type = BT_EIR_NAME_COMPLETE, - .data = "Test beacon", - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, "Test beacon", 11), }; #ifdef CONFIG_MICROKERNEL @@ -71,7 +58,7 @@ void main(void) BT_LE_ADV_ADDR_NRPA, BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2), - ad, sd); + ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; diff --git a/samples/bluetooth/central_hr/src/main.c b/samples/bluetooth/central_hr/src/main.c index fc077fef043..903ab7724ce 100644 --- a/samples/bluetooth/central_hr/src/main.c +++ b/samples/bluetooth/central_hr/src/main.c @@ -119,26 +119,27 @@ static void connected(struct bt_conn *conn) } } -static bool eir_found(const struct bt_eir *eir, void *user_data) +static bool eir_found(uint8_t type, const uint8_t *data, uint8_t data_len, + void *user_data) { bt_addr_le_t *addr = user_data; int i; - printk("[AD]: %u len %u\n", eir->type, eir->len); + printk("[AD]: %u data_len %u\n", type, data_len); - switch (eir->type) { - case BT_EIR_UUID16_SOME: - case BT_EIR_UUID16_ALL: - if ((eir->len - sizeof(eir->type)) % sizeof(uint16_t) != 0) { + switch (type) { + case BT_DATA_UUID16_SOME: + case BT_DATA_UUID16_ALL: + if (data_len % sizeof(uint16_t) != 0) { printk("AD malformed\n"); return true; } - for (i = 0; i < eir->len; i += sizeof(uint16_t)) { + for (i = 0; i < data_len; i += sizeof(uint16_t)) { uint16_t u16; int err; - memcpy(&u16, &eir->data[i], sizeof(u16)); + memcpy(&u16, &data[i], sizeof(u16)); if (sys_le16_to_cpu(u16) != BT_UUID_HRS->u16) { continue; } @@ -158,32 +159,30 @@ static bool eir_found(const struct bt_eir *eir, void *user_data) return true; } -static void ad_parse(const uint8_t *data, uint8_t len, - bool (*func)(const struct bt_eir *eir, void *user_data), +static void ad_parse(const uint8_t *data, uint8_t data_len, + bool (*func)(uint8_t type, const uint8_t *data, + uint8_t data_len, void *user_data), void *user_data) { - const void *p; - - for (p = data; len > 0;) { - const struct bt_eir *eir = p; + while (data_len > 1) { + uint8_t len = data[0]; /* Check for early termination */ - if (eir->len == 0) { + if (len == 0) { return; } - if ((eir->len + sizeof(eir->len) > len) || - (len < sizeof(eir->len) + sizeof(eir->type))) { + if ((len + 1 > data_len) || (data_len < 2)) { printk("AD malformed\n"); return; } - if (!func(eir, user_data)) { + if (!func(data[1], &data[2], len - 1, user_data)) { return; } - p += sizeof(eir->len) + eir->len; - len -= sizeof(eir->len) + eir->len; + data_len -= len + 1; + data += len + 1; } } diff --git a/samples/bluetooth/ipsp/src/main.c b/samples/bluetooth/ipsp/src/main.c index 6b20d3166ca..49de7423169 100644 --- a/samples/bluetooth/ipsp/src/main.c +++ b/samples/bluetooth/ipsp/src/main.c @@ -35,6 +35,7 @@ #include #define DEVICE_NAME "Test IPSP node" +#define DEVICE_NAME_LEN 14 #define UNKNOWN_APPEARANCE 0x0000 /* The 2001:db8::/32 is the private address space for documentation RFC 3849 */ @@ -103,27 +104,14 @@ static struct bt_gatt_attr attrs[] = { read_manuf, NULL, "Manufacturer"), }; -static const struct bt_eir ad[] = { - { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, - }, - { - .len = 3, - .type = BT_EIR_UUID16_ALL, - .data = { 0x20, 0x18 }, - }, - { } +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_FLAGS, + BT_BYTES(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR), 1), + BT_DATA(BT_DATA_UUID16_ALL, BT_BYTES(0x20, 0x18), 3), }; -static const struct bt_eir sd[] = { - { - .len = 15, - .type = BT_EIR_NAME_COMPLETE, - .data = DEVICE_NAME, - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn) @@ -270,7 +258,8 @@ void main(void) net_init(); - err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, sd); + err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), + sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; diff --git a/samples/bluetooth/nble/src/main.c b/samples/bluetooth/nble/src/main.c index 8d053c202c6..f96b9826196 100644 --- a/samples/bluetooth/nble/src/main.c +++ b/samples/bluetooth/nble/src/main.c @@ -33,6 +33,7 @@ #define CONFIG_BLUETOOTH_MAX_PAIRED 1 #define DEVICE_NAME "Test peripheral" +#define DEVICE_NAME_LEN 15 #define HEART_RATE_APPEARANCE 0x0341 static int read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, @@ -381,33 +382,18 @@ static struct bt_gatt_attr attrs[] = { read_signed, write_signed, &signed_value), }; -static const struct bt_eir ad[] = { - { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, - }, - { - .len = 7, - .type = BT_EIR_UUID16_ALL, - .data = { 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18 }, - }, - { - .len = 17, - .type = BT_EIR_UUID128_ALL, - .data = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, - }, - { } +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_FLAGS, + BT_BYTES(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR), 1), + BT_DATA(BT_DATA_UUID16_ALL, + BT_BYTES(0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18), 6), + BT_DATA(BT_DATA_UUID128_ALL, + BT_BYTES(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12), 16), }; -static const struct bt_eir sd[] = { - { - .len = 16, - .type = BT_EIR_NAME_COMPLETE, - .data = DEVICE_NAME, - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn) @@ -434,7 +420,8 @@ static void bt_ready(int err) printk("Bluetooth initialized\n"); - err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, sd); + err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), + sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; diff --git a/samples/bluetooth/peripheral/src/main.c b/samples/bluetooth/peripheral/src/main.c index b21af2ed526..5160f2081ea 100644 --- a/samples/bluetooth/peripheral/src/main.c +++ b/samples/bluetooth/peripheral/src/main.c @@ -31,6 +31,7 @@ #include #define DEVICE_NAME "Test peripheral" +#define DEVICE_NAME_LEN 15 #define HEART_RATE_APPEARANCE 0x0341 static int read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, @@ -379,33 +380,18 @@ static struct bt_gatt_attr attrs[] = { read_signed, write_signed, &signed_value), }; -static const struct bt_eir ad[] = { - { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, - }, - { - .len = 7, - .type = BT_EIR_UUID16_ALL, - .data = { 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18 }, - }, - { - .len = 17, - .type = BT_EIR_UUID128_ALL, - .data = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, - 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, - }, - { } +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_FLAGS, + BT_BYTES(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR), 1), + BT_DATA(BT_DATA_UUID16_ALL, + BT_BYTES(0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18), 6), + BT_DATA(BT_DATA_UUID128_ALL, + BT_BYTES(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12), 16), }; -static const struct bt_eir sd[] = { - { - .len = 16, - .type = BT_EIR_NAME_COMPLETE, - .data = DEVICE_NAME, - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn) @@ -432,7 +418,8 @@ static void bt_ready(int err) printk("Bluetooth initialized\n"); - err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, sd); + err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), + sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; diff --git a/samples/bluetooth/peripheral_dis/src/main.c b/samples/bluetooth/peripheral_dis/src/main.c index 13cdf1ac5b8..49d7e1182c3 100644 --- a/samples/bluetooth/peripheral_dis/src/main.c +++ b/samples/bluetooth/peripheral_dis/src/main.c @@ -31,6 +31,7 @@ #include #define DEVICE_NAME "DIS peripheral" +#define DEVICE_NAME_LEN 14 #define APPEARANCE 0x0000 static int read_appearance(struct bt_conn *conn, @@ -71,27 +72,14 @@ static struct bt_gatt_attr attrs[] = { read_string, NULL, "Manufacturer"), }; -static const struct bt_eir ad[] = { - { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, - }, - { - .len = 3, - .type = BT_EIR_UUID16_ALL, - .data = { 0x0a, 0x18 }, - }, - { } +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_FLAGS, + BT_BYTES(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR), 1), + BT_DATA(BT_DATA_UUID16_ALL, BT_BYTES(0x0a, 0x18), 2), }; -static const struct bt_eir sd[] = { - { - .len = 15, - .type = BT_EIR_NAME_COMPLETE, - .data = DEVICE_NAME, - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn) @@ -129,7 +117,8 @@ void main(void) bt_conn_cb_register(&conn_callbacks); - err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, sd); + err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), + sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; diff --git a/samples/bluetooth/peripheral_sc_only/src/main.c b/samples/bluetooth/peripheral_sc_only/src/main.c index 8c2e6eb4fd7..70bb1600bf3 100644 --- a/samples/bluetooth/peripheral_sc_only/src/main.c +++ b/samples/bluetooth/peripheral_sc_only/src/main.c @@ -31,6 +31,7 @@ #include #define DEVICE_NAME "SC only peripheral" +#define DEVICE_NAME_LEN 18 #define APPEARANCE 0x0000 static int read_appearance(struct bt_conn *conn, @@ -62,22 +63,13 @@ static struct bt_gatt_attr attrs[] = { read_appearance, NULL, NULL), }; -static const struct bt_eir ad[] = { - { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, - }, - { } +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_FLAGS, + BT_BYTES(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR), 1) }; -static const struct bt_eir sd[] = { - { - .len = 18, - .type = BT_EIR_NAME_COMPLETE, - .data = DEVICE_NAME, - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn) @@ -175,7 +167,8 @@ void main(void) bt_auth_cb_register(&auth_cb_display); bt_conn_cb_register(&conn_callbacks); - err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, sd); + err = bt_le_adv_start(BT_LE_ADV(BT_LE_ADV_IND), ad, ARRAY_SIZE(ad), + sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; diff --git a/samples/bluetooth/shell/src/main.c b/samples/bluetooth/shell/src/main.c index 32053c6508c..b69fea397d0 100644 --- a/samples/bluetooth/shell/src/main.c +++ b/samples/bluetooth/shell/src/main.c @@ -37,7 +37,8 @@ #include -#define DEVICE_NAME "test shell" +#define DEVICE_NAME "test shell" +#define DEVICE_NAME_LEN 10 #define AD_SHORT_NAME 0x08 #define AD_COMPLETE_NAME 0x09 #define CREDITS 10 @@ -489,30 +490,20 @@ static void cmd_gatt_exchange_mtu(int argc, char *argv[]) } } -static const struct bt_eir ad_discov[] = { - { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR }, - }, - { } +static const struct bt_data ad_discov[] = { + BT_DATA(BT_DATA_FLAGS, + BT_BYTES(BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR), 1), }; -static const struct bt_eir ad_non_discov[] = { { 0 } }; - -static const struct bt_eir sd[] = { - { - .len = sizeof(DEVICE_NAME), - .type = BT_EIR_NAME_COMPLETE, - .data = DEVICE_NAME, - }, - { } +static const struct bt_data sd[] = { + BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void cmd_advertise(int argc, char *argv[]) { struct bt_le_adv_param param; - const struct bt_eir *ad, *scan_rsp; + const struct bt_data *ad, *scan_rsp; + size_t ad_len, scan_rsp_len; if (argc < 2) { goto fail; @@ -535,14 +526,17 @@ static void cmd_advertise(int argc, char *argv[]) param.type = BT_LE_ADV_IND; param.addr_type = BT_LE_ADV_ADDR_PUBLIC; scan_rsp = sd; + scan_rsp_len = ARRAY_SIZE(sd); } else if (!strcmp(argv[1], "scan")) { param.type = BT_LE_ADV_SCAN_IND; param.addr_type = BT_LE_ADV_ADDR_PUBLIC; scan_rsp = sd; + scan_rsp_len = ARRAY_SIZE(sd); } else if (!strcmp(argv[1], "nconn")) { param.type = BT_LE_ADV_NONCONN_IND; param.addr_type = BT_LE_ADV_ADDR_NRPA; scan_rsp = NULL; + scan_rsp_len = 0; } else { goto fail; } @@ -553,16 +547,19 @@ static void cmd_advertise(int argc, char *argv[]) if (!strcmp(mode, "discov")) { ad = ad_discov; + ad_len = ARRAY_SIZE(ad_discov); } else if (!strcmp(mode, "non_discov")) { - ad = ad_non_discov; + ad = NULL; + ad_len = 0; } else { goto fail; } } else { ad = ad_discov; + ad_len = ARRAY_SIZE(ad_discov); } - if (bt_le_adv_start(¶m, ad, scan_rsp) < 0) { + if (bt_le_adv_start(¶m, ad, ad_len, scan_rsp, scan_rsp_len) < 0) { printk("Failed to start advertising\n"); } else { printk("Advertising started\n"); diff --git a/samples/bluetooth/tester/src/gap.c b/samples/bluetooth/tester/src/gap.c index 6e09ff1e3e5..a98fd37344e 100644 --- a/samples/bluetooth/tester/src/gap.c +++ b/samples/bluetooth/tester/src/gap.c @@ -143,31 +143,27 @@ static void set_connectable(uint8_t *data, uint16_t len) (uint8_t *) &rp, sizeof(rp)); } -static struct bt_eir ad_flags = { - .len = 2, - .type = BT_EIR_FLAGS, - .data = { BT_LE_AD_NO_BREDR }, -}; +static uint8_t ad_flags = BT_LE_AD_NO_BREDR; +static struct bt_data ad = BT_DATA(BT_DATA_FLAGS, &ad_flags, sizeof(ad_flags)); static void set_discoverable(uint8_t *data, uint16_t len) { const struct gap_set_discoverable_cmd *cmd = (void *) data; struct gap_set_discoverable_rp rp; - uint8_t *flags = &ad_flags.data[0]; switch (cmd->discoverable) { case GAP_NON_DISCOVERABLE: - *flags &= ~(BT_LE_AD_GENERAL | BT_LE_AD_LIMITED); + ad_flags &= ~(BT_LE_AD_GENERAL | BT_LE_AD_LIMITED); atomic_clear_bit(¤t_settings, GAP_SETTINGS_DISCOVERABLE); break; case GAP_GENERAL_DISCOVERABLE: - *flags &= ~BT_LE_AD_LIMITED; - *flags |= BT_LE_AD_GENERAL; + ad_flags &= ~BT_LE_AD_LIMITED; + ad_flags |= BT_LE_AD_GENERAL; atomic_set_bit(¤t_settings, GAP_SETTINGS_DISCOVERABLE); break; case GAP_LIMITED_DISCOVERABLE: - *flags &= ~BT_LE_AD_GENERAL; - *flags |= BT_LE_AD_LIMITED; + ad_flags &= ~BT_LE_AD_GENERAL; + ad_flags |= BT_LE_AD_LIMITED; atomic_set_bit(¤t_settings, GAP_SETTINGS_DISCOVERABLE); break; default: @@ -187,7 +183,6 @@ static void start_advertising(const uint8_t *data, uint16_t len) const struct gap_start_advertising_cmd *cmd = (void *) data; struct gap_start_advertising_rp rp; uint8_t adv_type; - struct bt_eir ad_data[] = { ad_flags }; /* TODO * convert adv_data and scan_rsp and pass them @@ -200,7 +195,7 @@ static void start_advertising(const uint8_t *data, uint16_t len) adv_type = BT_LE_ADV_NONCONN_IND; } - if (bt_le_adv_start(BT_LE_ADV(adv_type), ad_data, NULL) < 0) { + if (bt_le_adv_start(BT_LE_ADV(adv_type), &ad, 1, NULL, 0) < 0) { tester_rsp(BTP_SERVICE_ID_GAP, GAP_START_ADVERTISING, CONTROLLER_INDEX, BTP_STATUS_FAILED); return;