blob: 8ac42c3792a729a8cf729fd77c1b7814d5b766a4 [file] [log] [blame]
Chris Ye3fdbfef2021-01-06 18:45:18 -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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Chris Ye3fdbfef2021-01-06 18:45:18 -080018
Chris Ye1dd2e5c2021-04-04 23:12:41 -070019#include "PeripheralControllerInterface.h"
Chris Ye3fdbfef2021-01-06 18:45:18 -080020
21namespace android {
22
Chris Ye1dd2e5c2021-04-04 23:12:41 -070023class PeripheralController : public PeripheralControllerInterface {
Chris Ye3fdbfef2021-01-06 18:45:18 -080024 // Refer to https://developer.android.com/reference/kotlin/android/graphics/Color
25 /* Number of colors : {red, green, blue} */
26 static constexpr size_t COLOR_NUM = 3;
27 static constexpr int32_t MAX_BRIGHTNESS = 0xff;
28
29public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030 explicit PeripheralController(InputDeviceContext& deviceContext);
31 ~PeripheralController() override;
Chris Ye3fdbfef2021-01-06 18:45:18 -080032
Andy Chenf9f1a022022-08-29 20:07:10 -040033 int32_t getEventHubId() const override;
Chris Ye3fdbfef2021-01-06 18:45:18 -080034 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
35 void dump(std::string& dump) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -080036 bool setLightColor(int32_t lightId, int32_t color) override;
37 bool setLightPlayerId(int32_t lightId, int32_t playerId) override;
38 std::optional<int32_t> getLightColor(int32_t lightId) override;
39 std::optional<int32_t> getLightPlayerId(int32_t lightId) override;
Chris Yee2b1e5c2021-03-10 22:45:12 -080040 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override;
41 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -080042
43private:
Chris Yee2b1e5c2021-03-10 22:45:12 -080044 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
45 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -040046 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Yee2b1e5c2021-03-10 22:45:12 -080047
48 InputDeviceContext& mDeviceContext;
49 void configureLights();
50 void configureBattries();
51
52 struct Battery {
53 explicit Battery(InputDeviceContext& context, const std::string& name, int32_t id)
54 : context(context), name(name), id(id) {}
55 virtual ~Battery() {}
56 InputDeviceContext& context;
57 std::string name;
58 int32_t id;
59 };
60
Chris Ye3fdbfef2021-01-06 18:45:18 -080061 struct Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -080062 explicit Light(InputDeviceContext& context, const std::string& name, int32_t id,
Chris Ye3fdbfef2021-01-06 18:45:18 -080063 InputDeviceLightType type)
64 : context(context), name(name), id(id), type(type) {}
65 virtual ~Light() {}
66 InputDeviceContext& context;
67 std::string name;
68 int32_t id;
69 InputDeviceLightType type;
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000070 ftl::Flags<InputDeviceLightCapability> capabilityFlags;
Chris Ye3fdbfef2021-01-06 18:45:18 -080071
72 virtual bool setLightColor(int32_t color) { return false; }
73 virtual std::optional<int32_t> getLightColor() { return std::nullopt; }
74 virtual bool setLightPlayerId(int32_t playerId) { return false; }
75 virtual std::optional<int32_t> getLightPlayerId() { return std::nullopt; }
76
77 virtual void dump(std::string& dump) {}
78
79 std::optional<std::int32_t> getRawLightBrightness(int32_t rawLightId);
80 void setRawLightBrightness(int32_t rawLightId, int32_t brightness);
81 };
82
Chris Ye85758332021-05-16 23:05:17 -070083 struct MonoLight : public Light {
84 explicit MonoLight(InputDeviceContext& context, const std::string& name, int32_t id,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000085 InputDeviceLightType type, int32_t rawId)
86 : Light(context, name, id, type), rawId(rawId) {
87 capabilityFlags |= InputDeviceLightCapability::BRIGHTNESS;
88 }
Chris Ye3fdbfef2021-01-06 18:45:18 -080089 int32_t rawId;
90
91 bool setLightColor(int32_t color) override;
92 std::optional<int32_t> getLightColor() override;
93 void dump(std::string& dump) override;
94 };
95
96 struct RgbLight : public Light {
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000097 explicit RgbLight(InputDeviceContext& context, int32_t id, InputDeviceLightType type,
Greg Kaiser7d3bb352021-02-23 08:07:55 -080098 const std::unordered_map<LightColor, int32_t>& rawRgbIds,
Chris Ye3fdbfef2021-01-06 18:45:18 -080099 std::optional<int32_t> rawGlobalId)
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000100 : Light(context, "RGB", id, type), rawRgbIds(rawRgbIds), rawGlobalId(rawGlobalId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800101 brightness = rawGlobalId.has_value()
102 ? getRawLightBrightness(rawGlobalId.value()).value_or(MAX_BRIGHTNESS)
103 : MAX_BRIGHTNESS;
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000104 capabilityFlags |= InputDeviceLightCapability::BRIGHTNESS;
105 capabilityFlags |= InputDeviceLightCapability::RGB;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800106 }
107 // Map from color to raw light id.
108 std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds;
109 // Optional global control raw light id.
110 std::optional<int32_t> rawGlobalId;
111 int32_t brightness;
112
113 bool setLightColor(int32_t color) override;
114 std::optional<int32_t> getLightColor() override;
115 void dump(std::string& dump) override;
116 };
117
118 struct MultiColorLight : public Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -0800119 explicit MultiColorLight(InputDeviceContext& context, const std::string& name, int32_t id,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000120 InputDeviceLightType type, int32_t rawId)
121 : Light(context, name, id, type), rawId(rawId) {
122 capabilityFlags |= InputDeviceLightCapability::BRIGHTNESS;
123 capabilityFlags |= InputDeviceLightCapability::RGB;
124 }
Chris Ye3fdbfef2021-01-06 18:45:18 -0800125 int32_t rawId;
126
127 bool setLightColor(int32_t color) override;
128 std::optional<int32_t> getLightColor() override;
129 void dump(std::string& dump) override;
130 };
131
132 struct PlayerIdLight : public Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -0800133 explicit PlayerIdLight(InputDeviceContext& context, const std::string& name, int32_t id,
134 const std::unordered_map<int32_t, int32_t>& rawLightIds)
Chris Ye3fdbfef2021-01-06 18:45:18 -0800135 : Light(context, name, id, InputDeviceLightType::PLAYER_ID),
136 rawLightIds(rawLightIds) {}
137 // Map from player Id to raw light Id
138 std::unordered_map<int32_t, int32_t> rawLightIds;
139
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000140 bool setLightPlayerId(int32_t playerId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800141 std::optional<int32_t> getLightPlayerId() override;
142 void dump(std::string& dump) override;
143 };
144
145 int32_t mNextId = 0;
146
147 // Light color map from light color to the color index.
148 static const std::unordered_map<std::string, size_t> LIGHT_COLORS;
149
150 // Light map from light ID to Light
151 std::unordered_map<int32_t, std::unique_ptr<Light>> mLights;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800152
153 // Battery map from battery ID to battery
154 std::unordered_map<int32_t, std::unique_ptr<Battery>> mBatteries;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800155};
156
157} // namespace android