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