1*15d4d21cSHao Zhou // Copyright 2023 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
14a2056e9cSWilly Tu 
15c87de558SPatrick Venture #pragma once
16c87de558SPatrick Venture 
176c71b0f9SWilly Tu #include "bifurcation.hpp"
18*15d4d21cSHao Zhou #include "file_system_wrapper_impl.hpp"
19c87de558SPatrick Venture #include "handler.hpp"
20c87de558SPatrick Venture 
21c87de558SPatrick Venture #include <nlohmann/json.hpp>
224f0d1de6SSteve Foreman #include <sdbusplus/bus.hpp>
23444b5ea4SPatrick Williams 
24444b5ea4SPatrick Williams #include <cstdint>
25444b5ea4SPatrick Williams #include <map>
26*15d4d21cSHao Zhou #include <memory>
27c87de558SPatrick Venture #include <string>
284f0d1de6SSteve Foreman #include <string_view>
29c87de558SPatrick Venture #include <tuple>
30c87de558SPatrick Venture #include <vector>
31c87de558SPatrick Venture 
32c87de558SPatrick Venture namespace google
33c87de558SPatrick Venture {
34c87de558SPatrick Venture namespace ipmi
35c87de558SPatrick Venture {
36c87de558SPatrick Venture 
3796ad9069SWilliam A. Kennington III constexpr char defaultConfigFile[] =
3896ad9069SWilliam A. Kennington III     "/usr/share/ipmi-entity-association/entity_association_map.json";
39c87de558SPatrick Venture 
40c87de558SPatrick Venture class Handler : public HandlerInterface
41c87de558SPatrick Venture {
42c87de558SPatrick Venture   public:
43c87de558SPatrick Venture     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
44*15d4d21cSHao Zhou         fsPtr(std::make_unique<FileSystemWrapper>()),
456c71b0f9SWilly Tu         _configFile(entityConfigPath),
466c71b0f9SWilly Tu         bifurcationHelper(BifurcationStatic::createBifurcation()){};
476c71b0f9SWilly Tu     Handler(std::reference_wrapper<BifurcationInterface> bifurcationHelper,
486c71b0f9SWilly Tu             const std::string& entityConfigPath = defaultConfigFile) :
49*15d4d21cSHao Zhou         fsPtr(std::make_unique<FileSystemWrapper>()),
50*15d4d21cSHao Zhou         _configFile(entityConfigPath), bifurcationHelper(bifurcationHelper){};
51c87de558SPatrick Venture     ~Handler() = default;
52c87de558SPatrick Venture 
535e70dc8cSNikhil Namjoshi     uint8_t getBmcMode() override;
54b69209b4SWilliam A. Kennington III     std::tuple<std::uint8_t, std::string>
55b69209b4SWilliam A. Kennington III         getEthDetails(std::string intf) const override;
56c87de558SPatrick Venture     std::int64_t getRxPackets(const std::string& name) const override;
57c87de558SPatrick Venture     VersionTuple getCpldVersion(unsigned int id) const override;
58c87de558SPatrick Venture     void psuResetDelay(std::uint32_t delay) const override;
59ac4a16f7SShounak Mitra     void psuResetOnShutdown() const override;
60c87de558SPatrick Venture     std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
613b1b427cSWilly Tu     uint32_t getFlashSize() override;
6229f35bceSWilliam A. Kennington III     std::string getMachineName() override;
63c87de558SPatrick Venture     void buildI2cPcieMapping() override;
64c87de558SPatrick Venture     size_t getI2cPcieMappingSize() const override;
658cfa4c44Slinyuny     void hostPowerOffDelay(std::uint32_t delay) const override;
66c87de558SPatrick Venture     std::tuple<std::uint32_t, std::string>
67c87de558SPatrick Venture         getI2cEntry(unsigned int entry) const override;
686c71b0f9SWilly Tu     std::vector<uint8_t> pcieBifurcation(uint8_t) override;
69c87de558SPatrick Venture 
704f0d1de6SSteve Foreman     uint32_t accelOobDeviceCount() const override;
714f0d1de6SSteve Foreman     std::string accelOobDeviceName(size_t index) const override;
724f0d1de6SSteve Foreman     uint64_t accelOobRead(std::string_view name, uint64_t address,
734f0d1de6SSteve Foreman                           uint8_t num_bytes) const override;
744f0d1de6SSteve Foreman     void accelOobWrite(std::string_view name, uint64_t address,
754f0d1de6SSteve Foreman                        uint8_t num_bytes, uint64_t data) const override;
76a92d0e6bSJohn Wedig     void linuxBootDone() const override;
774f0d1de6SSteve Foreman 
784f0d1de6SSteve Foreman   protected:
794f0d1de6SSteve Foreman     // Exposed for dependency injection
8065371228SPatrick Williams     virtual sdbusplus::bus_t getDbus() const;
81*15d4d21cSHao Zhou     virtual const std::unique_ptr<FileSystemInterface>& getFs() const;
824f0d1de6SSteve Foreman 
83c87de558SPatrick Venture   private:
84*15d4d21cSHao Zhou     std::unique_ptr<FileSystemInterface> fsPtr;
85*15d4d21cSHao Zhou 
86c87de558SPatrick Venture     std::string _configFile;
87c87de558SPatrick Venture 
88c87de558SPatrick Venture     bool _entityConfigParsed = false;
89c87de558SPatrick Venture 
90c87de558SPatrick Venture     const std::map<uint8_t, std::string> _entityIdToName{
91c87de558SPatrick Venture         {0x03, "cpu"},
92c87de558SPatrick Venture         {0x04, "storage_device"},
93c87de558SPatrick Venture         {0x06, "system_management_module"},
94c87de558SPatrick Venture         {0x07, "system_board"},
95c87de558SPatrick Venture         {0x08, "memory_module"},
96c87de558SPatrick Venture         {0x0B, "add_in_card"},
97c87de558SPatrick Venture         {0x0E, "power_system_board"},
98c87de558SPatrick Venture         {0x10, "system_internal_expansion_board"},
99c87de558SPatrick Venture         {0x11, "other_system_board"},
100c87de558SPatrick Venture         {0x17, "system_chassis"},
101c87de558SPatrick Venture         {0x1D, "fan"},
102c87de558SPatrick Venture         {0x1E, "cooling_unit"},
103c87de558SPatrick Venture         {0x20, "memory_device"}};
104c87de558SPatrick Venture 
105c87de558SPatrick Venture     nlohmann::json _entityConfig{};
106c87de558SPatrick Venture 
107c87de558SPatrick Venture     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
1086c71b0f9SWilly Tu 
1096c71b0f9SWilly Tu     std::reference_wrapper<BifurcationInterface> bifurcationHelper;
110c87de558SPatrick Venture };
111c87de558SPatrick Venture 
112a3f9c2d3SPatrick Venture /**
113a3f9c2d3SPatrick Venture  * Given a type, entity instance, and a configuration, return the name.
114a3f9c2d3SPatrick Venture  *
115a3f9c2d3SPatrick Venture  * @param[in] type - the entity type
116a3f9c2d3SPatrick Venture  * @param[in] instance - the entity instance
117a3f9c2d3SPatrick Venture  * @param[in] config - the json object holding the entity mapping
118a3f9c2d3SPatrick Venture  * @return the name of the entity from the map
119a3f9c2d3SPatrick Venture  */
120a3f9c2d3SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
121a3f9c2d3SPatrick Venture                                const nlohmann::json& config);
122a3f9c2d3SPatrick Venture 
123c87de558SPatrick Venture } // namespace ipmi
124c87de558SPatrick Venture } // namespace google
125