1 /*
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 <optional>
20
21 #include <android-base/thread_annotations.h>
22 #include <gtest/gtest.h>
23 #include <linux/input-event-codes.h>
24
25 #include "TestConstants.h"
26
27 namespace android {
28
29 const std::string FakeEventHub::BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
30
~FakeEventHub()31 FakeEventHub::~FakeEventHub() {
32 for (size_t i = 0; i < mDevices.size(); i++) {
33 delete mDevices.valueAt(i);
34 }
35 }
36
addDevice(int32_t deviceId,const std::string & name,ftl::Flags<InputDeviceClass> classes,int bus)37 void FakeEventHub::addDevice(int32_t deviceId, const std::string& name,
38 ftl::Flags<InputDeviceClass> classes, int bus) {
39 Device* device = new Device(classes);
40 device->identifier.name = name;
41 device->identifier.bus = bus;
42 mDevices.add(deviceId, device);
43
44 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
45 }
46
removeDevice(int32_t deviceId)47 void FakeEventHub::removeDevice(int32_t deviceId) {
48 delete mDevices.valueFor(deviceId);
49 mDevices.removeItem(deviceId);
50
51 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
52 }
53
isDeviceEnabled(int32_t deviceId) const54 bool FakeEventHub::isDeviceEnabled(int32_t deviceId) const {
55 Device* device = getDevice(deviceId);
56 if (device == nullptr) {
57 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
58 return false;
59 }
60 return device->enabled;
61 }
62
enableDevice(int32_t deviceId)63 status_t FakeEventHub::enableDevice(int32_t deviceId) {
64 status_t result;
65 Device* device = getDevice(deviceId);
66 if (device == nullptr) {
67 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
68 return BAD_VALUE;
69 }
70 if (device->enabled) {
71 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
72 return OK;
73 }
74 result = device->enable();
75 return result;
76 }
77
disableDevice(int32_t deviceId)78 status_t FakeEventHub::disableDevice(int32_t deviceId) {
79 Device* device = getDevice(deviceId);
80 if (device == nullptr) {
81 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
82 return BAD_VALUE;
83 }
84 if (!device->enabled) {
85 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
86 return OK;
87 }
88 return device->disable();
89 }
90
addConfigurationProperty(int32_t deviceId,const char * key,const char * value)91 void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
92 getDevice(deviceId)->configuration.addProperty(key, value);
93 }
94
addConfigurationMap(int32_t deviceId,const PropertyMap * configuration)95 void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
96 getDevice(deviceId)->configuration.addAll(configuration);
97 }
98
addAbsoluteAxis(int32_t deviceId,int axis,int32_t minValue,int32_t maxValue,int flat,int fuzz,int resolution)99 void FakeEventHub::addAbsoluteAxis(int32_t deviceId, int axis, int32_t minValue, int32_t maxValue,
100 int flat, int fuzz, int resolution) {
101 Device* device = getDevice(deviceId);
102
103 RawAbsoluteAxisInfo info;
104 info.minValue = minValue;
105 info.maxValue = maxValue;
106 info.flat = flat;
107 info.fuzz = fuzz;
108 info.resolution = resolution;
109 device->absoluteAxes.add(axis, info);
110 }
111
addRelativeAxis(int32_t deviceId,int32_t axis)112 void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) {
113 getDevice(deviceId)->relativeAxes.add(axis, true);
114 }
115
setKeyCodeState(int32_t deviceId,int32_t keyCode,int32_t state)116 void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
117 getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state);
118 }
119
setRawLayoutInfo(int32_t deviceId,RawLayoutInfo info)120 void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) {
121 getDevice(deviceId)->layoutInfo = info;
122 }
123
setScanCodeState(int32_t deviceId,int32_t scanCode,int32_t state)124 void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
125 getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state);
126 }
127
setSwitchState(int32_t deviceId,int32_t switchCode,int32_t state)128 void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
129 getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state);
130 }
131
setAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t value)132 void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
133 getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value);
134 }
135
addKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t keyCode,uint32_t flags)136 void FakeEventHub::addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode,
137 uint32_t flags) {
138 Device* device = getDevice(deviceId);
139 KeyInfo info;
140 info.keyCode = keyCode;
141 info.flags = flags;
142 if (scanCode) {
143 device->keysByScanCode.add(scanCode, info);
144 }
145 if (usageCode) {
146 device->keysByUsageCode.add(usageCode, info);
147 }
148 }
149
addKeyCodeMapping(int32_t deviceId,int32_t fromKeyCode,int32_t toKeyCode)150 void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
151 getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
152 }
153
setKeyRemapping(int32_t deviceId,const std::map<int32_t,int32_t> & keyRemapping) const154 void FakeEventHub::setKeyRemapping(int32_t deviceId,
155 const std::map<int32_t, int32_t>& keyRemapping) const {
156 Device* device = getDevice(deviceId);
157 device->keyRemapping = keyRemapping;
158 }
159
addLed(int32_t deviceId,int32_t led,bool initialState)160 void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
161 getDevice(deviceId)->leds.add(led, initialState);
162 }
163
addSensorAxis(int32_t deviceId,int32_t absCode,InputDeviceSensorType sensorType,int32_t sensorDataIndex)164 void FakeEventHub::addSensorAxis(int32_t deviceId, int32_t absCode,
165 InputDeviceSensorType sensorType, int32_t sensorDataIndex) {
166 SensorInfo info;
167 info.sensorType = sensorType;
168 info.sensorDataIndex = sensorDataIndex;
169 getDevice(deviceId)->sensorsByAbsCode.emplace(absCode, info);
170 }
171
setMscEvent(int32_t deviceId,int32_t mscEvent)172 void FakeEventHub::setMscEvent(int32_t deviceId, int32_t mscEvent) {
173 typename BitArray<MSC_MAX>::Buffer buffer;
174 buffer[mscEvent / 32] = 1 << mscEvent % 32;
175 getDevice(deviceId)->mscBitmask.loadFromBuffer(buffer);
176 }
177
addRawLightInfo(int32_t rawId,RawLightInfo && info)178 void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
179 mRawLightInfos.emplace(rawId, std::move(info));
180 }
181
fakeLightBrightness(int32_t rawId,int32_t brightness)182 void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
183 mLightBrightness.emplace(rawId, brightness);
184 }
185
fakeLightIntensities(int32_t rawId,const std::unordered_map<LightColor,int32_t> intensities)186 void FakeEventHub::fakeLightIntensities(int32_t rawId,
187 const std::unordered_map<LightColor, int32_t> intensities) {
188 mLightIntensities.emplace(rawId, std::move(intensities));
189 }
190
getLedState(int32_t deviceId,int32_t led)191 bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
192 return getDevice(deviceId)->leds.valueFor(led);
193 }
194
getExcludedDevices()195 std::vector<std::string>& FakeEventHub::getExcludedDevices() {
196 return mExcludedDevices;
197 }
198
addVirtualKeyDefinition(int32_t deviceId,const VirtualKeyDefinition & definition)199 void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
200 const VirtualKeyDefinition& definition) {
201 getDevice(deviceId)->virtualKeys.push_back(definition);
202 }
203
enqueueEvent(nsecs_t when,nsecs_t readTime,int32_t deviceId,int32_t type,int32_t code,int32_t value)204 void FakeEventHub::enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type,
205 int32_t code, int32_t value) {
206 std::scoped_lock<std::mutex> lock(mLock);
207 RawEvent event;
208 event.when = when;
209 event.readTime = readTime;
210 event.deviceId = deviceId;
211 event.type = type;
212 event.code = code;
213 event.value = value;
214 mEvents.push_back(event);
215
216 if (type == EV_ABS) {
217 setAbsoluteAxisValue(deviceId, code, value);
218 }
219 }
220
setVideoFrames(std::unordered_map<int32_t,std::vector<TouchVideoFrame>> videoFrames)221 void FakeEventHub::setVideoFrames(
222 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
223 mVideoFrames = std::move(videoFrames);
224 }
225
assertQueueIsEmpty()226 void FakeEventHub::assertQueueIsEmpty() {
227 std::unique_lock<std::mutex> lock(mLock);
228 base::ScopedLockAssertion assumeLocked(mLock);
229 const bool queueIsEmpty =
230 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
231 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
232 if (!queueIsEmpty) {
233 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
234 }
235 }
236
getDevice(int32_t deviceId) const237 FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
238 ssize_t index = mDevices.indexOfKey(deviceId);
239 return index >= 0 ? mDevices.valueAt(index) : nullptr;
240 }
241
getDeviceClasses(int32_t deviceId) const242 ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
243 Device* device = getDevice(deviceId);
244 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
245 }
246
getDeviceIdentifier(int32_t deviceId) const247 InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
248 Device* device = getDevice(deviceId);
249 return device ? device->identifier : InputDeviceIdentifier();
250 }
251
getDeviceControllerNumber(int32_t) const252 int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
253 return 0;
254 }
255
getConfiguration(int32_t deviceId) const256 std::optional<PropertyMap> FakeEventHub::getConfiguration(int32_t deviceId) const {
257 Device* device = getDevice(deviceId);
258 if (device == nullptr) {
259 return {};
260 }
261 return device->configuration;
262 }
263
getAbsoluteAxisInfo(int32_t deviceId,int axis) const264 std::optional<RawAbsoluteAxisInfo> FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId,
265 int axis) const {
266 Device* device = getDevice(deviceId);
267 if (device) {
268 ssize_t index = device->absoluteAxes.indexOfKey(axis);
269 if (index >= 0) {
270 return device->absoluteAxes.valueAt(index);
271 }
272 }
273 return std::nullopt;
274 }
275
hasRelativeAxis(int32_t deviceId,int axis) const276 bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
277 Device* device = getDevice(deviceId);
278 if (device) {
279 return device->relativeAxes.indexOfKey(axis) >= 0;
280 }
281 return false;
282 }
283
hasInputProperty(int32_t,int) const284 bool FakeEventHub::hasInputProperty(int32_t, int) const {
285 return false;
286 }
287
hasMscEvent(int32_t deviceId,int mscEvent) const288 bool FakeEventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
289 Device* device = getDevice(deviceId);
290 if (device) {
291 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
292 }
293 return false;
294 }
295
mapKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t metaState,int32_t * outKeycode,int32_t * outMetaState,uint32_t * outFlags) const296 status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
297 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
298 uint32_t* outFlags) const {
299 Device* device = getDevice(deviceId);
300 if (device) {
301 const KeyInfo* key = getKey(device, scanCode, usageCode);
302 if (key) {
303 if (outKeycode) {
304 auto it = device->keyRemapping.find(key->keyCode);
305 *outKeycode = it != device->keyRemapping.end() ? it->second : key->keyCode;
306 }
307 if (outFlags) {
308 *outFlags = key->flags;
309 }
310 if (outMetaState) {
311 *outMetaState = metaState;
312 }
313 return OK;
314 }
315 }
316 return NAME_NOT_FOUND;
317 }
318
getKey(Device * device,int32_t scanCode,int32_t usageCode) const319 const FakeEventHub::KeyInfo* FakeEventHub::getKey(Device* device, int32_t scanCode,
320 int32_t usageCode) const {
321 if (usageCode) {
322 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
323 if (index >= 0) {
324 return &device->keysByUsageCode.valueAt(index);
325 }
326 }
327 if (scanCode) {
328 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
329 if (index >= 0) {
330 return &device->keysByScanCode.valueAt(index);
331 }
332 }
333 return nullptr;
334 }
335
mapAxis(int32_t,int32_t,AxisInfo *) const336 status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const {
337 return NAME_NOT_FOUND;
338 }
339
mapSensor(int32_t deviceId,int32_t absCode) const340 base::Result<std::pair<InputDeviceSensorType, int32_t>> FakeEventHub::mapSensor(
341 int32_t deviceId, int32_t absCode) const {
342 Device* device = getDevice(deviceId);
343 if (!device) {
344 return Errorf("Sensor device not found.");
345 }
346 auto it = device->sensorsByAbsCode.find(absCode);
347 if (it == device->sensorsByAbsCode.end()) {
348 return Errorf("Sensor map not found.");
349 }
350 const SensorInfo& info = it->second;
351 return std::make_pair(info.sensorType, info.sensorDataIndex);
352 }
353
setExcludedDevices(const std::vector<std::string> & devices)354 void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) {
355 mExcludedDevices = devices;
356 }
357
getEvents(int)358 std::vector<RawEvent> FakeEventHub::getEvents(int) {
359 std::scoped_lock lock(mLock);
360
361 std::vector<RawEvent> buffer;
362 std::swap(buffer, mEvents);
363
364 mEventsCondition.notify_all();
365 return buffer;
366 }
367
getVideoFrames(int32_t deviceId)368 std::vector<TouchVideoFrame> FakeEventHub::getVideoFrames(int32_t deviceId) {
369 auto it = mVideoFrames.find(deviceId);
370 if (it != mVideoFrames.end()) {
371 std::vector<TouchVideoFrame> frames = std::move(it->second);
372 mVideoFrames.erase(deviceId);
373 return frames;
374 }
375 return {};
376 }
377
getScanCodeState(int32_t deviceId,int32_t scanCode) const378 int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
379 Device* device = getDevice(deviceId);
380 if (device) {
381 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
382 if (index >= 0) {
383 return device->scanCodeStates.valueAt(index);
384 }
385 }
386 return AKEY_STATE_UNKNOWN;
387 }
388
getRawLayoutInfo(int32_t deviceId) const389 std::optional<RawLayoutInfo> FakeEventHub::getRawLayoutInfo(int32_t deviceId) const {
390 Device* device = getDevice(deviceId);
391 return device ? device->layoutInfo : std::nullopt;
392 }
393
getKeyCodeState(int32_t deviceId,int32_t keyCode) const394 int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
395 Device* device = getDevice(deviceId);
396 if (device) {
397 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
398 if (index >= 0) {
399 return device->keyCodeStates.valueAt(index);
400 }
401 }
402 return AKEY_STATE_UNKNOWN;
403 }
404
getSwitchState(int32_t deviceId,int32_t sw) const405 int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
406 Device* device = getDevice(deviceId);
407 if (device) {
408 ssize_t index = device->switchStates.indexOfKey(sw);
409 if (index >= 0) {
410 return device->switchStates.valueAt(index);
411 }
412 }
413 return AKEY_STATE_UNKNOWN;
414 }
415
getAbsoluteAxisValue(int32_t deviceId,int32_t axis) const416 std::optional<int32_t> FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis) const {
417 Device* device = getDevice(deviceId);
418 if (device) {
419 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
420 if (index >= 0) {
421 return device->absoluteAxisValue.valueAt(index);
422 }
423 }
424 return std::nullopt;
425 }
426
setMtSlotValues(int32_t deviceId,int32_t axis,const std::vector<int32_t> & values)427 void FakeEventHub::setMtSlotValues(int32_t deviceId, int32_t axis,
428 const std::vector<int32_t>& values) {
429 Device* device = getDevice(deviceId);
430 if (!device) {
431 FAIL() << "Missing device";
432 }
433 device->mtSlotValues[axis] = values;
434 }
435
getMtSlotValues(int32_t deviceId,int32_t axis,size_t slotCount) const436 base::Result<std::vector<int32_t>> FakeEventHub::getMtSlotValues(int32_t deviceId, int32_t axis,
437 size_t slotCount) const {
438 Device* device = getDevice(deviceId);
439 if (!device) {
440 ADD_FAILURE() << "Missing device";
441 return base::ResultError("Missing device", UNKNOWN_ERROR);
442 }
443 const auto& mtSlotValuesIterator = device->mtSlotValues.find(axis);
444 if (mtSlotValuesIterator == device->mtSlotValues.end()) {
445 return base::ResultError("axis not supported", NAME_NOT_FOUND);
446 }
447 const auto& mtSlotValues = mtSlotValuesIterator->second;
448 if (mtSlotValues.size() != slotCount) {
449 ADD_FAILURE() << "MtSlot values specified for " << mtSlotValues.size()
450 << " slots but expected for " << slotCount << " Slots";
451 return base::ResultError("Slot count mismatch", NAME_NOT_FOUND);
452 }
453 std::vector<int32_t> outValues(slotCount + 1);
454 outValues[0] = axis;
455 std::copy(mtSlotValues.begin(), mtSlotValues.end(), outValues.begin() + 1);
456 return std::move(outValues);
457 }
458
getKeyCodeForKeyLocation(int32_t deviceId,int32_t locationKeyCode) const459 int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
460 Device* device = getDevice(deviceId);
461 if (!device) {
462 return AKEYCODE_UNKNOWN;
463 }
464 auto it = device->keyCodeMapping.find(locationKeyCode);
465 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
466 }
467
468 // Return true if the device has non-empty key layout.
markSupportedKeyCodes(int32_t deviceId,const std::vector<int32_t> & keyCodes,uint8_t * outFlags) const469 bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
470 uint8_t* outFlags) const {
471 Device* device = getDevice(deviceId);
472 if (!device) return false;
473
474 bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
475 for (size_t i = 0; i < keyCodes.size(); i++) {
476 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
477 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
478 outFlags[i] = 1;
479 }
480 }
481 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
482 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
483 outFlags[i] = 1;
484 }
485 }
486 }
487 return result;
488 }
489
hasScanCode(int32_t deviceId,int32_t scanCode) const490 bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
491 Device* device = getDevice(deviceId);
492 if (device) {
493 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
494 return index >= 0;
495 }
496 return false;
497 }
498
hasKeyCode(int32_t deviceId,int32_t keyCode) const499 bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
500 Device* device = getDevice(deviceId);
501 if (!device) {
502 return false;
503 }
504 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
505 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
506 return true;
507 }
508 }
509 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
510 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
511 return true;
512 }
513 }
514 return false;
515 }
516
hasLed(int32_t deviceId,int32_t led) const517 bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
518 Device* device = getDevice(deviceId);
519 return device && device->leds.indexOfKey(led) >= 0;
520 }
521
setLedState(int32_t deviceId,int32_t led,bool on)522 void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
523 Device* device = getDevice(deviceId);
524 if (device) {
525 ssize_t index = device->leds.indexOfKey(led);
526 if (index >= 0) {
527 device->leds.replaceValueAt(led, on);
528 } else {
529 ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
530 "was not present. led="
531 << led;
532 }
533 }
534 }
535
getVirtualKeyDefinitions(int32_t deviceId,std::vector<VirtualKeyDefinition> & outVirtualKeys) const536 void FakeEventHub::getVirtualKeyDefinitions(
537 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
538 outVirtualKeys.clear();
539
540 Device* device = getDevice(deviceId);
541 if (device) {
542 outVirtualKeys = device->virtualKeys;
543 }
544 }
545
getKeyCharacterMap(int32_t) const546 const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
547 return nullptr;
548 }
549
setKeyboardLayoutOverlay(int32_t,std::shared_ptr<KeyCharacterMap>)550 bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
551 return false;
552 }
553
getVibratorIds(int32_t deviceId) const554 std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
555 return mVibrators;
556 }
557
getBatteryCapacity(int32_t,int32_t) const558 std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
559 return BATTERY_CAPACITY;
560 }
561
getBatteryStatus(int32_t,int32_t) const562 std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
563 return BATTERY_STATUS;
564 }
565
getRawBatteryIds(int32_t deviceId) const566 std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
567 return {DEFAULT_BATTERY};
568 }
569
getRawBatteryInfo(int32_t deviceId,int32_t batteryId) const570 std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
571 int32_t batteryId) const {
572 if (batteryId != DEFAULT_BATTERY) return {};
573 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
574 .name = "default battery",
575 .flags = InputBatteryClass::CAPACITY,
576 .path = BATTERY_DEVPATH};
577 return BATTERY_INFO;
578 }
579
getRawLightIds(int32_t deviceId) const580 std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
581 std::vector<int32_t> ids;
582 for (const auto& [rawId, info] : mRawLightInfos) {
583 ids.push_back(rawId);
584 }
585 return ids;
586 }
587
getRawLightInfo(int32_t deviceId,int32_t lightId) const588 std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
589 auto it = mRawLightInfos.find(lightId);
590 if (it == mRawLightInfos.end()) {
591 return std::nullopt;
592 }
593 return it->second;
594 }
595
setLightBrightness(int32_t deviceId,int32_t lightId,int32_t brightness)596 void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
597 mLightBrightness.emplace(lightId, brightness);
598 }
599
setLightIntensities(int32_t deviceId,int32_t lightId,std::unordered_map<LightColor,int32_t> intensities)600 void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
601 std::unordered_map<LightColor, int32_t> intensities) {
602 mLightIntensities.emplace(lightId, intensities);
603 };
604
getLightBrightness(int32_t deviceId,int32_t lightId) const605 std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
606 auto lightIt = mLightBrightness.find(lightId);
607 if (lightIt == mLightBrightness.end()) {
608 return std::nullopt;
609 }
610 return lightIt->second;
611 }
612
getLightIntensities(int32_t deviceId,int32_t lightId) const613 std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
614 int32_t deviceId, int32_t lightId) const {
615 auto lightIt = mLightIntensities.find(lightId);
616 if (lightIt == mLightIntensities.end()) {
617 return std::nullopt;
618 }
619 return lightIt->second;
620 };
621
setSysfsRootPath(int32_t deviceId,std::string sysfsRootPath) const622 void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const {
623 Device* device = getDevice(deviceId);
624 if (device == nullptr) {
625 return;
626 }
627 device->sysfsRootPath = sysfsRootPath;
628 }
629
sysfsNodeChanged(const std::string & sysfsNodePath)630 void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
631 int32_t foundDeviceId = -1;
632 Device* foundDevice = nullptr;
633 for (size_t i = 0; i < mDevices.size(); i++) {
634 Device* d = mDevices.valueAt(i);
635 if (sysfsNodePath.find(d->sysfsRootPath) != std::string::npos) {
636 foundDeviceId = mDevices.keyAt(i);
637 foundDevice = d;
638 }
639 }
640 if (foundDevice == nullptr) {
641 return;
642 }
643 // If device sysfs changed -> reopen the device
644 if (!mRawLightInfos.empty() && !foundDevice->classes.test(InputDeviceClass::LIGHT)) {
645 InputDeviceIdentifier identifier = foundDevice->identifier;
646 ftl::Flags<InputDeviceClass> classes = foundDevice->classes;
647 removeDevice(foundDeviceId);
648 addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT,
649 identifier.bus);
650 }
651 }
652
setKernelWakeEnabled(int32_t deviceId,bool enabled)653 bool FakeEventHub::setKernelWakeEnabled(int32_t deviceId, bool enabled) {
654 Device* device = getDevice(deviceId);
655 if (device == nullptr) {
656 return false;
657 }
658 mKernelWakeup.emplace(deviceId, enabled);
659 return true;
660 }
661
fakeReadKernelWakeup(int32_t deviceId) const662 bool FakeEventHub::fakeReadKernelWakeup(int32_t deviceId) const {
663 Device* device = getDevice(deviceId);
664 if (device == nullptr) {
665 return false;
666 }
667 auto it = mKernelWakeup.find(deviceId);
668 if (it == mKernelWakeup.end()) {
669 return false;
670 }
671 return it->second;
672 }
673
674 } // namespace android
675