1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "common.h"
8
9 extern enum bst_result_t bst_result;
10
11 CREATE_FLAG(flag_is_connected);
12
13 #define NUM_ITERATIONS 10
14
connected(struct bt_conn * conn,uint8_t err)15 static void connected(struct bt_conn *conn, uint8_t err)
16 {
17 char addr[BT_ADDR_LE_STR_LEN];
18
19 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
20
21 if (err != 0) {
22 FAIL("Failed to connect to %s (%u)\n", addr, err);
23 return;
24 }
25
26 printk("Connected to %s\n", addr);
27
28 SET_FLAG(flag_is_connected);
29 }
30
disconnected(struct bt_conn * conn,uint8_t reason)31 static void disconnected(struct bt_conn *conn, uint8_t reason)
32 {
33 char addr[BT_ADDR_LE_STR_LEN];
34
35 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
36
37 printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
38
39 UNSET_FLAG(flag_is_connected);
40 }
41
42 BT_CONN_CB_DEFINE(conn_callbacks) = {
43 .connected = connected,
44 .disconnected = disconnected,
45 };
46
47 static uint8_t chrc_data[CHRC_SIZE];
48 static uint8_t long_chrc_data[LONG_CHRC_SIZE];
49
read_test_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)50 static ssize_t read_test_chrc(struct bt_conn *conn,
51 const struct bt_gatt_attr *attr,
52 void *buf, uint16_t len, uint16_t offset)
53 {
54 return bt_gatt_attr_read(conn, attr, buf, len, offset,
55 (void *)chrc_data, sizeof(chrc_data));
56 }
57
write_test_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)58 static ssize_t write_test_chrc(struct bt_conn *conn,
59 const struct bt_gatt_attr *attr,
60 const void *buf, uint16_t len,
61 uint16_t offset, uint8_t flags)
62 {
63 printk("chrc len %u offset %u\n", len, offset);
64
65 if (len > sizeof(chrc_data)) {
66 printk("Invalid chrc length\n");
67 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
68 } else if (offset + len > sizeof(chrc_data)) {
69 printk("Invalid chrc offset and length\n");
70 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
71 }
72
73 if (flags != 0) {
74 FAIL("Invalid flags %u\n", flags);
75 return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
76 }
77
78 (void)memcpy(chrc_data + offset, buf, len);
79
80 return len;
81 }
82
read_long_test_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)83 static ssize_t read_long_test_chrc(struct bt_conn *conn,
84 const struct bt_gatt_attr *attr,
85 void *buf, uint16_t len, uint16_t offset)
86 {
87 return bt_gatt_attr_read(conn, attr, buf, len, offset,
88 (void *)long_chrc_data, sizeof(long_chrc_data));
89 }
90
write_long_test_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)91 static ssize_t write_long_test_chrc(struct bt_conn *conn,
92 const struct bt_gatt_attr *attr,
93 const void *buf, uint16_t len,
94 uint16_t offset, uint8_t flags)
95 {
96 static uint8_t prepare_count;
97
98 printk("long_chrc len %u offset %u\n", len, offset);
99
100 if (len > sizeof(long_chrc_data)) {
101 printk("Invalid long_chrc length\n");
102 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
103 } else if (offset + len > sizeof(long_chrc_data)) {
104 printk("Invalid long_chrc offset and length\n");
105 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
106 }
107
108 if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
109 printk("prepare_count %u\n", prepare_count++);
110 return BT_GATT_ERR(BT_ATT_ERR_SUCCESS);
111 }
112
113 (void)memcpy(long_chrc_data + offset, buf, len);
114 prepare_count = 0;
115
116 return len;
117 }
118
119 BT_GATT_SERVICE_DEFINE(test_svc,
120 BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID),
121 BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID,
122 BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
123 BT_GATT_PERM_WRITE | BT_GATT_PERM_READ,
124 read_test_chrc, write_test_chrc, NULL),
125 BT_GATT_CHARACTERISTIC(TEST_LONG_CHRC_UUID,
126 BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
127 BT_GATT_PERM_WRITE | BT_GATT_PERM_READ | BT_GATT_PERM_PREPARE_WRITE,
128 read_long_test_chrc, write_long_test_chrc, NULL),
129 );
130
test_main(void)131 static void test_main(void)
132 {
133 int err;
134 const struct bt_data ad[] = {
135 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))
136 };
137
138 for (int i = 0; i < NUM_ITERATIONS; i++) {
139 err = bt_enable(NULL);
140 if (err != 0) {
141 FAIL("Bluetooth init failed (err %d)\n", err);
142 return;
143 }
144
145 printk("Bluetooth initialized\n");
146
147 err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
148 if (err != 0) {
149 FAIL("Advertising failed to start (err %d)\n", err);
150 return;
151 }
152
153 printk("Advertising successfully started\n");
154
155 WAIT_FOR_FLAG(flag_is_connected);
156
157 WAIT_FOR_FLAG_UNSET(flag_is_connected);
158
159 err = bt_disable();
160 if (err != 0) {
161 FAIL("Bluetooth disable failed (err %d)\n", err);
162 return;
163 }
164
165 printk("Bluetooth disabled\n");
166 }
167
168 PASS("GATT server passed\n");
169 }
170
171 static const struct bst_test_instance test_gatt_server[] = {
172 {
173 .test_id = "gatt_server",
174 .test_pre_init_f = test_init,
175 .test_tick_f = test_tick,
176 .test_main_f = test_main
177 },
178 BSTEST_END_MARKER
179 };
180
test_gatt_server_install(struct bst_test_list * tests)181 struct bst_test_list *test_gatt_server_install(struct bst_test_list *tests)
182 {
183 return bst_add_tests(tests, test_gatt_server);
184 }
185