blob: ce8df46a477ae1d234a84d3325b8e7ab6c6a3e28 [file] [log] [blame]
Kuiying Wanga9d39e32018-08-14 13:47:32 +08001/*
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
Kuiying Wanga9d39e32018-08-14 13:47:32 +080018#include "common.hpp"
19#include "gpio.hpp"
Patrick Venture0d9377d2018-11-01 19:34:59 -070020#include "xyz/openbmc_project/Chassis/Buttons/Power/server.hpp"
21#include "xyz/openbmc_project/Chassis/Common/error.hpp"
22
23#include <unistd.h>
24
Matt Spinler93894f62018-11-05 15:31:18 -060025#include <chrono>
Patrick Venture0d9377d2018-11-01 19:34:59 -070026#include <phosphor-logging/elog-errors.hpp>
Kuiying Wanga9d39e32018-08-14 13:47:32 +080027
28const static constexpr char* POWER_BUTTON = "POWER_BUTTON";
29
30struct PowerButton
31 : sdbusplus::server::object::object<
32 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>
33{
34
Patrick Venture0d9377d2018-11-01 19:34:59 -070035 PowerButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
Kuiying Wanga9d39e32018-08-14 13:47:32 +080036 sd_event_io_handler_t handler = PowerButton::EventHandler) :
37 sdbusplus::server::object::object<
38 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
39 bus, path),
40 fd(-1), bus(bus), event(event), callbackHandler(handler)
41 {
42
43 int ret = -1;
44
45 // config gpio
46 ret = ::configGpio(POWER_BUTTON, &fd, bus);
47 if (ret < 0)
48 {
49 phosphor::logging::log<phosphor::logging::level::ERR>(
50 "POWER_BUTTON: failed to config GPIO");
Patrick Venture0d9377d2018-11-01 19:34:59 -070051 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
52 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080053 }
54
55 ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
56 callbackHandler, this);
57 if (ret < 0)
58 {
59 phosphor::logging::log<phosphor::logging::level::ERR>(
60 "POWER_BUTTON: failed to add to event loop");
61 ::closeGpio(fd);
Patrick Venture0d9377d2018-11-01 19:34:59 -070062 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
63 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080064 }
65 }
66
67 ~PowerButton()
68 {
69 ::closeGpio(fd);
70 }
71
72 void simPress() override;
73 void simLongPress() override;
74
Matt Spinler8605bdf2018-11-05 14:55:46 -060075 static const char* getGpioName()
76 {
77 return POWER_BUTTON;
78 }
79
Matt Spinler93894f62018-11-05 15:31:18 -060080 void updatePressedTime()
81 {
82 pressedTime = std::chrono::steady_clock::now();
83 }
84
85 auto getPressTime() const
86 {
87 return pressedTime;
88 }
89
Patrick Venture0d9377d2018-11-01 19:34:59 -070090 static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
91 void* userdata)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080092 {
93
94 int n = -1;
95 char buf = '0';
96
97 if (!userdata)
98 {
99 phosphor::logging::log<phosphor::logging::level::ERR>(
100 "POWER_BUTTON: userdata null!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700101 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
102 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800103 }
104
105 PowerButton* powerButton = static_cast<PowerButton*>(userdata);
106
107 if (!powerButton)
108 {
109 phosphor::logging::log<phosphor::logging::level::ERR>(
110 "POWER_BUTTON: null pointer!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700111 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
112 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800113 }
114
115 n = ::lseek(fd, 0, SEEK_SET);
116
117 if (n < 0)
118 {
119 phosphor::logging::log<phosphor::logging::level::ERR>(
120 "POWER_BUTTON: lseek error!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700121 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
122 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800123 }
124
125 n = ::read(fd, &buf, sizeof(buf));
126 if (n < 0)
127 {
128 phosphor::logging::log<phosphor::logging::level::ERR>(
129 "POWER_BUTTON: read error!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700130 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
131 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800132 }
133
134 if (buf == '0')
135 {
136 phosphor::logging::log<phosphor::logging::level::DEBUG>(
137 "POWER_BUTTON: pressed");
Matt Spinler93894f62018-11-05 15:31:18 -0600138
139 powerButton->updatePressedTime();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800140 // emit pressed signal
141 powerButton->pressed();
142 }
143 else
144 {
145 phosphor::logging::log<phosphor::logging::level::DEBUG>(
146 "POWER_BUTTON: released");
Matt Spinler93894f62018-11-05 15:31:18 -0600147
148 auto now = std::chrono::steady_clock::now();
149 auto d = std::chrono::duration_cast<std::chrono::milliseconds>(
150 now - powerButton->getPressTime());
151
152 if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS))
153 {
154 powerButton->pressedLong();
155 }
156 else
157 {
158 // released
159 powerButton->released();
160 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800161 }
162
163 return 0;
164 }
165
166 private:
167 int fd;
168 sdbusplus::bus::bus& bus;
169 EventPtr& event;
170 sd_event_io_handler_t callbackHandler;
Matt Spinler93894f62018-11-05 15:31:18 -0600171 decltype(std::chrono::steady_clock::now()) pressedTime;
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800172};