blob: 5fd8b12de5349fa680bde353c3c3c9a6b6e2c802 [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. Hesse209f5de2017-03-04 15:16:56 +010034 * @return The Integer with decimal base, -1 or errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010035 */
36int read_int(char const *path)
37{
38 int fd, len;
39 int num_bytes = 10;
40 char buf[11];
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010041 int ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010042
43 fd = open(path, O_RDONLY);
44 if (fd < 0) {
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010045 ret = errno;
46 ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
47 goto exit;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010048 }
49
50 len = read(fd, buf, num_bytes - 1);
51 if (len < 0) {
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010052 ret = errno;
53 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
57 buf[len] = '\0';
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010058
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010059 // 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. Hesse898e1fe2016-12-07 12:12:23 +010067
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010068exit:
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010069 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. Hesse209f5de2017-03-04 15:16:56 +010079 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010080 */
81int write_int(char const *path, const int value)
82{
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010083 int fd, len;
84 int ret = 0;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010085
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010086 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. Hesse898e1fe2016-12-07 12:12:23 +010091 }
Christopher N. Hesse209f5de2017-03-04 15:16:56 +010092
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
102exit:
103 if (fd >= 0)
104 close(fd);
105 return ret;
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100106}
107
108/*
109 * Set the current button brightness via sysfs.
110 *
111 * @param brightness The brightness value.
Christopher N. Hesse209f5de2017-03-04 15:16:56 +0100112 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100113 */
114inline 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 */
124inline 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 */
134inline 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. Hesse209f5de2017-03-04 15:16:56 +0100143 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100144 */
145inline 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. Hesse209f5de2017-03-04 15:16:56 +0100154 * @return 0 on success, errno on error.
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100155 */
156inline int set_max_panel_brightness(const int brightness)
157{
158 return write_int(PANEL_MAX_BRIGHTNESS_NODE, brightness);
159}