Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstdint> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 4 | #include <map> |
| 5 | #include <nlohmann/json.hpp> |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <tuple> |
| 8 | |
| 9 | namespace google |
| 10 | { |
| 11 | namespace ipmi |
| 12 | { |
| 13 | |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 14 | using VersionTuple = |
| 15 | std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>; |
| 16 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 17 | extern const std::string defaultConfigFile; |
| 18 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 19 | class 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 Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 30 | |
| 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 Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 39 | |
| 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 Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 48 | |
| 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 Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 56 | |
| 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 Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | class Handler : public HandlerInterface |
| 73 | { |
| 74 | public: |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 75 | explicit Handler(const std::string& entityConfigPath = defaultConfigFile) : |
| 76 | _configFile(entityConfigPath){}; |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 77 | ~Handler() = default; |
| 78 | |
| 79 | std::tuple<std::uint8_t, std::string> getEthDetails() const override; |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 80 | std::int64_t getRxPackets(const std::string& name) const override; |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 81 | VersionTuple getCpldVersion(unsigned int id) const override; |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 82 | void psuResetDelay(std::uint32_t delay) const override; |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 83 | std::string getEntityName(std::uint8_t id, std::uint8_t instance) override; |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 84 | |
| 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 Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 102 | /** |
| 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 | */ |
| 110 | std::string readNameFromConfig(const std::string& type, uint8_t instance, |
| 111 | const nlohmann::json& config); |
| 112 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 113 | extern Handler handlerImpl; |
| 114 | |
| 115 | } // namespace ipmi |
| 116 | } // namespace google |