1 /*
2 * Copyright (C) 2019 The Android Open Source 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 #define LOG_TAG "light"
18
19 #include <log/log.h>
20
21 #include <stdio.h>
22
23 #include "Light.h"
24
25 namespace android {
26 namespace hardware {
27 namespace light {
28 namespace V2_0 {
29 namespace implementation {
30
31 static_assert(LIGHT_FLASH_NONE == static_cast<int>(Flash::NONE),
32 "Flash::NONE must match legacy value.");
33 static_assert(LIGHT_FLASH_TIMED == static_cast<int>(Flash::TIMED),
34 "Flash::TIMED must match legacy value.");
35 static_assert(LIGHT_FLASH_HARDWARE == static_cast<int>(Flash::HARDWARE),
36 "Flash::HARDWARE must match legacy value.");
37
38 static_assert(BRIGHTNESS_MODE_USER == static_cast<int>(Brightness::USER),
39 "Brightness::USER must match legacy value.");
40 static_assert(BRIGHTNESS_MODE_SENSOR == static_cast<int>(Brightness::SENSOR),
41 "Brightness::SENSOR must match legacy value.");
42 static_assert(BRIGHTNESS_MODE_LOW_PERSISTENCE ==
43 static_cast<int>(Brightness::LOW_PERSISTENCE),
44 "Brightness::LOW_PERSISTENCE must match legacy value.");
45
Light(std::map<Type,light_device_t * > && lights)46 Light::Light(std::map<Type, light_device_t*>&& lights)
47 : mLights(std::move(lights)) {}
48
49 // Methods from ::android::hardware::light::V2_0::ILight follow.
setLight(Type type,const LightState & state)50 Return<Status> Light::setLight(Type type, const LightState& state) {
51 auto it = mLights.find(type);
52
53 if (it == mLights.end()) return Status::LIGHT_NOT_SUPPORTED;
54
55 light_device_t* hwLight = it->second;
56
57 light_state_t legacyState{
58 .color = state.color,
59 .flashMode = static_cast<int>(state.flashMode),
60 .flashOnMS = state.flashOnMs,
61 .flashOffMS = state.flashOffMs,
62 .brightnessMode = static_cast<int>(state.brightnessMode),
63 };
64
65 int ret = hwLight->set_light(hwLight, &legacyState);
66
67 switch (ret) {
68 case -ENOSYS:
69 return Status::BRIGHTNESS_NOT_SUPPORTED;
70 case 0:
71 return Status::SUCCESS;
72 default:
73 return Status::UNKNOWN;
74 }
75 }
76
getSupportedTypes(getSupportedTypes_cb _hidl_cb)77 Return<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) {
78 std::vector<Type> types(mLights.size());
79
80 int idx = 0;
81 for (const auto& pair : mLights) {
82 types[idx++] = pair.first;
83 }
84
85 hidl_vec<Type> hidl_types{};
86 hidl_types.setToExternal(types.data(), types.size());
87
88 _hidl_cb(hidl_types);
89
90 return Void();
91 }
92
93 static const std::map<Type, const char*> kLogicalLights = {
94 {Type::BACKLIGHT, LIGHT_ID_BACKLIGHT},
95 {Type::KEYBOARD, LIGHT_ID_KEYBOARD},
96 {Type::BUTTONS, LIGHT_ID_BUTTONS},
97 {Type::BATTERY, LIGHT_ID_BATTERY},
98 {Type::NOTIFICATIONS, LIGHT_ID_NOTIFICATIONS},
99 {Type::ATTENTION, LIGHT_ID_ATTENTION},
100 {Type::BLUETOOTH, LIGHT_ID_BLUETOOTH},
101 {Type::WIFI, LIGHT_ID_WIFI}};
102
debug(const hidl_handle & handle,const hidl_vec<hidl_string> &)103 Return<void> Light::debug(const hidl_handle& handle,
104 const hidl_vec<hidl_string>& /* options */) {
105 if (handle == nullptr || handle->numFds < 1) {
106 ALOGE("debug called with no handle\n");
107 return Void();
108 }
109
110 int fd = handle->data[0];
111 if (fd < 0) {
112 ALOGE("invalid FD: %d\n", handle->data[0]);
113 return Void();
114 }
115
116 dprintf(fd, "The following lights are registered: ");
117 for (const auto& pair : mLights) {
118 const Type type = pair.first;
119 dprintf(fd, "%s,", kLogicalLights.at(type));
120 }
121 dprintf(fd, ".\n");
122 fsync(fd);
123 return Void();
124 }
125
getLightDevice(const char * name)126 light_device_t* getLightDevice(const char* name) {
127 light_device_t* lightDevice;
128 const hw_module_t* hwModule = NULL;
129
130 int ret = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, &hwModule);
131 if (ret == 0) {
132 ret = hwModule->methods->open(
133 hwModule, name, reinterpret_cast<hw_device_t**>(&lightDevice));
134 if (ret != 0)
135 ALOGE("light_open %s %s failed: %d", LIGHTS_HARDWARE_MODULE_ID, name,
136 ret);
137 } else {
138 ALOGE("hw_get_module %s %s failed: %d", LIGHTS_HARDWARE_MODULE_ID, name,
139 ret);
140 }
141
142 if (ret == 0) {
143 return lightDevice;
144 } else {
145 ALOGE("Light passthrough failed to load legacy HAL.");
146 return nullptr;
147 }
148 }
149
HIDL_FETCH_ILight(const char *)150 ILight* HIDL_FETCH_ILight(const char* /* name */) {
151 std::map<Type, light_device_t*> lights;
152
153 for (const auto& pair : kLogicalLights) {
154 Type type = pair.first;
155 const char* name = pair.second;
156
157 light_device_t* light = getLightDevice(name);
158
159 if (light != nullptr) lights[type] = light;
160 }
161
162 if (lights.size() == 0) {
163 // Log information, but still return new Light.
164 // Some devices may not have any lights.
165 ALOGI("Could not open any lights.");
166 }
167
168 return new Light(std::move(lights));
169 }
170
171 } // namespace implementation
172 } // namespace V2_0
173 } // namespace light
174 } // namespace hardware
175 } // namespace android
176