1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stddef.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <string.h>
11 
12 #include <zephyr/types.h>
13 #include <zephyr/kernel.h>
14 
15 #include <zephyr/sys/printk.h>
16 #include <zephyr/sys/byteorder.h>
17 
18 #include <zephyr/bluetooth/att.h>
19 #include <zephyr/bluetooth/bluetooth.h>
20 #include <zephyr/bluetooth/hci.h>
21 #include <zephyr/bluetooth/conn.h>
22 #include <zephyr/bluetooth/uuid.h>
23 #include <zephyr/bluetooth/gatt.h>
24 
25 #define MTU_TEST_SERVICE_TYPE BT_UUID_128_ENCODE(0x2e2b8dc3, 0x06e0, 0x4f93, 0x9bb2, 0x734091c356f0)
26 #define MTU_TEST_SERVICE_NOTIFY_TYPE                                                               \
27 	BT_UUID_128_ENCODE(0x2e2b8dc3, 0x06e0, 0x4f93, 0x9bb2, 0x734091c356f3)
28 
29 #define BT_UUID_MTU_TEST	BT_UUID_DECLARE_128(MTU_TEST_SERVICE_TYPE)
30 #define BT_UUID_MTU_TEST_NOTIFY BT_UUID_DECLARE_128(MTU_TEST_SERVICE_NOTIFY_TYPE)
31 
32 static void start_scan(void);
33 
34 static struct bt_conn *default_conn;
35 
36 static struct bt_uuid_128 uuid = BT_UUID_INIT_128(0);
37 static const struct bt_uuid *ccc_uuid = BT_UUID_GATT_CCC;
38 
39 static struct bt_gatt_discover_params discover_params;
40 static struct bt_gatt_subscribe_params subscribe_params;
41 
42 bt_gatt_notify_func_t notify_cb;
43 
notify_func(struct bt_conn * conn,struct bt_gatt_subscribe_params * params,const void * data,uint16_t length)44 static uint8_t notify_func(struct bt_conn *conn,
45 			   struct bt_gatt_subscribe_params *params,
46 			   const void *data, uint16_t length)
47 {
48 	if (!data) {
49 		printk("[UNSUBSCRIBED]\n");
50 		params->value_handle = 0U;
51 		return BT_GATT_ITER_STOP;
52 	}
53 
54 	printk("[NOTIFICATION] data %p length %u\n", data, length);
55 
56 	if (notify_cb != NULL) {
57 		notify_cb(conn, params, data, length);
58 	}
59 
60 	return BT_GATT_ITER_STOP;
61 }
62 
discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)63 static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
64 			     struct bt_gatt_discover_params *params)
65 {
66 	int err;
67 
68 	if (!attr) {
69 		printk("Discover complete\n");
70 		(void)memset(params, 0, sizeof(*params));
71 		return BT_GATT_ITER_STOP;
72 	}
73 
74 	printk("[ATTRIBUTE] handle %u\n", attr->handle);
75 
76 	if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_MTU_TEST)) {
77 		memcpy(&uuid, BT_UUID_MTU_TEST_NOTIFY, sizeof(uuid));
78 		discover_params.uuid = &uuid.uuid;
79 		discover_params.start_handle = attr->handle + 1;
80 		discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
81 
82 		err = bt_gatt_discover(conn, &discover_params);
83 		if (err) {
84 			printk("Discover failed (err %d)\n", err);
85 		}
86 	} else if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_MTU_TEST_NOTIFY)) {
87 		discover_params.uuid = ccc_uuid;
88 		discover_params.start_handle = attr->handle + 2;
89 		discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
90 		subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
91 
92 		err = bt_gatt_discover(conn, &discover_params);
93 		if (err) {
94 			printk("Discover failed (err %d)\n", err);
95 		}
96 	} else {
97 		subscribe_params.notify = notify_func;
98 		subscribe_params.value = BT_GATT_CCC_NOTIFY;
99 		subscribe_params.ccc_handle = attr->handle;
100 
101 		err = bt_gatt_subscribe(conn, &subscribe_params);
102 		if (err && err != -EALREADY) {
103 			printk("Subscribe failed (err %d)\n", err);
104 		} else {
105 			printk("[SUBSCRIBED]\n");
106 		}
107 
108 		return BT_GATT_ITER_STOP;
109 	}
110 
111 	return BT_GATT_ITER_STOP;
112 }
113 
device_found(const bt_addr_le_t * addr,int8_t rssi,uint8_t type,struct net_buf_simple * ad)114 static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
115 			 struct net_buf_simple *ad)
116 {
117 	char addr_str[BT_ADDR_LE_STR_LEN];
118 	int err;
119 
120 	if (default_conn) {
121 		return;
122 	}
123 
124 	/* We're only interested in connectable events */
125 	if (type != BT_GAP_ADV_TYPE_ADV_IND &&
126 	    type != BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
127 		return;
128 	}
129 
130 	bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
131 	printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
132 
133 	/* connect only to devices in close proximity */
134 	if (rssi < -50) {
135 		return;
136 	}
137 
138 	if (bt_le_scan_stop()) {
139 		return;
140 	}
141 
142 	err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
143 				BT_LE_CONN_PARAM_DEFAULT, &default_conn);
144 	if (err) {
145 		printk("Create conn to %s failed (%u)\n", addr_str, err);
146 		start_scan();
147 	}
148 }
149 
start_scan(void)150 static void start_scan(void)
151 {
152 	int err;
153 
154 	err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
155 	if (err) {
156 		printk("Scanning failed to start (err %d)\n", err);
157 		return;
158 	}
159 
160 	printk("Scanning successfully started\n");
161 }
162 
mtu_exchange_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_exchange_params * params)163 static void mtu_exchange_cb(struct bt_conn *conn, uint8_t err,
164 			    struct bt_gatt_exchange_params *params)
165 {
166 	printk("%s: MTU exchange %s (%u)\n", __func__,
167 	       err == 0U ? "successful" : "failed",
168 	       bt_gatt_get_mtu(conn));
169 }
170 
171 static struct bt_gatt_exchange_params mtu_exchange_params = {
172 	.func = mtu_exchange_cb
173 };
174 
mtu_exchange(struct bt_conn * conn)175 static int mtu_exchange(struct bt_conn *conn)
176 {
177 	int err;
178 
179 	printk("%s: Current MTU = %u\n", __func__, bt_gatt_get_mtu(conn));
180 
181 	printk("%s: Exchange MTU...\n", __func__);
182 	err = bt_gatt_exchange_mtu(conn, &mtu_exchange_params);
183 	if (err) {
184 		printk("%s: MTU exchange failed (err %d)", __func__, err);
185 	}
186 
187 	return err;
188 }
189 
connected(struct bt_conn * conn,uint8_t err)190 static void connected(struct bt_conn *conn, uint8_t err)
191 {
192 	char addr[BT_ADDR_LE_STR_LEN];
193 
194 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
195 
196 	if (err) {
197 		printk("Failed to connect to %s %u %s\n", addr, err, bt_hci_err_to_str(err));
198 
199 		bt_conn_unref(default_conn);
200 		default_conn = NULL;
201 
202 		start_scan();
203 		return;
204 	}
205 
206 	printk("Connected: %s\n", addr);
207 
208 	(void)mtu_exchange(conn);
209 
210 	if (conn == default_conn) {
211 		memcpy(&uuid, BT_UUID_MTU_TEST, sizeof(uuid));
212 		discover_params.uuid = &uuid.uuid;
213 		discover_params.func = discover_func;
214 		discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
215 		discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
216 		discover_params.type = BT_GATT_DISCOVER_PRIMARY;
217 
218 		err = bt_gatt_discover(default_conn, &discover_params);
219 		if (err) {
220 			printk("Discover failed(err %d)\n", err);
221 			return;
222 		}
223 	}
224 }
225 
disconnected(struct bt_conn * conn,uint8_t reason)226 static void disconnected(struct bt_conn *conn, uint8_t reason)
227 {
228 	char addr[BT_ADDR_LE_STR_LEN];
229 
230 	if (conn != default_conn) {
231 		return;
232 	}
233 
234 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
235 
236 	printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
237 
238 	bt_conn_unref(default_conn);
239 	default_conn = NULL;
240 
241 	start_scan();
242 }
243 
244 BT_CONN_CB_DEFINE(conn_callbacks) = {
245 	.connected = connected,
246 	.disconnected = disconnected,
247 };
248 
run_central_sample(bt_gatt_notify_func_t cb)249 void run_central_sample(bt_gatt_notify_func_t cb)
250 {
251 	int err;
252 
253 	notify_cb = cb;
254 
255 	err = bt_enable(NULL);
256 	if (err) {
257 		printk("Bluetooth init failed (err %d)\n", err);
258 		return;
259 	}
260 
261 	printk("Bluetooth initialized\n");
262 
263 	start_scan();
264 }
265