core: Introduce resource-manager 33/296533/2
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 31 Jul 2023 07:06:08 +0000 (16:06 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Tue, 1 Aug 2023 08:40:01 +0000 (17:40 +0900)
The deviced core init/exit resource drivers. The helper functions have
been added for this job.
 - resource_init()
 - resource_exit()

Currently, there can be only a single resource instance by a resource
type. Therefore, the deviced manages resource ids of instances within
an array that each index contains a single integer of resource id.

Change-Id: I8818c64f9bfa70a9751a213330e36c5744a55daa
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
CMakeLists.txt
packaging/deviced.spec
src/core/main.c
src/core/resource.c [new file with mode: 0644]
src/core/resource.h [new file with mode: 0644]

index 582414a..52ab2f5 100644 (file)
@@ -67,6 +67,7 @@ SET(SRCS
        src/control/control.c
        src/core/delayed-init-notifier.c
        src/core/devices.c
+       src/core/resource.c
        src/core/event-handler.c
        src/core/log.c
        src/core/main.c
index 938235b..3f7bb9c 100644 (file)
@@ -35,6 +35,7 @@ BuildRequires:  pkgconfig(minizip)
 BuildRequires:  pkgconfig(libkmod)
 BuildRequires:  pkgconfig(libusbgx)
 BuildRequires:  pkgconfig(libsyscommon)
+BuildRequires:  pkgconfig(libsyscommon-plugin-api-deviced)
 BuildRequires:  pkgconfig(capi-system-device)
 BuildRequires:  pkgconfig(wayland-client)
 BuildRequires:  pkgconfig(tizen-extension-client)
index 6c4a616..7c560ad 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "core.h"
 #include "log.h"
+#include "resource.h"
 #include "shared/common.h"
 #include "shared/devices.h"
 #include "power/power.h"
@@ -131,6 +132,7 @@ static int deviced_main(int argc, char **argv)
        silent_boot = (retval == 0 && strncmp(bootmode, "silent", sizeof("silent")) == 0);
        CRITICAL_LOG("bootmode=%s", bootmode);
 
+       resource_init();
        devices_init(NULL);
 
        ret = gdbus_request_name(handle, DEVICED_BUS_NAME, deviced_dbus_name_acquired, NULL);
@@ -155,6 +157,8 @@ static int deviced_main(int argc, char **argv)
        g_main_loop_unref(mainloop);
 
        devices_exit(NULL);
+       resource_exit();
+
        unload_plugins();
 
        return 0;
diff --git a/src/core/resource.c b/src/core/resource.c
new file mode 100644 (file)
index 0000000..837df68
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+
+#include <libsyscommon/resource-manager.h>
+#include <system/syscommon-plugin-deviced-common-interface.h>
+
+#define RESOURCE_INSTANCE_UNINITIALIZED                (-1)
+/**
+ * Store resource id for each resource type.
+ *
+ * TODO: Currently, there can be only a single instance for each type of resource.
+ *       Therefore, it is enough to manage resource id by a single integer for each
+ *       resource type. If there would be a case that have multiple instance for a
+ *       resource type, however, it is required to fix this data structure to be able
+ *       to hold multiple instances by a type, such as list.
+ */
+static int resource_ids[DEVICED_RESOURCE_TYPE_END];
+
+int resource_init(void)
+{
+       int res_type;
+
+       syscommon_resman_init_resource_drivers();
+
+       for (res_type = DEVICED_RESOURCE_TYPE_UNKNOWN + 1; res_type < DEVICED_RESOURCE_TYPE_END; ++res_type) {
+               int ret;
+               int resource_id;
+
+               /* Initialize id by -1, meaning there is no created resource instance. */
+               resource_ids[res_type] = RESOURCE_INSTANCE_UNINITIALIZED;
+
+               ret = syscommon_resman_create_resource(&resource_id, res_type);
+               if (ret < 0)
+                       continue;
+
+               resource_ids[res_type] = resource_id;
+       }
+
+       return 0;
+}
+
+void resource_exit(void)
+{
+       int res_type;
+
+       for (res_type = DEVICED_RESOURCE_TYPE_UNKNOWN + 1; res_type < DEVICED_RESOURCE_TYPE_END; ++res_type) {
+               if (resource_ids[res_type] == RESOURCE_INSTANCE_UNINITIALIZED)
+                       continue;
+
+               syscommon_resman_delete_resource(resource_ids[res_type]);
+               resource_ids[res_type] = RESOURCE_INSTANCE_UNINITIALIZED;
+       }
+
+       syscommon_resman_exit_resource_drivers();
+}
diff --git a/src/core/resource.h b/src/core/resource.h
new file mode 100644 (file)
index 0000000..18aec49
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __DEVICED_RESOURCE_H__
+#define __DEVICED_RESOURCE_H__
+
+int resource_init(void);
+void resource_exit(void);
+
+#endif /* __DEVICED_RESOURCE_H__ */