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 |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 18 | #include "button_factory.hpp" |
| 19 | #include "button_interface.hpp" |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 20 | #include "common.hpp" |
Delphine CC Chiu | 15c60e2 | 2024-04-12 13:01:32 -0500 | [diff] [blame] | 21 | #include "config.hpp" |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 22 | #include "gpio.hpp" |
Patrick Venture | 0d9377d | 2018-11-01 19:34:59 -0700 | [diff] [blame] | 23 | #include "xyz/openbmc_project/Chassis/Buttons/Power/server.hpp" |
| 24 | #include "xyz/openbmc_project/Chassis/Common/error.hpp" |
| 25 | |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <phosphor-logging/elog-errors.hpp> |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 29 | |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 30 | #include <chrono> |
| 31 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 32 | static constexpr std::string_view POWER_BUTTON = "POWER_BUTTON"; |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 33 | |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 34 | class PowerButton : |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 35 | public sdbusplus::server::object_t< |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 36 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>, |
| 37 | public ButtonIface |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 38 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 39 | public: |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 40 | PowerButton(sdbusplus::bus_t& bus, const char* path, EventPtr& event, |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 41 | ButtonConfig& buttonCfg) : |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 42 | sdbusplus::server::object_t< |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 43 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>( |
| 44 | bus, path), |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 45 | ButtonIface(bus, event, buttonCfg) |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 46 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 47 | init(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ~PowerButton() |
| 51 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 52 | deInit(); |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void simPress() override; |
| 56 | void simLongPress() override; |
| 57 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 58 | static constexpr std::string_view getFormFactorName() |
Matt Spinler | 8605bdf | 2018-11-05 14:55:46 -0600 | [diff] [blame] | 59 | { |
| 60 | return POWER_BUTTON; |
| 61 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 62 | static constexpr const char* getDbusObjectPath() |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 63 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 64 | return POWER_DBUS_OBJECT_NAME; |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 65 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 66 | void updatePressedTime(); |
| 67 | auto getPressTime() const; |
| 68 | void handleEvent(sd_event_source* es, int fd, uint32_t revents) override; |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 69 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 70 | protected: |
Matt Spinler | 93894f6 | 2018-11-05 15:31:18 -0600 | [diff] [blame] | 71 | decltype(std::chrono::steady_clock::now()) pressedTime; |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 72 | }; |