1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "bs_tracing.h"
7 #include "bs_pc_base.h"
8 #include "bs_pause_args.h"
9 
10 /**
11  * This device pauses the simulation once for each element of the
12  * list provided from command line, while it awaits some user input
13  */
14 
15 static pb_dev_state_t pb_dev_state = {0};
16 
clean_up()17 static uint8_t clean_up() {
18   bs_trace_raw(8, "Cleaning up\n");
19   pb_dev_disconnect(&pb_dev_state);
20   return 0;
21 }
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[]) {
24   pause_args_t args;
25 
26   bs_trace_register_cleanup_function(clean_up);
27   bs_pause_argparse(argc, argv, &args);
28 
29   bs_trace_raw(9,"Connecting...\n");
30   pb_dev_init_com(&pb_dev_state, args.device_nbr, args.s_id, args.p_id);
31 
32   bs_time_t now = 0;
33   pb_wait_t wait_s;
34 
35   for (int i = 0 ; i < args.n_times ; i++) {
36     if ( args.times[i] < now ) {
37       bs_trace_error_line("The list of times needs to be ordered (index %i: %i < %i)\n",i, args.times[i], now);
38     }
39     wait_s.end = now = args.times[i];
40     if ( pb_dev_request_wait_block(&pb_dev_state, &wait_s) == -1 ){
41       bs_trace_raw(3,"We have been told to disconnect\n");
42       break;
43     } else {
44       char ret;
45       bs_trace_raw(2,"@%"PRItime" Press \"ENTER\" to continue...\n", now);
46       fflush(stdout);
47       ret = getc(stdin);
48       if ( ret == EOF ) {
49         bs_trace_warning_line("break in stdin. Are you running me in a script without feeding something to it?\n");
50         break;
51       }
52     }
53   }
54 
55   bs_trace_raw(9,"Disconnecting...\n");
56   pb_dev_disconnect(&pb_dev_state);
57 
58   return 0;
59 }
60