blob: 2cf28e4f74f042a17bc95276d3740debcae15b29 [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
Naveen Mosesa1af3292021-12-15 11:47:01 +053018#include "button_factory.hpp"
19#include "button_interface.hpp"
Kuiying Wanga9d39e32018-08-14 13:47:32 +080020#include "common.hpp"
Delphine CC Chiu15c60e22024-04-12 13:01:32 -050021#include "config.hpp"
Kuiying Wanga9d39e32018-08-14 13:47:32 +080022#include "gpio.hpp"
Patrick Venture0d9377d2018-11-01 19:34:59 -070023#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 Wanga9d39e32018-08-14 13:47:32 +080029
George Liu5b98f4d2022-06-20 13:31:14 +080030#include <chrono>
31
Naveen Mosesa1af3292021-12-15 11:47:01 +053032static constexpr std::string_view POWER_BUTTON = "POWER_BUTTON";
Kuiying Wanga9d39e32018-08-14 13:47:32 +080033
George Liu5b98f4d2022-06-20 13:31:14 +080034class PowerButton :
Patrick Williams9a529a62022-07-22 19:26:54 -050035 public sdbusplus::server::object_t<
George Liu5b98f4d2022-06-20 13:31:14 +080036 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>,
37 public ButtonIface
Kuiying Wanga9d39e32018-08-14 13:47:32 +080038{
Naveen Mosesa1af3292021-12-15 11:47:01 +053039 public:
Patrick Williams9a529a62022-07-22 19:26:54 -050040 PowerButton(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080041 ButtonConfig& buttonCfg) :
Patrick Williams9a529a62022-07-22 19:26:54 -050042 sdbusplus::server::object_t<
Kuiying Wanga9d39e32018-08-14 13:47:32 +080043 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::Power>(
44 bus, path),
Naveen Mosesa1af3292021-12-15 11:47:01 +053045 ButtonIface(bus, event, buttonCfg)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080046 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053047 init();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080048 }
49
50 ~PowerButton()
51 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053052 deInit();
Kuiying Wanga9d39e32018-08-14 13:47:32 +080053 }
54
55 void simPress() override;
56 void simLongPress() override;
57
Naveen Mosesa1af3292021-12-15 11:47:01 +053058 static constexpr std::string_view getFormFactorName()
Matt Spinler8605bdf2018-11-05 14:55:46 -060059 {
60 return POWER_BUTTON;
61 }
Naveen Mosesa1af3292021-12-15 11:47:01 +053062 static constexpr const char* getDbusObjectPath()
Matt Spinler93894f62018-11-05 15:31:18 -060063 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053064 return POWER_DBUS_OBJECT_NAME;
Matt Spinler93894f62018-11-05 15:31:18 -060065 }
Naveen Mosesa1af3292021-12-15 11:47:01 +053066 void updatePressedTime();
67 auto getPressTime() const;
68 void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
Matt Spinler93894f62018-11-05 15:31:18 -060069
Naveen Mosesa1af3292021-12-15 11:47:01 +053070 protected:
Matt Spinler93894f62018-11-05 15:31:18 -060071 decltype(std::chrono::steady_clock::now()) pressedTime;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080072};