2 * PASS (Power Aware System Service) - Network Resource Driver
4 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * @file resource-network.c
21 * @brief Provide network resource driver supporting the various resource
22 * attributes. It supports the network attributes such as network
23 * bandwidth and so on.
24 * @ingroup RESOURCE_MONITOR
29 #include <util/common.h>
30 #include <util/devices.h>
33 #include <libsyscommon/resource-manager.h>
34 #include <libsyscommon/resource-type.h>
35 #include <libsyscommon/resource-device.h>
37 #include <resource-monitor/resource-monitor.h>
39 struct network_context {
44 static int network_get_name(struct syscommon_resman_resource *res,
45 const struct syscommon_resman_resource_attribute *attr,
48 struct network_context *ctx;
49 char *buf = (char *)data;
51 if (!res || !attr || !data)
54 ctx = syscommon_resman_get_resource_privdata(res);
58 if (!ctx->device_name) {
59 _E("%s: NETWORK_CTRL_DEVICE_ID is not yet initialized\n",
60 syscommon_resman_get_resource_name(res));
64 strncpy(buf, ctx->device_name, BUFF_MAX);
69 static const struct syscommon_resman_resource_attribute network_attrs[] = {
71 .name = "NETWORK_ATTR_NAME",
72 .id = NETWORK_ATTR_NAME,
73 .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING,
74 .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE,
76 .get = network_get_name,
81 static int network_setup_device_id(struct syscommon_resman_resource *res,
82 const struct syscommon_resman_resource_control *ctrl,
85 struct network_context *ctx;
86 const struct syscommon_resman_resource_device *device;
87 int resource_index = (int)(intptr_t)data;
92 ctx = syscommon_resman_get_resource_privdata(res);
96 device = syscommon_resman_find_resource_device(syscommon_resman_get_resource_type(res), resource_index);
98 _E("Not available resource: type: %s, index: %d\n",
99 syscommon_resman_get_resource_name(res), resource_index);
103 if (ctx->device_name)
104 free(ctx->device_name);
106 ctx->device_name = g_strdup(device->name);
107 ctx->index = resource_index;
112 static const struct syscommon_resman_resource_control network_ctrls[] = {
114 .name = "NETWORK_CTRL_DEVICE_ID",
115 .id = NETWORK_CTRL_DEVICE_ID,
117 .set = network_setup_device_id,
122 static int network_create(struct syscommon_resman_resource *res)
124 struct network_context *ctx;
126 ctx = calloc(1, sizeof(struct network_context));
132 syscommon_resman_set_resource_privdata(res, ctx);
137 static void network_delete(struct syscommon_resman_resource *res)
139 struct network_context *ctx;
144 ctx = syscommon_resman_get_resource_privdata(res);
148 if (ctx->device_name)
149 free(ctx->device_name);
152 syscommon_resman_set_resource_privdata(res, NULL);
155 static const struct syscommon_resman_resource_driver network_resource_driver = {
157 .type = RESOURCE_TYPE_NETWORK,
158 .attrs = network_attrs,
159 .num_attrs = ARRAY_SIZE(network_attrs),
160 .ctrls = network_ctrls,
161 .num_ctrls = ARRAY_SIZE(network_ctrls),
163 .create = network_create,
164 .delete = network_delete,
167 SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&network_resource_driver)