1*4f0d1de6SSteve Foreman // Copyright 2022 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 
17c87de558SPatrick Venture #include "handler.hpp"
18c87de558SPatrick Venture 
19c87de558SPatrick Venture #include <cstdint>
20c87de558SPatrick Venture #include <map>
21c87de558SPatrick Venture #include <nlohmann/json.hpp>
22*4f0d1de6SSteve Foreman #include <sdbusplus/bus.hpp>
23c87de558SPatrick Venture #include <string>
24*4f0d1de6SSteve Foreman #include <string_view>
25c87de558SPatrick Venture #include <tuple>
26c87de558SPatrick Venture #include <vector>
27c87de558SPatrick Venture 
28c87de558SPatrick Venture namespace google
29c87de558SPatrick Venture {
30c87de558SPatrick Venture namespace ipmi
31c87de558SPatrick Venture {
32c87de558SPatrick Venture 
3396ad9069SWilliam A. Kennington III constexpr char defaultConfigFile[] =
3496ad9069SWilliam A. Kennington III     "/usr/share/ipmi-entity-association/entity_association_map.json";
35c87de558SPatrick Venture 
36c87de558SPatrick Venture class Handler : public HandlerInterface
37c87de558SPatrick Venture {
38c87de558SPatrick Venture   public:
39c87de558SPatrick Venture     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
40c87de558SPatrick Venture         _configFile(entityConfigPath){};
41c87de558SPatrick Venture     ~Handler() = default;
42c87de558SPatrick Venture 
43b69209b4SWilliam A. Kennington III     std::tuple<std::uint8_t, std::string>
44b69209b4SWilliam A. Kennington III         getEthDetails(std::string intf) const override;
45c87de558SPatrick Venture     std::int64_t getRxPackets(const std::string& name) const override;
46c87de558SPatrick Venture     VersionTuple getCpldVersion(unsigned int id) const override;
47c87de558SPatrick Venture     void psuResetDelay(std::uint32_t delay) const override;
48ac4a16f7SShounak Mitra     void psuResetOnShutdown() const override;
49c87de558SPatrick Venture     std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
503b1b427cSWilly Tu     uint32_t getFlashSize() override;
5129f35bceSWilliam A. Kennington III     std::string getMachineName() override;
52c87de558SPatrick Venture     void buildI2cPcieMapping() override;
53c87de558SPatrick Venture     size_t getI2cPcieMappingSize() const override;
548cfa4c44Slinyuny     void hostPowerOffDelay(std::uint32_t delay) const override;
55c87de558SPatrick Venture     std::tuple<std::uint32_t, std::string>
56c87de558SPatrick Venture         getI2cEntry(unsigned int entry) const override;
57c87de558SPatrick Venture 
58*4f0d1de6SSteve Foreman     uint32_t accelOobDeviceCount() const override;
59*4f0d1de6SSteve Foreman     std::string accelOobDeviceName(size_t index) const override;
60*4f0d1de6SSteve Foreman     uint64_t accelOobRead(std::string_view name, uint64_t address,
61*4f0d1de6SSteve Foreman                           uint8_t num_bytes) const override;
62*4f0d1de6SSteve Foreman     void accelOobWrite(std::string_view name, uint64_t address,
63*4f0d1de6SSteve Foreman                        uint8_t num_bytes, uint64_t data) const override;
64*4f0d1de6SSteve Foreman 
65*4f0d1de6SSteve Foreman   protected:
66*4f0d1de6SSteve Foreman     // Exposed for dependency injection
67*4f0d1de6SSteve Foreman     virtual sdbusplus::bus::bus accelOobGetDbus() const;
68*4f0d1de6SSteve Foreman 
69c87de558SPatrick Venture   private:
70c87de558SPatrick Venture     std::string _configFile;
71c87de558SPatrick Venture 
72c87de558SPatrick Venture     bool _entityConfigParsed = false;
73c87de558SPatrick Venture 
74c87de558SPatrick Venture     const std::map<uint8_t, std::string> _entityIdToName{
75c87de558SPatrick Venture         {0x03, "cpu"},
76c87de558SPatrick Venture         {0x04, "storage_device"},
77c87de558SPatrick Venture         {0x06, "system_management_module"},
78c87de558SPatrick Venture         {0x07, "system_board"},
79c87de558SPatrick Venture         {0x08, "memory_module"},
80c87de558SPatrick Venture         {0x0B, "add_in_card"},
81c87de558SPatrick Venture         {0x0E, "power_system_board"},
82c87de558SPatrick Venture         {0x10, "system_internal_expansion_board"},
83c87de558SPatrick Venture         {0x11, "other_system_board"},
84c87de558SPatrick Venture         {0x17, "system_chassis"},
85c87de558SPatrick Venture         {0x1D, "fan"},
86c87de558SPatrick Venture         {0x1E, "cooling_unit"},
87c87de558SPatrick Venture         {0x20, "memory_device"}};
88c87de558SPatrick Venture 
89c87de558SPatrick Venture     nlohmann::json _entityConfig{};
90c87de558SPatrick Venture 
91c87de558SPatrick Venture     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
92c87de558SPatrick Venture };
93c87de558SPatrick Venture 
94a3f9c2d3SPatrick Venture /**
95a3f9c2d3SPatrick Venture  * Given a type, entity instance, and a configuration, return the name.
96a3f9c2d3SPatrick Venture  *
97a3f9c2d3SPatrick Venture  * @param[in] type - the entity type
98a3f9c2d3SPatrick Venture  * @param[in] instance - the entity instance
99a3f9c2d3SPatrick Venture  * @param[in] config - the json object holding the entity mapping
100a3f9c2d3SPatrick Venture  * @return the name of the entity from the map
101a3f9c2d3SPatrick Venture  */
102a3f9c2d3SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
103a3f9c2d3SPatrick Venture                                const nlohmann::json& config);
104a3f9c2d3SPatrick Venture 
105c87de558SPatrick Venture } // namespace ipmi
106c87de558SPatrick Venture } // namespace google
107