blob: 283b149fb91e3b679c4406cee7ba50d413bf115d [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,
Naveen Mosesdd5495c2021-12-03 22:40:46 +053036 buttonConfig& buttonCfg,
Kuiying Wanga9d39e32018-08-14 13:47:32 +080037 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),
Naveen Mosesdd5495c2021-12-03 22:40:46 +053041 fd(-1), buttonIFConfig(buttonCfg), bus(bus), event(event),
42 callbackHandler(handler)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080043 {
44
45 int ret = -1;
46
Naveen Mosesdd5495c2021-12-03 22:40:46 +053047 // config group gpio based on the gpio defs read from the json file
48 ret = configGroupGpio(bus, buttonIFConfig);
49
Kuiying Wanga9d39e32018-08-14 13:47:32 +080050 if (ret < 0)
51 {
52 phosphor::logging::log<phosphor::logging::level::ERR>(
53 "POWER_BUTTON: failed to config GPIO");
Patrick Venture0d9377d2018-11-01 19:34:59 -070054 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
55 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080056 }
57
Naveen Mosesdd5495c2021-12-03 22:40:46 +053058 // initialize the button io fd from the buttonConfig
59 // which has fd stored when configGroupGpio is called
60 fd = buttonIFConfig.gpios[0].fd;
61
Matt Spinlerb3d86c92018-11-15 16:13:19 -060062 char buf;
63 ::read(fd, &buf, sizeof(buf));
64
Kuiying Wanga9d39e32018-08-14 13:47:32 +080065 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);
Patrick Venture0d9377d2018-11-01 19:34:59 -070072 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
73 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080074 }
75 }
76
77 ~PowerButton()
78 {
79 ::closeGpio(fd);
80 }
81
82 void simPress() override;
83 void simLongPress() override;
84
Naveen Mosesdd5495c2021-12-03 22:40:46 +053085 static const std::string getGpioName()
Matt Spinler8605bdf2018-11-05 14:55:46 -060086 {
87 return POWER_BUTTON;
88 }
89
Matt Spinler93894f62018-11-05 15:31:18 -060090 void updatePressedTime()
91 {
92 pressedTime = std::chrono::steady_clock::now();
93 }
94
95 auto getPressTime() const
96 {
97 return pressedTime;
98 }
99
Patrick Venture0d9377d2018-11-01 19:34:59 -0700100 static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
101 void* userdata)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800102 {
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!");
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 PowerButton* powerButton = static_cast<PowerButton*>(userdata);
116
117 if (!powerButton)
118 {
119 phosphor::logging::log<phosphor::logging::level::ERR>(
120 "POWER_BUTTON: null pointer!");
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 = ::lseek(fd, 0, SEEK_SET);
126
127 if (n < 0)
128 {
129 phosphor::logging::log<phosphor::logging::level::ERR>(
130 "POWER_BUTTON: lseek error!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700131 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
132 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800133 }
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!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700140 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
141 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800142 }
143
144 if (buf == '0')
145 {
146 phosphor::logging::log<phosphor::logging::level::DEBUG>(
147 "POWER_BUTTON: pressed");
Matt Spinler93894f62018-11-05 15:31:18 -0600148
149 powerButton->updatePressedTime();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800150 // emit pressed signal
151 powerButton->pressed();
152 }
153 else
154 {
155 phosphor::logging::log<phosphor::logging::level::DEBUG>(
156 "POWER_BUTTON: released");
Matt Spinler93894f62018-11-05 15:31:18 -0600157
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 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800171 }
172
173 return 0;
174 }
175
176 private:
177 int fd;
Naveen Mosesdd5495c2021-12-03 22:40:46 +0530178 buttonConfig buttonIFConfig; // button iface io details
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800179 sdbusplus::bus::bus& bus;
180 EventPtr& event;
181 sd_event_io_handler_t callbackHandler;
Matt Spinler93894f62018-11-05 15:31:18 -0600182 decltype(std::chrono::steady_clock::now()) pressedTime;
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800183};