1 /**
2  * Copyright © 2021 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <sdbusplus/bus.hpp>
19 
20 #include <map>
21 #include <string>
22 
23 namespace phosphor::power::regulators
24 {
25 
26 /**
27  * @class VPD
28  *
29  * Abstract base class that provides an interface to hardware VPD (Vital Product
30  * Data).
31  *
32  * The interface is used to obtain VPD keyword values.
33  */
34 class VPD
35 {
36   public:
37     // Specify which compiler-generated methods we want
38     VPD() = default;
39     VPD(const VPD&) = delete;
40     VPD(VPD&&) = delete;
41     VPD& operator=(const VPD&) = delete;
42     VPD& operator=(VPD&&) = delete;
43     virtual ~VPD() = default;
44 
45     /**
46      * Clears any cached hardware VPD values.
47      */
48     virtual void clearCache(void) = 0;
49 
50     /**
51      * Returns the value of the specified VPD keyword for the specified
52      * inventory path.
53      *
54      * May return a cached value if one is available to improve performance.
55      *
56      * Throws an exception if an error occurs while obtaining the VPD
57      * value.
58      *
59      * @param inventoryPath D-Bus inventory path of the hardware
60      * @param keyword VPD keyword
61      * @return VPD keyword value
62      */
63     virtual std::string getValue(const std::string& inventoryPath,
64                                  const std::string& keyword) = 0;
65 };
66 
67 /**
68  * @class DBusVPD
69  *
70  * Implementation of the VPD interface using D-Bus method calls.
71  */
72 class DBusVPD : public VPD
73 {
74   public:
75     // Specify which compiler-generated methods we want
76     DBusVPD() = delete;
77     DBusVPD(const DBusVPD&) = delete;
78     DBusVPD(DBusVPD&&) = delete;
79     DBusVPD& operator=(const DBusVPD&) = delete;
80     DBusVPD& operator=(DBusVPD&&) = delete;
81     virtual ~DBusVPD() = default;
82 
83     /**
84      * Constructor.
85      *
86      * @param bus D-Bus bus object
87      */
88     explicit DBusVPD(sdbusplus::bus::bus& bus) : bus{bus}
89     {
90     }
91 
92     /** @copydoc VPD::clearCache() */
93     virtual void clearCache(void) override
94     {
95         cache.clear();
96     }
97 
98     /** @copydoc VPD::getValue() */
99     virtual std::string getValue(const std::string& inventoryPath,
100                                  const std::string& keyword) override;
101 
102   private:
103     /**
104      * Type alias for map from keyword names to values.
105      */
106     using KeywordMap = std::map<std::string, std::string>;
107 
108     /**
109      * D-Bus bus object.
110      */
111     sdbusplus::bus::bus& bus;
112 
113     /**
114      * Cached VPD keyword values.
115      *
116      * Map from inventory paths to VPD keywords.
117      */
118     std::map<std::string, KeywordMap> cache{};
119 };
120 
121 } // namespace phosphor::power::regulators
122