1 /*
2  * Copyright (c) 2022 Codecoup
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <stddef.h>
9 
10 #include <zephyr/autoconf.h>
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/bluetooth/conn.h>
14 #include <zephyr/bluetooth/uuid.h>
15 #include <zephyr/bluetooth/gatt.h>
16 #include <zephyr/bluetooth/services/ias.h>
17 #include <zephyr/sys/printk.h>
18 #include <zephyr/types.h>
19 
20 #include "bstests.h"
21 #include "common.h"
22 
23 #ifdef CONFIG_BT_IAS
24 extern enum bst_result_t bst_result;
25 
26 CREATE_FLAG(g_high_alert_received);
27 CREATE_FLAG(g_mild_alert_received);
28 CREATE_FLAG(g_stop_alert_received);
29 
high_alert_cb(void)30 static void high_alert_cb(void)
31 {
32 	SET_FLAG(g_high_alert_received);
33 }
34 
mild_alert_cb(void)35 static void mild_alert_cb(void)
36 {
37 	SET_FLAG(g_mild_alert_received);
38 }
39 
no_alert_cb(void)40 static void no_alert_cb(void)
41 {
42 	SET_FLAG(g_stop_alert_received);
43 }
44 
45 BT_IAS_CB_DEFINE(ias_callbacks) = {
46 	.high_alert = high_alert_cb,
47 	.mild_alert = mild_alert_cb,
48 	.no_alert = no_alert_cb,
49 };
50 
test_main(void)51 static void test_main(void)
52 {
53 	int err;
54 
55 	err = bt_enable(NULL);
56 	if (err != 0) {
57 		FAIL("Bluetooth init failed (err %d)\n", err);
58 		return;
59 	}
60 
61 	err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, ad, AD_SIZE, NULL, 0);
62 	if (err) {
63 		FAIL("Advertising failed to start (err %d)\n", err);
64 		return;
65 	}
66 
67 	printk("Advertising successfully started\n");
68 
69 	WAIT_FOR_FLAG(flag_connected);
70 
71 	WAIT_FOR_FLAG(g_high_alert_received);
72 	printk("High alert received\n");
73 
74 	err = bt_ias_local_alert_stop();
75 	if (err != 0) {
76 		FAIL("Failed to locally stop alert: %d\n", err);
77 		return;
78 	}
79 	WAIT_FOR_FLAG(g_stop_alert_received);
80 
81 	WAIT_FOR_FLAG(g_mild_alert_received);
82 	printk("Mild alert received\n");
83 
84 	WAIT_FOR_FLAG(g_stop_alert_received);
85 	printk("Stop alert received\n");
86 
87 	PASS("IAS test passed\n");
88 }
89 
90 static const struct bst_test_instance test_ias[] = {
91 	{
92 		.test_id = "ias",
93 		.test_pre_init_f = test_init,
94 		.test_tick_f = test_tick,
95 		.test_main_f = test_main,
96 
97 	},
98 	BSTEST_END_MARKER
99 };
100 
test_ias_install(struct bst_test_list * tests)101 struct bst_test_list *test_ias_install(struct bst_test_list *tests)
102 {
103 	return bst_add_tests(tests, test_ias);
104 }
105 
106 #endif /* CONFIG_BT_IAS */
107