blob: 19593c2dd3d802750483189e8f0d8ef94a3445b4 [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 */
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070016#include <functional>
Brad Bishop5c589482017-06-14 22:32:20 -040017#include <phosphor-logging/elog-errors.hpp>
18#include <phosphor-logging/elog.hpp>
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070019#include <sdeventplus/event.hpp>
Brad Bishop5c589482017-06-14 22:32:20 -040020#include <tuple>
21#include <xyz/openbmc_project/Common/Callout/error.hpp>
22#include "gpio.hpp"
23#include "rpolicy.hpp"
Brad Bishop5c589482017-06-14 22:32:20 -040024
25namespace phosphor
26{
27namespace fan
28{
29namespace presence
30{
31
32Gpio::Gpio(
33 const std::string& physDevice,
Brad Bishop2e9788d2017-07-28 22:24:03 -040034 const std::string& device,
Brad Bishop5c589482017-06-14 22:32:20 -040035 unsigned int physPin) :
36 currentState(false),
Brad Bishop2e9788d2017-07-28 22:24:03 -040037 evdevfd(open(device.c_str(), O_RDONLY | O_NONBLOCK)),
Brad Bishop5c589482017-06-14 22:32:20 -040038 evdev(evdevpp::evdev::newFromFD(evdevfd())),
39 phys(physDevice),
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070040 pin(physPin)
Brad Bishop5c589482017-06-14 22:32:20 -040041{
42
43}
44
45bool Gpio::start()
46{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070047 source.emplace(sdeventplus::Event::get_default(),
48 evdevfd(), EPOLLIN,
49 std::bind(&Gpio::ioCallback, this));
Brad Bishop5c589482017-06-14 22:32:20 -040050 currentState = present();
51 return currentState;
52}
53
54void Gpio::stop()
55{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070056 source.reset();
Brad Bishop5c589482017-06-14 22:32:20 -040057}
58
59bool Gpio::present()
60{
61 return evdev.fetch(EV_KEY, pin) != 0;
62}
63
64void Gpio::fail()
65{
66 using namespace sdbusplus::xyz::openbmc_project::Common::Callout::Error;
67 using namespace phosphor::logging;
68 using namespace xyz::openbmc_project::Common::Callout;
69
70 report<sdbusplus::xyz::openbmc_project::Common::Callout::Error::GPIO>(
71 GPIO::CALLOUT_GPIO_NUM(pin),
72 GPIO::CALLOUT_ERRNO(0),
73 GPIO::CALLOUT_DEVICE_PATH(phys.c_str()));
74}
75
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070076void Gpio::ioCallback()
Brad Bishop5c589482017-06-14 22:32:20 -040077{
78 unsigned int type, code, value;
79
80 std::tie(type, code, value) = evdev.next();
81 if (type != EV_KEY || code != pin)
82 {
83 return;
84 }
85
86 bool newState = value != 0;
87
88 if (currentState != newState)
89 {
Brad Bishop11083ec2017-07-25 19:08:53 -040090 getPolicy().stateChanged(newState, *this);
Brad Bishop5c589482017-06-14 22:32:20 -040091 currentState = newState;
92 }
93}
94} // namespace presence
95} // namespace fan
96} // namespace phosphor