blob: c09525024b7e932ab38fe12bb96600aed5cdacbc
1 | /* |
2 | * Copyright (c) 2014 Amlogic, Inc. All rights reserved. |
3 | * |
4 | * This source code is subject to the terms and conditions defined in the |
5 | * file 'LICENSE' which is part of this source code package. |
6 | * |
7 | * Description: |
8 | */ |
9 | |
10 | |
11 | |
12 | #define LOG_TAG "amavutils" |
13 | |
14 | #include <stdlib.h> |
15 | #include <fcntl.h> |
16 | #include <errno.h> |
17 | #include <string.h> |
18 | #include <cutils/log.h> |
19 | #include <sys/ioctl.h> |
20 | #include "include/Amsysfsutils.h" |
21 | #include <Amsyswrite.h> |
22 | #include <cutils/properties.h> |
23 | #include <media_ctl.h> |
24 | |
25 | #ifndef LOGD |
26 | #define LOGV ALOGV |
27 | #define LOGD ALOGD |
28 | #define LOGI ALOGI |
29 | #define LOGW ALOGW |
30 | #define LOGE ALOGE |
31 | #endif |
32 | |
33 | #ifndef NO_USE_SYSWRITE //added by lifengcao for startup video |
34 | #define USE_SYSWRITE |
35 | #endif |
36 | |
37 | |
38 | #ifndef USE_SYSWRITE |
39 | int amsysfs_set_sysfs_str(const char *path, const char *val) |
40 | { |
41 | int fd,ret; |
42 | int bytes; |
43 | ret = mediactl_set_str_func(path,val); |
44 | if (ret == UnSupport) |
45 | { |
46 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
47 | if (fd >= 0) { |
48 | bytes = write(fd, val, strlen(val)); |
49 | close(fd); |
50 | return 0; |
51 | } else { |
52 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
53 | } |
54 | return -1; |
55 | } |
56 | else return ret; |
57 | } |
58 | int amsysfs_get_sysfs_str(const char *path, char *valstr, int size) |
59 | { |
60 | int fd,ret; |
61 | ret = mediactl_get_str_func(path, valstr, size); |
62 | if (ret == UnSupport) |
63 | { |
64 | fd = open(path, O_RDONLY); |
65 | if (fd >= 0) { |
66 | memset(valstr, 0, size); |
67 | read(fd, valstr, size - 1); |
68 | valstr[strlen(valstr)] = '\0'; |
69 | close(fd); |
70 | } else { |
71 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
72 | sprintf(valstr, "%s", "fail"); |
73 | return -1; |
74 | }; |
75 | //LOGI("get_sysfs_str=%s\n", valstr); |
76 | return 0; |
77 | } |
78 | else return ret; |
79 | } |
80 | int amsysfs_set_sysfs_int(const char *path, int val) |
81 | { |
82 | int fd,ret; |
83 | int bytes; |
84 | char bcmd[16]; |
85 | ret = mediactl_set_int_func(path,val); |
86 | if (ret == UnSupport) |
87 | { |
88 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
89 | if (fd >= 0) { |
90 | sprintf(bcmd, "%d", val); |
91 | bytes = write(fd, bcmd, strlen(bcmd)); |
92 | close(fd); |
93 | return 0; |
94 | } else { |
95 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
96 | } |
97 | return -1; |
98 | } |
99 | else return ret; |
100 | } |
101 | int amsysfs_get_sysfs_int(const char *path) |
102 | { |
103 | int fd,ret; |
104 | int val = 0; |
105 | char bcmd[16]; |
106 | ret = mediactl_get_int_func(path); |
107 | if (ret == UnSupport) { |
108 | fd = open(path, O_RDONLY); |
109 | if (fd >= 0) { |
110 | read(fd, bcmd, sizeof(bcmd)); |
111 | val = strtol(bcmd, NULL, 10); |
112 | close(fd); |
113 | } else { |
114 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
115 | } |
116 | return val; |
117 | } |
118 | else return ret; |
119 | } |
120 | int amsysfs_set_sysfs_int16(const char *path, int val) |
121 | { |
122 | int fd,ret; |
123 | int bytes; |
124 | ret = mediactl_set_int_func(path,val); |
125 | if (ret == UnSupport) { |
126 | char bcmd[16]; |
127 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
128 | if (fd >= 0) { |
129 | sprintf(bcmd, "0x%x", val); |
130 | bytes = write(fd, bcmd, strlen(bcmd)); |
131 | close(fd); |
132 | return 0; |
133 | } else { |
134 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
135 | } |
136 | |
137 | return -1; |
138 | } |
139 | else return ret; |
140 | } |
141 | int amsysfs_get_sysfs_int16(const char *path) |
142 | { |
143 | int fd,ret; |
144 | int val = 0; |
145 | char bcmd[16]; |
146 | ret = mediactl_get_int_func(path); |
147 | if (ret == UnSupport) { |
148 | fd = open(path, O_RDONLY); |
149 | if (fd >= 0) { |
150 | read(fd, bcmd, sizeof(bcmd)); |
151 | val = strtol(bcmd, NULL, 16); |
152 | close(fd); |
153 | } else { |
154 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
155 | } |
156 | return val; |
157 | } |
158 | else return ret; |
159 | } |
160 | unsigned long amsysfs_get_sysfs_ulong(const char *path) |
161 | { |
162 | int fd,ret; |
163 | char bcmd[24] = ""; |
164 | unsigned long num = 0; |
165 | ret = mediactl_get_int_func(path); |
166 | if (ret == UnSupport) { |
167 | if ((fd = open(path, O_RDONLY)) >= 0) { |
168 | read(fd, bcmd, sizeof(bcmd)); |
169 | num = strtoul(bcmd, NULL, 0); |
170 | close(fd); |
171 | } else { |
172 | LOGE("unable to open file %s,err: %s", path, strerror(errno)); |
173 | } |
174 | return num; |
175 | } |
176 | else return ret; |
177 | } |
178 | void amsysfs_write_prop(const char* key, const char* value) |
179 | { |
180 | int ret = 0; |
181 | ret = property_set(key,value); |
182 | } |
183 | #else |
184 | int amsysfs_set_sysfs_str(const char *path, const char *val) |
185 | { |
186 | int ret = 0; |
187 | ret = mediactl_set_str_func(path,val); |
188 | if (ret == UnSupport) { |
189 | LOGD("%s path =%s,val=%s\n",__FUNCTION__,path,val); |
190 | return amSystemWriteWriteSysfs(path, val); |
191 | } |
192 | else return ret; |
193 | |
194 | } |
195 | int amsysfs_get_sysfs_str(const char *path, char *valstr, int size) |
196 | { |
197 | int ret = 0; |
198 | ret = mediactl_get_str_func(path, valstr, size); |
199 | if (ret == UnSupport) { |
200 | LOGD("%s path =%s,val=%s,size=%d\n",__FUNCTION__,path,valstr,size); |
201 | if (amSystemWriteReadNumSysfs(path, valstr, size) != -1) { |
202 | return 0; |
203 | } |
204 | sprintf(valstr, "%s", "fail"); |
205 | return -1; |
206 | } |
207 | else return ret; |
208 | } |
209 | |
210 | int amsysfs_set_sysfs_int(const char *path, int val) |
211 | { |
212 | int ret = 0; |
213 | ret = mediactl_set_int_func(path,val); |
214 | if (ret == UnSupport) { |
215 | LOGD("%s path =%s,val=%d\n",__FUNCTION__,path,val); |
216 | char bcmd[16] = ""; |
217 | sprintf(bcmd, "%d", val); |
218 | return amSystemWriteWriteSysfs(path, bcmd); |
219 | } |
220 | else return ret; |
221 | } |
222 | |
223 | int amsysfs_get_sysfs_int(const char *path) |
224 | { |
225 | int ret = 0; |
226 | ret = mediactl_get_int_func(path); |
227 | if (ret == UnSupport) { |
228 | LOGD("%s path =%s\n",__FUNCTION__,path); |
229 | char bcmd[16] = ""; |
230 | int val = 0; |
231 | if (amSystemWriteReadSysfs(path, bcmd) == 0) { |
232 | val = strtol(bcmd, NULL, 10); |
233 | } |
234 | return val; |
235 | } |
236 | else return ret; |
237 | } |
238 | |
239 | int amsysfs_set_sysfs_int16(const char *path, int val) |
240 | { |
241 | int ret = 0; |
242 | ret = mediactl_set_int_func(path,val); |
243 | if (ret == UnSupport) { |
244 | LOGD("%s path =%s,val=%d\n",__FUNCTION__,path,val); |
245 | char bcmd[16] = ""; |
246 | sprintf(bcmd, "0x%x", val); |
247 | return amSystemWriteWriteSysfs(path, bcmd); |
248 | } |
249 | else return ret; |
250 | } |
251 | |
252 | int amsysfs_get_sysfs_int16(const char *path) |
253 | { |
254 | int ret = 0; |
255 | ret = mediactl_get_int_func(path); |
256 | if (ret == UnSupport) { |
257 | LOGD("%s path =%s\n",__FUNCTION__,path); |
258 | char bcmd[16] = ""; |
259 | int val = 0; |
260 | if (amSystemWriteReadSysfs(path, bcmd) == 0) { |
261 | val = strtol(bcmd, NULL, 16); |
262 | } |
263 | return val; |
264 | } |
265 | else return ret; |
266 | } |
267 | |
268 | unsigned long amsysfs_get_sysfs_ulong(const char *path) |
269 | { |
270 | int ret = 0; |
271 | ret = mediactl_get_int_func(path); |
272 | if (ret == UnSupport) { |
273 | LOGD("%s path =%s\n",__FUNCTION__,path); |
274 | char bcmd[24] = ""; |
275 | int val = 0; |
276 | if (amSystemWriteReadSysfs(path, bcmd) == 0) { |
277 | val = strtoul(bcmd, NULL, 0); |
278 | } |
279 | return val; |
280 | } |
281 | else return ret; |
282 | } |
283 | void amsysfs_write_prop(const char* key, const char* value) |
284 | { |
285 | amSystemWriteSetProperty(key,value); |
286 | } |
287 | #endif |
288 | |
289 |