blob: 4bccd50043e3404533e5002d9ade6dc476c5bb5e
1 | /* |
2 | // Copyright (c) 2016 Amlogic |
3 | // |
4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | // you may not use this file except in compliance with the License. |
6 | // You may obtain a copy of the License at |
7 | // |
8 | // http://www.apache.org/licenses/LICENSE-2.0 |
9 | // |
10 | // Unless required by applicable law or agreed to in writing, software |
11 | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | // See the License for the specific language governing permissions and |
14 | // limitations under the License. |
15 | // |
16 | */ |
17 | |
18 | |
19 | #include <hardware/hardware.h> |
20 | |
21 | #include <HwcTrace.h> |
22 | #include <fcntl.h> |
23 | #include <stdlib.h> |
24 | #include <errno.h> |
25 | #include <cutils/properties.h> |
26 | #include <hardware/hwcomposer2.h> |
27 | |
28 | #include <Utils.h> |
29 | |
30 | namespace android { |
31 | namespace amlogic { |
32 | |
33 | Utils::Utils() |
34 | { |
35 | |
36 | } |
37 | |
38 | Utils::~Utils() |
39 | { |
40 | |
41 | } |
42 | |
43 | bool Utils::get_bool_prop(const char* prop) { |
44 | char val[PROPERTY_VALUE_MAX]; |
45 | memset(val, 0, sizeof(val)); |
46 | if (property_get(prop, val, "false") && strcmp(val, "true") == 0) { |
47 | //VTRACE("prop: %s is %s",prop, val); |
48 | return true; |
49 | } |
50 | |
51 | return false; |
52 | } |
53 | |
54 | int Utils::get_int_prop(const char* prop) { |
55 | char val[PROPERTY_VALUE_MAX]; |
56 | memset(val, 0, sizeof(val)); |
57 | if (property_get(prop, val, "0")) { |
58 | VTRACE("prop: %s is %s",prop, val); |
59 | return atoi(val); |
60 | } |
61 | return -1; |
62 | } |
63 | |
64 | bool Utils::get_str_prop(const char *key, char *value, const char *def){ |
65 | property_get(key, value, def); |
66 | return true; |
67 | } |
68 | |
69 | int Utils::getSysfsInt(const char* syspath, int def) { |
70 | int val = def; |
71 | char valstr[64]; |
72 | if (getSysfsStr(syspath, valstr, sizeof(valstr)) == 0) { |
73 | val = atoi(valstr); |
74 | DTRACE("sysfs(%s) read int (%d)", syspath, val); |
75 | } |
76 | return val; |
77 | } |
78 | |
79 | int Utils::getSysfsStr(const char *syspath, char *valstr){ |
80 | char buf[MAX_STR_LEN+1] = {0}; |
81 | getSysfsStr(syspath, (char*)buf, MAX_STR_LEN, false); |
82 | strcpy(valstr, buf); |
83 | return 0; |
84 | } |
85 | |
86 | int Utils::getSysfsStr(const char* syspath, char *valstr, int size, |
87 | bool needOriginalData) { |
88 | |
89 | int fd, len; |
90 | |
91 | if ( NULL == valstr ) { |
92 | ETRACE("buf is NULL"); |
93 | return -1; |
94 | } |
95 | |
96 | if ((fd = open(syspath, O_RDONLY)) < 0) { |
97 | ETRACE("readSysFs, open %s fail.", syspath); |
98 | return -1; |
99 | } |
100 | |
101 | len = read(fd, valstr, size); |
102 | if (len < 0) { |
103 | ETRACE("read error: %s, %s\n", syspath, strerror(errno)); |
104 | close(fd); |
105 | return -1; |
106 | } |
107 | |
108 | if (!needOriginalData) { |
109 | int i , j; |
110 | for (i = 0, j = 0; i <= len -1; i++) { |
111 | /*change '\0' to 0x20(spacing), otherwise the string buffer will be cut off |
112 | * if the last char is '\0' should not replace it |
113 | */ |
114 | if (0x0 == valstr[i] && i < len - 1) { |
115 | valstr[i] = 0x20; |
116 | } |
117 | //DTRACE("read buffer index:%d is a 0x0, replace to spacing \n", i); |
118 | |
119 | |
120 | /* delete all the character of '\n' */ |
121 | if (0x0a != valstr[i]) { |
122 | valstr[j++] = valstr[i]; |
123 | } |
124 | } |
125 | |
126 | valstr[j] = 0x0; |
127 | } |
128 | |
129 | //DTRACE("read %s, result length:%d, val:%s\n", syspath, len, valstr); |
130 | |
131 | close(fd); |
132 | return 0; |
133 | |
134 | } |
135 | |
136 | int Utils::setSysfsStr(const char *path, const char *val) { |
137 | int bytes; |
138 | int fd = open(path, O_RDWR); |
139 | if (fd >= 0) { |
140 | bytes = write(fd, val, strlen(val)); |
141 | //DTRACE("setSysfsStr %s= %s\n", path,val); |
142 | close(fd); |
143 | return 0; |
144 | } else { |
145 | ETRACE(" open file error: [%s]", path); |
146 | return -1; |
147 | } |
148 | } |
149 | |
150 | bool Utils::checkBoolProp(const char* prop) { |
151 | char val[PROPERTY_VALUE_MAX]; |
152 | |
153 | memset(val, 0, sizeof(val)); |
154 | if (property_get(prop, val, "false") && strcmp(val, "true") == 0) { |
155 | DTRACE("prop: %s is %s",prop, val); |
156 | return true; |
157 | } |
158 | |
159 | return false; |
160 | } |
161 | |
162 | int32_t Utils::checkIntProp(const char* prop) { |
163 | char val[PROPERTY_VALUE_MAX]; |
164 | |
165 | memset(val, 0, sizeof(val)); |
166 | if (property_get(prop, val, "2")) { |
167 | VTRACE("prop: %s is %s",prop, val); |
168 | return atoi(val); |
169 | } |
170 | return 0; |
171 | } |
172 | |
173 | int32_t Utils::checkAndDupFd(int32_t fd) { |
174 | if (fd < 0) { |
175 | ETRACE("not a vliad fd %d", fd); |
176 | return -1; |
177 | } |
178 | |
179 | int32_t dup_fd = ::dup(fd); |
180 | if (dup_fd < 0) { |
181 | ETRACE("fd dup failed: %s", strerror(errno)); |
182 | } |
183 | |
184 | return dup_fd; |
185 | } |
186 | |
187 | bool Utils::checkSysfsStatus(const char* sysfstr, char* lastr, int32_t size) { |
188 | char val[32]; |
189 | char *p = lastr; |
190 | |
191 | memset(val, 0, sizeof(val)); |
192 | if (getSysfsStr(sysfstr, val, sizeof(val)) == 0) { |
193 | DTRACE("val: %s, lastr: %s",val, p); |
194 | if ((strcmp(val, p) != 0)) { |
195 | memset(p, 0, size); |
196 | strcpy(p, val); |
197 | return true; |
198 | } |
199 | } |
200 | |
201 | return false; |
202 | } |
203 | |
204 | const char* Utils::getHotplugUeventEnvelope() |
205 | { |
206 | return "change@/devices/virtual/amhdmitx/amhdmitx0/hdmi"; |
207 | } |
208 | |
209 | const char* Utils::getHdcpUeventEnvelope() |
210 | { |
211 | return "change@/devices/virtual/switch/hdcp"; |
212 | } |
213 | |
214 | const char* Utils::getModeChangeUeventEnvelope() |
215 | { |
216 | return "change@/devices/virtual/amhdmitx/amhdmitx0/setmode"; |
217 | } |
218 | |
219 | const char* Utils::getSwitchState0() |
220 | { |
221 | return "STATE=HDMI=0"; |
222 | } |
223 | |
224 | const char* Utils::getSwitchState1() |
225 | { |
226 | return "STATE=HDMI=1"; |
227 | } |
228 | |
229 | |
230 | bool Utils::rectEmpty(hwc_rect_t& rect) { |
231 | if ((rect.right - rect.left <= 0) ||(rect.bottom - rect.top <= 0)) |
232 | return true; |
233 | else |
234 | return false; |
235 | } |
236 | |
237 | bool Utils::rectIntersect(hwc_rect_t& source, hwc_rect_t& dest, hwc_rect_t& result) { |
238 | result.left = max(source.left, dest.left); |
239 | result.top = max(source.top, dest.top); |
240 | result.right = min(source.right, dest.right); |
241 | result.bottom = min(source.bottom, dest.bottom); |
242 | return !rectEmpty(result); |
243 | } |
244 | |
245 | Utils::OVERLAP_TYPE Utils::rectOverlap(hwc_rect_t& source, hwc_rect_t& dest) { |
246 | hwc_rect_t result; |
247 | if (!rectIntersect(source, dest, result)) { |
248 | return OVERLAP_EMPTY; |
249 | } else { |
250 | if (compareRect(result, source) == true) { |
251 | return OVERLAP_FULL; |
252 | } else { |
253 | return OVERLAP_PART; |
254 | } |
255 | } |
256 | } |
257 | |
258 | |
259 | } // namespace amlogic |
260 | } // namespace android |
261 |