1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/gatt.h>
8 #include <zephyr/kernel.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <zephyr/sys/printk.h>
12 
13 #include <zephyr/bluetooth/bluetooth.h>
14 #include <zephyr/bluetooth/conn.h>
15 #include <zephyr/bluetooth/uuid.h>
16 #include <zephyr/sys/util.h>
17 
18 #define MTU_TEST_SERVICE_TYPE BT_UUID_128_ENCODE(0x2e2b8dc3, 0x06e0, 0x4f93, 0x9bb2, 0x734091c356f0)
19 
20 /* Overhead: opcode (u8) + handle (u16) */
21 #define ATT_NTF_SIZE(payload_len) (1 + 2 + payload_len)
22 
23 static const struct bt_uuid_128 mtu_test_service = BT_UUID_INIT_128(MTU_TEST_SERVICE_TYPE);
24 
25 static const struct bt_uuid_128 notify_characteristic_uuid =
26 	BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x2e2b8dc3, 0x06e0, 0x4f93, 0x9bb2, 0x734091c356f3));
27 
28 static const struct bt_data adv_ad_data[] = {
29 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
30 	BT_DATA_BYTES(BT_DATA_UUID128_ALL, MTU_TEST_SERVICE_TYPE),
31 	BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
32 };
33 
34 static struct bt_conn *default_conn;
35 
ccc_cfg_changed(const struct bt_gatt_attr * attr,uint16_t value)36 static void ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
37 {
38 	ARG_UNUSED(attr);
39 
40 	bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
41 
42 	printk("MTU Test Update: notifications %s\n", notif_enabled ? "enabled" : "disabled");
43 }
44 
45 BT_GATT_SERVICE_DEFINE(mtu_test, BT_GATT_PRIMARY_SERVICE(&mtu_test_service),
46 		       BT_GATT_CHARACTERISTIC(&notify_characteristic_uuid.uuid, BT_GATT_CHRC_NOTIFY,
47 					      BT_GATT_PERM_NONE, NULL, NULL, NULL),
48 		       BT_GATT_CCC(ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE));
49 
mtu_updated(struct bt_conn * conn,uint16_t tx,uint16_t rx)50 void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
51 {
52 	printk("Updated MTU: TX: %d RX: %d bytes\n", tx, rx);
53 }
54 
55 static struct bt_gatt_cb gatt_callbacks = {
56 	.att_mtu_updated = mtu_updated,
57 };
58 
connected(struct bt_conn * conn,uint8_t err)59 static void connected(struct bt_conn *conn, uint8_t err)
60 {
61 	if (err != 0) {
62 		return;
63 	}
64 
65 	default_conn = bt_conn_ref(conn);
66 }
67 
disconnected(struct bt_conn * conn,uint8_t reason)68 static void disconnected(struct bt_conn *conn, uint8_t reason)
69 {
70 	bt_conn_unref(conn);
71 	default_conn = NULL;
72 }
73 
74 BT_CONN_CB_DEFINE(conn_callbacks) = {
75 	.connected = connected,
76 	.disconnected = disconnected,
77 };
78 
run_peripheral_sample(uint8_t * notify_data,size_t notify_data_size,uint16_t seconds)79 void run_peripheral_sample(uint8_t *notify_data, size_t notify_data_size, uint16_t seconds)
80 {
81 	int err;
82 
83 	err = bt_enable(NULL);
84 	if (err) {
85 		printk("Bluetooth init failed (err %d)\n", err);
86 		return;
87 	}
88 
89 	bt_gatt_cb_register(&gatt_callbacks);
90 
91 	struct bt_gatt_attr *notify_crch =
92 		bt_gatt_find_by_uuid(mtu_test.attrs, 0xffff, &notify_characteristic_uuid.uuid);
93 
94 	bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, adv_ad_data, ARRAY_SIZE(adv_ad_data), NULL, 0);
95 
96 	bool infinite = seconds == 0;
97 
98 	for (int i = 0; (i < seconds) || infinite; i++) {
99 		k_sleep(K_SECONDS(1));
100 		/* Only send the notification if the UATT MTU supports the required length */
101 		if (bt_gatt_get_uatt_mtu(default_conn) >= ATT_NTF_SIZE(notify_data_size)) {
102 			bt_gatt_notify(default_conn, notify_crch, notify_data, notify_data_size);
103 		} else {
104 			printk("Skipping notification since UATT MTU is not sufficient."
105 			       "Required: %d, Actual: %d\n",
106 			       ATT_NTF_SIZE(notify_data_size),
107 			       bt_gatt_get_uatt_mtu(default_conn));
108 		}
109 	}
110 }
111