forked from zombi-x/android_hardware_nvidia_power
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerhal_utils.cpp
108 lines (89 loc) · 2.57 KB
/
powerhal_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (C) 2012 The Android Open Source Project
* Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "powerHAL::common"
#include "powerhal_utils.h"
void sysfs_write(const char *path, const char *s)
{
char buf[80];
int len;
int fd = open(path, O_WRONLY);
if (fd < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error opening %s: %s\n", path, buf);
return;
}
len = write(fd, s, strlen(s));
if (len < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error writing to %s: %s\n", path, buf);
}
close(fd);
}
void sysfs_read(const char *path, char *s, int size)
{
int len;
int fd = open(path, O_RDONLY);
if (fd < 0) {
strerror_r(errno, s, size);
ALOGE("Error opening %s: %s\n", path, s);
return;
}
len = read(fd, s, size);
close(fd);
if (len < 0) {
strerror_r(errno, s, size);
ALOGE("Error reading from %s: %s\n", path, s);
}
}
bool sysfs_exists(const char *path)
{
bool val;
int fd = open(path, O_RDONLY);
val = fd < 0 ? false : true;
close(fd);
return val;
}
bool get_property_bool(const char *key, bool default_value)
{
char value[PROPERTY_VALUE_MAX];
if (property_get(key, value, NULL) > 0) {
if (!strcmp(value, "1") || !strcasecmp(value, "on") ||
!strcasecmp(value, "true")) {
return true;
}
if (!strcmp(value, "0") || !strcasecmp(value, "off") ||
!strcasecmp(value, "false")) {
return false;
}
}
return default_value;
}
void set_property_int(const char *key, int value)
{
char val[PROPERTY_VALUE_MAX];
int status;
snprintf(val, sizeof(val), "%d", value);
status = property_set(key, val);
if (status) {
ALOGE("Error writing to property: %s\n", key);
}
}
void sysfs_write_int(const char *path, int value)
{
char val[PROPERTY_VALUE_MAX];
snprintf(val, sizeof(val), "%d", value);
sysfs_write(path, val);
}