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