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 
getKeyCodeForKeyLocation(int32_t deviceId,int32_t locationKeyCode) const434 int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
435     Device* device = getDevice(deviceId);
436     if (!device) {
437         return AKEYCODE_UNKNOWN;
438     }
439     auto it = device->keyCodeMapping.find(locationKeyCode);
440     return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
441 }
442 
443 // Return true if the device has non-empty key layout.
markSupportedKeyCodes(int32_t deviceId,const std::vector<int32_t> & keyCodes,uint8_t * outFlags) const444 bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
445                                          uint8_t* outFlags) const {
446     Device* device = getDevice(deviceId);
447     if (!device) return false;
448 
449     bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
450     for (size_t i = 0; i < keyCodes.size(); i++) {
451         for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
452             if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
453                 outFlags[i] = 1;
454             }
455         }
456         for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
457             if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
458                 outFlags[i] = 1;
459             }
460         }
461     }
462     return result;
463 }
464 
hasScanCode(int32_t deviceId,int32_t scanCode) const465 bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
466     Device* device = getDevice(deviceId);
467     if (device) {
468         ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
469         return index >= 0;
470     }
471     return false;
472 }
473 
hasKeyCode(int32_t deviceId,int32_t keyCode) const474 bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
475     Device* device = getDevice(deviceId);
476     if (!device) {
477         return false;
478     }
479     for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
480         if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
481             return true;
482         }
483     }
484     for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
485         if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
486             return true;
487         }
488     }
489     return false;
490 }
491 
hasLed(int32_t deviceId,int32_t led) const492 bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
493     Device* device = getDevice(deviceId);
494     return device && device->leds.indexOfKey(led) >= 0;
495 }
496 
setLedState(int32_t deviceId,int32_t led,bool on)497 void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
498     Device* device = getDevice(deviceId);
499     if (device) {
500         ssize_t index = device->leds.indexOfKey(led);
501         if (index >= 0) {
502             device->leds.replaceValueAt(led, on);
503         } else {
504             ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
505                              "was not present.  led="
506                           << led;
507         }
508     }
509 }
510 
getVirtualKeyDefinitions(int32_t deviceId,std::vector<VirtualKeyDefinition> & outVirtualKeys) const511 void FakeEventHub::getVirtualKeyDefinitions(
512         int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
513     outVirtualKeys.clear();
514 
515     Device* device = getDevice(deviceId);
516     if (device) {
517         outVirtualKeys = device->virtualKeys;
518     }
519 }
520 
getKeyCharacterMap(int32_t) const521 const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
522     return nullptr;
523 }
524 
setKeyboardLayoutOverlay(int32_t,std::shared_ptr<KeyCharacterMap>)525 bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
526     return false;
527 }
528 
getVibratorIds(int32_t deviceId) const529 std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
530     return mVibrators;
531 }
532 
getBatteryCapacity(int32_t,int32_t) const533 std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
534     return BATTERY_CAPACITY;
535 }
536 
getBatteryStatus(int32_t,int32_t) const537 std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
538     return BATTERY_STATUS;
539 }
540 
getRawBatteryIds(int32_t deviceId) const541 std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
542     return {DEFAULT_BATTERY};
543 }
544 
getRawBatteryInfo(int32_t deviceId,int32_t batteryId) const545 std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
546                                                               int32_t batteryId) const {
547     if (batteryId != DEFAULT_BATTERY) return {};
548     static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
549                                                     .name = "default battery",
550                                                     .flags = InputBatteryClass::CAPACITY,
551                                                     .path = BATTERY_DEVPATH};
552     return BATTERY_INFO;
553 }
554 
getRawLightIds(int32_t deviceId) const555 std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
556     std::vector<int32_t> ids;
557     for (const auto& [rawId, info] : mRawLightInfos) {
558         ids.push_back(rawId);
559     }
560     return ids;
561 }
562 
getRawLightInfo(int32_t deviceId,int32_t lightId) const563 std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
564     auto it = mRawLightInfos.find(lightId);
565     if (it == mRawLightInfos.end()) {
566         return std::nullopt;
567     }
568     return it->second;
569 }
570 
setLightBrightness(int32_t deviceId,int32_t lightId,int32_t brightness)571 void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
572     mLightBrightness.emplace(lightId, brightness);
573 }
574 
setLightIntensities(int32_t deviceId,int32_t lightId,std::unordered_map<LightColor,int32_t> intensities)575 void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
576                                        std::unordered_map<LightColor, int32_t> intensities) {
577     mLightIntensities.emplace(lightId, intensities);
578 };
579 
getLightBrightness(int32_t deviceId,int32_t lightId) const580 std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
581     auto lightIt = mLightBrightness.find(lightId);
582     if (lightIt == mLightBrightness.end()) {
583         return std::nullopt;
584     }
585     return lightIt->second;
586 }
587 
getLightIntensities(int32_t deviceId,int32_t lightId) const588 std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
589         int32_t deviceId, int32_t lightId) const {
590     auto lightIt = mLightIntensities.find(lightId);
591     if (lightIt == mLightIntensities.end()) {
592         return std::nullopt;
593     }
594     return lightIt->second;
595 };
596 
setSysfsRootPath(int32_t deviceId,std::string sysfsRootPath) const597 void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const {
598     Device* device = getDevice(deviceId);
599     if (device == nullptr) {
600         return;
601     }
602     device->sysfsRootPath = sysfsRootPath;
603 }
604 
sysfsNodeChanged(const std::string & sysfsNodePath)605 void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
606     int32_t foundDeviceId = -1;
607     Device* foundDevice = nullptr;
608     for (size_t i = 0; i < mDevices.size(); i++) {
609         Device* d = mDevices.valueAt(i);
610         if (sysfsNodePath.find(d->sysfsRootPath) != std::string::npos) {
611             foundDeviceId = mDevices.keyAt(i);
612             foundDevice = d;
613         }
614     }
615     if (foundDevice == nullptr) {
616         return;
617     }
618     // If device sysfs changed -> reopen the device
619     if (!mRawLightInfos.empty() && !foundDevice->classes.test(InputDeviceClass::LIGHT)) {
620         InputDeviceIdentifier identifier = foundDevice->identifier;
621         ftl::Flags<InputDeviceClass> classes = foundDevice->classes;
622         removeDevice(foundDeviceId);
623         addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT,
624                   identifier.bus);
625     }
626 }
627 
628 } // namespace android
629