Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM 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 | */ |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 16 | #include "gpio.hpp" |
| 17 | |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 18 | #include "logging.hpp" |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 19 | #include "rpolicy.hpp" |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 20 | #include "sdbusplus.hpp" |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 21 | |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 22 | #include <phosphor-logging/elog-errors.hpp> |
| 23 | #include <phosphor-logging/elog.hpp> |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 24 | #include <sdeventplus/event.hpp> |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 25 | #include <xyz/openbmc_project/Common/Callout/error.hpp> |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 26 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 27 | |
| 28 | #include <functional> |
| 29 | #include <tuple> |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 30 | |
| 31 | namespace phosphor |
| 32 | { |
| 33 | namespace fan |
| 34 | { |
| 35 | namespace presence |
| 36 | { |
| 37 | |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 38 | const auto loggingService = "xyz.openbmc_project.Logging"; |
| 39 | const auto loggingPath = "/xyz/openbmc_project/logging"; |
| 40 | const auto loggingCreateIface = "xyz.openbmc_project.Logging.Create"; |
| 41 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 42 | Gpio::Gpio(const std::string& physDevice, const std::string& device, |
| 43 | unsigned int physPin) : |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 44 | currentState(false), evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)), |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 45 | evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin) |
| 46 | {} |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 47 | |
| 48 | bool Gpio::start() |
| 49 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 50 | source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN, |
| 51 | std::bind(&Gpio::ioCallback, this)); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 52 | currentState = present(); |
| 53 | return currentState; |
| 54 | } |
| 55 | |
| 56 | void Gpio::stop() |
| 57 | { |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 58 | source.reset(); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool Gpio::present() |
| 62 | { |
| 63 | return evdev.fetch(EV_KEY, pin) != 0; |
| 64 | } |
| 65 | |
| 66 | void Gpio::fail() |
| 67 | { |
| 68 | using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error; |
| 69 | using namespace phosphor::logging; |
| 70 | using namespace xyz::openbmc_project::Common::Callout; |
| 71 | |
| 72 | report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>( |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 73 | GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0), |
| 74 | GPIO::CALLOUT_DEVICE_PATH(phys.c_str())); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 75 | } |
| 76 | |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 77 | void Gpio::ioCallback() |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 78 | { |
| 79 | unsigned int type, code, value; |
| 80 | |
| 81 | std::tie(type, code, value) = evdev.next(); |
| 82 | if (type != EV_KEY || code != pin) |
| 83 | { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | bool newState = value != 0; |
| 88 | |
| 89 | if (currentState != newState) |
| 90 | { |
Brad Bishop | 11083ec | 2017-07-25 19:08:53 -0400 | [diff] [blame] | 91 | getPolicy().stateChanged(newState, *this); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 92 | currentState = newState; |
| 93 | } |
| 94 | } |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 95 | |
| 96 | void Gpio::logConflict(const std::string& fanInventoryPath) const |
| 97 | { |
| 98 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 99 | std::map<std::string, std::string> ad; |
| 100 | Entry::Level severity = Entry::Level::Informational; |
| 101 | |
| 102 | static constexpr auto errorName = |
| 103 | "xyz.openbmc_project.Fan.Presence.Error.Detection"; |
| 104 | |
| 105 | ad.emplace("_PID", std::to_string(getpid())); |
| 106 | ad.emplace("CALLOUT_INVENTORY_PATH", fanInventoryPath); |
| 107 | ad.emplace("GPIO_NUM", std::to_string(pin)); |
| 108 | ad.emplace("GPIO_DEVICE_PATH", (phys.c_str())); |
| 109 | |
| 110 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 111 | std::format("GPIO presence detect for fan {} said not present but " |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 112 | "other methods indicated present", |
| 113 | fanInventoryPath)); |
| 114 | try |
| 115 | { |
| 116 | util::SDBusPlus::callMethod(loggingService, loggingPath, |
| 117 | loggingCreateIface, "Create", errorName, |
| 118 | severity, ad); |
| 119 | } |
| 120 | catch (const util::DBusError& e) |
| 121 | { |
| 122 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 123 | std::format("Call to create a {} error for fan {} failed: {}", |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 124 | errorName, fanInventoryPath, e.what()), |
| 125 | Logger::error); |
| 126 | } |
| 127 | } |
| 128 | |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 129 | } // namespace presence |
| 130 | } // namespace fan |
| 131 | } // namespace phosphor |