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 "bs_pc_backchannel.h"
9 #include "argparse.h"
10 
11 #define BS_SECONDS(dur_sec)    ((bs_time_t)dur_sec * 1000000)
12 #define TEST_TIMEOUT_SIMULATED BS_SECONDS(60)
13 #define CHANNEL_ID	       0
14 #define MSG_SIZE	       1
15 
test_tick(bs_time_t HW_device_time)16 void test_tick(bs_time_t HW_device_time)
17 {
18 	bs_trace_debug_time(0, "Simulation ends now.\n");
19 	if (bst_result != Passed) {
20 		bst_result = Failed;
21 		bs_trace_error("Test did not pass before simulation ended.\n");
22 	}
23 }
24 
test_init(void)25 void test_init(void)
26 {
27 	bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED);
28 	bst_result = In_progress;
29 }
30 
backchannel_init(uint peer)31 void backchannel_init(uint peer)
32 {
33 	uint device_number = get_device_nbr();
34 	uint device_numbers[] = {peer};
35 	uint channel_numbers[] = {CHANNEL_ID};
36 	uint *ch;
37 
38 	ch = bs_open_back_channel(device_number, device_numbers, channel_numbers,
39 				  ARRAY_SIZE(channel_numbers));
40 	if (!ch) {
41 		FAIL("Unable to open backchannel\n");
42 	}
43 }
44 
backchannel_sync_send(void)45 void backchannel_sync_send(void)
46 {
47 	uint8_t sync_msg[MSG_SIZE] = {get_device_nbr()};
48 
49 	bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
50 }
51 
backchannel_sync_wait(void)52 void backchannel_sync_wait(void)
53 {
54 	uint8_t sync_msg[MSG_SIZE];
55 
56 	while (true) {
57 		if (bs_bc_is_msg_received(CHANNEL_ID) > 0) {
58 			bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg));
59 			if (sync_msg[0] != get_device_nbr()) {
60 				/* Received a message from another device, exit */
61 				break;
62 			}
63 		}
64 
65 		k_sleep(K_MSEC(1));
66 	}
67 }
68 
print_address(bt_addr_le_t * addr)69 void print_address(bt_addr_le_t *addr)
70 {
71 	char array[BT_ADDR_LE_STR_LEN];
72 
73 	bt_addr_le_to_str(addr, array, sizeof(array));
74 	printk("Address : %s\n", array);
75 }
76