From 57ed9ab77bd7dc81da2c43ef910eae3b369a3942 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 24 Jul 2023 20:25:11 +0900 Subject: [PATCH 01/16] Replace prefix 'libsys' with 'syscommon' Change-Id: Ibe92ea978eb95b6a2f470dae7195b2840dd7c2d5 Signed-off-by: Youngjae Cho --- include/libsyscommon/common.h | 10 +++++----- include/libsyscommon/file.h | 2 +- include/libsyscommon/ini-parser.h | 2 +- src/libcommon/common.c | 6 +++--- src/libcommon/file.c | 2 +- src/libcommon/ini-parser.c | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/libsyscommon/common.h b/include/libsyscommon/common.h index 8144929..7fd6f66 100644 --- a/include/libsyscommon/common.h +++ b/include/libsyscommon/common.h @@ -20,8 +20,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#ifndef __LIBCOMMON_COMMON_H__ -#define __LIBCOMMON_COMMON_H__ +#ifndef __SYSCOMMON_COMMON_H__ +#define __SYSCOMMON_COMMON_H__ #include @@ -34,17 +34,17 @@ extern "C" { * * @return true if running on emulator, otherwise false even on operation failure */ -bool libsys_is_emulator(void); +bool syscommon_is_emulator(void); /** * @brief Check if running on container * * @return true if running on container, otherwise false even on operation failure */ -bool libsys_is_container(void); +bool syscommon_is_container(void); #ifdef __cplusplus } #endif -#endif /* __LIBCOMMON_COMMON_H__ */ +#endif /* __SYSCOMMON_COMMON_H__ */ diff --git a/include/libsyscommon/file.h b/include/libsyscommon/file.h index c933fc9..5c038e6 100644 --- a/include/libsyscommon/file.h +++ b/include/libsyscommon/file.h @@ -99,7 +99,7 @@ int sys_set_str(char *fname, char *val); * * @return zero if cannot read else the number of matched and assigned items */ -int libsys_parse_cmdline_scanf(const char *format, ...); +int syscommon_parse_cmdline_scanf(const char *format, ...); #ifdef __cplusplus } diff --git a/include/libsyscommon/ini-parser.h b/include/libsyscommon/ini-parser.h index 9620103..ce9e8ad 100644 --- a/include/libsyscommon/ini-parser.h +++ b/include/libsyscommon/ini-parser.h @@ -69,7 +69,7 @@ int config_parse(const char *file_name, int cb(struct parse_result *result, * @param[in] user_data user data is passed to cb. * @return 0 on success, negative if failed */ -int libsys_config_parse_by_section(const char *file_name, int cb(const struct parse_result *, void *), void *user_data); +int syscommon_config_parse_by_section(const char *file_name, int cb(const struct parse_result *, void *), void *user_data); #ifdef __cplusplus } diff --git a/src/libcommon/common.c b/src/libcommon/common.c index 051f8bb..2e96f29 100644 --- a/src/libcommon/common.c +++ b/src/libcommon/common.c @@ -32,7 +32,7 @@ #define FEATURE_MODEL_NAME_EMULATOR "Emulator" #define CONTAINER_FILE_PATH "/run/systemd/container" -bool libsys_is_emulator(void) +bool syscommon_is_emulator(void) { int ret = 0; char *model_name = NULL; @@ -44,7 +44,7 @@ bool libsys_is_emulator(void) ret = system_info_get_platform_string(FEATURE_MODEL_NAME, &model_name); if (ret < 0) { - _E("Cannot get model name: %d, libsys_is_emulator() returns false on operation failure", ret); + _E("Cannot get model name: %d, syscommon_is_emulator() returns false on operation failure", ret); return false; } @@ -57,7 +57,7 @@ bool libsys_is_emulator(void) return is_emul; } -bool libsys_is_container(void) +bool syscommon_is_container(void) { static bool is_container = false; static bool is_cached = false; diff --git a/src/libcommon/file.c b/src/libcommon/file.c index c2f579d..6c6cbaf 100644 --- a/src/libcommon/file.c +++ b/src/libcommon/file.c @@ -138,7 +138,7 @@ int sys_set_str(char *fname, char *val) return 0; } -int libsys_parse_cmdline_scanf(const char *format, ...) +int syscommon_parse_cmdline_scanf(const char *format, ...) { FILE *fp = NULL; char *token = NULL; diff --git a/src/libcommon/ini-parser.c b/src/libcommon/ini-parser.c index 19355d2..0a4960a 100644 --- a/src/libcommon/ini-parser.c +++ b/src/libcommon/ini-parser.c @@ -157,7 +157,7 @@ static void free_data(gpointer data) free(data); } -int libsys_config_parse_by_section(const char *fname, int cb(const struct parse_result *, void *), void *user_data) +int syscommon_config_parse_by_section(const char *fname, int cb(const struct parse_result *, void *), void *user_data) { FILE *fp = NULL; char *line = NULL; -- 2.7.4 From 78b2e902742b129dc1bc580a75fc89febde186a4 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 24 Jul 2023 20:46:52 +0900 Subject: [PATCH 02/16] libcommon: Add syscommon_is_mounted() syscmmon_is_mounted() checks whether the given path exists in /etc/mtab. Change-Id: I994c4190a069330403e5eb516eb6fef7f5d5404f Signed-off-by: Youngjae Cho --- include/libsyscommon/common.h | 7 +++++++ src/libcommon/common.c | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/libsyscommon/common.h b/include/libsyscommon/common.h index 7fd6f66..f6cd108 100644 --- a/include/libsyscommon/common.h +++ b/include/libsyscommon/common.h @@ -43,6 +43,13 @@ bool syscommon_is_emulator(void); */ bool syscommon_is_container(void); +/** + * @brief Check if the path is mounted + * + * @return true if the path is mounted, otherwise return false + */ +bool syscommon_is_mounted(const char *path); + #ifdef __cplusplus } #endif diff --git a/src/libcommon/common.c b/src/libcommon/common.c index 2e96f29..0a220c7 100644 --- a/src/libcommon/common.c +++ b/src/libcommon/common.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "shared/log.h" #include "libsyscommon/common.h" @@ -72,3 +73,29 @@ bool syscommon_is_container(void) return is_container; } + +bool syscommon_is_mounted(const char *path) +{ + bool ret = false; + struct mntent *mnt; + const char *table = "/etc/mtab"; + FILE *fp; + int len; + + fp = setmntent(table, "r"); + if (!fp) + return ret; + + len = strlen(path) + 1; + while (1) { + mnt = getmntent(fp); + if (mnt == NULL) + break; + if (!strncmp(mnt->mnt_dir, path, len)) { + ret = true; + break; + } + } + endmntent(fp); + return ret; +} -- 2.7.4 From 62a63fe1f111ebbd2b0f9b51b0e9955a5bcb31a0 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 25 Jul 2023 20:21:10 +0900 Subject: [PATCH 03/16] libcommon: Introduce notifier The libsyscommon notifer provides mean that communicate between different modules. And it works based on publish-subscribe pattern. If someone wants to listen to an event, it subscribes to notify id that is defined by event publisher, using subscribe function such as - syscommon_notifier_subscribe_notify_priority() - syscommon_notifier_subscribe_notify() - syscommon_notifier_subscribe_notify_udata_priority() - syscommon_notifier_subscribe_notify_udata() Obviously, it is possible to unsubscribe notify via - syscommon_notifier_unsubscribe_notify() - syscommon_notifier_unsubscribe_notify_udata() If someone want publish a notify, it can be done by the functions such as - syscommon_notifier_emit_notify() - syscommon_notifier_emit_notify_once() See below for a detailed description about libsyscommon notifier. int syscommon_notifier_subscribe_notify_priority() - Subscribe notify without user_data callback. - Higher number of priority will be notified first. int syscommon_notifier_subscribe_notify() - Subscribe notify without user_data callback - Automatically assign default priority of 0. int syscommon_notifier_unsubscribe_notify() - Unsubscribe notify callback without user_data. int syscommon_notifier_subscribe_notify_udata_priority() - Subscribe notify with user_data callback. - Provide destroy callback to free user_data on unsubscribing. - Higher number of priority will be notified first. - Return notify id and can be used for unsubscribing. int syscommon_notifier_unsubscribe_notify_udata() - Subscribe notify with user_data callback - Provide destroy callback to free user_data on unsubscribing - Automatically assign default priority of 0. - Return notify id and can be used for unsubscribing. int syscommon_notifier_subscribe_notify_udata() - Unsubscribe notify callback with user_data. - Invoke destroy function with user_data. void syscommon_notifier_emit_notify() - Emit notify. void syscommon_notifier_emit_notify_once() - Emit notify only once. - Notification after the first one is ignored. Change-Id: I1853f5a51e999c1f20b34c9281becb9cfbdb8a8b Signed-off-by: Youngjae Cho Change-Id: Iff113c8869e27a90f11156181b500d25893387a8 --- CMakeLists.txt | 1 + include/libsyscommon/notifier.h | 77 +++++++++++++ src/libcommon/notifier.c | 215 ++++++++++++++++++++++++++++++++++++ tests/libcommon/test-common.c | 236 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 529 insertions(+) create mode 100644 include/libsyscommon/notifier.h create mode 100644 src/libcommon/notifier.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 9094e74..f708755 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ SET(libsyscommon_SRCS src/libcommon/common.c src/libcommon/bitmap.c src/libcommon/proc.c + src/libcommon/notifier.c src/resource-manager/resource-manager.c src/resource-manager/resource-device.c src/resource-manager/resource-listener-epoll.c diff --git a/include/libsyscommon/notifier.h b/include/libsyscommon/notifier.h new file mode 100644 index 0000000..f88d22f --- /dev/null +++ b/include/libsyscommon/notifier.h @@ -0,0 +1,77 @@ +/* MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ + +#ifndef __SYSCOMMON_NOTIFIER_H__ +#define __SYSCOMMON_NOTIFIER_H__ + +typedef int (*syscommon_notifier_cb) (void *notify_data); +typedef int (*syscommon_notifier_udata_cb) (void *notify_data, void *user_data); +typedef void (*syscommon_notifier_destroy_cb) (void *user_data); + +/** + * Subscribe notify without user_data callback. + * Higher number of priority will be notified first. + */ +int syscommon_notifier_subscribe_notify_priority(int type, syscommon_notifier_cb func, int priority); + +/* Subscribe notify without user_data callback, default priority 0 */ +static inline int syscommon_notifier_subscribe_notify(int type, syscommon_notifier_cb func) +{ + return syscommon_notifier_subscribe_notify_priority(type, func, 0); +} + +/* Unsubscribe notify callback without user_data */ +int syscommon_notifier_unsubscribe_notify(int type, syscommon_notifier_cb func); + +/** + * Subscribe notify with user_data callback, destroy function, and priority. + * Higher number of priority will be notified first. + * Return notify id and can it be used for unsubscribing. + */ +int syscommon_notifier_subscribe_notify_udata_priority(int type, + syscommon_notifier_udata_cb func_udata, void *user_data, + syscommon_notifier_destroy_cb func_destroy_udata, int priority); + +/** + * Subscribe notify with user_data callback, and default priority 0. + * Return notify id and it can be used for unsubscribing. + */ +static inline int syscommon_notifier_subscribe_notify_udata(int type, + syscommon_notifier_udata_cb func_udata, void *user_data, syscommon_notifier_destroy_cb func_destroy) +{ + return syscommon_notifier_subscribe_notify_udata_priority(type, func_udata, user_data, func_destroy, 0); +} + +/** + * Unsubscribe notify callback with user_data. + * Invoke destroy function with user_data. + */ +int syscommon_notifier_unsubscribe_notify_udata(int id); + +/* Emit notify. */ +void syscommon_notifier_emit_notify(int type, void *notify_data); + +/* Emit notify only once. + * Notification after the first one is ignored. */ +void syscommon_notifier_emit_notify_once(int type, void *notify_data); + +#endif /* __SYSCOMMON_NOTIFIER_H__ */ diff --git a/src/libcommon/notifier.c b/src/libcommon/notifier.c new file mode 100644 index 0000000..073bfd3 --- /dev/null +++ b/src/libcommon/notifier.c @@ -0,0 +1,215 @@ +/* MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ + +#include +#include +#include +#include + +#include "libsyscommon/notifier.h" +#include "libsyscommon/list.h" +#include "shared/log.h" + +struct syscommon_notifier { + int id; + int priority; /* descending order */ + bool deleted; + + int type; + int (*func)(void *data); + + int (*func_udata)(void *data, void *user_data); + void (*destroyer)(void *user_data); + void *user_data; +}; + +static GList *syscommon_notifier_list; +static guint idl; + +#define FIND_NOTIFIER(a, b, d, e, f) \ + SYS_G_LIST_FOREACH(a, b, d) \ + if (e == d->e && f == (d->f) && !d->deleted) + +static gint compare_priority(gconstpointer a, gconstpointer b) +{ + /* descending order */ + return ((const struct syscommon_notifier *)b)->priority - ((const struct syscommon_notifier *)a)->priority + 1; +} + +int syscommon_notifier_subscribe_notify_priority(int type, syscommon_notifier_cb func, int priority) +{ + GList *n; + struct syscommon_notifier *notifier; + + _I("notifier type=%d, func=%p", type, func); + + if (!func) { + _E("Invalid func address."); + return -EINVAL; + } + + FIND_NOTIFIER(syscommon_notifier_list, n, notifier, type, func) { + _E("Function has already been registered, [%d, %p]", type, func); + return -EINVAL; + } + + notifier = calloc(1, sizeof(struct syscommon_notifier)); + if (!notifier) { + _E("Fail to malloc for %d notifier.", type); + return -ENOMEM; + } + + notifier->type = type; + notifier->priority = priority; + notifier->func = func; + + syscommon_notifier_list = g_list_insert_sorted(syscommon_notifier_list, notifier, compare_priority); + + return 0; +} + + +int syscommon_notifier_subscribe_notify_udata_priority(int type, + syscommon_notifier_udata_cb func_udata, void *user_data, + syscommon_notifier_destroy_cb func_destroy_udata, int priority) +{ + struct syscommon_notifier *notifier; + static int id = 1; + + _I("notifier type=%d, func=%p", type, func_udata); + + if (!func_udata) { + _E("Invalid func address."); + return -EINVAL; + } + + notifier = calloc(1, sizeof(struct syscommon_notifier)); + if (!notifier) { + _E("Fail to malloc for %d notifier.", type); + return -ENOMEM; + } + + notifier->id = id; + notifier->priority = priority; + notifier->type = type; + notifier->func_udata = func_udata; + notifier->user_data = user_data; + notifier->destroyer = func_destroy_udata; + + syscommon_notifier_list = g_list_insert_sorted(syscommon_notifier_list, notifier, compare_priority); + + ++id; + + return notifier->id; +} + +int syscommon_notifier_unsubscribe_notify(int type, syscommon_notifier_cb func) +{ + GList *n; + struct syscommon_notifier *notifier; + + if (!func) { + _E("Invalid func address."); + return -EINVAL; + } + + FIND_NOTIFIER(syscommon_notifier_list, n, notifier, type, func) { + _I("notifier type=%d, func=%p", type, func); + notifier->deleted = true; + } + + return 0; +} + +int syscommon_notifier_unsubscribe_notify_udata(int id) +{ + GList *n; + struct syscommon_notifier *notifier; + + SYS_G_LIST_FOREACH(syscommon_notifier_list, n, notifier) { + if (notifier->id == id) { + if (notifier->destroyer) + notifier->destroyer(notifier->user_data); + notifier->deleted = true; + } + } + + return 0; +} + +static gboolean delete_unused_notifier_cb(void *data) +{ + GList *n; + GList *next; + struct syscommon_notifier *notifier; + + SYS_G_LIST_FOREACH_SAFE(syscommon_notifier_list, n, next, notifier) { + if (notifier->deleted) { + SYS_G_LIST_REMOVE_LIST(syscommon_notifier_list, n); + free(notifier); + } + } + + idl = 0; + return G_SOURCE_REMOVE; +} + +void syscommon_notifier_emit_notify(int type, void *data) +{ + GList *n; + struct syscommon_notifier *notifier; + + SYS_G_LIST_FOREACH(syscommon_notifier_list, n, notifier) { + if (!notifier->deleted && type == notifier->type) { + if (notifier->func) + notifier->func(data); + else if (notifier->func_udata) + notifier->func_udata(data, notifier->user_data); + } + } + + if (!idl) + idl = g_idle_add(delete_unused_notifier_cb, NULL); +} + +void syscommon_notifier_emit_notify_once(int type, void *data) +{ + GList *n; + struct syscommon_notifier *notifier; + + SYS_G_LIST_FOREACH(syscommon_notifier_list, n, notifier) { + if (!notifier->deleted && type == notifier->type) { + if (notifier->func) { + notifier->func(data); + } else if (notifier->func_udata) { + notifier->func_udata(data, notifier->user_data); + if (notifier->destroyer) + notifier->destroyer(notifier->user_data); + } + + notifier->deleted = true; + } + } + + if (!idl) + idl = g_idle_add(delete_unused_notifier_cb, NULL); +} diff --git a/tests/libcommon/test-common.c b/tests/libcommon/test-common.c index 9df8e61..f34ce27 100644 --- a/tests/libcommon/test-common.c +++ b/tests/libcommon/test-common.c @@ -10,12 +10,22 @@ #include "libsyscommon/list.h" #include "libsyscommon/ini-parser.h" #include "libsyscommon/bitmap.h" +#include "libsyscommon/notifier.h" #include "../test-main.h" #include "../test-mock.h" #define MOCK_FILE_PATH "testfile" #define MOCK_FILE_LENGTH 100 +#define SYSCOMMON_NOTIFIER_1 (1) +#define SYSCOMMON_NOTIFIER_2 (2) +#define SYSCOMMON_NOTIFIER_3 (3) +#define SYSCOMMON_NOTIFIER_4 (4) +#define SYSCOMMON_NOTIFIER_5 (5) +#define SYSCOMMON_NOTIFIER_6 (6) +#define SYSCOMMON_NOTIFIER_7 (7) +#define SYSCOMMON_NOTIFIER_8 (8) + static int setup(void **state) { FILE *fp; @@ -273,6 +283,224 @@ static void test_all_bit(void **state) syscommon_bitmap_deinit_bitmap(bm); } +static int notify_callback(void *data) +{ + check_expected(data); + + return 0; +} + +static int notify_callback_udata(void *data, void *udata) +{ + check_expected(data); + check_expected(udata); + + return 0; +} + +static void destroy_callback_udata(void *udata) +{ + check_expected(udata); +} + +static void test_syscommon_notifier_emit_notify_p1(void **state) +{ + int retval; + + retval = syscommon_notifier_subscribe_notify(SYSCOMMON_NOTIFIER_1, notify_callback); + assert_int_equal(retval, 0); + expect_value(notify_callback, data, (void *)(intptr_t) 0x3f3f3f3f); + + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_1, (void *)(intptr_t) 0x3f3f3f3f); +} + +static void test_syscommon_notifier_emit_notify_p2(void **state) +{ + int retval; + int notify_data; + const int udata2 = 0xaeaeaeae; + const int udata3 = 0x99997777; + + notify_data = 0x12456321; + + /* 1st notifier */ + retval = syscommon_notifier_subscribe_notify(SYSCOMMON_NOTIFIER_2, notify_callback); + assert_int_equal(retval, 0); + + /* 2nd notifier */ + retval = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_2, notify_callback_udata, (void *)(intptr_t) udata2, NULL); + assert_in_range(retval, 1, INT_MAX); + + /* expect invocation of 1st callback and check parameter */ + expect_value(notify_callback, data, (void *)(intptr_t) notify_data); + /* expect invocation of 2nd callback and check parameter */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) notify_data); + expect_value(notify_callback_udata, udata,(void *)(intptr_t) udata2); + + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_2, (void *)(intptr_t) notify_data); + + notify_data = 0x888899dd; + + /* 3rd notifier */ + retval = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_2, notify_callback_udata, (void *)(intptr_t) udata3, NULL); + assert_in_range(retval, 1, INT_MAX); + + /* expect invocation of 1st callback and check parameter */ + expect_value(notify_callback, data, (void *)(intptr_t) notify_data); + /* expect invocation of 2nd callback and check parameter */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) notify_data); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) udata2); + /* expect invocation of 3rd callback and check parameter */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) notify_data); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) udata3); + + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_2, (void *)(intptr_t) notify_data); +} + +static void test_syscommon_notifier_emit_notify_p3(void **state) +{ + int notify_data; + int id1, id2; + const int udata1 = 0x41a41a41; + const int udata2 = 0x77777777; + + /* first run */ + notify_data = 0x8575ddff; + + /* 1st notifier */ + id1 = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_3, notify_callback_udata, (void *)(intptr_t) udata1, NULL); + assert_in_range(id1, 1, INT_MAX); + + /* 2nd notifier */ + id2 = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_3, notify_callback_udata, (void *)(intptr_t) udata2, NULL); + assert_in_range(id2, 1, INT_MAX); + + /* expect invocation of 1st callback and check parameter */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) notify_data); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) udata1); + /* expect invocation of 2nd callback and check parameter */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) notify_data); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) udata2); + + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_3, (void *)(intptr_t) notify_data); + + /* second run. at this time syscommon_notifier_emit_notify() after unregistering 1st notifier */ + notify_data = 0x345; + + syscommon_notifier_unsubscribe_notify_udata(id1); + + /* only expect invocation of 2nd callback and check parameter */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) notify_data); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) udata2); + + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_3, (void *)(intptr_t) notify_data); +} + +static void test_syscommon_notifier_emit_notify_once_p(void **state) +{ + int retval; + + retval = syscommon_notifier_subscribe_notify(SYSCOMMON_NOTIFIER_4, notify_callback); + assert_int_equal(retval, 0); + + expect_value(notify_callback, data, (void *)(intptr_t) 0xabcdabcd); + syscommon_notifier_emit_notify_once(SYSCOMMON_NOTIFIER_4, (void *)(intptr_t) 0xabcdabcd); + + /* Don't add expect_value() for callback at this time. + * Therefore if the callback is invoked, check_expected() returns error */ + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_4, (void *)(intptr_t) 0xabcdabcd); + + retval = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_5, notify_callback_udata, (void *)(intptr_t) 0xfefefefe, NULL); + assert_in_range(retval, 1, INT_MAX); + + expect_value(notify_callback_udata, data, (void *)(intptr_t) 0x34343434); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) 0xfefefefe); + syscommon_notifier_emit_notify_once(SYSCOMMON_NOTIFIER_5, (void *)(intptr_t) 0x34343434); + + /* Don't add expect_value() for callback at this time. + * Therefore if the callback is invoked, check_expected() returns error */ + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_5, (void *)(intptr_t) 0x34343434); +} + +static void test_destroy_callback_p1(void **state) +{ + int id; + + id = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_6, notify_callback_udata, + (void *)(intptr_t) 0x4848, destroy_callback_udata); + assert_in_range(id, 1, INT_MAX); + + expect_value(destroy_callback_udata, udata, (void *)(intptr_t) 0x4848); + + syscommon_notifier_unsubscribe_notify_udata(id); +} + +static void test_destroy_callback_p2(void **state) +{ + int id; + + id = syscommon_notifier_subscribe_notify_udata(SYSCOMMON_NOTIFIER_6, notify_callback_udata, + (void *)(intptr_t) 0x1177, destroy_callback_udata); + assert_in_range(id, 1, INT_MAX); + + expect_value(notify_callback_udata, data, (void *)(intptr_t) 0x9a9a9a9a); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) 0x1177); + expect_value(destroy_callback_udata, udata, (void *)(intptr_t) 0x1177); + + syscommon_notifier_emit_notify_once(SYSCOMMON_NOTIFIER_6, (void *)(intptr_t) 0x9a9a9a9a); +} + +static void test_destroy_callback_p3(void **state) +{ + int retval; + + retval = syscommon_notifier_subscribe_notify(SYSCOMMON_NOTIFIER_7, notify_callback); + assert_int_equal(retval, 0); + expect_value(notify_callback, data, (void *)(intptr_t) 0x3f3f3f3f); + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_7, (void *)(intptr_t) 0x3f3f3f3f); + + /* notify_callback() should not be invoked */ + syscommon_notifier_unsubscribe_notify(SYSCOMMON_NOTIFIER_7, notify_callback); + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_7, (void *)(intptr_t) 0x34ab34ab); + + /* if a notifier have successfully been deleted, + * re-registering the notifier must be successful */ + retval = syscommon_notifier_subscribe_notify(SYSCOMMON_NOTIFIER_7, notify_callback); + assert_int_equal(retval, 0); + + expect_value(notify_callback, data, (void *)(intptr_t) 0x7878444); + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_7, (void *)(intptr_t) 0x7878444); +} + +static void test_syscommon_notifier_emit_notify_priority_p1(void **state) +{ + int id1, id2, id3; + + id1 = syscommon_notifier_subscribe_notify_udata_priority(SYSCOMMON_NOTIFIER_8, notify_callback_udata, + (void *)(intptr_t) 0x11111111, NULL, -300); + assert_in_range(id1, 1, INT_MAX); + + id2 = syscommon_notifier_subscribe_notify_udata_priority(SYSCOMMON_NOTIFIER_8, notify_callback_udata, + (void *)(intptr_t) 0x22222222, NULL, 500); + assert_in_range(id2, 1, INT_MAX); + + id3 = syscommon_notifier_subscribe_notify_udata_priority(SYSCOMMON_NOTIFIER_8, notify_callback_udata, + (void *)(intptr_t) 0x33333333, NULL, -300); + assert_in_range(id3, 1, INT_MAX); + + /* id2 will be invoked first */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) 0x1234); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) 0x22222222); + /* id1, id3 invocation follows it, + * and those are invoked in the order in which they were registered */ + expect_value(notify_callback_udata, data, (void *)(intptr_t) 0x1234); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) 0x11111111); + expect_value(notify_callback_udata, data, (void *)(intptr_t) 0x1234); + expect_value(notify_callback_udata, udata, (void *)(intptr_t) 0x33333333); + + syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_8, (void *)(intptr_t)0x1234); +} + int run_test_suite(void) { const struct CMUnitTest testsuite[] = { @@ -286,6 +514,14 @@ int run_test_suite(void) cmocka_unit_test(test_init_bitmap_n), cmocka_unit_test(test_test_bit), cmocka_unit_test(test_all_bit), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p1), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p2), + cmocka_unit_test(test_syscommon_notifier_emit_notify_p3), + cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p), + cmocka_unit_test(test_destroy_callback_p1), + cmocka_unit_test(test_destroy_callback_p2), + cmocka_unit_test(test_destroy_callback_p3), + cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1), }; return cmocka_run_group_tests(testsuite, NULL, NULL); -- 2.7.4 From 619e6c365399e371a1a2b7ad84ed7964e370e17f Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 26 Jul 2023 14:34:11 +0900 Subject: [PATCH 04/16] libcommon: Add syscommon_file_copy() Change-Id: I00608e8a28b61026e19c1e73d4e0ed063c50379f Signed-off-by: Youngjae Cho --- include/libsyscommon/file.h | 13 +++++++++++++ src/libcommon/file.c | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/libsyscommon/file.h b/include/libsyscommon/file.h index 5c038e6..b879874 100644 --- a/include/libsyscommon/file.h +++ b/include/libsyscommon/file.h @@ -24,6 +24,7 @@ #define __LIBCOMMON_FILE_H__ #include +#include #ifdef __cplusplus extern "C" { @@ -101,6 +102,18 @@ int sys_set_str(char *fname, char *val); */ int syscommon_parse_cmdline_scanf(const char *format, ...); +/** + * @brief Copy file + * + * @param[in] src Absolute path for a source file + * @param[in] dst Absolute path for a destination + * @param[in] overwrite If true, overwrite destination file if exist. + * If false, return -EEXIST if there is destination file. + * + * @return zero on success, otherwise negative error value + */ +int syscommon_file_copy(const char *src, const char *dst, bool overwrite); + #ifdef __cplusplus } #endif diff --git a/src/libcommon/file.c b/src/libcommon/file.c index 6c6cbaf..c632ef5 100644 --- a/src/libcommon/file.c +++ b/src/libcommon/file.c @@ -31,6 +31,14 @@ #define SHARED_H_BUF_MAX 255 +static void close_fd(int *fd) +{ + if (*fd != -1) + close(*fd); +} + +#define _cleanup_close_ __attribute__((__cleanup__(close_fd))) + int sys_read_buf(char *file, char *buf, int len) { int fd, r; @@ -164,3 +172,41 @@ int syscommon_parse_cmdline_scanf(const char *format, ...) return ret; } + +int syscommon_file_copy(const char *src, const char *dst, bool overwrite) +{ + _cleanup_close_ int rfd = -1; + _cleanup_close_ int wfd = -1; + + char buf[1024] = { 0 ,}; + ssize_t nread; + int r; + + if (!src || !dst) + return -EINVAL; + + if (!overwrite) { + r = access(dst, F_OK); + if (r == 0) + return -EALREADY; + else if (errno != ENOENT) + return -errno; + } + + wfd = open(dst, O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (wfd < 0) + return -errno; + + rfd = open(src, O_RDONLY); + if (rfd < 0) + return -errno; + + while ((nread = read(rfd, buf, 1024)) > 0) + if (write(wfd, buf, nread) != nread) + return -errno; + + if (nread < 0) + return -errno; + + return 0; +} -- 2.7.4 From 76100deb82b6fbfdc0cf51ad501e6be921c3dfdf Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 26 Jul 2023 16:34:36 +0900 Subject: [PATCH 05/16] libcommon: Add syscommon_mkdir() It makes directory of the given path, including the whole intermediate path if not exists. That is, it is equivalent to command 'mkdir -p'. It has come from the deviced as the function is eligible to being a common function to be utilized by overall system libs/daemons. Change-Id: Ifbf23c3fe721a149c54d6f650cce093dfb6f9e10 Signed-off-by: Youngjae Cho --- include/libsyscommon/common.h | 8 ++++++++ src/libcommon/common.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/libsyscommon/common.h b/include/libsyscommon/common.h index f6cd108..14d20de 100644 --- a/include/libsyscommon/common.h +++ b/include/libsyscommon/common.h @@ -24,6 +24,7 @@ #define __SYSCOMMON_COMMON_H__ #include +#include #ifdef __cplusplus extern "C" { @@ -50,6 +51,13 @@ bool syscommon_is_container(void); */ bool syscommon_is_mounted(const char *path); +/** + * @brief Make directory including intermediate directories + * + * @return 0 on succes, otherwise negative error value + */ +int syscommon_mkdir(const char *path, mode_t mode); + #ifdef __cplusplus } #endif diff --git a/src/libcommon/common.c b/src/libcommon/common.c index 0a220c7..116c131 100644 --- a/src/libcommon/common.c +++ b/src/libcommon/common.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "shared/log.h" #include "libsyscommon/common.h" @@ -33,6 +34,8 @@ #define FEATURE_MODEL_NAME_EMULATOR "Emulator" #define CONTAINER_FILE_PATH "/run/systemd/container" +#define PATH_MAX 256 + bool syscommon_is_emulator(void) { int ret = 0; @@ -99,3 +102,32 @@ bool syscommon_is_mounted(const char *path) endmntent(fp); return ret; } + +int syscommon_mkdir(const char *path, mode_t mode) +{ + char dir[PATH_MAX] = { 0, }; + size_t sep, l; + int pos; + int ret; + + if (!path) + return -EINVAL; + + l = strlen(path); + + for (pos = 0, sep = 0; pos < l; pos += sep + 1) { + sep = strcspn(path + pos, "/"); + if (!sep) + continue; + + ret = snprintf(dir, pos + sep + 1, "%s", path); + if (ret < 0) + return ret; + + ret = mkdir(dir, mode); + if (ret < 0 && errno != EEXIST) + return -errno; + } + + return 0; +} -- 2.7.4 From 4389240a8eecbafdbe3f301ca545da53c4c8b84f Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Thu, 27 Jul 2023 16:43:24 +0900 Subject: [PATCH 06/16] plugin-api: common: Add SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0 As tizen 7.5 is replaced with 8.0, this definition should be added. Change-Id: Ifa6a0a2107ff700672f26f68839c3ef9c08314f9 Signed-off-by: Yunhee Seo --- .../common/include/syscommon-plugin-common-interface.h | 4 ++-- src/plugin-api/common/src/syscommon-plugin-api-list.h | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/plugin-api/common/include/syscommon-plugin-common-interface.h b/src/plugin-api/common/include/syscommon-plugin-common-interface.h index 1cda550..2345cde 100644 --- a/src/plugin-api/common/include/syscommon-plugin-common-interface.h +++ b/src/plugin-api/common/include/syscommon-plugin-common-interface.h @@ -32,14 +32,14 @@ extern "C" { enum syscommon_plugin_abi_version { SYSCOMMON_PLUGIN_ABI_VERSION_UNKNOWN = 0, SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_0, - SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_5, + SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, SYSCOMMON_PLUGIN_ABI_VERSION_END, }; static const char *const syscommon_plugin_abi_version_str[] = { [SYSCOMMON_PLUGIN_ABI_VERSION_UNKNOWN] = "Unknown PLUGIN ABI Version", [SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_0] = "PLUGIN_ABI_VERSION_TIZEN_7_0", - [SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_5] = "PLUGIN_ABI_VERSION_TIZEN_7_5", + [SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0] = "PLUGIN_ABI_VERSION_TIZEN_8_0", }; typedef struct __syscommon_plugin_backend { diff --git a/src/plugin-api/common/src/syscommon-plugin-api-list.h b/src/plugin-api/common/src/syscommon-plugin-api-list.h index 477c6da..6257378 100644 --- a/src/plugin-api/common/src/syscommon-plugin-api-list.h +++ b/src/plugin-api/common/src/syscommon-plugin-api-list.h @@ -32,13 +32,20 @@ #define PLUGIN_ABI_VERSION_MAX 10 enum syscommon_plugin_abi_version g_platform_curr_abi_version = - SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_5; + SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0; static struct plugin_abi_version_match abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_END][PLUGIN_ABI_VERSION_MAX] = { [SYSCOMMON_PLUGIN_MODULE_RESOURCED_MEMORY_LMK] = { [0] = { - .platform_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_5, - .backend_min_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_7_5, + .platform_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, + .backend_min_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, + }, + }, + /* PLUGIN_GROUP_DEVICED */ + [SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY] = { + [0] = { + .platform_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, + .backend_min_abi_version = SYSCOMMON_PLUGIN_ABI_VERSION_TIZEN_8_0, }, }, }; -- 2.7.4 From 0dc9ac2cbb2709cc7101f9ef84fec941610c9d96 Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Wed, 26 Jul 2023 20:00:04 +0900 Subject: [PATCH 07/16] plugin-api: deviced: Add syscommon_plugin_deviced_battery_is_possible_to_notify_battery_full() To use deviced plugin battery module, plugin backend battery module getter/setter are added. And, new plugin module definition "SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY" is added. Also, to utilize plugin backend battery module function, new battery interface header and source file was created. To use plugin backend module, these are newly added. - int syscommon_plugin_deviced_battery_get_backend(void); - int syscommon_plugin_deviced_battery_put_backend(void); - int syscommon_plugin_deviced_battery_is_possible_to_notify_battery_full(bool *possible_notify); -> This is used to determine whether to inform the fully charged or not. * libsyscommon.spec typo was also corrected. Change-Id: I30a76504cd27ab44000459080fb7cd0f72f42c92 Signed-off-by: Yunhee Seo --- packaging/libsyscommon.spec | 2 +- .../common/include/syscommon-plugin-common.h | 1 + .../common/src/syscommon-plugin-api-list.h | 12 +++ src/plugin-api/deviced/CMakeLists.txt | 1 + .../syscommon-plugin-deviced-battery-interface.h | 42 +++++++++ .../include/syscommon-plugin-deviced-battery.h | 56 +++++++++++ src/plugin-api/deviced/src/common.h | 42 +++++++++ .../deviced/src/syscommon-plugin-deviced-battery.c | 102 +++++++++++++++++++++ 8 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 src/plugin-api/deviced/include/syscommon-plugin-deviced-battery-interface.h create mode 100644 src/plugin-api/deviced/include/syscommon-plugin-deviced-battery.h create mode 100644 src/plugin-api/deviced/src/common.h create mode 100644 src/plugin-api/deviced/src/syscommon-plugin-deviced-battery.c diff --git a/packaging/libsyscommon.spec b/packaging/libsyscommon.spec index e7d9c82..bb2d003 100644 --- a/packaging/libsyscommon.spec +++ b/packaging/libsyscommon.spec @@ -103,7 +103,7 @@ PLUGIN_API_DEVICED_MAJORVER=$(echo %{libsyscommon_plugin_api_deviced_version} | -DPLUGIN_API_DEVICED_MAJORVER=${PLUGIN_API_DEVICED_MAJORVER} \ -DPLUGIN_API_COMMON_ENABLE_DLOG=1 \ -DPLUGIN_API_RESOURCED_ENABLE_DLOG=1 \ - -DPLUGIN_API_DEVICEDD_ENABLE_DLOG=1 + -DPLUGIN_API_DEVICED_ENABLE_DLOG=1 make %{?_smp_mflags} %install diff --git a/src/plugin-api/common/include/syscommon-plugin-common.h b/src/plugin-api/common/include/syscommon-plugin-common.h index d530c0a..c644453 100644 --- a/src/plugin-api/common/include/syscommon-plugin-common.h +++ b/src/plugin-api/common/include/syscommon-plugin-common.h @@ -34,6 +34,7 @@ extern "C" { enum syscommon_plugin_module { SYSCOMMON_PLUGIN_MODULE_UNKNOWN = 0, SYSCOMMON_PLUGIN_MODULE_RESOURCED_MEMORY_LMK, + SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY, SYSCOMMON_PLUGIN_MODULE_END, }; diff --git a/src/plugin-api/common/src/syscommon-plugin-api-list.h b/src/plugin-api/common/src/syscommon-plugin-api-list.h index 6257378..66a6c01 100644 --- a/src/plugin-api/common/src/syscommon-plugin-api-list.h +++ b/src/plugin-api/common/src/syscommon-plugin-api-list.h @@ -63,6 +63,18 @@ static struct __plugin_module_info g_plugin_module_info[] = { .num_abi_versions = ARRAY_SIZE(abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_RESOURCED_MEMORY_LMK]), .abi_versions = abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_RESOURCED_MEMORY_LMK], }, + [SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY] = { + .group = PLUGIN_GROUP_DEVICED, + .module = SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY, + .license = PLUGIN_LICENSE_APACHE_2_0, + .module_name = "SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY", + .backend_module_name = "deviced-battery", + .library_name = "/usr/lib/system/plugin/libplugin-backend-deviced-battery.so", + .library_name_64bit = "/usr/lib64/system/plugin/libplugin-backend-deviced-battery.so", + .symbol_name = "system_plugin_backend_deviced_battery", + .num_abi_versions = ARRAY_SIZE(abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY]), + .abi_versions = abi_version_match_data[SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY], + }, }; #endif /* __PLUGIN_API_LIST_H__ */ diff --git a/src/plugin-api/deviced/CMakeLists.txt b/src/plugin-api/deviced/CMakeLists.txt index 78af80a..a82642b 100644 --- a/src/plugin-api/deviced/CMakeLists.txt +++ b/src/plugin-api/deviced/CMakeLists.txt @@ -40,6 +40,7 @@ SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") # Please remove them from SRCS and add 'syscommon-plugin-api-common' # to PKG_MODULES. SET(SRCS src/syscommon-plugin-deviced-display.c + src/syscommon-plugin-deviced-battery.c ../common/src/syscommon-plugin-api-common.c ../common/src/syscommon-plugin-api-conf.c) diff --git a/src/plugin-api/deviced/include/syscommon-plugin-deviced-battery-interface.h b/src/plugin-api/deviced/include/syscommon-plugin-deviced-battery-interface.h new file mode 100644 index 0000000..1088509 --- /dev/null +++ b/src/plugin-api/deviced/include/syscommon-plugin-deviced-battery-interface.h @@ -0,0 +1,42 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __SYSCOMMON_PLUGIN_DEVICED_BATTERY_INTERFACE_H__ +#define __SYSCOMMON_PLUGIN_DEVICED_BATTERY_INTERFACE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +typedef struct _syscommon_plugin_backend_deviced_battery_funcs { + bool (*is_possible_to_notify_battery_full) (void); + +} syscommon_plugin_backend_deviced_battery_funcs; + +#ifdef __cplusplus +} +#endif + +#endif //__SYSCOMMON_PLUGIN_DEVICED_BATTERY_INTERFACE_H__ diff --git a/src/plugin-api/deviced/include/syscommon-plugin-deviced-battery.h b/src/plugin-api/deviced/include/syscommon-plugin-deviced-battery.h new file mode 100644 index 0000000..3e376b0 --- /dev/null +++ b/src/plugin-api/deviced/include/syscommon-plugin-deviced-battery.h @@ -0,0 +1,56 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __SYSCOMMON_PLUGIN_DEVICED_BATTERY_H__ +#define __SYSCOMMON_PLUGIN_DEVICED_BATTERY_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief Get the backend data of deviced-battery module + * @return @c 0 on success, otherwise a negative error value + */ +int syscommon_plugin_deviced_battery_get_backend(void); + +/** + * @brief Put the backend data of deviced-battery module + * @return @c 0 on success, otherwise a negative error value + */ +int syscommon_plugin_deviced_battery_put_backend(void); + +/** + * @brief Call the is_possible_to_notify_battery_full function of deviced-battery module + * @return @c 0 on success, otherwise a negative error value + */ +int syscommon_plugin_deviced_battery_is_possible_to_notify_battery_full(bool *possible_notify); + +#ifdef __cplusplus +} +#endif + +#endif //__SYSCOMMON_PLUGIN_DEVICED_BATTERY_H__ diff --git a/src/plugin-api/deviced/src/common.h b/src/plugin-api/deviced/src/common.h new file mode 100644 index 0000000..236c616 --- /dev/null +++ b/src/plugin-api/deviced/src/common.h @@ -0,0 +1,42 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __COMMON_H__ +#define __COMMON_H__ + +#ifdef ENABLE_DLOG +#include + +#define _D(fmt, args...) SLOGD(fmt, ##args) +#define _I(fmt, args...) SLOGI(fmt, ##args) +#define _W(fmt, args...) SLOGW(fmt, ##args) +#define _E(fmt, args...) SLOGE(fmt, ##args) +#else +#define _D(fmt, args...) do { } while(0) +#define _I(fmt, args...) do { } while(0) +#define _W(fmt, args...) do { } while(0) +#define _E(fmt, args...) do { } while(0) +#endif + +#endif /* __COMMON_H__ */ diff --git a/src/plugin-api/deviced/src/syscommon-plugin-deviced-battery.c b/src/plugin-api/deviced/src/syscommon-plugin-deviced-battery.c new file mode 100644 index 0000000..f242f2d --- /dev/null +++ b/src/plugin-api/deviced/src/syscommon-plugin-deviced-battery.c @@ -0,0 +1,102 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include + +#include "syscommon-plugin-common.h" + +#include "common.h" +#include "syscommon-plugin-deviced-battery.h" +#include "syscommon-plugin-deviced-battery-interface.h" + +#ifndef EXPORT +#define EXPORT __attribute__((visibility("default"))) +#endif + +static syscommon_plugin_backend_deviced_battery_funcs *funcs = NULL; + +EXPORT +int syscommon_plugin_deviced_battery_get_backend(void) +{ + int ret = 0; + + if (funcs) + return 0; + + ret = syscommon_plugin_common_get_backend( + SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY, + (void **)&funcs); + if (ret < 0) { + _E("Failed to get deviced_battery backend: %d", ret); + return ret; + } + + _I("Success to get deviced_battery backend: %d", ret); + + return 0; +} + +EXPORT +int syscommon_plugin_deviced_battery_put_backend(void) +{ + int ret = 0; + + if (!funcs) + return 0; + + ret = syscommon_plugin_common_put_backend( + SYSCOMMON_PLUGIN_MODULE_DEVICED_BATTERY, + (void *)funcs); + if (ret < 0) { + _E("Failed to put deviced_battery backend: %d", ret); + return ret; + } + funcs = NULL; + + _I("Success to put deviced_battery backend: %d", ret); + + return 0; +} + +EXPORT +int syscommon_plugin_deviced_battery_is_possible_to_notify_battery_full(bool *possible_notify) +{ + int ret = 0; + + if (!possible_notify) + return -EINVAL; + + if (!funcs) { + ret = syscommon_plugin_deviced_battery_get_backend(); + if (ret < 0) + return ret; + } + + if (!funcs || !funcs->is_possible_to_notify_battery_full) { + _E("No backend or no \"is_possible_to_notify_battery_full\" function"); + return -ENOTSUP; + } + + *possible_notify = funcs->is_possible_to_notify_battery_full(); + return 0; +} -- 2.7.4 From c2888ff6aeec24b764c51f7f7c1581091cc36f82 Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Thu, 22 Jun 2023 19:23:32 +0900 Subject: [PATCH 08/16] plugin-api: resourced: Add window info into 'task_info' for LMK governor Add window information list in 'task_info'. Each window includes information such as visibility, focusing status, and coordinate for LMK governor resourced low memory killer (LMK) passes app information to LMK governor during LMK process. Sometimes, LMK governor needs to know window information of foreground app to prioritize which apps to be killed. This is because foreground app(s) are visible status. If visible app(s) are killed by LMK suddenly, then it embarrass the user. Change-Id: I6b7b4ae00ca95534e7d99ec8b412909038d2fdc6 Signed-off-by: Unsung Lee --- .../include/syscommon-plugin-resourced-memory-lmk.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h index 49f257b..5f1a8b5 100644 --- a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h +++ b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h @@ -59,6 +59,25 @@ struct task_info { */ bool *proc_app_info_oom_killed; int proc_app_info_flags; + + /** + * TODO: A process can have more than one window, + * and an app can have more than one process. Therefore, + * a task_info can have more than one window. + */ + int window_pid; + int x; + int y; + int z; + int w; + int h; + int layer; + int opaque; + int visibility; + bool is_transformed; + bool alpha; + bool is_focused; + bool is_mapped; }; /** -- 2.7.4 From fd9e938afd953da3504b82ef76da4a78cb84e03b Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Thu, 22 Jun 2023 11:43:05 +0900 Subject: [PATCH 09/16] plugin-api: resourced: Add a skeleton of get_kill_candidates_post funcs Add a skeleton of three get_kill_candidates_post funcs. These functions are directly mapped to get_kill_candidates_post funcs in resourced backend. Support LMK governor post funcs which will be called after LMK governor function to reorder a victim candidate list resourced supports three type of get_kill_candidates_post ops newly - int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post( GArray *candidates) to consider user custom governor policy - int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( GArray *candidates) to consider working set size of app - int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_foreground( GArray *candidates) to consider window information of app Change-Id: I2a3c42635ce105f4400a68c8c382fb6548a11f8e Signed-off-by: Unsung Lee --- ...scommon-plugin-resourced-memory-lmk-interface.h | 4 +- .../syscommon-plugin-resourced-memory-lmk.h | 33 +++++++++++ .../src/syscommon-plugin-resourced-memory-lmk.c | 64 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h index 9bfc4ac..3b7447a 100644 --- a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h +++ b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h @@ -36,7 +36,9 @@ typedef struct _syscommon_plugin_backend_resourced_memory_lmk_funcs { GArray *task_info_app_array, GArray *task_info_proc_array, unsigned long totalram_kb); - + int (*get_kill_candidates_post)(GArray *candidates); + int (*get_kill_candidates_post_with_wss)(GArray *candidates); + int (*get_kill_candidates_post_with_foreground)(GArray *candidates); } syscommon_plugin_backend_resourced_memory_lmk_funcs; #ifdef __cplusplus diff --git a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h index 5f1a8b5..bdcb8e8 100644 --- a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h +++ b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h @@ -107,6 +107,39 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates( GArray *task_info_proc_array, unsigned long totalram_kb); +/** + * @brief Reorder candidates list, sorted first by get_kill_candidates fuction, + according to custom rule + * @param[in] candidates is a GArray to return kill candidates, it is already + sorted by get_kill_candidates + * @return @c zero on success, + * otherwise a negative error value + */ +int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post( + GArray *candidates); + +/** + * @brief Reorder candidates list sorted first by get_kill_candidates fuction + according to working set size rule + * @param[in] candidates is a GArray to return kill candidates, it is already + sorted by get_kill_candidates + * @return @c zero on success, + * otherwise a negative error value + */ +int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( + GArray *candidates); + +/** + * @brief Reorder candidates list sorted first by get_kill_candidates fuction + according to foreground rule (i.e., rule for foreground apps) + * @param[in] candidates is a GArray to return kill candidates, it is already + sorted by get_kill_candidates + * @return @c zero on success, + * otherwise a negative error value + */ +int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_foreground( + GArray *candidates); + #ifdef __cplusplus } #endif diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c index c79eeb0..205e975 100644 --- a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c @@ -22,6 +22,7 @@ * THE SOFTWARE. */ +#include #include #include "syscommon-plugin-common.h" @@ -104,3 +105,66 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates( task_info_proc_array, totalram_kb); } + +EXPORT +int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post( + GArray *candidates) +{ + int ret = 0; + + if (!funcs) { + ret = syscommon_plugin_resourced_memory_lmk_get_backend(); + if (ret < 0) + return ret; + } + + assert(funcs); + if (!funcs->get_kill_candidates_post) { + _E("No \"get_kill_candidates_post\" function"); + return -ENOTSUP; + } + + return funcs->get_kill_candidates_post(candidates); +} + +EXPORT +int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( + GArray *candidates) +{ + int ret = 0; + + if (!funcs) { + ret = syscommon_plugin_resourced_memory_lmk_get_backend(); + if (ret < 0) + return ret; + } + + assert(funcs); + if (!funcs->get_kill_candidates_post_with_wss) { + _E("No \"get_kill_candidates_post_with_wss\" function"); + return -ENOTSUP; + } + + return funcs->get_kill_candidates_post_with_wss(candidates); +} + +EXPORT +int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_foreground( + GArray *candidates) +{ + int ret = 0; + + if (!funcs) { + ret = syscommon_plugin_resourced_memory_lmk_get_backend(); + if (ret < 0) + return ret; + } + + assert(funcs); + if (!funcs->get_kill_candidates_post_with_foreground) { + _E("No \"get_kill_candidates_post_with_foreground\" function"); + return -ENOTSUP; + } + + return funcs->get_kill_candidates_post_with_foreground(candidates); +} -- 2.7.4 From 7d02ec432105640fa6c5f08fc00a903d8fc17ead Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 25 Jul 2023 19:09:00 +0900 Subject: [PATCH 10/16] plugin-api: resourced: Add enum syscommon_resourced_memory_lmk_oom_level Add enumeration of oom level into libsyscommon to reference it from both resourced and resourced plugin-backend Oom level will be referenced by resourced and resourced backend when LMK is triggered by low memory. Therefore, it should be located in common library (i.e., libsyscommon) Change-Id: I2dcc1c7e3768dccf5db256845614557b6d8dc60c Signed-off-by: Unsung Lee --- .../resourced/include/syscommon-plugin-resourced-memory-lmk.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h index bdcb8e8..fb070ae 100644 --- a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h +++ b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h @@ -32,6 +32,14 @@ extern "C" { #endif +enum syscommon_resourced_memory_lmk_oom_level { + OOM_LEVEL_BACKGROUND_LEAST_RECENTLY_USED, + OOM_LEVEL_BACKGROUND_MOST_RECENTLY_USED, + OOM_LEVEL_FOREGROUND, + OOM_LEVEL_FOREGROUND_AND_PROC, + OOM_LEVEL_ALL, +}; + struct task_info { /* * Mostly, there are not multiple processes with the same pgid. -- 2.7.4 From bac2f0522331809a94d3c6b12c0ff8c30fc6a602 Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Tue, 25 Jul 2023 20:21:34 +0900 Subject: [PATCH 11/16] plugin-api: resourced: Pass oom level to LMK governor post for fg app LMK governor post function reordering foreground app candidates requires oom level, so pass oom level to this post function. Change-Id: I77b24eb298506698fdef73d8136990734eb0ef24 Signed-off-by: Unsung Lee --- .../include/syscommon-plugin-resourced-memory-lmk-interface.h | 3 ++- .../resourced/include/syscommon-plugin-resourced-memory-lmk.h | 3 ++- src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h index 3b7447a..a07f0c4 100644 --- a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h +++ b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk-interface.h @@ -38,7 +38,8 @@ typedef struct _syscommon_plugin_backend_resourced_memory_lmk_funcs { unsigned long totalram_kb); int (*get_kill_candidates_post)(GArray *candidates); int (*get_kill_candidates_post_with_wss)(GArray *candidates); - int (*get_kill_candidates_post_with_foreground)(GArray *candidates); + int (*get_kill_candidates_post_with_foreground)(GArray *candidates, + enum syscommon_resourced_memory_lmk_oom_level oom_level); } syscommon_plugin_backend_resourced_memory_lmk_funcs; #ifdef __cplusplus diff --git a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h index fb070ae..d3d5b67 100644 --- a/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h +++ b/src/plugin-api/resourced/include/syscommon-plugin-resourced-memory-lmk.h @@ -146,7 +146,8 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( * otherwise a negative error value */ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_foreground( - GArray *candidates); + GArray *candidates, + enum syscommon_resourced_memory_lmk_oom_level oom_level); #ifdef __cplusplus } diff --git a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c index 205e975..431625a 100644 --- a/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c +++ b/src/plugin-api/resourced/src/syscommon-plugin-resourced-memory-lmk.c @@ -150,7 +150,8 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_wss( EXPORT int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_foreground( - GArray *candidates) + GArray *candidates, + enum syscommon_resourced_memory_lmk_oom_level oom_level) { int ret = 0; @@ -166,5 +167,5 @@ int syscommon_plugin_resourced_memory_lmk_get_kill_candidates_post_with_foregrou return -ENOTSUP; } - return funcs->get_kill_candidates_post_with_foreground(candidates); + return funcs->get_kill_candidates_post_with_foreground(candidates, oom_level); } -- 2.7.4 From 153f06c55997c59fc2278f9b512883bee9b35972 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 31 Jul 2023 16:17:45 +0900 Subject: [PATCH 12/16] plugin-api: deviced: Add skeleton header for power module syscommon-plugin-deviced-common-interface.h - Define common types and macros that can be commonly utilized over the whole modules, by both core and plugin. - Added enum deviced_resource_type so that all modules can share the enum. This is because the enum must be unique for each module. syscommon-plugin-deviced-power.h - Contain plugin-api functions that invoking plugin functions from the deviced core. syscommon-plugin-deviced-power-interface.h - Contain types and macros that can be utilized by both core and plugin. Change-Id: Iab8bc083a3610b720222df284f880d961fb75188 Signed-off-by: Youngjae Cho --- .../syscommon-plugin-deviced-common-interface.h | 42 ++++++++++++ .../syscommon-plugin-deviced-power-interface.h | 79 ++++++++++++++++++++++ .../include/syscommon-plugin-deviced-power.h | 36 ++++++++++ 3 files changed, 157 insertions(+) create mode 100644 src/plugin-api/deviced/include/syscommon-plugin-deviced-common-interface.h create mode 100644 src/plugin-api/deviced/include/syscommon-plugin-deviced-power-interface.h create mode 100644 src/plugin-api/deviced/include/syscommon-plugin-deviced-power.h diff --git a/src/plugin-api/deviced/include/syscommon-plugin-deviced-common-interface.h b/src/plugin-api/deviced/include/syscommon-plugin-deviced-common-interface.h new file mode 100644 index 0000000..53b4118 --- /dev/null +++ b/src/plugin-api/deviced/include/syscommon-plugin-deviced-common-interface.h @@ -0,0 +1,42 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __SYSCOMMON_PLUGIN_DEVICED_COMMON_INTERFACE_H__ +#define __SYSCOMMON_PLUGIN_DEVICED_COMMON_INTERFACE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +enum deviced_resource_type { + DEVICED_RESOURCE_TYPE_UNKNOWN, + DEVICED_RESOURCE_TYPE_POWER, + DEVICED_RESOURCE_TYPE_END, +}; + +#ifdef __cplusplus +} +#endif + +#endif /*__SYSCOMMON_PLUGIN_DEVICED_COMMON_INTERFACE_H__ */ diff --git a/src/plugin-api/deviced/include/syscommon-plugin-deviced-power-interface.h b/src/plugin-api/deviced/include/syscommon-plugin-deviced-power-interface.h new file mode 100644 index 0000000..010639b --- /dev/null +++ b/src/plugin-api/deviced/include/syscommon-plugin-deviced-power-interface.h @@ -0,0 +1,79 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __SYSCOMMON_PLUGIN_DEVICED_POWER_INTERFACE_H__ +#define __SYSCOMMON_PLUGIN_DEVICED_POWER_INTERFACE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define DEVICED_POWER_ATTR_SET_UINT64_4_CURRENT_STATE (1ULL << 0) +#define DEVICED_POWER_ATTR_UINT64_CURRENT_STATE (1ULL << 1) +#define DEVICED_POWER_ATTR_INT_WAKEUP_REASON (1ULL << 2) + +enum { + DEVICED_POWER_STATE_MIN_INDEX, + DEVICED_POWER_STATE_START_INDEX = DEVICED_POWER_STATE_MIN_INDEX, + DEVICED_POWER_STATE_NORMAL_INDEX, + DEVICED_POWER_STATE_SLEEP_INDEX, + DEVICED_POWER_STATE_POWEROFF_INDEX, + DEVICED_POWER_STATE_REBOOT_INDEX, + DEVICED_POWER_STATE_EXIT_INDEX, + DEVICED_POWER_STATE_MAX_INDEX, + + DEVICED_POWER_TRANSIENT_STATE_MIN_INDEX, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY_INDEX = DEVICED_POWER_TRANSIENT_STATE_MIN_INDEX, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_INDEX, + DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE_INDEX, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY_INDEX, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_INDEX, + DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE_INDEX, + DEVICED_POWER_TRANSIENT_STATE_MAX_INDEX, +}; + +#define DEVICED_POWER_STATE_UNDEFINED (0) + +#define DEVICED_POWER_STATE_START (1ULL << DEVICED_POWER_STATE_START_INDEX) +#define DEVICED_POWER_STATE_NORMAL (1ULL << DEVICED_POWER_STATE_NORMAL_INDEX) +#define DEVICED_POWER_STATE_SLEEP (1ULL << DEVICED_POWER_STATE_SLEEP_INDEX) +#define DEVICED_POWER_STATE_POWEROFF (1ULL << DEVICED_POWER_STATE_POWEROFF_INDEX) +#define DEVICED_POWER_STATE_REBOOT (1ULL << DEVICED_POWER_STATE_REBOOT_INDEX) +#define DEVICED_POWER_STATE_EXIT (1ULL << DEVICED_POWER_STATE_EXIT_INDEX) +#define DEVICED_POWER_STATE_ALL ((1ULL << DEVICED_POWER_STATE_MAX_INDEX) - (1ULL << DEVICED_POWER_STATE_MIN_INDEX)) + +#define DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY (1ULL << DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_SUSPENDING (1ULL << DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE (1ULL << DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY (1ULL << DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_RESUMING (1ULL << DEVICED_POWER_TRANSIENT_STATE_RESUMING_INDEX) +#define DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE (1ULL << DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE_INDEX) + +#define DEVICED_POWER_STATE_INDEX(state) (__builtin_ctzll(state)) + +#ifdef __cplusplus +} +#endif + +#endif /*__SYSCOMMON_PLUGIN_DEVICED_POWER_INTERFACE_H__ */ diff --git a/src/plugin-api/deviced/include/syscommon-plugin-deviced-power.h b/src/plugin-api/deviced/include/syscommon-plugin-deviced-power.h new file mode 100644 index 0000000..0ae2223 --- /dev/null +++ b/src/plugin-api/deviced/include/syscommon-plugin-deviced-power.h @@ -0,0 +1,36 @@ +/** + * MIT License + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __SYSCOMMON_PLUGIN_DEVICED_POWER_H__ +#define __SYSCOMMON_PLUGIN_DEVICED_POWER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /*__SYSCOMMON_PLUGIN_DEVICED_POWER_H__ */ -- 2.7.4 From 40be16ec3e93f3beed0cbe3ffe2ea4c51558b69d Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 1 Aug 2023 14:00:08 +0900 Subject: [PATCH 13/16] resource-manager: Add syscommon_resman_get_resource_id() It is necessary that a plugin know about resource_id so that the plugin invoke resource-manager functions that works on top of resource_id. To this end, added getter for resource_id. - syscommon_resman_get_resource_id() The function gets resource_id of the given type. The id is resource instance created first. - syscommon_resman_get_number_of_instance() The function gets how may resource instances have been created on a specific resource type. To support these operations, the resource-manager internally manages hashtable dedicated to reosurce_id. The hashtable maps resource_type into head of resource_id list. Change-Id: I9d0df9a098444af506a94a81d14fae0f20bc1e04 Signed-off-by: Youngjae Cho --- include/libsyscommon/resource-manager.h | 2 + src/resource-manager/resource-manager.c | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/libsyscommon/resource-manager.h b/include/libsyscommon/resource-manager.h index d89f08b..1df9244 100644 --- a/include/libsyscommon/resource-manager.h +++ b/include/libsyscommon/resource-manager.h @@ -177,6 +177,8 @@ syscommon_resman_resource_attr_supported_always(int resource_id, int syscommon_resman_monitor_get_resource_attrs_json(int resource_id, char **json_string); int syscommon_resman_monitor_get_resource_attr_json(int resource_id, u_int64_t attr_id, char **json_string); int syscommon_resman_get_resource_list_json(char **json_string); +int syscommon_resman_get_number_of_resource_instance(int resource_type, int *n_instance); +int syscommon_resman_get_resource_id(int resource_type, int *resource_id); int syscommon_resman_get_resource_attr_int(int resource_id, u_int64_t attr_id, int32_t *data); int syscommon_resman_get_resource_attr_int64(int resource_id, u_int64_t attr_id, int64_t *data); diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 25817ce..03737cb 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -75,8 +75,25 @@ static int unset_resource_attr_interest(struct syscommon_resman_resource *resour static bool is_resource_attr_interested(struct syscommon_resman_resource *resource, u_int64_t interest_mask); static GList *g_resource_driver_head; + +/** + * key: resource_id, value: struct syscommon_resman_resource + */ static GHashTable *g_resource_hash_table; +/** + * key: resource_type, value: head of resource_id list + */ +static GHashTable *g_resource_id_list_hash_table; + +static GList** find_resource_id_list(int resource_type) +{ + if (!g_resource_id_list_hash_table) + return NULL; + + return g_hash_table_lookup(g_resource_id_list_hash_table, GINT_TO_POINTER(resource_type)); +} + static int clear_sign_bit(unsigned int val) { return (int)((val << 1) >> 1); @@ -128,6 +145,7 @@ static void free_resource(struct syscommon_resman_resource *resource) static void delete_resource(void *data) { + GList **resource_id_list = NULL; struct syscommon_resman_resource *resource = (struct syscommon_resman_resource *) data; if (!resource) @@ -136,11 +154,20 @@ static void delete_resource(void *data) if (resource->driver && resource->driver->ops.delete) resource->driver->ops.delete(resource->id); + resource_id_list = find_resource_id_list(resource->resource_type); + if (resource_id_list) { + *resource_id_list = g_list_remove(*resource_id_list, GINT_TO_POINTER(resource->id)); + if (*resource_id_list == NULL) + g_hash_table_remove(g_resource_id_list_hash_table, resource_id_list); + } + free_resource(resource); } static int add_resource(struct syscommon_resman_resource *resource) { + GList **resource_id_list = NULL; + if (!resource) return -EINVAL; @@ -150,7 +177,22 @@ static int add_resource(struct syscommon_resman_resource *resource) return -ENOMEM; } + if (!g_resource_id_list_hash_table) { + g_resource_id_list_hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, free); + if (!g_resource_id_list_hash_table) + return -ENOMEM; + } + + resource_id_list = find_resource_id_list(resource->resource_type); + if (!resource_id_list) { + resource_id_list = (GList **) calloc(1, sizeof(void *)); + if (!resource_id_list) + return -ENOMEM; + g_hash_table_insert(g_resource_id_list_hash_table, GINT_TO_POINTER(resource->resource_type), resource_id_list); + } + g_hash_table_insert(g_resource_hash_table, GINT_TO_POINTER(resource->id), resource); + *resource_id_list = g_list_append(*resource_id_list, GINT_TO_POINTER(resource->id)); return 0; } @@ -188,6 +230,32 @@ syscommon_resman_find_resource_driver(int resource_type) return (struct syscommon_resman_resource_driver *)node->data; } +int syscommon_resman_get_number_of_resource_instance(int resource_type, int *n_instance) +{ + GList **resource_id_list = NULL; + + resource_id_list = find_resource_id_list(resource_type); + if (!resource_id_list) + *n_instance = 0; + else + *n_instance = g_list_length(*resource_id_list); + + return 0; +} + +int syscommon_resman_get_resource_id(int resource_type, int *resource_id) +{ + GList **resource_id_list = NULL; + + resource_id_list = find_resource_id_list(resource_type); + if (!resource_id_list) + return -EINVAL; + + *resource_id = GPOINTER_TO_INT(g_list_nth_data(*resource_id_list, 0)); + + return 0; +} + void syscommon_resman_add_resource_driver(const struct syscommon_resman_resource_driver *driver) { -- 2.7.4 From 8353a00ba8d7a64f7877a658c7acf640bc8dd071 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Tue, 1 Aug 2023 17:56:28 +0900 Subject: [PATCH 14/16] resource-manager: Fix BIT() to SYSCOMMON_BIT() The BIT() is too general so that it is possible to collide with other header when it is imported. Therefore, added prefix for the macro. Change-Id: Idc999d6c09364bfc9fbc0dc93fb63c1d5eeea995 Signed-off-by: Youngjae Cho --- include/libsyscommon/resource-type.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/libsyscommon/resource-type.h b/include/libsyscommon/resource-type.h index 5d2f64d..5aa3608 100644 --- a/include/libsyscommon/resource-type.h +++ b/include/libsyscommon/resource-type.h @@ -38,7 +38,7 @@ enum syscommon_resman_data_type { SYSCOMMON_RESMAN_DATA_TYPE_NUM }; -#define BIT(x) (1ULL << x) +#define SYSCOMMON_BIT(x) (1ULL << x) /* * Resource driver flags @@ -47,15 +47,15 @@ enum syscommon_resman_data_type { * - RESOURCE_DRIVER_FLAG_UNCOUNTABLE incidates that resource which is not * bound physical h/w. */ -#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE BIT(0) -#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_UNCOUNTABLE BIT(1) +#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE SYSCOMMON_BIT(0) +#define SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_UNCOUNTABLE SYSCOMMON_BIT(1) /* Resource flags */ -#define SYSCOMMON_RESMAN_RESOURCE_FLAG_PRIVATE BIT(0) -#define SYSCOMMON_RESMAN_RESOURCE_FLAG_PUBLIC BIT(1) +#define SYSCOMMON_RESMAN_RESOURCE_FLAG_PRIVATE SYSCOMMON_BIT(0) +#define SYSCOMMON_RESMAN_RESOURCE_FLAG_PUBLIC SYSCOMMON_BIT(1) /* Flags for resource_attribute */ -#define SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE BIT(0) -#define SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC BIT(1) +#define SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE SYSCOMMON_BIT(0) +#define SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC SYSCOMMON_BIT(1) #endif -- 2.7.4 From 159352956d488574e4ae6b8212139d09276ad583 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 2 Aug 2023 16:57:51 +0900 Subject: [PATCH 15/16] resource-manager: Fix bug of set_resource_attr_value_data() It must check existence of setter instead of getter. Change-Id: I1e34647390257ba1ac0c580b35bf1934eeb10d18 Signed-off-by: Youngjae Cho --- src/resource-manager/resource-manager.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index 03737cb..c1662d6 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1256,7 +1256,7 @@ set_resource_attr_value_data(struct syscommon_resman_resource *resource, return -EINVAL; attr = get_resource_attr(resource, attr_id); - if (!attr || !attr->ops.get) + if (!attr || !attr->ops.set) return -EINVAL; attr_value = get_resource_attr_value(resource, attr_id); -- 2.7.4 From 14d78d66a5e28d28d1978e0fddd46894fd3c6160 Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Wed, 2 Aug 2023 17:26:17 +0900 Subject: [PATCH 16/16] resource-manager: Add N-tuple setter to pass multiple parameter Add N-tuple setter function of resource manager to support the case of when some attribute requires the multi paramters as setter. [Newly added setter of resource-manager] - int syscommon_resman_set_resource_attr_uint64_2( int resource_id, u_int64_t attr_id, u_int64_t data1, u_int64_t data2) : It pass the two parameter to the specific attribute which requires the two parameter as setter. - int syscommon_resman_set_resource_attr_uint64_3( int resource_id, u_int64_t attr_id, u_int64_t data1, u_int64_t data2, u_int64_t data3) : It pass the three parameter to the specific attribute which requires the three parameter as setter. - int syscommon_resman_set_resource_attr_uint64_4( int resource_id, u_int64_t attr_id, u_int64_t data1, u_int64_t data2, u_int64_t data3, u_int64_t data4) : It pass the four parameter to the specific attribute which requires the four parameter as setter. [Newly added data type of resource manager] - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64 : This data type include the two of u_int64 data type - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64 : This data type include the three of u_int64 data type - SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64 : This data type include the four of u_int64 data type Change-Id: I8c659e5a9f5447e7aecb17c71005e5fa939960e8 Signed-off-by: Chanwoo Choi Signed-off-by: Youngjae Cho --- include/libsyscommon/resource-manager.h | 19 ++++ include/libsyscommon/resource-type.h | 4 + src/resource-manager/resource-manager.c | 166 +++++++++++++++++++++++++++++++- 3 files changed, 188 insertions(+), 1 deletion(-) diff --git a/include/libsyscommon/resource-manager.h b/include/libsyscommon/resource-manager.h index 1df9244..ddf70b4 100644 --- a/include/libsyscommon/resource-manager.h +++ b/include/libsyscommon/resource-manager.h @@ -52,6 +52,18 @@ struct syscommon_resman_resource_attribute_ops { int (*set)(int resource_id, const struct syscommon_resman_resource_attribute *attr, const void *data, int count); + int (*set_2_tuple)(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + const void *data1, const void *data2, + int count1, int count2); + int (*set_3_tuple)(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + const void *data1, const void *data2, const void *data3, + int count1, int count2, int count3); + int (*set_4_tuple)(int resource_id, + const struct syscommon_resman_resource_attribute *attr, + const void *data1, const void *data2, const void *data3, const void *data4, + int count1, int count2, int count3, int count4); int (*get)(int resource_id, const struct syscommon_resman_resource_attribute *attr, void *data); @@ -207,6 +219,13 @@ int syscommon_resman_set_resource_attr_string(int resource_id, u_int64_t attr_id int syscommon_resman_set_resource_attr_array(int resource_id, u_int64_t attr_id, void *data, int count); int syscommon_resman_set_resource_attr_ptr(int resource_id, u_int64_t attr_id, void *data); +int syscommon_resman_set_resource_attr_uint64_2(int resource_id, u_int64_t attr_id, + u_int64_t data1, u_int64_t data2); +int syscommon_resman_set_resource_attr_uint64_3(int resource_id, u_int64_t attr_id, + u_int64_t data1, u_int64_t data2, u_int64_t data3); +int syscommon_resman_set_resource_attr_uint64_4(int resource_id, u_int64_t attr_id, + u_int64_t data1, u_int64_t data2, u_int64_t data3, u_int64_t data4); + int syscommon_resman_set_resource_attr_interest(int resource_id, u_int64_t interest_mask); int syscommon_resman_unset_resource_attr_interest(int resource_id, u_int64_t interest_mask); bool syscommon_resman_is_resource_attr_interested(int resource_id, u_int64_t interest_mask); diff --git a/include/libsyscommon/resource-type.h b/include/libsyscommon/resource-type.h index 5aa3608..abb460a 100644 --- a/include/libsyscommon/resource-type.h +++ b/include/libsyscommon/resource-type.h @@ -35,6 +35,10 @@ enum syscommon_resman_data_type { SYSCOMMON_RESMAN_DATA_TYPE_ARRAY, SYSCOMMON_RESMAN_DATA_TYPE_PTR, SYSCOMMON_RESMAN_DATA_TYPE_BOOLEAN, + + SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64, + SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64, SYSCOMMON_RESMAN_DATA_TYPE_NUM }; diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index c1662d6..79c1daa 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -1302,6 +1302,116 @@ set_resource_attr_value_data(struct syscommon_resman_resource *resource, return 0; } +static int +set_resource_attr_value_data_2_tuple(struct syscommon_resman_resource *resource, + u_int64_t attr_id, enum syscommon_resman_data_type type, + const void *data1, const void *data2, + int count1, int count2) +{ + const struct syscommon_resman_resource_attribute *attr = NULL; + int ret; + + if (!data1 || !data2) + return -EINVAL; + + if (!check_attr_validate(resource, attr_id, type)) + return -EINVAL; + + attr = get_resource_attr(resource, attr_id); + if (!attr) + return -EINVAL; + + if (!attr->ops.set_2_tuple) + return -ENOTSUP; + + ret = attr->ops.set_2_tuple(resource->id, attr, + data1, data2, count1, count2); + if (ret < 0) + return ret; + + /** + * FIXME: N-tuple data doesn't support the getter temporarily. + * So that don't store the passed data to attr_value. + * I will be implemented for getter on later. + */ + + return 0; +} + +static int +set_resource_attr_value_data_3_tuple(struct syscommon_resman_resource *resource, + u_int64_t attr_id, enum syscommon_resman_data_type type, + const void *data1, const void *data2, const void *data3, + int count1, int count2, int count3) +{ + const struct syscommon_resman_resource_attribute *attr = NULL; + int ret; + + if (!data1 || !data2 || !data3) + return -EINVAL; + + if (!check_attr_validate(resource, attr_id, type)) + return -EINVAL; + + attr = get_resource_attr(resource, attr_id); + if (!attr) + return -EINVAL; + + if (!attr->ops.set_3_tuple) + return -ENOTSUP; + + ret = attr->ops.set_3_tuple(resource->id, attr, + data1, data2, data3, + count1, count2, count3); + if (ret < 0) + return ret; + + /** + * FIXME: N-tuple data doesn't support the getter temporarily. + * So that don't store the passed data to attr_value. + * I will be implemented for getter on later. + */ + + return 0; +} + +static int +set_resource_attr_value_data_4_tuple(struct syscommon_resman_resource *resource, + u_int64_t attr_id, enum syscommon_resman_data_type type, + const void *data1, const void *data2, const void *data3, const void *data4, + int count1, int count2, int count3, int count4) +{ + const struct syscommon_resman_resource_attribute *attr = NULL; + int ret; + + if (!data1 || !data2 || !data3 || !data4) + return -EINVAL; + + if (!check_attr_validate(resource, attr_id, type)) + return -EINVAL; + + attr = get_resource_attr(resource, attr_id); + if (!attr) + return -EINVAL; + + if (!attr->ops.set_4_tuple) + return -ENOTSUP; + + ret = attr->ops.set_4_tuple(resource->id, attr, + data1, data2, data3, data4, + count1, count2, count3, count4); + if (ret < 0) + return ret; + + /** + * FIXME: N-tuple data doesn't support the getter temporarily. + * So that don't store the passed data to attr_value. + * I will be implemented for getter on later. + */ + + return 0; +} + int syscommon_resman_set_resource_attr_int(int resource_id, u_int64_t attr_id, int32_t data) { @@ -1390,6 +1500,48 @@ syscommon_resman_set_resource_attr_ptr(int resource_id, u_int64_t attr_id, void return set_resource_attr_value_data(resource, attr_id, SYSCOMMON_RESMAN_DATA_TYPE_PTR, &data, 1); } +int +syscommon_resman_set_resource_attr_uint64_2(int resource_id, u_int64_t attr_id, + u_int64_t data1, u_int64_t data2) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) + return -EINVAL; + + return set_resource_attr_value_data_2_tuple(resource, attr_id, + SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64, + &data1, &data2, 1, 1); +} + +int +syscommon_resman_set_resource_attr_uint64_3(int resource_id, u_int64_t attr_id, + u_int64_t data1, u_int64_t data2, u_int64_t data3) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) + return -EINVAL; + + return set_resource_attr_value_data_3_tuple(resource, attr_id, + SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64, + &data1, &data2, &data3, 1, 1, 1); +} + +int +syscommon_resman_set_resource_attr_uint64_4(int resource_id, u_int64_t attr_id, + u_int64_t data1, u_int64_t data2, u_int64_t data3, u_int64_t data4) +{ + struct syscommon_resman_resource *resource = find_resource(resource_id); + + if (!is_valid_resource(resource, INSTANCE_TYPE_NORMAL)) + return -EINVAL; + + return set_resource_attr_value_data_4_tuple(resource, attr_id, + SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64, + &data1, &data2, &data3, &data4, 1, 1, 1, 1); +} + static inline bool is_resource_attr_visible(struct syscommon_resman_resource *resource, const struct syscommon_resman_resource_attribute *attr) @@ -1412,6 +1564,7 @@ set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t struct syscommon_resman_resource_attribute_value *attr_value; int i, ret; bool supported; + bool setter_only = false; if (!resource) return -EINVAL; @@ -1474,12 +1627,23 @@ set_resource_attr_interest(struct syscommon_resman_resource *resource, u_int64_t case SYSCOMMON_RESMAN_DATA_TYPE_ARRAY: attr_value->data = calloc(1, sizeof(struct syscommon_resman_array_value)); break; + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64: + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64: + case SYSCOMMON_RESMAN_DATA_TYPE_UINT64_UINT64_UINT64_UINT64: + /** + * FIXME: N-tuple data doesn't support the getter temporarily. + * Don't store the passed data to attr_value. + * So that don't allocate the memory for attr_value->data. + * I will be implemented on later. + */ + setter_only = true; + break; default: ret = -EINVAL; goto err; } - if (!attr_value->data) { + if (!setter_only && !attr_value->data) { ret = -ENOMEM; goto err; } -- 2.7.4