blob: ae84945927a4014428f21b55b830c4c616837161 [file] [log] [blame]
Patrick Venturef085d912019-03-15 08:50:00 -07001#pragma once
2
3#include <cstdint>
Patrick Ventureab650002019-03-16 09:08:47 -07004#include <map>
5#include <nlohmann/json.hpp>
Patrick Venturef085d912019-03-15 08:50:00 -07006#include <string>
7#include <tuple>
Patrick Venture49f23ad2019-03-16 11:59:55 -07008#include <vector>
Patrick Venturef085d912019-03-15 08:50:00 -07009
10namespace google
11{
12namespace ipmi
13{
14
Patrick Venturebb90d4f2019-03-15 13:42:06 -070015using VersionTuple =
16 std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
17
Patrick Ventureab650002019-03-16 09:08:47 -070018extern const std::string defaultConfigFile;
19
Patrick Venturef085d912019-03-15 08:50:00 -070020class HandlerInterface
21{
22 public:
23 virtual ~HandlerInterface() = default;
24
25 /**
26 * Return ethernet details (hard-coded).
27 *
28 * @return tuple of ethernet details (channel, if name).
29 */
30 virtual std::tuple<std::uint8_t, std::string> getEthDetails() const = 0;
Patrick Ventured2037c62019-03-15 10:29:47 -070031
32 /**
33 * Return the value of rx_packets, given a if_name.
34 *
35 * @param[in] name, the interface name.
36 * @return the number of packets received.
37 * @throw IpmiException on failure.
38 */
39 virtual std::int64_t getRxPackets(const std::string& name) const = 0;
Patrick Venturebb90d4f2019-03-15 13:42:06 -070040
41 /**
42 * Return the values from a cpld version file.
43 *
44 * @param[in] id - the cpld id number.
45 * @return the quad of numbers as a tuple (maj,min,pt,subpt)
46 * @throw IpmiException on failure.
47 */
48 virtual VersionTuple getCpldVersion(unsigned int id) const = 0;
Patrick Ventureaa374122019-03-15 15:09:10 -070049
50 /**
51 * Set the PSU Reset delay.
52 *
53 * @param[in] delay - delay in seconds.
54 * @throw IpmiException on failure.
55 */
56 virtual void psuResetDelay(std::uint32_t delay) const = 0;
Patrick Venture07f85152019-03-15 21:36:56 -070057
58 /**
59 * Return the entity name.
60 * On the first call to this method it'll build the list of entities.
61 * @todo Consider moving the list building to construction time (and ignore
62 * failures).
63 *
64 * @param[in] id - the entity id value
65 * @param[in] instance - the entity instance
66 * @return the entity's name
67 * @throw IpmiException on failure.
68 */
69 virtual std::string getEntityName(std::uint8_t id,
70 std::uint8_t instance) = 0;
Patrick Venture49f23ad2019-03-16 11:59:55 -070071
72 /**
73 * Populate the i2c-pcie mapping vector.
74 */
75 virtual void buildI2cPcieMapping() = 0;
76
77 /**
78 * Return the size of the i2c-pcie mapping vector.
79 *
80 * @return the size of the vector holding the i2c-pcie mapping tuples.
81 */
82 virtual size_t getI2cPcieMappingSize() const = 0;
83
84 /**
85 * Return a copy of the entry in the vector.
86 *
87 * @param[in] entry - the index into the vector.
88 * @return the tuple at that index.
89 */
90 virtual std::tuple<std::uint32_t, std::string>
91 getI2cEntry(unsigned int entry) const = 0;
Patrick Venturef085d912019-03-15 08:50:00 -070092};
93
94class Handler : public HandlerInterface
95{
96 public:
Patrick Ventureab650002019-03-16 09:08:47 -070097 explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
98 _configFile(entityConfigPath){};
Patrick Venturef085d912019-03-15 08:50:00 -070099 ~Handler() = default;
100
101 std::tuple<std::uint8_t, std::string> getEthDetails() const override;
Patrick Ventured2037c62019-03-15 10:29:47 -0700102 std::int64_t getRxPackets(const std::string& name) const override;
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700103 VersionTuple getCpldVersion(unsigned int id) const override;
Patrick Ventureaa374122019-03-15 15:09:10 -0700104 void psuResetDelay(std::uint32_t delay) const override;
Patrick Venture07f85152019-03-15 21:36:56 -0700105 std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
Patrick Venture49f23ad2019-03-16 11:59:55 -0700106 void buildI2cPcieMapping() override;
107 size_t getI2cPcieMappingSize() const override;
108 std::tuple<std::uint32_t, std::string>
109 getI2cEntry(unsigned int entry) const override;
Patrick Ventureab650002019-03-16 09:08:47 -0700110
111 private:
112 std::string _configFile;
113
114 bool _entityConfigParsed = false;
115
116 const std::map<uint8_t, std::string> _entityIdToName{
117 {0x03, "cpu"},
118 {0x04, "storage_device"},
119 {0x06, "system_management_module"},
120 {0x08, "memory_module"},
121 {0x0B, "add_in_card"},
122 {0x17, "system_chassis"},
123 {0x20, "memory_device"}};
124
125 nlohmann::json _entityConfig{};
Patrick Venture49f23ad2019-03-16 11:59:55 -0700126
127 std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
Patrick Venturef085d912019-03-15 08:50:00 -0700128};
129
Patrick Ventureab650002019-03-16 09:08:47 -0700130/**
131 * Given a type, entity instance, and a configuration, return the name.
132 *
133 * @param[in] type - the entity type
134 * @param[in] instance - the entity instance
135 * @param[in] config - the json object holding the entity mapping
136 * @return the name of the entity from the map
137 */
138std::string readNameFromConfig(const std::string& type, uint8_t instance,
139 const nlohmann::json& config);
140
Patrick Venturef085d912019-03-15 08:50:00 -0700141extern Handler handlerImpl;
142
143} // namespace ipmi
144} // namespace google