blob: 96723b21122b8af7771c0b7361c19fe14b693f22 [file] [log] [blame]
Ben Tynerb797b3e2020-06-29 10:12:05 -05001#include <attn/attn_handler.hpp>
Ben Tynerb797b3e2020-06-29 10:12:05 -05002#include <attn/attn_monitor.hpp>
austinfcuibfa831a2022-01-26 15:37:07 -06003#include <util/trace.hpp>
Ben Tyner73ac3682020-01-09 10:46:47 -06004
5namespace attn
6{
7
8/** @brief Register a callback for gpio event */
9void AttnMonitor::scheduleGPIOEvent()
10{
Ben Tyner73ac3682020-01-09 10:46:47 -060011 // Register async callback, note that callback is a
12 // lambda function with "this" pointer captured
13 iv_gpioEventDescriptor.async_wait(
14 boost::asio::posix::stream_descriptor::wait_read,
15 [this](const boost::system::error_code& ec) {
16 if (ec)
17 {
austinfcuibfa831a2022-01-26 15:37:07 -060018 trace::err("GPIO Async wait error: %s", ec.message().c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060019 }
20 else
21 {
austinfcuibfa831a2022-01-26 15:37:07 -060022 trace::inf("Attention GPIO active");
Ben Tyner73ac3682020-01-09 10:46:47 -060023 handleGPIOEvent(); // gpio trigger detected
24 }
25 return;
26 }); // register async callback
27}
28
29/** @brief Handle the GPIO state change event */
30void AttnMonitor::handleGPIOEvent()
31{
32 gpiod_line_event gpioEvent;
33 std::string logMessage;
34
35 if (gpiod_line_event_read_fd(iv_gpioEventDescriptor.native_handle(),
36 &gpioEvent) < 0)
37 {
austinfcuibfa831a2022-01-26 15:37:07 -060038 trace::err("GPIO line read failed");
Ben Tyner73ac3682020-01-09 10:46:47 -060039 }
40 else
41 {
42 switch (gpiod_line_get_value(iv_gpioLine))
43 {
44 // active attention when gpio == 0
45 case 0:
Ben Tyner3fb52e52020-03-31 10:10:07 -050046 attnHandler(iv_config);
Ben Tyner73ac3682020-01-09 10:46:47 -060047 break;
48
49 // gpio == 1, GPIO handler should not be executing
50 case 1:
austinfcuibfa831a2022-01-26 15:37:07 -060051 trace::inf("GPIO handler out of sync");
Ben Tyner73ac3682020-01-09 10:46:47 -060052 break;
53
54 // unexpected value
55 default:
austinfcuibfa831a2022-01-26 15:37:07 -060056 trace::inf("GPIO line unexpected value");
Ben Tyner73ac3682020-01-09 10:46:47 -060057 }
58 }
59 scheduleGPIOEvent(); // continue monitoring gpio
60}
61
62/** @brief Request a GPIO line for monitoring attention events */
63void AttnMonitor::requestGPIOEvent()
64{
65 if (0 != gpiod_line_request(iv_gpioLine, &iv_gpioConfig, 0))
66 {
austinfcuibfa831a2022-01-26 15:37:07 -060067 trace::err("failed request for GPIO");
Ben Tyner73ac3682020-01-09 10:46:47 -060068 }
69 else
70 {
71 int gpioLineFd;
72
73 gpioLineFd = gpiod_line_event_get_fd(iv_gpioLine);
74 if (gpioLineFd < 0)
75 {
austinfcuibfa831a2022-01-26 15:37:07 -060076 trace::err("failed to get file descriptor");
Ben Tyner73ac3682020-01-09 10:46:47 -060077 }
78 else
79 {
80 // Register file descriptor for monitoring
81 iv_gpioEventDescriptor.assign(gpioLineFd);
82
83 // Start monitoring
84 scheduleGPIOEvent();
85 }
86 }
87}
88
89} // namespace attn