blob: e71d9f18fb151ec2da1e84049297ffae3c9eee4e [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
Matt Spinlerb3d86c92018-11-15 16:13:19 -060055 char buf;
56 ::read(fd, &buf, sizeof(buf));
57
Kuiying Wanga9d39e32018-08-14 13:47:32 +080058 ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
59 callbackHandler, this);
60 if (ret < 0)
61 {
62 phosphor::logging::log<phosphor::logging::level::ERR>(
63 "POWER_BUTTON: failed to add to event loop");
64 ::closeGpio(fd);
Patrick Venture0d9377d2018-11-01 19:34:59 -070065 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
66 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080067 }
68 }
69
70 ~PowerButton()
71 {
72 ::closeGpio(fd);
73 }
74
75 void simPress() override;
76 void simLongPress() override;
77
Matt Spinler8605bdf2018-11-05 14:55:46 -060078 static const char* getGpioName()
79 {
80 return POWER_BUTTON;
81 }
82
Matt Spinler93894f62018-11-05 15:31:18 -060083 void updatePressedTime()
84 {
85 pressedTime = std::chrono::steady_clock::now();
86 }
87
88 auto getPressTime() const
89 {
90 return pressedTime;
91 }
92
Patrick Venture0d9377d2018-11-01 19:34:59 -070093 static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
94 void* userdata)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080095 {
96
97 int n = -1;
98 char buf = '0';
99
100 if (!userdata)
101 {
102 phosphor::logging::log<phosphor::logging::level::ERR>(
103 "POWER_BUTTON: userdata null!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700104 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
105 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800106 }
107
108 PowerButton* powerButton = static_cast<PowerButton*>(userdata);
109
110 if (!powerButton)
111 {
112 phosphor::logging::log<phosphor::logging::level::ERR>(
113 "POWER_BUTTON: null pointer!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700114 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
115 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800116 }
117
118 n = ::lseek(fd, 0, SEEK_SET);
119
120 if (n < 0)
121 {
122 phosphor::logging::log<phosphor::logging::level::ERR>(
123 "POWER_BUTTON: lseek error!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700124 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
125 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800126 }
127
128 n = ::read(fd, &buf, sizeof(buf));
129 if (n < 0)
130 {
131 phosphor::logging::log<phosphor::logging::level::ERR>(
132 "POWER_BUTTON: read error!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700133 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
134 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800135 }
136
137 if (buf == '0')
138 {
139 phosphor::logging::log<phosphor::logging::level::DEBUG>(
140 "POWER_BUTTON: pressed");
Matt Spinler93894f62018-11-05 15:31:18 -0600141
142 powerButton->updatePressedTime();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800143 // emit pressed signal
144 powerButton->pressed();
145 }
146 else
147 {
148 phosphor::logging::log<phosphor::logging::level::DEBUG>(
149 "POWER_BUTTON: released");
Matt Spinler93894f62018-11-05 15:31:18 -0600150
151 auto now = std::chrono::steady_clock::now();
152 auto d = std::chrono::duration_cast<std::chrono::milliseconds>(
153 now - powerButton->getPressTime());
154
155 if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS))
156 {
157 powerButton->pressedLong();
158 }
159 else
160 {
161 // released
162 powerButton->released();
163 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800164 }
165
166 return 0;
167 }
168
169 private:
170 int fd;
171 sdbusplus::bus::bus& bus;
172 EventPtr& event;
173 sd_event_io_handler_t callbackHandler;
Matt Spinler93894f62018-11-05 15:31:18 -0600174 decltype(std::chrono::steady_clock::now()) pressedTime;
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800175};