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