Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3fef469 authored by Chris Ye's avatar Chris Ye
Browse files

Add InputDevice stats atom logging

Logging InputDevice information with Android stats atom, when an
InputDevice is added to inputflinger.

Bug: 170321221
Test: Manual test: m statsd_testdrive, statsd_testdrive id
Change-Id: I484fd0a487b35c1946ee7db6ff0f70a92168b6bd
parent 7aba0545
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -148,6 +148,11 @@ TEST(Flags, EqualsOperator_DontShareState) {
    ASSERT_NE(flags1, flags2);
}

TEST(Flags, GetValue) {
    Flags<TestFlags> flags = TestFlags::ONE | TestFlags::TWO;
    ASSERT_EQ(flags.get(), 0x3);
}

TEST(Flags, String_NoFlags) {
    Flags<TestFlags> flags;
    ASSERT_EQ(flags.string(), "0x0");
+1 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ cc_defaults {
        "libcutils",
        "libinput",
        "liblog",
        "libstatslog",
        "libui",
        "libutils",
    ],
+14 −5
Original line number Diff line number Diff line
@@ -37,13 +37,14 @@

// #define LOG_NDEBUG 0
#include <android-base/file.h>
#include <android-base/strings.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/properties.h>
#include <input/KeyCharacterMap.h>
#include <input/KeyLayoutMap.h>
#include <input/VirtualKeyMap.h>
#include <openssl/sha.h>
#include <statslog.h>
#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/Timers.h>
@@ -1461,7 +1462,7 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
            for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();
                 it++) {
                std::unique_ptr<TouchVideoDevice>& videoDevice = *it;
                if (tryAddVideoDevice(*device, videoDevice)) {
                if (tryAddVideoDeviceLocked(*device, videoDevice)) {
                    // videoDevice was transferred to 'device'
                    it = mUnattachedVideoDevices.erase(it);
                    break;
@@ -1771,6 +1772,13 @@ void EventHub::unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& vide
    }
}

void EventHub::reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
                                                    Flags<InputDeviceClass> classes) {
    android::util::stats_write(android::util::INPUTDEVICE_REGISTERED, identifier.name.c_str(),
                               identifier.vendor, identifier.product, identifier.version,
                               identifier.bus, identifier.uniqueId.c_str(), classes.get());
}

void EventHub::openDeviceLocked(const std::string& devicePath) {
    // If an input device happens to register around the time when EventHub's constructor runs, it
    // is possible that the same input event node (for example, /dev/input/event3) will be noticed
@@ -2076,7 +2084,7 @@ void EventHub::openVideoDeviceLocked(const std::string& devicePath) {
    }
    // Transfer ownership of this video device to a matching input device
    for (const auto& [id, device] : mDevices) {
        if (tryAddVideoDevice(*device, videoDevice)) {
        if (tryAddVideoDeviceLocked(*device, videoDevice)) {
            return; // 'device' now owns 'videoDevice'
        }
    }
@@ -2088,7 +2096,7 @@ void EventHub::openVideoDeviceLocked(const std::string& devicePath) {
    mUnattachedVideoDevices.push_back(std::move(videoDevice));
}

bool EventHub::tryAddVideoDevice(EventHub::Device& device,
bool EventHub::tryAddVideoDeviceLocked(EventHub::Device& device,
                                       std::unique_ptr<TouchVideoDevice>& videoDevice) {
    if (videoDevice->getName() != device.identifier.name) {
        return false;
@@ -2163,6 +2171,7 @@ void EventHub::createVirtualKeyboardLocked() {
}

void EventHub::addDeviceLocked(std::unique_ptr<Device> device) {
    reportDeviceAddedForStatisticsLocked(device->identifier, device->classes);
    mOpeningDevices.push_back(std::move(device));
}

+29 −26
Original line number Diff line number Diff line
@@ -570,8 +570,8 @@ private:
    /**
     * Create a new device for the provided path.
     */
    void openDeviceLocked(const std::string& devicePath);
    void openVideoDeviceLocked(const std::string& devicePath);
    void openDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
    void openVideoDeviceLocked(const std::string& devicePath) REQUIRES(mLock);
    /**
     * Try to associate a video device with an input device. If the association succeeds,
     * the videoDevice is moved into the input device. 'videoDevice' will become null if this
@@ -579,39 +579,42 @@ private:
     * Return true if the association succeeds.
     * Return false otherwise.
     */
    bool tryAddVideoDevice(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice);
    void createVirtualKeyboardLocked();
    void addDeviceLocked(std::unique_ptr<Device> device);
    void assignDescriptorLocked(InputDeviceIdentifier& identifier);
    bool tryAddVideoDeviceLocked(Device& device, std::unique_ptr<TouchVideoDevice>& videoDevice)
            REQUIRES(mLock);
    void createVirtualKeyboardLocked() REQUIRES(mLock);
    void addDeviceLocked(std::unique_ptr<Device> device) REQUIRES(mLock);
    void assignDescriptorLocked(InputDeviceIdentifier& identifier) REQUIRES(mLock);

    void closeDeviceByPathLocked(const std::string& devicePath);
    void closeVideoDeviceByPathLocked(const std::string& devicePath);
    void closeDeviceLocked(Device& device);
    void closeAllDevicesLocked();
    void closeDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
    void closeVideoDeviceByPathLocked(const std::string& devicePath) REQUIRES(mLock);
    void closeDeviceLocked(Device& device) REQUIRES(mLock);
    void closeAllDevicesLocked() REQUIRES(mLock);

    status_t registerFdForEpoll(int fd);
    status_t unregisterFdFromEpoll(int fd);
    status_t registerDeviceForEpollLocked(Device& device);
    void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice);
    status_t unregisterDeviceFromEpollLocked(Device& device);
    void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice);

    status_t scanDirLocked(const std::string& dirname);
    status_t scanVideoDirLocked(const std::string& dirname);
    void scanDevicesLocked();
    status_t readNotifyLocked();

    Device* getDeviceByDescriptorLocked(const std::string& descriptor) const;
    Device* getDeviceLocked(int32_t deviceId) const;
    Device* getDeviceByPathLocked(const std::string& devicePath) const;
    status_t registerDeviceForEpollLocked(Device& device) REQUIRES(mLock);
    void registerVideoDeviceForEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);
    status_t unregisterDeviceFromEpollLocked(Device& device) REQUIRES(mLock);
    void unregisterVideoDeviceFromEpollLocked(const TouchVideoDevice& videoDevice) REQUIRES(mLock);

    status_t scanDirLocked(const std::string& dirname) REQUIRES(mLock);
    status_t scanVideoDirLocked(const std::string& dirname) REQUIRES(mLock);
    void scanDevicesLocked() REQUIRES(mLock);
    status_t readNotifyLocked() REQUIRES(mLock);

    Device* getDeviceByDescriptorLocked(const std::string& descriptor) const REQUIRES(mLock);
    Device* getDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
    Device* getDeviceByPathLocked(const std::string& devicePath) const REQUIRES(mLock);
    /**
     * Look through all available fd's (both for input devices and for video devices),
     * and return the device pointer.
     */
    Device* getDeviceByFdLocked(int fd) const;
    Device* getDeviceByFdLocked(int fd) const REQUIRES(mLock);

    int32_t getNextControllerNumberLocked(const std::string& name);
    void releaseControllerNumberLocked(int32_t num);
    int32_t getNextControllerNumberLocked(const std::string& name) REQUIRES(mLock);
    void releaseControllerNumberLocked(int32_t num) REQUIRES(mLock);
    void reportDeviceAddedForStatisticsLocked(const InputDeviceIdentifier& identifier,
                                              Flags<InputDeviceClass> classes) REQUIRES(mLock);

    // Protect all internal state.
    mutable std::mutex mLock;