blob: 14f6cae5d34897bee7a2eb6c4e07c34a37d50add
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 | int Utils::getSysfsInt(const char* syspath, int def) { |
65 | int val = def; |
66 | char valstr[64]; |
67 | if (getSysfsStr(syspath, valstr, sizeof(valstr)) == 0) { |
68 | val = atoi(valstr); |
69 | DTRACE("sysfs(%s) read int (%d)", syspath, val); |
70 | } |
71 | return val; |
72 | } |
73 | |
74 | int Utils::getSysfsStr(const char* syspath, char *valstr, int size, |
75 | bool needOriginalData) { |
76 | |
77 | int fd, len; |
78 | |
79 | if ( NULL == valstr ) { |
80 | ETRACE("buf is NULL"); |
81 | return -1; |
82 | } |
83 | |
84 | if ((fd = open(syspath, O_RDONLY)) < 0) { |
85 | ETRACE("readSysFs, open %s fail.", syspath); |
86 | return -1; |
87 | } |
88 | |
89 | len = read(fd, valstr, size); |
90 | if (len < 0) { |
91 | ETRACE("read error: %s, %s\n", syspath, strerror(errno)); |
92 | close(fd); |
93 | return -1; |
94 | } |
95 | |
96 | if (!needOriginalData) { |
97 | int i , j; |
98 | for (i = 0, j = 0; i <= len -1; i++) { |
99 | /*change '\0' to 0x20(spacing), otherwise the string buffer will be cut off |
100 | * if the last char is '\0' should not replace it |
101 | */ |
102 | if (0x0 == valstr[i] && i < len - 1) { |
103 | valstr[i] = 0x20; |
104 | } |
105 | //DTRACE("read buffer index:%d is a 0x0, replace to spacing \n", i); |
106 | |
107 | |
108 | /* delete all the character of '\n' */ |
109 | if (0x0a != valstr[i]) { |
110 | valstr[j++] = valstr[i]; |
111 | } |
112 | } |
113 | |
114 | valstr[j] = 0x0; |
115 | } |
116 | |
117 | //DTRACE("read %s, result length:%d, val:%s\n", syspath, len, valstr); |
118 | |
119 | close(fd); |
120 | return 0; |
121 | |
122 | } |
123 | |
124 | int Utils::setSysfsStr(const char *path, const char *val) { |
125 | int bytes; |
126 | int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
127 | if (fd >= 0) { |
128 | bytes = write(fd, val, strlen(val)); |
129 | //DTRACE("setSysfsStr %s= %s\n", path,val); |
130 | close(fd); |
131 | return 0; |
132 | } else { |
133 | return -1; |
134 | } |
135 | } |
136 | |
137 | bool Utils::checkBoolProp(const char* prop) { |
138 | char val[PROPERTY_VALUE_MAX]; |
139 | |
140 | memset(val, 0, sizeof(val)); |
141 | if (property_get(prop, val, "false") && strcmp(val, "true") == 0) { |
142 | DTRACE("prop: %s is %s",prop, val); |
143 | return true; |
144 | } |
145 | |
146 | return false; |
147 | } |
148 | |
149 | int32_t Utils::checkIntProp(const char* prop) { |
150 | char val[PROPERTY_VALUE_MAX]; |
151 | |
152 | memset(val, 0, sizeof(val)); |
153 | if (property_get(prop, val, "2")) { |
154 | VTRACE("prop: %s is %s",prop, val); |
155 | return atoi(val); |
156 | } |
157 | return 0; |
158 | } |
159 | |
160 | int32_t Utils::checkAndDupFd(int32_t fd) { |
161 | if (fd < 0) { |
162 | ETRACE("not a vliad fd %d", fd); |
163 | return -1; |
164 | } |
165 | |
166 | int32_t dup_fd = ::dup(fd); |
167 | if (dup_fd < 0) { |
168 | ETRACE("fd dup failed: %s", strerror(errno)); |
169 | } |
170 | |
171 | return dup_fd; |
172 | } |
173 | |
174 | bool Utils::checkSysfsStatus(const char* sysfstr, char* lastr, int32_t size) { |
175 | char val[32]; |
176 | char *p = lastr; |
177 | |
178 | memset(val, 0, sizeof(val)); |
179 | if (getSysfsStr(sysfstr, val, sizeof(val)) == 0) { |
180 | DTRACE("val: %s, lastr: %s",val, p); |
181 | if ((strcmp(val, p) != 0)) { |
182 | memset(p, 0, size); |
183 | strcpy(p, val); |
184 | return true; |
185 | } |
186 | } |
187 | |
188 | return false; |
189 | } |
190 | |
191 | const char* Utils::getHotplugUeventEnvelope() |
192 | { |
193 | return "change@/devices/virtual/switch/hdmi_delay"; |
194 | } |
195 | |
196 | const char* Utils::getHdcpUeventEnvelope() |
197 | { |
198 | return "change@/devices/virtual/switch/hdcp"; |
199 | } |
200 | |
201 | const char* Utils::getSwitchState0() |
202 | { |
203 | return "SWITCH_STATE=0"; |
204 | } |
205 | |
206 | const char* Utils::getSwitchState1() |
207 | { |
208 | return "SWITCH_STATE=1"; |
209 | } |
210 | |
211 | |
212 | bool Utils::rectEmpty(hwc_rect_t& rect) { |
213 | if ((rect.right - rect.left <= 0) ||(rect.bottom - rect.top <= 0)) |
214 | return true; |
215 | else |
216 | return false; |
217 | } |
218 | |
219 | bool Utils::rectIntersect(hwc_rect_t& source, hwc_rect_t& dest, hwc_rect_t& result) { |
220 | result.left = max(source.left, dest.left); |
221 | result.top = max(source.top, dest.top); |
222 | result.right = min(source.right, dest.right); |
223 | result.bottom = min(source.bottom, dest.bottom); |
224 | return !rectEmpty(result); |
225 | } |
226 | |
227 | Utils::OVERLAP_TYPE Utils::rectOverlap(hwc_rect_t& source, hwc_rect_t& dest) { |
228 | hwc_rect_t result; |
229 | if (!rectIntersect(source, dest, result)) { |
230 | return OVERLAP_EMPTY; |
231 | } else { |
232 | if (compareRect(result, source) == true) { |
233 | return OVERLAP_FULL; |
234 | } else { |
235 | return OVERLAP_PART; |
236 | } |
237 | } |
238 | } |
239 | |
240 | |
241 | } // namespace amlogic |
242 | } // namespace android |
243 |