blob: 0bd5b6f7e776c7a921cffb709135cab95bc9172a [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) :
Patrick Williamsdfddd642024-08-16 15:21:51 -040044 currentState(false), evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)),
Matthew Barth2d2caa32020-05-26 11:07:24 -050045 evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin)
46{}
Brad Bishop5c589482017-06-14 22:32:20 -040047
48bool Gpio::start()
49{
Matthew Barth2d2caa32020-05-26 11:07:24 -050050 source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN,
51 std::bind(&Gpio::ioCallback, this));
Brad Bishop5c589482017-06-14 22:32:20 -040052 currentState = present();
53 return currentState;
54}
55
56void Gpio::stop()
57{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070058 source.reset();
Brad Bishop5c589482017-06-14 22:32:20 -040059}
60
61bool Gpio::present()
62{
63 return evdev.fetch(EV_KEY, pin) != 0;
64}
65
66void 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 Barth2d2caa32020-05-26 11:07:24 -050073 GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0),
74 GPIO::CALLOUT_DEVICE_PATH(phys.c_str()));
Brad Bishop5c589482017-06-14 22:32:20 -040075}
76
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070077void Gpio::ioCallback()
Brad Bishop5c589482017-06-14 22:32:20 -040078{
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 Bishop11083ec2017-07-25 19:08:53 -040091 getPolicy().stateChanged(newState, *this);
Brad Bishop5c589482017-06-14 22:32:20 -040092 currentState = newState;
93 }
94}
Matt Spinlerc65d91d2021-04-21 13:09:49 -050095
96void 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 Williamsfbf47032023-07-17 12:27:34 -0500111 std::format("GPIO presence detect for fan {} said not present but "
Matt Spinlerc65d91d2021-04-21 13:09:49 -0500112 "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 Williamsfbf47032023-07-17 12:27:34 -0500123 std::format("Call to create a {} error for fan {} failed: {}",
Matt Spinlerc65d91d2021-04-21 13:09:49 -0500124 errorName, fanInventoryPath, e.what()),
125 Logger::error);
126 }
127}
128
Brad Bishop5c589482017-06-14 22:32:20 -0400129} // namespace presence
130} // namespace fan
131} // namespace phosphor