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 | |
| 18 | #include "rpolicy.hpp" |
| 19 | |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 20 | #include <phosphor-logging/elog-errors.hpp> |
| 21 | #include <phosphor-logging/elog.hpp> |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 22 | #include <sdeventplus/event.hpp> |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 23 | #include <xyz/openbmc_project/Common/Callout/error.hpp> |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 24 | |
| 25 | #include <functional> |
| 26 | #include <tuple> |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 27 | |
| 28 | namespace phosphor |
| 29 | { |
| 30 | namespace fan |
| 31 | { |
| 32 | namespace presence |
| 33 | { |
| 34 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 35 | Gpio::Gpio(const std::string& physDevice, const std::string& device, |
| 36 | unsigned int physPin) : |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 37 | currentState(false), |
Brad Bishop | 2e9788d | 2017-07-28 22:24:03 -0400 | [diff] [blame] | 38 | evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)), |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 39 | evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin) |
| 40 | {} |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 41 | |
| 42 | bool Gpio::start() |
| 43 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 44 | source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN, |
| 45 | std::bind(&Gpio::ioCallback, this)); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 46 | currentState = present(); |
| 47 | return currentState; |
| 48 | } |
| 49 | |
| 50 | void Gpio::stop() |
| 51 | { |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 52 | source.reset(); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | bool Gpio::present() |
| 56 | { |
| 57 | return evdev.fetch(EV_KEY, pin) != 0; |
| 58 | } |
| 59 | |
| 60 | void Gpio::fail() |
| 61 | { |
| 62 | using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error; |
| 63 | using namespace phosphor::logging; |
| 64 | using namespace xyz::openbmc_project::Common::Callout; |
| 65 | |
| 66 | report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>( |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 67 | GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0), |
| 68 | GPIO::CALLOUT_DEVICE_PATH(phys.c_str())); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 69 | } |
| 70 | |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 71 | void Gpio::ioCallback() |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 72 | { |
| 73 | unsigned int type, code, value; |
| 74 | |
| 75 | std::tie(type, code, value) = evdev.next(); |
| 76 | if (type != EV_KEY || code != pin) |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | bool newState = value != 0; |
| 82 | |
| 83 | if (currentState != newState) |
| 84 | { |
Brad Bishop | 11083ec | 2017-07-25 19:08:53 -0400 | [diff] [blame] | 85 | getPolicy().stateChanged(newState, *this); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 86 | currentState = newState; |
| 87 | } |
| 88 | } |
| 89 | } // namespace presence |
| 90 | } // namespace fan |
| 91 | } // namespace phosphor |