Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 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 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 19 | // add the button iface class to registry |
| 20 | static ButtonIFRegister<PowerButton> buttonRegister; |
| 21 | |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 22 | void PowerButton::simPress() |
| 23 | { |
| 24 | pressed(); |
| 25 | } |
| 26 | |
| 27 | void PowerButton::simLongPress() |
| 28 | { |
| 29 | pressedLong(); |
| 30 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 31 | |
| 32 | void PowerButton::updatePressedTime() |
| 33 | { |
| 34 | pressedTime = std::chrono::steady_clock::now(); |
| 35 | } |
| 36 | |
| 37 | auto PowerButton::getPressTime() const |
| 38 | { |
| 39 | return pressedTime; |
| 40 | } |
| 41 | |
George Liu | 94afa4b | 2022-06-20 13:36:43 +0800 | [diff] [blame^] | 42 | void PowerButton::handleEvent(sd_event_source* /* es */, int fd, |
| 43 | uint32_t /* revents */) |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 44 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 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 | } |