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 <nlohmann/json.hpp> |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 21 | #include <phosphor-logging/elog-errors.hpp> |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame^] | 22 | #include <phosphor-logging/lg2.hpp> |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 23 | |
| 24 | #include <fstream> |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 25 | static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json"; |
| 26 | |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 27 | nlohmann::json gpioDefs; |
| 28 | |
George Liu | 94afa4b | 2022-06-20 13:36:43 +0800 | [diff] [blame] | 29 | int main(void) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 30 | { |
| 31 | int ret = 0; |
| 32 | |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame^] | 33 | lg2::info("Start Phosphor buttons service..."); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 34 | |
| 35 | sd_event* event = nullptr; |
| 36 | ret = sd_event_default(&event); |
| 37 | if (ret < 0) |
| 38 | { |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame^] | 39 | lg2::error("Error creating a default sd_event handler"); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | EventPtr eventP{event}; |
| 43 | event = nullptr; |
| 44 | |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 45 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
| 46 | sdbusplus::server::manager_t objManager{ |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 47 | bus, "/xyz/openbmc_project/Chassis/Buttons"}; |
| 48 | |
| 49 | bus.request_name("xyz.openbmc_project.Chassis.Buttons"); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 50 | // |
| 51 | std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces; |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 52 | |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 53 | std::ifstream gpios{gpioDefFile}; |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 54 | auto gpioDefJson = nlohmann::json::parse(gpios, nullptr, true); |
| 55 | gpioDefs = gpioDefJson["gpio_definitions"]; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 56 | |
| 57 | // load gpio config from gpio defs json file and create button interface |
| 58 | // objects based on the button form factor type |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 59 | |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 60 | for (const auto& gpioConfig : gpioDefs) |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 61 | { |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 62 | std::string formFactorName = gpioConfig["name"]; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 63 | buttonConfig buttonCfg; |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 64 | buttonCfg.formFactorName = formFactorName; |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 65 | buttonCfg.extraJsonInfo = gpioConfig; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 66 | |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 67 | /* The folloing code checks if the gpio config read |
| 68 | from json file is single gpio config or group gpio config, |
| 69 | based on that further data is processed. */ |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame^] | 70 | lg2::debug("Found button config : {FORM_FACTOR_NAME}", |
| 71 | "FORM_FACTOR_NAME", buttonCfg.formFactorName); |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 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 |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 85 | { |
| 86 | gpioInfo gpioCfg; |
| 87 | gpioCfg.number = getGpioNum(gpioConfig["pin"]); |
| 88 | gpioCfg.direction = gpioConfig["direction"]; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 89 | buttonCfg.gpios.push_back(gpioCfg); |
| 90 | } |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 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 | } |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 101 | } |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 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 | { |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame^] | 109 | lg2::error("Error occurred during the sd_event_loop : {RESULT}", |
| 110 | "RESULT", ret); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 111 | } |
| 112 | } |
Patrick Williams | 6d724ce | 2021-10-06 12:40:26 -0500 | [diff] [blame] | 113 | catch (const std::exception& e) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 114 | { |
| 115 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 116 | ret = -1; |
| 117 | } |
| 118 | return ret; |
| 119 | } |