blob: 879c65903409ffa318c76c788bbd7d130767db9b [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#include "power_button.hpp"
18
Rush Chen31ce3752024-11-08 14:57:27 +080019#include "button_handler.hpp"
20
Naveen Mosesa1af3292021-12-15 11:47:01 +053021// add the button iface class to registry
Rush Chen31ce3752024-11-08 14:57:27 +080022static ButtonIFRegister<PowerButton>
23 multiButtonRegister(phosphor::button::numberOfChassis());
Naveen Mosesa1af3292021-12-15 11:47:01 +053024
Kuiying Wanga9d39e32018-08-14 13:47:32 +080025void PowerButton::simPress()
26{
27 pressed();
28}
29
30void PowerButton::simLongPress()
31{
32 pressedLong();
33}
Naveen Mosesa1af3292021-12-15 11:47:01 +053034
35void PowerButton::updatePressedTime()
36{
37 pressedTime = std::chrono::steady_clock::now();
38}
39
40auto PowerButton::getPressTime() const
41{
42 return pressedTime;
43}
44
George Liu94afa4b2022-06-20 13:36:43 +080045void PowerButton::handleEvent(sd_event_source* /* es */, int fd,
46 uint32_t /* revents */)
Naveen Mosesa1af3292021-12-15 11:47:01 +053047{
Naveen Mosesa1af3292021-12-15 11:47:01 +053048 int n = -1;
49 char buf = '0';
50
51 n = ::lseek(fd, 0, SEEK_SET);
52
53 if (n < 0)
54 {
55 phosphor::logging::log<phosphor::logging::level::ERR>(
56 "POWER_BUTTON: lseek error!");
57 return;
58 }
59
60 n = ::read(fd, &buf, sizeof(buf));
61 if (n < 0)
62 {
63 phosphor::logging::log<phosphor::logging::level::ERR>(
64 "POWER_BUTTON: read error!");
65 return;
66 }
67
68 if (buf == '0')
69 {
70 phosphor::logging::log<phosphor::logging::level::DEBUG>(
71 "POWER_BUTTON: pressed");
72
73 updatePressedTime();
74 // emit pressed signal
75 pressed();
76 }
77 else
78 {
79 phosphor::logging::log<phosphor::logging::level::DEBUG>(
80 "POWER_BUTTON: released");
81
82 auto now = std::chrono::steady_clock::now();
DelphineCCChiu395c7642023-04-19 10:46:47 +080083 auto d = std::chrono::duration_cast<std::chrono::microseconds>(
Naveen Mosesa1af3292021-12-15 11:47:01 +053084 now - getPressTime());
DelphineCCChiu395c7642023-04-19 10:46:47 +080085 // released
86 released(d.count());
Naveen Mosesa1af3292021-12-15 11:47:01 +053087 }
88}