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