1a9d39e30SKuiying Wang /*
2a9d39e30SKuiying Wang // Copyright (c) 2018 Intel Corporation
3a9d39e30SKuiying Wang //
4a9d39e30SKuiying Wang // Licensed under the Apache License, Version 2.0 (the "License");
5a9d39e30SKuiying Wang // you may not use this file except in compliance with the License.
6a9d39e30SKuiying Wang // You may obtain a copy of the License at
7a9d39e30SKuiying Wang //
8a9d39e30SKuiying Wang //      http://www.apache.org/licenses/LICENSE-2.0
9a9d39e30SKuiying Wang //
10a9d39e30SKuiying Wang // Unless required by applicable law or agreed to in writing, software
11a9d39e30SKuiying Wang // distributed under the License is distributed on an "AS IS" BASIS,
12a9d39e30SKuiying Wang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9d39e30SKuiying Wang // See the License for the specific language governing permissions and
14a9d39e30SKuiying Wang // limitations under the License.
15a9d39e30SKuiying Wang */
16a9d39e30SKuiying Wang 
17a9d39e30SKuiying Wang #pragma once
18*a1af329fSNaveen Moses #include "button_factory.hpp"
19*a1af329fSNaveen Moses #include "button_interface.hpp"
20a9d39e30SKuiying Wang #include "common.hpp"
21a9d39e30SKuiying Wang #include "gpio.hpp"
220d9377d2SPatrick Venture #include "xyz/openbmc_project/Chassis/Buttons/Power/server.hpp"
230d9377d2SPatrick Venture #include "xyz/openbmc_project/Chassis/Common/error.hpp"
240d9377d2SPatrick Venture 
250d9377d2SPatrick Venture #include <unistd.h>
260d9377d2SPatrick Venture 
2793894f6eSMatt Spinler #include <chrono>
280d9377d2SPatrick Venture #include <phosphor-logging/elog-errors.hpp>
29a9d39e30SKuiying Wang 
30*a1af329fSNaveen Moses static constexpr std::string_view POWER_BUTTON = "POWER_BUTTON";
31a9d39e30SKuiying Wang 
32*a1af329fSNaveen Moses class PowerButton
33*a1af329fSNaveen Moses     : public sdbusplus::server::object::object<
34*a1af329fSNaveen Moses           sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>,
35*a1af329fSNaveen Moses       public ButtonIface
36a9d39e30SKuiying Wang {
37*a1af329fSNaveen Moses   public:
380d9377d2SPatrick Venture     PowerButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
39*a1af329fSNaveen Moses                 buttonConfig& buttonCfg) :
40a9d39e30SKuiying Wang         sdbusplus::server::object::object<
41a9d39e30SKuiying Wang             sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
42a9d39e30SKuiying Wang             bus, path),
43*a1af329fSNaveen Moses         ButtonIface(bus, event, buttonCfg)
44a9d39e30SKuiying Wang     {
45*a1af329fSNaveen Moses         init();
46a9d39e30SKuiying Wang     }
47a9d39e30SKuiying Wang 
48a9d39e30SKuiying Wang     ~PowerButton()
49a9d39e30SKuiying Wang     {
50*a1af329fSNaveen Moses 
51*a1af329fSNaveen Moses         deInit();
52a9d39e30SKuiying Wang     }
53a9d39e30SKuiying Wang 
54a9d39e30SKuiying Wang     void simPress() override;
55a9d39e30SKuiying Wang     void simLongPress() override;
56a9d39e30SKuiying Wang 
57*a1af329fSNaveen Moses     static constexpr std::string_view getFormFactorName()
588605bdffSMatt Spinler     {
598605bdffSMatt Spinler         return POWER_BUTTON;
608605bdffSMatt Spinler     }
61*a1af329fSNaveen Moses     static constexpr const char* getDbusObjectPath()
6293894f6eSMatt Spinler     {
63*a1af329fSNaveen Moses         return POWER_DBUS_OBJECT_NAME;
6493894f6eSMatt Spinler     }
65*a1af329fSNaveen Moses     void updatePressedTime();
66*a1af329fSNaveen Moses     auto getPressTime() const;
67*a1af329fSNaveen Moses     void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
6893894f6eSMatt Spinler 
69*a1af329fSNaveen Moses   protected:
7093894f6eSMatt Spinler     decltype(std::chrono::steady_clock::now()) pressedTime;
71a9d39e30SKuiying Wang };
72