blob: 07ade7c93112539d48efc281b2ee0d713e0d9103 [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
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000079 void configureSuggestedBrightnessLevels();
Chris Ye3fdbfef2021-01-06 18:45:18 -080080 std::optional<std::int32_t> getRawLightBrightness(int32_t rawLightId);
81 void setRawLightBrightness(int32_t rawLightId, int32_t brightness);
82 };
83
Chris Ye85758332021-05-16 23:05:17 -070084 struct MonoLight : public Light {
85 explicit MonoLight(InputDeviceContext& context, const std::string& name, int32_t id,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000086 InputDeviceLightType type, int32_t rawId)
87 : Light(context, name, id, type), rawId(rawId) {
88 capabilityFlags |= InputDeviceLightCapability::BRIGHTNESS;
89 }
Chris Ye3fdbfef2021-01-06 18:45:18 -080090 int32_t rawId;
91
92 bool setLightColor(int32_t color) override;
93 std::optional<int32_t> getLightColor() override;
94 void dump(std::string& dump) override;
95 };
96
97 struct RgbLight : public Light {
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000098 explicit RgbLight(InputDeviceContext& context, int32_t id, InputDeviceLightType type,
Greg Kaiser7d3bb352021-02-23 08:07:55 -080099 const std::unordered_map<LightColor, int32_t>& rawRgbIds,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800100 std::optional<int32_t> rawGlobalId)
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000101 : Light(context, "RGB", id, type), rawRgbIds(rawRgbIds), rawGlobalId(rawGlobalId) {
Chris Ye3fdbfef2021-01-06 18:45:18 -0800102 brightness = rawGlobalId.has_value()
103 ? getRawLightBrightness(rawGlobalId.value()).value_or(MAX_BRIGHTNESS)
104 : MAX_BRIGHTNESS;
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000105 capabilityFlags |= InputDeviceLightCapability::BRIGHTNESS;
106 capabilityFlags |= InputDeviceLightCapability::RGB;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107 }
108 // Map from color to raw light id.
109 std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds;
110 // Optional global control raw light id.
111 std::optional<int32_t> rawGlobalId;
112 int32_t brightness;
113
114 bool setLightColor(int32_t color) override;
115 std::optional<int32_t> getLightColor() override;
116 void dump(std::string& dump) override;
117 };
118
119 struct MultiColorLight : public Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -0800120 explicit MultiColorLight(InputDeviceContext& context, const std::string& name, int32_t id,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000121 InputDeviceLightType type, int32_t rawId)
122 : Light(context, name, id, type), rawId(rawId) {
123 capabilityFlags |= InputDeviceLightCapability::BRIGHTNESS;
124 capabilityFlags |= InputDeviceLightCapability::RGB;
125 }
Chris Ye3fdbfef2021-01-06 18:45:18 -0800126 int32_t rawId;
127
128 bool setLightColor(int32_t color) override;
129 std::optional<int32_t> getLightColor() override;
130 void dump(std::string& dump) override;
131 };
132
133 struct PlayerIdLight : public Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -0800134 explicit PlayerIdLight(InputDeviceContext& context, const std::string& name, int32_t id,
135 const std::unordered_map<int32_t, int32_t>& rawLightIds)
Chris Ye3fdbfef2021-01-06 18:45:18 -0800136 : Light(context, name, id, InputDeviceLightType::PLAYER_ID),
137 rawLightIds(rawLightIds) {}
138 // Map from player Id to raw light Id
139 std::unordered_map<int32_t, int32_t> rawLightIds;
140
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000141 bool setLightPlayerId(int32_t playerId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800142 std::optional<int32_t> getLightPlayerId() override;
143 void dump(std::string& dump) override;
144 };
145
146 int32_t mNextId = 0;
147
148 // Light color map from light color to the color index.
149 static const std::unordered_map<std::string, size_t> LIGHT_COLORS;
150
151 // Light map from light ID to Light
152 std::unordered_map<int32_t, std::unique_ptr<Light>> mLights;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800153
154 // Battery map from battery ID to battery
155 std::unordered_map<int32_t, std::unique_ptr<Battery>> mBatteries;
Vaibhav Devmurari16c24192023-05-04 15:20:12 +0000156
157 std::set<BrightnessLevel> getPreferredBrightnessLevels(const Light* light) const;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800158};
159
160} // namespace android