blob: 549b51f45f51527c785506c3c03cab471a34733a [file] [log] [blame]
Kuiying Wanga9d39e32018-08-14 13:47:32 +08001/*
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
George Liud6a1bae2022-06-20 13:47:31 +080018#include "config.h"
19
Naveen Mosesa1af3292021-12-15 11:47:01 +053020#include "button_factory.hpp"
21#include "button_interface.hpp"
Kuiying Wanga9d39e32018-08-14 13:47:32 +080022#include "common.hpp"
23#include "gpio.hpp"
Patrick Venture0d9377d2018-11-01 19:34:59 -070024#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>
Kuiying Wanga9d39e32018-08-14 13:47:32 +080030
George Liu5b98f4d2022-06-20 13:31:14 +080031#include <chrono>
32
Naveen Mosesa1af3292021-12-15 11:47:01 +053033static constexpr std::string_view POWER_BUTTON = "POWER_BUTTON";
Kuiying Wanga9d39e32018-08-14 13:47:32 +080034
George Liu5b98f4d2022-06-20 13:31:14 +080035class PowerButton :
Patrick Williams9a529a62022-07-22 19:26:54 -050036 public sdbusplus::server::object_t<
George Liu5b98f4d2022-06-20 13:31:14 +080037 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>,
38 public ButtonIface
Kuiying Wanga9d39e32018-08-14 13:47:32 +080039{
Naveen Mosesa1af3292021-12-15 11:47:01 +053040 public:
Patrick Williams9a529a62022-07-22 19:26:54 -050041 PowerButton(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080042 ButtonConfig& buttonCfg) :
Patrick Williams9a529a62022-07-22 19:26:54 -050043 sdbusplus::server::object_t<
Kuiying Wanga9d39e32018-08-14 13:47:32 +080044 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
45 bus, path),
Naveen Mosesa1af3292021-12-15 11:47:01 +053046 ButtonIface(bus, event, buttonCfg)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080047 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053048 init();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080049 }
50
51 ~PowerButton()
52 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053053 deInit();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080054 }
55
56 void simPress() override;
57 void simLongPress() override;
58
Naveen Mosesa1af3292021-12-15 11:47:01 +053059 static constexpr std::string_view getFormFactorName()
Matt Spinler8605bdf2018-11-05 14:55:46 -060060 {
61 return POWER_BUTTON;
62 }
Naveen Mosesa1af3292021-12-15 11:47:01 +053063 static constexpr const char* getDbusObjectPath()
Matt Spinler93894f62018-11-05 15:31:18 -060064 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053065 return POWER_DBUS_OBJECT_NAME;
Matt Spinler93894f62018-11-05 15:31:18 -060066 }
Naveen Mosesa1af3292021-12-15 11:47:01 +053067 void updatePressedTime();
68 auto getPressTime() const;
69 void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
Matt Spinler93894f62018-11-05 15:31:18 -060070
Naveen Mosesa1af3292021-12-15 11:47:01 +053071 protected:
Matt Spinler93894f62018-11-05 15:31:18 -060072 decltype(std::chrono::steady_clock::now()) pressedTime;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080073};