blob: ca7fbaa851de634fa75d4dd8358d3eecb13ccce2 [file] [log] [blame]
Kuiying Wanga9d39e32018-08-14 13:47:32 +08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Naveen Mosesdd5495c2021-12-03 22:40:46 +053017#include "gpio.hpp"
Kuiying Wanga9d39e32018-08-14 13:47:32 +080018#include "id_button.hpp"
Patrick Venture0d9377d2018-11-01 19:34:59 -070019#include "power_button.hpp"
20#include "reset_button.hpp"
Kuiying Wanga9d39e32018-08-14 13:47:32 +080021
Naveen Mosesdd5495c2021-12-03 22:40:46 +053022#include <fstream>
23#include <nlohmann/json.hpp>
24
25static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
26
Kuiying Wanga9d39e32018-08-14 13:47:32 +080027int main(int argc, char* argv[])
28{
29 int ret = 0;
30
31 phosphor::logging::log<phosphor::logging::level::INFO>(
32 "Start power button service...");
33
34 sd_event* event = nullptr;
35 ret = sd_event_default(&event);
36 if (ret < 0)
37 {
38 phosphor::logging::log<phosphor::logging::level::ERR>(
39 "Error creating a default sd_event handler");
40 return ret;
41 }
42 EventPtr eventP{event};
43 event = nullptr;
44
45 sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
46 sdbusplus::server::manager::manager objManager{
47 bus, "/xyz/openbmc_project/Chassis/Buttons"};
48
49 bus.request_name("xyz.openbmc_project.Chassis.Buttons");
50
Naveen Mosesdd5495c2021-12-03 22:40:46 +053051 std::ifstream gpios{gpioDefFile};
52 auto json = nlohmann::json::parse(gpios, nullptr, true);
53 auto gpioDefs = json["gpio_definitions"];
54
55 // load gpio config from gpio defs json file and create button interface
56 // objects based on the button form factor type
Matt Spinler8605bdf2018-11-05 14:55:46 -060057 std::unique_ptr<PowerButton> pb;
Matt Spinler8605bdf2018-11-05 14:55:46 -060058 std::unique_ptr<ResetButton> rb;
Matt Spinler8605bdf2018-11-05 14:55:46 -060059 std::unique_ptr<IDButton> ib;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053060
61 for (auto groupGpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060062 {
Naveen Mosesdd5495c2021-12-03 22:40:46 +053063 std::string formFactorName = groupGpioConfig["name"];
64 buttonConfig buttonCfg;
65 auto groupGpio = groupGpioConfig["gpio_config"];
66
67 for (auto gpioConfig : groupGpio)
68 {
69 gpioInfo gpioCfg;
70 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
71 gpioCfg.direction = gpioConfig["direction"];
72 buttonCfg.formFactorName = formFactorName;
73 buttonCfg.gpios.push_back(gpioCfg);
74 }
75 if (buttonCfg.formFactorName == PowerButton::getGpioName())
76 {
77 pb = std::make_unique<PowerButton>(bus, POWER_DBUS_OBJECT_NAME,
78 eventP, buttonCfg);
79 }
80
81 if (buttonCfg.formFactorName == ResetButton::getGpioName())
82 {
83 rb = std::make_unique<ResetButton>(bus, RESET_DBUS_OBJECT_NAME,
84 eventP, buttonCfg);
85 }
86
87 if (buttonCfg.formFactorName == IDButton::getGpioName())
88 {
89 ib = std::make_unique<IDButton>(bus, ID_DBUS_OBJECT_NAME, eventP,
90 buttonCfg);
91 }
Matt Spinler8605bdf2018-11-05 14:55:46 -060092 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +080093
94 try
95 {
96 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
97 ret = sd_event_loop(eventP.get());
98 if (ret < 0)
99 {
100 phosphor::logging::log<phosphor::logging::level::ERR>(
101 "Error occurred during the sd_event_loop",
102 phosphor::logging::entry("RET=%d", ret));
103 }
104 }
Patrick Williams6d724ce2021-10-06 12:40:26 -0500105 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800106 {
107 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
108 ret = -1;
109 }
110 return ret;
111}