-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.c
281 lines (221 loc) · 6.63 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
* @brief Central Heart Rate over LE Coded PHY sample
*/
#include <zephyr/types.h>
#include <stddef.h>
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
#include <bluetooth/gatt_dm.h>
#include <bluetooth/scan.h>
#include <bluetooth/services/hrs_client.h>
static struct bt_conn *default_conn;
static struct bt_hrs_client hrs_c;
static void notify_func(struct bt_hrs_client *hrs_c,
const struct bt_hrs_client_measurement *meas,
int err)
{
if (err) {
printk("Error during receiving Heart Rate Measurement notification, err: %d\n",
err);
return;
}
printk("Heart Rate Measurement notification received:\n\n");
printk("\tHeart Rate Measurement Value Format: %s\n",
meas->flags.value_format ? "16 - bit" : "8 - bit");
printk("\tSensor Contact detected: %d\n", meas->flags.sensor_contact_detected);
printk("\tSensor Contact supported: %d\n", meas->flags.sensor_contact_supported);
printk("\tEnergy Expended present: %d\n", meas->flags.energy_expended_present);
printk("\tRR-Intervals present: %d\n", meas->flags.rr_intervals_present);
printk("\n\tHeart Rate Measurement Value: %d bpm\n", meas->hr_value);
if (meas->flags.energy_expended_present) {
printk("\n\tEnergy Expended: %d J\n", meas->energy_expended);
}
if (meas->flags.rr_intervals_present) {
printk("\t RR-intervals: ");
for (size_t i = 0; i < meas->rr_intervals_count; i++) {
printk("%d ", meas->rr_intervals[i]);
}
}
printk("\n");
}
static void discover_hrs_completed(struct bt_gatt_dm *dm, void *ctx)
{
int err;
printk("The discovery procedure succeeded\n");
bt_gatt_dm_data_print(dm);
err = bt_hrs_client_handles_assign(dm, &hrs_c);
if (err) {
printk("Could not init HRS client object (err %d)\n", err);
return;
}
err = bt_hrs_client_measurement_subscribe(&hrs_c, notify_func);
if (err && err != -EALREADY) {
printk("Subscribe failed (err %d)\n", err);
} else {
printk("[SUBSCRIBED]\n");
}
err = bt_gatt_dm_data_release(dm);
if (err) {
printk("Could not release the discovery data (err %d)\n", err);
}
}
static void discover_hrs_service_not_found(struct bt_conn *conn, void *ctx)
{
printk("No more services\n");
}
static void discover_hrs_error_found(struct bt_conn *conn, int err, void *ctx)
{
printk("The discovery procedure failed, err %d\n", err);
}
static struct bt_gatt_dm_cb discover_hrs_cb = {
.completed = discover_hrs_completed,
.service_not_found = discover_hrs_service_not_found,
.error_found = discover_hrs_error_found,
};
static void scan_filter_match(struct bt_scan_device_info *device_info,
struct bt_scan_filter_match *filter_match,
bool connectable)
{
int err;
char addr[BT_ADDR_LE_STR_LEN];
struct bt_conn_le_create_param *conn_params;
bt_addr_le_to_str(device_info->recv_info->addr, addr, sizeof(addr));
printk("Filters matched. Address: %s connectable: %s\n",
addr, connectable ? "yes" : "no");
err = bt_scan_stop();
if (err) {
printk("Stop LE scan failed (err %d)\n", err);
}
conn_params = BT_CONN_LE_CREATE_PARAM(
BT_CONN_LE_OPT_CODED | BT_CONN_LE_OPT_NO_1M,
BT_GAP_SCAN_FAST_INTERVAL,
BT_GAP_SCAN_FAST_INTERVAL);
err = bt_conn_le_create(device_info->recv_info->addr, conn_params,
BT_LE_CONN_PARAM_DEFAULT,
&default_conn);
if (err) {
printk("Create conn failed (err %d)\n", err);
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return;
}
}
printk("Connection pending\n");
}
BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL, NULL, NULL);
static void scan_init(void)
{
int err;
/* Use active scanning and disable duplicate filtering to handle any
* devices that might update their advertising data at runtime. */
struct bt_le_scan_param scan_param = {
.type = BT_LE_SCAN_TYPE_ACTIVE,
.interval = BT_GAP_SCAN_FAST_INTERVAL,
.window = BT_GAP_SCAN_FAST_WINDOW,
.options = BT_LE_SCAN_OPT_CODED | BT_LE_SCAN_OPT_NO_1M
};
struct bt_scan_init_param scan_init = {
.connect_if_match = 0,
.scan_param = &scan_param,
.conn_param = NULL
};
bt_scan_init(&scan_init);
bt_scan_cb_register(&scan_cb);
err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_HRS);
if (err) {
printk("Scanning filters cannot be set (err %d)\n", err);
return;
}
err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
if (err) {
printk("Filters cannot be turned on (err %d)\n", err);
}
}
static void connected(struct bt_conn *conn, uint8_t conn_err)
{
int err;
struct bt_conn_info info;
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (conn_err) {
printk("Failed to connect to %s (%u)\n", addr, conn_err);
bt_conn_unref(default_conn);
default_conn = NULL;
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
}
return;
}
err = bt_conn_get_info(conn, &info);
if (err) {
printk("Failed to get connection info (err %d)\n", err);
} else {
const struct bt_conn_le_phy_info *phy_info;
phy_info = info.le.phy;
printk("Connected: %s, tx_phy %u, rx_phy %u\n",
addr, phy_info->tx_phy, phy_info->rx_phy);
}
if (conn == default_conn) {
err = bt_gatt_dm_start(conn, BT_UUID_HRS, &discover_hrs_cb, NULL);
if (err) {
printk("Failed to start discovery (err %d)\n", err);
}
}
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
int err;
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
if (default_conn != conn) {
return;
}
bt_conn_unref(default_conn);
default_conn = NULL;
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
}
}
BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
};
int main(void)
{
int err;
printk("Starting Bluetooth Central HR coded example\n");
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return 0;
}
printk("Bluetooth initialized\n");
err = bt_hrs_client_init(&hrs_c);
if (err) {
printk("Heart Rate Service client failed to init (err %d)\n", err);
return 0;
}
scan_init();
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return 0;
}
printk("Scanning successfully started\n");
return 0;
}