Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 1 | /* |
| 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 Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame^] | 17 | #include "gpio.hpp" |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 18 | #include "id_button.hpp" |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 19 | #include "power_button.hpp" |
| 20 | #include "reset_button.hpp" |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 21 | |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame^] | 22 | #include <fstream> |
| 23 | #include <nlohmann/json.hpp> |
| 24 | |
| 25 | static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json"; |
| 26 | |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 27 | int 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 Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame^] | 51 | 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 Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 57 | std::unique_ptr<PowerButton> pb; |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 58 | std::unique_ptr<ResetButton> rb; |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 59 | std::unique_ptr<IDButton> ib; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame^] | 60 | |
| 61 | for (auto groupGpioConfig : gpioDefs) |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 62 | { |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame^] | 63 | 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 Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 92 | } |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 93 | |
| 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 Williams | 6d724ce | 2021-10-06 12:40:26 -0500 | [diff] [blame] | 105 | catch (const std::exception& e) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 106 | { |
| 107 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 108 | ret = -1; |
| 109 | } |
| 110 | return ret; |
| 111 | } |