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 | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame^] | 17 | #include "button_factory.hpp" |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 18 | #include "gpio.hpp" |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 19 | |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <nlohmann/json.hpp> |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame^] | 22 | #include <phosphor-logging/elog-errors.hpp> |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 23 | static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json"; |
| 24 | |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 25 | int main(int argc, char* argv[]) |
| 26 | { |
| 27 | int ret = 0; |
| 28 | |
| 29 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame^] | 30 | "Start Phosphor buttons service..."); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 31 | |
| 32 | sd_event* event = nullptr; |
| 33 | ret = sd_event_default(&event); |
| 34 | if (ret < 0) |
| 35 | { |
| 36 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 37 | "Error creating a default sd_event handler"); |
| 38 | return ret; |
| 39 | } |
| 40 | EventPtr eventP{event}; |
| 41 | event = nullptr; |
| 42 | |
| 43 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
| 44 | sdbusplus::server::manager::manager objManager{ |
| 45 | bus, "/xyz/openbmc_project/Chassis/Buttons"}; |
| 46 | |
| 47 | bus.request_name("xyz.openbmc_project.Chassis.Buttons"); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame^] | 48 | // |
| 49 | std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces; |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 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 |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 57 | |
| 58 | for (auto groupGpioConfig : gpioDefs) |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 59 | { |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 60 | std::string formFactorName = groupGpioConfig["name"]; |
| 61 | buttonConfig buttonCfg; |
| 62 | auto groupGpio = groupGpioConfig["gpio_config"]; |
| 63 | |
| 64 | for (auto gpioConfig : groupGpio) |
| 65 | { |
| 66 | gpioInfo gpioCfg; |
| 67 | gpioCfg.number = getGpioNum(gpioConfig["pin"]); |
| 68 | gpioCfg.direction = gpioConfig["direction"]; |
| 69 | buttonCfg.formFactorName = formFactorName; |
| 70 | buttonCfg.gpios.push_back(gpioCfg); |
| 71 | } |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 72 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame^] | 73 | buttonInterfaces.emplace_back(ButtonFactory::instance().createInstance( |
| 74 | formFactorName, bus, eventP, buttonCfg)); |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 75 | } |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 76 | |
| 77 | try |
| 78 | { |
| 79 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 80 | ret = sd_event_loop(eventP.get()); |
| 81 | if (ret < 0) |
| 82 | { |
| 83 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 84 | "Error occurred during the sd_event_loop", |
| 85 | phosphor::logging::entry("RET=%d", ret)); |
| 86 | } |
| 87 | } |
Patrick Williams | 6d724ce | 2021-10-06 12:40:26 -0500 | [diff] [blame] | 88 | catch (const std::exception& e) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 89 | { |
| 90 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 91 | ret = -1; |
| 92 | } |
| 93 | return ret; |
| 94 | } |