Aaron Kling | b2b3dd0 | 2015-11-12 09:25:04 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved. |
| 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 | #define LOG_TAG "powerHAL::common" |
| 17 | |
| 18 | #include "powerhal_utils.h" |
| 19 | |
| 20 | void sysfs_write(const char *path, const char *s) |
| 21 | { |
| 22 | char buf[80]; |
| 23 | int len; |
| 24 | int fd = open(path, O_WRONLY); |
| 25 | |
| 26 | if (fd < 0) { |
| 27 | strerror_r(errno, buf, sizeof(buf)); |
| 28 | ALOGE("Error opening %s: %s\n", path, buf); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | len = write(fd, s, strlen(s)); |
| 33 | if (len < 0) { |
| 34 | strerror_r(errno, buf, sizeof(buf)); |
| 35 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 36 | } |
| 37 | close(fd); |
| 38 | } |
| 39 | |
| 40 | void sysfs_read(const char *path, char *s, int size) |
| 41 | { |
| 42 | int len; |
| 43 | int fd = open(path, O_RDONLY); |
| 44 | |
| 45 | if (fd < 0) { |
| 46 | strerror_r(errno, s, size); |
| 47 | ALOGE("Error opening %s: %s\n", path, s); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | len = read(fd, s, size); |
| 52 | close(fd); |
| 53 | |
| 54 | if (len < 0) { |
| 55 | strerror_r(errno, s, size); |
| 56 | ALOGE("Error reading from %s: %s\n", path, s); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool sysfs_exists(const char *path) |
| 61 | { |
| 62 | bool val; |
| 63 | int fd = open(path, O_RDONLY); |
| 64 | |
| 65 | val = fd < 0 ? false : true; |
| 66 | close(fd); |
| 67 | |
| 68 | return val; |
| 69 | } |
| 70 | |
| 71 | bool get_property_bool(const char *key, bool default_value) |
| 72 | { |
| 73 | char value[PROPERTY_VALUE_MAX]; |
| 74 | |
| 75 | if (property_get(key, value, NULL) > 0) { |
| 76 | if (!strcmp(value, "1") || !strcasecmp(value, "on") || |
| 77 | !strcasecmp(value, "true")) { |
| 78 | return true; |
| 79 | } |
| 80 | if (!strcmp(value, "0") || !strcasecmp(value, "off") || |
| 81 | !strcasecmp(value, "false")) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return default_value; |
| 87 | } |
| 88 | |
| 89 | void set_property_int(const char *key, int value) |
| 90 | { |
| 91 | char val[PROPERTY_VALUE_MAX]; |
| 92 | int status; |
| 93 | |
| 94 | snprintf(val, sizeof(val), "%d", value); |
| 95 | status = property_set(key, val); |
| 96 | |
| 97 | if (status) { |
| 98 | ALOGE("Error writing to property: %s\n", key); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void sysfs_write_int(const char *path, int value) |
| 103 | { |
| 104 | char val[PROPERTY_VALUE_MAX]; |
| 105 | |
| 106 | snprintf(val, sizeof(val), "%d", value); |
| 107 | sysfs_write(path, val); |
| 108 | } |