From c57fd340545a844a1a140385481545adfc200d2b Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Wed, 18 Dec 2024 17:19:36 +0900 Subject: [PATCH] osu: Make index of getters and setters using board_params_e Make index of getters and setters using enumeration called board_params_e. It allows specific getter and setter functions to be referenced directly through the board_params_e enum. The index of board_params is like below: { BOARD_PARAMS_BOOT_NONE = -1, BOARD_PARAMS_BOOT_MODE = 0, BOARD_PARAMS_CURRENT_PARTITION, BOARD_PARAMS_PARTITION_AB, BOARD_PARAMS_PARTITION_AB_CLONED, BOARD_PARAMS_PARTITION_STATUS, BOARD_PARAMS_UPGRADE_PROGRESS_STATUS, BOARD_PARAMS_UPGRADE_STATE, BOARD_PARAMS_UPGRADE_TYPE, BOARD_PARAMS_MAX, } BOARD_PARAMS_BOOT_NONE: index before real board param BOARD_PARAMS_UPGRADE_TYPE ~ BOARD_PARAMS_UPGRADE_TYPE: index of real board param BOARD_PARAMS_MAX: index after real board param Change-Id: I4fc1ecc285cec8fa1205d7a1c31b6a78f98e108d Signed-off-by: Unsung Lee --- tools/osu/board-params-getter.c | 46 ++++++++++++++++++++----------- tools/osu/board-params-setter.c | 48 +++++++++++++++++++++++---------- tools/osu/board-params.h | 13 +++++++++ 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/tools/osu/board-params-getter.c b/tools/osu/board-params-getter.c index 929aea9..6e587bf 100644 --- a/tools/osu/board-params-getter.c +++ b/tools/osu/board-params-getter.c @@ -8,6 +8,7 @@ #include "board-params-getter.h" #include "board-params-util.h" +#include "board-params.h" #define BOARD_PARAM_PARAMS_LEN 2 @@ -113,36 +114,48 @@ static int upgrade_type_getter(char **params, size_t params_len, char *buffer, s return hal_device_board_get_upgrade_type(buffer, buffer_size); } -static struct board_param_getter getters[] = { - { +static struct board_param_getter g_getters[] = { + [BOARD_PARAMS_BOOT_MODE] = { .param_name = "boot-mode", .description = "Get boot mode.", .getter = boot_mode_getter - }, { + }, + [BOARD_PARAMS_CURRENT_PARTITION] = { .param_name = "current-partition", .description = "Get current partition (a or b)", .getter = current_partition_getter - }, { + }, + [BOARD_PARAMS_PARTITION_AB] = { + .param_name = "none", + .description = "None", + .getter = NULL + }, + [BOARD_PARAMS_PARTITION_AB_CLONED] = { .param_name = "partition-ab-cloned", .description = "Get partition is cloned or not: 1 is cloned, 0 is not cloned.", .getter = partition_ab_cloned_getter - }, { + }, + [BOARD_PARAMS_PARTITION_STATUS] = { .param_name = "partition-status", .description = "Get partition status. 1 param is required: 'a' or 'b'.", .getter = partition_status_getter - }, { + }, + [BOARD_PARAMS_UPGRADE_PROGRESS_STATUS] = { .param_name = "upgrade-progress-status", .description = "Get current upgrade progress status: -1 ~ 100", .getter = upgrade_progress_status_getter - }, { + }, + [BOARD_PARAMS_UPGRADE_STATE] = { .param_name = "upgrade-state", .description = "Get current upgrade state.", .getter = upgrade_state_getter - }, { + }, + [BOARD_PARAMS_UPGRADE_TYPE] = { .param_name = "upgrade-type", .description = "Get current upgrade type: offline or online", .getter = upgrade_type_getter - }, { + }, + [BOARD_PARAMS_MAX] = { /* sentinel for this array */ .param_name = NULL, .description = NULL, @@ -154,9 +167,12 @@ static int get_board_param_getter(const char *param_name, struct board_param_get { size_t param_name_len = strlen(param_name); - for (int i = 0; getters[i].param_name != NULL; ++i) { - if (strncmp(getters[i].param_name, param_name, param_name_len + 1) == 0) { - *board_param_getter = &getters[i]; + for (int i = 0; g_getters[i].param_name != NULL; ++i) { + if (g_getters[i].getter == NULL) + continue; + + if (strncmp(g_getters[i].param_name, param_name, param_name_len + 1) == 0) { + *board_param_getter = &g_getters[i]; return 0; } } @@ -167,10 +183,10 @@ static int get_board_param_getter(const char *param_name, struct board_param_get static void print_board_param_getters(void) { printf("Available getter board params:\n"); - for (int i = 0; getters[i].param_name != NULL; ++i) { - if (getters[i].getter == NULL) + for (int i = 0; g_getters[i].param_name != NULL; ++i) { + if (g_getters[i].getter == NULL) continue; - printf(" %s: %s\n", getters[i].param_name, getters[i].description); + printf(" %s: %s\n", g_getters[i].param_name, g_getters[i].description); } printf("\n" diff --git a/tools/osu/board-params-setter.c b/tools/osu/board-params-setter.c index 3bd2b5c..517bc2d 100644 --- a/tools/osu/board-params-setter.c +++ b/tools/osu/board-params-setter.c @@ -9,6 +9,7 @@ #include "board-params-setter.h" #include "board-params-util.h" +#include "board-params.h" #define BOARD_PARAM_PARAMS_LEN 3 @@ -102,35 +103,51 @@ static int upgrade_state_setter(char **params, size_t params_len) return hal_device_board_set_upgrade_state(state_from, state_to); } -static struct board_param_setter setters[] = { - { +static struct board_param_setter g_setters[] = { + [BOARD_PARAMS_BOOT_MODE] = { + .param_name = "none", + .description = "None", + .setter = NULL + }, + [BOARD_PARAMS_CURRENT_PARTITION] = { + .param_name = "none", + .description = "None", + .setter = NULL + }, + [BOARD_PARAMS_PARTITION_AB] = { .param_name = "partition-ab", .description = "Switch partition between a/b. " "1 param is required: ", .setter = partition_ab_setter - }, { + }, + [BOARD_PARAMS_PARTITION_AB_CLONED] = { .param_name = "partition-ab-cloned", .description = "Set cloned flag 0 or 1. " "1 param is required: <0 or 1>", .setter = partition_ab_cloned_setter - }, { + }, + [BOARD_PARAMS_PARTITION_STATUS] = { .param_name = "partition-status", .description = "Set partition status. " "2 params are required: ,", .setter = partition_status_setter - }, { + }, + [BOARD_PARAMS_UPGRADE_PROGRESS_STATUS] = { .param_name = "upgrade-progress-status", .description = "Set upgrade progress status. 1 integer is requied: <-1 ~ 100>", .setter = upgrade_progress_status_setter - }, { + }, + [BOARD_PARAMS_UPGRADE_TYPE] = { .param_name = "upgrade-type", .description = "Set upgrade type. 1 param is required: ", .setter = upgrade_type_setter - }, { + }, + [BOARD_PARAMS_UPGRADE_STATE] = { .param_name = "upgrade-state", .description = "Set upgrade state. 2 params are required.", .setter = upgrade_state_setter - }, { + }, + [BOARD_PARAMS_MAX] = { /* sentinel for this array */ .param_name = NULL, .description = NULL, @@ -142,9 +159,12 @@ static int get_board_param_setter(const char *param_name, struct board_param_set { size_t param_name_len = strlen(param_name); - for (int i = 0; setters[i].param_name != NULL; ++i) { - if (strncmp(setters[i].param_name, param_name, param_name_len + 1) == 0) { - *board_param_setter = &setters[i]; + for (int i = 0; g_setters[i].param_name != NULL; ++i) { + if (g_setters[i].setter == NULL) + continue; + + if (strncmp(g_setters[i].param_name, param_name, param_name_len + 1) == 0) { + *board_param_setter = &g_setters[i]; return 0; } } @@ -155,10 +175,10 @@ static int get_board_param_setter(const char *param_name, struct board_param_set static void print_board_param_setters(void) { printf("Available board params:\n"); - for (int i = 0; setters[i].param_name != NULL; ++i) { - if (setters[i].setter == NULL) + for (int i = 0; g_setters[i].param_name != NULL; ++i) { + if (g_setters[i].setter == NULL) continue; - printf(" %s: %s\n", setters[i].param_name, setters[i].description); + printf(" %s: %s\n", g_setters[i].param_name, g_setters[i].description); } printf("\n" diff --git a/tools/osu/board-params.h b/tools/osu/board-params.h index 6a65c2c..5cf1e1b 100644 --- a/tools/osu/board-params.h +++ b/tools/osu/board-params.h @@ -18,3 +18,16 @@ #include "board-params-getter.h" #include "board-params-setter.h" + +typedef enum { + BOARD_PARAMS_BOOT_NONE = -1, + BOARD_PARAMS_BOOT_MODE = 0, + BOARD_PARAMS_CURRENT_PARTITION, + BOARD_PARAMS_PARTITION_AB, + BOARD_PARAMS_PARTITION_AB_CLONED, + BOARD_PARAMS_PARTITION_STATUS, + BOARD_PARAMS_UPGRADE_PROGRESS_STATUS, + BOARD_PARAMS_UPGRADE_STATE, + BOARD_PARAMS_UPGRADE_TYPE, + BOARD_PARAMS_MAX, +} board_params_e; -- 2.34.1