pass-hal: tw3: Update pass-hal-tw3 by using hal-api-power interface
[platform/adaptation/tw3/pass-hal-tw3.git] / src / sysfs.c
1 /*
2  * PASS (Power Aware System Service)
3  *
4  * Copyright (c) 2017 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 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "sysfs.h"
26
27 static int sysfs_read_buf(char *path, char *buf, int len)
28 {
29         int r, fd;
30
31         if ((!path) || (!buf) || (len < 0))
32                 return -EINVAL;
33
34         fd = open(path, O_RDONLY);
35         if (fd == -1)
36                 return -ENOENT;
37
38         r = read(fd, buf, len);
39         close(fd);
40
41         if ((r < 0) || (r > len))
42                 return -EIO;
43
44         buf[r] = '\0';
45
46         return 0;
47 }
48
49 static int sysfs_write_buf(char *path, char *buf)
50 {
51         int w, fd;
52
53         if ((!path) || (!buf))
54                 return -EINVAL;
55
56         fd = open(path, O_WRONLY);
57         if (fd == -1)
58                 return -ENOENT;
59
60         w = write(fd, buf, strlen(buf));
61         close(fd);
62
63         if (w < 0)
64                 return -EIO;
65
66         return 0;
67 }
68
69 int sysfs_read_int(char *path, int *val)
70 {
71         char buf[MAX_BUF_SIZE + 1];
72         int r;
73
74         if ((!path) || (!val))
75                 return -EINVAL;
76
77         r = sysfs_read_buf(path, buf, MAX_BUF_SIZE);
78         if (r < 0)
79                 return r;
80
81         *val = atoi(buf);
82         return 0;
83 }
84
85 int sysfs_read_str(char *path, char *str, int len)
86 {
87         int r;
88
89         if ((!path) || (!str) || (len <= 0))
90                 return -EINVAL;
91
92         r = sysfs_read_buf(path, str, len);
93         if (r < 0)
94                 return r;
95
96         return 0;
97 }
98
99 int sysfs_write_int(char *path, int val)
100 {
101         char buf[MAX_BUF_SIZE + 1];
102         int w;
103
104         if (!path)
105                 return -EINVAL;
106
107         snprintf(buf, MAX_BUF_SIZE, "%d", val);
108         w = sysfs_write_buf(path, buf);
109         if (w < 0)
110                 return w;
111
112         return 0;
113 }
114
115 int sysfs_write_str(char *path, char *str)
116 {
117         int w;
118
119         if ((!path) || (!str))
120                 return -EINVAL;
121
122         w = sysfs_write_buf(path, str);
123         if (w < 0)
124                 return w;
125
126         return 0;
127 }