Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The CyanogenMod Project |
| 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 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <cutils/log.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <liblights/samsung_lights_helper.h> |
| 26 | |
| 27 | /* |
| 28 | * Reads an Integer from a file. |
| 29 | * |
| 30 | * @param path The absolute path string. |
| 31 | * @return The Integer with decimal base, -1 on error. |
| 32 | */ |
| 33 | int read_int(char const *path) |
| 34 | { |
| 35 | int fd, len; |
| 36 | int num_bytes = 10; |
| 37 | char buf[11]; |
| 38 | int retval; |
| 39 | |
| 40 | fd = open(path, O_RDONLY); |
| 41 | if (fd < 0) { |
| 42 | ALOGE("%s: failed to open %s\n", __func__, path); |
| 43 | goto fail; |
| 44 | } |
| 45 | |
| 46 | len = read(fd, buf, num_bytes - 1); |
| 47 | if (len < 0) { |
| 48 | ALOGE("%s: failed to read from %s\n", __func__, path); |
| 49 | goto fail; |
| 50 | } |
| 51 | |
| 52 | buf[len] = '\0'; |
| 53 | close(fd); |
| 54 | |
| 55 | // no endptr, decimal base |
| 56 | retval = strtol(buf, NULL, 10); |
| 57 | return retval == 0 ? -1 : retval; |
| 58 | |
| 59 | fail: |
| 60 | if (fd >= 0) |
| 61 | close(fd); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Writes an Integer to a file. |
| 67 | * |
| 68 | * @param path The absolute path string. |
| 69 | * @param value The Integer value to be written. |
| 70 | * @return 0 on success, -1 or errno on error. |
| 71 | */ |
| 72 | int write_int(char const *path, const int value) |
| 73 | { |
| 74 | int fd; |
| 75 | static int already_warned; |
| 76 | |
| 77 | already_warned = 0; |
| 78 | |
| 79 | ALOGV("write_int: path %s, value %d", path, value); |
| 80 | fd = open(path, O_RDWR); |
| 81 | |
| 82 | if (fd >= 0) { |
| 83 | char buffer[20]; |
| 84 | int bytes = sprintf(buffer, "%d\n", value); |
| 85 | int amt = write(fd, buffer, bytes); |
| 86 | close(fd); |
| 87 | return amt == -1 ? -errno : 0; |
| 88 | } else { |
| 89 | if (already_warned == 0) { |
| 90 | ALOGE("write_int failed to open %s\n", path); |
| 91 | already_warned = 1; |
| 92 | } |
| 93 | return -errno; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Set the current button brightness via sysfs. |
| 99 | * |
| 100 | * @param brightness The brightness value. |
| 101 | * @return 0 on success, -1 or errno on error. |
| 102 | */ |
| 103 | inline int set_cur_button_brightness(const int brightness) |
| 104 | { |
| 105 | return write_int(BUTTON_BRIGHTNESS_NODE, brightness); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Read the current panel brightness from sysfs. |
| 110 | * |
| 111 | * @return The brightness as Integer, -1 on error. |
| 112 | */ |
| 113 | inline int get_cur_panel_brightness() |
| 114 | { |
| 115 | return read_int(PANEL_BRIGHTNESS_NODE); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Read the maximum panel brightness from sysfs. |
| 120 | * |
| 121 | * @return The brightness as Integer, -1 on error. |
| 122 | */ |
| 123 | inline int get_max_panel_brightness() |
| 124 | { |
| 125 | return read_int(PANEL_MAX_BRIGHTNESS_NODE); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Set the current panel brightness via sysfs. |
| 130 | * |
| 131 | * @param brightness The (scaled) brightness value. |
| 132 | * @return 0 on success, -1 or errno on error. |
| 133 | */ |
| 134 | inline int set_cur_panel_brightness(const int brightness) |
| 135 | { |
| 136 | return write_int(PANEL_BRIGHTNESS_NODE, brightness); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Set the maximum panel brightness via sysfs. |
| 141 | * |
| 142 | * @param brightness The brightness value. |
| 143 | * @return 0 on success, -1 or errno on error. |
| 144 | */ |
| 145 | inline int set_max_panel_brightness(const int brightness) |
| 146 | { |
| 147 | return write_int(PANEL_MAX_BRIGHTNESS_NODE, brightness); |
| 148 | } |