1 /* Copyright (c) 2023 Nordic Semiconductor ASA
2 * SPDX-License-Identifier: Apache-2.0
3 */
4
5 #include <stdint.h>
6 #include <argparse.h>
7 #include <posix_native_task.h>
8 #include <bs_pc_backchannel.h>
9
10 #include <zephyr/logging/log.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <bs_tracing.h>
13 #include <zephyr/kernel.h>
14 LOG_MODULE_REGISTER(bs_sync, LOG_LEVEL_INF);
15
16 static int n_devs;
17
register_more_cmd_args(void)18 static void register_more_cmd_args(void)
19 {
20 static bs_args_struct_t args_struct_toadd[] = {
21 {
22 .option = "D",
23 .name = "number_devices",
24 .type = 'i',
25 .dest = (void *)&n_devs,
26 .descript = "Number of devices which will connect in this phy",
27 .is_mandatory = true,
28 },
29 ARG_TABLE_ENDMARKER,
30 };
31
32 bs_add_extra_dynargs(args_struct_toadd);
33 }
34 NATIVE_TASK(register_more_cmd_args, PRE_BOOT_1, 100);
35
36 static uint *backchannels;
setup_backchannels(void)37 static void setup_backchannels(void)
38 {
39 __ASSERT_NO_MSG(n_devs > 0);
40 uint self = get_device_nbr();
41 uint device_nbrs[n_devs];
42 uint channel_numbers[n_devs];
43
44 for (int i = 0; i < n_devs; i++) {
45 device_nbrs[i] = i;
46 channel_numbers[i] = 0;
47 }
48
49 backchannels =
50 bs_open_back_channel(self, device_nbrs, channel_numbers, ARRAY_SIZE(device_nbrs));
51 __ASSERT_NO_MSG(backchannels != NULL);
52 }
53 NATIVE_TASK(setup_backchannels, PRE_BOOT_3, 100);
54
bs_bc_receive_msg_sync(uint ch,size_t size,uint8_t * data)55 void bs_bc_receive_msg_sync(uint ch, size_t size, uint8_t *data)
56 {
57 while (bs_bc_is_msg_received(ch) < size) {
58 k_msleep(1);
59 }
60 bs_bc_receive_msg(ch, data, size);
61 }
62
bs_bc_send_uint(uint ch,uint64_t data)63 void bs_bc_send_uint(uint ch, uint64_t data)
64 {
65 uint8_t data_bytes[sizeof(data)];
66
67 sys_put_le64(data, data_bytes);
68 bs_bc_send_msg(ch, data_bytes, sizeof(data_bytes));
69 }
70
bs_bc_recv_uint(uint ch)71 uint64_t bs_bc_recv_uint(uint ch)
72 {
73 uint8_t data[sizeof(uint64_t)];
74
75 bs_bc_receive_msg_sync(ch, sizeof(data), data);
76 return sys_get_le64(data);
77 }
78
bt_testlib_bs_sync_all(void)79 void bt_testlib_bs_sync_all(void)
80 {
81 static uint64_t counter;
82
83 LOG_DBG("%llu d%u enter", counter, get_device_nbr());
84
85 /* Device 0 acts as hub. */
86 if (get_device_nbr() == 0) {
87 for (int i = 1; i < n_devs; i++) {
88 uint64_t counter_cfm;
89
90 counter_cfm = bs_bc_recv_uint(backchannels[i]);
91 __ASSERT(counter_cfm == counter, "%luu %luu", counter_cfm, counter);
92 }
93 for (int i = 1; i < n_devs; i++) {
94 bs_bc_send_uint(backchannels[i], counter);
95 }
96 } else {
97 uint64_t counter_cfm;
98
99 bs_bc_send_uint(backchannels[0], counter);
100 counter_cfm = bs_bc_recv_uint(backchannels[0]);
101 __ASSERT(counter_cfm == counter, "%luu %luu", counter_cfm, counter);
102 }
103
104 LOG_DBG("%llu d%u exit", counter, get_device_nbr());
105
106 counter++;
107 }
108