blob: a0910ea847a80c5036fc93d9bd3d67a0d8823a99 [file] [log] [blame]
Michael Ensingb8d93262020-05-12 00:41:30 -07001/*
2 * Copyright 2022 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 */
Michael Ensingb8d93262020-05-12 00:41:30 -070016#pragma once
17
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Aditya Wazireea5be02022-10-31 12:40:10 +053021#include <ThreadSafeFuzzedDataProvider.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070022#include "android/hardware/input/InputDeviceCountryCode.h"
23
24using android::hardware::input::InputDeviceCountryCode;
25
26constexpr size_t kValidTypes[] = {EV_SW,
27 EV_SYN,
28 SYN_REPORT,
29 EV_ABS,
30 EV_KEY,
31 EV_MSC,
32 EV_REL,
33 android::EventHubInterface::DEVICE_ADDED,
34 android::EventHubInterface::DEVICE_REMOVED,
35 android::EventHubInterface::FINISHED_DEVICE_SCAN};
36
37constexpr size_t kValidCodes[] = {
38 SYN_REPORT,
39 ABS_MT_SLOT,
40 SYN_MT_REPORT,
41 ABS_MT_POSITION_X,
42 ABS_MT_POSITION_Y,
43 ABS_MT_TOUCH_MAJOR,
44 ABS_MT_TOUCH_MINOR,
45 ABS_MT_WIDTH_MAJOR,
46 ABS_MT_WIDTH_MINOR,
47 ABS_MT_ORIENTATION,
48 ABS_MT_TRACKING_ID,
49 ABS_MT_PRESSURE,
50 ABS_MT_DISTANCE,
51 ABS_MT_TOOL_TYPE,
52 SYN_MT_REPORT,
53 MSC_SCAN,
54 REL_X,
55 REL_Y,
56 REL_WHEEL,
57 REL_HWHEEL,
58 BTN_LEFT,
59 BTN_RIGHT,
60 BTN_MIDDLE,
61 BTN_BACK,
62 BTN_SIDE,
63 BTN_FORWARD,
64 BTN_EXTRA,
65 BTN_TASK,
66};
67
68constexpr InputDeviceCountryCode kCountryCodes[] = {
69 InputDeviceCountryCode::INVALID,
70 InputDeviceCountryCode::NOT_SUPPORTED,
71 InputDeviceCountryCode::ARABIC,
72 InputDeviceCountryCode::BELGIAN,
73 InputDeviceCountryCode::CANADIAN_BILINGUAL,
74 InputDeviceCountryCode::CANADIAN_FRENCH,
75 InputDeviceCountryCode::CZECH_REPUBLIC,
76 InputDeviceCountryCode::DANISH,
77 InputDeviceCountryCode::FINNISH,
78 InputDeviceCountryCode::FRENCH,
79 InputDeviceCountryCode::GERMAN,
80 InputDeviceCountryCode::GREEK,
81 InputDeviceCountryCode::HEBREW,
82 InputDeviceCountryCode::HUNGARY,
83 InputDeviceCountryCode::INTERNATIONAL,
84 InputDeviceCountryCode::ITALIAN,
85 InputDeviceCountryCode::JAPAN,
86 InputDeviceCountryCode::KOREAN,
87 InputDeviceCountryCode::LATIN_AMERICAN,
88 InputDeviceCountryCode::DUTCH,
89 InputDeviceCountryCode::NORWEGIAN,
90 InputDeviceCountryCode::PERSIAN,
91 InputDeviceCountryCode::POLAND,
92 InputDeviceCountryCode::PORTUGUESE,
93 InputDeviceCountryCode::RUSSIA,
94 InputDeviceCountryCode::SLOVAKIA,
95 InputDeviceCountryCode::SPANISH,
96 InputDeviceCountryCode::SWEDISH,
97 InputDeviceCountryCode::SWISS_FRENCH,
98 InputDeviceCountryCode::SWISS_GERMAN,
99 InputDeviceCountryCode::SWITZERLAND,
100 InputDeviceCountryCode::TAIWAN,
101 InputDeviceCountryCode::TURKISH_Q,
102 InputDeviceCountryCode::UK,
103 InputDeviceCountryCode::US,
104 InputDeviceCountryCode::YUGOSLAVIA,
105 InputDeviceCountryCode::TURKISH_F,
106};
107
108constexpr size_t kMaxSize = 256;
109
110namespace android {
111
112class FuzzEventHub : public EventHubInterface {
113 InputDeviceIdentifier mIdentifier;
114 std::vector<TouchVideoFrame> mVideoFrames;
115 PropertyMap mFuzzConfig;
Aditya Wazireea5be02022-10-31 12:40:10 +0530116 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700117
118public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530119 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700120 ~FuzzEventHub() {}
121 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700122
123 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
124 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
125 }
126 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
127 return mIdentifier;
128 }
129 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
130 return mFdp->ConsumeIntegral<int32_t>();
131 }
132 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
133 *outConfiguration = mFuzzConfig;
134 }
135 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
136 RawAbsoluteAxisInfo* outAxisInfo) const override {
137 return mFdp->ConsumeIntegral<status_t>();
138 }
139 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
140 bool hasInputProperty(int32_t deviceId, int property) const override {
141 return mFdp->ConsumeBool();
142 }
143 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
144 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
145 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
146 return mFdp->ConsumeIntegral<status_t>();
147 }
148 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
149 return mFdp->ConsumeIntegral<status_t>();
150 }
151 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700152 std::vector<RawEvent> getEvents(int timeoutMillis) override {
153 std::vector<RawEvent> events;
154 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
155 for (size_t i = 0; i < count; ++i) {
156 int32_t type = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidTypes)
157 : mFdp->ConsumeIntegral<int32_t>();
158 int32_t code = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidCodes)
159 : mFdp->ConsumeIntegral<int32_t>();
160 events.push_back({
161 .when = mFdp->ConsumeIntegral<nsecs_t>(),
162 .readTime = mFdp->ConsumeIntegral<nsecs_t>(),
163 .deviceId = mFdp->ConsumeIntegral<int32_t>(),
164 .type = type,
165 .code = code,
166 .value = mFdp->ConsumeIntegral<int32_t>(),
167 });
168 }
169 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700170 }
171 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
172
173 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
174 int32_t deviceId, int32_t absCode) const override {
175 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
176 };
177 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
178 // containing the raw info of the sysfs node structure.
179 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
180 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
181 int32_t BatteryId) const override {
182 return std::nullopt;
183 };
184
185 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
186 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
187 return std::nullopt;
188 };
189 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
190 return std::nullopt;
191 };
192 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
193 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
194 int32_t deviceId, int32_t lightId) const override {
195 return std::nullopt;
196 };
197 void setLightIntensities(int32_t deviceId, int32_t lightId,
198 std::unordered_map<LightColor, int32_t> intensities) override{};
199
200 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
201 return mFdp->PickValueInArray<InputDeviceCountryCode>(kCountryCodes);
202 };
203
204 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
205 return mFdp->ConsumeIntegral<int32_t>();
206 }
207 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
208 return mFdp->ConsumeIntegral<int32_t>();
209 }
210 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
211 return mFdp->ConsumeIntegral<int32_t>();
212 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000213 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700214 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
215 return mFdp->ConsumeIntegral<int32_t>();
216 }
217 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
218 int32_t* outValue) const override {
219 return mFdp->ConsumeIntegral<status_t>();
220 }
221 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
222 uint8_t* outFlags) const override {
223 return mFdp->ConsumeBool();
224 }
225 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
226 return mFdp->ConsumeBool();
227 }
228 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
229 return mFdp->ConsumeBool();
230 }
231 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
232 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
233 void getVirtualKeyDefinitions(
234 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
235 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
236 return nullptr;
237 }
238 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
239 return mFdp->ConsumeBool();
240 }
241 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
242 void cancelVibrate(int32_t deviceId) override {}
243
244 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
245
246 /* Query battery level. */
247 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
248 return std::nullopt;
249 };
250
251 /* Query battery status. */
252 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
253 return std::nullopt;
254 };
255
256 void requestReopenDevices() override {}
257 void wake() override {}
258 void dump(std::string& dump) const override {}
259 void monitor() const override {}
260 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
261 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
262 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
263};
264
265class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530266 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700267
268public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530269 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700270 ~FuzzPointerController() {}
271 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
272 return mFdp->ConsumeBool();
273 }
274 void move(float deltaX, float deltaY) override {}
275 void setButtonState(int32_t buttonState) override {}
276 int32_t getButtonState() const override { return mFdp->ConsumeIntegral<int32_t>(); }
277 void setPosition(float x, float y) override {}
278 void getPosition(float* outX, float* outY) const override {}
279 void fade(Transition transition) override {}
280 void unfade(Transition transition) override {}
281 void setPresentation(Presentation presentation) override {}
282 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
283 BitSet32 spotIdBits, int32_t displayId) override {}
284 void clearSpots() override {}
285 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
286 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
287};
288
289class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
290 TouchAffineTransformation mTransform;
291 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530292 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700293
294protected:
295 ~FuzzInputReaderPolicy() {}
296
297public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530298 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700299 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
300 }
301 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
302 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
303 return mPointerController;
304 }
305 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
306 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
307 const InputDeviceIdentifier& identifier) override {
308 return nullptr;
309 }
310 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
311 return mFdp->ConsumeRandomLengthString(32);
312 }
313 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000314 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700315 return mTransform;
316 }
317 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000318 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700319};
320
321class FuzzInputListener : public virtual InputListenerInterface {
322public:
323 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {}
324 void notifyKey(const NotifyKeyArgs* args) override {}
325 void notifyMotion(const NotifyMotionArgs* args) override {}
326 void notifySwitch(const NotifySwitchArgs* args) override {}
327 void notifySensor(const NotifySensorArgs* args) override{};
328 void notifyVibratorState(const NotifyVibratorStateArgs* args) override{};
329 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {}
330 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override{};
331};
332
333class FuzzInputReaderContext : public InputReaderContext {
334 std::shared_ptr<EventHubInterface> mEventHub;
335 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530336 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700337
338public:
339 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
340 const sp<InputReaderPolicyInterface>& policy,
341 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530342 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700343 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700344 ~FuzzInputReaderContext() {}
345 void updateGlobalMetaState() override {}
346 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
347 void disableVirtualKeysUntil(nsecs_t time) override {}
348 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
349 return mFdp->ConsumeBool();
350 }
351 void fadePointer() override {}
352 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
353 return mPolicy->obtainPointerController(0);
354 }
355 void requestTimeoutAtTime(nsecs_t when) override {}
356 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
357 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700358 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
359 return {};
360 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700361 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700362 EventHubInterface* getEventHub() override { return mEventHub.get(); }
363 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
364
365 void updateLedMetaState(int32_t metaState) override{};
366 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000367 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700368};
369
370} // namespace android