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 | e37aedd | 2017-04-05 18:55:28 +0200 | [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 | */ |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 36 | static int read_int(char const *path) |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 37 | { |
| 38 | int fd, len; |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 39 | int ret = 0; |
| 40 | const int INT_MAX_STRLEN = 10; |
| 41 | char buffer[INT_MAX_STRLEN+1]; |
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 | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 45 | ret = -errno; |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 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 | |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 50 | len = read(fd, buffer, INT_MAX_STRLEN-1); |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 51 | if (len < 0) { |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 52 | ret = -errno; |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 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 | |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 57 | buffer[len] = '\0'; |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 58 | |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 59 | /* now parse the integer from buffer */ |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 60 | char *endptr = NULL; |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 61 | ret = (int) strtol(buffer, &endptr, 10); |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 62 | if (ret == 0 && endptr == NULL) { |
| 63 | ret = -1; |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 64 | ALOGE("%s: failed to extract number from string %s", __func__, buffer); |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 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 | e37aedd | 2017-04-05 18:55:28 +0200 | [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 | e37aedd | 2017-04-05 18:55:28 +0200 | [diff] [blame] | 79 | * @return 0 on success, -errno on error. |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 80 | */ |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 81 | static int write_int(char const *path, const int value) |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 82 | { |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 83 | int fd, len, num_bytes; |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 84 | int ret = 0; |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 85 | const int INT_MAX_STRLEN = 10; |
| 86 | char buffer[INT_MAX_STRLEN+1]; |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 87 | |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 88 | fd = open(path, O_WRONLY); |
| 89 | if (fd < 0) { |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 90 | ret = -errno; |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 91 | ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno)); |
| 92 | goto exit; |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 93 | } |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 94 | |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 95 | num_bytes = sprintf(buffer, "%d", value); |
| 96 | len = write(fd, buffer, num_bytes); |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 97 | if (len < 0) { |
Christopher N. Hesse | 235c298 | 2017-04-02 17:20:16 +0200 | [diff] [blame] | 98 | ret = -errno; |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 99 | ALOGE("%s: failed to write to %s (%s)", __func__, path, strerror(errno)); |
| 100 | goto exit; |
| 101 | } |
| 102 | |
| 103 | exit: |
Christopher N. Hesse | e37aedd | 2017-04-05 18:55:28 +0200 | [diff] [blame] | 104 | if (fd >= 0) |
| 105 | close(fd); |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 106 | return ret; |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Set the current button brightness via sysfs. |
| 111 | * |
| 112 | * @param brightness The brightness value. |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 113 | * @return 0 on success, errno on error. |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 114 | */ |
| 115 | inline int set_cur_button_brightness(const int brightness) |
| 116 | { |
| 117 | return write_int(BUTTON_BRIGHTNESS_NODE, brightness); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Read the current panel brightness from sysfs. |
| 122 | * |
| 123 | * @return The brightness as Integer, -1 on error. |
| 124 | */ |
| 125 | inline int get_cur_panel_brightness() |
| 126 | { |
| 127 | return read_int(PANEL_BRIGHTNESS_NODE); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Read the maximum panel brightness from sysfs. |
| 132 | * |
| 133 | * @return The brightness as Integer, -1 on error. |
| 134 | */ |
| 135 | inline int get_max_panel_brightness() |
| 136 | { |
| 137 | return read_int(PANEL_MAX_BRIGHTNESS_NODE); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Set the current panel brightness via sysfs. |
| 142 | * |
| 143 | * @param brightness The (scaled) brightness value. |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 144 | * @return 0 on success, errno on error. |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 145 | */ |
| 146 | inline int set_cur_panel_brightness(const int brightness) |
| 147 | { |
| 148 | return write_int(PANEL_BRIGHTNESS_NODE, brightness); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Set the maximum panel brightness via sysfs. |
| 153 | * |
| 154 | * @param brightness The brightness value. |
Christopher N. Hesse | 209f5de | 2017-03-04 15:16:56 +0100 | [diff] [blame] | 155 | * @return 0 on success, errno on error. |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 156 | */ |
| 157 | inline int set_max_panel_brightness(const int brightness) |
| 158 | { |
| 159 | return write_int(PANEL_MAX_BRIGHTNESS_NODE, brightness); |
| 160 | } |