1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/bus/match.hpp>
5 
6 namespace openpower
7 {
8 namespace pels
9 {
10 
11 using DBusValue = sdbusplus::message::variant<std::string>;
12 using DBusProperty = std::string;
13 using DBusInterface = std::string;
14 using DBusService = std::string;
15 using DBusPath = std::string;
16 using DBusInterfaceList = std::vector<DBusInterface>;
17 using DBusPropertyMap = std::map<DBusProperty, DBusValue>;
18 
19 /**
20  * @class DataInterface
21  *
22  * An abstract interface class for gathering data about the system
23  * for use in PELs. Implemented this way to facilitate mocking.
24  */
25 class DataInterfaceBase
26 {
27   public:
28     DataInterfaceBase() = default;
29     virtual ~DataInterfaceBase() = default;
30     DataInterfaceBase(const DataInterfaceBase&) = default;
31     DataInterfaceBase& operator=(const DataInterfaceBase&) = default;
32     DataInterfaceBase(DataInterfaceBase&&) = default;
33     DataInterfaceBase& operator=(DataInterfaceBase&&) = default;
34 
35     /**
36      * @brief Pure virtual for returning the MTM
37      *
38      * @return string - The machine Type/Model string
39      */
40     virtual std::string getMachineTypeModel() const = 0;
41 
42     /**
43      * @brief Pure virtual for returning the machine SN
44      *
45      * @return string - The machine serial number
46      */
47     virtual std::string getMachineSerialNumber() const = 0;
48 };
49 
50 /**
51  * @class DataInterface
52  *
53  * Concrete implementation of DataInterfaceBase.
54  */
55 class DataInterface : public DataInterfaceBase
56 {
57   public:
58     DataInterface() = delete;
59     ~DataInterface() = default;
60     DataInterface(const DataInterface&) = default;
61     DataInterface& operator=(const DataInterface&) = default;
62     DataInterface(DataInterface&&) = default;
63     DataInterface& operator=(DataInterface&&) = default;
64 
65     /**
66      * @brief Constructor
67      *
68      * @param[in] bus - The sdbusplus bus object
69      */
70     explicit DataInterface(sdbusplus::bus::bus& bus);
71 
72     /**
73      * @brief Returns the machine type/model value
74      *
75      * @return string - The machine Type/Model string
76      */
77     std::string getMachineTypeModel() const override
78     {
79         return _machineTypeModel;
80     }
81 
82     /**
83      * @brief Returns the machine SN
84      *
85      * @return string - The machine serial number
86      */
87     std::string getMachineSerialNumber() const override
88     {
89         return _machineSerialNumber;
90     }
91 
92   private:
93     /**
94      * @brief Reads the machine type/model and SN from D-Bus.
95      *
96      * Looks for them on the 'system' inventory object, and also
97      * places a properties changed watch on them to obtain any changes
98      * (or read them for the first time if the inventory isn't ready
99      * when this function runs.)
100      */
101     void readMTMS();
102 
103     /**
104      * @brief Finds the D-Bus service name that hosts the
105      *        passed in path and interface.
106      *
107      * @param[in] objectPath - The D-Bus object path
108      * @param[in] interface - The D-Bus interface
109      */
110     DBusService getService(const std::string& objectPath,
111                            const std::string& interface);
112     /**
113      * @brief Wrapper for the 'GetAll' properties method call
114      *
115      * @param[in] service - The D-Bus service to call it on
116      * @param[in] objectPath - The D-Bus object path
117      * @param[in] interface - The interface to get the props on
118      *
119      * @return DBusPropertyMap - The property results
120      */
121     DBusPropertyMap getAllProperties(const std::string& service,
122                                      const std::string& objectPath,
123                                      const std::string& interface);
124 
125     /**
126      * @brief The properties changed callback for the Asset iface
127      *        on the system inventory object.
128      *
129      * @param[in] msg - The sdbusplus message of the signal
130      */
131     void sysAssetPropChanged(sdbusplus::message::message& msg);
132 
133     /**
134      * @brief The machine type-model.  Always kept up to date
135      */
136     std::string _machineTypeModel;
137 
138     /**
139      * @brief The machine serial number.  Always kept up to date
140      */
141     std::string _machineSerialNumber;
142 
143     /**
144      * @brief The match object for the system path's properties
145      */
146     std::unique_ptr<sdbusplus::bus::match_t> _sysInventoryPropMatch;
147 
148     /**
149      * @brief The sdbusplus bus object for making D-Bus calls.
150      */
151     sdbusplus::bus::bus& _bus;
152 };
153 
154 } // namespace pels
155 } // namespace openpower
156