1 /*
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
19 // add the button iface class to registry
20 static ButtonIFRegister<PowerButton> buttonRegister;
21
simPress()22 void PowerButton::simPress()
23 {
24 pressed();
25 }
26
simLongPress()27 void PowerButton::simLongPress()
28 {
29 pressedLong();
30 }
31
updatePressedTime()32 void PowerButton::updatePressedTime()
33 {
34 pressedTime = std::chrono::steady_clock::now();
35 }
36
getPressTime() const37 auto PowerButton::getPressTime() const
38 {
39 return pressedTime;
40 }
41
handleEvent(sd_event_source *,int fd,uint32_t)42 void PowerButton::handleEvent(sd_event_source* /* es */, int fd,
43 uint32_t /* revents */)
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::microseconds>(
81 now - getPressTime());
82 // released
83 released(d.count());
84 }
85 }
86