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 17 #include "gpio.hpp" 18 #include "id_button.hpp" 19 #include "power_button.hpp" 20 #include "reset_button.hpp" 21 22 #include <fstream> 23 #include <nlohmann/json.hpp> 24 25 static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json"; 26 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 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 57 std::unique_ptr<PowerButton> pb; 58 std::unique_ptr<ResetButton> rb; 59 std::unique_ptr<IDButton> ib; 60 61 for (auto groupGpioConfig : gpioDefs) 62 { 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 } 92 } 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 } 105 catch (const std::exception& e) 106 { 107 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 108 ret = -1; 109 } 110 return ret; 111 } 112