1 /**
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 */
7
8 #include <stdint.h>
9
10 #include <zephyr/bluetooth/addr.h>
11 #include <zephyr/bluetooth/conn.h>
12 #include <zephyr/bluetooth/gatt.h>
13 #include <zephyr/bluetooth/uuid.h>
14 #include <zephyr/settings/settings.h>
15 #include <zephyr/bluetooth/bluetooth.h>
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(test_peripheral, LOG_LEVEL_DBG);
19
20 #include "bs_bt_utils.h"
21
22 #define UUID_1 BT_UUID_DECLARE_128(0xdb, 0x1f, 0xe2, 0x52, 0xf3, 0xc6, 0x43, 0x66, \
23 0xb3, 0x92, 0x5d, 0xc6, 0xe7, 0xc9, 0x59, 0x9d)
24 #define UUID_2 BT_UUID_DECLARE_128(0x3f, 0xa4, 0x7f, 0x44, 0x2e, 0x2a, 0x43, 0x05, \
25 0xab, 0x38, 0x07, 0x8d, 0x16, 0xbf, 0x99, 0xf1)
26
new_svc_ccc_cfg_changed(const struct bt_gatt_attr * attr,uint16_t value)27 static void new_svc_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
28 {
29 ARG_UNUSED(attr);
30
31 bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
32
33 LOG_DBG("CCC Update: notification %s", notif_enabled ? "enabled" : "disabled");
34 }
35
36 static struct bt_gatt_attr attrs[] = {
37 BT_GATT_PRIMARY_SERVICE(UUID_1),
38 BT_GATT_CHARACTERISTIC(UUID_2, BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_NONE, NULL, NULL, NULL),
39 BT_GATT_CCC(new_svc_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
40 };
41
42 static struct bt_gatt_service svc = {
43 .attrs = attrs,
44 .attr_count = ARRAY_SIZE(attrs),
45 };
46
peripheral(void)47 void peripheral(void)
48 {
49 /*
50 * test goal: check that service changed indication is sent on
51 * reconnection when the server's GATT database has been updated since
52 * last connection
53 *
54 * the peripheral will wait for connection/disconnection, when
55 * disconnected it will register a new service, when reconnecting, the
56 * central should receive an indication
57 */
58
59 int err;
60 struct bt_le_ext_adv *adv = NULL;
61
62 err = bt_enable(NULL);
63 BSIM_ASSERT(!err, "bt_enable failed (%d)\n", err);
64
65 err = settings_load();
66 BSIM_ASSERT(!err, "settings_load failed (%d)\n", err);
67
68 create_adv(&adv);
69 start_adv(adv);
70 wait_connected();
71
72 stop_adv(adv);
73
74 wait_disconnected();
75 clear_g_conn();
76
77 /* add a new service to trigger the service changed indication */
78 err = bt_gatt_service_register(&svc);
79 BSIM_ASSERT(!err, "bt_gatt_service_register failed (%d)\n", err);
80 LOG_DBG("New service added");
81
82 start_adv(adv);
83 wait_connected();
84
85 PASS("Done\n");
86 }
87