blob: 8e4a6d27057eea318edf26d1fec0009632d461ca [file] [log] [blame]
Brad Bishop5c589482017-06-14 22:32:20 -04001/**
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 Barth2d2caa32020-05-26 11:07:24 -050016#include "gpio.hpp"
17
Matt Spinlerc65d91d2021-04-21 13:09:49 -050018#include "logging.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050019#include "rpolicy.hpp"
Matt Spinlerc65d91d2021-04-21 13:09:49 -050020#include "sdbusplus.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050021
Brad Bishop5c589482017-06-14 22:32:20 -040022#include <phosphor-logging/elog-errors.hpp>
23#include <phosphor-logging/elog.hpp>
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070024#include <sdeventplus/event.hpp>
Brad Bishop5c589482017-06-14 22:32:20 -040025#include <xyz/openbmc_project/Common/Callout/error.hpp>
Matt Spinlerc65d91d2021-04-21 13:09:49 -050026#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Matthew Barth2d2caa32020-05-26 11:07:24 -050027
28#include <functional>
29#include <tuple>
Brad Bishop5c589482017-06-14 22:32:20 -040030
31namespace phosphor
32{
33namespace fan
34{
35namespace presence
36{
37
Matt Spinlerc65d91d2021-04-21 13:09:49 -050038const auto loggingService = "xyz.openbmc_project.Logging";
39const auto loggingPath = "/xyz/openbmc_project/logging";
40const auto loggingCreateIface = "xyz.openbmc_project.Logging.Create";
41
Matthew Barth2d2caa32020-05-26 11:07:24 -050042Gpio::Gpio(const std::string& physDevice, const std::string& device,
43 unsigned int physPin) :
Brad Bishop5c589482017-06-14 22:32:20 -040044 currentState(false),
Brad Bishop2e9788d2017-07-28 22:24:03 -040045 evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)),
Matthew Barth2d2caa32020-05-26 11:07:24 -050046 evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin)
47{}
Brad Bishop5c589482017-06-14 22:32:20 -040048
49bool Gpio::start()
50{
Matthew Barth2d2caa32020-05-26 11:07:24 -050051 source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN,
52 std::bind(&Gpio::ioCallback, this));
Brad Bishop5c589482017-06-14 22:32:20 -040053 currentState = present();
54 return currentState;
55}
56
57void Gpio::stop()
58{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070059 source.reset();
Brad Bishop5c589482017-06-14 22:32:20 -040060}
61
62bool Gpio::present()
63{
64 return evdev.fetch(EV_KEY, pin) != 0;
65}
66
67void Gpio::fail()
68{
69 using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error;
70 using namespace phosphor::logging;
71 using namespace xyz::openbmc_project::Common::Callout;
72
73 report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050074 GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0),
75 GPIO::CALLOUT_DEVICE_PATH(phys.c_str()));
Brad Bishop5c589482017-06-14 22:32:20 -040076}
77
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070078void Gpio::ioCallback()
Brad Bishop5c589482017-06-14 22:32:20 -040079{
80 unsigned int type, code, value;
81
82 std::tie(type, code, value) = evdev.next();
83 if (type != EV_KEY || code != pin)
84 {
85 return;
86 }
87
88 bool newState = value != 0;
89
90 if (currentState != newState)
91 {
Brad Bishop11083ec2017-07-25 19:08:53 -040092 getPolicy().stateChanged(newState, *this);
Brad Bishop5c589482017-06-14 22:32:20 -040093 currentState = newState;
94 }
95}
Matt Spinlerc65d91d2021-04-21 13:09:49 -050096
97void Gpio::logConflict(const std::string& fanInventoryPath) const
98{
99 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
100 std::map<std::string, std::string> ad;
101 Entry::Level severity = Entry::Level::Informational;
102
103 static constexpr auto errorName =
104 "xyz.openbmc_project.Fan.Presence.Error.Detection";
105
106 ad.emplace("_PID", std::to_string(getpid()));
107 ad.emplace("CALLOUT_INVENTORY_PATH", fanInventoryPath);
108 ad.emplace("GPIO_NUM", std::to_string(pin));
109 ad.emplace("GPIO_DEVICE_PATH", (phys.c_str()));
110
111 getLogger().log(
112 fmt::format("GPIO presence detect for fan {} said not present but "
113 "other methods indicated present",
114 fanInventoryPath));
115 try
116 {
117 util::SDBusPlus::callMethod(loggingService, loggingPath,
118 loggingCreateIface, "Create", errorName,
119 severity, ad);
120 }
121 catch (const util::DBusError& e)
122 {
123 getLogger().log(
124 fmt::format("Call to create a {} error for fan {} failed: {}",
125 errorName, fanInventoryPath, e.what()),
126 Logger::error);
127 }
128}
129
Brad Bishop5c589482017-06-14 22:32:20 -0400130} // namespace presence
131} // namespace fan
132} // namespace phosphor