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  */
16 #include "gpio.hpp"
17 
18 #include "logging.hpp"
19 #include "rpolicy.hpp"
20 #include "sdbusplus.hpp"
21 
22 #include <phosphor-logging/elog-errors.hpp>
23 #include <phosphor-logging/elog.hpp>
24 #include <sdeventplus/event.hpp>
25 #include <xyz/openbmc_project/Common/Callout/error.hpp>
26 #include <xyz/openbmc_project/Logging/Entry/server.hpp>
27 
28 #include <functional>
29 #include <tuple>
30 
31 namespace phosphor
32 {
33 namespace fan
34 {
35 namespace presence
36 {
37 
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 
Gpio(const std::string & physDevice,const std::string & device,unsigned int physPin)42 Gpio::Gpio(const std::string& physDevice, const std::string& device,
43            unsigned int physPin) :
44     currentState(false), evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)),
45     evdev(evdevpp::evdev::newFromFD(evdevfd())), phys(physDevice), pin(physPin)
46 {}
47 
start()48 bool Gpio::start()
49 {
50     source.emplace(sdeventplus::Event::get_default(), evdevfd(), EPOLLIN,
51                    std::bind(&Gpio::ioCallback, this));
52     currentState = present();
53     return currentState;
54 }
55 
stop()56 void Gpio::stop()
57 {
58     source.reset();
59 }
60 
present()61 bool Gpio::present()
62 {
63     return evdev.fetch(EV_KEY, pin) != 0;
64 }
65 
fail()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>(
73         GPIO::CALLOUT_GPIO_NUM(pin), GPIO::CALLOUT_ERRNO(0),
74         GPIO::CALLOUT_DEVICE_PATH(phys.c_str()));
75 }
76 
ioCallback()77 void Gpio::ioCallback()
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     {
91         getPolicy().stateChanged(newState, *this);
92         currentState = newState;
93     }
94 }
95 
logConflict(const std::string & fanInventoryPath) const96 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(
111         std::format("GPIO presence detect for fan {} said not present but "
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(
123             std::format("Call to create a {} error for fan {} failed: {}",
124                         errorName, fanInventoryPath, e.what()),
125             Logger::error);
126     }
127 }
128 
129 } // namespace presence
130 } // namespace fan
131 } // namespace phosphor
132