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 static struct bt_conn *g_conn;
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 g_conn = bt_conn_ref(conn);
29 SET_FLAG(flag_is_connected);
30 }
31
disconnected(struct bt_conn * conn,uint8_t reason)32 static void disconnected(struct bt_conn *conn, uint8_t reason)
33 {
34 char addr[BT_ADDR_LE_STR_LEN];
35
36 if (conn != g_conn) {
37 return;
38 }
39
40 bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
41
42 printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
43
44 bt_conn_unref(g_conn);
45
46 g_conn = NULL;
47 UNSET_FLAG(flag_is_connected);
48 }
49
50 static struct bt_conn_cb conn_callbacks = {
51 .connected = connected,
52 .disconnected = disconnected,
53 };
54
55 static uint8_t chrc_data[CHRC_SIZE];
56 static uint8_t long_chrc_data[LONG_CHRC_SIZE];
57
read_test_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)58 static ssize_t read_test_chrc(struct bt_conn *conn,
59 const struct bt_gatt_attr *attr,
60 void *buf, uint16_t len, uint16_t offset)
61 {
62 return bt_gatt_attr_read(conn, attr, buf, len, offset,
63 (void *)chrc_data, sizeof(chrc_data));
64 }
65
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)66 static ssize_t write_test_chrc(struct bt_conn *conn,
67 const struct bt_gatt_attr *attr,
68 const void *buf, uint16_t len,
69 uint16_t offset, uint8_t flags)
70 {
71 printk("chrc len %u offset %u\n", len, offset);
72
73 if (len > sizeof(chrc_data)) {
74 printk("Invalid chrc length\n");
75 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
76 } else if (offset + len > sizeof(chrc_data)) {
77 printk("Invalid chrc offset and length\n");
78 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
79 }
80
81 if (flags != 0) {
82 FAIL("Invalid flags %u\n", flags);
83 return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
84 }
85
86 (void)memcpy(chrc_data + offset, buf, len);
87
88 return len;
89 }
90
read_long_test_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)91 static ssize_t read_long_test_chrc(struct bt_conn *conn,
92 const struct bt_gatt_attr *attr,
93 void *buf, uint16_t len, uint16_t offset)
94 {
95 return bt_gatt_attr_read(conn, attr, buf, len, offset,
96 (void *)long_chrc_data, sizeof(long_chrc_data));
97 }
98
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)99 static ssize_t write_long_test_chrc(struct bt_conn *conn,
100 const struct bt_gatt_attr *attr,
101 const void *buf, uint16_t len,
102 uint16_t offset, uint8_t flags)
103 {
104 static uint8_t prepare_count;
105
106 printk("long_chrc len %u offset %u\n", len, offset);
107
108 if (len > sizeof(long_chrc_data)) {
109 printk("Invalid long_chrc length\n");
110 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
111 } else if (offset + len > sizeof(long_chrc_data)) {
112 printk("Invalid long_chrc offset and length\n");
113 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
114 }
115
116 if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
117 printk("prepare_count %u\n", prepare_count++);
118 return BT_GATT_ERR(BT_ATT_ERR_SUCCESS);
119 }
120
121 (void)memcpy(long_chrc_data + offset, buf, len);
122 prepare_count = 0;
123
124 return len;
125 }
126
127 BT_GATT_SERVICE_DEFINE(test_svc,
128 BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID),
129 BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID,
130 BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
131 BT_GATT_PERM_WRITE | BT_GATT_PERM_READ,
132 read_test_chrc, write_test_chrc, NULL),
133 BT_GATT_CHARACTERISTIC(TEST_LONG_CHRC_UUID,
134 BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
135 BT_GATT_PERM_WRITE | BT_GATT_PERM_READ | BT_GATT_PERM_PREPARE_WRITE,
136 read_long_test_chrc, write_long_test_chrc, NULL),
137 BT_GATT_CHARACTERISTIC(TEST_ENC_CHRC_UUID,
138 BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
139 BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
140 read_test_chrc, write_test_chrc, NULL),
141 BT_GATT_CHARACTERISTIC(TEST_LESC_CHRC_UUID,
142 BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
143 BT_GATT_PERM_READ_LESC | BT_GATT_PERM_WRITE_LESC,
144 read_test_chrc, write_test_chrc, NULL),
145 );
146
test_main(void)147 static void test_main(void)
148 {
149 int err;
150 const struct bt_data ad[] = {
151 BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))
152 };
153
154 bt_conn_cb_register(&conn_callbacks);
155
156 err = bt_enable(NULL);
157 if (err != 0) {
158 FAIL("Bluetooth init failed (err %d)\n", err);
159 return;
160 }
161
162 printk("Bluetooth initialized\n");
163
164 err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
165 if (err != 0) {
166 FAIL("Advertising failed to start (err %d)\n", err);
167 return;
168 }
169
170 printk("Advertising successfully started\n");
171
172 WAIT_FOR_FLAG(flag_is_connected);
173
174 PASS("GATT server passed\n");
175 }
176
177 static const struct bst_test_instance test_gatt_server[] = {
178 {
179 .test_id = "gatt_server",
180 .test_pre_init_f = test_init,
181 .test_tick_f = test_tick,
182 .test_main_f = test_main
183 },
184 BSTEST_END_MARKER
185 };
186
test_gatt_server_install(struct bst_test_list * tests)187 struct bst_test_list *test_gatt_server_install(struct bst_test_list *tests)
188 {
189 return bst_add_tests(tests, test_gatt_server);
190 }
191