766df4b4f43c5a2fd6bf79a34a57740b818795f3
[adaptation/device-manager-plugin-pinetrail.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         DBG("!@#read[%s], value[%s], line[%d]", file, buf, __LINE__);
164         if ((r >= 0) && (r <= BUFF_MAX))
165                 buf[r] = '\0';
166         else {
167                 ERR("%s read error: %s", file, strerror(errno));
168                 return -1;
169         }
170
171         close(fd);
172         INFO("read %s, value= %s", file, buf);
173         DBG("read[%s], value[%s], line[%d]", file, buf, __LINE__);
174         return 0;
175 }
176
177 static int sys_write_buf(char *file, char *buf)
178 {
179         int fd;
180         int r;
181
182         fd = open(file, O_WRONLY);
183         if (fd == -1) {
184                 ERR("%s open error: %s", file, strerror(errno));
185                 return -1;
186         }
187
188         r = write(fd, buf, strlen(buf));
189         close(fd);
190         if (r < 0) {
191                 ERR("%s write error: %s", file, strerror(errno));
192                 return -1;
193         }
194         INFO("write %s, value= %s", file, buf);
195         DBG("write[%s], value[%s], line[%d]", file, buf, __LINE__);
196         return 0;
197 }
198
199 int sys_get_int(char *fname, int *val)
200 {
201         char buf[BUFF_MAX];
202
203         if (sys_read_buf(fname, buf) == 0) {
204                 *val = atoi(buf);
205                 return 0;
206         } else {
207                 *val = -1;
208                 return -1;
209         }
210 }
211
212 char *sys_get_str(char *fname)
213 {
214         char buf[BUFF_MAX];
215         char *r = NULL;
216
217         if (sys_read_buf(fname, buf) == 0)
218                 r = strdup((char *)buf);
219
220         return r;
221 }
222
223 int sys_set_int(char *fname, int val)
224 {
225         char buf[BUFF_MAX];
226         int r = -1;
227         snprintf(buf, sizeof(buf), "%d", val);
228
229         if (sys_write_buf(fname, buf) == 0)
230                 r = 0;
231
232         return r;
233 }
234
235 int sys_set_str(char *fname, char *val)
236 {
237         int r = -1;
238
239         if (val != NULL) {
240                 if (sys_write_buf(fname, val) == 0)
241                         r = 0;
242         }
243
244         return r;
245 }