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