blob: daa000f2ce36dc5b53d201b34b47fb6a152f95b3 [file] [log] [blame]
Harry Cuttsa5b71292022-11-28 12:56:17 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "FakeEventHub.h"
18
19#include <android-base/thread_annotations.h>
20#include <gtest/gtest.h>
21#include <linux/input-event-codes.h>
22
23#include "TestConstants.h"
24
25namespace android {
26
27const std::string FakeEventHub::BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
28
29FakeEventHub::~FakeEventHub() {
30 for (size_t i = 0; i < mDevices.size(); i++) {
31 delete mDevices.valueAt(i);
32 }
33}
34
35void FakeEventHub::addDevice(int32_t deviceId, const std::string& name,
36 ftl::Flags<InputDeviceClass> classes, int bus) {
37 Device* device = new Device(classes);
38 device->identifier.name = name;
39 device->identifier.bus = bus;
40 mDevices.add(deviceId, device);
41
42 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
43}
44
45void FakeEventHub::removeDevice(int32_t deviceId) {
46 delete mDevices.valueFor(deviceId);
47 mDevices.removeItem(deviceId);
48
49 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
50}
51
52bool FakeEventHub::isDeviceEnabled(int32_t deviceId) const {
53 Device* device = getDevice(deviceId);
54 if (device == nullptr) {
55 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
56 return false;
57 }
58 return device->enabled;
59}
60
61status_t FakeEventHub::enableDevice(int32_t deviceId) {
62 status_t result;
63 Device* device = getDevice(deviceId);
64 if (device == nullptr) {
65 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
66 return BAD_VALUE;
67 }
68 if (device->enabled) {
69 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
70 return OK;
71 }
72 result = device->enable();
73 return result;
74}
75
76status_t FakeEventHub::disableDevice(int32_t deviceId) {
77 Device* device = getDevice(deviceId);
78 if (device == nullptr) {
79 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
80 return BAD_VALUE;
81 }
82 if (!device->enabled) {
83 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
84 return OK;
85 }
86 return device->disable();
87}
88
89void FakeEventHub::finishDeviceScan() {
90 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
91}
92
93void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
94 getDevice(deviceId)->configuration.addProperty(key, value);
95}
96
97void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
98 getDevice(deviceId)->configuration.addAll(configuration);
99}
100
101void FakeEventHub::addAbsoluteAxis(int32_t deviceId, int axis, int32_t minValue, int32_t maxValue,
102 int flat, int fuzz, int resolution) {
103 Device* device = getDevice(deviceId);
104
105 RawAbsoluteAxisInfo info;
106 info.valid = true;
107 info.minValue = minValue;
108 info.maxValue = maxValue;
109 info.flat = flat;
110 info.fuzz = fuzz;
111 info.resolution = resolution;
112 device->absoluteAxes.add(axis, info);
113}
114
115void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) {
116 getDevice(deviceId)->relativeAxes.add(axis, true);
117}
118
119void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
120 getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state);
121}
122
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000123void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) {
124 getDevice(deviceId)->layoutInfo = info;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000125}
126
127void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
128 getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state);
129}
130
131void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
132 getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state);
133}
134
135void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
136 getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value);
137}
138
139void FakeEventHub::addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode,
140 uint32_t flags) {
141 Device* device = getDevice(deviceId);
142 KeyInfo info;
143 info.keyCode = keyCode;
144 info.flags = flags;
145 if (scanCode) {
146 device->keysByScanCode.add(scanCode, info);
147 }
148 if (usageCode) {
149 device->keysByUsageCode.add(usageCode, info);
150 }
151}
152
153void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
154 getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
155}
156
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000157void 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
Harry Cuttsa5b71292022-11-28 12:56:17 +0000162void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
163 getDevice(deviceId)->leds.add(led, initialState);
164}
165
166void 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
174void 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
180void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
181 mRawLightInfos.emplace(rawId, std::move(info));
182}
183
184void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
185 mLightBrightness.emplace(rawId, brightness);
186}
187
188void FakeEventHub::fakeLightIntensities(int32_t rawId,
189 const std::unordered_map<LightColor, int32_t> intensities) {
190 mLightIntensities.emplace(rawId, std::move(intensities));
191}
192
193bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
194 return getDevice(deviceId)->leds.valueFor(led);
195}
196
197std::vector<std::string>& FakeEventHub::getExcludedDevices() {
198 return mExcludedDevices;
199}
200
201void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
202 const VirtualKeyDefinition& definition) {
203 getDevice(deviceId)->virtualKeys.push_back(definition);
204}
205
206void 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
223void FakeEventHub::setVideoFrames(
224 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
225 mVideoFrames = std::move(videoFrames);
226}
227
228void 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
239FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
240 ssize_t index = mDevices.indexOfKey(deviceId);
241 return index >= 0 ? mDevices.valueAt(index) : nullptr;
242}
243
244ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
245 Device* device = getDevice(deviceId);
246 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
247}
248
249InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
250 Device* device = getDevice(deviceId);
251 return device ? device->identifier : InputDeviceIdentifier();
252}
253
254int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
255 return 0;
256}
257
Harry Cuttsc34f7582023-03-07 16:23:30 +0000258std::optional<PropertyMap> FakeEventHub::getConfiguration(int32_t deviceId) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000259 Device* device = getDevice(deviceId);
Harry Cuttsc34f7582023-03-07 16:23:30 +0000260 if (device == nullptr) {
261 return {};
Harry Cuttsa5b71292022-11-28 12:56:17 +0000262 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000263 return device->configuration;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000264}
265
266status_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
280bool 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
288bool FakeEventHub::hasInputProperty(int32_t, int) const {
289 return false;
290}
291
292bool 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
300status_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) {
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000308 auto it = device->keyRemapping.find(key->keyCode);
309 *outKeycode = it != device->keyRemapping.end() ? it->second : key->keyCode;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000310 }
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
323const 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
340status_t FakeEventHub::mapAxis(int32_t, int32_t, AxisInfo*) const {
341 return NAME_NOT_FOUND;
342}
343
344base::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
358void FakeEventHub::setExcludedDevices(const std::vector<std::string>& devices) {
359 mExcludedDevices = devices;
360}
361
362std::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
372std::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
382int32_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
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000393std::optional<RawLayoutInfo> FakeEventHub::getRawLayoutInfo(int32_t deviceId) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000394 Device* device = getDevice(deviceId);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000395 return device ? device->layoutInfo : std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000396}
397
398int32_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
409int32_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
420status_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
Arpit Singh4b4a4572023-11-24 18:19:56 +0000434void 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
443base::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
Harry Cuttsa5b71292022-11-28 12:56:17 +0000466int32_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.
476bool 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
497bool 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
506bool 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
524bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
525 Device* device = getDevice(deviceId);
526 return device && device->leds.indexOfKey(led) >= 0;
527}
528
529void 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
543void 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
553const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
554 return nullptr;
555}
556
557bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
558 return false;
559}
560
561std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
562 return mVibrators;
563}
564
565std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
566 return BATTERY_CAPACITY;
567}
568
569std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
570 return BATTERY_STATUS;
571}
572
573std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
574 return {DEFAULT_BATTERY};
575}
576
577std::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
587std::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
595std::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
603void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
604 mLightBrightness.emplace(lightId, brightness);
605}
606
607void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
608 std::unordered_map<LightColor, int32_t> intensities) {
609 mLightIntensities.emplace(lightId, intensities);
610};
611
612std::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
620std::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
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000629void 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
637void 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)) {
Vaibhav Devmurarifb219eb2023-05-02 13:20:37 +0000652 InputDeviceIdentifier identifier = foundDevice->identifier;
653 ftl::Flags<InputDeviceClass> classes = foundDevice->classes;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000654 removeDevice(foundDeviceId);
Vaibhav Devmurarifb219eb2023-05-02 13:20:37 +0000655 addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT,
656 identifier.bus);
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000657 }
658}
659
Harry Cuttsa5b71292022-11-28 12:56:17 +0000660} // namespace android