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
18 #include "config.h"
19 
20 #include "button_factory.hpp"
21 #include "button_interface.hpp"
22 #include "common.hpp"
23 #include "gpio.hpp"
24 #include "xyz/openbmc_project/Chassis/Buttons/Power/server.hpp"
25 #include "xyz/openbmc_project/Chassis/Common/error.hpp"
26 
27 #include <unistd.h>
28 
29 #include <phosphor-logging/elog-errors.hpp>
30 
31 #include <chrono>
32 
33 static constexpr std::string_view POWER_BUTTON = "POWER_BUTTON";
34 
35 class PowerButton :
36     public sdbusplus::server::object_t<
37         sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>,
38     public ButtonIface
39 {
40   public:
41     PowerButton(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
42                 ButtonConfig& buttonCfg) :
43         sdbusplus::server::object_t<
44             sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
45             bus, path),
46         ButtonIface(bus, event, buttonCfg)
47     {
48         init();
49     }
50 
51     ~PowerButton()
52     {
53         deInit();
54     }
55 
56     void simPress() override;
57     void simLongPress() override;
58 
59     static constexpr std::string_view getFormFactorName()
60     {
61         return POWER_BUTTON;
62     }
63     static constexpr const char* getDbusObjectPath()
64     {
65         return POWER_DBUS_OBJECT_NAME;
66     }
67     void updatePressedTime();
68     auto getPressTime() const;
69     void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
70 
71   protected:
72     decltype(std::chrono::steady_clock::now()) pressedTime;
73 };
74