aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/udev/udev_device.h
blob: 2704a22ba516c04cf8e292c403aa28d4940b7677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBBRILLO_BRILLO_UDEV_UDEV_DEVICE_H_
#define LIBBRILLO_BRILLO_UDEV_UDEV_DEVICE_H_

#include <stdint.h>
#include <sys/types.h>

#include <memory>

#include <base/macros.h>
#include <brillo/brillo_export.h>
#include <brillo/udev/udev_list_entry.h>

struct udev_device;

namespace brillo {

// A udev device, which wraps a udev_device C struct from libudev and related
// library functions into a C++ object.
class BRILLO_EXPORT UdevDevice {
 public:
  // Constructs a UdevDevice object by taking a raw pointer to a udev_device
  // struct as |device|. The ownership of |device| is not transferred, but its
  // reference count is increased by one during the lifetime of this object.
  explicit UdevDevice(udev_device* device);

  // Destructs this UdevDevice object and decreases the reference count of the
  // underlying udev_device struct by one.
  virtual ~UdevDevice();

  // Wraps udev_device_get_parent().
  virtual std::unique_ptr<UdevDevice> GetParent() const;

  // Wraps udev_device_get_parent_with_subsystem_devtype().
  virtual std::unique_ptr<UdevDevice> GetParentWithSubsystemDeviceType(
      const char* subsystem, const char* device_type) const;

  // Wraps udev_device_get_is_initialized().
  virtual bool IsInitialized() const;

  // Wraps udev_device_get_usec_since_initialized().
  virtual uint64_t GetMicrosecondsSinceInitialized() const;

  // Wraps udev_device_get_seqnum().
  virtual uint64_t GetSequenceNumber() const;

  // Wraps udev_device_get_devpath().
  virtual const char* GetDevicePath() const;

  // Wraps udev_device_get_devnode().
  virtual const char* GetDeviceNode() const;

  // Wraps udev_device_get_devnum().
  virtual dev_t GetDeviceNumber() const;

  // Wraps udev_device_get_devtype().
  virtual const char* GetDeviceType() const;

  // Wraps udev_device_get_driver().
  virtual const char* GetDriver() const;

  // Wraps udev_device_get_subsystem().
  virtual const char* GetSubsystem() const;

  // Wraps udev_device_get_syspath().
  virtual const char* GetSysPath() const;

  // Wraps udev_device_get_sysname().
  virtual const char* GetSysName() const;

  // Wraps udev_device_get_sysnum().
  virtual const char* GetSysNumber() const;

  // Wraps udev_device_get_action().
  virtual const char* GetAction() const;

  // Wraps udev_device_get_devlinks_list_entry().
  virtual std::unique_ptr<UdevListEntry> GetDeviceLinksListEntry() const;

  // Wraps udev_device_get_properties_list_entry().
  virtual std::unique_ptr<UdevListEntry> GetPropertiesListEntry() const;

  // Wraps udev_device_get_property_value().
  virtual const char* GetPropertyValue(const char* key) const;

  // Wraps udev_device_get_tags_list_entry().
  virtual std::unique_ptr<UdevListEntry> GetTagsListEntry() const;

  // Wraps udev_device_get_sysattr_list_entry().
  virtual std::unique_ptr<UdevListEntry> GetSysAttributeListEntry() const;

  // Wraps udev_device_get_sysattr_value().
  virtual const char* GetSysAttributeValue(const char* attribute) const;

  // Creates a copy of this UdevDevice pointing to the same underlying
  // struct udev_device* (increasing its libudev reference count by 1).
  virtual std::unique_ptr<UdevDevice> Clone();

 private:
  // Allows MockUdevDevice to invoke the private default constructor below.
  friend class MockUdevDevice;

  // Constructs a UdevDevice object without referencing a udev_device struct,
  // which is only allowed to be called by MockUdevDevice.
  UdevDevice();

  udev_device* device_;

  DISALLOW_COPY_AND_ASSIGN(UdevDevice);
};

}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_UDEV_UDEV_DEVICE_H_