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 | |
| 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* POWER_BUTTON = "POWER_BUTTON"; |
| 28 | |
| 29 | struct PowerButton |
| 30 | : sdbusplus::server::object::object< |
| 31 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power> |
| 32 | { |
| 33 | |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 34 | PowerButton(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 = PowerButton::EventHandler) : |
| 36 | sdbusplus::server::object::object< |
| 37 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>( |
| 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(POWER_BUTTON, &fd, bus); |
| 46 | if (ret < 0) |
| 47 | { |
| 48 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 49 | "POWER_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 | |
| 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 | "POWER_BUTTON: failed to add to event loop"); |
| 60 | ::closeGpio(fd); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 61 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 62 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | ~PowerButton() |
| 67 | { |
| 68 | ::closeGpio(fd); |
| 69 | } |
| 70 | |
| 71 | void simPress() override; |
| 72 | void simLongPress() override; |
| 73 | |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 74 | static int EventHandler(sd_event_source* es, int fd, uint32_t revents, |
| 75 | void* userdata) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 76 | { |
| 77 | |
| 78 | int n = -1; |
| 79 | char buf = '0'; |
| 80 | |
| 81 | if (!userdata) |
| 82 | { |
| 83 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 84 | "POWER_BUTTON: userdata null!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 85 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 86 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | PowerButton* powerButton = static_cast<PowerButton*>(userdata); |
| 90 | |
| 91 | if (!powerButton) |
| 92 | { |
| 93 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 94 | "POWER_BUTTON: null pointer!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 95 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 96 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | n = ::lseek(fd, 0, SEEK_SET); |
| 100 | |
| 101 | if (n < 0) |
| 102 | { |
| 103 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 104 | "POWER_BUTTON: lseek error!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 105 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 106 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | n = ::read(fd, &buf, sizeof(buf)); |
| 110 | if (n < 0) |
| 111 | { |
| 112 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 113 | "POWER_BUTTON: read error!"); |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame^] | 114 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 115 | IOError(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (buf == '0') |
| 119 | { |
| 120 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 121 | "POWER_BUTTON: pressed"); |
| 122 | // emit pressed signal |
| 123 | powerButton->pressed(); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 128 | "POWER_BUTTON: released"); |
| 129 | // released |
| 130 | powerButton->released(); |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | int fd; |
| 138 | sdbusplus::bus::bus& bus; |
| 139 | EventPtr& event; |
| 140 | sd_event_io_handler_t callbackHandler; |
| 141 | }; |