blob: b10f241712018f4f828900af642d8fca52e66697 [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/Reset/server.hpp"
21#include "xyz/openbmc_project/Chassis/Common/error.hpp"
22
23#include <unistd.h>
24
25#include <phosphor-logging/elog-errors.hpp>
Kuiying Wanga9d39e32018-08-14 13:47:32 +080026
27const static constexpr char* RESET_BUTTON = "RESET_BUTTON";
28
29struct ResetButton
30 : sdbusplus::server::object::object<
31 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Reset>
32{
33
Patrick Venture0d9377d2018-11-01 19:34:59 -070034 ResetButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
Kuiying Wanga9d39e32018-08-14 13:47:32 +080035 sd_event_io_handler_t handler = ResetButton::EventHandler) :
36 sdbusplus::server::object::object<
37 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Reset>(
38 bus, path),
39 fd(-1), bus(bus), event(event), callbackHandler(handler)
40 {
41
42 int ret = -1;
43
44 // config gpio
45 ret = ::configGpio(RESET_BUTTON, &fd, bus);
46 if (ret < 0)
47 {
48 phosphor::logging::log<phosphor::logging::level::ERR>(
49 "RESET_BUTTON: failed to config GPIO");
Patrick Venture0d9377d2018-11-01 19:34:59 -070050 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
51 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080052 }
53
54 ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
55 callbackHandler, this);
56 if (ret < 0)
57 {
58 phosphor::logging::log<phosphor::logging::level::ERR>(
59 "RESET_BUTTON: failed to add to event loop");
60 ::closeGpio(fd);
Patrick Venture0d9377d2018-11-01 19:34:59 -070061 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
62 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080063 }
64 }
65
66 ~ResetButton()
67 {
68 ::closeGpio(fd);
69 }
70
71 void simPress() override;
72
Patrick Venture0d9377d2018-11-01 19:34:59 -070073 static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
74 void* userdata)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080075 {
76
77 int n = -1;
78 char buf = '0';
79
80 if (!userdata)
81 {
82 phosphor::logging::log<phosphor::logging::level::ERR>(
83 "RESET_BUTTON: userdata null!");
Patrick Venture0d9377d2018-11-01 19:34:59 -070084 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
85 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080086 }
87
88 ResetButton* resetButton = static_cast<ResetButton*>(userdata);
89
90 if (!resetButton)
91 {
92 phosphor::logging::log<phosphor::logging::level::ERR>(
93 "RESET_BUTTON: null pointer!");
Patrick Venture0d9377d2018-11-01 19:34:59 -070094 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
95 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080096 }
97
98 n = ::lseek(fd, 0, SEEK_SET);
99
100 if (n < 0)
101 {
102 phosphor::logging::log<phosphor::logging::level::ERR>(
103 "RESET_BUTTON: lseek error!");
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 n = ::read(fd, &buf, sizeof(buf));
109 if (n < 0)
110 {
111 phosphor::logging::log<phosphor::logging::level::ERR>(
112 "RESET_BUTTON: read error!");
Patrick Venture0d9377d2018-11-01 19:34:59 -0700113 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
114 IOError();
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800115 }
116
117 if (buf == '0')
118 {
119 phosphor::logging::log<phosphor::logging::level::DEBUG>(
120 "RESET_BUTTON: pressed");
121 // emit pressed signal
122 resetButton->pressed();
123 }
124 else
125 {
126 phosphor::logging::log<phosphor::logging::level::DEBUG>(
127 "RESET_BUTTON: released");
128 // released
129 resetButton->released();
130 }
131
132 return 0;
133 }
134
135 private:
136 int fd;
137 sdbusplus::bus::bus& bus;
138 EventPtr& event;
139 sd_event_io_handler_t callbackHandler;
140};