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