blob: 2e69154c8c5df6caff790929634517bc136bf558 [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
Naveen Mosesa1af3292021-12-15 11:47:01 +053019// add the button iface class to registry
20static ButtonIFRegister<PowerButton> buttonRegister;
21
Kuiying Wanga9d39e32018-08-14 13:47:32 +080022void PowerButton::simPress()
23{
24 pressed();
25}
26
27void PowerButton::simLongPress()
28{
29 pressedLong();
30}
Naveen Mosesa1af3292021-12-15 11:47:01 +053031
32void PowerButton::updatePressedTime()
33{
34 pressedTime = std::chrono::steady_clock::now();
35}
36
37auto PowerButton::getPressTime() const
38{
39 return pressedTime;
40}
41
42void PowerButton::handleEvent(sd_event_source* es, int fd, uint32_t revents)
43{
44
45 int n = -1;
46 char buf = '0';
47
48 n = ::lseek(fd, 0, SEEK_SET);
49
50 if (n < 0)
51 {
52 phosphor::logging::log<phosphor::logging::level::ERR>(
53 "POWER_BUTTON: lseek error!");
54 return;
55 }
56
57 n = ::read(fd, &buf, sizeof(buf));
58 if (n < 0)
59 {
60 phosphor::logging::log<phosphor::logging::level::ERR>(
61 "POWER_BUTTON: read error!");
62 return;
63 }
64
65 if (buf == '0')
66 {
67 phosphor::logging::log<phosphor::logging::level::DEBUG>(
68 "POWER_BUTTON: pressed");
69
70 updatePressedTime();
71 // emit pressed signal
72 pressed();
73 }
74 else
75 {
76 phosphor::logging::log<phosphor::logging::level::DEBUG>(
77 "POWER_BUTTON: released");
78
79 auto now = std::chrono::steady_clock::now();
80 auto d = std::chrono::duration_cast<std::chrono::milliseconds>(
81 now - getPressTime());
82
83 if (d > std::chrono::milliseconds(LONG_PRESS_TIME_MS))
84 {
85 pressedLong();
86 }
87 else
88 {
89 // released
90 released();
91 }
92 }
93}