Enable group gpio configuration
This change adds support to configure single
as well as group gpio config using a single api(configGroupGpio()).
This change is introduced to support the button/input
interfaces which has multiple gpios associated with them.
As an improvement reading of gpio def json file is
done once in main function rather than reading it
everytime before creating the button interface object.
Signed-off-by: Naveen Moses <naveen.mosess@hcl.com>
Change-Id: Ib73dda618c78fd2f14b5d3432fd04c9f4cd2dd9b
diff --git a/src/main.cpp b/src/main.cpp
index 27f9b6f..ca7fbaa 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -14,10 +14,16 @@
// limitations under the License.
*/
+#include "gpio.hpp"
#include "id_button.hpp"
#include "power_button.hpp"
#include "reset_button.hpp"
+#include <fstream>
+#include <nlohmann/json.hpp>
+
+static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
+
int main(int argc, char* argv[])
{
int ret = 0;
@@ -42,22 +48,47 @@
bus.request_name("xyz.openbmc_project.Chassis.Buttons");
+ std::ifstream gpios{gpioDefFile};
+ auto json = nlohmann::json::parse(gpios, nullptr, true);
+ auto gpioDefs = json["gpio_definitions"];
+
+ // 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;
- if (hasGpio<PowerButton>())
- {
- pb = std::make_unique<PowerButton>(bus, POWER_DBUS_OBJECT_NAME, eventP);
- }
-
std::unique_ptr<ResetButton> rb;
- if (hasGpio<ResetButton>())
- {
- rb = std::make_unique<ResetButton>(bus, RESET_DBUS_OBJECT_NAME, eventP);
- }
-
std::unique_ptr<IDButton> ib;
- if (hasGpio<IDButton>())
+
+ for (auto groupGpioConfig : gpioDefs)
{
- ib = std::make_unique<IDButton>(bus, ID_DBUS_OBJECT_NAME, eventP);
+ std::string formFactorName = groupGpioConfig["name"];
+ buttonConfig buttonCfg;
+ auto groupGpio = groupGpioConfig["gpio_config"];
+
+ for (auto gpioConfig : groupGpio)
+ {
+ gpioInfo gpioCfg;
+ gpioCfg.number = getGpioNum(gpioConfig["pin"]);
+ gpioCfg.direction = gpioConfig["direction"];
+ 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);
+ }
}
try