blob: 8ae1314a86a54beab9536280437dd3a6aaabe5a8 [file] [log] [blame]
Shawn McCarney4e0402c2021-02-05 00:08:33 -06001/**
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
Matt Spinleraacc2aa2021-05-25 09:31:35 -060020#include <cstdint>
Shawn McCarney4e0402c2021-02-05 00:08:33 -060021#include <map>
22#include <string>
Matt Spinleraacc2aa2021-05-25 09:31:35 -060023#include <vector>
Shawn McCarney4e0402c2021-02-05 00:08:33 -060024
25namespace 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 */
36class 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 */
Matt Spinleraacc2aa2021-05-25 09:31:35 -060065 virtual std::vector<uint8_t> getValue(const std::string& inventoryPath,
66 const std::string& keyword) = 0;
Shawn McCarney4e0402c2021-02-05 00:08:33 -060067};
68
69/**
70 * @class DBusVPD
71 *
72 * Implementation of the VPD interface using D-Bus method calls.
73 */
74class 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
94 /** @copydoc VPD::clearCache() */
95 virtual void clearCache(void) override
96 {
97 cache.clear();
98 }
99
100 /** @copydoc VPD::getValue() */
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600101 virtual std::vector<uint8_t> getValue(const std::string& inventoryPath,
102 const std::string& keyword) override;
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600103
104 private:
105 /**
106 * Type alias for map from keyword names to values.
107 */
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600108 using KeywordMap = std::map<std::string, std::vector<uint8_t>>;
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600109
110 /**
111 * D-Bus bus object.
112 */
113 sdbusplus::bus::bus& bus;
114
115 /**
116 * Cached VPD keyword values.
117 *
118 * Map from inventory paths to VPD keywords.
119 */
120 std::map<std::string, KeywordMap> cache{};
121};
122
123} // namespace phosphor::power::regulators