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 | */ |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "light" |
Mark Salyzyn | 3ff5260 | 2017-01-10 10:16:48 -0800 | [diff] [blame] | 18 | |
| 19 | #include <log/log.h> |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 20 | |
Steven Moreland | b0c20e9 | 2017-12-20 14:50:53 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | |
Steven Moreland | 81f5da9 | 2016-09-30 16:32:24 -0700 | [diff] [blame] | 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 | |
| 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. |
| 50 | Return<Status> Light::setLight(Type type, const LightState& state) { |
| 51 | auto it = mLights.find(type); |
| 52 | |
| 53 | if (it == mLights.end()) { |
| 54 | return Status::LIGHT_NOT_SUPPORTED; |
| 55 | } |
| 56 | |
| 57 | light_device_t* hwLight = it->second; |
| 58 | |
| 59 | light_state_t legacyState { |
| 60 | .color = state.color, |
| 61 | .flashMode = static_cast<int>(state.flashMode), |
| 62 | .flashOnMS = state.flashOnMs, |
| 63 | .flashOffMS = state.flashOffMs, |
| 64 | .brightnessMode = static_cast<int>(state.brightnessMode), |
| 65 | }; |
| 66 | |
| 67 | int ret = hwLight->set_light(hwLight, &legacyState); |
| 68 | |
| 69 | switch (ret) { |
| 70 | case -ENOSYS: |
| 71 | return Status::BRIGHTNESS_NOT_SUPPORTED; |
| 72 | case 0: |
| 73 | return Status::SUCCESS; |
| 74 | default: |
| 75 | return Status::UNKNOWN; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Return<void> Light::getSupportedTypes(getSupportedTypes_cb _hidl_cb) { |
| 80 | Type *types = new Type[mLights.size()]; |
| 81 | |
| 82 | int idx = 0; |
| 83 | for(auto const &pair : mLights) { |
| 84 | Type type = pair.first; |
| 85 | |
| 86 | types[idx++] = type; |
| 87 | } |
| 88 | |
| 89 | { |
| 90 | hidl_vec<Type> hidl_types{}; |
| 91 | hidl_types.setToExternal(types, mLights.size()); |
| 92 | |
| 93 | _hidl_cb(hidl_types); |
| 94 | } |
| 95 | |
| 96 | delete[] types; |
| 97 | |
| 98 | return Void(); |
| 99 | } |
| 100 | |
| 101 | const static std::map<Type, const char*> kLogicalLights = { |
| 102 | {Type::BACKLIGHT, LIGHT_ID_BACKLIGHT}, |
| 103 | {Type::KEYBOARD, LIGHT_ID_KEYBOARD}, |
| 104 | {Type::BUTTONS, LIGHT_ID_BUTTONS}, |
| 105 | {Type::BATTERY, LIGHT_ID_BATTERY}, |
| 106 | {Type::NOTIFICATIONS, LIGHT_ID_NOTIFICATIONS}, |
| 107 | {Type::ATTENTION, LIGHT_ID_ATTENTION}, |
| 108 | {Type::BLUETOOTH, LIGHT_ID_BLUETOOTH}, |
| 109 | {Type::WIFI, LIGHT_ID_WIFI} |
| 110 | }; |
| 111 | |
Steven Moreland | b0c20e9 | 2017-12-20 14:50:53 -0800 | [diff] [blame] | 112 | Return<void> Light::debug(const hidl_handle& handle, const hidl_vec<hidl_string>& /* options */) { |
| 113 | if (handle == nullptr || handle->numFds < 1) { |
| 114 | ALOGE("debug called with no handle\n"); |
| 115 | return Void(); |
| 116 | } |
| 117 | |
| 118 | int fd = handle->data[0]; |
| 119 | if (fd < 0) { |
| 120 | ALOGE("invalid FD: %d\n", handle->data[0]); |
| 121 | return Void(); |
| 122 | } |
| 123 | |
| 124 | dprintf(fd, "The following lights are registered: "); |
| 125 | for (auto const& pair : mLights) { |
| 126 | const Type type = pair.first; |
| 127 | dprintf(fd, "%s,", kLogicalLights.at(type)); |
| 128 | } |
| 129 | dprintf(fd, ".\n"); |
| 130 | fsync(fd); |
| 131 | return Void(); |
| 132 | } |
| 133 | |
Steven Moreland | 81f5da9 | 2016-09-30 16:32:24 -0700 | [diff] [blame] | 134 | light_device_t* getLightDevice(const char* name) { |
| 135 | light_device_t* lightDevice; |
| 136 | const hw_module_t* hwModule = NULL; |
| 137 | |
| 138 | int ret = hw_get_module (LIGHTS_HARDWARE_MODULE_ID, &hwModule); |
| 139 | if (ret == 0) { |
| 140 | ret = hwModule->methods->open(hwModule, name, |
| 141 | reinterpret_cast<hw_device_t**>(&lightDevice)); |
| 142 | if (ret != 0) { |
| 143 | ALOGE("light_open %s %s failed: %d", LIGHTS_HARDWARE_MODULE_ID, name, ret); |
| 144 | } |
| 145 | } else { |
| 146 | ALOGE("hw_get_module %s %s failed: %d", LIGHTS_HARDWARE_MODULE_ID, name, ret); |
| 147 | } |
| 148 | |
| 149 | if (ret == 0) { |
| 150 | return lightDevice; |
| 151 | } else { |
| 152 | ALOGE("Light passthrough failed to load legacy HAL."); |
| 153 | return nullptr; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ILight* HIDL_FETCH_ILight(const char* /* name */) { |
| 158 | std::map<Type, light_device_t*> lights; |
| 159 | |
| 160 | for(auto const &pair : kLogicalLights) { |
| 161 | Type type = pair.first; |
| 162 | const char* name = pair.second; |
| 163 | |
| 164 | light_device_t* light = getLightDevice(name); |
| 165 | |
| 166 | if (light != nullptr) { |
| 167 | lights[type] = light; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (lights.size() == 0) { |
| 172 | // Log information, but still return new Light. |
| 173 | // Some devices may not have any lights. |
| 174 | ALOGI("Could not open any lights."); |
| 175 | } |
| 176 | |
| 177 | return new Light(std::move(lights)); |
| 178 | } |
| 179 | |
| 180 | } // namespace implementation |
| 181 | } // namespace V2_0 |
| 182 | } // namespace light |
| 183 | } // namespace hardware |
| 184 | } // namespace android |