split out handler implementation from interface
Split out the handler object definition from the interface and drop the
default parameters.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: Ie8c9dbf7881bbe6b5af3596a60430e3b3d573762
diff --git a/cable.hpp b/cable.hpp
index 6e559c0..58b82ce 100644
--- a/cable.hpp
+++ b/cable.hpp
@@ -19,7 +19,7 @@
// Handle the cablecheck. Sys must supply which ethernet device they're
// interested in.
ipmi_ret_t cableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen,
- const HandlerInterface* handler = &handlerImpl);
+ const HandlerInterface* handler);
} // namespace ipmi
} // namespace google
diff --git a/cpld.hpp b/cpld.hpp
index 5c747ed..4c8e059 100644
--- a/cpld.hpp
+++ b/cpld.hpp
@@ -11,8 +11,7 @@
// Given a cpld identifier, return a version if available.
ipmi_ret_t cpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf,
- size_t* dataLen,
- const HandlerInterface* handler = &handlerImpl);
+ size_t* dataLen, const HandlerInterface* handler);
} // namespace ipmi
} // namespace google
diff --git a/entity_name.hpp b/entity_name.hpp
index c04b996..5ed442b 100644
--- a/entity_name.hpp
+++ b/entity_name.hpp
@@ -12,8 +12,7 @@
// Handle the "entity id:entity instance" to entity name mapping command.
// Sys can query the entity name for a particular "entity id:entity instance".
ipmi_ret_t getEntityName(const uint8_t* reqBuf, uint8_t* replyBuf,
- size_t* dataLen,
- HandlerInterface* handler = &handlerImpl);
+ size_t* dataLen, HandlerInterface* handler);
} // namespace ipmi
} // namespace google
diff --git a/eth.hpp b/eth.hpp
index fee363d..4454818 100644
--- a/eth.hpp
+++ b/eth.hpp
@@ -25,8 +25,7 @@
// Sys can query the ifName and IPMI channel of the BMC's NCSI ethernet
// device.
ipmi_ret_t getEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
- size_t* dataLen,
- const HandlerInterface* handler = &handlerImpl);
+ size_t* dataLen, const HandlerInterface* handler);
} // namespace ipmi
} // namespace google
diff --git a/handler.cpp b/handler.cpp
index 341e195..4605e2f 100644
--- a/handler.cpp
+++ b/handler.cpp
@@ -17,6 +17,7 @@
#include "handler.hpp"
#include "errors.hpp"
+#include "handler_impl.hpp"
#include "util.hpp"
#include <ipmid/api.h>
@@ -276,7 +277,5 @@
const std::string defaultConfigFile =
"/usr/share/ipmi-entity-association/entity_association_map.json";
-Handler handlerImpl;
-
} // namespace ipmi
} // namespace google
diff --git a/handler.hpp b/handler.hpp
index cc16448..36a377b 100644
--- a/handler.hpp
+++ b/handler.hpp
@@ -15,8 +15,6 @@
using VersionTuple =
std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
-extern const std::string defaultConfigFile;
-
class HandlerInterface
{
public:
@@ -91,48 +89,6 @@
getI2cEntry(unsigned int entry) const = 0;
};
-class Handler : public HandlerInterface
-{
- public:
- explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
- _configFile(entityConfigPath){};
- ~Handler() = default;
-
- std::tuple<std::uint8_t, std::string> getEthDetails() const override;
- std::int64_t getRxPackets(const std::string& name) const override;
- VersionTuple getCpldVersion(unsigned int id) const override;
- void psuResetDelay(std::uint32_t delay) const override;
- std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
- void buildI2cPcieMapping() override;
- size_t getI2cPcieMappingSize() const override;
- std::tuple<std::uint32_t, std::string>
- getI2cEntry(unsigned int entry) const override;
-
- private:
- std::string _configFile;
-
- bool _entityConfigParsed = false;
-
- const std::map<uint8_t, std::string> _entityIdToName{
- {0x03, "cpu"},
- {0x04, "storage_device"},
- {0x06, "system_management_module"},
- {0x07, "system_board"},
- {0x08, "memory_module"},
- {0x0B, "add_in_card"},
- {0x0E, "power_system_board"},
- {0x10, "system_internal_expansion_board"},
- {0x11, "other_system_board"},
- {0x17, "system_chassis"},
- {0x1D, "fan"},
- {0x1E, "cooling_unit"},
- {0x20, "memory_device"}};
-
- nlohmann::json _entityConfig{};
-
- std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
-};
-
/**
* Given a type, entity instance, and a configuration, return the name.
*
@@ -144,7 +100,5 @@
std::string readNameFromConfig(const std::string& type, uint8_t instance,
const nlohmann::json& config);
-extern Handler handlerImpl;
-
} // namespace ipmi
} // namespace google
diff --git a/handler_impl.hpp b/handler_impl.hpp
new file mode 100644
index 0000000..08452d7
--- /dev/null
+++ b/handler_impl.hpp
@@ -0,0 +1,62 @@
+#pragma once
+
+#include "handler.hpp"
+
+#include <cstdint>
+#include <map>
+#include <nlohmann/json.hpp>
+#include <string>
+#include <tuple>
+#include <vector>
+
+namespace google
+{
+namespace ipmi
+{
+
+extern const std::string defaultConfigFile;
+
+class Handler : public HandlerInterface
+{
+ public:
+ explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
+ _configFile(entityConfigPath){};
+ ~Handler() = default;
+
+ std::tuple<std::uint8_t, std::string> getEthDetails() const override;
+ std::int64_t getRxPackets(const std::string& name) const override;
+ VersionTuple getCpldVersion(unsigned int id) const override;
+ void psuResetDelay(std::uint32_t delay) const override;
+ std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
+ void buildI2cPcieMapping() override;
+ size_t getI2cPcieMappingSize() const override;
+ std::tuple<std::uint32_t, std::string>
+ getI2cEntry(unsigned int entry) const override;
+
+ private:
+ std::string _configFile;
+
+ bool _entityConfigParsed = false;
+
+ const std::map<uint8_t, std::string> _entityIdToName{
+ {0x03, "cpu"},
+ {0x04, "storage_device"},
+ {0x06, "system_management_module"},
+ {0x07, "system_board"},
+ {0x08, "memory_module"},
+ {0x0B, "add_in_card"},
+ {0x0E, "power_system_board"},
+ {0x10, "system_internal_expansion_board"},
+ {0x11, "other_system_board"},
+ {0x17, "system_chassis"},
+ {0x1D, "fan"},
+ {0x1E, "cooling_unit"},
+ {0x20, "memory_device"}};
+
+ nlohmann::json _entityConfig{};
+
+ std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
+};
+
+} // namespace ipmi
+} // namespace google
diff --git a/main.cpp b/main.cpp
index 372cc04..f1cb516 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,6 +20,7 @@
#include "cpld.hpp"
#include "entity_name.hpp"
#include "eth.hpp"
+#include "handler_impl.hpp"
#include "pcie_i2c.hpp"
#include "psu.hpp"
@@ -43,6 +44,8 @@
namespace ipmi
{
+Handler handlerImpl;
+
static ipmi_ret_t handleSysCommand(ipmi_cmd_t cmd, const uint8_t* reqBuf,
uint8_t* replyCmdBuf, size_t* dataLen)
{
@@ -57,19 +60,20 @@
switch (reqBuf[0])
{
case SysCableCheck:
- return cableCheck(reqBuf, replyCmdBuf, dataLen);
+ return cableCheck(reqBuf, replyCmdBuf, dataLen, &handlerImpl);
case SysCpldVersion:
- return cpldVersion(reqBuf, replyCmdBuf, dataLen);
+ return cpldVersion(reqBuf, replyCmdBuf, dataLen, &handlerImpl);
case SysGetEthDevice:
- return getEthDevice(reqBuf, replyCmdBuf, dataLen);
+ return getEthDevice(reqBuf, replyCmdBuf, dataLen, &handlerImpl);
case SysPsuHardReset:
- return psuHardReset(reqBuf, replyCmdBuf, dataLen);
+ return psuHardReset(reqBuf, replyCmdBuf, dataLen, &handlerImpl);
case SysPcieSlotCount:
- return pcieSlotCount(reqBuf, replyCmdBuf, dataLen);
+ return pcieSlotCount(reqBuf, replyCmdBuf, dataLen, &handlerImpl);
case SysPcieSlotI2cBusMapping:
- return pcieSlotI2cBusMapping(reqBuf, replyCmdBuf, dataLen);
+ return pcieSlotI2cBusMapping(reqBuf, replyCmdBuf, dataLen,
+ &handlerImpl);
case SysEntityName:
- return getEntityName(reqBuf, replyCmdBuf, dataLen);
+ return getEntityName(reqBuf, replyCmdBuf, dataLen, &handlerImpl);
default:
std::fprintf(stderr, "Invalid subcommand: 0x%x\n", reqBuf[0]);
return IPMI_CC_INVALID;
diff --git a/pcie_i2c.hpp b/pcie_i2c.hpp
index ef89865..556cd23 100644
--- a/pcie_i2c.hpp
+++ b/pcie_i2c.hpp
@@ -12,14 +12,12 @@
// Handle the pcie slot count command.
// Sys can query the number of pcie slots.
ipmi_ret_t pcieSlotCount(const uint8_t* reqBuf, uint8_t* replyBuf,
- size_t* dataLen,
- HandlerInterface* handler = &handlerImpl);
+ size_t* dataLen, HandlerInterface* handler);
// Handle the pcie slot to i2c bus mapping command.
// Sys can query which i2c bus is routed to which pcie slot.
ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf,
- size_t* dataLen,
- HandlerInterface* handler = &handlerImpl);
+ size_t* dataLen, HandlerInterface* handler);
} // namespace ipmi
} // namespace google
diff --git a/psu.hpp b/psu.hpp
index d2c5fbe..d45ce7f 100644
--- a/psu.hpp
+++ b/psu.hpp
@@ -18,8 +18,7 @@
// Set a time-delayed PSU hard reset.
ipmi_ret_t psuHardReset(const uint8_t* reqBuf, uint8_t* replyBuf,
- size_t* dataLen,
- const HandlerInterface* handler = &handlerImpl);
+ size_t* dataLen, const HandlerInterface* handler);
} // namespace ipmi
} // namespace google
diff --git a/test/handler_unittest.cpp b/test/handler_unittest.cpp
index a4f9281..99782fa 100644
--- a/test/handler_unittest.cpp
+++ b/test/handler_unittest.cpp
@@ -1,5 +1,6 @@
#include "errors.hpp"
#include "handler.hpp"
+#include "handler_impl.hpp"
#include <fstream>
#include <nlohmann/json.hpp>