blob: 31fbf209a3335fb40f786389ef9645536106f534 [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
Harry Cutts207674d2024-06-06 18:53:41 +000019#include <optional>
20
Harry Cuttsa5b71292022-11-28 12:56:17 +000021#include <android-base/thread_annotations.h>
22#include <gtest/gtest.h>
23#include <linux/input-event-codes.h>
24
25#include "TestConstants.h"
26
27namespace android {
28
29const std::string FakeEventHub::BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
30
31FakeEventHub::~FakeEventHub() {
32 for (size_t i = 0; i < mDevices.size(); i++) {
33 delete mDevices.valueAt(i);
34 }
35}
36
37void 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
47void 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
54bool 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
63status_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
78status_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
Harry Cuttsa5b71292022-11-28 12:56:17 +000091void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
92 getDevice(deviceId)->configuration.addProperty(key, value);
93}
94
95void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
96 getDevice(deviceId)->configuration.addAll(configuration);
97}
98
99void 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;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000104 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
112void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) {
113 getDevice(deviceId)->relativeAxes.add(axis, true);
114}
115
116void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
117 getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state);
118}
119
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000120void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) {
121 getDevice(deviceId)->layoutInfo = info;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000122}
123
124void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
125 getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state);
126}
127
128void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
129 getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state);
130}
131
132void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
133 getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value);
134}
135
136void 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
150void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
151 getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
152}
153
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000154void FakeEventHub::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
155 Device* device = getDevice(deviceId);
156 device->keyRemapping.insert_or_assign(fromKeyCode, toKeyCode);
157}
158
Harry Cuttsa5b71292022-11-28 12:56:17 +0000159void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
160 getDevice(deviceId)->leds.add(led, initialState);
161}
162
163void FakeEventHub::addSensorAxis(int32_t deviceId, int32_t absCode,
164 InputDeviceSensorType sensorType, int32_t sensorDataIndex) {
165 SensorInfo info;
166 info.sensorType = sensorType;
167 info.sensorDataIndex = sensorDataIndex;
168 getDevice(deviceId)->sensorsByAbsCode.emplace(absCode, info);
169}
170
171void FakeEventHub::setMscEvent(int32_t deviceId, int32_t mscEvent) {
172 typename BitArray<MSC_MAX>::Buffer buffer;
173 buffer[mscEvent / 32] = 1 << mscEvent % 32;
174 getDevice(deviceId)->mscBitmask.loadFromBuffer(buffer);
175}
176
177void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
178 mRawLightInfos.emplace(rawId, std::move(info));
179}
180
181void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
182 mLightBrightness.emplace(rawId, brightness);
183}
184
185void FakeEventHub::fakeLightIntensities(int32_t rawId,
186 const std::unordered_map<LightColor, int32_t> intensities) {
187 mLightIntensities.emplace(rawId, std::move(intensities));
188}
189
190bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
191 return getDevice(deviceId)->leds.valueFor(led);
192}
193
194std::vector<std::string>& FakeEventHub::getExcludedDevices() {
195 return mExcludedDevices;
196}
197
198void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
199 const VirtualKeyDefinition& definition) {
200 getDevice(deviceId)->virtualKeys.push_back(definition);
201}
202
203void FakeEventHub::enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type,
204 int32_t code, int32_t value) {
205 std::scoped_lock<std::mutex> lock(mLock);
206 RawEvent event;
207 event.when = when;
208 event.readTime = readTime;
209 event.deviceId = deviceId;
210 event.type = type;
211 event.code = code;
212 event.value = value;
213 mEvents.push_back(event);
214
215 if (type == EV_ABS) {
216 setAbsoluteAxisValue(deviceId, code, value);
217 }
218}
219
220void FakeEventHub::setVideoFrames(
221 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
222 mVideoFrames = std::move(videoFrames);
223}
224
225void FakeEventHub::assertQueueIsEmpty() {
226 std::unique_lock<std::mutex> lock(mLock);
227 base::ScopedLockAssertion assumeLocked(mLock);
228 const bool queueIsEmpty =
229 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
230 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
231 if (!queueIsEmpty) {
232 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
233 }
234}
235
236FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
237 ssize_t index = mDevices.indexOfKey(deviceId);
238 return index >= 0 ? mDevices.valueAt(index) : nullptr;
239}
240
241ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
242 Device* device = getDevice(deviceId);
243 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
244}
245
246InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
247 Device* device = getDevice(deviceId);
248 return device ? device->identifier : InputDeviceIdentifier();
249}
250
251int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
252 return 0;
253}
254
Harry Cuttsc34f7582023-03-07 16:23:30 +0000255std::optional<PropertyMap> FakeEventHub::getConfiguration(int32_t deviceId) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000256 Device* device = getDevice(deviceId);
Harry Cuttsc34f7582023-03-07 16:23:30 +0000257 if (device == nullptr) {
258 return {};
Harry Cuttsa5b71292022-11-28 12:56:17 +0000259 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000260 return device->configuration;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000261}
262
Harry Cutts207674d2024-06-06 18:53:41 +0000263std::optional<RawAbsoluteAxisInfo> FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId,
264 int axis) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000265 Device* device = getDevice(deviceId);
266 if (device) {
267 ssize_t index = device->absoluteAxes.indexOfKey(axis);
268 if (index >= 0) {
Harry Cutts207674d2024-06-06 18:53:41 +0000269 return device->absoluteAxes.valueAt(index);
Harry Cuttsa5b71292022-11-28 12:56:17 +0000270 }
271 }
Harry Cutts207674d2024-06-06 18:53:41 +0000272 return std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000273}
274
275bool FakeEventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
276 Device* device = getDevice(deviceId);
277 if (device) {
278 return device->relativeAxes.indexOfKey(axis) >= 0;
279 }
280 return false;
281}
282
283bool FakeEventHub::hasInputProperty(int32_t, int) const {
284 return false;
285}
286
287bool FakeEventHub::hasMscEvent(int32_t deviceId, int mscEvent) const {
288 Device* device = getDevice(deviceId);
289 if (device) {
290 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
291 }
292 return false;
293}
294
295status_t FakeEventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
296 int32_t metaState, int32_t* outKeycode, int32_t* outMetaState,
297 uint32_t* outFlags) const {
298 Device* device = getDevice(deviceId);
299 if (device) {
300 const KeyInfo* key = getKey(device, scanCode, usageCode);
301 if (key) {
302 if (outKeycode) {
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000303 auto it = device->keyRemapping.find(key->keyCode);
304 *outKeycode = it != device->keyRemapping.end() ? it->second : key->keyCode;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000305 }
306 if (outFlags) {
307 *outFlags = key->flags;
308 }
309 if (outMetaState) {
310 *outMetaState = metaState;
311 }
312 return OK;
313 }
314 }
315 return NAME_NOT_FOUND;
316}
317
318const FakeEventHub::KeyInfo* FakeEventHub::getKey(Device* device, int32_t scanCode,
319 int32_t usageCode) const {
320 if (usageCode) {
321 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
322 if (index >= 0) {
323 return &device->keysByUsageCode.valueAt(index);
324 }
325 }
326 if (scanCode) {
327 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
328 if (index >= 0) {
329 return &device->keysByScanCode.valueAt(index);
330 }
331 }
332 return nullptr;
333}
334
335status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const {
336 return NAME_NOT_FOUND;
337}
338
339base::Result<std::pair<InputDeviceSensorType, int32_t>> FakeEventHub::mapSensor(
340 int32_t deviceId, int32_t absCode) const {
341 Device* device = getDevice(deviceId);
342 if (!device) {
343 return Errorf("Sensor device not found.");
344 }
345 auto it = device->sensorsByAbsCode.find(absCode);
346 if (it == device->sensorsByAbsCode.end()) {
347 return Errorf("Sensor map not found.");
348 }
349 const SensorInfo& info = it->second;
350 return std::make_pair(info.sensorType, info.sensorDataIndex);
351}
352
353void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) {
354 mExcludedDevices = devices;
355}
356
357std::vector<RawEvent> FakeEventHub::getEvents(int) {
358 std::scoped_lock lock(mLock);
359
360 std::vector<RawEvent> buffer;
361 std::swap(buffer, mEvents);
362
363 mEventsCondition.notify_all();
364 return buffer;
365}
366
367std::vector<TouchVideoFrame> FakeEventHub::getVideoFrames(int32_t deviceId) {
368 auto it = mVideoFrames.find(deviceId);
369 if (it != mVideoFrames.end()) {
370 std::vector<TouchVideoFrame> frames = std::move(it->second);
371 mVideoFrames.erase(deviceId);
372 return frames;
373 }
374 return {};
375}
376
377int32_t FakeEventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
378 Device* device = getDevice(deviceId);
379 if (device) {
380 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
381 if (index >= 0) {
382 return device->scanCodeStates.valueAt(index);
383 }
384 }
385 return AKEY_STATE_UNKNOWN;
386}
387
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000388std::optional<RawLayoutInfo> FakeEventHub::getRawLayoutInfo(int32_t deviceId) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000389 Device* device = getDevice(deviceId);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000390 return device ? device->layoutInfo : std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000391}
392
393int32_t FakeEventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
394 Device* device = getDevice(deviceId);
395 if (device) {
396 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
397 if (index >= 0) {
398 return device->keyCodeStates.valueAt(index);
399 }
400 }
401 return AKEY_STATE_UNKNOWN;
402}
403
404int32_t FakeEventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
405 Device* device = getDevice(deviceId);
406 if (device) {
407 ssize_t index = device->switchStates.indexOfKey(sw);
408 if (index >= 0) {
409 return device->switchStates.valueAt(index);
410 }
411 }
412 return AKEY_STATE_UNKNOWN;
413}
414
Harry Cuttse2c5e202024-06-21 17:08:48 +0000415std::optional<int32_t> FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000416 Device* device = getDevice(deviceId);
417 if (device) {
418 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
419 if (index >= 0) {
Harry Cuttse2c5e202024-06-21 17:08:48 +0000420 return device->absoluteAxisValue.valueAt(index);
Harry Cuttsa5b71292022-11-28 12:56:17 +0000421 }
422 }
Harry Cuttse2c5e202024-06-21 17:08:48 +0000423 return std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000424}
425
Arpit Singh4b4a4572023-11-24 18:19:56 +0000426void FakeEventHub::setMtSlotValues(int32_t deviceId, int32_t axis,
427 const std::vector<int32_t>& values) {
428 Device* device = getDevice(deviceId);
429 if (!device) {
430 FAIL() << "Missing device";
431 }
432 device->mtSlotValues[axis] = values;
433}
434
435base::Result<std::vector<int32_t>> FakeEventHub::getMtSlotValues(int32_t deviceId, int32_t axis,
436 size_t slotCount) const {
437 Device* device = getDevice(deviceId);
438 if (!device) {
439 ADD_FAILURE() << "Missing device";
440 return base::ResultError("Missing device", UNKNOWN_ERROR);
441 }
442 const auto& mtSlotValuesIterator = device->mtSlotValues.find(axis);
443 if (mtSlotValuesIterator == device->mtSlotValues.end()) {
444 return base::ResultError("axis not supported", NAME_NOT_FOUND);
445 }
446 const auto& mtSlotValues = mtSlotValuesIterator->second;
447 if (mtSlotValues.size() != slotCount) {
448 ADD_FAILURE() << "MtSlot values specified for " << mtSlotValues.size()
449 << " slots but expected for " << slotCount << " Slots";
450 return base::ResultError("Slot count mismatch", NAME_NOT_FOUND);
451 }
452 std::vector<int32_t> outValues(slotCount + 1);
453 outValues[0] = axis;
454 std::copy(mtSlotValues.begin(), mtSlotValues.end(), outValues.begin() + 1);
455 return std::move(outValues);
456}
457
Harry Cuttsa5b71292022-11-28 12:56:17 +0000458int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
459 Device* device = getDevice(deviceId);
460 if (!device) {
461 return AKEYCODE_UNKNOWN;
462 }
463 auto it = device->keyCodeMapping.find(locationKeyCode);
464 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
465}
466
467// Return true if the device has non-empty key layout.
468bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
469 uint8_t* outFlags) const {
470 Device* device = getDevice(deviceId);
471 if (!device) return false;
472
473 bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
474 for (size_t i = 0; i < keyCodes.size(); i++) {
475 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
476 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
477 outFlags[i] = 1;
478 }
479 }
480 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
481 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
482 outFlags[i] = 1;
483 }
484 }
485 }
486 return result;
487}
488
489bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
490 Device* device = getDevice(deviceId);
491 if (device) {
492 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
493 return index >= 0;
494 }
495 return false;
496}
497
498bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
499 Device* device = getDevice(deviceId);
500 if (!device) {
501 return false;
502 }
503 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
504 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
505 return true;
506 }
507 }
508 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
509 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
510 return true;
511 }
512 }
513 return false;
514}
515
516bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
517 Device* device = getDevice(deviceId);
518 return device && device->leds.indexOfKey(led) >= 0;
519}
520
521void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
522 Device* device = getDevice(deviceId);
523 if (device) {
524 ssize_t index = device->leds.indexOfKey(led);
525 if (index >= 0) {
526 device->leds.replaceValueAt(led, on);
527 } else {
528 ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
529 "was not present. led="
530 << led;
531 }
532 }
533}
534
535void FakeEventHub::getVirtualKeyDefinitions(
536 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
537 outVirtualKeys.clear();
538
539 Device* device = getDevice(deviceId);
540 if (device) {
541 outVirtualKeys = device->virtualKeys;
542 }
543}
544
545const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
546 return nullptr;
547}
548
549bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
550 return false;
551}
552
553std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
554 return mVibrators;
555}
556
557std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
558 return BATTERY_CAPACITY;
559}
560
561std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
562 return BATTERY_STATUS;
563}
564
565std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
566 return {DEFAULT_BATTERY};
567}
568
569std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
570 int32_t batteryId) const {
571 if (batteryId != DEFAULT_BATTERY) return {};
572 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
573 .name = "default battery",
574 .flags = InputBatteryClass::CAPACITY,
575 .path = BATTERY_DEVPATH};
576 return BATTERY_INFO;
577}
578
579std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
580 std::vector<int32_t> ids;
581 for (const auto& [rawId, info] : mRawLightInfos) {
582 ids.push_back(rawId);
583 }
584 return ids;
585}
586
587std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
588 auto it = mRawLightInfos.find(lightId);
589 if (it == mRawLightInfos.end()) {
590 return std::nullopt;
591 }
592 return it->second;
593}
594
595void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
596 mLightBrightness.emplace(lightId, brightness);
597}
598
599void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
600 std::unordered_map<LightColor, int32_t> intensities) {
601 mLightIntensities.emplace(lightId, intensities);
602};
603
604std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
605 auto lightIt = mLightBrightness.find(lightId);
606 if (lightIt == mLightBrightness.end()) {
607 return std::nullopt;
608 }
609 return lightIt->second;
610}
611
612std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
613 int32_t deviceId, int32_t lightId) const {
614 auto lightIt = mLightIntensities.find(lightId);
615 if (lightIt == mLightIntensities.end()) {
616 return std::nullopt;
617 }
618 return lightIt->second;
619};
620
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000621void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const {
622 Device* device = getDevice(deviceId);
623 if (device == nullptr) {
624 return;
625 }
626 device->sysfsRootPath = sysfsRootPath;
627}
628
629void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
630 int32_t foundDeviceId = -1;
631 Device* foundDevice = nullptr;
632 for (size_t i = 0; i < mDevices.size(); i++) {
633 Device* d = mDevices.valueAt(i);
634 if (sysfsNodePath.find(d->sysfsRootPath) != std::string::npos) {
635 foundDeviceId = mDevices.keyAt(i);
636 foundDevice = d;
637 }
638 }
639 if (foundDevice == nullptr) {
640 return;
641 }
642 // If device sysfs changed -> reopen the device
643 if (!mRawLightInfos.empty() && !foundDevice->classes.test(InputDeviceClass::LIGHT)) {
Vaibhav Devmurarifb219eb2023-05-02 13:20:37 +0000644 InputDeviceIdentifier identifier = foundDevice->identifier;
645 ftl::Flags<InputDeviceClass> classes = foundDevice->classes;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000646 removeDevice(foundDeviceId);
Vaibhav Devmurarifb219eb2023-05-02 13:20:37 +0000647 addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT,
648 identifier.bus);
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000649 }
650}
651
Harry Cuttsa5b71292022-11-28 12:56:17 +0000652} // namespace android