xref: /openbmc/phosphor-power/phosphor-regulators/src/vpd.hpp (revision 0c9a33d66f74a76c1842990f41da678f79fa0c05)
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 <cstdint>
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 namespace phosphor::power::regulators
26 {
27 
28 /**
29  * @class VPD
30  *
31  * Abstract base class that provides an interface to hardware VPD (Vital Product
32  * Data).
33  *
34  * The interface is used to obtain VPD keyword values.
35  */
36 class VPD
37 {
38   public:
39     // Specify which compiler-generated methods we want
40     VPD() = default;
41     VPD(const VPD&) = delete;
42     VPD(VPD&&) = delete;
43     VPD& operator=(const VPD&) = delete;
44     VPD& operator=(VPD&&) = delete;
45     virtual ~VPD() = default;
46 
47     /**
48      * Clears any cached hardware VPD values.
49      */
50     virtual void clearCache(void) = 0;
51 
52     /**
53      * Returns the value of the specified VPD keyword for the specified
54      * inventory path.
55      *
56      * May return a cached value if one is available to improve performance.
57      *
58      * Throws an exception if an error occurs while obtaining the VPD
59      * value.
60      *
61      * @param inventoryPath D-Bus inventory path of the hardware
62      * @param keyword VPD keyword
63      * @return VPD keyword value
64      */
65     virtual std::vector<uint8_t> getValue(const std::string& inventoryPath,
66                                           const std::string& keyword) = 0;
67 };
68 
69 /**
70  * @class DBusVPD
71  *
72  * Implementation of the VPD interface using D-Bus method calls.
73  */
74 class DBusVPD : public VPD
75 {
76   public:
77     // Specify which compiler-generated methods we want
78     DBusVPD() = delete;
79     DBusVPD(const DBusVPD&) = delete;
80     DBusVPD(DBusVPD&&) = delete;
81     DBusVPD& operator=(const DBusVPD&) = delete;
82     DBusVPD& operator=(DBusVPD&&) = delete;
83     virtual ~DBusVPD() = default;
84 
85     /**
86      * Constructor.
87      *
88      * @param bus D-Bus bus object
89      */
90     explicit DBusVPD(sdbusplus::bus::bus& bus) : bus{bus}
91     {}
92 
93     /** @copydoc VPD::clearCache() */
94     virtual void clearCache(void) override
95     {
96         cache.clear();
97     }
98 
99     /** @copydoc VPD::getValue() */
100     virtual std::vector<uint8_t> getValue(const std::string& inventoryPath,
101                                           const std::string& keyword) override;
102 
103   private:
104     /**
105      * Type alias for map from keyword names to values.
106      */
107     using KeywordMap = std::map<std::string, std::vector<uint8_t>>;
108 
109     /**
110      * D-Bus bus object.
111      */
112     sdbusplus::bus::bus& bus;
113 
114     /**
115      * Cached VPD keyword values.
116      *
117      * Map from inventory paths to VPD keywords.
118      */
119     std::map<std::string, KeywordMap> cache{};
120 };
121 
122 } // namespace phosphor::power::regulators
123