blob: 13b7f15e71f538cef4747ecfed398b17d5b5156c [file] [log] [blame]
#include "attn_monitor.hpp"
#include "attn_handler.hpp"
#include <logging.hpp>
namespace attn
{
/** @brief Register a callback for gpio event */
void AttnMonitor::scheduleGPIOEvent()
{
std::string logMessage = "... waiting for events ...";
log<level::INFO>(logMessage.c_str());
// Register async callback, note that callback is a
// lambda function with "this" pointer captured
iv_gpioEventDescriptor.async_wait(
boost::asio::posix::stream_descriptor::wait_read,
[this](const boost::system::error_code& ec) {
if (ec)
{
std::string logMessage =
"GPIO Async wait error: " + std::string(ec.message());
log<level::INFO>(logMessage.c_str());
}
else
{
handleGPIOEvent(); // gpio trigger detected
}
return;
}); // register async callback
}
/** @brief Handle the GPIO state change event */
void AttnMonitor::handleGPIOEvent()
{
gpiod_line_event gpioEvent;
std::string logMessage;
if (gpiod_line_event_read_fd(iv_gpioEventDescriptor.native_handle(),
&gpioEvent) < 0)
{
logMessage = "GPIO line read failed";
log<level::INFO>(logMessage.c_str());
}
else
{
switch (gpiod_line_get_value(iv_gpioLine))
{
// active attention when gpio == 0
case 0:
attnHandler(iv_config);
break;
// gpio == 1, GPIO handler should not be executing
case 1:
logMessage = "GPIO handler out of sync";
log<level::INFO>(logMessage.c_str());
break;
// unexpected value
default:
logMessage = "GPIO line unexpected value";
log<level::INFO>(logMessage.c_str());
}
}
scheduleGPIOEvent(); // continue monitoring gpio
}
/** @brief Request a GPIO line for monitoring attention events */
void AttnMonitor::requestGPIOEvent()
{
if (0 != gpiod_line_request(iv_gpioLine, &iv_gpioConfig, 0))
{
std::string logMessage = "failed request for GPIO";
log<level::INFO>(logMessage.c_str());
}
else
{
int gpioLineFd;
gpioLineFd = gpiod_line_event_get_fd(iv_gpioLine);
if (gpioLineFd < 0)
{
std::string logMessage = "failed to get file descriptor";
log<level::INFO>(logMessage.c_str());
}
else
{
// Register file descriptor for monitoring
iv_gpioEventDescriptor.assign(gpioLineFd);
// Start monitoring
scheduleGPIOEvent();
}
}
}
} // namespace attn