plugin-api: deviced: Add interface for detecting setting value change 70/312870/1
authorYoungjae Cho <y0.cho@samsung.com>
Wed, 5 Jun 2024 07:08:15 +0000 (16:08 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Mon, 17 Jun 2024 04:54:57 +0000 (13:54 +0900)
The function has been added as display plugin operation
 : on_changed_setting_value(int key, int value)

It is invoked when setting app changes display setting such as
screen timeout or brightness. The parameters are saying that which
'key' has changed to what 'value'.

Change-Id: Id904488c3fb393a648618d4fd30df2d283dfa6ef
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display-interface.h
src/plugin-api/deviced/include/system/syscommon-plugin-deviced-display.h
src/plugin-api/deviced/src/syscommon-plugin-deviced-display.c
tests/CMakeLists.txt
tests/plugin-api/deviced/test-plugin-display.c [new file with mode: 0644]
tests/plugin-api/deviced/test.c [new file with mode: 0644]
tests/test-mock.c
tests/test-mock.h

index caf54142a7b047a152e34e10ac7a3376499a2fd1..5b5d266fd8ca72a9d1f1423ea1cd1612e7a37716 100644 (file)
@@ -140,6 +140,7 @@ enum syscommon_deviced_display_capability {
 
 typedef struct _syscommon_plugin_backend_deviced_display_funcs {
        int (*load_display_config) (struct syscommon_deviced_display_config **);
+       int (*on_changed_setting_value) (int, int);
 } syscommon_plugin_backend_deviced_display_funcs;
 
 #ifdef __cplusplus
index a999c9563fdee68739f11eb0e984d5721ffd5495..67b003adbadcdccde69a0769cc1d406ffc25d58b 100644 (file)
@@ -45,11 +45,20 @@ int syscommon_plugin_deviced_display_get_backend(void);
 int syscommon_plugin_deviced_display_put_backend(void);
 
 /**
- * @brief Load the backend config of deviced-displaymodule
+ * @brief Load the backend config of deviced-display module
  * @return @c 0 on success, otherwise a negative error value
  */
 int syscommon_plugin_deviced_display_load_config(struct syscommon_deviced_display_config **);
 
+/**
+ * @brief Let plugin know about setting value change
+ * @param[in] key Key of which setting value has changed
+ * @param[in] value Value that a key has changed to
+ * @return @c 0 on success, otherwise a negative error value
+ */
+
+int syscommon_plugin_deviced_display_notify_setting_value_changed(int key, int value);
+
 #ifdef __cplusplus
 }
 #endif
index 57495fbfe8c325a519c018e1eb73b83e514142c7..0f49ec553897e8a3d832ff097884577e5ae4e8a5 100644 (file)
@@ -22,6 +22,7 @@
  * THE SOFTWARE.
  */
 #include <assert.h>
+#include <errno.h>
 
 #include <glib.h>
 
@@ -95,3 +96,22 @@ int syscommon_plugin_deviced_display_load_config(struct syscommon_deviced_displa
 
        return g_display_funcs->load_display_config(data);
 }
+
+EXPORT
+int syscommon_plugin_deviced_display_notify_setting_value_changed(int key, int value)
+{
+       int ret = 0;
+
+       if (!g_display_funcs) {
+               ret = syscommon_plugin_deviced_display_get_backend();
+               if (ret < 0)
+                       return ret;
+       }
+
+       assert(g_display_funcs);
+
+       if (!g_display_funcs->on_changed_setting_value)
+               return -EOPNOTSUPP;
+
+       return g_display_funcs->on_changed_setting_value(key, value);
+}
index ae480bf5d8cf5ea3018d24c1cc1b7cc64f1cbb8d..e53ee04e18738739c686c88d23a2324334cf10a8 100644 (file)
@@ -1,6 +1,8 @@
 SET(TEST_DRIVER "test-driver")
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/plugin-api/common/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/plugin-api/deviced/include)
 
 FILE(GLOB TEST_DRIVER_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
 FILE(GLOB LIBCOMMON_SRCS
@@ -9,14 +11,25 @@ FILE(GLOB LIBCOMMON_SRCS
 FILE(GLOB RESOURCE_MANAGER_SRCS
        "${CMAKE_SOURCE_DIR}/tests/resource-manager/*.c"
        "${CMAKE_SOURCE_DIR}/src/resource-manager/*.c")
+FILE(GLOB PLUGIN_API_COMMON_SRCS
+       "${CMAKE_SOURCE_DIR}/src/plugin-api/common/src/*.c")
+FILE(GLOB PLUGIN_API_DEVICED_SRCS
+       "${CMAKE_SOURCE_DIR}/tests/plugin-api/deviced/*.c"
+       "${CMAKE_SOURCE_DIR}/src/plugin-api/deviced/src/*.c")
 
 SET(SRCS
        ${TEST_DRIVER_SRCS}
        ${LIBCOMMON_SRCS}
-       ${RESOURCE_MANAGER_SRCS})
+       ${RESOURCE_MANAGER_SRCS}
+       ${PLUGIN_API_COMMON_SRCS}
+       ${PLUGIN_API_DEVICED_SRCS})
 
 SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open")
 SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64")
+SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=dlopen")
+SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=dlclose")
+SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=dlsym")
+SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=access")
 
 INCLUDE(FindPkgConfig)
 PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED
@@ -26,7 +39,7 @@ PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED
        cmocka)
 
 ADD_EXECUTABLE(${TEST_DRIVER} ${SRCS})
-TARGET_LINK_LIBRARIES(${TEST_DRIVER} ${REQUIRED_PKGS_LDFLAGS})
+TARGET_LINK_LIBRARIES(${TEST_DRIVER} "${REQUIRED_PKGS_LDFLAGS} -ldl")
 # Suppress implicit declaration warning for __real functions.
 SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES COMPILE_FLAGS "-Wno-implicit-function-declaration")
 SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "${WRAP_FLAGS} -Wl,--export-dynamic")
diff --git a/tests/plugin-api/deviced/test-plugin-display.c b/tests/plugin-api/deviced/test-plugin-display.c
new file mode 100644 (file)
index 0000000..4af264f
--- /dev/null
@@ -0,0 +1,65 @@
+#include <system/syscommon-plugin-common-interface.h>
+#include <system/syscommon-plugin-deviced-common-interface.h>
+#include <system/syscommon-plugin-deviced-display.h>
+#include <system/syscommon-plugin-deviced-display-interface.h>
+
+#include "../../test-main.h"
+
+#define EXPORT __attribute__ ((visibility("default")))
+
+static struct syscommon_deviced_display_config configurations = {
+       0, /* define plugin own configurations */ };
+
+static int load_display_config(struct syscommon_deviced_display_config **data)
+{
+       if (!data)
+               return -EINVAL;
+
+       *data = &configurations;
+
+       return 0;
+}
+
+static int on_changed_setting_value(int key, int value)
+{
+       check_expected(key);
+
+       switch (key) {
+               case 1:
+                       check_expected(value);
+                       // do something
+                       break;
+               case 2:
+                       check_expected(value);
+                       // do something
+                       break;
+               default:
+                       break;
+       }
+
+       return 0;
+}
+
+static syscommon_plugin_backend_deviced_display_funcs g_display_funcs = {
+       .load_display_config = load_display_config,
+       .on_changed_setting_value = on_changed_setting_value,
+};
+
+static int deviced_display_init(void **data)
+{
+       *data = (void *)&g_display_funcs;
+
+       return 0;
+}
+
+static int deviced_display_exit(void *data)
+{
+       return 0;
+}
+
+syscommon_plugin_backend EXPORT system_plugin_backend_deviced_display_data = {
+       .name = "test-deviced-display",
+       .vendor = "Samsung",
+       .init = deviced_display_init,
+       .exit = deviced_display_exit,
+};
diff --git a/tests/plugin-api/deviced/test.c b/tests/plugin-api/deviced/test.c
new file mode 100644 (file)
index 0000000..112d998
--- /dev/null
@@ -0,0 +1,48 @@
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include "../../test-main.h"
+#include "../../test-mock.h"
+
+#include <system/syscommon-plugin-common-interface.h>
+
+extern syscommon_plugin_backend system_plugin_backend_deviced_display_data;
+
+static int setup_plugin_deviced(void **state)
+{
+       int ret;
+
+       mock_set_plugin_funcs(&system_plugin_backend_deviced_display_data);
+
+       ret = syscommon_plugin_deviced_display_get_backend();
+       assert_int_equal(ret, 0);
+
+       return 0;
+}
+
+static int teardown_plugin_deviced(void **state)
+{
+       syscommon_plugin_deviced_display_put_backend();
+
+       mock_unset_plugin_funcs();
+
+       return 0;
+}
+
+static void test_notify_setting_value_changed(void **state)
+{
+       int ret;
+
+       expect_value(on_changed_setting_value, key, 1);
+       expect_value(on_changed_setting_value, value, 1);
+
+       ret = syscommon_plugin_deviced_display_notify_setting_value_changed(1, 1);
+       assert_int_equal(ret, 0);
+}
+
+static const struct CMUnitTest testsuite_plugin_api_deviced[] = {
+       cmocka_unit_test(test_notify_setting_value_changed),
+};
+TESTSUITE_FIXTURE(testsuite_plugin_api_deviced, setup_plugin_deviced, teardown_plugin_deviced)
index 5dffc7cecd051d2ad3195f1f31c48bc7b9f9d481..84e60fad704d83abe91f6183fa7a8e0afd8dbee9 100644 (file)
@@ -1,6 +1,8 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <dlfcn.h>
 #include <unistd.h>
 
 #include "test-main.h"
@@ -8,6 +10,13 @@
 
 //static FILE *__fp = NULL;
 
+static void *plugin_funcs = NULL;
+
+int __wrap_access(const char *pathname, int mode)
+{
+       return 0;
+}
+
 int __wrap_open(const char *pathname, int flags)
 {
        const char *mock_pathname;
@@ -35,6 +44,43 @@ void mock_open(const char *pathname, int flags)
        will_return(__wrap_open, flags);
 }
 
+void mock_set_plugin_funcs(void *funcs)
+{
+       plugin_funcs = funcs;
+}
+
+void mock_unset_plugin_funcs(void)
+{
+       plugin_funcs = NULL;
+}
+
+extern void* __real_dlopen(const char *filename, int flags);
+void *__wrap_dlopen(const char *filename, int flags)
+{
+       if (plugin_funcs)
+               return (void *)(intptr_t) 1;
+
+       return __real_dlopen(filename, flags);
+}
+
+extern int __real_dlclose(void *handle);
+int __wrap_dlclose(void *handle)
+{
+       if (plugin_funcs)
+               return 0;
+
+       return __real_dlclose(handle);
+}
+
+extern void* __real_dlsym(void *handle, const char *symbol);
+void *__wrap_dlsym(void *handle, const char *symbol)
+{
+       if (plugin_funcs)
+               return plugin_funcs;
+
+       return __real_dlsym(handle, symbol);
+}
+
 /*
 FILE *__wrap_fopen(const char *pathname, const char *mode)
 {
index 22c7196882e35157cb23ba23f9fb1011a0d6ded9..a384aa1c760569a7e235eb76a9dc3c2ced17df37 100644 (file)
@@ -5,4 +5,7 @@
 
 void mock_open(const char *pathname, int flags);
 
+void mock_set_plugin_funcs(void *funcs);
+void mock_unset_plugin_funcs(void);
+
 #endif //__TEST_MOCK_H__