Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 1 | /* |
| 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 Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 18 | #include "common.hpp" |
| 19 | #include "gpio.hpp" |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 20 | #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 Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 26 | |
| 27 | const static constexpr char* RESET_BUTTON = "RESET_BUTTON"; |
| 28 | |
| 29 | struct ResetButton |
| 30 | : sdbusplus::server::object::object< |
| 31 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Reset> |
| 32 | { |
| 33 | |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 34 | ResetButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event, |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 35 | 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 Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 50 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 51 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 52 | } |
| 53 | |
Matt Spinler | b3d86c9 | 2018-11-15 16:13:19 -0600 | [diff] [blame^] | 54 | char buf; |
| 55 | ::read(fd, &buf, sizeof(buf)); |
| 56 | |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 57 | ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI, |
| 58 | callbackHandler, this); |
| 59 | if (ret < 0) |
| 60 | { |
| 61 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 62 | "RESET_BUTTON: failed to add to event loop"); |
| 63 | ::closeGpio(fd); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 64 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 65 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | ~ResetButton() |
| 70 | { |
| 71 | ::closeGpio(fd); |
| 72 | } |
| 73 | |
| 74 | void simPress() override; |
| 75 | |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 76 | static const char* getGpioName() |
| 77 | { |
| 78 | return RESET_BUTTON; |
| 79 | } |
| 80 | |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 81 | static int EventHandler(sd_event_source* es, int fd, uint32_t revents, |
| 82 | void* userdata) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 83 | { |
| 84 | |
| 85 | int n = -1; |
| 86 | char buf = '0'; |
| 87 | |
| 88 | if (!userdata) |
| 89 | { |
| 90 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 91 | "RESET_BUTTON: userdata null!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 92 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 93 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | ResetButton* resetButton = static_cast<ResetButton*>(userdata); |
| 97 | |
| 98 | if (!resetButton) |
| 99 | { |
| 100 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 101 | "RESET_BUTTON: null pointer!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 102 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 103 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | n = ::lseek(fd, 0, SEEK_SET); |
| 107 | |
| 108 | if (n < 0) |
| 109 | { |
| 110 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 111 | "RESET_BUTTON: lseek error!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 112 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 113 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | n = ::read(fd, &buf, sizeof(buf)); |
| 117 | if (n < 0) |
| 118 | { |
| 119 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 120 | "RESET_BUTTON: read error!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 121 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 122 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (buf == '0') |
| 126 | { |
| 127 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 128 | "RESET_BUTTON: pressed"); |
| 129 | // emit pressed signal |
| 130 | resetButton->pressed(); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 135 | "RESET_BUTTON: released"); |
| 136 | // released |
| 137 | resetButton->released(); |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | int fd; |
| 145 | sdbusplus::bus::bus& bus; |
| 146 | EventPtr& event; |
| 147 | sd_event_io_handler_t callbackHandler; |
| 148 | }; |