1 /*
2 * Copyright (c) 2022 Codecoup
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stddef.h>
8
9 #include <zephyr/autoconf.h>
10 #include <zephyr/bluetooth/bluetooth.h>
11 #include <zephyr/bluetooth/services/ias.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/printk.h>
14
15 #include "bstests.h"
16 #include "common.h"
17
18 #ifdef CONFIG_BT_IAS_CLIENT
19
20 extern enum bst_result_t bst_result;
21
22 CREATE_FLAG(g_service_discovered);
23
discover_cb(struct bt_conn * conn,int err)24 static void discover_cb(struct bt_conn *conn, int err)
25 {
26 if (err) {
27 FAIL("Failed to discover IAS (err %d)\n", err);
28 return;
29 }
30
31 printk("IAS discovered\n");
32 SET_FLAG(g_service_discovered);
33 }
34
35 static const struct bt_ias_client_cb ias_client_cb = {
36 .discover = discover_cb,
37 };
38
test_alert_high(struct bt_conn * conn)39 static void test_alert_high(struct bt_conn *conn)
40 {
41 int err;
42
43 err = bt_ias_client_alert_write(conn, BT_IAS_ALERT_LVL_HIGH_ALERT);
44 if (err == 0) {
45 printk("High alert sent\n");
46 } else {
47 FAIL("Failed to send high alert\n");
48 }
49 }
50
test_alert_mild(struct bt_conn * conn)51 static void test_alert_mild(struct bt_conn *conn)
52 {
53 int err;
54
55 err = bt_ias_client_alert_write(conn, BT_IAS_ALERT_LVL_MILD_ALERT);
56 if (err == 0) {
57 printk("Mild alert sent\n");
58 } else {
59 FAIL("Failed to send mild alert\n");
60 }
61 }
62
test_alert_stop(struct bt_conn * conn)63 static void test_alert_stop(struct bt_conn *conn)
64 {
65 int err;
66
67 err = bt_ias_client_alert_write(conn, BT_IAS_ALERT_LVL_NO_ALERT);
68 if (err == 0) {
69 printk("Stop alert sent\n");
70 } else {
71 FAIL("Failed to send no alert\n");
72 }
73 }
74
discover_ias(void)75 static void discover_ias(void)
76 {
77 int err;
78
79 UNSET_FLAG(g_service_discovered);
80
81 err = bt_ias_discover(default_conn);
82 if (err < 0) {
83 FAIL("Failed to discover IAS (err %d)\n", err);
84 return;
85 }
86
87 WAIT_FOR_FLAG(g_service_discovered);
88 }
89
test_main(void)90 static void test_main(void)
91 {
92 int err;
93
94 err = bt_enable(NULL);
95 if (err < 0) {
96 FAIL("Bluetooth discover failed (err %d)\n", err);
97 return;
98 }
99
100 printk("Bluetooth initialized\n");
101
102 err = bt_ias_client_cb_register(&ias_client_cb);
103 if (err < 0) {
104 FAIL("Failed to register callbacks (err %d)\n", err);
105 return;
106 }
107
108 bt_le_scan_cb_register(&common_scan_cb);
109
110 err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, NULL);
111 if (err < 0) {
112 FAIL("Scanning failed to start (err %d)\n", err);
113 return;
114 }
115
116 printk("Scanning successfully started\n");
117
118 WAIT_FOR_FLAG(flag_connected);
119
120 discover_ias();
121 discover_ias(); /* test that we can discover twice */
122
123 /* Set alert levels with a delay to let the server handle any changes it want */
124 test_alert_high(default_conn);
125 k_sleep(K_SECONDS(1));
126
127 test_alert_mild(default_conn);
128 k_sleep(K_SECONDS(1));
129
130 test_alert_stop(default_conn);
131 k_sleep(K_SECONDS(1));
132
133 PASS("IAS client PASS\n");
134 }
135
136 static const struct bst_test_instance test_ias[] = {
137 {
138 .test_id = "ias_client",
139 .test_pre_init_f = test_init,
140 .test_tick_f = test_tick,
141 .test_main_f = test_main,
142 },
143 BSTEST_END_MARKER
144 };
145
test_ias_client_install(struct bst_test_list * tests)146 struct bst_test_list *test_ias_client_install(struct bst_test_list *tests)
147 {
148 return bst_add_tests(tests, test_ias);
149 }
150 #else
test_ias_client_install(struct bst_test_list * tests)151 struct bst_test_list *test_ias_client_install(struct bst_test_list *tests)
152 {
153 return tests;
154 }
155
156 #endif /* CONFIG_BT_IAS_CLIENT */
157