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 | |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 17 | #include "button_config.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 18 | #include "button_factory.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 | |
George Liu | 94afa4b | 2022-06-20 13:36:43 +0800 | [diff] [blame] | 27 | int main(void) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 28 | { |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 29 | nlohmann::json gpioDefs; |
| 30 | nlohmann::json cpldDefs; |
| 31 | |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 32 | int ret = 0; |
| 33 | |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 34 | lg2::info("Start Phosphor buttons service..."); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 35 | |
| 36 | sd_event* event = nullptr; |
| 37 | ret = sd_event_default(&event); |
| 38 | if (ret < 0) |
| 39 | { |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 40 | lg2::error("Error creating a default sd_event handler"); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 41 | return ret; |
| 42 | } |
| 43 | EventPtr eventP{event}; |
| 44 | event = nullptr; |
| 45 | |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 46 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
| 47 | sdbusplus::server::manager_t objManager{ |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 48 | bus, "/xyz/openbmc_project/Chassis/Buttons"}; |
| 49 | |
| 50 | bus.request_name("xyz.openbmc_project.Chassis.Buttons"); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 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}; |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 54 | auto configDefJson = nlohmann::json::parse(gpios, nullptr, true); |
| 55 | gpioDefs = configDefJson["gpio_definitions"]; |
| 56 | cpldDefs = configDefJson["cpld_definitions"]; |
| 57 | |
| 58 | // load cpld config from gpio defs json file and create button interface |
| 59 | for (const auto& cpldConfig : cpldDefs) |
| 60 | { |
| 61 | std::string formFactorName = cpldConfig["name"]; |
| 62 | |
| 63 | ButtonConfig buttonCfg; |
| 64 | buttonCfg.type = ConfigType::cpld; |
| 65 | buttonCfg.formFactorName = formFactorName; |
| 66 | buttonCfg.extraJsonInfo = cpldConfig; |
| 67 | |
| 68 | CpldInfo cpldCfg; |
| 69 | cpldCfg.registerName = cpldConfig["register_name"]; |
| 70 | |
| 71 | cpldCfg.i2cAddress = cpldConfig["i2c_address"].get<int>(); |
| 72 | cpldCfg.i2cBus = cpldConfig["i2c_bus"].get<int>(); |
| 73 | buttonCfg.cpld = cpldCfg; |
| 74 | |
| 75 | auto tempButtonIf = ButtonFactory::instance().createInstance( |
| 76 | formFactorName, bus, eventP, buttonCfg); |
| 77 | if (tempButtonIf) |
| 78 | { |
| 79 | buttonInterfaces.emplace_back(std::move(tempButtonIf)); |
| 80 | } |
| 81 | } |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 82 | |
| 83 | // load gpio config from gpio defs json file and create button interface |
| 84 | // objects based on the button form factor type |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 85 | |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 86 | for (const auto& gpioConfig : gpioDefs) |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 87 | { |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 88 | std::string formFactorName = gpioConfig["name"]; |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 89 | ButtonConfig buttonCfg; |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 90 | buttonCfg.formFactorName = formFactorName; |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 91 | buttonCfg.extraJsonInfo = gpioConfig; |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 92 | buttonCfg.type = ConfigType::gpio; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 93 | |
Manojkiran Eda | 010035e | 2024-06-17 14:14:38 +0530 | [diff] [blame^] | 94 | /* The following code checks if the gpio config read |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 95 | from json file is single gpio config or group gpio config, |
| 96 | based on that further data is processed. */ |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 97 | lg2::debug("Found button config : {FORM_FACTOR_NAME}", |
| 98 | "FORM_FACTOR_NAME", buttonCfg.formFactorName); |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 99 | if (gpioConfig.contains("group_gpio_config")) |
| 100 | { |
| 101 | const auto& groupGpio = gpioConfig["group_gpio_config"]; |
| 102 | |
| 103 | for (const auto& config : groupGpio) |
| 104 | { |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 105 | GpioInfo gpioCfg; |
Jonico Eustaquio | cb418b0 | 2024-01-16 10:03:43 -0600 | [diff] [blame] | 106 | if (gpioConfig.contains("pin")) |
| 107 | { |
| 108 | // When "pin" key is used, parse as alphanumeric |
| 109 | gpioCfg.number = getGpioNum(gpioConfig.at("pin")); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // Without "pin", "num" is assumed and parsed as an integer |
| 114 | gpioCfg.number = gpioConfig.at("num").get<uint32_t>(); |
| 115 | } |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 116 | gpioCfg.direction = config["direction"]; |
Naveen Moses | d219fa3 | 2022-07-20 00:01:46 +0530 | [diff] [blame] | 117 | gpioCfg.name = config["name"]; |
| 118 | gpioCfg.polarity = (config["polarity"] == "active_high") |
| 119 | ? GpioPolarity::activeHigh |
| 120 | : GpioPolarity::activeLow; |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 121 | buttonCfg.gpios.push_back(gpioCfg); |
| 122 | } |
| 123 | } |
| 124 | else |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 125 | { |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 126 | GpioInfo gpioCfg; |
Jonico Eustaquio | cb418b0 | 2024-01-16 10:03:43 -0600 | [diff] [blame] | 127 | if (gpioConfig.contains("pin")) |
| 128 | { |
| 129 | // When "pin" key is used, parse as alphanumeric |
| 130 | gpioCfg.number = getGpioNum(gpioConfig.at("pin")); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | // Without "pin", "num" is assumed and parsed as an integer |
| 135 | gpioCfg.number = gpioConfig.at("num").get<uint32_t>(); |
| 136 | } |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 137 | gpioCfg.direction = gpioConfig["direction"]; |
Naveen Moses | dd5495c | 2021-12-03 22:40:46 +0530 | [diff] [blame] | 138 | buttonCfg.gpios.push_back(gpioCfg); |
| 139 | } |
Naveen Moses | eea8a4a | 2022-02-18 01:14:15 +0530 | [diff] [blame] | 140 | auto tempButtonIf = ButtonFactory::instance().createInstance( |
| 141 | formFactorName, bus, eventP, buttonCfg); |
| 142 | /* There are additional gpio configs present in some platforms |
| 143 | that are not supported in phosphor-buttons. |
| 144 | But they may be used by other applications. so skipping such configs |
| 145 | if present in gpio_defs.json file*/ |
| 146 | if (tempButtonIf) |
| 147 | { |
| 148 | buttonInterfaces.emplace_back(std::move(tempButtonIf)); |
| 149 | } |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 150 | } |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 151 | |
| 152 | try |
| 153 | { |
| 154 | bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); |
| 155 | ret = sd_event_loop(eventP.get()); |
| 156 | if (ret < 0) |
| 157 | { |
Naveen Moses | a6d4e65 | 2022-04-13 19:27:25 +0530 | [diff] [blame] | 158 | lg2::error("Error occurred during the sd_event_loop : {RESULT}", |
| 159 | "RESULT", ret); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 160 | } |
| 161 | } |
Patrick Williams | 6d724ce | 2021-10-06 12:40:26 -0500 | [diff] [blame] | 162 | catch (const std::exception& e) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 163 | { |
| 164 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 165 | ret = -1; |
| 166 | } |
| 167 | return ret; |
| 168 | } |