blob: 879c65903409ffa318c76c788bbd7d130767db9b [file] [log] [blame]
/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include "power_button.hpp"
#include "button_handler.hpp"
// add the button iface class to registry
static ButtonIFRegister<PowerButton>
multiButtonRegister(phosphor::button::numberOfChassis());
void PowerButton::simPress()
{
pressed();
}
void PowerButton::simLongPress()
{
pressedLong();
}
void PowerButton::updatePressedTime()
{
pressedTime = std::chrono::steady_clock::now();
}
auto PowerButton::getPressTime() const
{
return pressedTime;
}
void PowerButton::handleEvent(sd_event_source* /* es */, int fd,
uint32_t /* revents */)
{
int n = -1;
char buf = '0';
n = ::lseek(fd, 0, SEEK_SET);
if (n < 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"POWER_BUTTON: lseek error!");
return;
}
n = ::read(fd, &buf, sizeof(buf));
if (n < 0)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"POWER_BUTTON: read error!");
return;
}
if (buf == '0')
{
phosphor::logging::log<phosphor::logging::level::DEBUG>(
"POWER_BUTTON: pressed");
updatePressedTime();
// emit pressed signal
pressed();
}
else
{
phosphor::logging::log<phosphor::logging::level::DEBUG>(
"POWER_BUTTON: released");
auto now = std::chrono::steady_clock::now();
auto d = std::chrono::duration_cast<std::chrono::microseconds>(
now - getPressTime());
// released
released(d.count());
}
}