blob: 3e9b17f82fb91295e5132bde99c48205d5061e62 [file] [log] [blame]
Jan Altensen53afa332019-07-09 22:43:47 +02001/*
2 * Copyright (C) 2019 The LineageOS 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#define LOG_TAG "android.hardware.light@2.0-service.samsung"
17
18#include <iomanip>
19
20#include "Light.h"
21
22#define COLOR_MASK 0x00ffffff
23#define MAX_INPUT_BRIGHTNESS 255
24
25using android::hardware::light::V2_0::LightState;
26using android::hardware::light::V2_0::Status;
27using android::hardware::light::V2_0::Type;
28
29namespace android {
30namespace hardware {
31namespace light {
32namespace V2_0 {
33namespace implementation {
34
35/*
36 * Write value to path and close file.
37 */
38template <typename T>
39static void set(const std::string& path, const T& value) {
40 std::ofstream file(path);
41 file << value << std::endl;
42}
43
44template <typename T>
45static T get(const std::string& path, const T& def) {
46 std::ifstream file(path);
47 T result;
48
49 file >> result;
50 return file.fail() ? def : result;
51}
52
53Light::Light() {
54 mLights.emplace(Type::BACKLIGHT,
55 std::bind(&Light::handleBacklight, this, std::placeholders::_1));
56#ifdef BUTTON_BRIGHTNESS_NODE
57 mLights.emplace(Type::BUTTONS, std::bind(&Light::handleButtons, this, std::placeholders::_1));
58#endif
59 mLights.emplace(Type::BATTERY, std::bind(&Light::handleBattery, this, std::placeholders::_1));
60 mLights.emplace(Type::NOTIFICATIONS,
61 std::bind(&Light::handleNotifications, this, std::placeholders::_1));
62 mLights.emplace(Type::ATTENTION,
63 std::bind(&Light::handleAttention, this, std::placeholders::_1));
64}
65
66Return<Status> Light::setLight(Type type, const LightState& state) {
67 auto it = mLights.find(type);
68
69 if (it == mLights.end()) {
70 return Status::LIGHT_NOT_SUPPORTED;
71 }
72
73 /*
74 * Lock global mutex until light state is updated.
75 */
76 std::lock_guard<std::mutex> lock(mLock);
77
78 it->second(state);
79
80 return Status::SUCCESS;
81}
82
83void Light::handleBacklight(const LightState& state) {
84 uint32_t max_brightness = get(PANEL_MAX_BRIGHTNESS_NODE, MAX_INPUT_BRIGHTNESS);
85 uint32_t brightness = rgbToBrightness(state);
86
87 if (max_brightness != MAX_INPUT_BRIGHTNESS) {
88 brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS;
89 }
90
91 set(PANEL_BRIGHTNESS_NODE, brightness);
92}
93
94#ifdef BUTTON_BRIGHTNESS_NODE
95void Light::handleButtons(const LightState& state) {
96#ifdef VAR_BUTTON_BRIGHTNESS
97 uint32_t brightness = rgbToBrightness(state);
98#else
99 uint32_t brightness = (state.color & COLOR_MASK) ? 1 : 0;
100#endif
101
102 set(BUTTON_BRIGHTNESS_NODE, brightness);
103}
104#endif
105
106void Light::handleBattery(const LightState& state) {
107 mBatteryState = state;
108 setNotificationLED();
109}
110
111void Light::handleNotifications(const LightState& state) {
112 mNotificationState = state;
113 setNotificationLED();
114}
115
116void Light::handleAttention(const LightState& state) {
117 mAttentionState = state;
118 setNotificationLED();
119}
120
121void Light::setNotificationLED() {
122 int32_t adjusted_brightness = MAX_INPUT_BRIGHTNESS;
123 LightState state;
124#ifdef LED_BLN_NODE
125 bool bln = false;
126#endif
127
Jan Altensen7ffb4952019-08-07 11:33:32 +0200128 if (mNotificationState.color & COLOR_MASK) {
Jan Altensen53afa332019-07-09 22:43:47 +0200129 adjusted_brightness = LED_BRIGHTNESS_NOTIFICATION;
130 state = mNotificationState;
131#ifdef LED_BLN_NODE
132 bln = true;
133#endif
134 } else if (mAttentionState.color & COLOR_MASK) {
135 adjusted_brightness = LED_BRIGHTNESS_ATTENTION;
136 state = mAttentionState;
137 if (state.flashMode == Flash::HARDWARE) {
138 if (state.flashOnMs > 0 && state.flashOffMs == 0) state.flashMode = Flash::NONE;
139 state.color = 0x000000ff;
140 }
141 if (state.flashMode == Flash::NONE) {
142 state.color = 0;
143 }
Jan Altensen7ffb4952019-08-07 11:33:32 +0200144 } else if (mBatteryState.color & COLOR_MASK) {
145 adjusted_brightness = LED_BRIGHTNESS_BATTERY;
146 state = mBatteryState;
Jan Altensen53afa332019-07-09 22:43:47 +0200147 } else {
148 set(LED_BLINK_NODE, "0x00000000 0 0");
149 return;
150 }
151
152 if (state.flashMode == Flash::NONE) {
153 state.flashOnMs = 0;
154 state.flashOffMs = 0;
155 }
156
157 state.color = calibrateColor(state.color & COLOR_MASK, adjusted_brightness);
158 std::stringstream ss;
159 ss << std::hex << "0x" << std::setfill('0') << std::setw(8) << state.color << std::dec
160 << " " << state.flashOnMs << " " << state.flashOffMs;
161 set(LED_BLINK_NODE, ss.str());
162
163#ifdef LED_BLN_NODE
164 if (bln) {
165 set(LED_BLN_NODE, (state.color & COLOR_MASK) ? 1 : 0);
166 }
167#endif
168}
169
170Return<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
171 std::vector<Type> types;
172
173 for (auto const& light : mLights) {
174 types.push_back(light.first);
175 }
176
177 _hidl_cb(types);
178
179 return Void();
180}
181
182uint32_t Light::rgbToBrightness(const LightState& state) {
183 uint32_t color = state.color & COLOR_MASK;
184
185 return ((77 * ((color >> 16) & 0xff)) + (150 * ((color >> 8) & 0xff)) + (29 * (color & 0xff))) >>
186 8;
187}
188
189uint32_t Light::calibrateColor(uint32_t color, int32_t brightness) {
190 uint32_t red = ((color >> 16) & 0xFF) * LED_ADJUSTMENT_R;
191 uint32_t green = ((color >> 8) & 0xFF) * LED_ADJUSTMENT_G;
192 uint32_t blue = (color & 0xFF) * LED_ADJUSTMENT_B;
193
194 return (((red * brightness) / 255) << 16) + (((green * brightness) / 255) << 8) +
195 ((blue * brightness) / 255);
196}
197
198} // namespace implementation
199} // namespace V2_0
200} // namespace light
201} // namespace hardware
202} // namespace android