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