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