| 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> | 
| Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 8 | #include <vector> | 
| Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 9 |  | 
|  | 10 | namespace google | 
|  | 11 | { | 
|  | 12 | namespace ipmi | 
|  | 13 | { | 
|  | 14 |  | 
| Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 15 | using VersionTuple = | 
|  | 16 | std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>; | 
|  | 17 |  | 
| Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 18 | extern const std::string defaultConfigFile; | 
|  | 19 |  | 
| Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 20 | class 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 Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 31 |  | 
|  | 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 Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 40 |  | 
|  | 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 Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 49 |  | 
|  | 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 Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 57 |  | 
|  | 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 Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 71 |  | 
|  | 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 Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 92 | }; | 
|  | 93 |  | 
|  | 94 | class Handler : public HandlerInterface | 
|  | 95 | { | 
|  | 96 | public: | 
| Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 97 | explicit Handler(const std::string& entityConfigPath = defaultConfigFile) : | 
|  | 98 | _configFile(entityConfigPath){}; | 
| Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 99 | ~Handler() = default; | 
|  | 100 |  | 
|  | 101 | std::tuple<std::uint8_t, std::string> getEthDetails() const override; | 
| Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 102 | std::int64_t getRxPackets(const std::string& name) const override; | 
| Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 103 | VersionTuple getCpldVersion(unsigned int id) const override; | 
| Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 104 | void psuResetDelay(std::uint32_t delay) const override; | 
| Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 105 | std::string getEntityName(std::uint8_t id, std::uint8_t instance) override; | 
| Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 106 | 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 Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 110 |  | 
|  | 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 Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map; | 
| Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 128 | }; | 
|  | 129 |  | 
| Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 130 | /** | 
|  | 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 | */ | 
|  | 138 | std::string readNameFromConfig(const std::string& type, uint8_t instance, | 
|  | 139 | const nlohmann::json& config); | 
|  | 140 |  | 
| Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 141 | extern Handler handlerImpl; | 
|  | 142 |  | 
|  | 143 | } // namespace ipmi | 
|  | 144 | } // namespace google |