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