#pragma once

#include "device_bus/monitor/IDeviceMonitor.h"

#if defined(__linux__)

#include <libudev.h>

#include <QSocketNotifier>

namespace softbus::device_bus::monitor
{

class UdevDeviceMonitor : public IDeviceMonitor
{
    Q_OBJECT
public:
    explicit UdevDeviceMonitor(QObject* parent = nullptr);
    ~UdevDeviceMonitor() override;

    bool start() override;
    void stop() override;
    std::vector<DeviceEvent> snapshotExisting() override;

private:
    void onReadable();
    bool shouldKeepEvent(const DeviceEvent& ev) const;
    DeviceEvent toEvent(struct udev_device* dev, const char* action);
    static QString getProp(struct udev_device* dev, const char* key);

private:
    struct udev* m_udev{nullptr};
    struct udev_monitor* m_mon{nullptr};
    int m_fd{-1};
    QSocketNotifier* m_notifier{nullptr};
};

} // namespace softbus::device_bus::monitor

#endif // __linux__

