3e527c1387e54f06cd0e12d0b901a8cf23ef3006
[platform/hal/backend/emulator/device-emulator.git] / src / device_manager_io.c
1 /*
2  * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact:
5  * JiHye Kim <jihye1128.kim@samsung.com>
6  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21
22 /*
23  * 2012-03-05 Dohyung Hong <don.hong@samsung.com> Changed package name for the maru board of emulator
24  * 2014-11-05 Jinhyung Choi <jinhyung2.choi@samsung.com> Changed package name for the emulator
25  */
26
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <devman_plugin_intf.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #define EXPORT_API  __attribute__((visibility("default")))
38
39 #define BUFF_MAX        255
40 #define SUCCESS         0
41 #define FAIL            1
42
43 int sys_check_node(char *path)
44 {
45         int fd = -1;
46
47         fd = open(path, O_RDONLY);
48
49         if (fd == -1)
50                 return -1;
51
52         close(fd);
53         return 0;
54 }
55
56 int sys_get_node(char *path, char *node)
57 {
58         if (0 >= snprintf(node, strlen(path) + 1, "%s", path))
59                 return -1;
60
61         return 0;
62 }
63
64 int sys_get_int_wo_convert(char *path, int *value)
65 {
66         int fd = -1;
67
68         fd = open(path, O_RDONLY);
69         if (fd == -1) {
70                 return -1;
71         }
72
73         if (0 > read(fd, value, sizeof(int))) {
74                 close(fd);
75                 return -1;
76         }
77         close (fd);
78
79         return 0;
80 }
81
82 int sys_set_int_wo_convert(char *path, int value)
83 {
84         int fd = -1;
85
86         fd = open(path, O_WRONLY);
87         if (fd == -1) {
88                 return -1;
89         }
90
91         if (0 > write(fd, &value, sizeof(int))) {
92                 close(fd);
93                 return -1;
94         }
95         close(fd);
96
97         return 0;
98 }
99
100 static int sys_read_buf(char *file, char *buf)
101 {
102         int fd;
103         int r;
104
105         fd = open(file, O_RDONLY);
106         if (fd == -1) {
107                 return -ENOENT;
108         }
109
110         r = read(fd, buf, BUFF_MAX - 1);
111         if ((r >= 0) && (r < BUFF_MAX))
112                 buf[r] = '\0';
113         else {
114                 close(fd);
115                 return -EIO;
116         }
117
118         close(fd);
119         return 0;
120 }
121
122 static int sys_write_buf(char *file, char *buf)
123 {
124         int fd;
125         int r;
126
127         fd = open(file, O_WRONLY);
128         if (fd == -1) {
129                 return -1;
130         }
131
132         r = write(fd, buf, strlen(buf));
133         close(fd);
134         if (r < 0) {
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 int sys_get_int(char *fname, int *val)
142 {
143         char buf[BUFF_MAX];
144
145         if (sys_read_buf(fname, buf) == 0) {
146                 *val = atoi(buf);
147                 return 0;
148         } else {
149                 *val = -1;
150                 return -1;
151         }
152 }
153
154 int sys_get_str(char *fname, char* str)
155 {
156         char buf[BUFF_MAX] = {0};
157
158         if (sys_read_buf(fname, buf) == 0) {
159                 memset(str, 0, strlen(buf) + 1);
160                 memcpy(str, buf, strlen(buf));
161                 return 0;
162         }
163
164         return -1;
165 }
166
167 int sys_set_int(char *fname, int val)
168 {
169         char buf[BUFF_MAX];
170         int r = -1;
171         snprintf(buf, sizeof(buf), "%d", val);
172
173         if (sys_write_buf(fname, buf) == 0)
174                 r = 0;
175
176         return r;
177 }
178
179 int sys_set_str(char *fname, char *val)
180 {
181         int r = -1;
182
183         if (val != NULL) {
184                 if (sys_write_buf(fname, val) == 0)
185                         r = 0;
186         }
187
188         return r;
189 }