Skip to content

Commit

Permalink
port verbose option over to ebg_set_opt
Browse files Browse the repository at this point in the history
This patch deprecated the ebg_beverbose function and ports the logic
over to the ebg_set_opt_bool infrastructure. By that, the interface can
be simplified.

Signed-off-by: Felix Moessbauer <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
fmoessbauer authored and jan-kiszka committed Oct 18, 2023
1 parent ffbd35f commit 8df7c6e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
11 changes: 9 additions & 2 deletions env/env_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ int ebg_set_opt_bool(ebg_opt_t opt, bool value)
case EBG_OPT_PROBE_ALL_DEVICES:
ebgenv_opts.search_all_devices = value;
break;
case EBG_OPT_VERBOSE:
ebgenv_opts.verbose = value;
bgenv_be_verbose(value);
break;
default:
return EINVAL;
}
Expand All @@ -78,15 +82,18 @@ int ebg_get_opt_bool(ebg_opt_t opt, bool *value)
case EBG_OPT_PROBE_ALL_DEVICES:
*value = ebgenv_opts.search_all_devices;
break;
case EBG_OPT_VERBOSE:
*value = ebgenv_opts.verbose;
break;
default:
return EINVAL;
}
return 0;
}

void ebg_beverbose(ebgenv_t __attribute__((unused)) *e, bool v)
void ebg_beverbose(ebgenv_t __attribute__((unused)) * e, bool v)
{
bgenv_be_verbose(v);
ebg_set_opt_bool(EBG_OPT_VERBOSE, v);
}

int ebg_env_create_new(ebgenv_t *e)
Expand Down
2 changes: 0 additions & 2 deletions env/env_api_fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "test-interface.h"
#include "ebgpart.h"

bool bgenv_verbosity = false;
extern ebgenv_opts_t ebgenv_opts;

EBGENVKEY bgenv_str2enum(char *key)
Expand Down Expand Up @@ -48,7 +47,6 @@ EBGENVKEY bgenv_str2enum(char *key)

void bgenv_be_verbose(bool v)
{
bgenv_verbosity = v;
ebgpart_beverbose(v);
}

Expand Down
6 changes: 4 additions & 2 deletions include/ebgenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

typedef struct {
bool search_all_devices;
bool verbose;
} ebgenv_opts_t;

typedef struct {
Expand All @@ -46,7 +47,7 @@ typedef struct {
ebgenv_opts_t opts;
} ebgenv_t;

typedef enum { EBG_OPT_PROBE_ALL_DEVICES } ebg_opt_t;
typedef enum { EBG_OPT_PROBE_ALL_DEVICES, EBG_OPT_VERBOSE } ebg_opt_t;

/**
* @brief Set a global EBG option. Call before creating the ebg env.
Expand All @@ -67,8 +68,9 @@ int ebg_get_opt_bool(ebg_opt_t opt, bool *value);
/** @brief Tell the library to output information for the user.
* @param e A pointer to an ebgenv_t context.
* @param v A boolean to set verbosity.
* @note deprecated. Use \c ebg_set_opt_bool(EBG_OPT_VERBOSE,true) instead
*/
void ebg_beverbose(ebgenv_t *e, bool v);
void __attribute__((deprecated)) ebg_beverbose(ebgenv_t *e, bool v);

/** @brief Initialize environment library and open environment. The first
* time this function is called, it will create a new environment with
Expand Down
7 changes: 3 additions & 4 deletions include/env_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@

#define DEFAULT_TIMEOUT_SEC 30

extern bool bgenv_verbosity;
extern ebgenv_opts_t ebgenv_opts;

#define VERBOSE(o, ...) \
if (bgenv_verbosity) \
fprintf(o, __VA_ARGS__)
#define VERBOSE(o, ...) \
if (ebgenv_opts.verbose) fprintf(o, __VA_ARGS__)

typedef enum {
EBGENV_KERNELFILE,
Expand Down
3 changes: 3 additions & 0 deletions tools/bg_printenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ error_t bg_printenv(int argc, char **argv)
if (arguments.common.search_all_devices) {
ebg_set_opt_bool(EBG_OPT_PROBE_ALL_DEVICES, true);
}
if (arguments.common.verbosity) {
ebg_set_opt_bool(EBG_OPT_VERBOSE, true);
}
if (!bgenv_init()) {
fprintf(stderr, "Error initializing FAT environment.\n");
return 1;
Expand Down
3 changes: 3 additions & 0 deletions tools/bg_setenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ error_t bg_setenv(int argc, char **argv)
if (arguments.common.search_all_devices) {
ebg_set_opt_bool(EBG_OPT_PROBE_ALL_DEVICES, true);
}
if (arguments.common.verbosity) {
ebg_set_opt_bool(EBG_OPT_VERBOSE, true);
}
if (!bgenv_init()) {
fprintf(stderr, "Error initializing FAT environment.\n");
return 1;
Expand Down
19 changes: 19 additions & 0 deletions tools/tests/test_ebgenv_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ int __wrap_bgenv_set(BGENV *env, char *key, uint64_t type, void *buffer,
CONFIG_PART config_parts[ENV_NUM_CONFIG_PARTS];
BG_ENVDATA envdata[ENV_NUM_CONFIG_PARTS];

START_TEST(ebgenv_api_ebg_env_options)
{
int status;
status = ebg_set_opt_bool(EBG_OPT_VERBOSE, true);
ck_assert_int_eq(status, 0);
bool verbose;
status = ebg_get_opt_bool(EBG_OPT_VERBOSE, &verbose);
ck_assert_int_eq(status, 0);
ck_assert_int_eq(verbose, 1);

// try invalid option
status = ebg_set_opt_bool(0xffff, false);
ck_assert_int_ne(status, 0);
status = ebg_get_opt_bool(0xffff, &verbose);
ck_assert_int_ne(status, 0);
}
END_TEST

START_TEST(ebgenv_api_ebg_env_create_new)
{
ebgenv_t e;
Expand Down Expand Up @@ -665,6 +683,7 @@ Suite *ebg_test_suite(void)

tc_core = tcase_create("Core");

tcase_add_test(tc_core, ebgenv_api_ebg_env_options);
tcase_add_test(tc_core, ebgenv_api_ebg_env_create_new);
tcase_add_test(tc_core, ebgenv_api_ebg_env_open_current);
tcase_add_test(tc_core, ebgenv_api_ebg_env_get);
Expand Down

0 comments on commit 8df7c6e

Please sign in to comment.