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/Power/server.hpp" |
| 21 | #include "xyz/openbmc_project/Chassis/Common/error.hpp" |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 25 | #include <chrono> |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 26 | #include <phosphor-logging/elog-errors.hpp> |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 27 | |
| 28 | const static constexpr char* POWER_BUTTON = "POWER_BUTTON"; |
| 29 | |
| 30 | struct PowerButton |
| 31 | : sdbusplus::server::object::object< |
| 32 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power> |
| 33 | { |
| 34 | |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 35 | PowerButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event, |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 36 | 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 Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 51 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 52 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI, |
| 56 | callbackHandler, this); |
| 57 | if (ret < 0) |
| 58 | { |
| 59 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 60 | "POWER_BUTTON: failed to add to event loop"); |
| 61 | ::closeGpio(fd); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 62 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 63 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | ~PowerButton() |
| 68 | { |
| 69 | ::closeGpio(fd); |
| 70 | } |
| 71 | |
| 72 | void simPress() override; |
| 73 | void simLongPress() override; |
| 74 | |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 75 | static const char* getGpioName() |
| 76 | { |
| 77 | return POWER_BUTTON; |
| 78 | } |
| 79 | |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 80 | void updatePressedTime() |
| 81 | { |
| 82 | pressedTime = std::chrono::steady_clock::now(); |
| 83 | } |
| 84 | |
| 85 | auto getPressTime() const |
| 86 | { |
| 87 | return pressedTime; |
| 88 | } |
| 89 | |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 90 | static int EventHandler(sd_event_source* es, int fd, uint32_t revents, |
| 91 | void* userdata) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 92 | { |
| 93 | |
| 94 | int n = -1; |
| 95 | char buf = '0'; |
| 96 | |
| 97 | if (!userdata) |
| 98 | { |
| 99 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 100 | "POWER_BUTTON: userdata null!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 101 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 102 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | PowerButton* powerButton = static_cast<PowerButton*>(userdata); |
| 106 | |
| 107 | if (!powerButton) |
| 108 | { |
| 109 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 110 | "POWER_BUTTON: null pointer!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 111 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 112 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | n = ::lseek(fd, 0, SEEK_SET); |
| 116 | |
| 117 | if (n < 0) |
| 118 | { |
| 119 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 120 | "POWER_BUTTON: lseek 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 | n = ::read(fd, &buf, sizeof(buf)); |
| 126 | if (n < 0) |
| 127 | { |
| 128 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 129 | "POWER_BUTTON: read error!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 130 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 131 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (buf == '0') |
| 135 | { |
| 136 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 137 | "POWER_BUTTON: pressed"); |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 138 | |
| 139 | powerButton->updatePressedTime(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 140 | // emit pressed signal |
| 141 | powerButton->pressed(); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 146 | "POWER_BUTTON: released"); |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 147 | |
| 148 | auto now = std::chrono::steady_clock::now(); |
| 149 | auto d = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 150 | now - powerButton->getPressTime()); |
| 151 | |
| 152 | if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS)) |
| 153 | { |
| 154 | powerButton->pressedLong(); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | // released |
| 159 | powerButton->released(); |
| 160 | } |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | int fd; |
| 168 | sdbusplus::bus::bus& bus; |
| 169 | EventPtr& event; |
| 170 | sd_event_io_handler_t callbackHandler; |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 171 | decltype(std::chrono::steady_clock::now()) pressedTime; |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 172 | }; |