1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "bs_bt_utils.h"
8 #include "argparse.h"
9 #include "bs_pc_backchannel.h"
10 
11 #define BS_SECONDS(dur_sec)    ((bs_time_t)dur_sec * 1000000)
12 #define TEST_TIMEOUT_SIMULATED BS_SECONDS(70)
13 
test_tick(bs_time_t HW_device_time)14 void test_tick(bs_time_t HW_device_time)
15 {
16 	bs_trace_debug_time(0, "Simulation ends now.\n");
17 	if (bst_result != Passed) {
18 		bst_result = Failed;
19 
20 		bs_trace_error("Test did not pass before simulation ended.\n");
21 	}
22 }
23 
test_init(void)24 void test_init(void)
25 {
26 	bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED);
27 	bst_result = In_progress;
28 }
29 
30 #define CHANNEL_ID 0
31 #define MSG_SIZE 1
32 
backchannel_init(uint peer)33 void backchannel_init(uint peer)
34 {
35 	uint device_number = get_device_nbr();
36 	uint device_numbers[] = { peer };
37 	uint channel_numbers[] = { CHANNEL_ID };
38 	uint *ch;
39 
40 	ch = bs_open_back_channel(device_number, device_numbers,
41 				  channel_numbers, ARRAY_SIZE(channel_numbers));
42 	if (!ch) {
43 		FAIL("Unable to open backchannel\n");
44 	}
45 }
46 
backchannel_sync_send(void)47 void backchannel_sync_send(void)
48 {
49 	uint8_t sync_msg[MSG_SIZE] = { get_device_nbr() };
50 
51 	printk("Sending sync\n");
52 	bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
53 }
54 
backchannel_sync_wait(void)55 void backchannel_sync_wait(void)
56 {
57 	uint8_t sync_msg[MSG_SIZE];
58 
59 	while (true) {
60 		if (bs_bc_is_msg_received(CHANNEL_ID) > 0) {
61 			bs_bc_receive_msg(CHANNEL_ID, sync_msg,
62 					  ARRAY_SIZE(sync_msg));
63 			if (sync_msg[0] != get_device_nbr()) {
64 				/* Received a message from another device, exit */
65 				break;
66 			}
67 		}
68 
69 		k_sleep(K_MSEC(1));
70 	}
71 
72 	printk("Sync received\n");
73 }
74