resource: Add the new device ops create/delete
[platform/core/system/pass.git] / include / util / resource.h
1 /*
2  * PASS
3  *
4  * Copyright (c) 2021 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 #ifndef __RESOURCE_H__
21 #define __RESOURCE_H__
22
23 #include <sys/time.h>
24 #include <errno.h>
25 #include <glib.h>
26 #include "common.h"
27
28 /* Resource flags */
29 /*
30  * This flags indicates that the resource has the only one resource count
31  * such as system/memory resources.
32  */
33 #define RESOURCE_FLAG_COUNT_ONLY_ONE                    BIT(0)
34
35 /*
36  * This flag incidates that resource is either process or process-group
37  * and is not physical h/w resource.
38  */
39 #define RESOURCE_FLAG_PROCESS                           BIT(1)
40
41 struct resource;
42 struct resource_attribute;
43
44 struct array_value {
45         int type;
46         int length;
47         void *data;
48 };
49
50 struct resource_attribute_value {
51         int type;
52         void *data;
53 };
54
55 struct resource_attribute_ops {
56         int (*set)(struct resource *res,
57                    const struct resource_attribute *attr,
58                    const void *data, int count);
59         int (*get)(struct resource *res,
60                    const struct resource_attribute *attr,
61                    void *data);
62         /*
63          * If .is_supported ops is not implemented, use .get ops in order to
64          * check whether the resource attribute is supported or not.
65          */
66         bool (*is_supported)(struct resource *res,
67                    const struct resource_attribute *attr);
68 };
69
70 struct resource_attribute {
71         const char name[BUFF_MAX];
72         const u_int64_t id;
73         const int type;
74         const struct resource_attribute_ops ops;
75 };
76
77 struct resource_control;
78
79 struct resource_control_ops {
80         const int (*set)(struct resource *res,
81                          const struct resource_control *ctrl,
82                          const void *data);
83         const int (*get)(struct resource *res,
84                          const struct resource_control *ctrl,
85                          void **data);
86 };
87
88 struct resource_control {
89         const char name[BUFF_MAX];
90         const u_int64_t id;
91         const struct resource_control_ops ops;
92 };
93
94 struct resource_driver_ops {
95         int (*init)(void);
96         void (*exit)(void);
97         int (*create)(struct resource *res);
98         void (*delete)(struct resource *res);
99         /*
100          * If prepare_update is specified, it will be called
101          * at every update_resource_attrs().
102          */
103         int (*prepare_update)(struct resource *res);
104 };
105
106 struct resource_driver {
107         const char *name;
108         const int type;
109         const u_int64_t flag;
110         const int num_attrs;
111         const struct resource_attribute *attrs;
112         const int num_ctrls;
113         const struct resource_control *ctrls;
114         const struct resource_driver_ops ops;
115 };
116
117 struct resource_device {
118         char *name;
119         int type;
120
121         /*
122          * Never initialize it by user of add_resource_device().
123          * It will be initialized by add_resource_device function automatically.
124          */
125         int index;
126 };
127
128 #define RESOURCE_DRIVER_REGISTER(resource_driver)       \
129 static void __CONSTRUCTOR__ module_init(void)   \
130 {       \
131         add_resource_driver(resource_driver);   \
132 }       \
133 static void __DESTRUCTOR__ module_exit(void)    \
134 {       \
135         remove_resource_driver(resource_driver);        \
136 }
137
138 /* Add/remove resource driver and device */
139 void add_resource_driver(const struct resource_driver *resource_driver);
140 void remove_resource_driver(const struct resource_driver *resource_driver);
141
142 int get_resource_device_count_all(void);
143 int get_resource_device_count(int resource_type);
144 const struct resource_device *find_resource_device(int resource_type, int resource_index);
145 int add_resource_device(struct resource_device *resource_device);
146 void remove_resource_device(struct resource_device *resource_device);
147
148 /* Create/delete resource instance */
149 struct resource *create_resource(int resource_type);
150 void delete_resource(struct resource *resource);
151
152 /* Handle resource control */
153 int set_resource_control(struct resource *resource, u_int64_t ctrl_id, const void *data);
154 const char *get_resource_control_name(struct resource *resource, u_int64_t ctrl_id);
155
156 /* Handle resource attribute */
157 int update_resource_attrs(struct resource *resource);
158 const struct resource_attribute *get_resource_attr(struct resource *resource, u_int64_t attr_id);
159 struct resource_attribute_value *
160 get_resource_attr_value(struct resource *resource, u_int64_t attr_id);
161 int is_resource_attr_supported(struct resource *resource, u_int64_t attr_id, bool *supported);
162
163 static inline bool
164 resource_attr_supported_always(struct resource *resource,
165                                const struct resource_attribute *attr)
166 {
167         return true;
168 }
169
170 int get_resource_attrs_json(struct resource *resource, char **json_string);
171 int get_resource_attr_json(struct resource *resource, u_int64_t attr_id, char **json_string);
172 int get_resource_list_json(char **json_string);
173
174 int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t *data);
175 int get_resource_attr_int64(struct resource *resource, u_int64_t attr_id, int64_t *data);
176 int get_resource_attr_uint(struct resource *resource, u_int64_t attr_id, u_int32_t *data);
177 int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int64_t *data);
178 int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data);
179 int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char *data);
180 int get_resource_attr_array(struct resource *resource, u_int64_t attr_id,
181                             struct array_value **data);
182 int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **data);
183
184 int set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask);
185 int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask);
186 bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_mask);
187 const char *get_resource_attr_name(struct resource *resource, u_int64_t attr_id);
188
189 const char *get_resource_name(struct resource *resource);
190 void *get_resource_privdata(struct resource *resource);
191 int get_resource_id(struct resource *resource);
192 int get_resource_type(struct resource *resource);
193 int get_resource_ts(struct resource *resource, int64_t *ts_start, int64_t *ts_end);
194 void init_resource_id(void);
195
196 int set_resource_privdata(struct resource *resource, void *priv);
197
198 inline __attribute__((always_inline)) int64_t get_time_now(void)
199 {
200         struct timeval tv;
201
202         gettimeofday(&tv, NULL);
203         return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
204 }
205 #endif