1*a2056e9cSWilly Tu // Copyright 2021 Google LLC
2*a2056e9cSWilly Tu //
3*a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4*a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5*a2056e9cSWilly Tu // You may obtain a copy of the License at
6*a2056e9cSWilly Tu //
7*a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8*a2056e9cSWilly Tu //
9*a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10*a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11*a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13*a2056e9cSWilly Tu // limitations under the License.
14*a2056e9cSWilly Tu 
15c87de558SPatrick Venture #pragma once
16c87de558SPatrick Venture 
17c87de558SPatrick Venture #include "handler.hpp"
18c87de558SPatrick Venture 
19c87de558SPatrick Venture #include <cstdint>
20c87de558SPatrick Venture #include <map>
21c87de558SPatrick Venture #include <nlohmann/json.hpp>
22c87de558SPatrick Venture #include <string>
23c87de558SPatrick Venture #include <tuple>
24c87de558SPatrick Venture #include <vector>
25c87de558SPatrick Venture 
26c87de558SPatrick Venture namespace google
27c87de558SPatrick Venture {
28c87de558SPatrick Venture namespace ipmi
29c87de558SPatrick Venture {
30c87de558SPatrick Venture 
3196ad9069SWilliam A. Kennington III constexpr char defaultConfigFile[] =
3296ad9069SWilliam A. Kennington III     "/usr/share/ipmi-entity-association/entity_association_map.json";
33c87de558SPatrick Venture 
34c87de558SPatrick Venture class Handler : public HandlerInterface
35c87de558SPatrick Venture {
36c87de558SPatrick Venture   public:
37c87de558SPatrick Venture     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
38c87de558SPatrick Venture         _configFile(entityConfigPath){};
39c87de558SPatrick Venture     ~Handler() = default;
40c87de558SPatrick Venture 
41b69209b4SWilliam A. Kennington III     std::tuple<std::uint8_t, std::string>
42b69209b4SWilliam A. Kennington III         getEthDetails(std::string intf) const override;
43c87de558SPatrick Venture     std::int64_t getRxPackets(const std::string& name) const override;
44c87de558SPatrick Venture     VersionTuple getCpldVersion(unsigned int id) const override;
45c87de558SPatrick Venture     void psuResetDelay(std::uint32_t delay) const override;
46ac4a16f7SShounak Mitra     void psuResetOnShutdown() const override;
47c87de558SPatrick Venture     std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
483b1b427cSWilly Tu     uint32_t getFlashSize() override;
4929f35bceSWilliam A. Kennington III     std::string getMachineName() override;
50c87de558SPatrick Venture     void buildI2cPcieMapping() override;
51c87de558SPatrick Venture     size_t getI2cPcieMappingSize() const override;
528cfa4c44Slinyuny     void hostPowerOffDelay(std::uint32_t delay) const override;
53c87de558SPatrick Venture     std::tuple<std::uint32_t, std::string>
54c87de558SPatrick Venture         getI2cEntry(unsigned int entry) const override;
55c87de558SPatrick Venture 
56c87de558SPatrick Venture   private:
57c87de558SPatrick Venture     std::string _configFile;
58c87de558SPatrick Venture 
59c87de558SPatrick Venture     bool _entityConfigParsed = false;
60c87de558SPatrick Venture 
61c87de558SPatrick Venture     const std::map<uint8_t, std::string> _entityIdToName{
62c87de558SPatrick Venture         {0x03, "cpu"},
63c87de558SPatrick Venture         {0x04, "storage_device"},
64c87de558SPatrick Venture         {0x06, "system_management_module"},
65c87de558SPatrick Venture         {0x07, "system_board"},
66c87de558SPatrick Venture         {0x08, "memory_module"},
67c87de558SPatrick Venture         {0x0B, "add_in_card"},
68c87de558SPatrick Venture         {0x0E, "power_system_board"},
69c87de558SPatrick Venture         {0x10, "system_internal_expansion_board"},
70c87de558SPatrick Venture         {0x11, "other_system_board"},
71c87de558SPatrick Venture         {0x17, "system_chassis"},
72c87de558SPatrick Venture         {0x1D, "fan"},
73c87de558SPatrick Venture         {0x1E, "cooling_unit"},
74c87de558SPatrick Venture         {0x20, "memory_device"}};
75c87de558SPatrick Venture 
76c87de558SPatrick Venture     nlohmann::json _entityConfig{};
77c87de558SPatrick Venture 
78c87de558SPatrick Venture     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
79c87de558SPatrick Venture };
80c87de558SPatrick Venture 
81a3f9c2d3SPatrick Venture /**
82a3f9c2d3SPatrick Venture  * Given a type, entity instance, and a configuration, return the name.
83a3f9c2d3SPatrick Venture  *
84a3f9c2d3SPatrick Venture  * @param[in] type - the entity type
85a3f9c2d3SPatrick Venture  * @param[in] instance - the entity instance
86a3f9c2d3SPatrick Venture  * @param[in] config - the json object holding the entity mapping
87a3f9c2d3SPatrick Venture  * @return the name of the entity from the map
88a3f9c2d3SPatrick Venture  */
89a3f9c2d3SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
90a3f9c2d3SPatrick Venture                                const nlohmann::json& config);
91a3f9c2d3SPatrick Venture 
92c87de558SPatrick Venture } // namespace ipmi
93c87de558SPatrick Venture } // namespace google
94