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 "button_factory.hpp" 18 #include "gpio.hpp" 19 20 #include <nlohmann/json.hpp> 21 #include <phosphor-logging/elog-errors.hpp> 22 23 #include <fstream> 24 static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json"; 25 26 using namespace phosphor::logging; 27 nlohmann::json gpioDefs; 28 29 int main(int argc, char* argv[]) 30 { 31 int ret = 0; 32 33 phosphor::logging::log<phosphor::logging::level::INFO>( 34 "Start Phosphor buttons service..."); 35 36 sd_event* event = nullptr; 37 ret = sd_event_default(&event); 38 if (ret < 0) 39 { 40 phosphor::logging::log<phosphor::logging::level::ERR>( 41 "Error creating a default sd_event handler"); 42 return ret; 43 } 44 EventPtr eventP{event}; 45 event = nullptr; 46 47 sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); 48 sdbusplus::server::manager::manager objManager{ 49 bus, "/xyz/openbmc_project/Chassis/Buttons"}; 50 51 bus.request_name("xyz.openbmc_project.Chassis.Buttons"); 52 // 53 std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces; 54 55 std::ifstream gpios{gpioDefFile}; 56 auto gpioDefJson = nlohmann::json::parse(gpios, nullptr, true); 57 gpioDefs = gpioDefJson["gpio_definitions"]; 58 59 // load gpio config from gpio defs json file and create button interface 60 // objects based on the button form factor type 61 62 for (const auto& gpioConfig : gpioDefs) 63 { 64 std::string formFactorName = gpioConfig["name"]; 65 buttonConfig buttonCfg; 66 buttonCfg.formFactorName = formFactorName; 67 buttonCfg.extraJsonInfo = gpioConfig; 68 69 /* The folloing code checks if the gpio config read 70 from json file is single gpio config or group gpio config, 71 based on that further data is processed. */ 72 if (gpioConfig.contains("group_gpio_config")) 73 { 74 const auto& groupGpio = gpioConfig["group_gpio_config"]; 75 76 for (const auto& config : groupGpio) 77 { 78 gpioInfo gpioCfg; 79 gpioCfg.number = getGpioNum(config["pin"]); 80 gpioCfg.direction = config["direction"]; 81 buttonCfg.gpios.push_back(gpioCfg); 82 } 83 } 84 else 85 { 86 gpioInfo gpioCfg; 87 gpioCfg.number = getGpioNum(gpioConfig["pin"]); 88 gpioCfg.direction = gpioConfig["direction"]; 89 buttonCfg.gpios.push_back(gpioCfg); 90 } 91 auto tempButtonIf = ButtonFactory::instance().createInstance( 92 formFactorName, bus, eventP, buttonCfg); 93 /* There are additional gpio configs present in some platforms 94 that are not supported in phosphor-buttons. 95 But they may be used by other applications. so skipping such configs 96 if present in gpio_defs.json file*/ 97 if (tempButtonIf) 98 { 99 buttonInterfaces.emplace_back(std::move(tempButtonIf)); 100 } 101 } 102 103 try 104 { 105 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); 106 ret = sd_event_loop(eventP.get()); 107 if (ret < 0) 108 { 109 phosphor::logging::log<phosphor::logging::level::ERR>( 110 "Error occurred during the sd_event_loop", 111 phosphor::logging::entry("RET=%d", ret)); 112 } 113 } 114 catch (const std::exception& e) 115 { 116 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 117 ret = -1; 118 } 119 return ret; 120 } 121