d5b6507853c1f479f3f3d0762b3de0e72a8d9c4c
[platform/core/system/pass.git] / src / resource / resource-network.c
1 /*
2  * PASS (Power Aware System Service) - Network Resource Driver
3  *
4  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
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
25  */
26
27 #include <glib.h>
28
29 #include <util/common.h>
30 #include <util/devices.h>
31 #include <util/log.h>
32
33 #include <libsyscommon/resource-manager.h>
34 #include <libsyscommon/resource-type.h>
35 #include <libsyscommon/resource-device.h>
36
37 #include <resource-monitor/resource-monitor.h>
38
39 struct network_context {
40         char *device_name;
41         int index;
42 };
43
44 static int network_get_name(struct syscommon_resman_resource *res,
45                                 const struct syscommon_resman_resource_attribute *attr,
46                                 void *data)
47 {
48         struct network_context *ctx;
49         char *buf = (char *)data;
50
51         if (!res || !attr || !data)
52                 return -EINVAL;
53
54         ctx = syscommon_resman_get_resource_privdata(res);
55         if (!ctx)
56                 return -EINVAL;
57
58         if (!ctx->device_name) {
59                 _E("%s: NETWORK_CTRL_DEVICE_ID is not yet initialized\n",
60                                 syscommon_resman_get_resource_name(res));
61                 return -EINVAL;
62         }
63
64         strncpy(buf, ctx->device_name, BUFF_MAX);
65
66         return 0;
67 }
68
69 static const struct syscommon_resman_resource_attribute network_attrs[] = {
70         {
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,
75                 .ops    = {
76                         .get = network_get_name,
77                 },
78         },
79 };
80
81 static int network_setup_device_id(struct syscommon_resman_resource *res,
82                                 const struct syscommon_resman_resource_control *ctrl,
83                                 const void *data)
84 {
85         struct network_context *ctx;
86         const struct syscommon_resman_resource_device *device;
87         int resource_index = (int)(intptr_t)data;
88
89         if (!res || !ctrl)
90                 return -EINVAL;
91
92         ctx = syscommon_resman_get_resource_privdata(res);
93         if (!ctx)
94                 return -EINVAL;
95
96         device = syscommon_resman_find_resource_device(syscommon_resman_get_resource_type(res), resource_index);
97         if (!device) {
98                 _E("Not available resource: type: %s, index: %d\n",
99                                 syscommon_resman_get_resource_name(res), resource_index);
100                 return -EINVAL;
101         }
102
103         if (ctx->device_name)
104                 free(ctx->device_name);
105
106         ctx->device_name = g_strdup(device->name);
107         ctx->index = resource_index;
108
109         return 0;
110 }
111
112 static const struct syscommon_resman_resource_control network_ctrls[] = {
113         {
114                 .name = "NETWORK_CTRL_DEVICE_ID",
115                 .id = NETWORK_CTRL_DEVICE_ID,
116                 .ops = {
117                         .set = network_setup_device_id,
118                 },
119         },
120 };
121
122 static int network_create(struct syscommon_resman_resource *res)
123 {
124         struct network_context *ctx;
125
126         ctx = calloc(1, sizeof(struct network_context));
127         if (!ctx)
128                 return -ENOMEM;
129
130         ctx->index = -1;
131
132         syscommon_resman_set_resource_privdata(res, ctx);
133
134         return 0;
135 }
136
137 static void network_delete(struct syscommon_resman_resource *res)
138 {
139         struct network_context *ctx;
140
141         if (!res)
142                 return;
143
144         ctx = syscommon_resman_get_resource_privdata(res);
145         if (!ctx)
146                 return;
147
148         if (ctx->device_name)
149                 free(ctx->device_name);
150
151         free(ctx);
152         syscommon_resman_set_resource_privdata(res, NULL);
153 }
154
155 static const struct syscommon_resman_resource_driver network_resource_driver = {
156         .name           = "NETWORK",
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),
162         .ops            = {
163                 .create = network_create,
164                 .delete = network_delete,
165         },
166 };
167 SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&network_resource_driver)