xref: /aosp_15_r20/external/libbrillo/brillo/udev/udev_monitor.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_UDEV_UDEV_MONITOR_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_UDEV_UDEV_MONITOR_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <memory>
9*1a96fba6SXin Li 
10*1a96fba6SXin Li #include <base/macros.h>
11*1a96fba6SXin Li #include <brillo/brillo_export.h>
12*1a96fba6SXin Li 
13*1a96fba6SXin Li struct udev_monitor;
14*1a96fba6SXin Li 
15*1a96fba6SXin Li namespace brillo {
16*1a96fba6SXin Li 
17*1a96fba6SXin Li class UdevDevice;
18*1a96fba6SXin Li 
19*1a96fba6SXin Li // A udev monitor, which wraps a udev_monitor C struct from libudev and related
20*1a96fba6SXin Li // library functions into a C++ object.
21*1a96fba6SXin Li class BRILLO_EXPORT UdevMonitor {
22*1a96fba6SXin Li  public:
23*1a96fba6SXin Li   static const int kInvalidFileDescriptor = -1;
24*1a96fba6SXin Li 
25*1a96fba6SXin Li   // Constructs a UdevMonitor object by taking a raw pointer to a udev_monitor
26*1a96fba6SXin Li   // struct as |monitor|. The ownership of |monitor| is not transferred, but its
27*1a96fba6SXin Li   // reference count is increased by one during the lifetime of this object.
28*1a96fba6SXin Li   explicit UdevMonitor(udev_monitor* monitor);
29*1a96fba6SXin Li 
30*1a96fba6SXin Li   // Destructs this UdevMonitor object and decreases the reference count of the
31*1a96fba6SXin Li   // underlying udev_monitor struct by one.
32*1a96fba6SXin Li   virtual ~UdevMonitor();
33*1a96fba6SXin Li 
34*1a96fba6SXin Li   // Wraps udev_monitor_enable_receiving(). Returns true on success.
35*1a96fba6SXin Li   virtual bool EnableReceiving();
36*1a96fba6SXin Li 
37*1a96fba6SXin Li   // Wraps udev_monitor_get_fd().
38*1a96fba6SXin Li   virtual int GetFileDescriptor() const;
39*1a96fba6SXin Li 
40*1a96fba6SXin Li   // Wraps udev_monitor_receive_device().
41*1a96fba6SXin Li   virtual std::unique_ptr<UdevDevice> ReceiveDevice();
42*1a96fba6SXin Li 
43*1a96fba6SXin Li   // Wraps udev_monitor_filter_add_match_subsystem_devtype(). Returns true on
44*1a96fba6SXin Li   // success.
45*1a96fba6SXin Li   virtual bool FilterAddMatchSubsystemDeviceType(const char* subsystem,
46*1a96fba6SXin Li                                                  const char* device_type);
47*1a96fba6SXin Li 
48*1a96fba6SXin Li   // Wraps udev_monitor_filter_add_match_tag(). Returns true on success.
49*1a96fba6SXin Li   virtual bool FilterAddMatchTag(const char* tag);
50*1a96fba6SXin Li 
51*1a96fba6SXin Li   // Wraps udev_monitor_filter_update(). Returns true on success.
52*1a96fba6SXin Li   virtual bool FilterUpdate();
53*1a96fba6SXin Li 
54*1a96fba6SXin Li   // Wraps udev_monitor_filter_remove(). Returns true on success.
55*1a96fba6SXin Li   virtual bool FilterRemove();
56*1a96fba6SXin Li 
57*1a96fba6SXin Li  private:
58*1a96fba6SXin Li   // Allows MockUdevMonitor to invoke the private default constructor below.
59*1a96fba6SXin Li   friend class MockUdevMonitor;
60*1a96fba6SXin Li 
61*1a96fba6SXin Li   // Constructs a UdevMonitor object without referencing a udev_monitor struct,
62*1a96fba6SXin Li   // which is only allowed to be called by MockUdevMonitor.
63*1a96fba6SXin Li   UdevMonitor();
64*1a96fba6SXin Li 
65*1a96fba6SXin Li   udev_monitor* monitor_;
66*1a96fba6SXin Li 
67*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(UdevMonitor);
68*1a96fba6SXin Li };
69*1a96fba6SXin Li 
70*1a96fba6SXin Li }  // namespace brillo
71*1a96fba6SXin Li 
72*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_UDEV_UDEV_MONITOR_H_
73