blob: c114bdf0b0bae721bba72d91c877b376c56d7db1 [file] [log] [blame]
Ben Tynerb797b3e2020-06-29 10:12:05 -05001#include <attn/attn_handler.hpp>
2#include <attn/attn_logging.hpp>
3#include <attn/attn_monitor.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 Tyner18db69a2020-08-20 08:23:07 -050011 // std::string logMessage = "... waiting for events ...";
12 // trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060013
14 // Register async callback, note that callback is a
15 // lambda function with "this" pointer captured
16 iv_gpioEventDescriptor.async_wait(
17 boost::asio::posix::stream_descriptor::wait_read,
18 [this](const boost::system::error_code& ec) {
19 if (ec)
20 {
Ben Tyner9ae5ca42020-02-28 13:13:50 -060021 std::string logMessage =
22 "GPIO Async wait error: " + std::string(ec.message());
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050023 trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060024 }
25 else
26 {
27 handleGPIOEvent(); // gpio trigger detected
28 }
29 return;
30 }); // register async callback
31}
32
33/** @brief Handle the GPIO state change event */
34void AttnMonitor::handleGPIOEvent()
35{
36 gpiod_line_event gpioEvent;
37 std::string logMessage;
38
39 if (gpiod_line_event_read_fd(iv_gpioEventDescriptor.native_handle(),
40 &gpioEvent) < 0)
41 {
Ben Tyner9ae5ca42020-02-28 13:13:50 -060042 logMessage = "GPIO line read failed";
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050043 trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060044 }
45 else
46 {
47 switch (gpiod_line_get_value(iv_gpioLine))
48 {
49 // active attention when gpio == 0
50 case 0:
Ben Tyner3fb52e52020-03-31 10:10:07 -050051 attnHandler(iv_config);
Ben Tyner73ac3682020-01-09 10:46:47 -060052 break;
53
54 // gpio == 1, GPIO handler should not be executing
55 case 1:
Ben Tyner9ae5ca42020-02-28 13:13:50 -060056 logMessage = "GPIO handler out of sync";
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050057 trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060058 break;
59
60 // unexpected value
61 default:
Ben Tyner9ae5ca42020-02-28 13:13:50 -060062 logMessage = "GPIO line unexpected value";
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050063 trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060064 }
65 }
66 scheduleGPIOEvent(); // continue monitoring gpio
67}
68
69/** @brief Request a GPIO line for monitoring attention events */
70void AttnMonitor::requestGPIOEvent()
71{
72 if (0 != gpiod_line_request(iv_gpioLine, &iv_gpioConfig, 0))
73 {
Ben Tyner9ae5ca42020-02-28 13:13:50 -060074 std::string logMessage = "failed request for GPIO";
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050075 trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060076 }
77 else
78 {
79 int gpioLineFd;
80
81 gpioLineFd = gpiod_line_event_get_fd(iv_gpioLine);
82 if (gpioLineFd < 0)
83 {
Ben Tyner9ae5ca42020-02-28 13:13:50 -060084 std::string logMessage = "failed to get file descriptor";
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050085 trace<level::INFO>(logMessage.c_str());
Ben Tyner73ac3682020-01-09 10:46:47 -060086 }
87 else
88 {
89 // Register file descriptor for monitoring
90 iv_gpioEventDescriptor.assign(gpioLineFd);
91
92 // Start monitoring
93 scheduleGPIOEvent();
94 }
95 }
96}
97
98} // namespace attn