From 08f69c7822bde9c7c3e10d8c19eeb280a4cdc4e8 Mon Sep 17 00:00:00 2001 From: Jon Szymaniak Date: Thu, 20 Aug 2015 16:30:51 -0400 Subject: [PATCH] bladeRF-cli: Cleaned up 'print' formatting Various items supported by 'print' have been made a bit more consistent in terms of spacing. Units have been separated from numbers, as typically recommended by NIST. Some options have been added to the printset_table to specify how to group items when printing all of them (and to skip items). --- host/utilities/bladeRF-cli/src/cmd/printset.c | 174 +++++++++++------- 1 file changed, 106 insertions(+), 68 deletions(-) diff --git a/host/utilities/bladeRF-cli/src/cmd/printset.c b/host/utilities/bladeRF-cli/src/cmd/printset.c index 07362865..f00877ab 100644 --- a/host/utilities/bladeRF-cli/src/cmd/printset.c +++ b/host/utilities/bladeRF-cli/src/cmd/printset.c @@ -25,18 +25,42 @@ #include "cmd.h" #include "conversions.h" -#define PRINTSET_DECL(x) int print_##x(struct cli_state *, int, char **); int set_##x(struct cli_state *, int, char **); -#define PRINTSET_ENTRY(x) { \ +#define PRINTSET_DECL(x) \ + int print_##x(struct cli_state *, int, char **); int set_##x(struct cli_state *, int, char **); + +#define PRINTSET_ENTRY(x, printall_option) { \ FIELD_INIT(.print, print_##x), \ FIELD_INIT(.set, set_##x), \ - FIELD_INIT(.name, #x) \ + FIELD_INIT(.name, #x), \ + FIELD_INIT(.pa_option, printall_option), \ } +/** + * Any special actions we should take when printing all values when invoking + * the 'print' command with no arguments. + */ +enum printall_option +{ + PRINTALL_OPTION_NONE, /**< No special actions */ + PRINTALL_OPTION_SKIP, /**< Skip this entry */ + PRINTALL_OPTION_APPEND_NEWLINE, /**< Print an extra newline after entry */ +}; + /* An entry in the printset table */ struct printset_entry { - int (*print)(struct cli_state *s, int argc, char **argv); /*<< Function pointer to thing that prints parameter */ - int (*set)(struct cli_state *s, int argc, char **argv); /*<< Function pointer to setter */ - char *name; /*<< String associated with parameter */ + /** Function pointer to thing that prints parameter */ + int (*print)(struct cli_state *s, int argc, char **argv); + + /** Function pointer to setter */ + int (*set)(struct cli_state *s, int argc, char **argv); + + /** String associated with name */ + const char *name; + + /** + * Additional options when printing this entry as part of the + * 'print command' with no options (i.e., print everything) */ + enum printall_option pa_option; }; /* Declarations */ @@ -58,21 +82,21 @@ PRINTSET_DECL(xb_gpio_dir) /* print/set parameter table */ struct printset_entry printset_table[] = { - PRINTSET_ENTRY(bandwidth), - PRINTSET_ENTRY(frequency), - PRINTSET_ENTRY(gpio), - PRINTSET_ENTRY(loopback), - PRINTSET_ENTRY(lnagain), - PRINTSET_ENTRY(rxvga1), - PRINTSET_ENTRY(rxvga2), - PRINTSET_ENTRY(txvga1), - PRINTSET_ENTRY(txvga2), - PRINTSET_ENTRY(sampling), - PRINTSET_ENTRY(samplerate), - PRINTSET_ENTRY(trimdac), - PRINTSET_ENTRY(xb_spi), - PRINTSET_ENTRY(xb_gpio), - PRINTSET_ENTRY(xb_gpio_dir), + PRINTSET_ENTRY(bandwidth, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(frequency, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(gpio, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(loopback, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(lnagain, PRINTALL_OPTION_NONE), + PRINTSET_ENTRY(rxvga1, PRINTALL_OPTION_NONE), + PRINTSET_ENTRY(rxvga2, PRINTALL_OPTION_NONE), + PRINTSET_ENTRY(txvga1, PRINTALL_OPTION_NONE), + PRINTSET_ENTRY(txvga2, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(sampling, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(samplerate, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(trimdac, PRINTALL_OPTION_APPEND_NEWLINE), + PRINTSET_ENTRY(xb_spi, PRINTALL_OPTION_SKIP), + PRINTSET_ENTRY(xb_gpio, PRINTALL_OPTION_NONE), + PRINTSET_ENTRY(xb_gpio_dir, PRINTALL_OPTION_NONE), /* End of table marked by entry with NULL/empty fields */ { FIELD_INIT(.print, NULL), FIELD_INIT(.set, NULL), FIELD_INIT(.name, "") } @@ -129,7 +153,7 @@ int print_bandwidth(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " RX Bandwidth: %9uHz\n", bw ); + printf( " RX Bandwidth: %9u Hz\n", bw ); } } @@ -139,7 +163,7 @@ int print_bandwidth(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " TX Bandwidth: %9uHz\n", bw ); + printf( " TX Bandwidth: %9u Hz\n", bw ); } } @@ -211,7 +235,7 @@ int set_bandwidth(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " Set RX bandwidth - req:%9uHz actual:%9uHz\n", + printf( " Set RX bandwidth - req:%9u Hz actual:%9u Hz\n", bw, actual ); } } @@ -225,7 +249,7 @@ int set_bandwidth(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " Set TX bandwidth - req:%9uHz actual:%9uHz\n", + printf( " Set TX bandwidth - req:%9u Hz actual:%9u Hz\n", bw, actual ); } } @@ -263,10 +287,7 @@ int print_frequency(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " RX Frequency: %10uHz\n", freq ); - if( argc == 3 ) { - printf( "\n" ); - } + printf( " RX Frequency: %10u Hz\n", freq ); } } @@ -276,10 +297,7 @@ int print_frequency(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - if( argc == 3 ) { - printf( "\n" ); - } - printf( " TX Frequency: %10uHz\n", freq ); + printf( " TX Frequency: %10u Hz\n", freq ); } } } @@ -477,7 +495,7 @@ int print_xb_gpio_dir(struct cli_state *state, int argc, char **argv) { state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " Expansion GPIO direction (1=output): 0x%8.8x\n", val ); + printf( " Expansion GPIO direction: 0x%8.8x (1=output, 0=input)\n", val ); } return rv; } @@ -516,19 +534,19 @@ int print_lnagain(struct cli_state *state, int argc, char **argv) } else { switch (gain) { case BLADERF_LNA_GAIN_MAX: - printf("\n LNA Gain: 6 dB\n\n"); + printf(" RXLNA Gain: 6 dB\n"); break; case BLADERF_LNA_GAIN_MID: - printf("\n LNA Gain: 3 dB\n\n"); + printf(" RXLNA Gain: 3 dB\n"); break; case BLADERF_LNA_GAIN_BYPASS: - printf("\n LNA Gain: 0 dB\n\n"); + printf(" RXLNA Gain: 0 dB\n"); break; default: - printf("\n LNA Gain: Unknown (%d)\n\n", gain); + printf(" RXLNA Gain: Unknown (%d)\n", gain); break; } @@ -583,39 +601,39 @@ int print_loopback(struct cli_state *state, int argc, char **argv) switch (loopback) { case BLADERF_LB_BB_TXLPF_RXVGA2: - printf("\n Loopback mode: bb_txlpf_rxvga2\n"); + printf(" Loopback mode: bb_txlpf_rxvga2\n"); break; case BLADERF_LB_BB_TXLPF_RXLPF: - printf("\n Loopback mode: bb_txlpf_rxlpf\n"); + printf(" Loopback mode: bb_txlpf_rxlpf\n"); break; case BLADERF_LB_BB_TXVGA1_RXVGA2: - printf("\n Loopback mode: bb_txvga1_rxvga2\n"); + printf(" Loopback mode: bb_txvga1_rxvga2\n"); break; case BLADERF_LB_BB_TXVGA1_RXLPF: - printf("\n Loopback mode: bb_txvga1_rxlpf\n"); + printf(" Loopback mode: bb_txvga1_rxlpf\n"); break; case BLADERF_LB_RF_LNA1: - printf("\n Loopback mode: rf_lna1\n"); + printf(" Loopback mode: rf_lna1\n"); break; case BLADERF_LB_RF_LNA2: - printf("\n Loopback mode: rf_lna2\n"); + printf(" Loopback mode: rf_lna2\n"); break; case BLADERF_LB_RF_LNA3: - printf("\n Loopback mode: rf_lna3\n"); + printf(" Loopback mode: rf_lna3\n"); break; case BLADERF_LB_FIRMWARE: - printf("\n Loopback mode: firmware\n"); + printf(" Loopback mode: firmware\n"); break; case BLADERF_LB_NONE: - printf("\n Loopback mode: none\n"); + printf(" Loopback mode: none\n"); break; default: @@ -694,7 +712,7 @@ int print_rxvga1(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " RXVGA1 Gain: %3ddB\n", gain ); + printf( " RXVGA1 Gain: %3d dB\n", gain ); } return rv; @@ -732,7 +750,7 @@ int print_rxvga2(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " RXVGA2 Gain: %3ddB\n", gain ); + printf( " RXVGA2 Gain: %3d dB\n", gain ); } return rv; @@ -777,7 +795,7 @@ int print_samplerate(struct cli_state *state, int argc, char **argv) status = CLI_RET_INVPARAM; } else { status = bladerf_get_rational_sample_rate(state->dev, module, &rate); - printf("\n %s sample rate: %"PRIu64" %"PRIu64"/%"PRIu64"\n\n", + printf(" %s sample rate: %"PRIu64" %"PRIu64"/%"PRIu64"\n", module == BLADERF_MODULE_RX ? "RX" : "TX", rate.integer, rate.num, rate.den); } @@ -791,10 +809,10 @@ int print_samplerate(struct cli_state *state, int argc, char **argv) BLADERF_MODULE_TX, &tx_rate); if (!status) { - printf("\n RX sample rate: %"PRIu64" %"PRIu64"/%"PRIu64"\n", + printf(" RX sample rate: %"PRIu64" %"PRIu64"/%"PRIu64"\n", rate.integer, rate.num, rate.den); - printf(" TX sample rate: %"PRIu64" %"PRIu64"/%"PRIu64"\n\n", + printf(" TX sample rate: %"PRIu64" %"PRIu64"/%"PRIu64"\n", tx_rate.integer, tx_rate.num, tx_rate.den); } else { state->last_lib_error = status; @@ -998,15 +1016,15 @@ int print_sampling( struct cli_state *state, int argc, char **argv) } else { switch (mode) { case BLADERF_SAMPLING_EXTERNAL: - printf("\n Sampling: External\n"); + printf(" Sampling: External\n"); break; case BLADERF_SAMPLING_INTERNAL: - printf("\n Sampling: Internal\n"); + printf(" Sampling: Internal\n"); break; default: - printf("\n Sampling: Unknown\n"); + printf(" Sampling: Unknown\n"); } } } @@ -1031,8 +1049,8 @@ int print_trimdac(struct cli_state *state, int argc, char **argv) return CLI_RET_LIBBLADERF; } - printf(" Current VCTCXO trim DAC value: 0x%04x\n", curr); - printf(" Calibration data in flash: 0x%04x\n", cal); + printf(" Current VCTCXO trim: 0x%04x\n", curr); + printf(" Stored VCTCXO trim: 0x%04x\n", cal); return 0; } @@ -1063,7 +1081,7 @@ int set_trimdac(struct cli_state *state, int argc, char **argv) int print_xb_spi(struct cli_state *state, int argc, char **argv) { - /* All of the SPI devices so far are write-only */ + cli_err_nnl(state, "Error", "XB SPI is currently write-only.\n"); return CLI_RET_OK; } @@ -1101,7 +1119,7 @@ int print_txvga1(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " TXVGA1 Gain: %ddB\n", gain ) ; + printf( " TXVGA1 Gain: %3d dB\n", gain ) ; } return rv; @@ -1145,7 +1163,7 @@ int print_txvga2(struct cli_state *state, int argc, char **argv) state->last_lib_error = status; rv = CLI_RET_LIBBLADERF; } else { - printf( " TXVGA2 Gain: %ddB\n", gain ); + printf( " TXVGA2 Gain: %3d dB\n", gain ); } return rv; } @@ -1216,7 +1234,6 @@ int cmd_set(struct cli_state *state, int argc, char **argv) int cmd_print(struct cli_state *state, int argc, char **argv) { int rv = CLI_RET_OK; - int i = 0; struct printset_entry *entry = NULL; char *empty_argv[2]; @@ -1231,22 +1248,43 @@ int cmd_print(struct cli_state *state, int argc, char **argv) rv = entry->print(state, argc, argv); } else { /* Incorrect parameter to print */ - cli_err(state, argv[0], "Invalid parameter (%s)\n", argv[1]); + cli_err_nnl(state, argv[0], "Invalid parameter (%s)\n", argv[1]); rv = CLI_RET_INVPARAM; } } else { /* We fake an argumentless argv with each command's name supplied as argv[1]. */ - empty_argv[0] = argv[0]; - for( i = 0; printset_table[i].print != NULL; i++) { - empty_argv[1] = (char *) printset_table[i].name; - rv = printset_table[i].print(state, 2, (char **)empty_argv); - if (rv != CLI_RET_OK) { - break; + entry = &printset_table[0]; + + while (entry->print != NULL) { + if (entry->pa_option != PRINTALL_OPTION_SKIP) { + empty_argv[1] = (char *) entry->name; + + rv = entry->print(state, 2, (char **)empty_argv); + if (rv != CLI_RET_OK) { + if (cli_fatal(rv)) { + return rv; + } + + /* Print error messages here so that we can continue + * onto the remaining items */ + cli_err_nnl(state, empty_argv[1], "%s\n", + cli_strerror(rv, state->last_lib_error)); + + /* We've reported this, don't pass it back up + * the call stack */ + rv = 0; + } + + if (entry->pa_option == PRINTALL_OPTION_APPEND_NEWLINE) { + printf("\n"); + } } + + entry++; } }