blob: 4af3fa7dbd34701b77a46ee3cd4cecf8594dca15 [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}
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000091 {}
Shawn McCarney4e0402c2021-02-05 00:08:33 -060092
93 /** @copydoc VPD::clearCache() */
94 virtual void clearCache(void) override
95 {
96 cache.clear();
97 }
98
99 /** @copydoc VPD::getValue() */
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600100 virtual std::vector<uint8_t> getValue(const std::string& inventoryPath,
101 const std::string& keyword) override;
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600102
103 private:
104 /**
105 * Type alias for map from keyword names to values.
106 */
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600107 using KeywordMap = std::map<std::string, std::vector<uint8_t>>;
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600108
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