Branch and submit for Tizen 2.0 final
[profile/ivi/device-manager-plugin-ivi.git] / device_engine.c
1 /*
2  *  devman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@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 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <dirent.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33 #include "devlog.h"
34 #include "device_engine.h"
35
36 struct device *dev_head = NULL;
37 #define BUFF_MAX 255
38
39 void add_dev(struct device *dev)
40 {
41         dev->next = dev_head;
42         dev_head = dev;
43 }
44
45 void print_devices()
46 {
47         struct device *dev;
48         dev = dev_head;
49
50         while (dev) {
51                 DBG("%s - %d", dev->devname, dev->devtype);
52                 dev = dev->next;
53         }
54 }
55
56 void reset_devtype()
57 {
58         struct device *dev;
59         dev = dev_head;
60
61         while (dev) {
62                 dev->devtype = -1;
63                 dev = dev->next;
64         }
65 }
66
67 struct device *find_device(struct device *root_dev, devtype_t devtype)
68 {
69         struct device *dev;
70
71         if (devtype == -1)
72                 return NULL;
73
74         if (root_dev == NULL)
75                 dev = dev_head;
76         else
77                 dev = root_dev;
78
79         while (dev) {
80                 DBG("devname = %s %d %d", dev->devname, dev->devtype, devtype);
81                 if (dev->devtype == devtype)
82                         return dev;
83                 dev = dev->next;
84         }
85
86         return NULL;
87 }
88
89 int find_sysfs_node(char *path, char *node_name)
90 {
91         DIR *dp;
92         struct dirent *entry;
93
94         dp = opendir(path);
95         if (dp == NULL) {
96                 DBG("path is not existed : %s", path);
97                 return -1;
98         }
99
100         while ((entry = readdir(dp)) != NULL) {
101                 if (strncmp(entry->d_name, ".", 1) == 0 ||
102                     strncmp(entry->d_name, "..", 2) == 0)
103                         continue;
104                 else
105                         break;
106         }
107
108         /* copy node name */
109         if (entry != NULL) {
110                 if (node_name != NULL)
111                         strncpy(node_name, entry->d_name,PATH_MAX);
112
113         } else {
114                 DBG("sysfs node not existed");
115                 if (closedir(dp) != 0)
116                         DBG("Unable to close directory");
117                 return -1;
118         }
119
120         if (closedir(dp) != 0)
121                 DBG("Unable to close directory");
122         return 0;
123 }
124
125 int set_devtype(char *devname, devtype_t devtype)
126 {
127         int ret;
128         struct device *dev;
129         dev = dev_head;
130
131         while (dev) {
132                 if (strstr(dev->devname, devname)) {
133                         if ((strstr(dev->devname, "auto") != NULL) &&
134                             (dev->probe != NULL)) {
135                                 ret = dev->probe();
136                                 if (ret < 0) {
137                                         DBG("auto probe failed");
138                                         return -1;
139                                 }
140                         }
141
142                         dev->devtype = devtype;
143                         return 0;
144                 }
145                 dev = dev->next;
146         }
147
148         return -1;
149 }
150
151 static int sys_read_buf(char *file, char *buf)
152 {
153         int fd;
154         int r;
155
156         fd = open(file, O_RDONLY);
157         if (fd == -1) {
158                 ERR("%s open error: %s", file, strerror(errno));
159                 return -1;
160         }
161
162         r = read(fd, buf, BUFF_MAX);
163         if ((r >= 0) && (r <= BUFF_MAX))
164                 buf[r] = '\0';
165         else {
166                 ERR("%s read error: %s", file, strerror(errno));
167                 return -1;
168         }
169
170         close(fd);
171         INFO("read %s, value= %s", file, buf);
172         return 0;
173 }
174
175 static int sys_write_buf(char *file, char *buf)
176 {
177         int fd;
178         int r;
179
180         fd = open(file, O_WRONLY);
181         if (fd == -1) {
182                 ERR("%s open error: %s", file, strerror(errno));
183                 return -1;
184         }
185
186         r = write(fd, buf, strlen(buf));
187         close(fd);
188         if (r < 0) {
189                 ERR("%s write error: %s", file, strerror(errno));
190                 return -1;
191         }
192         INFO("write %s, value= %s", file, buf);
193         DBG("write[%s], value[%s], line[%d]", file, buf, __LINE__);
194         return 0;
195 }
196
197 int sys_get_int(char *fname, int *val)
198 {
199         char buf[BUFF_MAX+1];
200
201         if (sys_read_buf(fname, buf) == 0) {
202                 *val = atoi(buf);
203                 return 0;
204         } else {
205                 *val = -1;
206                 return -1;
207         }
208 }
209
210 char *sys_get_str(char *fname)
211 {
212         char buf[BUFF_MAX+1];
213         char *r = NULL;
214
215         if (sys_read_buf(fname, buf) == 0)
216                 r = strdup((char *)buf);
217
218         return r;
219 }
220
221 int sys_set_int(char *fname, int val)
222 {
223         char buf[BUFF_MAX+1];
224         int r = -1;
225         snprintf(buf, sizeof(buf), "%d", val);
226
227         if (sys_write_buf(fname, buf) == 0)
228                 r = 0;
229
230         return r;
231 }
232
233 int sys_set_str(char *fname, char *val)
234 {
235         int r = -1;
236
237         if (val != NULL) {
238                 if (sys_write_buf(fname, val) == 0)
239                         r = 0;
240         }
241
242         return r;
243 }