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 | |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 19 | #include "button_handler.hpp" |
| 20 | |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 21 | // add the button iface class to registry |
Rush Chen | 31ce375 | 2024-11-08 14:57:27 +0800 | [diff] [blame^] | 22 | static ButtonIFRegister<PowerButton> |
| 23 | multiButtonRegister(phosphor::button::numberOfChassis()); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 24 | |
Kuiying Wang | a9d39e3 | 2018-08-14 13:47:32 +0800 | [diff] [blame] | 25 | void PowerButton::simPress() |
| 26 | { |
| 27 | pressed(); |
| 28 | } |
| 29 | |
| 30 | void PowerButton::simLongPress() |
| 31 | { |
| 32 | pressedLong(); |
| 33 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 34 | |
| 35 | void PowerButton::updatePressedTime() |
| 36 | { |
| 37 | pressedTime = std::chrono::steady_clock::now(); |
| 38 | } |
| 39 | |
| 40 | auto PowerButton::getPressTime() const |
| 41 | { |
| 42 | return pressedTime; |
| 43 | } |
| 44 | |
George Liu | 94afa4b | 2022-06-20 13:36:43 +0800 | [diff] [blame] | 45 | void PowerButton::handleEvent(sd_event_source* /* es */, int fd, |
| 46 | uint32_t /* revents */) |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 47 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 48 | 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(); |
DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 83 | auto d = std::chrono::duration_cast<std::chrono::microseconds>( |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 84 | now - getPressTime()); |
DelphineCCChiu | 395c764 | 2023-04-19 10:46:47 +0800 | [diff] [blame] | 85 | // released |
| 86 | released(d.count()); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 87 | } |
| 88 | } |