summaryrefslogtreecommitdiff
path: root/hwc2/common/utils/Utils.cpp (plain)
blob: 53eed8d8b49a8ee77145ea3b2bd8e96051d2f0bc
1/*
2// Copyright(c) 2016 Amlogic Corporation
3*/
4
5#include <hardware/hardware.h>
6
7#include <HwcTrace.h>
8#include <fcntl.h>
9#include <stdlib.h>
10#include <errno.h>
11#include <cutils/properties.h>
12#include <hardware/hwcomposer2.h>
13
14#include <Utils.h>
15
16namespace android {
17namespace amlogic {
18
19Utils::Utils()
20{
21
22}
23
24Utils::~Utils()
25{
26
27}
28
29bool Utils::get_bool_prop(const char* prop) {
30 char val[PROPERTY_VALUE_MAX];
31 memset(val, 0, sizeof(val));
32 if (property_get(prop, val, "false") && strcmp(val, "true") == 0) {
33 //VTRACE("prop: %s is %s",prop, val);
34 return true;
35 }
36
37 return false;
38}
39
40int Utils::get_int_prop(const char* prop) {
41 char val[PROPERTY_VALUE_MAX];
42 memset(val, 0, sizeof(val));
43 if (property_get(prop, val, "2")) {
44 //VTRACE("prop: %s is %s",prop, val);
45 return atoi(val);
46 }
47 return 0;
48}
49
50int Utils::getSysfsInt(const char* syspath, int def) {
51 int val = def;
52 char valstr[64];
53 if (getSysfsStr(syspath, valstr, sizeof(valstr)) == 0) {
54 val = atoi(valstr);
55 //DTRACE("sysfs(%s) read int (%s)=(%d)", syspath, valstr, val);
56 }
57 return val;
58}
59
60int Utils::getSysfsStr(const char* syspath, char *valstr, int size,
61 bool needOriginalData) {
62
63 int fd, len;
64
65 if ( NULL == valstr ) {
66 ETRACE("buf is NULL");
67 return -1;
68 }
69
70 if ((fd = open(syspath, O_RDONLY)) < 0) {
71 ETRACE("readSysFs, open %s fail.", syspath);
72 return -1;
73 }
74
75 len = read(fd, valstr, size);
76 if (len < 0) {
77 ETRACE("read error: %s, %s\n", syspath, strerror(errno));
78 close(fd);
79 return -1;
80 }
81
82 if (!needOriginalData) {
83 int i , j;
84 for (i = 0, j = 0; i <= len -1; i++) {
85 /*change '\0' to 0x20(spacing), otherwise the string buffer will be cut off
86 * if the last char is '\0' should not replace it
87 */
88 if (0x0 == valstr[i] && i < len - 1) {
89 valstr[i] = 0x20;
90 }
91 //DTRACE("read buffer index:%d is a 0x0, replace to spacing \n", i);
92
93
94 /* delete all the character of '\n' */
95 if (0x0a != valstr[i]) {
96 valstr[j++] = valstr[i];
97 }
98 }
99
100 valstr[j] = 0x0;
101 }
102
103 //DTRACE("read %s, result length:%d, val:%s\n", syspath, len, valstr);
104
105 close(fd);
106 return 0;
107
108}
109
110int Utils::setSysfsStr(const char *path, const char *val) {
111 int bytes;
112 int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
113 if (fd >= 0) {
114 bytes = write(fd, val, strlen(val));
115 //DTRACE("setSysfsStr %s= %s\n", path,val);
116 close(fd);
117 return 0;
118 } else {
119 return -1;
120 }
121}
122
123bool Utils::checkBoolProp(const char* prop) {
124 char val[PROPERTY_VALUE_MAX];
125
126 memset(val, 0, sizeof(val));
127 if (property_get(prop, val, "false") && strcmp(val, "true") == 0) {
128 DTRACE("prop: %s is %s",prop, val);
129 return true;
130 }
131
132 return false;
133}
134
135int32_t Utils::checkIntProp(const char* prop) {
136 char val[PROPERTY_VALUE_MAX];
137
138 memset(val, 0, sizeof(val));
139 if (property_get(prop, val, "2")) {
140 VTRACE("prop: %s is %s",prop, val);
141 return atoi(val);
142 }
143 return 0;
144}
145
146int32_t Utils::checkAndDupFd(int32_t fd) {
147 if (fd < 0) {
148 ETRACE("not a vliad fd %d", fd);
149 return -1;
150 }
151
152 int32_t dup_fd = ::dup(fd);
153 if (dup_fd < 0) {
154 ETRACE("fd dup failed: %s", strerror(errno));
155 }
156
157 return dup_fd;
158}
159
160#if WITH_LIBPLAYER_MODULE
161bool Utils::checkSysfsStatus(const char* sysfstr, char* lastr, int32_t size) {
162 char val[32];
163 char *p = lastr;
164
165 memset(val, 0, sizeof(val));
166 if (getSysfsStr(sysfstr, val, sizeof(val)) == 0) {
167 DTRACE("val: %s, lastr: %s",val, p);
168 if ((strcmp(val, p) != 0)) {
169 memset(p, 0, size);
170 strcpy(p, val);
171 return true;
172 }
173 }
174
175 return false;
176}
177#endif
178
179bool Utils::checkVinfo(framebuffer_info_t *fbInfo) {
180 if (fbInfo != NULL && fbInfo->fd >= 0) {
181 struct fb_var_screeninfo vinfo;
182 if (ioctl(fbInfo->fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
183 {
184 ETRACE("FBIOGET_VSCREENINFO error!!!");
185 return -errno;
186 }
187
188 if (vinfo.xres != fbInfo->info.xres
189 || vinfo.yres != fbInfo->info.yres
190 || vinfo.width != fbInfo->info.width
191 || vinfo.height != fbInfo->info.height) {
192 if (int32_t(vinfo.width) <= 16 || int32_t(vinfo.height) <= 9) {
193 // the driver doesn't return that information
194 // default to 160 dpi
195 vinfo.width = ((vinfo.xres * 25.4f)/160.0f + 0.5f);
196 vinfo.height = ((vinfo.yres * 25.4f)/160.0f + 0.5f);
197 }
198 fbInfo->xdpi = (vinfo.xres * 25.4f) / vinfo.width;
199 fbInfo->ydpi = (vinfo.yres * 25.4f) / vinfo.height;
200
201 fbInfo->info.xres = vinfo.xres;
202 fbInfo->info.yres = vinfo.yres;
203 fbInfo->info.width = vinfo.width;
204 fbInfo->info.height = vinfo.height;
205 }
206 return true;
207 }
208
209 return false;
210}
211
212const char* Utils::getHotplugUeventEnvelope()
213{
214 return "change@/devices/virtual/switch/hdmi_audio";
215}
216
217const char* Utils::getHdcpUeventEnvelope()
218{
219 return "change@/devices/virtual/switch/hdcp";
220}
221
222const char* Utils::getSwitchState0()
223{
224 return "SWITCH_STATE=0";
225}
226
227const char* Utils::getSwitchState1()
228{
229 return "SWITCH_STATE=1";
230}
231
232} // namespace amlogic
233} // namespace android
234