blob: 1a40d06f45e4d64f6fb6f3c5997b29c08afe77f0 [file] [log] [blame]
Chris Yee2b1e5c2021-03-10 22:45:12 -08001/*
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
Chris Ye1dd2e5c2021-04-04 23:12:41 -070022#include "PeripheralController.h"
Chris Yee2b1e5c2021-03-10 22:45:12 -080023#include "input/NamedEnum.h"
24
25// Log detailed debug messages about input device lights.
26static constexpr bool DEBUG_LIGHT_DETAILS = false;
27
28namespace android {
29
30static inline int32_t getAlpha(int32_t color) {
31 return (color >> 24) & 0xff;
32}
33
34static inline int32_t getRed(int32_t color) {
35 return (color >> 16) & 0xff;
36}
37
38static inline int32_t getGreen(int32_t color) {
39 return (color >> 8) & 0xff;
40}
41
42static inline int32_t getBlue(int32_t color) {
43 return color & 0xff;
44}
45
46static 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 Ye1dd2e5c2021-04-04 23:12:41 -070055PeripheralController::PeripheralController(InputDeviceContext& deviceContext)
Chris Yee2b1e5c2021-03-10 22:45:12 -080056 : mDeviceContext(deviceContext) {
57 configureBattries();
58 configureLights();
59}
60
Chris Ye1dd2e5c2021-04-04 23:12:41 -070061PeripheralController::~PeripheralController() {}
Chris Yee2b1e5c2021-03-10 22:45:12 -080062
Chris Ye1dd2e5c2021-04-04 23:12:41 -070063std::optional<std::int32_t> PeripheralController::Light::getRawLightBrightness(int32_t rawLightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080064 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 Ye1dd2e5c2021-04-04 23:12:41 -070088void PeripheralController::Light::setRawLightBrightness(int32_t rawLightId, int32_t brightness) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080089 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 Ye1dd2e5c2021-04-04 23:12:41 -0700107bool PeripheralController::SingleLight::setLightColor(int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800108 int32_t brightness = getAlpha(color);
109 setRawLightBrightness(rawId, brightness);
110
111 return true;
112}
113
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700114bool PeripheralController::RgbLight::setLightColor(int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800115 // 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 Ye1dd2e5c2021-04-04 23:12:41 -0700140bool PeripheralController::MultiColorLight::setLightColor(int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800141 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 Ye1dd2e5c2021-04-04 23:12:41 -0700151std::optional<int32_t> PeripheralController::SingleLight::getLightColor() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800152 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 Ye1dd2e5c2021-04-04 23:12:41 -0700160std::optional<int32_t> PeripheralController::RgbLight::getLightColor() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800161 // 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 Ye1dd2e5c2021-04-04 23:12:41 -0700195std::optional<int32_t> PeripheralController::MultiColorLight::getLightColor() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800196 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 Ye1dd2e5c2021-04-04 23:12:41 -0700213bool PeripheralController::PlayerIdLight::setLightPlayerId(int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800214 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 Ye1dd2e5c2021-04-04 23:12:41 -0700227std::optional<int32_t> PeripheralController::PlayerIdLight::getLightPlayerId() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800228 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 Ye1dd2e5c2021-04-04 23:12:41 -0700237void PeripheralController::SingleLight::dump(std::string& dump) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800238 dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
239}
240
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700241void PeripheralController::PlayerIdLight::dump(std::string& dump) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800242 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 Ye1dd2e5c2021-04-04 23:12:41 -0700250void PeripheralController::RgbLight::dump(std::string& dump) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800251 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 Ye1dd2e5c2021-04-04 23:12:41 -0700260void PeripheralController::MultiColorLight::dump(std::string& dump) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800261 dump += StringPrintf(INDENT4 "Color: 0x%x\n", getLightColor().value_or(0));
262}
263
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700264void PeripheralController::populateDeviceInfo(InputDeviceInfo* deviceInfo) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800265 // 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 Ye1dd2e5c2021-04-04 23:12:41 -0700282void PeripheralController::dump(std::string& dump) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800283 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 Ye1dd2e5c2021-04-04 23:12:41 -0700343void PeripheralController::configureBattries() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800344 // 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 Ye1dd2e5c2021-04-04 23:12:41 -0700358void PeripheralController::configureLights() {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800359 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
426 // Player ID light, so we do not expose these raw lights as single lights.
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 }
463 // Construct a single LED light
464 if (DEBUG_LIGHT_DETAILS) {
465 ALOGD("Single light Id %d name %s \n", rawInfo.id, rawInfo.name.c_str());
466 }
467 std::unique_ptr<Light> light =
468 std::make_unique<SingleLight>(getDeviceContext(), rawInfo.name, ++mNextId,
469 rawInfo.id);
470
471 mLights.insert_or_assign(light->id, std::move(light));
472 }
473}
474
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700475std::optional<int32_t> PeripheralController::getBatteryCapacity(int batteryId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800476 return getDeviceContext().getBatteryCapacity(batteryId);
477}
478
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700479std::optional<int32_t> PeripheralController::getBatteryStatus(int batteryId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800480 return getDeviceContext().getBatteryStatus(batteryId);
481}
482
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700483bool PeripheralController::setLightColor(int32_t lightId, int32_t color) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800484 auto it = mLights.find(lightId);
485 if (it == mLights.end()) {
486 return false;
487 }
488 auto& light = it->second;
489 if (DEBUG_LIGHT_DETAILS) {
490 ALOGD("setLightColor lightId %d type %s color 0x%x", lightId,
491 NamedEnum::string(light->type).c_str(), color);
492 }
493 return light->setLightColor(color);
494}
495
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700496std::optional<int32_t> PeripheralController::getLightColor(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800497 auto it = mLights.find(lightId);
498 if (it == mLights.end()) {
499 return std::nullopt;
500 }
501 auto& light = it->second;
502 std::optional<int32_t> color = light->getLightColor();
503 if (DEBUG_LIGHT_DETAILS) {
504 ALOGD("getLightColor lightId %d type %s color 0x%x", lightId,
505 NamedEnum::string(light->type).c_str(), color.value_or(0));
506 }
507 return color;
508}
509
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700510bool PeripheralController::setLightPlayerId(int32_t lightId, int32_t playerId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800511 auto it = mLights.find(lightId);
512 if (it == mLights.end()) {
513 return false;
514 }
515 auto& light = it->second;
516 return light->setLightPlayerId(playerId);
517}
518
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700519std::optional<int32_t> PeripheralController::getLightPlayerId(int32_t lightId) {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800520 auto it = mLights.find(lightId);
521 if (it == mLights.end()) {
522 return std::nullopt;
523 }
524 auto& light = it->second;
525 return light->getLightPlayerId();
526}
527
528} // namespace android