blob: f6cf1cc304babd1c5f8dc6dc03a462228ba08f69 [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
123void FakeEventHub::setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
124 getDevice(deviceId)->countryCode = countryCode;
125}
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
157void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
158 getDevice(deviceId)->leds.add(led, initialState);
159}
160
161void FakeEventHub::addSensorAxis(int32_t deviceId, int32_t absCode,
162 InputDeviceSensorType sensorType, int32_t sensorDataIndex) {
163 SensorInfo info;
164 info.sensorType = sensorType;
165 info.sensorDataIndex = sensorDataIndex;
166 getDevice(deviceId)->sensorsByAbsCode.emplace(absCode, info);
167}
168
169void FakeEventHub::setMscEvent(int32_t deviceId, int32_t mscEvent) {
170 typename BitArray<MSC_MAX>::Buffer buffer;
171 buffer[mscEvent / 32] = 1 << mscEvent % 32;
172 getDevice(deviceId)->mscBitmask.loadFromBuffer(buffer);
173}
174
175void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
176 mRawLightInfos.emplace(rawId, std::move(info));
177}
178
179void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
180 mLightBrightness.emplace(rawId, brightness);
181}
182
183void FakeEventHub::fakeLightIntensities(int32_t rawId,
184 const std::unordered_map<LightColor, int32_t> intensities) {
185 mLightIntensities.emplace(rawId, std::move(intensities));
186}
187
188bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
189 return getDevice(deviceId)->leds.valueFor(led);
190}
191
192std::vector<std::string>& FakeEventHub::getExcludedDevices() {
193 return mExcludedDevices;
194}
195
196void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
197 const VirtualKeyDefinition& definition) {
198 getDevice(deviceId)->virtualKeys.push_back(definition);
199}
200
201void FakeEventHub::enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type,
202 int32_t code, int32_t value) {
203 std::scoped_lock<std::mutex> lock(mLock);
204 RawEvent event;
205 event.when = when;
206 event.readTime = readTime;
207 event.deviceId = deviceId;
208 event.type = type;
209 event.code = code;
210 event.value = value;
211 mEvents.push_back(event);
212
213 if (type == EV_ABS) {
214 setAbsoluteAxisValue(deviceId, code, value);
215 }
216}
217
218void FakeEventHub::setVideoFrames(
219 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
220 mVideoFrames = std::move(videoFrames);
221}
222
223void FakeEventHub::assertQueueIsEmpty() {
224 std::unique_lock<std::mutex> lock(mLock);
225 base::ScopedLockAssertion assumeLocked(mLock);
226 const bool queueIsEmpty =
227 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
228 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
229 if (!queueIsEmpty) {
230 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
231 }
232}
233
234FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
235 ssize_t index = mDevices.indexOfKey(deviceId);
236 return index >= 0 ? mDevices.valueAt(index) : nullptr;
237}
238
239ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
240 Device* device = getDevice(deviceId);
241 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
242}
243
244InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
245 Device* device = getDevice(deviceId);
246 return device ? device->identifier : InputDeviceIdentifier();
247}
248
249int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
250 return 0;
251}
252
253void FakeEventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
254 Device* device = getDevice(deviceId);
255 if (device) {
256 *outConfiguration = device->configuration;
257 }
258}
259
260status_t FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
261 RawAbsoluteAxisInfo* outAxisInfo) const {
262 Device* device = getDevice(deviceId);
263 if (device) {
264 ssize_t index = device->absoluteAxes.indexOfKey(axis);
265 if (index >= 0) {
266 *outAxisInfo = device->absoluteAxes.valueAt(index);
267 return OK;
268 }
269 }
270 outAxisInfo->clear();
271 return -1;
272}
273
274bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
275 Device* device = getDevice(deviceId);
276 if (device) {
277 return device->relativeAxes.indexOfKey(axis) >= 0;
278 }
279 return false;
280}
281
282bool FakeEventHub::hasInputProperty(int32_t, int) const {
283 return false;
284}
285
286bool FakeEventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
287 Device* device = getDevice(deviceId);
288 if (device) {
289 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
290 }
291 return false;
292}
293
294status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
295 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
296 uint32_t* outFlags) const {
297 Device* device = getDevice(deviceId);
298 if (device) {
299 const KeyInfo* key = getKey(device, scanCode, usageCode);
300 if (key) {
301 if (outKeycode) {
302 *outKeycode = key->keyCode;
303 }
304 if (outFlags) {
305 *outFlags = key->flags;
306 }
307 if (outMetaState) {
308 *outMetaState = metaState;
309 }
310 return OK;
311 }
312 }
313 return NAME_NOT_FOUND;
314}
315
316const FakeEventHub::KeyInfo* FakeEventHub::getKey(Device* device, int32_t scanCode,
317 int32_t usageCode) const {
318 if (usageCode) {
319 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
320 if (index >= 0) {
321 return &device->keysByUsageCode.valueAt(index);
322 }
323 }
324 if (scanCode) {
325 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
326 if (index >= 0) {
327 return &device->keysByScanCode.valueAt(index);
328 }
329 }
330 return nullptr;
331}
332
333status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const {
334 return NAME_NOT_FOUND;
335}
336
337base::Result<std::pair<InputDeviceSensorType, int32_t>> FakeEventHub::mapSensor(
338 int32_t deviceId, int32_t absCode) const {
339 Device* device = getDevice(deviceId);
340 if (!device) {
341 return Errorf("Sensor device not found.");
342 }
343 auto it = device->sensorsByAbsCode.find(absCode);
344 if (it == device->sensorsByAbsCode.end()) {
345 return Errorf("Sensor map not found.");
346 }
347 const SensorInfo& info = it->second;
348 return std::make_pair(info.sensorType, info.sensorDataIndex);
349}
350
351void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) {
352 mExcludedDevices = devices;
353}
354
355std::vector<RawEvent> FakeEventHub::getEvents(int) {
356 std::scoped_lock lock(mLock);
357
358 std::vector<RawEvent> buffer;
359 std::swap(buffer, mEvents);
360
361 mEventsCondition.notify_all();
362 return buffer;
363}
364
365std::vector<TouchVideoFrame> FakeEventHub::getVideoFrames(int32_t deviceId) {
366 auto it = mVideoFrames.find(deviceId);
367 if (it != mVideoFrames.end()) {
368 std::vector<TouchVideoFrame> frames = std::move(it->second);
369 mVideoFrames.erase(deviceId);
370 return frames;
371 }
372 return {};
373}
374
375int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
376 Device* device = getDevice(deviceId);
377 if (device) {
378 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
379 if (index >= 0) {
380 return device->scanCodeStates.valueAt(index);
381 }
382 }
383 return AKEY_STATE_UNKNOWN;
384}
385
386InputDeviceCountryCode FakeEventHub::getCountryCode(int32_t deviceId) const {
387 Device* device = getDevice(deviceId);
388 return device ? device->countryCode : InputDeviceCountryCode::INVALID;
389}
390
391int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
392 Device* device = getDevice(deviceId);
393 if (device) {
394 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
395 if (index >= 0) {
396 return device->keyCodeStates.valueAt(index);
397 }
398 }
399 return AKEY_STATE_UNKNOWN;
400}
401
402int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
403 Device* device = getDevice(deviceId);
404 if (device) {
405 ssize_t index = device->switchStates.indexOfKey(sw);
406 if (index >= 0) {
407 return device->switchStates.valueAt(index);
408 }
409 }
410 return AKEY_STATE_UNKNOWN;
411}
412
413status_t FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
414 int32_t* outValue) const {
415 Device* device = getDevice(deviceId);
416 if (device) {
417 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
418 if (index >= 0) {
419 *outValue = device->absoluteAxisValue.valueAt(index);
420 return OK;
421 }
422 }
423 *outValue = 0;
424 return -1;
425}
426
427int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
428 Device* device = getDevice(deviceId);
429 if (!device) {
430 return AKEYCODE_UNKNOWN;
431 }
432 auto it = device->keyCodeMapping.find(locationKeyCode);
433 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
434}
435
436// Return true if the device has non-empty key layout.
437bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
438 uint8_t* outFlags) const {
439 Device* device = getDevice(deviceId);
440 if (!device) return false;
441
442 bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
443 for (size_t i = 0; i < keyCodes.size(); i++) {
444 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
445 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
446 outFlags[i] = 1;
447 }
448 }
449 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
450 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
451 outFlags[i] = 1;
452 }
453 }
454 }
455 return result;
456}
457
458bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
459 Device* device = getDevice(deviceId);
460 if (device) {
461 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
462 return index >= 0;
463 }
464 return false;
465}
466
467bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
468 Device* device = getDevice(deviceId);
469 if (!device) {
470 return false;
471 }
472 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
473 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
474 return true;
475 }
476 }
477 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
478 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
479 return true;
480 }
481 }
482 return false;
483}
484
485bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
486 Device* device = getDevice(deviceId);
487 return device && device->leds.indexOfKey(led) >= 0;
488}
489
490void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
491 Device* device = getDevice(deviceId);
492 if (device) {
493 ssize_t index = device->leds.indexOfKey(led);
494 if (index >= 0) {
495 device->leds.replaceValueAt(led, on);
496 } else {
497 ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
498 "was not present. led="
499 << led;
500 }
501 }
502}
503
504void FakeEventHub::getVirtualKeyDefinitions(
505 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
506 outVirtualKeys.clear();
507
508 Device* device = getDevice(deviceId);
509 if (device) {
510 outVirtualKeys = device->virtualKeys;
511 }
512}
513
514const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
515 return nullptr;
516}
517
518bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
519 return false;
520}
521
522std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
523 return mVibrators;
524}
525
526std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
527 return BATTERY_CAPACITY;
528}
529
530std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
531 return BATTERY_STATUS;
532}
533
534std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
535 return {DEFAULT_BATTERY};
536}
537
538std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
539 int32_t batteryId) const {
540 if (batteryId != DEFAULT_BATTERY) return {};
541 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
542 .name = "default battery",
543 .flags = InputBatteryClass::CAPACITY,
544 .path = BATTERY_DEVPATH};
545 return BATTERY_INFO;
546}
547
548std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
549 std::vector<int32_t> ids;
550 for (const auto& [rawId, info] : mRawLightInfos) {
551 ids.push_back(rawId);
552 }
553 return ids;
554}
555
556std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
557 auto it = mRawLightInfos.find(lightId);
558 if (it == mRawLightInfos.end()) {
559 return std::nullopt;
560 }
561 return it->second;
562}
563
564void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
565 mLightBrightness.emplace(lightId, brightness);
566}
567
568void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
569 std::unordered_map<LightColor, int32_t> intensities) {
570 mLightIntensities.emplace(lightId, intensities);
571};
572
573std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
574 auto lightIt = mLightBrightness.find(lightId);
575 if (lightIt == mLightBrightness.end()) {
576 return std::nullopt;
577 }
578 return lightIt->second;
579}
580
581std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
582 int32_t deviceId, int32_t lightId) const {
583 auto lightIt = mLightIntensities.find(lightId);
584 if (lightIt == mLightIntensities.end()) {
585 return std::nullopt;
586 }
587 return lightIt->second;
588};
589
590} // namespace android