6f56601f2639a9cc992ea05fe79087c20ebea0c8
[platform/adaptation/samsung_exynos/device-manager-plugin-artik.git] / hw / common / common.h
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __HAL_BACKEND_COMMON_H__
18 #define __HAL_BACKEND_COMMON_H__
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #ifdef FEATURE_DLOG
28     #define LOG_TAG "HAL_BACKEND_DEVICE"
29     #include <dlog.h>
30     #define _D(fmt, args...)    SLOGD(fmt, ##args)
31     #define _I(fmt, args...)    SLOGI(fmt, ##args)
32     #define _W(fmt, args...)    SLOGW(fmt, ##args)
33     #define _E(fmt, args...)    SLOGE(fmt, ##args)
34 #else
35     #define _D(x, ...)
36     #define _I(x, ...)
37     #define _W(x, ...)
38     #define _E(x, ...)
39 #endif
40
41 #define EXPORT __attribute__ ((visibility("default")))
42
43 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
44
45 #define SHARED_H_BUF_MAX 255
46
47 static inline int sys_read_buf(char *file, char *buf, int len)
48 {
49         int fd, r;
50
51         if (!file || !buf || len < 0)
52                 return -EINVAL;
53
54         fd = open(file, O_RDONLY);
55         if (fd == -1)
56                 return -ENOENT;
57
58         r = read(fd, buf, len);
59         close(fd);
60         if ((r >= 0) && (r < len))
61                 buf[r] = '\0';
62         else
63                 return -EIO;
64
65         return 0;
66 }
67
68 static inline int sys_write_buf(char *file, char *buf)
69 {
70         int fd, r;
71
72         if (!file || !buf)
73                 return -EINVAL;
74
75         fd = open(file, O_WRONLY);
76         if (fd == -1)
77                 return -EPERM;
78
79         r = write(fd, buf, strlen(buf));
80         close(fd);
81         if (r < 0)
82                 return -EIO;
83
84         return 0;
85 }
86
87 static inline int sys_get_int(char *fname, int *val)
88 {
89         char buf[SHARED_H_BUF_MAX];
90         int r;
91
92         if (!fname || !val)
93                 return -EINVAL;
94
95         r = sys_read_buf(fname, buf, sizeof(buf));
96         if (r < 0)
97                 return r;
98
99         *val = atoi(buf);
100         return 0;
101 }
102
103 static inline int sys_get_str(char *fname, char *str, int len)
104 {
105         int r;
106
107         if (!fname || !str || len < 0)
108                 return -EINVAL;
109
110         r = sys_read_buf(fname, str, len);
111         if (r < 0)
112                 return r;
113
114         return 0;
115 }
116
117 static inline int sys_set_int(char *fname, int val)
118 {
119         char buf[SHARED_H_BUF_MAX];
120         int r;
121
122         if (!fname)
123                 return -EINVAL;
124
125         snprintf(buf, sizeof(buf), "%d", val);
126         r = sys_write_buf(fname, buf);
127         if (r < 0)
128                 return r;
129
130         return 0;
131 }
132
133 static inline int sys_set_str(char *fname, char *val)
134 {
135         int r;
136
137         if (!fname || !val)
138                 return -EINVAL;
139
140         r = sys_write_buf(fname, val);
141         if (r < 0)
142                 return r;
143
144         return 0;
145 }
146
147 #endif /* __HAL_BACKEND_COMMON_H__ */