blob: be59399a3910953526adacc96e51b2d6b9e0d1e2 [file] [log] [blame]
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +01001/*
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. Hessef154b172017-03-04 15:47:45 +010017#define LOG_TAG "SamsungLightsHelper"
18/* #define LOG_NDEBUG 0 */
19
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010020#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. Hessee37aedd2017-04-05 18:55:28 +020034 * @return The Integer with decimal base, -1 or -errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010035 */
Christopher N. Hesse235c2982017-04-02 17:20:16 +020036static int read_int(char const *path)
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010037{
38 int fd, len;
Christopher N. Hesse235c2982017-04-02 17:20:16 +020039 int ret = 0;
40 const int INT_MAX_STRLEN = 10;
41 char buffer[INT_MAX_STRLEN+1];
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010042
43 fd = open(path, O_RDONLY);
44 if (fd < 0) {
Christopher N. Hesse235c2982017-04-02 17:20:16 +020045 ret = -errno;
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010046 ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
47 goto exit;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010048 }
49
Christopher N. Hesse235c2982017-04-02 17:20:16 +020050 len = read(fd, buffer, INT_MAX_STRLEN-1);
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010051 if (len < 0) {
Christopher N. Hesse235c2982017-04-02 17:20:16 +020052 ret = -errno;
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010053 ALOGE("%s: failed to read from %s (%s)", __func__, path, strerror(errno));
54 goto exit;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010055 }
56
Christopher N. Hesse235c2982017-04-02 17:20:16 +020057 buffer[len] = '\0';
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010058
Christopher N. Hesse235c2982017-04-02 17:20:16 +020059 /* now parse the integer from buffer */
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010060 char *endptr = NULL;
Christopher N. Hesse235c2982017-04-02 17:20:16 +020061 ret = (int) strtol(buffer, &endptr, 10);
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010062 if (ret == 0 && endptr == NULL) {
63 ret = -1;
Christopher N. Hesse235c2982017-04-02 17:20:16 +020064 ALOGE("%s: failed to extract number from string %s", __func__, buffer);
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010065 goto exit;
66 }
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010067
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010068exit:
Christopher N. Hessee37aedd2017-04-05 18:55:28 +020069 if (fd >= 0)
70 close(fd);
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010071 return ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010072}
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. Hessee37aedd2017-04-05 18:55:28 +020079 * @return 0 on success, -errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010080 */
Christopher N. Hesse235c2982017-04-02 17:20:16 +020081static int write_int(char const *path, const int value)
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010082{
Christopher N. Hesse235c2982017-04-02 17:20:16 +020083 int fd, len, num_bytes;
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010084 int ret = 0;
Christopher N. Hesse235c2982017-04-02 17:20:16 +020085 const int INT_MAX_STRLEN = 10;
86 char buffer[INT_MAX_STRLEN+1];
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010087
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010088 fd = open(path, O_WRONLY);
89 if (fd < 0) {
Christopher N. Hesse235c2982017-04-02 17:20:16 +020090 ret = -errno;
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010091 ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
92 goto exit;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010093 }
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010094
Christopher N. Hesse235c2982017-04-02 17:20:16 +020095 num_bytes = sprintf(buffer, "%d", value);
96 len = write(fd, buffer, num_bytes);
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010097 if (len < 0) {
Christopher N. Hesse235c2982017-04-02 17:20:16 +020098 ret = -errno;
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010099 ALOGE("%s: failed to write to %s (%s)", __func__, path, strerror(errno));
100 goto exit;
101 }
102
103exit:
Christopher N. Hessee37aedd2017-04-05 18:55:28 +0200104 if (fd >= 0)
105 close(fd);
Christopher N. Hesse209f5de2017-03-04 15:16:56 +0100106 return ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100107}
108
109/*
110 * Set the current button brightness via sysfs.
111 *
112 * @param brightness The brightness value.
Christopher N. Hesse209f5de2017-03-04 15:16:56 +0100113 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100114 */
115inline 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 */
125inline 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 */
135inline 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. Hesse209f5de2017-03-04 15:16:56 +0100144 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100145 */
146inline 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. Hesse209f5de2017-03-04 15:16:56 +0100155 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100156 */
157inline int set_max_panel_brightness(const int brightness)
158{
159 return write_int(PANEL_MAX_BRIGHTNESS_NODE, brightness);
160}