Add abstract factory to create button iface objects
A abstract factory class is implemented to return
the instance of button interface class based on the
button iface formfactor name provided as
parameter to the abstract factory createInstance
method.
Signed-off-by: Naveen Moses <naveen.mosess@hcl.com>
Change-Id: Ia791a2b6f52d09dd87da0e50a709fc72ac9d1bd7
diff --git a/src/main.cpp b/src/main.cpp
index ca7fbaa..a99c603 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -14,14 +14,12 @@
// limitations under the License.
*/
+#include "button_factory.hpp"
#include "gpio.hpp"
-#include "id_button.hpp"
-#include "power_button.hpp"
-#include "reset_button.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
-
+#include <phosphor-logging/elog-errors.hpp>
static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
int main(int argc, char* argv[])
@@ -29,7 +27,7 @@
int ret = 0;
phosphor::logging::log<phosphor::logging::level::INFO>(
- "Start power button service...");
+ "Start Phosphor buttons service...");
sd_event* event = nullptr;
ret = sd_event_default(&event);
@@ -47,6 +45,8 @@
bus, "/xyz/openbmc_project/Chassis/Buttons"};
bus.request_name("xyz.openbmc_project.Chassis.Buttons");
+ //
+ std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces;
std::ifstream gpios{gpioDefFile};
auto json = nlohmann::json::parse(gpios, nullptr, true);
@@ -54,9 +54,6 @@
// load gpio config from gpio defs json file and create button interface
// objects based on the button form factor type
- std::unique_ptr<PowerButton> pb;
- std::unique_ptr<ResetButton> rb;
- std::unique_ptr<IDButton> ib;
for (auto groupGpioConfig : gpioDefs)
{
@@ -72,23 +69,9 @@
buttonCfg.formFactorName = formFactorName;
buttonCfg.gpios.push_back(gpioCfg);
}
- if (buttonCfg.formFactorName == PowerButton::getGpioName())
- {
- pb = std::make_unique<PowerButton>(bus, POWER_DBUS_OBJECT_NAME,
- eventP, buttonCfg);
- }
- if (buttonCfg.formFactorName == ResetButton::getGpioName())
- {
- rb = std::make_unique<ResetButton>(bus, RESET_DBUS_OBJECT_NAME,
- eventP, buttonCfg);
- }
-
- if (buttonCfg.formFactorName == IDButton::getGpioName())
- {
- ib = std::make_unique<IDButton>(bus, ID_DBUS_OBJECT_NAME, eventP,
- buttonCfg);
- }
+ buttonInterfaces.emplace_back(ButtonFactory::instance().createInstance(
+ formFactorName, bus, eventP, buttonCfg));
}
try