blob: 565101b17d6f29d3079cdf3c5e42c9735636677c [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
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. Hesse209f5de2017-03-04 15:16:56 +010031 * @return The Integer with decimal base, -1 or errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010032 */
33int read_int(char const *path)
34{
35 int fd, len;
36 int num_bytes = 10;
37 char buf[11];
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010038 int ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010039
40 fd = open(path, O_RDONLY);
41 if (fd < 0) {
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010042 ret = errno;
43 ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
44 goto exit;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010045 }
46
47 len = read(fd, buf, num_bytes - 1);
48 if (len < 0) {
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010049 ret = errno;
50 ALOGE("%s: failed to read from %s (%s)", __func__, path, strerror(errno));
51 goto exit;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010052 }
53
54 buf[len] = '\0';
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010055
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010056 // 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. Hesse898e1fe2016-12-07 12:12:23 +010064
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010065exit:
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010066 if (fd >= 0)
67 close(fd);
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010068 return ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010069}
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. Hesse209f5de2017-03-04 15:16:56 +010076 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010077 */
78int write_int(char const *path, const int value)
79{
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010080 int fd, len;
81 int ret = 0;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010082
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010083 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. Hesse898e1fe2016-12-07 12:12:23 +010088 }
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010089
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
99exit:
100 if (fd >= 0)
101 close(fd);
102 return ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100103}
104
105/*
106 * Set the current button brightness via sysfs.
107 *
108 * @param brightness The brightness value.
Christopher N. Hesse209f5de2017-03-04 15:16:56 +0100109 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100110 */
111inline 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 */
121inline 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 */
131inline 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. Hesse209f5de2017-03-04 15:16:56 +0100140 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100141 */
142inline 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. Hesse209f5de2017-03-04 15:16:56 +0100151 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100152 */
153inline int set_max_panel_brightness(const int brightness)
154{
155 return write_int(PANEL_MAX_BRIGHTNESS_NODE, brightness);
156}