Skip to content

Commit 6864ff2

Browse files
test: bluetooth: resume_adv: Create advertising resume tester for issue zephyrproject-rtos#51256
Signed-off-by: Esalm Essa <eslam@cloud2gnd.com>
1 parent f33d437 commit 6864ff2

File tree

23 files changed

+1252
-0
lines changed

23 files changed

+1252
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(central_hr)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.. _bluetooth_central_hr:
2+
3+
Bluetooth: Central / Heart-rate Monitor
4+
#######################################
5+
6+
Overview
7+
********
8+
9+
Similar to the :ref:`Central <bluetooth_central>` sample, except that this
10+
application specifically looks for heart-rate monitors and reports the
11+
heart-rate readings once connected.
12+
13+
Requirements
14+
************
15+
16+
* BlueZ running on the host, or
17+
* A board with BLE support
18+
19+
Building and Running
20+
********************
21+
22+
This sample can be found under :zephyr_file:`samples/bluetooth/central_hr` in the
23+
Zephyr tree.
24+
25+
See :ref:`bluetooth samples section <bluetooth-samples>` for details.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2019 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&lptmr1 {
8+
interrupt-parent = <&intmux0_ch2>;
9+
};
10+
11+
&intmux0_ch2 {
12+
status = "okay";
13+
};
14+
15+
&intmux0_ch3 {
16+
status = "okay";
17+
};
18+
19+
&generic_fsk {
20+
interrupt-parent = <&intmux0_ch3>;
21+
status = "okay";
22+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_BT=y
2+
CONFIG_BT_DEBUG_LOG=y
3+
CONFIG_BT_CENTRAL=y
4+
CONFIG_BT_SMP=y
5+
CONFIG_BT_GATT_CLIENT=y
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
name: Bluetooth Central HR
3+
tests:
4+
sample.bluetooth.central_hr:
5+
arch_allow: x86
6+
harness: bluetooth
7+
tags: bluetooth
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/* main.c - Application main entry point */
2+
3+
/*
4+
* Copyright (c) 2015-2016 Intel Corporation
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <zephyr/types.h>
10+
#include <stddef.h>
11+
#include <errno.h>
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/sys/printk.h>
14+
15+
#include <zephyr/bluetooth/bluetooth.h>
16+
#include <zephyr/bluetooth/hci.h>
17+
#include <zephyr/bluetooth/conn.h>
18+
#include <zephyr/bluetooth/uuid.h>
19+
#include <zephyr/bluetooth/gatt.h>
20+
#include <zephyr/sys/byteorder.h>
21+
22+
static void start_scan(void);
23+
24+
static struct bt_conn *default_conn;
25+
26+
static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0);
27+
static struct bt_gatt_discover_params discover_params;
28+
static struct bt_gatt_subscribe_params subscribe_params;
29+
30+
static uint8_t notify_func(struct bt_conn *conn,
31+
struct bt_gatt_subscribe_params *params,
32+
const void *data, uint16_t length)
33+
{
34+
if (!data) {
35+
printk("[UNSUBSCRIBED]\n");
36+
params->value_handle = 0U;
37+
return BT_GATT_ITER_STOP;
38+
}
39+
40+
printk("[NOTIFICATION] data %p length %u\n", data, length);
41+
42+
return BT_GATT_ITER_CONTINUE;
43+
}
44+
45+
static uint8_t discover_func(struct bt_conn *conn,
46+
const struct bt_gatt_attr *attr,
47+
struct bt_gatt_discover_params *params)
48+
{
49+
int err;
50+
51+
if (!attr) {
52+
printk("Discover complete\n");
53+
(void)memset(params, 0, sizeof(*params));
54+
return BT_GATT_ITER_STOP;
55+
}
56+
57+
printk("[ATTRIBUTE] handle %u\n", attr->handle);
58+
59+
if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_HRS)) {
60+
memcpy(&uuid, BT_UUID_HRS_MEASUREMENT, sizeof(uuid));
61+
discover_params.uuid = &uuid.uuid;
62+
discover_params.start_handle = attr->handle + 1;
63+
discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
64+
65+
err = bt_gatt_discover(conn, &discover_params);
66+
if (err) {
67+
printk("Discover failed (err %d)\n", err);
68+
}
69+
} else if (!bt_uuid_cmp(discover_params.uuid,
70+
BT_UUID_HRS_MEASUREMENT)) {
71+
memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid));
72+
discover_params.uuid = &uuid.uuid;
73+
discover_params.start_handle = attr->handle + 2;
74+
discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
75+
subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
76+
77+
err = bt_gatt_discover(conn, &discover_params);
78+
if (err) {
79+
printk("Discover failed (err %d)\n", err);
80+
}
81+
} else {
82+
subscribe_params.notify = notify_func;
83+
subscribe_params.value = BT_GATT_CCC_NOTIFY;
84+
subscribe_params.ccc_handle = attr->handle;
85+
86+
err = bt_gatt_subscribe(conn, &subscribe_params);
87+
if (err && err != -EALREADY) {
88+
printk("Subscribe failed (err %d)\n", err);
89+
} else {
90+
printk("[SUBSCRIBED]\n");
91+
}
92+
93+
return BT_GATT_ITER_STOP;
94+
}
95+
96+
return BT_GATT_ITER_STOP;
97+
}
98+
99+
static bool eir_found(struct bt_data *data, void *user_data)
100+
{
101+
bt_addr_le_t *addr = user_data;
102+
int i;
103+
104+
printk("[AD]: %u data_len %u\n", data->type, data->data_len);
105+
106+
switch (data->type) {
107+
case BT_DATA_UUID16_SOME:
108+
case BT_DATA_UUID16_ALL:
109+
if (data->data_len % sizeof(uint16_t) != 0U) {
110+
printk("AD malformed\n");
111+
return true;
112+
}
113+
114+
for (i = 0; i < data->data_len; i += sizeof(uint16_t)) {
115+
struct bt_le_conn_param *param;
116+
struct bt_uuid *uuid;
117+
uint16_t u16;
118+
int err;
119+
120+
memcpy(&u16, &data->data[i], sizeof(u16));
121+
uuid = BT_UUID_DECLARE_16(sys_le16_to_cpu(u16));
122+
if (bt_uuid_cmp(uuid, BT_UUID_HRS)) {
123+
continue;
124+
}
125+
126+
err = bt_le_scan_stop();
127+
if (err) {
128+
printk("Stop LE scan failed (err %d)\n", err);
129+
continue;
130+
}
131+
132+
param = BT_LE_CONN_PARAM_DEFAULT;
133+
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
134+
param, &default_conn);
135+
if (err) {
136+
printk("Create conn failed (err %d)\n", err);
137+
start_scan();
138+
}
139+
140+
return false;
141+
}
142+
}
143+
144+
return true;
145+
}
146+
147+
static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
148+
struct net_buf_simple *ad)
149+
{
150+
char dev[BT_ADDR_LE_STR_LEN];
151+
152+
bt_addr_le_to_str(addr, dev, sizeof(dev));
153+
printk("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",
154+
dev, type, ad->len, rssi);
155+
156+
/* We're only interested in connectable events */
157+
if (type == BT_GAP_ADV_TYPE_ADV_IND ||
158+
type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
159+
bt_data_parse(ad, eir_found, (void *)addr);
160+
}
161+
}
162+
163+
static void start_scan(void)
164+
{
165+
int err;
166+
167+
/* Use active scanning and disable duplicate filtering to handle any
168+
* devices that might update their advertising data at runtime. */
169+
struct bt_le_scan_param scan_param = {
170+
.type = BT_LE_SCAN_TYPE_ACTIVE,
171+
.options = BT_LE_SCAN_OPT_NONE,
172+
.interval = BT_GAP_SCAN_FAST_INTERVAL,
173+
.window = BT_GAP_SCAN_FAST_WINDOW,
174+
};
175+
176+
err = bt_le_scan_start(&scan_param, device_found);
177+
if (err) {
178+
printk("Scanning failed to start (err %d)\n", err);
179+
return;
180+
}
181+
182+
printk("Scanning successfully started\n");
183+
}
184+
185+
static void connected(struct bt_conn *conn, uint8_t conn_err)
186+
{
187+
char addr[BT_ADDR_LE_STR_LEN];
188+
int err;
189+
190+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
191+
192+
if (conn_err) {
193+
printk("Failed to connect to %s (%u)\n", addr, conn_err);
194+
195+
bt_conn_unref(default_conn);
196+
default_conn = NULL;
197+
198+
start_scan();
199+
return;
200+
}
201+
202+
printk("Connected: %s\n", addr);
203+
204+
if (conn == default_conn) {
205+
memcpy(&uuid, BT_UUID_HRS, sizeof(uuid));
206+
discover_params.uuid = &uuid.uuid;
207+
discover_params.func = discover_func;
208+
discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
209+
discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
210+
discover_params.type = BT_GATT_DISCOVER_PRIMARY;
211+
212+
err = bt_gatt_discover(default_conn, &discover_params);
213+
if (err) {
214+
printk("Discover failed(err %d)\n", err);
215+
return;
216+
}
217+
}
218+
}
219+
220+
static void disconnected(struct bt_conn *conn, uint8_t reason)
221+
{
222+
char addr[BT_ADDR_LE_STR_LEN];
223+
224+
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
225+
226+
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
227+
228+
if (default_conn != conn) {
229+
return;
230+
}
231+
232+
bt_conn_unref(default_conn);
233+
default_conn = NULL;
234+
}
235+
236+
BT_CONN_CB_DEFINE(conn_callbacks) = {
237+
.connected = connected,
238+
.disconnected = disconnected,
239+
};
240+
241+
void main(void)
242+
{
243+
int err;
244+
err = bt_enable(NULL);
245+
246+
if (err) {
247+
printk("Bluetooth init failed (err %d)\n", err);
248+
return;
249+
}
250+
251+
printk("Bluetooth initialized\n");
252+
253+
start_scan();
254+
255+
k_sleep(K_SECONDS(2));
256+
bt_conn_disconnect(default_conn ,BT_HCI_ERR_REMOTE_USER_TERM_CONN);
257+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(central_peripheral_hr)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.. _bluetooth_central_hr:
2+
3+
Bluetooth: Central / Heart-rate Monitor
4+
#######################################
5+
6+
Overview
7+
********
8+
9+
Similar to the :ref:`Central <bluetooth_central>` sample, except that this
10+
application specifically looks for heart-rate monitors and reports the
11+
heart-rate readings once connected.
12+
13+
Requirements
14+
************
15+
16+
* BlueZ running on the host, or
17+
* A board with BLE support
18+
19+
Building and Running
20+
********************
21+
22+
This sample can be found under :zephyr_file:`samples/bluetooth/central_hr` in the
23+
Zephyr tree.
24+
25+
See :ref:`bluetooth samples section <bluetooth-samples>` for details.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2019 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&lptmr1 {
8+
interrupt-parent = <&intmux0_ch2>;
9+
};
10+
11+
&intmux0_ch2 {
12+
status = "okay";
13+
};
14+
15+
&intmux0_ch3 {
16+
status = "okay";
17+
};
18+
19+
&generic_fsk {
20+
interrupt-parent = <&intmux0_ch3>;
21+
status = "okay";
22+
};

0 commit comments

Comments
 (0)