osu: Add board parameter setters 33/317633/7 accepted/tizen_unified_toolchain accepted/tizen/9.0/unified/20241030.235401 accepted/tizen/unified/20241006.053324 accepted/tizen/unified/toolchain/20241022.122650 accepted/tizen/unified/toolchain/20241022.123054 accepted/tizen/unified/x/20241007.013550 accepted/tizen/unified/x/asan/20241014.000507 tizen_9.0_m2_release
authorSangYoun Kwak <sy.kwak@samsung.com>
Thu, 12 Sep 2024 11:22:38 +0000 (20:22 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Wed, 2 Oct 2024 07:18:16 +0000 (16:18 +0900)
Setter for board parameters using hal apis are added.
 * hal_device_board_switch_partition
 * hal_device_board_switch_partition
 * hal_device_board_clear_partition_ab_cloned
 * hal_device_board_set_partition_ab_cloned
 * hal_device_board_set_partition_status
 * hal_device_board_set_upgrade_progress_status
 * hal_device_board_set_upgrade_type
 * hal_device_board_set_upgrade_state

Change-Id: I13d39b210349766d76fadee3679dff337c6b2689
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
tools/osu/CMakeLists.txt
tools/osu/board-params-getter.c
tools/osu/board-params-setter.c [new file with mode: 0644]
tools/osu/board-params-setter.h [new file with mode: 0644]
tools/osu/board-params-util.c [new file with mode: 0644]
tools/osu/board-params-util.h [new file with mode: 0644]
tools/osu/board-params.h
tools/osu/osu.c

index 6790f7a6cd305a7cb2f81ccc853b83f3b5e5810e..c5e805a90b07c4535a08ae173f3d140c5ca3e461 100644 (file)
@@ -10,12 +10,19 @@ INCLUDE(FindPkgConfig)
 pkg_check_modules(REQUIRED_PKGS REQUIRED ${PKG_MODULES})
 
 FOREACH(flag ${REQUIRED_PKGS_CFLAGS})
-        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
 INCLUDE_DIRECTORIES(. ${CMAKE_SOURCE_DIR}/include)
 
-FILE(GLOB_RECURSE SRCS osu.c resize.c update.c board-params-getter.c)
+FILE(GLOB_RECURSE SRCS
+       osu.c
+       resize.c
+       update.c
+       board-params-getter.c
+       board-params-setter.c
+       board-params-util.c
+)
 ADD_EXECUTABLE(${OUTPUT_BIN_NAME} ${SRCS})
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 TARGET_LINK_LIBRARIES(${OUTPUT_BIN_NAME} ${REQUIRED_PKGS_LDFLAGS} update-control)
index 4fb98f944f1090158859a23b4e642bc5eb7f8a0e..425099b401f80c4d8b1714743deb7b4f1362cb77 100644 (file)
@@ -6,6 +6,9 @@
 
 #include <hal/hal-device-board.h>
 
+#include "board-params-getter.h"
+#include "board-params-util.h"
+
 #define BOARD_PARAM_PARAMS_LEN 2
 
 struct board_param_getter {
@@ -175,26 +178,6 @@ static void print_board_param_getters(void)
                " * e.g. \"partition-status,a\"\n");
 }
 
-static size_t split_params(char **params, size_t params_len, const char *delim, char *params_str)
-{
-       assert(params_len > 0);
-
-       char *strtok_saveptr = NULL;
-       size_t params_index = 0;
-
-       params[0] = strtok_r(params_str, delim, &strtok_saveptr);
-       if (params[0] == NULL)
-               return 0;
-
-       for (params_index = 1; params_index < params_len; ++params_index) {
-               params[params_index] = strtok_r(NULL, delim, &strtok_saveptr);
-               if (params[params_index] == NULL)
-                       break;
-       }
-
-       return params_index;
-}
-
 int do_get_board_param(const char *params_str)
 {
        int ret = 0;
diff --git a/tools/osu/board-params-setter.c b/tools/osu/board-params-setter.c
new file mode 100644 (file)
index 0000000..1ea762b
--- /dev/null
@@ -0,0 +1,219 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+
+#include <hal/hal-device-board.h>
+
+#include "board-params-setter.h"
+#include "board-params-util.h"
+
+#define BOARD_PARAM_PARAMS_LEN 3
+
+struct board_param_setter {
+       char *param_name;
+       char *description;
+       int (*setter)(char **params, size_t params_len);
+};
+
+static int partition_ab_setter(char **params, size_t params_len)
+{
+       assert(params != NULL);
+
+       if (params_len != 1)
+               return -EINVAL;
+
+       if (strncmp(params[0], "toggle", sizeof("toggle")) == 0)
+               return hal_device_board_switch_partition('\0');
+
+       char partition_ab = params[0][0];
+
+       return hal_device_board_switch_partition(partition_ab);
+}
+
+static int partition_ab_cloned_setter(char **params, size_t params_len)
+{
+       assert(params != NULL);
+
+       if (params_len != 1)
+               return -EINVAL;
+
+       if (strncmp(params[0], "0", sizeof("0")) == 0)
+               return hal_device_board_clear_partition_ab_cloned();
+
+       if (strncmp(params[0], "1", sizeof("1")) == 0)
+               return hal_device_board_set_partition_ab_cloned();
+
+       return -EINVAL;
+}
+
+static int partition_status_setter(char **params, size_t params_len)
+{
+       assert(params != NULL);
+
+       if (params_len != 2)
+               return -EINVAL;
+
+       char partition_ab = params[0][0];
+       char *status = params[1];
+
+       return hal_device_board_set_partition_status(partition_ab, status);
+}
+
+static int upgrade_progress_status_setter(char **params, size_t params_len)
+{
+       assert(params != NULL);
+
+       if (params_len != 1)
+               return -EINVAL;
+
+       int upgrade_progress_status = 0;
+
+       if (parse_integer(params[0], &upgrade_progress_status) < 0)
+               return -EINVAL;
+
+       return hal_device_board_set_upgrade_progress_status(upgrade_progress_status);
+}
+
+static int upgrade_type_setter(char **params, size_t params_len)
+{
+       assert(params != NULL);
+
+       if (params_len != 1)
+               return -EINVAL;
+
+       char *upgrade_type = params[0];
+
+       return hal_device_board_set_upgrade_type(upgrade_type);
+}
+
+static int upgrade_state_setter(char **params, size_t params_len)
+{
+       assert(params != NULL);
+
+       if (params_len != 2)
+               return -EINVAL;
+
+       char *state_from = params[0];
+       char *state_to = params[1];
+
+       return  hal_device_board_set_upgrade_state(state_from, state_to);
+}
+
+static struct board_param_setter setters[] = {
+       {
+               .param_name = "partition-ab",
+               .description = "Switch partition between a/b. "
+                               "1 param is required: <a, b or toggle>",
+               .setter = partition_ab_setter
+       }, {
+               .param_name = "partition-ab-cloned",
+               .description = "Set cloned flag 0 or 1. "
+                               "1 param is required: <0 or 1>",
+               .setter = partition_ab_cloned_setter
+       }, {
+               .param_name = "partition-status",
+               .description = "Set partition status. "
+                               "2 params are required: <a, b>,<ok, failed or corrupted>",
+               .setter = partition_status_setter
+       }, {
+               .param_name = "upgrade-progress-status",
+               .description = "Set upgrade progress status. 1 integer is requied: <-1 ~ 100>",
+               .setter = upgrade_progress_status_setter
+       }, {
+               .param_name = "upgrade-type",
+               .description = "Set upgrade type. 1 param is required: <online or offline>",
+               .setter = upgrade_type_setter
+       }, {
+               .param_name = "upgrade-state",
+               .description = "Set upgrade state. 2 params are required.",
+               .setter = upgrade_state_setter
+       }, {
+               /* sentinel for this array */
+               .param_name = NULL,
+               .description = NULL,
+               .setter = NULL
+       },
+};
+
+static int get_board_param_setter(const char *param_name, struct board_param_setter **board_param_setter)
+{
+       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];
+                       return 0;
+               }
+       }
+
+       return -1;
+}
+
+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)
+                       continue;
+               printf("    %s: %s\n", setters[i].param_name, setters[i].description);
+       }
+
+       printf("\n"
+               " * Parameters should be provided separated with ','.\n"
+               " * e.g. \"upgrade_progress_status,100\"\n");
+}
+
+int do_set_board_param(const char *params_str)
+{
+       int ret = 0;
+       char *params_str_dup = NULL;
+       struct board_param_setter *board_param_setter = NULL;
+
+       char *params[BOARD_PARAM_PARAMS_LEN] = { NULL, };
+       size_t params_len = BOARD_PARAM_PARAMS_LEN;
+       char *param_name = NULL;
+
+       params_str_dup = strdup(params_str);
+       if (params_str_dup == NULL) {
+               printf("Failed to allocate memory for params.\n");
+               return 1;
+       }
+
+       params_len = split_params(params, params_len, ",", params_str_dup);
+       if (params_len < 1) {
+               printf("Invalid parameter.\n");
+               ret = -1;
+               goto release_and_exit;
+       }
+       param_name = params[0];
+
+       if (strncmp(param_name, "help", sizeof("help")) == 0) {
+               print_board_param_setters();
+               ret = 0;
+               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();
+               goto release_and_exit;
+       }
+
+       ret = (board_param_setter->setter)(params + 1, params_len - 1);
+       if (ret != 0) {
+               printf("Failed to set board param %s: %d\n", param_name, ret);
+               printf("%s: %s\n", param_name, board_param_setter->description);
+               goto release_and_exit;
+       }
+
+       printf("Success\n");
+
+release_and_exit:
+       free(params_str_dup);
+
+       return ret;
+}
diff --git a/tools/osu/board-params-setter.h b/tools/osu/board-params-setter.h
new file mode 100644 (file)
index 0000000..84cc3e2
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+int do_set_board_param(const char *param_name);
diff --git a/tools/osu/board-params-util.c b/tools/osu/board-params-util.c
new file mode 100644 (file)
index 0000000..edf1f32
--- /dev/null
@@ -0,0 +1,46 @@
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <limits.h>
+#include <errno.h>
+
+size_t split_params(char **params, size_t params_len, const char *delim, char *params_str)
+{
+       assert(params_len > 0);
+
+       char *strtok_saveptr = NULL;
+       size_t params_index = 0;
+
+       params[0] = strtok_r(params_str, delim, &strtok_saveptr);
+       if (params[0] == NULL)
+               return 0;
+
+       for (params_index = 1; params_index < params_len; ++params_index) {
+               params[params_index] = strtok_r(NULL, delim, &strtok_saveptr);
+               if (params[params_index] == NULL)
+                       break;
+       }
+
+       return params_index;
+}
+
+int parse_integer(const char *str, int *val)
+{
+       char *endptr = NULL;
+       long longval = 0;
+
+       errno = 0;
+       longval = strtol(str, &endptr, 10);
+
+       if (errno != 0 || endptr[0] != '\0') {
+               errno = 0;
+               return -EINVAL;
+       }
+
+       if (longval > INT_MAX || longval < INT_MIN)
+               return -EINVAL;
+
+       *val = (int)longval;
+
+       return 0;
+}
diff --git a/tools/osu/board-params-util.h b/tools/osu/board-params-util.h
new file mode 100644 (file)
index 0000000..1510d97
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+size_t split_params(char **params, size_t params_len, const char *delim, char *params_str);
+int parse_integer(const char *str, int *val);
index 9bd7d78deeb7c4b04cd807942e18719223ddff92..6a65c2cd202c86cd23a8910f762550eb96247478 100644 (file)
@@ -17,3 +17,4 @@
 #pragma once
 
 #include "board-params-getter.h"
+#include "board-params-setter.h"
index 6ff9ab5eb0af5fbb5cef408774262ee1139da7c1..73f32182b39cdc64fdfe08000b0e33c31b6dedda 100644 (file)
@@ -34,6 +34,7 @@ struct arguments {
        bool update;
        bool online;
        bool get_board_param;
+       bool set_board_param;
        char *board_param;
 };
 
@@ -49,6 +50,7 @@ static int parse_args(int argc, char **argv, struct arguments *args)
        args->update = false;
        args->online = false;
        args->get_board_param = false;
+       args->set_board_param = false;
        args->board_param = NULL;
 
        struct option long_options[] = {
@@ -57,6 +59,7 @@ static int parse_args(int argc, char **argv, struct arguments *args)
                {"offline-update", no_argument, NULL, 'f'},
                {"online-update", no_argument, NULL, 'n'},
                {"get-board-param", required_argument, NULL, 'g'},
+               {"set-board-param", required_argument, NULL, 's'},
                {"help", no_argument, NULL, 'h'},
                {}
        };
@@ -83,6 +86,11 @@ static int parse_args(int argc, char **argv, struct arguments *args)
                                args->board_param = optarg;
                                return 0;
                        }
+                       case 's': {
+                               args->set_board_param = true;
+                               args->board_param = optarg;
+                               return 0;
+                       }
                        case 'h': {
                                args->help = true;
                                return 0;
@@ -114,6 +122,8 @@ static void print_help(char *my_name)
                "                                               After that, performing the OS Upgrade will be impossible.\n"
                "  --get-board-param <param name>[,<param>...]  Print value of a board param\n"
                "                                               Use <param name> as 'help' for details.\n"
+               "  --set-board-param <param name>[,<param>...]  Set value of a board param\n"
+               "                                               Use <param name> as 'help' for details.\n"
                "\n" ,my_name);
 }
 
@@ -139,6 +149,9 @@ int main(int argc, char **argv)
        if (args.get_board_param)
                return do_get_board_param(args.board_param);
 
+       if (args.set_board_param)
+               return do_set_board_param(args.board_param);
+
        print_help(argv[0]);
 
        return 1;