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