Bluetooth: Extend advertising API

Extend bt_start_advertising to make it possible to add any data type
instead of just the name.

Change-Id: I3f2afe1eb64aec51f321f7fd7439e97b3d67374c
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This commit is contained in:
Luiz Augusto von Dentz 2015-04-30 16:07:09 +03:00 committed by Anas Nashif
commit b3cdfed003
3 changed files with 54 additions and 18 deletions

View file

@ -77,7 +77,15 @@ int bt_driver_register(struct bt_driver *drv);
/* Unregister a previously registered HCI driver */ /* Unregister a previously registered HCI driver */
void bt_driver_unregister(struct bt_driver *drv); void bt_driver_unregister(struct bt_driver *drv);
/* Advertising testing API */ /* Advertising API */
int bt_start_advertising(uint8_t type, const char *name, uint8_t name_len);
struct bt_eir {
uint8_t len;
uint8_t type;
uint8_t data[29];
} PACK_STRUCT;
int bt_start_advertising(uint8_t type, const struct bt_eir *ad,
const struct bt_eir *sd);
#endif /* __BT_BLUETOOTH_H */ #endif /* __BT_BLUETOOTH_H */

View file

@ -733,16 +733,17 @@ int bt_init(void)
return bt_buf_init(ACL_IN_MAX, acl_out); return bt_buf_init(ACL_IN_MAX, acl_out);
} }
int bt_start_advertising(uint8_t type, const char *name, uint8_t name_len) int bt_start_advertising(uint8_t type, const struct bt_eir *ad,
const struct bt_eir *sd)
{ {
struct bt_buf *buf; struct bt_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_data *scan_rsp; struct bt_hci_cp_le_set_adv_data *scan_rsp;
struct bt_hci_cp_le_set_adv_parameters *set_param; struct bt_hci_cp_le_set_adv_parameters *set_param;
int i;
/* We don't support shortening of names, for now */ if (!ad)
if (name_len > 29) goto send_scan_rsp;
return -EINVAL;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_DATA, sizeof(*set_data)); buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_DATA, sizeof(*set_data));
if (!buf) if (!buf)
@ -752,14 +753,21 @@ int bt_start_advertising(uint8_t type, const char *name, uint8_t name_len)
memset(set_data, 0, sizeof(*set_data)); memset(set_data, 0, sizeof(*set_data));
/* Advertising flags */ for (i = 0; ad[i].len; i++) {
set_data->data[0] = 0x02; /* Length */ /* Check if ad fit in the remaining buffer */
set_data->data[1] = BT_EIR_FLAGS; if (set_data->len + ad[i].len + 1 > 29)
set_data->data[2] = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR; break;
memcpy(&set_data->data[set_data->len], &ad[i], ad[i].len + 1);
set_data->len += ad[i].len + 1;
}
set_data->len = 3;
bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_DATA, buf); bt_hci_cmd_send(BT_HCI_OP_LE_SET_ADV_DATA, buf);
send_scan_rsp:
if (!sd)
goto send_set_param;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_RSP_DATA,
sizeof(*scan_rsp)); sizeof(*scan_rsp));
if (!buf) if (!buf)
@ -769,14 +777,18 @@ int bt_start_advertising(uint8_t type, const char *name, uint8_t name_len)
memset(scan_rsp, 0, sizeof(*scan_rsp)); memset(scan_rsp, 0, sizeof(*scan_rsp));
/* Friendly name */ for (i = 0; sd[i].len; i++) {
scan_rsp->data[0] = name_len + 1; /* Check if ad fit in the remaining buffer */
scan_rsp->data[1] = BT_EIR_NAME_COMPLETE; if (scan_rsp->len + sd[i].len + 1 > 29)
memcpy(scan_rsp->data + 2, name, name_len); break;
memcpy(&scan_rsp->data[scan_rsp->len], &sd[i], sd[i].len + 1);
scan_rsp->len += sd[i].len + 1;
}
scan_rsp->len = name_len + 2;
bt_hci_cmd_send(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, buf); 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)

View file

@ -37,13 +37,29 @@
#include "bluetooth/bluetooth.h" #include "bluetooth/bluetooth.h"
#include "bluetooth/hci.h" #include "bluetooth/hci.h"
const struct bt_eir ad[] = {
{
.len = 2,
.type = BT_EIR_FLAGS,
.data = { BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR },
},
{ }
};
const struct bt_eir sd[] = {
{
.len = 16,
.type = BT_EIR_NAME_COMPLETE,
.data = "Test peripheral",
},
{ }
};
#ifdef CONFIG_MICROKERNEL #ifdef CONFIG_MICROKERNEL
void mainloop(void) void mainloop(void)
#else #else
void main(void) void main(void)
#endif #endif
{ {
const char *name = "Test Peripheral";
int err; int err;
err = bt_init(); err = bt_init();
@ -54,7 +70,7 @@ void main(void)
printk("Bluetooth initialized\n"); printk("Bluetooth initialized\n");
err = bt_start_advertising(BT_LE_ADV_IND, name, strlen(name)); err = bt_start_advertising(BT_LE_ADV_IND, ad, sd);
if (err) { if (err) {
printk("Advertising failed to start (err %d)\n", err); printk("Advertising failed to start (err %d)\n", err);
return; return;