blob: ec776578c8473cfee15c83c90947cccafe24d7ad [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>
8
9namespace google
10{
11namespace ipmi
12{
13
Patrick Venturebb90d4f2019-03-15 13:42:06 -070014using VersionTuple =
15 std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
16
Patrick Ventureab650002019-03-16 09:08:47 -070017extern const std::string defaultConfigFile;
18
Patrick Venturef085d912019-03-15 08:50:00 -070019class HandlerInterface
20{
21 public:
22 virtual ~HandlerInterface() = default;
23
24 /**
25 * Return ethernet details (hard-coded).
26 *
27 * @return tuple of ethernet details (channel, if name).
28 */
29 virtual std::tuple<std::uint8_t, std::string> getEthDetails() const = 0;
Patrick Ventured2037c62019-03-15 10:29:47 -070030
31 /**
32 * Return the value of rx_packets, given a if_name.
33 *
34 * @param[in] name, the interface name.
35 * @return the number of packets received.
36 * @throw IpmiException on failure.
37 */
38 virtual std::int64_t getRxPackets(const std::string& name) const = 0;
Patrick Venturebb90d4f2019-03-15 13:42:06 -070039
40 /**
41 * Return the values from a cpld version file.
42 *
43 * @param[in] id - the cpld id number.
44 * @return the quad of numbers as a tuple (maj,min,pt,subpt)
45 * @throw IpmiException on failure.
46 */
47 virtual VersionTuple getCpldVersion(unsigned int id) const = 0;
Patrick Ventureaa374122019-03-15 15:09:10 -070048
49 /**
50 * Set the PSU Reset delay.
51 *
52 * @param[in] delay - delay in seconds.
53 * @throw IpmiException on failure.
54 */
55 virtual void psuResetDelay(std::uint32_t delay) const = 0;
Patrick Venture07f85152019-03-15 21:36:56 -070056
57 /**
58 * Return the entity name.
59 * On the first call to this method it'll build the list of entities.
60 * @todo Consider moving the list building to construction time (and ignore
61 * failures).
62 *
63 * @param[in] id - the entity id value
64 * @param[in] instance - the entity instance
65 * @return the entity's name
66 * @throw IpmiException on failure.
67 */
68 virtual std::string getEntityName(std::uint8_t id,
69 std::uint8_t instance) = 0;
Patrick Venturef085d912019-03-15 08:50:00 -070070};
71
72class Handler : public HandlerInterface
73{
74 public:
Patrick Ventureab650002019-03-16 09:08:47 -070075 explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
76 _configFile(entityConfigPath){};
Patrick Venturef085d912019-03-15 08:50:00 -070077 ~Handler() = default;
78
79 std::tuple<std::uint8_t, std::string> getEthDetails() const override;
Patrick Ventured2037c62019-03-15 10:29:47 -070080 std::int64_t getRxPackets(const std::string& name) const override;
Patrick Venturebb90d4f2019-03-15 13:42:06 -070081 VersionTuple getCpldVersion(unsigned int id) const override;
Patrick Ventureaa374122019-03-15 15:09:10 -070082 void psuResetDelay(std::uint32_t delay) const override;
Patrick Venture07f85152019-03-15 21:36:56 -070083 std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
Patrick Ventureab650002019-03-16 09:08:47 -070084
85 private:
86 std::string _configFile;
87
88 bool _entityConfigParsed = false;
89
90 const std::map<uint8_t, std::string> _entityIdToName{
91 {0x03, "cpu"},
92 {0x04, "storage_device"},
93 {0x06, "system_management_module"},
94 {0x08, "memory_module"},
95 {0x0B, "add_in_card"},
96 {0x17, "system_chassis"},
97 {0x20, "memory_device"}};
98
99 nlohmann::json _entityConfig{};
Patrick Venturef085d912019-03-15 08:50:00 -0700100};
101
Patrick Ventureab650002019-03-16 09:08:47 -0700102/**
103 * Given a type, entity instance, and a configuration, return the name.
104 *
105 * @param[in] type - the entity type
106 * @param[in] instance - the entity instance
107 * @param[in] config - the json object holding the entity mapping
108 * @return the name of the entity from the map
109 */
110std::string readNameFromConfig(const std::string& type, uint8_t instance,
111 const nlohmann::json& config);
112
Patrick Venturef085d912019-03-15 08:50:00 -0700113extern Handler handlerImpl;
114
115} // namespace ipmi
116} // namespace google