1 /* Copyright (c) 2023 Codecoup
2 * SPDX-License-Identifier: Apache-2.0
3 */
4
5 #include <argparse.h>
6
7 #include <zephyr/bluetooth/gatt.h>
8 #include <zephyr/logging/log.h>
9 #include <zephyr/sys/__assert.h>
10 #include <zephyr/sys/byteorder.h>
11
12 #include "testlib/adv.h"
13 #include "testlib/security.h"
14
15 #include "../common_defs.h"
16 #include "../test_utils.h"
17
18 LOG_MODULE_REGISTER(server, LOG_LEVEL_DBG);
19
read_chrc(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t buf_len,uint16_t offset)20 static ssize_t read_chrc(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
21 uint16_t buf_len, uint16_t offset)
22 {
23 return 0;
24 }
25
26 BT_GATT_SERVICE_DEFINE(test_svc,
27 BT_GATT_PRIMARY_SERVICE(TEST_SERVICE_UUID),
28 BT_GATT_CHARACTERISTIC(TEST_CHRC_UUID, BT_GATT_CHRC_READ,
29 BT_GATT_PERM_READ_ENCRYPT, read_chrc, NULL, NULL));
30
test_common(struct bt_conn ** conn)31 static void test_common(struct bt_conn **conn)
32 {
33 int err;
34
35 err = bt_enable(NULL);
36 __ASSERT_NO_MSG(!err);
37
38 __ASSERT_NO_MSG(get_device_nbr() == 1);
39 err = bt_set_name("d1");
40 __ASSERT_NO_MSG(!err);
41
42 err = bt_testlib_adv_conn(conn, BT_ID_DEFAULT, bt_get_name());
43 __ASSERT_NO_MSG(!err);
44 }
45
test_server(void)46 static void test_server(void)
47 {
48 test_common(NULL);
49
50 PASS("PASS\n");
51 }
52
test_server_security_request(void)53 static void test_server_security_request(void)
54 {
55 struct bt_conn *conn = NULL;
56 int err;
57
58 test_common(&conn);
59
60 err = bt_testlib_secure(conn, BT_SECURITY_L2);
61 __ASSERT(!err, "err %d", err);
62
63 PASS("PASS\n");
64 }
65
66 static const struct bst_test_instance server_tests[] = {
67 {
68 .test_id = "test_server",
69 .test_pre_init_f = test_init,
70 .test_tick_f = test_tick,
71 .test_main_f = test_server,
72 },
73 {
74 .test_id = "test_server_security_request",
75 .test_pre_init_f = test_init,
76 .test_tick_f = test_tick,
77 .test_main_f = test_server_security_request,
78 },
79 BSTEST_END_MARKER,
80 };
81
server_tests_install(struct bst_test_list * tests)82 static struct bst_test_list *server_tests_install(struct bst_test_list *tests)
83 {
84 return bst_add_tests(tests, server_tests);
85 };
86
87 bst_test_install_t test_installers[] = {
88 server_tests_install,
89 NULL
90 };
91
main(void)92 int main(void)
93 {
94 bst_main();
95
96 return 0;
97 }
98