| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2021 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 | #include <locale> | 
|  | 18 | #include <regex> | 
|  | 19 |  | 
|  | 20 | #include "../Macros.h" | 
|  | 21 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 22 | #include <ftl/NamedEnum.h> | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 23 | #include "PeripheralController.h" | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 24 |  | 
|  | 25 | // Log detailed debug messages about input device lights. | 
|  | 26 | static constexpr bool DEBUG_LIGHT_DETAILS = false; | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | static inline int32_t getAlpha(int32_t color) { | 
|  | 31 | return (color >> 24) & 0xff; | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | static inline int32_t getRed(int32_t color) { | 
|  | 35 | return (color >> 16) & 0xff; | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | static inline int32_t getGreen(int32_t color) { | 
|  | 39 | return (color >> 8) & 0xff; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | static inline int32_t getBlue(int32_t color) { | 
|  | 43 | return color & 0xff; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | static inline int32_t toArgb(int32_t brightness, int32_t red, int32_t green, int32_t blue) { | 
|  | 47 | return (brightness & 0xff) << 24 | (red & 0xff) << 16 | (green & 0xff) << 8 | (blue & 0xff); | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | /** | 
|  | 51 | * Input controller owned by InputReader device, implements the native API for querying input | 
|  | 52 | * lights, getting and setting the lights brightness and color, by interacting with EventHub | 
|  | 53 | * devices. | 
|  | 54 | */ | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 55 | PeripheralController::PeripheralController(InputDeviceContext& deviceContext) | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 56 | : mDeviceContext(deviceContext) { | 
|  | 57 | configureBattries(); | 
|  | 58 | configureLights(); | 
|  | 59 | } | 
|  | 60 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 61 | PeripheralController::~PeripheralController() {} | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 62 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 63 | std::optional<std::int32_t> PeripheralController::Light::getRawLightBrightness(int32_t rawLightId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 64 | std::optional<RawLightInfo> rawInfoOpt = context.getRawLightInfo(rawLightId); | 
|  | 65 | if (!rawInfoOpt.has_value()) { | 
|  | 66 | return std::nullopt; | 
|  | 67 | } | 
|  | 68 | std::optional<int32_t> brightnessOpt = context.getLightBrightness(rawLightId); | 
|  | 69 | if (!brightnessOpt.has_value()) { | 
|  | 70 | return std::nullopt; | 
|  | 71 | } | 
|  | 72 | int brightness = brightnessOpt.value(); | 
|  | 73 |  | 
|  | 74 | // If the light node doesn't have max brightness, use the default max brightness. | 
|  | 75 | int rawMaxBrightness = rawInfoOpt->maxBrightness.value_or(MAX_BRIGHTNESS); | 
|  | 76 | float ratio = MAX_BRIGHTNESS / rawMaxBrightness; | 
|  | 77 | // Scale the returned brightness in [0, rawMaxBrightness] to [0, 255] | 
|  | 78 | if (rawMaxBrightness != MAX_BRIGHTNESS) { | 
|  | 79 | brightness = brightness * ratio; | 
|  | 80 | } | 
|  | 81 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 82 | ALOGD("getRawLightBrightness rawLightId %d brightness 0x%x ratio %.2f", rawLightId, | 
|  | 83 | brightness, ratio); | 
|  | 84 | } | 
|  | 85 | return brightness; | 
|  | 86 | } | 
|  | 87 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 88 | void PeripheralController::Light::setRawLightBrightness(int32_t rawLightId, int32_t brightness) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 89 | std::optional<RawLightInfo> rawInfo = context.getRawLightInfo(rawLightId); | 
|  | 90 | if (!rawInfo.has_value()) { | 
|  | 91 | return; | 
|  | 92 | } | 
|  | 93 | // If the light node doesn't have max brightness, use the default max brightness. | 
|  | 94 | int rawMaxBrightness = rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS); | 
|  | 95 | float ratio = MAX_BRIGHTNESS / rawMaxBrightness; | 
|  | 96 | // Scale the requested brightness in [0, 255] to [0, rawMaxBrightness] | 
|  | 97 | if (rawMaxBrightness != MAX_BRIGHTNESS) { | 
|  | 98 | brightness = ceil(brightness / ratio); | 
|  | 99 | } | 
|  | 100 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 101 | ALOGD("setRawLightBrightness rawLightId %d brightness 0x%x ratio %.2f", rawLightId, | 
|  | 102 | brightness, ratio); | 
|  | 103 | } | 
|  | 104 | context.setLightBrightness(rawLightId, brightness); | 
|  | 105 | } | 
|  | 106 |  | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 107 | bool PeripheralController::MonoLight::setLightColor(int32_t color) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 108 | int32_t brightness = getAlpha(color); | 
|  | 109 | setRawLightBrightness(rawId, brightness); | 
|  | 110 |  | 
|  | 111 | return true; | 
|  | 112 | } | 
|  | 113 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 114 | bool PeripheralController::RgbLight::setLightColor(int32_t color) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 115 | // Compose color value as per: | 
|  | 116 | // https://developer.android.com/reference/android/graphics/Color?hl=en | 
|  | 117 | // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff); | 
|  | 118 | // The alpha component is used to scale the R,G,B leds brightness, with the ratio to | 
|  | 119 | // MAX_BRIGHTNESS. | 
|  | 120 | brightness = getAlpha(color); | 
|  | 121 | int32_t red = 0; | 
|  | 122 | int32_t green = 0; | 
|  | 123 | int32_t blue = 0; | 
|  | 124 | if (brightness > 0) { | 
|  | 125 | float ratio = MAX_BRIGHTNESS / brightness; | 
|  | 126 | red = ceil(getRed(color) / ratio); | 
|  | 127 | green = ceil(getGreen(color) / ratio); | 
|  | 128 | blue = ceil(getBlue(color) / ratio); | 
|  | 129 | } | 
|  | 130 | setRawLightBrightness(rawRgbIds.at(LightColor::RED), red); | 
|  | 131 | setRawLightBrightness(rawRgbIds.at(LightColor::GREEN), green); | 
|  | 132 | setRawLightBrightness(rawRgbIds.at(LightColor::BLUE), blue); | 
|  | 133 | if (rawGlobalId.has_value()) { | 
|  | 134 | setRawLightBrightness(rawGlobalId.value(), brightness); | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | return true; | 
|  | 138 | } | 
|  | 139 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 140 | bool PeripheralController::MultiColorLight::setLightColor(int32_t color) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 141 | std::unordered_map<LightColor, int32_t> intensities; | 
|  | 142 | intensities.emplace(LightColor::RED, getRed(color)); | 
|  | 143 | intensities.emplace(LightColor::GREEN, getGreen(color)); | 
|  | 144 | intensities.emplace(LightColor::BLUE, getBlue(color)); | 
|  | 145 |  | 
|  | 146 | context.setLightIntensities(rawId, intensities); | 
|  | 147 | setRawLightBrightness(rawId, getAlpha(color)); | 
|  | 148 | return true; | 
|  | 149 | } | 
|  | 150 |  | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 151 | std::optional<int32_t> PeripheralController::MonoLight::getLightColor() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 152 | std::optional<int32_t> brightness = getRawLightBrightness(rawId); | 
|  | 153 | if (!brightness.has_value()) { | 
|  | 154 | return std::nullopt; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | return toArgb(brightness.value(), 0 /* red */, 0 /* green */, 0 /* blue */); | 
|  | 158 | } | 
|  | 159 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 160 | std::optional<int32_t> PeripheralController::RgbLight::getLightColor() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 161 | // If the Alpha component is zero, then return color 0. | 
|  | 162 | if (brightness == 0) { | 
|  | 163 | return 0; | 
|  | 164 | } | 
|  | 165 | // Compose color value as per: | 
|  | 166 | // https://developer.android.com/reference/android/graphics/Color?hl=en | 
|  | 167 | // int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff); | 
|  | 168 | std::optional<int32_t> redOr = getRawLightBrightness(rawRgbIds.at(LightColor::RED)); | 
|  | 169 | std::optional<int32_t> greenOr = getRawLightBrightness(rawRgbIds.at(LightColor::GREEN)); | 
|  | 170 | std::optional<int32_t> blueOr = getRawLightBrightness(rawRgbIds.at(LightColor::BLUE)); | 
|  | 171 | // If we can't get brightness for any of the RGB light | 
|  | 172 | if (!redOr.has_value() || !greenOr.has_value() || !blueOr.has_value()) { | 
|  | 173 | return std::nullopt; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | // Compose the ARGB format color. As the R,G,B color led brightness is scaled by Alpha | 
|  | 177 | // value, scale it back to return the nominal color value. | 
|  | 178 | float ratio = MAX_BRIGHTNESS / brightness; | 
|  | 179 | int32_t red = round(redOr.value() * ratio); | 
|  | 180 | int32_t green = round(greenOr.value() * ratio); | 
|  | 181 | int32_t blue = round(blueOr.value() * ratio); | 
|  | 182 |  | 
|  | 183 | if (red > MAX_BRIGHTNESS || green > MAX_BRIGHTNESS || blue > MAX_BRIGHTNESS) { | 
|  | 184 | // Previously stored brightness isn't valid for current LED values, so just reset to max | 
|  | 185 | // brightness since an app couldn't have provided these values in the first place. | 
|  | 186 | red = redOr.value(); | 
|  | 187 | green = greenOr.value(); | 
|  | 188 | blue = blueOr.value(); | 
|  | 189 | brightness = MAX_BRIGHTNESS; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | return toArgb(brightness, red, green, blue); | 
|  | 193 | } | 
|  | 194 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 195 | std::optional<int32_t> PeripheralController::MultiColorLight::getLightColor() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 196 | auto ret = context.getLightIntensities(rawId); | 
|  | 197 | if (!ret.has_value()) { | 
|  | 198 | return std::nullopt; | 
|  | 199 | } | 
|  | 200 | std::unordered_map<LightColor, int32_t> intensities = ret.value(); | 
|  | 201 | // Get red, green, blue colors | 
|  | 202 | int32_t color = toArgb(0 /* brightness */, intensities.at(LightColor::RED) /* red */, | 
|  | 203 | intensities.at(LightColor::GREEN) /* green */, | 
|  | 204 | intensities.at(LightColor::BLUE) /* blue */); | 
|  | 205 | // Get brightness | 
|  | 206 | std::optional<int32_t> brightness = getRawLightBrightness(rawId); | 
|  | 207 | if (brightness.has_value()) { | 
|  | 208 | return toArgb(brightness.value() /* A */, 0, 0, 0) | color; | 
|  | 209 | } | 
|  | 210 | return std::nullopt; | 
|  | 211 | } | 
|  | 212 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 213 | bool PeripheralController::PlayerIdLight::setLightPlayerId(int32_t playerId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 214 | if (rawLightIds.find(playerId) == rawLightIds.end()) { | 
|  | 215 | return false; | 
|  | 216 | } | 
|  | 217 | for (const auto& [id, rawId] : rawLightIds) { | 
|  | 218 | if (playerId == id) { | 
|  | 219 | setRawLightBrightness(rawId, MAX_BRIGHTNESS); | 
|  | 220 | } else { | 
|  | 221 | setRawLightBrightness(rawId, 0); | 
|  | 222 | } | 
|  | 223 | } | 
|  | 224 | return true; | 
|  | 225 | } | 
|  | 226 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 227 | std::optional<int32_t> PeripheralController::PlayerIdLight::getLightPlayerId() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 228 | for (const auto& [id, rawId] : rawLightIds) { | 
|  | 229 | std::optional<int32_t> brightness = getRawLightBrightness(rawId); | 
|  | 230 | if (brightness.has_value() && brightness.value() > 0) { | 
|  | 231 | return id; | 
|  | 232 | } | 
|  | 233 | } | 
|  | 234 | return std::nullopt; | 
|  | 235 | } | 
|  | 236 |  | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 237 | void PeripheralController::MonoLight::dump(std::string& dump) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 238 | dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0)); | 
|  | 239 | } | 
|  | 240 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 241 | void PeripheralController::PlayerIdLight::dump(std::string& dump) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 242 | dump += StringPrintf(INDENT4 "PlayerId: %d\n", getLightPlayerId().value_or(-1)); | 
|  | 243 | dump += StringPrintf(INDENT4 "Raw Player ID LEDs:"); | 
|  | 244 | for (const auto& [id, rawId] : rawLightIds) { | 
|  | 245 | dump += StringPrintf("id %d -> %d ", id, rawId); | 
|  | 246 | } | 
|  | 247 | dump += "\n"; | 
|  | 248 | } | 
|  | 249 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 250 | void PeripheralController::RgbLight::dump(std::string& dump) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 251 | dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0)); | 
|  | 252 | dump += StringPrintf(INDENT4 "Raw RGB LEDs: [%d, %d, %d] ", rawRgbIds.at(LightColor::RED), | 
|  | 253 | rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE)); | 
|  | 254 | if (rawGlobalId.has_value()) { | 
|  | 255 | dump += StringPrintf(INDENT4 "Raw Global LED: [%d] ", rawGlobalId.value()); | 
|  | 256 | } | 
|  | 257 | dump += "\n"; | 
|  | 258 | } | 
|  | 259 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 260 | void PeripheralController::MultiColorLight::dump(std::string& dump) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 261 | dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0)); | 
|  | 262 | } | 
|  | 263 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 264 | void PeripheralController::populateDeviceInfo(InputDeviceInfo* deviceInfo) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 265 | // TODO: b/180733860 Remove this after enabling multi-battery | 
|  | 266 | if (!mBatteries.empty()) { | 
|  | 267 | deviceInfo->setHasBattery(true); | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | for (const auto& [batteryId, battery] : mBatteries) { | 
|  | 271 | InputDeviceBatteryInfo batteryInfo(battery->name, battery->id); | 
|  | 272 | deviceInfo->addBatteryInfo(batteryInfo); | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | for (const auto& [lightId, light] : mLights) { | 
|  | 276 | // Input device light doesn't support ordinal, always pass 1. | 
|  | 277 | InputDeviceLightInfo lightInfo(light->name, light->id, light->type, 1 /* ordinal */); | 
|  | 278 | deviceInfo->addLightInfo(lightInfo); | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 282 | void PeripheralController::dump(std::string& dump) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 283 | dump += INDENT2 "Input Controller:\n"; | 
|  | 284 | if (!mLights.empty()) { | 
|  | 285 | dump += INDENT3 "Lights:\n"; | 
|  | 286 | for (const auto& [lightId, light] : mLights) { | 
|  | 287 | dump += StringPrintf(INDENT4 "Id: %d", lightId); | 
|  | 288 | dump += StringPrintf(INDENT4 "Name: %s", light->name.c_str()); | 
|  | 289 | dump += StringPrintf(INDENT4 "Type: %s", NamedEnum::string(light->type).c_str()); | 
|  | 290 | light->dump(dump); | 
|  | 291 | } | 
|  | 292 | } | 
|  | 293 | // Dump raw lights | 
|  | 294 | dump += INDENT3 "RawLights:\n"; | 
|  | 295 | dump += INDENT4 "Id:\t Name:\t Flags:\t Max brightness:\t Brightness\n"; | 
|  | 296 | const std::vector<int32_t> rawLightIds = getDeviceContext().getRawLightIds(); | 
|  | 297 | // Map from raw light id to raw light info | 
|  | 298 | std::unordered_map<int32_t, RawLightInfo> rawInfos; | 
|  | 299 | for (const auto& rawId : rawLightIds) { | 
|  | 300 | std::optional<RawLightInfo> rawInfo = getDeviceContext().getRawLightInfo(rawId); | 
|  | 301 | if (!rawInfo.has_value()) { | 
|  | 302 | continue; | 
|  | 303 | } | 
|  | 304 | dump += StringPrintf(INDENT4 "%d", rawId); | 
|  | 305 | dump += StringPrintf(INDENT4 "%s", rawInfo->name.c_str()); | 
|  | 306 | dump += StringPrintf(INDENT4 "%s", rawInfo->flags.string().c_str()); | 
|  | 307 | dump += StringPrintf(INDENT4 "%d", rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS)); | 
|  | 308 | dump += StringPrintf(INDENT4 "%d\n", | 
|  | 309 | getDeviceContext().getLightBrightness(rawId).value_or(-1)); | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | if (!mBatteries.empty()) { | 
|  | 313 | dump += INDENT3 "Batteries:\n"; | 
|  | 314 | for (const auto& [batteryId, battery] : mBatteries) { | 
|  | 315 | dump += StringPrintf(INDENT4 "Id: %d", batteryId); | 
|  | 316 | dump += StringPrintf(INDENT4 "Name: %s", battery->name.c_str()); | 
|  | 317 | dump += getBatteryCapacity(batteryId).has_value() | 
|  | 318 | ? StringPrintf(INDENT3 "Capacity: %d\n", getBatteryCapacity(batteryId).value()) | 
|  | 319 | : StringPrintf(INDENT3 "Capacity: Unknown"); | 
|  | 320 |  | 
|  | 321 | std::string status; | 
|  | 322 | switch (getBatteryStatus(batteryId).value_or(BATTERY_STATUS_UNKNOWN)) { | 
|  | 323 | case BATTERY_STATUS_CHARGING: | 
|  | 324 | status = "Charging"; | 
|  | 325 | break; | 
|  | 326 | case BATTERY_STATUS_DISCHARGING: | 
|  | 327 | status = "Discharging"; | 
|  | 328 | break; | 
|  | 329 | case BATTERY_STATUS_NOT_CHARGING: | 
|  | 330 | status = "Not charging"; | 
|  | 331 | break; | 
|  | 332 | case BATTERY_STATUS_FULL: | 
|  | 333 | status = "Full"; | 
|  | 334 | break; | 
|  | 335 | default: | 
|  | 336 | status = "Unknown"; | 
|  | 337 | } | 
|  | 338 | dump += StringPrintf(INDENT3 "Status: %s\n", status.c_str()); | 
|  | 339 | } | 
|  | 340 | } | 
|  | 341 | } | 
|  | 342 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 343 | void PeripheralController::configureBattries() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 344 | // Check raw batteries | 
|  | 345 | const std::vector<int32_t> rawBatteryIds = getDeviceContext().getRawBatteryIds(); | 
|  | 346 |  | 
|  | 347 | for (const auto& rawId : rawBatteryIds) { | 
|  | 348 | std::optional<RawBatteryInfo> rawInfo = getDeviceContext().getRawBatteryInfo(rawId); | 
|  | 349 | if (!rawInfo.has_value()) { | 
|  | 350 | continue; | 
|  | 351 | } | 
|  | 352 | std::unique_ptr<Battery> battery = | 
|  | 353 | std::make_unique<Battery>(getDeviceContext(), rawInfo->name, rawInfo->id); | 
|  | 354 | mBatteries.insert_or_assign(rawId, std::move(battery)); | 
|  | 355 | } | 
|  | 356 | } | 
|  | 357 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 358 | void PeripheralController::configureLights() { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 359 | bool hasRedLed = false; | 
|  | 360 | bool hasGreenLed = false; | 
|  | 361 | bool hasBlueLed = false; | 
|  | 362 | std::optional<int32_t> rawGlobalId = std::nullopt; | 
|  | 363 | // Player ID light common name string | 
|  | 364 | std::string playerIdName; | 
|  | 365 | // Raw RGB color to raw light ID | 
|  | 366 | std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds; | 
|  | 367 | // Map from player Id to raw light Id | 
|  | 368 | std::unordered_map<int32_t, int32_t> playerIdLightIds; | 
|  | 369 |  | 
|  | 370 | // Check raw lights | 
|  | 371 | const std::vector<int32_t> rawLightIds = getDeviceContext().getRawLightIds(); | 
|  | 372 | // Map from raw light id to raw light info | 
|  | 373 | std::unordered_map<int32_t, RawLightInfo> rawInfos; | 
|  | 374 | for (const auto& rawId : rawLightIds) { | 
|  | 375 | std::optional<RawLightInfo> rawInfo = getDeviceContext().getRawLightInfo(rawId); | 
|  | 376 | if (!rawInfo.has_value()) { | 
|  | 377 | continue; | 
|  | 378 | } | 
|  | 379 | rawInfos.insert_or_assign(rawId, rawInfo.value()); | 
|  | 380 | // Check if this is a group LEDs for player ID | 
|  | 381 | std::regex lightPattern("([a-z]+)([0-9]+)"); | 
|  | 382 | std::smatch results; | 
|  | 383 | if (std::regex_match(rawInfo->name, results, lightPattern)) { | 
|  | 384 | std::string commonName = results[1].str(); | 
|  | 385 | int32_t playerId = std::stoi(results[2]); | 
|  | 386 | if (playerIdLightIds.empty()) { | 
|  | 387 | playerIdName = commonName; | 
|  | 388 | playerIdLightIds.insert_or_assign(playerId, rawId); | 
|  | 389 | } else { | 
|  | 390 | // Make sure the player ID leds have common string name | 
|  | 391 | if (playerIdName.compare(commonName) == 0 && | 
|  | 392 | playerIdLightIds.find(playerId) == playerIdLightIds.end()) { | 
|  | 393 | playerIdLightIds.insert_or_assign(playerId, rawId); | 
|  | 394 | } | 
|  | 395 | } | 
|  | 396 | } | 
|  | 397 | // Check if this is an LED of RGB light | 
|  | 398 | if (rawInfo->flags.test(InputLightClass::RED)) { | 
|  | 399 | hasRedLed = true; | 
|  | 400 | rawRgbIds.emplace(LightColor::RED, rawId); | 
|  | 401 | } | 
|  | 402 | if (rawInfo->flags.test(InputLightClass::GREEN)) { | 
|  | 403 | hasGreenLed = true; | 
|  | 404 | rawRgbIds.emplace(LightColor::GREEN, rawId); | 
|  | 405 | } | 
|  | 406 | if (rawInfo->flags.test(InputLightClass::BLUE)) { | 
|  | 407 | hasBlueLed = true; | 
|  | 408 | rawRgbIds.emplace(LightColor::BLUE, rawId); | 
|  | 409 | } | 
|  | 410 | if (rawInfo->flags.test(InputLightClass::GLOBAL)) { | 
|  | 411 | rawGlobalId = rawId; | 
|  | 412 | } | 
|  | 413 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 414 | ALOGD("Light rawId %d name %s max %d flags %s \n", rawInfo->id, rawInfo->name.c_str(), | 
|  | 415 | rawInfo->maxBrightness.value_or(MAX_BRIGHTNESS), rawInfo->flags.string().c_str()); | 
|  | 416 | } | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | // Construct a player ID light | 
|  | 420 | if (playerIdLightIds.size() > 1) { | 
|  | 421 | std::unique_ptr<Light> light = | 
|  | 422 | std::make_unique<PlayerIdLight>(getDeviceContext(), playerIdName, ++mNextId, | 
|  | 423 | playerIdLightIds); | 
|  | 424 | mLights.insert_or_assign(light->id, std::move(light)); | 
|  | 425 | // Remove these raw lights from raw light info as they've been used to compose a | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 426 | // Player ID light, so we do not expose these raw lights as mono lights. | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 427 | for (const auto& [playerId, rawId] : playerIdLightIds) { | 
|  | 428 | rawInfos.erase(rawId); | 
|  | 429 | } | 
|  | 430 | } | 
|  | 431 | // Construct a RGB light for composed RGB light | 
|  | 432 | if (hasRedLed && hasGreenLed && hasBlueLed) { | 
|  | 433 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 434 | ALOGD("Rgb light ids [%d, %d, %d] \n", rawRgbIds.at(LightColor::RED), | 
|  | 435 | rawRgbIds.at(LightColor::GREEN), rawRgbIds.at(LightColor::BLUE)); | 
|  | 436 | } | 
|  | 437 | std::unique_ptr<Light> light = | 
|  | 438 | std::make_unique<RgbLight>(getDeviceContext(), ++mNextId, rawRgbIds, rawGlobalId); | 
|  | 439 | mLights.insert_or_assign(light->id, std::move(light)); | 
|  | 440 | // Remove from raw light info as they've been composed a RBG light. | 
|  | 441 | rawInfos.erase(rawRgbIds.at(LightColor::RED)); | 
|  | 442 | rawInfos.erase(rawRgbIds.at(LightColor::GREEN)); | 
|  | 443 | rawInfos.erase(rawRgbIds.at(LightColor::BLUE)); | 
|  | 444 | if (rawGlobalId.has_value()) { | 
|  | 445 | rawInfos.erase(rawGlobalId.value()); | 
|  | 446 | } | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | // Check the rest of raw light infos | 
|  | 450 | for (const auto& [rawId, rawInfo] : rawInfos) { | 
|  | 451 | // If the node is multi-color led, construct a MULTI_COLOR light | 
|  | 452 | if (rawInfo.flags.test(InputLightClass::MULTI_INDEX) && | 
|  | 453 | rawInfo.flags.test(InputLightClass::MULTI_INTENSITY)) { | 
|  | 454 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 455 | ALOGD("Multicolor light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str()); | 
|  | 456 | } | 
|  | 457 | std::unique_ptr<Light> light = | 
|  | 458 | std::make_unique<MultiColorLight>(getDeviceContext(), rawInfo.name, ++mNextId, | 
|  | 459 | rawInfo.id); | 
|  | 460 | mLights.insert_or_assign(light->id, std::move(light)); | 
|  | 461 | continue; | 
|  | 462 | } | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 463 | // Construct a Mono LED light | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 464 | if (DEBUG_LIGHT_DETAILS) { | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 465 | ALOGD("Mono light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str()); | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 466 | } | 
| Chris Ye | 8575833 | 2021-05-16 23:05:17 -0700 | [diff] [blame] | 467 | std::unique_ptr<Light> light = std::make_unique<MonoLight>(getDeviceContext(), rawInfo.name, | 
|  | 468 | ++mNextId, rawInfo.id); | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 469 |  | 
|  | 470 | mLights.insert_or_assign(light->id, std::move(light)); | 
|  | 471 | } | 
|  | 472 | } | 
|  | 473 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 474 | std::optional<int32_t> PeripheralController::getBatteryCapacity(int batteryId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 475 | return getDeviceContext().getBatteryCapacity(batteryId); | 
|  | 476 | } | 
|  | 477 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 478 | std::optional<int32_t> PeripheralController::getBatteryStatus(int batteryId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 479 | return getDeviceContext().getBatteryStatus(batteryId); | 
|  | 480 | } | 
|  | 481 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 482 | bool PeripheralController::setLightColor(int32_t lightId, int32_t color) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 483 | auto it = mLights.find(lightId); | 
|  | 484 | if (it == mLights.end()) { | 
|  | 485 | return false; | 
|  | 486 | } | 
|  | 487 | auto& light = it->second; | 
|  | 488 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 489 | ALOGD("setLightColor lightId %d type %s color 0x%x", lightId, | 
|  | 490 | NamedEnum::string(light->type).c_str(), color); | 
|  | 491 | } | 
|  | 492 | return light->setLightColor(color); | 
|  | 493 | } | 
|  | 494 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 495 | std::optional<int32_t> PeripheralController::getLightColor(int32_t lightId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 496 | auto it = mLights.find(lightId); | 
|  | 497 | if (it == mLights.end()) { | 
|  | 498 | return std::nullopt; | 
|  | 499 | } | 
|  | 500 | auto& light = it->second; | 
|  | 501 | std::optional<int32_t> color = light->getLightColor(); | 
|  | 502 | if (DEBUG_LIGHT_DETAILS) { | 
|  | 503 | ALOGD("getLightColor lightId %d type %s color 0x%x", lightId, | 
|  | 504 | NamedEnum::string(light->type).c_str(), color.value_or(0)); | 
|  | 505 | } | 
|  | 506 | return color; | 
|  | 507 | } | 
|  | 508 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 509 | bool PeripheralController::setLightPlayerId(int32_t lightId, int32_t playerId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 510 | auto it = mLights.find(lightId); | 
|  | 511 | if (it == mLights.end()) { | 
|  | 512 | return false; | 
|  | 513 | } | 
|  | 514 | auto& light = it->second; | 
|  | 515 | return light->setLightPlayerId(playerId); | 
|  | 516 | } | 
|  | 517 |  | 
| Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 518 | std::optional<int32_t> PeripheralController::getLightPlayerId(int32_t lightId) { | 
| Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 519 | auto it = mLights.find(lightId); | 
|  | 520 | if (it == mLights.end()) { | 
|  | 521 | return std::nullopt; | 
|  | 522 | } | 
|  | 523 | auto& light = it->second; | 
|  | 524 | return light->getLightPlayerId(); | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | } // namespace android |