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 #include "button_handler.hpp" 20 21 // add the button iface class to registry 22 static ButtonIFRegister<PowerButton> 23 multiButtonRegister(phosphor::button::numberOfChassis()); 24 25 void PowerButton::simPress() 26 { 27 pressed(); 28 } 29 30 void PowerButton::simLongPress() 31 { 32 pressedLong(); 33 } 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 45 void PowerButton::handleEvent(sd_event_source* /* es */, int fd, 46 uint32_t /* revents */) 47 { 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(); 83 auto d = std::chrono::duration_cast<std::chrono::microseconds>( 84 now - getPressTime()); 85 // released 86 released(d.count()); 87 } 88 } 89