1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdio.h>
7 #include "modem_magic_args.h"
8 #include "bs_tracing.h"
9 #include "bs_oswrap.h"
10 #include "bs_rand_main.h"
11 #include "bs_cmd_line_typical.h"
12 
13 static char library_name[] = "Modem Magic";
14 
component_print_post_help()15 void component_print_post_help(){
16   fprintf(stdout,"This modem will receive anything no mattter the SNR\n"
17       "(you can force a given BER or sync PER with -BER=<BER> -PER=<PER>\n");
18 }
19 
20 static mo_magic_args_t *args_g;
21 static double ber, per;
22 static uint device_nbr_g;
23 
cmd_ber_found(char * argv,int offset)24 static void cmd_ber_found(char * argv, int offset) {
25   if ((ber < 0) || (ber > 1)) {
26     bs_trace_error("modem: %i: (magic) args: BER shall be between 0 and 1 '%s'\n", device_nbr_g, ber, argv);
27   }
28   args_g->BER = ber*(double)RAND_PROB_1;
29   bs_trace_raw(9,"modem: %i: (magic) BER set to %e (%u/%u)\n", device_nbr_g, ber, args_g->BER, RAND_PROB_1);
30 }
31 
cmd_per_found(char * argv,int offset)32 static void cmd_per_found(char * argv, int offset) {
33   if ((per < 0) || (per > 1)) {
34     bs_trace_error("modem: %i: (magic) args: PER shall be between 0 and 1 '%s'\n", device_nbr_g, per, argv);
35   }
36   args_g->sync_prob = (1.0-per)*(double)RAND_PROB_1;
37   bs_trace_raw(9,"modem: %i: (magic) sync probability (per) set to %u/%u (%e)\n", device_nbr_g, args_g->sync_prob, RAND_PROB_1, per);
38 }
39 
40 /**
41  * Check the arguments provided in the command line: set args based on it
42  * or defaults, and check they are correct
43  */
modem_magic_argparse(int argc,char * argv[],uint dev_nbr,mo_magic_args_t * args)44 void modem_magic_argparse(int argc, char *argv[], uint dev_nbr, mo_magic_args_t *args)
45 {
46   args_g = args;
47   device_nbr_g = dev_nbr;
48   bs_args_struct_t args_struct[] = {
49     /*manual,mandatory,switch,option,name , type, destination,  callback,       description */
50     { false , false  , false, "BER", "ber", 'f', (void*)&ber, cmd_ber_found, "bit error rate (for any type of packet) (0)..1"},
51     { false , false  , false, "PER", "per", 'f', (void*)&per, cmd_per_found, "synchronization error rate (for any type of packet), (0)..1"},
52     ARG_TABLE_ENDMARKER
53   };
54 
55   args->sync_prob = RAND_PROB_1;
56   args->BER = 0;
57 
58   char trace_prefix[50]; //it will not be used as soon as we get out of this function
59   snprintf(trace_prefix,50, "modem: %i: (magic) ",dev_nbr);
60 
61   bs_args_override_exe_name(library_name);
62   bs_args_set_trace_prefix(trace_prefix);
63   bs_args_parse_all_cmd_line(argc, argv, args_struct);
64 }
65