• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H_
6 #define LIBBRILLO_BRILLO_UDEV_UDEV_H_
7 
8 #include <sys/types.h>
9 
10 #include <memory>
11 
12 #include <base/macros.h>
13 #include <brillo/brillo_export.h>
14 
15 struct udev;
16 struct udev_device;
17 
18 namespace brillo {
19 
20 class UdevDevice;
21 class UdevEnumerate;
22 class UdevMonitor;
23 
24 // A udev library context, which wraps a udev C struct from libudev and related
25 // library functions into a C++ object.
26 class BRILLO_EXPORT Udev {
27  public:
28   // Creates and initializes a Udev object. Returns nullptr on failure.
29   static std::unique_ptr<Udev> Create();
30   virtual ~Udev();
31 
32   // Wraps udev_device_new_from_syspath().
33   virtual std::unique_ptr<UdevDevice> CreateDeviceFromSysPath(
34       const char* sys_path);
35 
36   // Wraps udev_device_new_from_devnum().
37   virtual std::unique_ptr<UdevDevice> CreateDeviceFromDeviceNumber(
38       char type, dev_t device_number);
39 
40   // Wraps udev_device_new_from_subsystem_sysname().
41   virtual std::unique_ptr<UdevDevice> CreateDeviceFromSubsystemSysName(
42       const char* subsystem, const char* sys_name);
43 
44   // Wraps udev_enumerate_new().
45   virtual std::unique_ptr<UdevEnumerate> CreateEnumerate();
46 
47   // Wraps udev_monitor_new_from_netlink().
48   virtual std::unique_ptr<UdevMonitor> CreateMonitorFromNetlink(
49       const char* name);
50 
51  private:
52   friend class MockUdev;
53 
54   // Creates a Udev by taking ownership of the |udev|.
55   explicit Udev(struct udev* udev);
56 
57   // Creates a UdevDevice object that wraps a given udev_device struct pointed
58   // by |device|. The ownership of |device| is transferred to returned
59   // UdevDevice object.
60   static std::unique_ptr<UdevDevice> CreateDevice(udev_device* device);
61 
62   struct udev* udev_;
63 
64   DISALLOW_COPY_AND_ASSIGN(Udev);
65 };
66 
67 }  // namespace brillo
68 
69 #endif  // LIBBRILLO_BRILLO_UDEV_UDEV_H_
70