blob: 25cf435620af6aa55bb6a08715690d406a8a2ebf [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;
70
71 virtual bool setLightColor(int32_t color) { return false; }
72 virtual std::optional<int32_t> getLightColor() { return std::nullopt; }
73 virtual bool setLightPlayerId(int32_t playerId) { return false; }
74 virtual std::optional<int32_t> getLightPlayerId() { return std::nullopt; }
75
76 virtual void dump(std::string& dump) {}
77
78 std::optional<std::int32_t> getRawLightBrightness(int32_t rawLightId);
79 void setRawLightBrightness(int32_t rawLightId, int32_t brightness);
80 };
81
Chris Ye85758332021-05-16 23:05:17 -070082 struct MonoLight : public Light {
83 explicit MonoLight(InputDeviceContext& context, const std::string& name, int32_t id,
84 int32_t rawId)
85 : Light(context, name, id, InputDeviceLightType::MONO), rawId(rawId) {}
Chris Ye3fdbfef2021-01-06 18:45:18 -080086 int32_t rawId;
87
88 bool setLightColor(int32_t color) override;
89 std::optional<int32_t> getLightColor() override;
90 void dump(std::string& dump) override;
91 };
92
93 struct RgbLight : public Light {
94 explicit RgbLight(InputDeviceContext& context, int32_t id,
Greg Kaiser7d3bb352021-02-23 08:07:55 -080095 const std::unordered_map<LightColor, int32_t>& rawRgbIds,
Chris Ye3fdbfef2021-01-06 18:45:18 -080096 std::optional<int32_t> rawGlobalId)
97 : Light(context, "RGB", id, InputDeviceLightType::RGB),
98 rawRgbIds(rawRgbIds),
99 rawGlobalId(rawGlobalId) {
100 brightness = rawGlobalId.has_value()
101 ? getRawLightBrightness(rawGlobalId.value()).value_or(MAX_BRIGHTNESS)
102 : MAX_BRIGHTNESS;
103 }
104 // Map from color to raw light id.
105 std::unordered_map<LightColor, int32_t /* rawLightId */> rawRgbIds;
106 // Optional global control raw light id.
107 std::optional<int32_t> rawGlobalId;
108 int32_t brightness;
109
110 bool setLightColor(int32_t color) override;
111 std::optional<int32_t> getLightColor() override;
112 void dump(std::string& dump) override;
113 };
114
115 struct MultiColorLight : public Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -0800116 explicit MultiColorLight(InputDeviceContext& context, const std::string& name, int32_t id,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800117 int32_t rawId)
118 : Light(context, name, id, InputDeviceLightType::MULTI_COLOR), rawId(rawId) {}
119 int32_t rawId;
120
121 bool setLightColor(int32_t color) override;
122 std::optional<int32_t> getLightColor() override;
123 void dump(std::string& dump) override;
124 };
125
126 struct PlayerIdLight : public Light {
Greg Kaiser7d3bb352021-02-23 08:07:55 -0800127 explicit PlayerIdLight(InputDeviceContext& context, const std::string& name, int32_t id,
128 const std::unordered_map<int32_t, int32_t>& rawLightIds)
Chris Ye3fdbfef2021-01-06 18:45:18 -0800129 : Light(context, name, id, InputDeviceLightType::PLAYER_ID),
130 rawLightIds(rawLightIds) {}
131 // Map from player Id to raw light Id
132 std::unordered_map<int32_t, int32_t> rawLightIds;
133
134 bool setLightPlayerId(int32_t palyerId) override;
135 std::optional<int32_t> getLightPlayerId() override;
136 void dump(std::string& dump) override;
137 };
138
139 int32_t mNextId = 0;
140
141 // Light color map from light color to the color index.
142 static const std::unordered_map<std::string, size_t> LIGHT_COLORS;
143
144 // Light map from light ID to Light
145 std::unordered_map<int32_t, std::unique_ptr<Light>> mLights;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800146
147 // Battery map from battery ID to battery
148 std::unordered_map<int32_t, std::unique_ptr<Battery>> mBatteries;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800149};
150
151} // namespace android