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