1 /*
2 // Copyright (c) 2018 Intel 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 
17 #pragma once
18 #include "common.hpp"
19 #include "gpio.hpp"
20 #include "xyz/openbmc_project/Chassis/Buttons/Power/server.hpp"
21 #include "xyz/openbmc_project/Chassis/Common/error.hpp"
22 
23 #include <unistd.h>
24 
25 #include <chrono>
26 #include <phosphor-logging/elog-errors.hpp>
27 
28 const static constexpr char* POWER_BUTTON = "POWER_BUTTON";
29 
30 struct PowerButton
31     : sdbusplus::server::object::object<
32           sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>
33 {
34 
35     PowerButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
36                 buttonConfig& buttonCfg,
37                 sd_event_io_handler_t handler = PowerButton::EventHandler) :
38         sdbusplus::server::object::object<
39             sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
40             bus, path),
41         fd(-1), buttonIFConfig(buttonCfg), bus(bus), event(event),
42         callbackHandler(handler)
43     {
44 
45         int ret = -1;
46 
47         // config group gpio based on the gpio defs read from the json file
48         ret = configGroupGpio(bus, buttonIFConfig);
49 
50         if (ret < 0)
51         {
52             phosphor::logging::log<phosphor::logging::level::ERR>(
53                 "POWER_BUTTON: failed to config GPIO");
54             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
55                 IOError();
56         }
57 
58         // initialize the button io fd from the buttonConfig
59         // which has fd stored when configGroupGpio is called
60         fd = buttonIFConfig.gpios[0].fd;
61 
62         char buf;
63         ::read(fd, &buf, sizeof(buf));
64 
65         ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
66                               callbackHandler, this);
67         if (ret < 0)
68         {
69             phosphor::logging::log<phosphor::logging::level::ERR>(
70                 "POWER_BUTTON: failed to add to event loop");
71             ::closeGpio(fd);
72             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
73                 IOError();
74         }
75     }
76 
77     ~PowerButton()
78     {
79         ::closeGpio(fd);
80     }
81 
82     void simPress() override;
83     void simLongPress() override;
84 
85     static const std::string getGpioName()
86     {
87         return POWER_BUTTON;
88     }
89 
90     void updatePressedTime()
91     {
92         pressedTime = std::chrono::steady_clock::now();
93     }
94 
95     auto getPressTime() const
96     {
97         return pressedTime;
98     }
99 
100     static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
101                             void* userdata)
102     {
103 
104         int n = -1;
105         char buf = '0';
106 
107         if (!userdata)
108         {
109             phosphor::logging::log<phosphor::logging::level::ERR>(
110                 "POWER_BUTTON: userdata null!");
111             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
112                 IOError();
113         }
114 
115         PowerButton* powerButton = static_cast<PowerButton*>(userdata);
116 
117         if (!powerButton)
118         {
119             phosphor::logging::log<phosphor::logging::level::ERR>(
120                 "POWER_BUTTON: null pointer!");
121             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
122                 IOError();
123         }
124 
125         n = ::lseek(fd, 0, SEEK_SET);
126 
127         if (n < 0)
128         {
129             phosphor::logging::log<phosphor::logging::level::ERR>(
130                 "POWER_BUTTON: lseek error!");
131             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
132                 IOError();
133         }
134 
135         n = ::read(fd, &buf, sizeof(buf));
136         if (n < 0)
137         {
138             phosphor::logging::log<phosphor::logging::level::ERR>(
139                 "POWER_BUTTON: read error!");
140             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
141                 IOError();
142         }
143 
144         if (buf == '0')
145         {
146             phosphor::logging::log<phosphor::logging::level::DEBUG>(
147                 "POWER_BUTTON: pressed");
148 
149             powerButton->updatePressedTime();
150             // emit pressed signal
151             powerButton->pressed();
152         }
153         else
154         {
155             phosphor::logging::log<phosphor::logging::level::DEBUG>(
156                 "POWER_BUTTON: released");
157 
158             auto now = std::chrono::steady_clock::now();
159             auto d = std::chrono::duration_cast<std::chrono::milliseconds>(
160                 now - powerButton->getPressTime());
161 
162             if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS))
163             {
164                 powerButton->pressedLong();
165             }
166             else
167             {
168                 // released
169                 powerButton->released();
170             }
171         }
172 
173         return 0;
174     }
175 
176   private:
177     int fd;
178     buttonConfig buttonIFConfig; // button iface io details
179     sdbusplus::bus::bus& bus;
180     EventPtr& event;
181     sd_event_io_handler_t callbackHandler;
182     decltype(std::chrono::steady_clock::now()) pressedTime;
183 };
184