blob: 12736c8cc03cec3b735e8888b893544c3e48bbd9 [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
91void FakeEventHub::finishDeviceScan() {
92 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
93}
94
95void FakeEventHub::addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
96 getDevice(deviceId)->configuration.addProperty(key, value);
97}
98
99void FakeEventHub::addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
100 getDevice(deviceId)->configuration.addAll(configuration);
101}
102
103void FakeEventHub::addAbsoluteAxis(int32_t deviceId, int axis, int32_t minValue, int32_t maxValue,
104 int flat, int fuzz, int resolution) {
105 Device* device = getDevice(deviceId);
106
107 RawAbsoluteAxisInfo info;
108 info.valid = true;
109 info.minValue = minValue;
110 info.maxValue = maxValue;
111 info.flat = flat;
112 info.fuzz = fuzz;
113 info.resolution = resolution;
114 device->absoluteAxes.add(axis, info);
115}
116
117void FakeEventHub::addRelativeAxis(int32_t deviceId, int32_t axis) {
118 getDevice(deviceId)->relativeAxes.add(axis, true);
119}
120
121void FakeEventHub::setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
122 getDevice(deviceId)->keyCodeStates.replaceValueFor(keyCode, state);
123}
124
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000125void FakeEventHub::setRawLayoutInfo(int32_t deviceId, RawLayoutInfo info) {
126 getDevice(deviceId)->layoutInfo = info;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000127}
128
129void FakeEventHub::setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
130 getDevice(deviceId)->scanCodeStates.replaceValueFor(scanCode, state);
131}
132
133void FakeEventHub::setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
134 getDevice(deviceId)->switchStates.replaceValueFor(switchCode, state);
135}
136
137void FakeEventHub::setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
138 getDevice(deviceId)->absoluteAxisValue.replaceValueFor(axis, value);
139}
140
141void FakeEventHub::addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t keyCode,
142 uint32_t flags) {
143 Device* device = getDevice(deviceId);
144 KeyInfo info;
145 info.keyCode = keyCode;
146 info.flags = flags;
147 if (scanCode) {
148 device->keysByScanCode.add(scanCode, info);
149 }
150 if (usageCode) {
151 device->keysByUsageCode.add(usageCode, info);
152 }
153}
154
155void FakeEventHub::addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
156 getDevice(deviceId)->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
157}
158
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000159void FakeEventHub::addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
160 Device* device = getDevice(deviceId);
161 device->keyRemapping.insert_or_assign(fromKeyCode, toKeyCode);
162}
163
Harry Cuttsa5b71292022-11-28 12:56:17 +0000164void FakeEventHub::addLed(int32_t deviceId, int32_t led, bool initialState) {
165 getDevice(deviceId)->leds.add(led, initialState);
166}
167
168void FakeEventHub::addSensorAxis(int32_t deviceId, int32_t absCode,
169 InputDeviceSensorType sensorType, int32_t sensorDataIndex) {
170 SensorInfo info;
171 info.sensorType = sensorType;
172 info.sensorDataIndex = sensorDataIndex;
173 getDevice(deviceId)->sensorsByAbsCode.emplace(absCode, info);
174}
175
176void FakeEventHub::setMscEvent(int32_t deviceId, int32_t mscEvent) {
177 typename BitArray<MSC_MAX>::Buffer buffer;
178 buffer[mscEvent / 32] = 1 << mscEvent % 32;
179 getDevice(deviceId)->mscBitmask.loadFromBuffer(buffer);
180}
181
182void FakeEventHub::addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
183 mRawLightInfos.emplace(rawId, std::move(info));
184}
185
186void FakeEventHub::fakeLightBrightness(int32_t rawId, int32_t brightness) {
187 mLightBrightness.emplace(rawId, brightness);
188}
189
190void FakeEventHub::fakeLightIntensities(int32_t rawId,
191 const std::unordered_map<LightColor, int32_t> intensities) {
192 mLightIntensities.emplace(rawId, std::move(intensities));
193}
194
195bool FakeEventHub::getLedState(int32_t deviceId, int32_t led) {
196 return getDevice(deviceId)->leds.valueFor(led);
197}
198
199std::vector<std::string>& FakeEventHub::getExcludedDevices() {
200 return mExcludedDevices;
201}
202
203void FakeEventHub::addVirtualKeyDefinition(int32_t deviceId,
204 const VirtualKeyDefinition& definition) {
205 getDevice(deviceId)->virtualKeys.push_back(definition);
206}
207
208void FakeEventHub::enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type,
209 int32_t code, int32_t value) {
210 std::scoped_lock<std::mutex> lock(mLock);
211 RawEvent event;
212 event.when = when;
213 event.readTime = readTime;
214 event.deviceId = deviceId;
215 event.type = type;
216 event.code = code;
217 event.value = value;
218 mEvents.push_back(event);
219
220 if (type == EV_ABS) {
221 setAbsoluteAxisValue(deviceId, code, value);
222 }
223}
224
225void FakeEventHub::setVideoFrames(
226 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> videoFrames) {
227 mVideoFrames = std::move(videoFrames);
228}
229
230void FakeEventHub::assertQueueIsEmpty() {
231 std::unique_lock<std::mutex> lock(mLock);
232 base::ScopedLockAssertion assumeLocked(mLock);
233 const bool queueIsEmpty =
234 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
235 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
236 if (!queueIsEmpty) {
237 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
238 }
239}
240
241FakeEventHub::Device* FakeEventHub::getDevice(int32_t deviceId) const {
242 ssize_t index = mDevices.indexOfKey(deviceId);
243 return index >= 0 ? mDevices.valueAt(index) : nullptr;
244}
245
246ftl::Flags<InputDeviceClass> FakeEventHub::getDeviceClasses(int32_t deviceId) const {
247 Device* device = getDevice(deviceId);
248 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
249}
250
251InputDeviceIdentifier FakeEventHub::getDeviceIdentifier(int32_t deviceId) const {
252 Device* device = getDevice(deviceId);
253 return device ? device->identifier : InputDeviceIdentifier();
254}
255
256int32_t FakeEventHub::getDeviceControllerNumber(int32_t) const {
257 return 0;
258}
259
Harry Cuttsc34f7582023-03-07 16:23:30 +0000260std::optional<PropertyMap> FakeEventHub::getConfiguration(int32_t deviceId) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000261 Device* device = getDevice(deviceId);
Harry Cuttsc34f7582023-03-07 16:23:30 +0000262 if (device == nullptr) {
263 return {};
Harry Cuttsa5b71292022-11-28 12:56:17 +0000264 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000265 return device->configuration;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000266}
267
Harry Cutts207674d2024-06-06 18:53:41 +0000268std::optional<RawAbsoluteAxisInfo> FakeEventHub::getAbsoluteAxisInfo(int32_t deviceId,
269 int axis) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000270 Device* device = getDevice(deviceId);
271 if (device) {
272 ssize_t index = device->absoluteAxes.indexOfKey(axis);
273 if (index >= 0) {
Harry Cutts207674d2024-06-06 18:53:41 +0000274 return device->absoluteAxes.valueAt(index);
Harry Cuttsa5b71292022-11-28 12:56:17 +0000275 }
276 }
Harry Cutts207674d2024-06-06 18:53:41 +0000277 return std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000278}
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
Harry Cuttse2c5e202024-06-21 17:08:48 +0000420std::optional<int32_t> FakeEventHub::getAbsoluteAxisValue(int32_t deviceId, int32_t axis) const {
Harry Cuttsa5b71292022-11-28 12:56:17 +0000421 Device* device = getDevice(deviceId);
422 if (device) {
423 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
424 if (index >= 0) {
Harry Cuttse2c5e202024-06-21 17:08:48 +0000425 return device->absoluteAxisValue.valueAt(index);
Harry Cuttsa5b71292022-11-28 12:56:17 +0000426 }
427 }
Harry Cuttse2c5e202024-06-21 17:08:48 +0000428 return std::nullopt;
Harry Cuttsa5b71292022-11-28 12:56:17 +0000429}
430
Arpit Singh4b4a4572023-11-24 18:19:56 +0000431void FakeEventHub::setMtSlotValues(int32_t deviceId, int32_t axis,
432 const std::vector<int32_t>& values) {
433 Device* device = getDevice(deviceId);
434 if (!device) {
435 FAIL() << "Missing device";
436 }
437 device->mtSlotValues[axis] = values;
438}
439
440base::Result<std::vector<int32_t>> FakeEventHub::getMtSlotValues(int32_t deviceId, int32_t axis,
441 size_t slotCount) const {
442 Device* device = getDevice(deviceId);
443 if (!device) {
444 ADD_FAILURE() << "Missing device";
445 return base::ResultError("Missing device", UNKNOWN_ERROR);
446 }
447 const auto& mtSlotValuesIterator = device->mtSlotValues.find(axis);
448 if (mtSlotValuesIterator == device->mtSlotValues.end()) {
449 return base::ResultError("axis not supported", NAME_NOT_FOUND);
450 }
451 const auto& mtSlotValues = mtSlotValuesIterator->second;
452 if (mtSlotValues.size() != slotCount) {
453 ADD_FAILURE() << "MtSlot values specified for " << mtSlotValues.size()
454 << " slots but expected for " << slotCount << " Slots";
455 return base::ResultError("Slot count mismatch", NAME_NOT_FOUND);
456 }
457 std::vector<int32_t> outValues(slotCount + 1);
458 outValues[0] = axis;
459 std::copy(mtSlotValues.begin(), mtSlotValues.end(), outValues.begin() + 1);
460 return std::move(outValues);
461}
462
Harry Cuttsa5b71292022-11-28 12:56:17 +0000463int32_t FakeEventHub::getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
464 Device* device = getDevice(deviceId);
465 if (!device) {
466 return AKEYCODE_UNKNOWN;
467 }
468 auto it = device->keyCodeMapping.find(locationKeyCode);
469 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
470}
471
472// Return true if the device has non-empty key layout.
473bool FakeEventHub::markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
474 uint8_t* outFlags) const {
475 Device* device = getDevice(deviceId);
476 if (!device) return false;
477
478 bool result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
479 for (size_t i = 0; i < keyCodes.size(); i++) {
480 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
481 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
482 outFlags[i] = 1;
483 }
484 }
485 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
486 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
487 outFlags[i] = 1;
488 }
489 }
490 }
491 return result;
492}
493
494bool FakeEventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
495 Device* device = getDevice(deviceId);
496 if (device) {
497 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
498 return index >= 0;
499 }
500 return false;
501}
502
503bool FakeEventHub::hasKeyCode(int32_t deviceId, int32_t keyCode) const {
504 Device* device = getDevice(deviceId);
505 if (!device) {
506 return false;
507 }
508 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
509 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
510 return true;
511 }
512 }
513 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
514 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
515 return true;
516 }
517 }
518 return false;
519}
520
521bool FakeEventHub::hasLed(int32_t deviceId, int32_t led) const {
522 Device* device = getDevice(deviceId);
523 return device && device->leds.indexOfKey(led) >= 0;
524}
525
526void FakeEventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
527 Device* device = getDevice(deviceId);
528 if (device) {
529 ssize_t index = device->leds.indexOfKey(led);
530 if (index >= 0) {
531 device->leds.replaceValueAt(led, on);
532 } else {
533 ADD_FAILURE() << "Attempted to set the state of an LED that the EventHub declared "
534 "was not present. led="
535 << led;
536 }
537 }
538}
539
540void FakeEventHub::getVirtualKeyDefinitions(
541 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
542 outVirtualKeys.clear();
543
544 Device* device = getDevice(deviceId);
545 if (device) {
546 outVirtualKeys = device->virtualKeys;
547 }
548}
549
550const std::shared_ptr<KeyCharacterMap> FakeEventHub::getKeyCharacterMap(int32_t) const {
551 return nullptr;
552}
553
554bool FakeEventHub::setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) {
555 return false;
556}
557
558std::vector<int32_t> FakeEventHub::getVibratorIds(int32_t deviceId) const {
559 return mVibrators;
560}
561
562std::optional<int32_t> FakeEventHub::getBatteryCapacity(int32_t, int32_t) const {
563 return BATTERY_CAPACITY;
564}
565
566std::optional<int32_t> FakeEventHub::getBatteryStatus(int32_t, int32_t) const {
567 return BATTERY_STATUS;
568}
569
570std::vector<int32_t> FakeEventHub::getRawBatteryIds(int32_t deviceId) const {
571 return {DEFAULT_BATTERY};
572}
573
574std::optional<RawBatteryInfo> FakeEventHub::getRawBatteryInfo(int32_t deviceId,
575 int32_t batteryId) const {
576 if (batteryId != DEFAULT_BATTERY) return {};
577 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
578 .name = "default battery",
579 .flags = InputBatteryClass::CAPACITY,
580 .path = BATTERY_DEVPATH};
581 return BATTERY_INFO;
582}
583
584std::vector<int32_t> FakeEventHub::getRawLightIds(int32_t deviceId) const {
585 std::vector<int32_t> ids;
586 for (const auto& [rawId, info] : mRawLightInfos) {
587 ids.push_back(rawId);
588 }
589 return ids;
590}
591
592std::optional<RawLightInfo> FakeEventHub::getRawLightInfo(int32_t deviceId, int32_t lightId) const {
593 auto it = mRawLightInfos.find(lightId);
594 if (it == mRawLightInfos.end()) {
595 return std::nullopt;
596 }
597 return it->second;
598}
599
600void FakeEventHub::setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) {
601 mLightBrightness.emplace(lightId, brightness);
602}
603
604void FakeEventHub::setLightIntensities(int32_t deviceId, int32_t lightId,
605 std::unordered_map<LightColor, int32_t> intensities) {
606 mLightIntensities.emplace(lightId, intensities);
607};
608
609std::optional<int32_t> FakeEventHub::getLightBrightness(int32_t deviceId, int32_t lightId) const {
610 auto lightIt = mLightBrightness.find(lightId);
611 if (lightIt == mLightBrightness.end()) {
612 return std::nullopt;
613 }
614 return lightIt->second;
615}
616
617std::optional<std::unordered_map<LightColor, int32_t>> FakeEventHub::getLightIntensities(
618 int32_t deviceId, int32_t lightId) const {
619 auto lightIt = mLightIntensities.find(lightId);
620 if (lightIt == mLightIntensities.end()) {
621 return std::nullopt;
622 }
623 return lightIt->second;
624};
625
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000626void FakeEventHub::setSysfsRootPath(int32_t deviceId, std::string sysfsRootPath) const {
627 Device* device = getDevice(deviceId);
628 if (device == nullptr) {
629 return;
630 }
631 device->sysfsRootPath = sysfsRootPath;
632}
633
634void FakeEventHub::sysfsNodeChanged(const std::string& sysfsNodePath) {
635 int32_t foundDeviceId = -1;
636 Device* foundDevice = nullptr;
637 for (size_t i = 0; i < mDevices.size(); i++) {
638 Device* d = mDevices.valueAt(i);
639 if (sysfsNodePath.find(d->sysfsRootPath) != std::string::npos) {
640 foundDeviceId = mDevices.keyAt(i);
641 foundDevice = d;
642 }
643 }
644 if (foundDevice == nullptr) {
645 return;
646 }
647 // If device sysfs changed -> reopen the device
648 if (!mRawLightInfos.empty() && !foundDevice->classes.test(InputDeviceClass::LIGHT)) {
Vaibhav Devmurarifb219eb2023-05-02 13:20:37 +0000649 InputDeviceIdentifier identifier = foundDevice->identifier;
650 ftl::Flags<InputDeviceClass> classes = foundDevice->classes;
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651 removeDevice(foundDeviceId);
Vaibhav Devmurarifb219eb2023-05-02 13:20:37 +0000652 addDevice(foundDeviceId, identifier.name, classes | InputDeviceClass::LIGHT,
653 identifier.bus);
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000654 }
655}
656
Harry Cuttsa5b71292022-11-28 12:56:17 +0000657} // namespace android