osu: Move print function from getter,setter to osu 15/316915/9
authorUnsung Lee <unsung.lee@samsung.com>
Wed, 18 Dec 2024 11:19:57 +0000 (20:19 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Fri, 3 Jan 2025 06:00:47 +0000 (15:00 +0900)
Move print function of board parameters from board-params-getter and board-params-setter files to osu file.
Previously, it was responsible for the function of printing the board parameters
after the getting and setting them in board-params-getter/setter files.
In order to print the board parameter in osu file,
add get_param_name() and get_description() functions in board-params-getter/setter files
to get parameter name and description.
Additionally, in the process of moving the print function to the osu file,
the board-params.c file has been added for commonly used as getter/setter.

Change-Id: Idc66100b2b52af89ff92b8b43c0f3ce13c589e7d
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
tools/osu/CMakeLists.txt
tools/osu/board-params-getter.c
tools/osu/board-params-getter.h
tools/osu/board-params-setter.c
tools/osu/board-params-setter.h
tools/osu/board-params.c [new file with mode: 0644]
tools/osu/board-params.h
tools/osu/osu.c

index c5e805a90b07c4535a08ae173f3d140c5ca3e461..9d7803683ce2726257eecb9b6a69621db00eff58 100644 (file)
@@ -22,6 +22,7 @@ FILE(GLOB_RECURSE SRCS
        board-params-getter.c
        board-params-setter.c
        board-params-util.c
+       board-params.c
 )
 ADD_EXECUTABLE(${OUTPUT_BIN_NAME} ${SRCS})
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
index 6e587bf6cce4dad191f01bdbde87dcdfb211475a..78aaeafa067ead1c0dc1be85594815ba06dd2a1a 100644 (file)
@@ -180,26 +180,66 @@ static int get_board_param_getter(const char *param_name, struct board_param_get
        return -1;
 }
 
-static void print_board_param_getters(void)
+int board_params_getter_get_param_name(board_params_e board_params,
+               char *name, size_t name_size)
 {
-       printf("Available getter board params:\n");
-       for (int i = 0; g_getters[i].param_name != NULL; ++i) {
-               if (g_getters[i].getter == NULL)
-                       continue;
-               printf("    %s: %s\n", g_getters[i].param_name, g_getters[i].description);
+       size_t board_param_name_size;
+
+       if (!board_params_is_valid_board_params(board_params)) {
+               printf("Failed to verify board param: enum board_params (%d)\n", board_params);
+               return -EINVAL;
+       }
+
+       if (g_getters[board_params].param_name == NULL) {
+               printf("Board parameter name is null\n");
+               return -EINVAL;
        }
 
-       printf("\n"
-               " * Parameters should be provided separated with ','.\n"
-               " * e.g. \"partition-status,a\"\n");
+       board_param_name_size = strlen (g_getters[board_params].param_name) + 1;
+       if (name_size < board_param_name_size) {
+               printf("Buffer is not enough to copy param name: src(%lu), dst(%lu)\n",
+                               (unsigned long)board_param_name_size, (unsigned long)name_size);
+               return -EINVAL;
+       }
+
+       strncpy(name, g_getters[board_params].param_name, name_size - 1);
+
+       return 0;
 }
 
-int board_params_getter_get_board_param(const char *params_str)
+int board_params_getter_get_description(board_params_e board_params,
+               char *description, size_t description_size)
+{
+       size_t board_param_description_size;
+
+       if (!board_params_is_valid_board_params(board_params)) {
+               printf("Failed to verify board param: enum board_params (%d)\n", board_params);
+               return -EINVAL;
+       }
+
+       if (g_getters[board_params].description == NULL) {
+               printf("Board parameter description is null\n");
+               return -EINVAL;
+       }
+
+       board_param_description_size = strlen (g_getters[board_params].description) + 1;
+       if (description_size < board_param_description_size) {
+               printf("Buffer is not enough to copy param description: src(%lu), dst(%lu)\n",
+                               (unsigned long)board_param_description_size, (unsigned long)description_size);
+               return -EINVAL;
+       }
+
+       strncpy(description, g_getters[board_params].description, description_size - 1);
+
+       return 0;
+}
+
+int board_params_getter_get_board_param(const char *params_str,
+               char *result_buffer, size_t buffer_size)
 {
        int ret = 0;
        char *params_str_dup = NULL;
        struct board_param_getter *board_param_getter = NULL;
-       char result_buffer[1024] = { 0, };
 
        char *params[BOARD_PARAM_PARAMS_LEN] = { NULL, };
        size_t params_len = BOARD_PARAM_PARAMS_LEN;
@@ -220,28 +260,24 @@ int board_params_getter_get_board_param(const char *params_str)
        param_name = params[0];
 
        if (strncmp(param_name, "help", sizeof("help")) == 0) {
-               print_board_param_getters();
-               ret = 0;
+               ret = -EAGAIN;
                goto release_and_exit;
        }
 
        ret = get_board_param_getter(param_name, &board_param_getter);
        if (ret != 0) {
                printf("Invalid board param: %s\n", params_str);
-               print_board_param_getters();
+               ret = -EAGAIN;
                goto release_and_exit;
        }
 
-       ret = (board_param_getter->getter)(params + 1, params_len - 1, result_buffer, sizeof(result_buffer));
+       ret = (board_param_getter->getter)(params + 1, params_len - 1, result_buffer, buffer_size);
        if (ret != 0) {
                printf("Failed to get board param %s: %d\n", param_name, ret);
                printf("%s: %s\n", param_name, board_param_getter->description);
                goto release_and_exit;
        }
 
-       ret = 0;
-       printf("%s\n", result_buffer);
-
 release_and_exit:
        free(params_str_dup);
 
index a39821ab469815bd676596f801808012f96a0a65..f37487ebbddd2f97f8ebd029ed26c207ec445ce8 100644 (file)
 
 #pragma once
 
-int board_params_getter_get_board_param(const char *param_name);
+#include "board-params.h"
+
+int board_params_getter_get_param_name(board_params_e board_params,
+               char *name, size_t name_size);
+int board_params_getter_get_description(board_params_e board_params,
+               char *description, size_t description_size);
+int board_params_getter_get_board_param(const char *params_str,
+               char *result_buffer, size_t buffer_size);
index 517bc2d9072207702e395d1ca624f51cb42bacdc..e99e801492b36a97dfe5e7649ed9c6cba9f5ffdf 100644 (file)
@@ -172,18 +172,58 @@ static int get_board_param_setter(const char *param_name, struct board_param_set
        return -1;
 }
 
-static void print_board_param_setters(void)
+int board_params_setter_get_param_name(board_params_e board_params,
+               char *name, size_t name_size)
 {
-       printf("Available board params:\n");
-       for (int i = 0; g_setters[i].param_name != NULL; ++i) {
-               if (g_setters[i].setter == NULL)
-                       continue;
-               printf("    %s: %s\n", g_setters[i].param_name, g_setters[i].description);
+       size_t board_param_name_size;
+
+       if (!board_params_is_valid_board_params(board_params)) {
+               printf("Failed to verify board param: enum board_params (%d)\n", board_params);
+               return -EINVAL;
+       }
+
+       if (g_setters[board_params].param_name == NULL) {
+               printf("Board parameter name is null\n");
+               return -EINVAL;
+       }
+
+       board_param_name_size = strlen (g_setters[board_params].param_name) + 1;
+       if (name_size < board_param_name_size) {
+               printf("Buffer is not enough to copy param name: src(%lu), dst(%lu)\n",
+                               (unsigned long)board_param_name_size, (unsigned long)name_size);
+               return -EINVAL;
        }
 
-       printf("\n"
-               " * Parameters should be provided separated with ','.\n"
-               " * e.g. \"upgrade_progress_status,100\"\n");
+       strncpy(name, g_setters[board_params].param_name, name_size - 1);
+
+       return 0;
+}
+
+int board_params_setter_get_description(board_params_e board_params,
+               char *description, size_t description_size)
+{
+       size_t board_param_description_size;
+
+       if (!board_params_is_valid_board_params(board_params)) {
+               printf("Failed to verify board param: enum board_params (%d)\n", board_params);
+               return -EINVAL;
+       }
+
+       if (g_setters[board_params].description == NULL) {
+               printf("Board parameter description is null\n");
+               return -EINVAL;
+       }
+
+       board_param_description_size = strlen (g_setters[board_params].description) + 1;
+       if (description_size < board_param_description_size) {
+               printf("Buffer is not enough to copy param description: src(%lu), dst(%lu)\n",
+                               (unsigned long)board_param_description_size, (unsigned long)description_size);
+               return -EINVAL;
+       }
+
+       strncpy(description, g_setters[board_params].description, description_size - 1);
+
+       return 0;
 }
 
 int board_params_setter_set_board_param(const char *params_str)
@@ -211,15 +251,14 @@ int board_params_setter_set_board_param(const char *params_str)
        param_name = params[0];
 
        if (strncmp(param_name, "help", sizeof("help")) == 0) {
-               print_board_param_setters();
-               ret = 0;
+               ret = -EAGAIN;
                goto release_and_exit;
        }
 
        ret = get_board_param_setter(param_name, &board_param_setter);
        if (ret != 0) {
                printf("Invalid parameter(s): %s\n", param_name);
-               print_board_param_setters();
+               ret = -EAGAIN;
                goto release_and_exit;
        }
 
@@ -230,8 +269,6 @@ int board_params_setter_set_board_param(const char *params_str)
                goto release_and_exit;
        }
 
-       printf("Success\n");
-
 release_and_exit:
        free(params_str_dup);
 
index 1300c7860766335342ee1cbb8bad497fdafee8b2..e525584764fca121e5befa859a25e200f4884472 100644 (file)
 
 #pragma once
 
+#include "board-params.h"
+
+int board_params_setter_get_param_name(board_params_e board_params,
+               char *name, size_t name_size);
+int board_params_setter_get_description(board_params_e board_params,
+               char *description, size_t description_size);
 int board_params_setter_set_board_param(const char *param_name);
diff --git a/tools/osu/board-params.c b/tools/osu/board-params.c
new file mode 100644 (file)
index 0000000..a66a7ed
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *
+ * OSU is a tool to support Tizen OS Upgrade testing and development
+ *
+ */
+
+#include <stdbool.h>
+
+#include "board-params.h"
+
+bool board_params_is_valid_board_params(board_params_e board_params)
+{
+       switch (board_params) {
+       case BOARD_PARAMS_BOOT_MODE:
+       case BOARD_PARAMS_CURRENT_PARTITION:
+       case BOARD_PARAMS_PARTITION_AB:
+       case BOARD_PARAMS_PARTITION_AB_CLONED:
+       case BOARD_PARAMS_PARTITION_STATUS:
+       case BOARD_PARAMS_UPGRADE_PROGRESS_STATUS:
+       case BOARD_PARAMS_UPGRADE_STATE:
+       case BOARD_PARAMS_UPGRADE_TYPE:
+               return true;
+       default:
+               return false;
+       }
+}
index 5cf1e1b293474f040db0b7d276a85a1392be166a..d9aa51cd4f5698395798ab135cf87ebd299cb30c 100644 (file)
@@ -16,8 +16,7 @@
 
 #pragma once
 
-#include "board-params-getter.h"
-#include "board-params-setter.h"
+#include <stdbool.h>
 
 typedef enum {
        BOARD_PARAMS_BOOT_NONE = -1,
@@ -31,3 +30,5 @@ typedef enum {
        BOARD_PARAMS_UPGRADE_TYPE,
        BOARD_PARAMS_MAX,
 } board_params_e;
+
+bool board_params_is_valid_board_params(board_params_e board_params);
index 3ef80b867f5b9e94963e7632dc4ed329ec88480f..78c7b64d01d716ddb6d8b6fffe564b3a6a25aca2 100644 (file)
 #include <stdbool.h>
 #include <getopt.h>
 #include <assert.h>
+#include <string.h>
+#include <errno.h>
 
 #include "resize.h"
 #include "update.h"
 #include "board-params.h"
-
+#include "board-params-getter.h"
+#include "board-params-setter.h"
 
 struct arguments {
        bool help;
@@ -127,9 +130,97 @@ static void print_help(char *my_name)
                "\n" ,my_name);
 }
 
+static int print_board_param_getters(void)
+{
+       char param_name[1024] = { 0, };
+       char description[1024] = { 0, };
+       int ret;
+
+       printf("Available getter board params:\n");
+       for (board_params_e board_params = BOARD_PARAMS_BOOT_MODE;
+                       board_params < BOARD_PARAMS_MAX; board_params++) {
+               ret = board_params_getter_get_param_name(board_params,
+                               param_name, sizeof(param_name));
+               if (ret != 0)
+                       return ret;
+
+               if (strncmp(param_name, "none", strlen("none") + 1) == 0)
+                       continue;
+
+               ret = board_params_getter_get_description(board_params,
+                               description, sizeof(description));
+               if (ret != 0)
+                       return ret;
+
+               if (strncmp(description, "None", strlen("None") + 1) == 0)
+                       continue;
+
+               printf("    %s: %s\n", param_name, description);
+       }
+
+       printf("\n"
+               " * Parameters should be provided separated with ','.\n"
+               " * e.g. \"partition-status,a\"\n\n");
+
+       return 0;
+}
+
+static int print_board_param_setters(void)
+{
+       char param_name[1024] = { 0, };
+       char description[1024] = { 0, };
+       int ret;
+
+       printf("Available setter board params:\n");
+       for (board_params_e board_params = BOARD_PARAMS_BOOT_MODE;
+                       board_params < BOARD_PARAMS_MAX; board_params++) {
+               ret = board_params_setter_get_param_name(board_params,
+                               param_name, sizeof(param_name));
+               if (ret != 0)
+                       return ret;
+
+               if (strncmp(param_name, "none", strlen("none") + 1) == 0)
+                       continue;
+
+               ret = board_params_setter_get_description(board_params,
+                               description, sizeof(description));
+               if (ret != 0)
+                       return ret;
+
+               if (strncmp(description, "None", strlen("None") + 1) == 0)
+                       continue;
+
+               printf("    %s: %s\n", param_name, description);
+       }
+
+       printf("\n"
+               " * Parameters should be provided separated with ','.\n"
+               " * e.g. \"upgrade_progress_status,100\"\n\n");
+
+       return 0;
+}
+
+static int print_argument(struct arguments *args)
+{
+       if (args == NULL)
+               return -EINVAL;
+
+       if (args->get_board_param) {
+               print_board_param_getters();
+       }
+
+       if (args->set_board_param) {
+               print_board_param_setters();
+       }
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
+       char result_buffer[1024] = { 0, };
        struct arguments args;
+       int ret = 0;
 
        if (parse_args(argc, argv, &args) != 0) {
                return 1;
@@ -146,11 +237,32 @@ int main(int argc, char **argv)
        if (args.update)
                return update_do_update(args.online);
 
-       if (args.get_board_param)
-               return board_params_getter_get_board_param(args.board_param);
+       if (args.get_board_param) {
+               ret = board_params_getter_get_board_param(args.board_param,
+                               result_buffer, sizeof(result_buffer));
+               if (ret == -EAGAIN)
+                       return print_argument(&args);
 
-       if (args.set_board_param)
-               return board_params_setter_set_board_param(args.board_param);
+               if (ret != 0)
+                       return ret;
+
+               printf("%s\n", result_buffer);
+
+               return 0;
+       }
+
+       if (args.set_board_param) {
+               ret = board_params_setter_set_board_param(args.board_param);
+               if (ret == -EAGAIN)
+                       return print_argument(&args);
+
+               if (ret != 0)
+                       return ret;
+
+               printf("Success\n");
+
+               return 0;
+       }
 
        print_help(argv[0]);