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