1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <signal.h>
9 #include "bs_tracing.h"
10 #include "bs_oswrap.h"
11 #include "bs_cmd_line.h"
12 #include "bs_cmd_line_typical.h"
13 #include "bs_pc_base.h"
14 
15 typedef bs_basic_dev_args_t empty_args_t;
16 
17 char executable_name[] = "bs_device_empty";
component_print_post_help()18 void component_print_post_help(){
19   fprintf(stdout,"\n"
20           "This device just connects to a phy and disconnects, effectively\n"
21           "using up one of its interfaces\n\n");
22 }
23 
24 empty_args_t args;
25 
cmd_trace_lvl_found(char * argv,int offset)26 void cmd_trace_lvl_found(char * argv, int offset){
27   bs_trace_set_level(args.verb);
28 }
29 
cmd_gdev_nbr_found(char * argv,int offset)30 void cmd_gdev_nbr_found(char * argv, int offset){
31   bs_trace_set_prefix_dev(args.global_device_nbr);
32 }
33 
34 /**
35  * Check the arguments provided in the command line: set args based on it or
36  * defaults, and check they are correct
37  */
bs_empty_argparse(int argc,char * argv[],empty_args_t * args)38 void bs_empty_argparse(int argc, char *argv[], empty_args_t *args)
39 {
40   bs_args_struct_t args_struct[] = {
41       BS_BASIC_DEVICE_2G4_FAKE_OPTIONS_ARG_STRUCT,
42       ARG_TABLE_ENDMARKER
43   };
44 
45   bs_args_typical_dev_set_defaults((bs_basic_dev_args_t *)args, args_struct);
46   static char default_phy[] ="2G4";
47 
48   bs_args_parse_cmd_line(argc, argv, args_struct);
49 
50   bs_args_typical_dev_post_check((bs_basic_dev_args_t *)args, args_struct, default_phy);
51 }
52 
53 static pb_dev_state_t pcom_dev_state;
54 
clean_up()55 static uint8_t clean_up() {
56   bs_trace_raw(8,"Cleaning up\n");
57   pb_dev_clean_up(&pcom_dev_state);
58   return 0;
59 }
60 
61 /**
62  * Handler for SIGTERM and SIGINT
63  * We do not need to do anything in this empty device
64  * The signal will cause any blocking libPhyCom op to return an error
65  */
signal_end_handler(int sig)66 static void signal_end_handler(int sig)
67 {
68   bs_trace_raw(2,"Signal \"%s\" received\n", strsignal(sig));
69 }
70 
71 /**
72  * This device just connects to a phy and disconnects, effectively
73  * using up one of the phy interfaces
74  */
main(int argc,char * argv[])75 int main(int argc, char *argv[]) {
76   bs_trace_register_cleanup_function(clean_up);
77   bs_set_sig_term_handler(signal_end_handler, (int[]){SIGTERM, SIGINT}, 2);
78 
79   bs_empty_argparse(argc, argv, &args);
80 
81   pb_dev_init_com(&pcom_dev_state, args.device_nbr, args.s_id, args.p_id);
82   pb_dev_disconnect(&pcom_dev_state);
83   return 0;
84 }
85