blob: 6ac0bfb54125744062c8a447e7d22567573a5bc9 [file] [log] [blame]
Harry Cuttsa5b71292022-11-28 12:56:17 +00001/*
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 */
16
17#include "FakeEventHub.h"
18
19#include <android-base/thread_annotations.h>
20#include <gtest/gtest.h>
21#include <linux/input-event-codes.h>
22
23#include "TestConstants.h"
24
25namespace android {
26
27const std::string FakeEventHub::BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
28
29FakeEventHub::~FakeEventHub() {
30 for (size_t i = 0; i < mDevices.size(); i++) {
31 delete mDevices.valueAt(i);
32 }
33}
34
35void FakeEventHub::addDevice(int32_t deviceId, const std::string& name,
36 ftl::Flags<InputDeviceClass> classes, int bus) {
37 Device* device = new Device(classes);
38 device->identifier.name = name;
39 device->identifier.bus = bus;
40 mDevices.add(deviceId, device);
41
42 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
43}
44
45void FakeEventHub::removeDevice(int32_t deviceId) {
46 delete mDevices.valueFor(deviceId);
47 mDevices.removeItem(deviceId);
48
49 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
50}
51
52bool FakeEventHub::isDeviceEnabled(int32_t deviceId) const {
53 Device* device = getDevice(deviceId);
54 if (device == nullptr) {
55 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
56 return false;
57 }
58 return device->enabled;
59}
60
61status_t FakeEventHub::enableDevice(int32_t deviceId) {
62 status_t result;
63 Device* device = getDevice(deviceId);
64 if (device == nullptr) {
65 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
66 return BAD_VALUE;
67 }
68 if (device->enabled) {
69 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
70 return OK;
71 }
72 result = device->enable();
73 return result;
74}
75
76status_t FakeEventHub::disableDevice(int32_t deviceId) {
77 Device* device = getDevice(deviceId);
78 if (device == nullptr) {
79 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
80 return BAD_VALUE;
81 }
82 if (!device->enabled) {
83 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
84 return OK;
85 }
86 return device->disable();
87}
88
89void FakeEventHub::finishDeviceScan() {
90 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
91}
92
93void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
94 getDevice(deviceId)->configuration.addProperty(key, value);
95}
96
97void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
98 getDevice(deviceId)->configuration.addAll(configuration);
99}
100
101void FakeEventHub::addAbsoluteAxis(int32_t deviceId, int axis, int32_t minValue, int32_t maxValue,
102 int flat, int fuzz, int resolution) {
103 Device* device = getDevice(deviceId);
104
105 RawAbsoluteAxisInfo info;
106 info.valid = true;
107 info.minValue = minValue;
108 info.maxValue = maxValue;
109 info.flat = flat;
110 info.fuzz = fuzz;
111 info.resolution = resolution;
112 device->absoluteAxes.add(axis, info);
113}
114
115void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) {
116 getDevice(deviceId)->relativeAxes.add(axis, true);
117}
118
119void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
120 getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state);
121}
122
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000123void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) {
124 getDevice(deviceId)->layoutInfo = info;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000125}
126
127void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
128 getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state);
129}
130
131void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
132 getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state);
133}
134
135void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
136 getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value);
137}
138
139void FakeEventHub::addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode,
140 uint32_t flags) {
141 Device* device = getDevice(deviceId);
142 KeyInfo info;
143 info.keyCode = keyCode;
144 info.flags = flags;
145 if (scanCode) {
146 device->keysByScanCode.add(scanCode, info);
147 }
148 if (usageCode) {
149 device->keysByUsageCode.add(usageCode, info);
150 }
151}
152
153void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
154 getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
155}
156
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000157void FakeEventHub::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
158 Device* device = getDevice(deviceId);
159 device->keyRemapping.insert_or_assign(fromKeyCode, toKeyCode);
160}
161
Harry Cuttsa5b71292022-11-28 12:56:17 +0000162void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
163 getDevice(deviceId)->leds.add(led, initialState);
164}
165
166void FakeEventHub::addSensorAxis(int32_t deviceId, int32_t absCode,
167 InputDeviceSensorType sensorType, int32_t sensorDataIndex) {
168 SensorInfo info;
169 info.sensorType = sensorType;
170 info.sensorDataIndex = sensorDataIndex;
171 getDevice(deviceId)->sensorsByAbsCode.emplace(absCode, info);
172}
173
174void FakeEventHub::setMscEvent(int32_t deviceId, int32_t mscEvent) {
175 typename BitArray<MSC_MAX>::Buffer buffer;
176 buffer[mscEvent / 32] = 1 << mscEvent % 32;
177 getDevice(deviceId)->mscBitmask.loadFromBuffer(buffer);
178}
179
180void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
181 mRawLightInfos.emplace(rawId, std::move(info));
182}
183
184void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
185 mLightBrightness.emplace(rawId, brightness);
186}
187
188void FakeEventHub::fakeLightIntensities(int32_t rawId,
189 const std::unordered_map<LightColor, int32_t> intensities) {
190 mLightIntensities.emplace(rawId, std::move(intensities));
191}
192
193bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
194 return getDevice(deviceId)->leds.valueFor(led);
195}
196
197std::vector<std::string>& FakeEventHub::getExcludedDevices() {
198 return mExcludedDevices;
199}
200
201void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
202 const VirtualKeyDefinition& definition) {
203 getDevice(deviceId)->virtualKeys.push_back(definition);
204}
205
206void FakeEventHub::enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type,
207 int32_t code, int32_t value) {
208 std::scoped_lock<std::mutex> lock(mLock);
209 RawEvent event;
210 event.when = when;
211 event.readTime = readTime;
212 event.deviceId = deviceId;
213 event.type = type;
214 event.code = code;
215 event.value = value;
216 mEvents.push_back(event);
217
218 if (type == EV_ABS) {
219 setAbsoluteAxisValue(deviceId, code, value);
220 }
221}
222
223void FakeEventHub::setVideoFrames(
224 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
225 mVideoFrames = std::move(videoFrames);
226}
227
228void FakeEventHub::assertQueueIsEmpty() {
229 std::unique_lock<std::mutex> lock(mLock);
230 base::ScopedLockAssertion assumeLocked(mLock);
231 const bool queueIsEmpty =
232 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
233 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
234 if (!queueIsEmpty) {
235 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
236 }
237}
238
239FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
240 ssize_t index = mDevices.indexOfKey(deviceId);
241 return index >= 0 ? mDevices.valueAt(index) : nullptr;
242}
243
244ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
245 Device* device = getDevice(deviceId);
246 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
247}
248
249InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
250 Device* device = getDevice(deviceId);
251 return device ? device->identifier : InputDeviceIdentifier();
252}
253
254int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
255 return 0;
256}
257
258void FakeEventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
259 Device* device = getDevice(deviceId);
260 if (device) {
261 *outConfiguration = device->configuration;
262 }
263}
264
265status_t FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
266 RawAbsoluteAxisInfo* outAxisInfo) const {
267 Device* device = getDevice(deviceId);
268 if (device) {
269 ssize_t index = device->absoluteAxes.indexOfKey(axis);
270 if (index >= 0) {
271 *outAxisInfo = device->absoluteAxes.valueAt(index);
272 return OK;
273 }
274 }
275 outAxisInfo->clear();
276 return -1;
277}
278
279bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
280 Device* device = getDevice(deviceId);
281 if (device) {
282 return device->relativeAxes.indexOfKey(axis) >= 0;
283 }
284 return false;
285}
286
287bool FakeEventHub::hasInputProperty(int32_t, int) const {
288 return false;
289}
290
291bool FakeEventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
292 Device* device = getDevice(deviceId);
293 if (device) {
294 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
295 }
296 return false;
297}
298
299status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
300 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
301 uint32_t* outFlags) const {
302 Device* device = getDevice(deviceId);
303 if (device) {
304 const KeyInfo* key = getKey(device, scanCode, usageCode);
305 if (key) {
306 if (outKeycode) {
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000307 auto it = device->keyRemapping.find(key->keyCode);
308 *outKeycode = it != device->keyRemapping.end() ? it->second : key->keyCode;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000309 }
310 if (outFlags) {
311 *outFlags = key->flags;
312 }
313 if (outMetaState) {
314 *outMetaState = metaState;
315 }
316 return OK;
317 }
318 }
319 return NAME_NOT_FOUND;
320}
321
322const FakeEventHub::KeyInfo* FakeEventHub::getKey(Device* device, int32_t scanCode,
323 int32_t usageCode) const {
324 if (usageCode) {
325 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
326 if (index >= 0) {
327 return &device->keysByUsageCode.valueAt(index);
328 }
329 }
330 if (scanCode) {
331 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
332 if (index >= 0) {
333 return &device->keysByScanCode.valueAt(index);
334 }
335 }
336 return nullptr;
337}
338
339status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const {
340 return NAME_NOT_FOUND;
341}
342
343base::Result<std::pair<InputDeviceSensorType, int32_t>> FakeEventHub::mapSensor(
344 int32_t deviceId, int32_t absCode) const {
345 Device* device = getDevice(deviceId);
346 if (!device) {
347 return Errorf("Sensor device not found.");
348 }
349 auto it = device->sensorsByAbsCode.find(absCode);
350 if (it == device->sensorsByAbsCode.end()) {
351 return Errorf("Sensor map not found.");
352 }
353 const SensorInfo& info = it->second;
354 return std::make_pair(info.sensorType, info.sensorDataIndex);
355}
356
357void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) {
358 mExcludedDevices = devices;
359}
360
361std::vector<RawEvent> FakeEventHub::getEvents(int) {
362 std::scoped_lock lock(mLock);
363
364 std::vector<RawEvent> buffer;
365 std::swap(buffer, mEvents);
366
367 mEventsCondition.notify_all();
368 return buffer;
369}
370
371std::vector<TouchVideoFrame> FakeEventHub::getVideoFrames(int32_t deviceId) {
372 auto it = mVideoFrames.find(deviceId);
373 if (it != mVideoFrames.end()) {
374 std::vector<TouchVideoFrame> frames = std::move(it->second);
375 mVideoFrames.erase(deviceId);
376 return frames;
377 }
378 return {};
379}
380
381int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
382 Device* device = getDevice(deviceId);
383 if (device) {
384 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
385 if (index >= 0) {
386 return device->scanCodeStates.valueAt(index);
387 }
388 }
389 return AKEY_STATE_UNKNOWN;
390}
391
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000392std::optional<RawLayoutInfo> FakeEventHub::getRawLayoutInfo(int32_t deviceId) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000393 Device* device = getDevice(deviceId);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000394 return device ? device->layoutInfo : std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000395}
396
397int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
398 Device* device = getDevice(deviceId);
399 if (device) {
400 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
401 if (index >= 0) {
402 return device->keyCodeStates.valueAt(index);
403 }
404 }
405 return AKEY_STATE_UNKNOWN;
406}
407
408int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
409 Device* device = getDevice(deviceId);
410 if (device) {
411 ssize_t index = device->switchStates.indexOfKey(sw);
412 if (index >= 0) {
413 return device->switchStates.valueAt(index);
414 }
415 }
416 return AKEY_STATE_UNKNOWN;
417}
418
419status_t FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
420 int32_t* outValue) const {
421 Device* device = getDevice(deviceId);
422 if (device) {
423 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
424 if (index >= 0) {
425 *outValue = device->absoluteAxisValue.valueAt(index);
426 return OK;
427 }
428 }
429 *outValue = 0;
430 return -1;
431}
432
433int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
434 Device* device = getDevice(deviceId);
435 if (!device) {
436 return AKEYCODE_UNKNOWN;
437 }
438 auto it = device->keyCodeMapping.find(locationKeyCode);
439 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
440}
441
442// Return true if the device has non-empty key layout.
443bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
444 uint8_t* outFlags) const {
445 Device* device = getDevice(deviceId);
446 if (!device) return false;
447
448 bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
449 for (size_t i = 0; i < keyCodes.size(); i++) {
450 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
451 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
452 outFlags[i] = 1;
453 }
454 }
455 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
456 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
457 outFlags[i] = 1;
458 }
459 }
460 }
461 return result;
462}
463
464bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
465 Device* device = getDevice(deviceId);
466 if (device) {
467 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
468 return index >= 0;
469 }
470 return false;
471}
472
473bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
474 Device* device = getDevice(deviceId);
475 if (!device) {
476 return false;
477 }
478 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
479 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
480 return true;
481 }
482 }
483 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
484 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
485 return true;
486 }
487 }
488 return false;
489}
490
491bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
492 Device* device = getDevice(deviceId);
493 return device && device->leds.indexOfKey(led) >= 0;
494}
495
496void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
497 Device* device = getDevice(deviceId);
498 if (device) {
499 ssize_t index = device->leds.indexOfKey(led);
500 if (index >= 0) {
501 device->leds.replaceValueAt(led, on);
502 } else {
503 ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
504 "was not present. led="
505 << led;
506 }
507 }
508}
509
510void FakeEventHub::getVirtualKeyDefinitions(
511 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
512 outVirtualKeys.clear();
513
514 Device* device = getDevice(deviceId);
515 if (device) {
516 outVirtualKeys = device->virtualKeys;
517 }
518}
519
520const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
521 return nullptr;
522}
523
524bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
525 return false;
526}
527
528std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
529 return mVibrators;
530}
531
532std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
533 return BATTERY_CAPACITY;
534}
535
536std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
537 return BATTERY_STATUS;
538}
539
540std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
541 return {DEFAULT_BATTERY};
542}
543
544std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
545 int32_t batteryId) const {
546 if (batteryId != DEFAULT_BATTERY) return {};
547 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
548 .name = "default battery",
549 .flags = InputBatteryClass::CAPACITY,
550 .path = BATTERY_DEVPATH};
551 return BATTERY_INFO;
552}
553
554std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
555 std::vector<int32_t> ids;
556 for (const auto& [rawId, info] : mRawLightInfos) {
557 ids.push_back(rawId);
558 }
559 return ids;
560}
561
562std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
563 auto it = mRawLightInfos.find(lightId);
564 if (it == mRawLightInfos.end()) {
565 return std::nullopt;
566 }
567 return it->second;
568}
569
570void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
571 mLightBrightness.emplace(lightId, brightness);
572}
573
574void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
575 std::unordered_map<LightColor, int32_t> intensities) {
576 mLightIntensities.emplace(lightId, intensities);
577};
578
579std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
580 auto lightIt = mLightBrightness.find(lightId);
581 if (lightIt == mLightBrightness.end()) {
582 return std::nullopt;
583 }
584 return lightIt->second;
585}
586
587std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
588 int32_t deviceId, int32_t lightId) const {
589 auto lightIt = mLightIntensities.find(lightId);
590 if (lightIt == mLightIntensities.end()) {
591 return std::nullopt;
592 }
593 return lightIt->second;
594};
595
596} // namespace android