Return a copy of InputDeviceLightInfo instead of pointer

Refactor InputReaderInterface to return a copy instead of pointer. This
will ensure that we don't read from memory that's been freed.

Also, refactor InputDevice api's to return a list of infos instead of
having to query each individually. In all usages so far, there's no need
to get a specific info by type.

Bug: 190126442
Test: atest inputflinger_tests libinput_tests
Change-Id: I7f993a14259bb802e2631663c1c8bb65cc9b6702
diff --git a/services/inputflinger/reader/InputDevice.cpp b/services/inputflinger/reader/InputDevice.cpp
index ad503fd..7af014c 100644
--- a/services/inputflinger/reader/InputDevice.cpp
+++ b/services/inputflinger/reader/InputDevice.cpp
@@ -89,8 +89,7 @@
 }
 
 void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
-    InputDeviceInfo deviceInfo;
-    getDeviceInfo(&deviceInfo);
+    InputDeviceInfo deviceInfo = getDeviceInfo();
 
     dump += StringPrintf(INDENT "Device %d: %s\n", deviceInfo.getId(),
                          deviceInfo.getDisplayName().c_str());
@@ -417,15 +416,17 @@
     for_each_mapper([state](InputMapper& mapper) { mapper.updateExternalStylusState(state); });
 }
 
-void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
-    outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
-                              mHasMic);
+InputDeviceInfo InputDevice::getDeviceInfo() {
+    InputDeviceInfo outDeviceInfo;
+    outDeviceInfo.initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias, mIsExternal,
+                             mHasMic);
     for_each_mapper(
-            [outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(outDeviceInfo); });
+            [&outDeviceInfo](InputMapper& mapper) { mapper.populateDeviceInfo(&outDeviceInfo); });
 
     if (mController) {
-        mController->populateDeviceInfo(outDeviceInfo);
+        mController->populateDeviceInfo(&outDeviceInfo);
     }
+    return outDeviceInfo;
 }
 
 int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
diff --git a/services/inputflinger/reader/InputReader.cpp b/services/inputflinger/reader/InputReader.cpp
index e91f84e..10c04f6 100644
--- a/services/inputflinger/reader/InputReader.cpp
+++ b/services/inputflinger/reader/InputReader.cpp
@@ -406,9 +406,7 @@
     for (auto& devicePair : mDevices) {
         std::shared_ptr<InputDevice>& device = devicePair.second;
         if (device->getClasses().test(InputDeviceClass::EXTERNAL_STYLUS) && !device->isIgnored()) {
-            InputDeviceInfo info;
-            device->getDeviceInfo(&info);
-            outDevices.push_back(info);
+            outDevices.push_back(device->getDeviceInfo());
         }
     }
 }
@@ -498,9 +496,7 @@
 
     for (const auto& [device, eventHubIds] : mDeviceToEventHubIdsMap) {
         if (!device->isIgnored()) {
-            InputDeviceInfo info;
-            device->getDeviceInfo(&info);
-            outInputDevices.push_back(info);
+            outInputDevices.push_back(device->getDeviceInfo());
         }
     }
     return outInputDevices;
@@ -695,28 +691,26 @@
     return std::nullopt;
 }
 
-std::vector<int32_t> InputReader::getLightIds(int32_t deviceId) {
+std::vector<InputDeviceLightInfo> InputReader::getLights(int32_t deviceId) {
     std::scoped_lock _l(mLock);
 
     InputDevice* device = findInputDeviceLocked(deviceId);
-    if (device) {
-        InputDeviceInfo info;
-        device->getDeviceInfo(&info);
-        return info.getLightIds();
+    if (device == nullptr) {
+        return {};
     }
-    return {};
+
+    return device->getDeviceInfo().getLights();
 }
 
-const InputDeviceLightInfo* InputReader::getLightInfo(int32_t deviceId, int32_t lightId) {
+std::vector<InputDeviceSensorInfo> InputReader::getSensors(int32_t deviceId) {
     std::scoped_lock _l(mLock);
 
     InputDevice* device = findInputDeviceLocked(deviceId);
-    if (device) {
-        InputDeviceInfo info;
-        device->getDeviceInfo(&info);
-        return info.getLightInfo(lightId);
+    if (device == nullptr) {
+        return {};
     }
-    return nullptr;
+
+    return device->getDeviceInfo().getSensors();
 }
 
 bool InputReader::setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
diff --git a/services/inputflinger/reader/include/InputDevice.h b/services/inputflinger/reader/include/InputDevice.h
index 291f105..2f2eba7 100644
--- a/services/inputflinger/reader/include/InputDevice.h
+++ b/services/inputflinger/reader/include/InputDevice.h
@@ -80,7 +80,7 @@
     void timeoutExpired(nsecs_t when);
     void updateExternalStylusState(const StylusState& state);
 
-    void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
+    InputDeviceInfo getDeviceInfo();
     int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
     int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
     int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
diff --git a/services/inputflinger/reader/include/InputReader.h b/services/inputflinger/reader/include/InputReader.h
index bc79ccf..a00c5af 100644
--- a/services/inputflinger/reader/include/InputReader.h
+++ b/services/inputflinger/reader/include/InputReader.h
@@ -99,9 +99,9 @@
 
     std::optional<int32_t> getBatteryStatus(int32_t deviceId) override;
 
-    std::vector<int32_t> getLightIds(int32_t deviceId) override;
+    std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) override;
 
-    const InputDeviceLightInfo* getLightInfo(int32_t deviceId, int32_t lightId) override;
+    std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) override;
 
     bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) override;
 
@@ -130,24 +130,24 @@
         // lock is already held by the input loop
         void updateGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
         int32_t getGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
-        void disableVirtualKeysUntil(nsecs_t time) NO_THREAD_SAFETY_ANALYSIS override;
-        bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
-                                  int32_t scanCode) NO_THREAD_SAFETY_ANALYSIS override;
-        void fadePointer() NO_THREAD_SAFETY_ANALYSIS override;
+        void disableVirtualKeysUntil(nsecs_t time) REQUIRES(mReader->mLock) override;
+        bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode)
+                REQUIRES(mReader->mLock) override;
+        void fadePointer() REQUIRES(mReader->mLock) override;
         std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId)
-                NO_THREAD_SAFETY_ANALYSIS override;
-        void requestTimeoutAtTime(nsecs_t when) NO_THREAD_SAFETY_ANALYSIS override;
+                REQUIRES(mReader->mLock) override;
+        void requestTimeoutAtTime(nsecs_t when) REQUIRES(mReader->mLock) override;
         int32_t bumpGeneration() NO_THREAD_SAFETY_ANALYSIS override;
         void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices)
-                NO_THREAD_SAFETY_ANALYSIS override;
+                REQUIRES(mReader->mLock) override;
         void dispatchExternalStylusState(const StylusState& outState)
-                NO_THREAD_SAFETY_ANALYSIS override;
-        InputReaderPolicyInterface* getPolicy() NO_THREAD_SAFETY_ANALYSIS override;
-        InputListenerInterface* getListener() NO_THREAD_SAFETY_ANALYSIS override;
-        EventHubInterface* getEventHub() NO_THREAD_SAFETY_ANALYSIS override;
+                REQUIRES(mReader->mLock) override;
+        InputReaderPolicyInterface* getPolicy() REQUIRES(mReader->mLock) override;
+        InputListenerInterface* getListener() REQUIRES(mReader->mLock) override;
+        EventHubInterface* getEventHub() REQUIRES(mReader->mLock) override;
         int32_t getNextId() NO_THREAD_SAFETY_ANALYSIS override;
-        void updateLedMetaState(int32_t metaState) NO_THREAD_SAFETY_ANALYSIS override;
-        int32_t getLedMetaState() NO_THREAD_SAFETY_ANALYSIS override;
+        void updateLedMetaState(int32_t metaState) REQUIRES(mReader->mLock) override;
+        int32_t getLedMetaState() REQUIRES(mReader->mLock) REQUIRES(mLock) override;
     } mContext;
 
     friend class ContextImpl;