1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "bifurcation.hpp"
18 #include "handler.hpp"
19 
20 #include <nlohmann/json.hpp>
21 #include <sdbusplus/bus.hpp>
22 
23 #include <cstdint>
24 #include <map>
25 #include <string>
26 #include <string_view>
27 #include <tuple>
28 #include <vector>
29 
30 namespace google
31 {
32 namespace ipmi
33 {
34 
35 constexpr char defaultConfigFile[] =
36     "/usr/share/ipmi-entity-association/entity_association_map.json";
37 
38 class Handler : public HandlerInterface
39 {
40   public:
41     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
42         _configFile(entityConfigPath),
43         bifurcationHelper(BifurcationStatic::createBifurcation()){};
44     Handler(std::reference_wrapper<BifurcationInterface> bifurcationHelper,
45             const std::string& entityConfigPath = defaultConfigFile) :
46         _configFile(entityConfigPath),
47         bifurcationHelper(bifurcationHelper){};
48     ~Handler() = default;
49 
50     uint8_t getBmcMode() override;
51     std::tuple<std::uint8_t, std::string>
52         getEthDetails(std::string intf) const override;
53     std::int64_t getRxPackets(const std::string& name) const override;
54     VersionTuple getCpldVersion(unsigned int id) const override;
55     void psuResetDelay(std::uint32_t delay) const override;
56     void psuResetOnShutdown() const override;
57     std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
58     uint32_t getFlashSize() override;
59     std::string getMachineName() override;
60     void buildI2cPcieMapping() override;
61     size_t getI2cPcieMappingSize() const override;
62     void hostPowerOffDelay(std::uint32_t delay) const override;
63     std::tuple<std::uint32_t, std::string>
64         getI2cEntry(unsigned int entry) const override;
65     std::vector<uint8_t> pcieBifurcation(uint8_t) override;
66 
67     uint32_t accelOobDeviceCount() const override;
68     std::string accelOobDeviceName(size_t index) const override;
69     uint64_t accelOobRead(std::string_view name, uint64_t address,
70                           uint8_t num_bytes) const override;
71     void accelOobWrite(std::string_view name, uint64_t address,
72                        uint8_t num_bytes, uint64_t data) const override;
73     void linuxBootDone() const override;
74 
75   protected:
76     // Exposed for dependency injection
77     virtual sdbusplus::bus_t getDbus() const;
78 
79   private:
80     std::string _configFile;
81 
82     bool _entityConfigParsed = false;
83 
84     const std::map<uint8_t, std::string> _entityIdToName{
85         {0x03, "cpu"},
86         {0x04, "storage_device"},
87         {0x06, "system_management_module"},
88         {0x07, "system_board"},
89         {0x08, "memory_module"},
90         {0x0B, "add_in_card"},
91         {0x0E, "power_system_board"},
92         {0x10, "system_internal_expansion_board"},
93         {0x11, "other_system_board"},
94         {0x17, "system_chassis"},
95         {0x1D, "fan"},
96         {0x1E, "cooling_unit"},
97         {0x20, "memory_device"}};
98 
99     nlohmann::json _entityConfig{};
100 
101     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
102 
103     std::reference_wrapper<BifurcationInterface> bifurcationHelper;
104 };
105 
106 /**
107  * Given a type, entity instance, and a configuration, return the name.
108  *
109  * @param[in] type - the entity type
110  * @param[in] instance - the entity instance
111  * @param[in] config - the json object holding the entity mapping
112  * @return the name of the entity from the map
113  */
114 std::string readNameFromConfig(const std::string& type, uint8_t instance,
115                                const nlohmann::json& config);
116 
117 } // namespace ipmi
118 } // namespace google
119