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_DEVICE_H_ 6 #define LIBBRILLO_BRILLO_UDEV_UDEV_DEVICE_H_ 7 8 #include <stdint.h> 9 #include <sys/types.h> 10 11 #include <memory> 12 13 #include <base/macros.h> 14 #include <brillo/brillo_export.h> 15 #include <brillo/udev/udev_list_entry.h> 16 17 struct udev_device; 18 19 namespace brillo { 20 21 // A udev device, which wraps a udev_device C struct from libudev and related 22 // library functions into a C++ object. 23 class BRILLO_EXPORT UdevDevice { 24 public: 25 // Constructs a UdevDevice object by taking a raw pointer to a udev_device 26 // struct as |device|. The ownership of |device| is not transferred, but its 27 // reference count is increased by one during the lifetime of this object. 28 explicit UdevDevice(udev_device* device); 29 30 // Destructs this UdevDevice object and decreases the reference count of the 31 // underlying udev_device struct by one. 32 virtual ~UdevDevice(); 33 34 // Wraps udev_device_get_parent(). 35 virtual std::unique_ptr<UdevDevice> GetParent() const; 36 37 // Wraps udev_device_get_parent_with_subsystem_devtype(). 38 virtual std::unique_ptr<UdevDevice> GetParentWithSubsystemDeviceType( 39 const char* subsystem, const char* device_type) const; 40 41 // Wraps udev_device_get_is_initialized(). 42 virtual bool IsInitialized() const; 43 44 // Wraps udev_device_get_usec_since_initialized(). 45 virtual uint64_t GetMicrosecondsSinceInitialized() const; 46 47 // Wraps udev_device_get_seqnum(). 48 virtual uint64_t GetSequenceNumber() const; 49 50 // Wraps udev_device_get_devpath(). 51 virtual const char* GetDevicePath() const; 52 53 // Wraps udev_device_get_devnode(). 54 virtual const char* GetDeviceNode() const; 55 56 // Wraps udev_device_get_devnum(). 57 virtual dev_t GetDeviceNumber() const; 58 59 // Wraps udev_device_get_devtype(). 60 virtual const char* GetDeviceType() const; 61 62 // Wraps udev_device_get_driver(). 63 virtual const char* GetDriver() const; 64 65 // Wraps udev_device_get_subsystem(). 66 virtual const char* GetSubsystem() const; 67 68 // Wraps udev_device_get_syspath(). 69 virtual const char* GetSysPath() const; 70 71 // Wraps udev_device_get_sysname(). 72 virtual const char* GetSysName() const; 73 74 // Wraps udev_device_get_sysnum(). 75 virtual const char* GetSysNumber() const; 76 77 // Wraps udev_device_get_action(). 78 virtual const char* GetAction() const; 79 80 // Wraps udev_device_get_devlinks_list_entry(). 81 virtual std::unique_ptr<UdevListEntry> GetDeviceLinksListEntry() const; 82 83 // Wraps udev_device_get_properties_list_entry(). 84 virtual std::unique_ptr<UdevListEntry> GetPropertiesListEntry() const; 85 86 // Wraps udev_device_get_property_value(). 87 virtual const char* GetPropertyValue(const char* key) const; 88 89 // Wraps udev_device_get_tags_list_entry(). 90 virtual std::unique_ptr<UdevListEntry> GetTagsListEntry() const; 91 92 // Wraps udev_device_get_sysattr_list_entry(). 93 virtual std::unique_ptr<UdevListEntry> GetSysAttributeListEntry() const; 94 95 // Wraps udev_device_get_sysattr_value(). 96 virtual const char* GetSysAttributeValue(const char* attribute) const; 97 98 // Creates a copy of this UdevDevice pointing to the same underlying 99 // struct udev_device* (increasing its libudev reference count by 1). 100 virtual std::unique_ptr<UdevDevice> Clone(); 101 102 private: 103 // Allows MockUdevDevice to invoke the private default constructor below. 104 friend class MockUdevDevice; 105 106 // Constructs a UdevDevice object without referencing a udev_device struct, 107 // which is only allowed to be called by MockUdevDevice. 108 UdevDevice(); 109 110 udev_device* device_; 111 112 DISALLOW_COPY_AND_ASSIGN(UdevDevice); 113 }; 114 115 } // namespace brillo 116 117 #endif // LIBBRILLO_BRILLO_UDEV_UDEV_DEVICE_H_ 118