Bluetooth: Convert sample code to use the new bt_data_parse() API
Convert the shell and central_hr sample apps to use the newly introduced bt_data_parse() API. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
parent
8818244959
commit
d41d9bd112
|
@ -124,28 +124,27 @@ static void connected(struct bt_conn *conn, u8_t conn_err)
|
|||
}
|
||||
}
|
||||
|
||||
static bool eir_found(u8_t type, const u8_t *data, u8_t data_len,
|
||||
void *user_data)
|
||||
static bool eir_found(struct bt_data *data, void *user_data)
|
||||
{
|
||||
bt_addr_le_t *addr = user_data;
|
||||
int i;
|
||||
|
||||
printk("[AD]: %u data_len %u\n", type, data_len);
|
||||
printk("[AD]: %u data_len %u\n", data->type, data->data_len);
|
||||
|
||||
switch (type) {
|
||||
switch (data->type) {
|
||||
case BT_DATA_UUID16_SOME:
|
||||
case BT_DATA_UUID16_ALL:
|
||||
if (data_len % sizeof(u16_t) != 0) {
|
||||
if (data->data_len % sizeof(u16_t) != 0) {
|
||||
printk("AD malformed\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
for (i = 0; i < data_len; i += sizeof(u16_t)) {
|
||||
for (i = 0; i < data->data_len; i += sizeof(u16_t)) {
|
||||
struct bt_uuid *uuid;
|
||||
u16_t u16;
|
||||
int err;
|
||||
|
||||
memcpy(&u16, &data[i], sizeof(u16));
|
||||
memcpy(&u16, &data->data[i], sizeof(u16));
|
||||
uuid = BT_UUID_DECLARE_16(sys_le16_to_cpu(u16));
|
||||
if (bt_uuid_cmp(uuid, BT_UUID_HRS)) {
|
||||
continue;
|
||||
|
@ -166,35 +165,6 @@ static bool eir_found(u8_t type, const u8_t *data, u8_t data_len,
|
|||
return true;
|
||||
}
|
||||
|
||||
static void ad_parse(struct net_buf_simple *ad,
|
||||
bool (*func)(u8_t type, const u8_t *data,
|
||||
u8_t data_len, void *user_data),
|
||||
void *user_data)
|
||||
{
|
||||
while (ad->len > 1) {
|
||||
u8_t len = net_buf_simple_pull_u8(ad);
|
||||
u8_t type;
|
||||
|
||||
/* Check for early termination */
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (len > ad->len) {
|
||||
printk("AD malformed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
type = net_buf_simple_pull_u8(ad);
|
||||
|
||||
if (!func(type, ad->data, len - 1, user_data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
net_buf_simple_pull(ad, len - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type,
|
||||
struct net_buf_simple *ad)
|
||||
{
|
||||
|
@ -206,8 +176,7 @@ static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type,
|
|||
|
||||
/* We're only interested in connectable events */
|
||||
if (type == BT_LE_ADV_IND || type == BT_LE_ADV_DIRECT_IND) {
|
||||
/* TODO: Move this to a place it can be shared */
|
||||
ad_parse(ad, eir_found, (void *)addr);
|
||||
bt_data_parse(ad, eir_found, (void *)addr);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -130,43 +130,31 @@ static struct bt_sdp_record spp_rec = BT_SDP_RECORD(spp_attrs);
|
|||
|
||||
#endif /* CONFIG_BT_RFCOMM */
|
||||
|
||||
#define NAME_LEN 30
|
||||
|
||||
static bool data_cb(struct bt_data *data, void *user_data)
|
||||
{
|
||||
char *name = user_data;
|
||||
|
||||
switch (data->type) {
|
||||
case BT_DATA_NAME_SHORTENED:
|
||||
case BT_DATA_NAME_COMPLETE:
|
||||
memcpy(name, data->data, min(data->data_len, NAME_LEN - 1));
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t evtype,
|
||||
struct net_buf_simple *buf)
|
||||
{
|
||||
char le_addr[BT_ADDR_LE_STR_LEN];
|
||||
char name[30];
|
||||
char name[NAME_LEN];
|
||||
|
||||
memset(name, 0, sizeof(name));
|
||||
|
||||
while (buf->len > 1) {
|
||||
u8_t len, type;
|
||||
|
||||
len = net_buf_simple_pull_u8(buf);
|
||||
if (!len) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Check if field length is correct */
|
||||
if (len > buf->len) {
|
||||
break;
|
||||
}
|
||||
|
||||
type = net_buf_simple_pull_u8(buf);
|
||||
switch (type) {
|
||||
case BT_DATA_NAME_SHORTENED:
|
||||
case BT_DATA_NAME_COMPLETE:
|
||||
if (len > sizeof(name) - 1) {
|
||||
memcpy(name, buf->data, sizeof(name) - 1);
|
||||
} else {
|
||||
memcpy(name, buf->data, len - 1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
net_buf_simple_pull(buf, len - 1);
|
||||
}
|
||||
bt_data_parse(buf, data_cb, name);
|
||||
|
||||
bt_addr_le_to_str(addr, le_addr, sizeof(le_addr));
|
||||
printk("[DEVICE]: %s, AD evt type %u, RSSI %i %s\n", le_addr, evtype,
|
||||
|
|
Loading…
Reference in a new issue