• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/thread_annotations.h>
20 #include <gtest/gtest.h>
21 #include <linux/input-event-codes.h>
22 
23 #include "TestConstants.h"
24 
25 namespace android {
26 
27 const std::string FakeEventHub::BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
28 
~FakeEventHub()29 FakeEventHub::~FakeEventHub() {
30     for (size_t i = 0; i < mDevices.size(); i++) {
31         delete mDevices.valueAt(i);
32     }
33 }
34 
addDevice(int32_t deviceId,const std::string & name,ftl::Flags<InputDeviceClass> classes,int bus)35 void 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 
removeDevice(int32_t deviceId)45 void 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 
isDeviceEnabled(int32_t deviceId) const52 bool 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 
enableDevice(int32_t deviceId)61 status_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 
disableDevice(int32_t deviceId)76 status_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 
finishDeviceScan()89 void FakeEventHub::finishDeviceScan() {
90     enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
91 }
92 
addConfigurationProperty(int32_t deviceId,const char * key,const char * value)93 void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
94     getDevice(deviceId)->configuration.addProperty(key, value);
95 }
96 
addConfigurationMap(int32_t deviceId,const PropertyMap * configuration)97 void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
98     getDevice(deviceId)->configuration.addAll(configuration);
99 }
100 
addAbsoluteAxis(int32_t deviceId,int axis,int32_t minValue,int32_t maxValue,int flat,int fuzz,int resolution)101 void 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 
addRelativeAxis(int32_t deviceId,int32_t axis)115 void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) {
116     getDevice(deviceId)->relativeAxes.add(axis, true);
117 }
118 
setKeyCodeState(int32_t deviceId,int32_t keyCode,int32_t state)119 void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
120     getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state);
121 }
122 
setRawLayoutInfo(int32_t deviceId,RawLayoutInfo info)123 void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) {
124     getDevice(deviceId)->layoutInfo = info;
125 }
126 
setScanCodeState(int32_t deviceId,int32_t scanCode,int32_t state)127 void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
128     getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state);
129 }
130 
setSwitchState(int32_t deviceId,int32_t switchCode,int32_t state)131 void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
132     getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state);
133 }
134 
setAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t value)135 void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
136     getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value);
137 }
138 
addKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t keyCode,uint32_t flags)139 void 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 
addKeyCodeMapping(int32_t deviceId,int32_t fromKeyCode,int32_t toKeyCode)153 void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
154     getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
155 }
156 
addKeyRemapping(int32_t deviceId,int32_t fromKeyCode,int32_t toKeyCode) const157 void 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 
addLed(int32_t deviceId,int32_t led,bool initialState)162 void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
163     getDevice(deviceId)->leds.add(led, initialState);
164 }
165 
addSensorAxis(int32_t deviceId,int32_t absCode,InputDeviceSensorType sensorType,int32_t sensorDataIndex)166 void 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 
setMscEvent(int32_t deviceId,int32_t mscEvent)174 void 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 
addRawLightInfo(int32_t rawId,RawLightInfo && info)180 void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
181     mRawLightInfos.emplace(rawId, std::move(info));
182 }
183 
fakeLightBrightness(int32_t rawId,int32_t brightness)184 void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
185     mLightBrightness.emplace(rawId, brightness);
186 }
187 
fakeLightIntensities(int32_t rawId,const std::unordered_map<LightColor,int32_t> intensities)188 void FakeEventHub::fakeLightIntensities(int32_t rawId,
189                                         const std::unordered_map<LightColor, int32_t> intensities) {
190     mLightIntensities.emplace(rawId, std::move(intensities));
191 }
192 
getLedState(int32_t deviceId,int32_t led)193 bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
194     return getDevice(deviceId)->leds.valueFor(led);
195 }
196 
getExcludedDevices()197 std::vector<std::string>& FakeEventHub::getExcludedDevices() {
198     return mExcludedDevices;
199 }
200 
addVirtualKeyDefinition(int32_t deviceId,const VirtualKeyDefinition & definition)201 void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
202                                            const VirtualKeyDefinition& definition) {
203     getDevice(deviceId)->virtualKeys.push_back(definition);
204 }
205 
enqueueEvent(nsecs_t when,nsecs_t readTime,int32_t deviceId,int32_t type,int32_t code,int32_t value)206 void 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 
setVideoFrames(std::unordered_map<int32_t,std::vector<TouchVideoFrame>> videoFrames)223 void FakeEventHub::setVideoFrames(
224         std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
225     mVideoFrames = std::move(videoFrames);
226 }
227 
assertQueueIsEmpty()228 void 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 
getDevice(int32_t deviceId) const239 FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
240     ssize_t index = mDevices.indexOfKey(deviceId);
241     return index >= 0 ? mDevices.valueAt(index) : nullptr;
242 }
243 
getDeviceClasses(int32_t deviceId) const244 ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
245     Device* device = getDevice(deviceId);
246     return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
247 }
248 
getDeviceIdentifier(int32_t deviceId) const249 InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
250     Device* device = getDevice(deviceId);
251     return device ? device->identifier : InputDeviceIdentifier();
252 }
253 
getDeviceControllerNumber(int32_t) const254 int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
255     return 0;
256 }
257 
getConfiguration(int32_t deviceId) const258 std::optional<PropertyMap> FakeEventHub::getConfiguration(int32_t deviceId) const {
259     Device* device = getDevice(deviceId);
260     if (device == nullptr) {
261         return {};
262     }
263     return device->configuration;
264 }
265 
getAbsoluteAxisInfo(int32_t deviceId,int axis,RawAbsoluteAxisInfo * outAxisInfo) const266 status_t FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId, int axis,
267                                            RawAbsoluteAxisInfo* outAxisInfo) const {
268     Device* device = getDevice(deviceId);
269     if (device) {
270         ssize_t index = device->absoluteAxes.indexOfKey(axis);
271         if (index >= 0) {
272             *outAxisInfo = device->absoluteAxes.valueAt(index);
273             return OK;
274         }
275     }
276     outAxisInfo->clear();
277     return -1;
278 }
279 
hasRelativeAxis(int32_t deviceId,int axis) const280 bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
281     Device* device = getDevice(deviceId);
282     if (device) {
283         return device->relativeAxes.indexOfKey(axis) >= 0;
284     }
285     return false;
286 }
287 
hasInputProperty(int32_t,int) const288 bool FakeEventHub::hasInputProperty(int32_t, int) const {
289     return false;
290 }
291 
hasMscEvent(int32_t deviceId,int mscEvent) const292 bool FakeEventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
293     Device* device = getDevice(deviceId);
294     if (device) {
295         return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
296     }
297     return false;
298 }
299 
mapKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t metaState,int32_t * outKeycode,int32_t * outMetaState,uint32_t * outFlags) const300 status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
301                               int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
302                               uint32_t* outFlags) const {
303     Device* device = getDevice(deviceId);
304     if (device) {
305         const KeyInfo* key = getKey(device, scanCode, usageCode);
306         if (key) {
307             if (outKeycode) {
308                 auto it = device->keyRemapping.find(key->keyCode);
309                 *outKeycode = it != device->keyRemapping.end() ? it->second : key->keyCode;
310             }
311             if (outFlags) {
312                 *outFlags = key->flags;
313             }
314             if (outMetaState) {
315                 *outMetaState = metaState;
316             }
317             return OK;
318         }
319     }
320     return NAME_NOT_FOUND;
321 }
322 
getKey(Device * device,int32_t scanCode,int32_t usageCode) const323 const FakeEventHub::KeyInfo* FakeEventHub::getKey(Device* device, int32_t scanCode,
324                                                   int32_t usageCode) const {
325     if (usageCode) {
326         ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
327         if (index >= 0) {
328             return &device->keysByUsageCode.valueAt(index);
329         }
330     }
331     if (scanCode) {
332         ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
333         if (index >= 0) {
334             return &device->keysByScanCode.valueAt(index);
335         }
336     }
337     return nullptr;
338 }
339 
mapAxis(int32_t,int32_t,AxisInfo *) const340 status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const {
341     return NAME_NOT_FOUND;
342 }
343 
mapSensor(int32_t deviceId,int32_t absCode) const344 base::Result<std::pair<InputDeviceSensorType, int32_t>> FakeEventHub::mapSensor(
345         int32_t deviceId, int32_t absCode) const {
346     Device* device = getDevice(deviceId);
347     if (!device) {
348         return Errorf("Sensor device not found.");
349     }
350     auto it = device->sensorsByAbsCode.find(absCode);
351     if (it == device->sensorsByAbsCode.end()) {
352         return Errorf("Sensor map not found.");
353     }
354     const SensorInfo& info = it->second;
355     return std::make_pair(info.sensorType, info.sensorDataIndex);
356 }
357 
setExcludedDevices(const std::vector<std::string> & devices)358 void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) {
359     mExcludedDevices = devices;
360 }
361 
getEvents(int)362 std::vector<RawEvent> FakeEventHub::getEvents(int) {
363     std::scoped_lock lock(mLock);
364 
365     std::vector<RawEvent> buffer;
366     std::swap(buffer, mEvents);
367 
368     mEventsCondition.notify_all();
369     return buffer;
370 }
371 
getVideoFrames(int32_t deviceId)372 std::vector<TouchVideoFrame> FakeEventHub::getVideoFrames(int32_t deviceId) {
373     auto it = mVideoFrames.find(deviceId);
374     if (it != mVideoFrames.end()) {
375         std::vector<TouchVideoFrame> frames = std::move(it->second);
376         mVideoFrames.erase(deviceId);
377         return frames;
378     }
379     return {};
380 }
381 
getScanCodeState(int32_t deviceId,int32_t scanCode) const382 int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
383     Device* device = getDevice(deviceId);
384     if (device) {
385         ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
386         if (index >= 0) {
387             return device->scanCodeStates.valueAt(index);
388         }
389     }
390     return AKEY_STATE_UNKNOWN;
391 }
392 
getRawLayoutInfo(int32_t deviceId) const393 std::optional<RawLayoutInfo> FakeEventHub::getRawLayoutInfo(int32_t deviceId) const {
394     Device* device = getDevice(deviceId);
395     return device ? device->layoutInfo : std::nullopt;
396 }
397 
getKeyCodeState(int32_t deviceId,int32_t keyCode) const398 int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
399     Device* device = getDevice(deviceId);
400     if (device) {
401         ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
402         if (index >= 0) {
403             return device->keyCodeStates.valueAt(index);
404         }
405     }
406     return AKEY_STATE_UNKNOWN;
407 }
408 
getSwitchState(int32_t deviceId,int32_t sw) const409 int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
410     Device* device = getDevice(deviceId);
411     if (device) {
412         ssize_t index = device->switchStates.indexOfKey(sw);
413         if (index >= 0) {
414             return device->switchStates.valueAt(index);
415         }
416     }
417     return AKEY_STATE_UNKNOWN;
418 }
419 
getAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t * outValue) const420 status_t FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
421                                             int32_t* outValue) const {
422     Device* device = getDevice(deviceId);
423     if (device) {
424         ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
425         if (index >= 0) {
426             *outValue = device->absoluteAxisValue.valueAt(index);
427             return OK;
428         }
429     }
430     *outValue = 0;
431     return -1;
432 }
433 
setMtSlotValues(int32_t deviceId,int32_t axis,const std::vector<int32_t> & values)434 void FakeEventHub::setMtSlotValues(int32_t deviceId, int32_t axis,
435                                    const std::vector<int32_t>& values) {
436     Device* device = getDevice(deviceId);
437     if (!device) {
438         FAIL() << "Missing device";
439     }
440     device->mtSlotValues[axis] = values;
441 }
442 
getMtSlotValues(int32_t deviceId,int32_t axis,size_t slotCount) const443 base::Result<std::vector<int32_t>> FakeEventHub::getMtSlotValues(int32_t deviceId, int32_t axis,
444                                                                  size_t slotCount) const {
445     Device* device = getDevice(deviceId);
446     if (!device) {
447         ADD_FAILURE() << "Missing device";
448         return base::ResultError("Missing device", UNKNOWN_ERROR);
449     }
450     const auto& mtSlotValuesIterator = device->mtSlotValues.find(axis);
451     if (mtSlotValuesIterator == device->mtSlotValues.end()) {
452         return base::ResultError("axis not supported", NAME_NOT_FOUND);
453     }
454     const auto& mtSlotValues = mtSlotValuesIterator->second;
455     if (mtSlotValues.size() != slotCount) {
456         ADD_FAILURE() << "MtSlot values specified for " << mtSlotValues.size()
457                       << " slots but expected for " << slotCount << " Slots";
458         return base::ResultError("Slot count mismatch", NAME_NOT_FOUND);
459     }
460     std::vector<int32_t> outValues(slotCount + 1);
461     outValues[0] = axis;
462     std::copy(mtSlotValues.begin(), mtSlotValues.end(), outValues.begin() + 1);
463     return std::move(outValues);
464 }
465 
getKeyCodeForKeyLocation(int32_t deviceId,int32_t locationKeyCode) const466 int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
467     Device* device = getDevice(deviceId);
468     if (!device) {
469         return AKEYCODE_UNKNOWN;
470     }
471     auto it = device->keyCodeMapping.find(locationKeyCode);
472     return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
473 }
474 
475 // Return true if the device has non-empty key layout.
markSupportedKeyCodes(int32_t deviceId,const std::vector<int32_t> & keyCodes,uint8_t * outFlags) const476 bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
477                                          uint8_t* outFlags) const {
478     Device* device = getDevice(deviceId);
479     if (!device) return false;
480 
481     bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
482     for (size_t i = 0; i < keyCodes.size(); i++) {
483         for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
484             if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
485                 outFlags[i] = 1;
486             }
487         }
488         for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
489             if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
490                 outFlags[i] = 1;
491             }
492         }
493     }
494     return result;
495 }
496 
hasScanCode(int32_t deviceId,int32_t scanCode) const497 bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
498     Device* device = getDevice(deviceId);
499     if (device) {
500         ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
501         return index >= 0;
502     }
503     return false;
504 }
505 
hasKeyCode(int32_t deviceId,int32_t keyCode) const506 bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
507     Device* device = getDevice(deviceId);
508     if (!device) {
509         return false;
510     }
511     for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
512         if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
513             return true;
514         }
515     }
516     for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
517         if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
518             return true;
519         }
520     }
521     return false;
522 }
523 
hasLed(int32_t deviceId,int32_t led) const524 bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
525     Device* device = getDevice(deviceId);
526     return device && device->leds.indexOfKey(led) >= 0;
527 }
528 
setLedState(int32_t deviceId,int32_t led,bool on)529 void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
530     Device* device = getDevice(deviceId);
531     if (device) {
532         ssize_t index = device->leds.indexOfKey(led);
533         if (index >= 0) {
534             device->leds.replaceValueAt(led, on);
535         } else {
536             ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
537                              "was not present.  led="
538                           << led;
539         }
540     }
541 }
542 
getVirtualKeyDefinitions(int32_t deviceId,std::vector<VirtualKeyDefinition> & outVirtualKeys) const543 void FakeEventHub::getVirtualKeyDefinitions(
544         int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
545     outVirtualKeys.clear();
546 
547     Device* device = getDevice(deviceId);
548     if (device) {
549         outVirtualKeys = device->virtualKeys;
550     }
551 }
552 
getKeyCharacterMap(int32_t) const553 const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
554     return nullptr;
555 }
556 
setKeyboardLayoutOverlay(int32_t,std::shared_ptr<KeyCharacterMap>)557 bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
558     return false;
559 }
560 
getVibratorIds(int32_t deviceId) const561 std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
562     return mVibrators;
563 }
564 
getBatteryCapacity(int32_t,int32_t) const565 std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
566     return BATTERY_CAPACITY;
567 }
568 
getBatteryStatus(int32_t,int32_t) const569 std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
570     return BATTERY_STATUS;
571 }
572 
getRawBatteryIds(int32_t deviceId) const573 std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
574     return {DEFAULT_BATTERY};
575 }
576 
getRawBatteryInfo(int32_t deviceId,int32_t batteryId) const577 std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
578                                                               int32_t batteryId) const {
579     if (batteryId != DEFAULT_BATTERY) return {};
580     static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
581                                                     .name = "default battery",
582                                                     .flags = InputBatteryClass::CAPACITY,
583                                                     .path = BATTERY_DEVPATH};
584     return BATTERY_INFO;
585 }
586 
getRawLightIds(int32_t deviceId) const587 std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
588     std::vector<int32_t> ids;
589     for (const auto& [rawId, info] : mRawLightInfos) {
590         ids.push_back(rawId);
591     }
592     return ids;
593 }
594 
getRawLightInfo(int32_t deviceId,int32_t lightId) const595 std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
596     auto it = mRawLightInfos.find(lightId);
597     if (it == mRawLightInfos.end()) {
598         return std::nullopt;
599     }
600     return it->second;
601 }
602 
setLightBrightness(int32_t deviceId,int32_t lightId,int32_t brightness)603 void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
604     mLightBrightness.emplace(lightId, brightness);
605 }
606 
setLightIntensities(int32_t deviceId,int32_t lightId,std::unordered_map<LightColor,int32_t> intensities)607 void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
608                                        std::unordered_map<LightColor, int32_t> intensities) {
609     mLightIntensities.emplace(lightId, intensities);
610 };
611 
getLightBrightness(int32_t deviceId,int32_t lightId) const612 std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
613     auto lightIt = mLightBrightness.find(lightId);
614     if (lightIt == mLightBrightness.end()) {
615         return std::nullopt;
616     }
617     return lightIt->second;
618 }
619 
getLightIntensities(int32_t deviceId,int32_t lightId) const620 std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
621         int32_t deviceId, int32_t lightId) const {
622     auto lightIt = mLightIntensities.find(lightId);
623     if (lightIt == mLightIntensities.end()) {
624         return std::nullopt;
625     }
626     return lightIt->second;
627 };
628 
setSysfsRootPath(int32_t deviceId,std::string sysfsRootPath) const629 void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const {
630     Device* device = getDevice(deviceId);
631     if (device == nullptr) {
632         return;
633     }
634     device->sysfsRootPath = sysfsRootPath;
635 }
636 
sysfsNodeChanged(const std::string & sysfsNodePath)637 void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
638     int32_t foundDeviceId = -1;
639     Device* foundDevice = nullptr;
640     for (size_t i = 0; i < mDevices.size(); i++) {
641         Device* d = mDevices.valueAt(i);
642         if (sysfsNodePath.find(d->sysfsRootPath) != std::string::npos) {
643             foundDeviceId = mDevices.keyAt(i);
644             foundDevice = d;
645         }
646     }
647     if (foundDevice == nullptr) {
648         return;
649     }
650     // If device sysfs changed -> reopen the device
651     if (!mRawLightInfos.empty() && !foundDevice->classes.test(InputDeviceClass::LIGHT)) {
652         InputDeviceIdentifier identifier = foundDevice->identifier;
653         ftl::Flags<InputDeviceClass> classes = foundDevice->classes;
654         removeDevice(foundDeviceId);
655         addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT,
656                   identifier.bus);
657     }
658 }
659 
660 } // namespace android
661