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