From 209e8d258f786e630672fb9b186b0420a94e32a6 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Mon, 31 Jul 2023 16:06:08 +0900 Subject: [PATCH] core: Introduce resource-manager 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 --- CMakeLists.txt | 1 + packaging/deviced.spec | 1 + src/core/main.c | 4 +++ src/core/resource.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/resource.h | 25 ++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 src/core/resource.c create mode 100644 src/core/resource.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 582414a..52ab2f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/packaging/deviced.spec b/packaging/deviced.spec index 938235b..3f7bb9c 100644 --- a/packaging/deviced.spec +++ b/packaging/deviced.spec @@ -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) diff --git a/src/core/main.c b/src/core/main.c index 6c4a616..7c560ad 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -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 index 0000000..837df68 --- /dev/null +++ b/src/core/resource.c @@ -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 + +#include +#include + +#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 index 0000000..18aec49 --- /dev/null +++ b/src/core/resource.h @@ -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__ */ -- 2.7.4