Ben Tyner | b797b3e | 2020-06-29 10:12:05 -0500 | [diff] [blame] | 1 | #include <attn/attn_handler.hpp> |
| 2 | #include <attn/attn_logging.hpp> |
| 3 | #include <attn/attn_monitor.hpp> |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 4 | |
| 5 | namespace attn |
| 6 | { |
| 7 | |
| 8 | /** @brief Register a callback for gpio event */ |
| 9 | void AttnMonitor::scheduleGPIOEvent() |
| 10 | { |
Ben Tyner | 18db69a | 2020-08-20 08:23:07 -0500 | [diff] [blame] | 11 | // std::string logMessage = "... waiting for events ..."; |
| 12 | // trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 13 | |
| 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 Tyner | 9ae5ca4 | 2020-02-28 13:13:50 -0600 | [diff] [blame] | 21 | std::string logMessage = |
| 22 | "GPIO Async wait error: " + std::string(ec.message()); |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 23 | trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 24 | } |
| 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 */ |
| 34 | void 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 Tyner | 9ae5ca4 | 2020-02-28 13:13:50 -0600 | [diff] [blame] | 42 | logMessage = "GPIO line read failed"; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 43 | trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 44 | } |
| 45 | else |
| 46 | { |
| 47 | switch (gpiod_line_get_value(iv_gpioLine)) |
| 48 | { |
| 49 | // active attention when gpio == 0 |
| 50 | case 0: |
Ben Tyner | 3fb52e5 | 2020-03-31 10:10:07 -0500 | [diff] [blame] | 51 | attnHandler(iv_config); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 52 | break; |
| 53 | |
| 54 | // gpio == 1, GPIO handler should not be executing |
| 55 | case 1: |
Ben Tyner | 9ae5ca4 | 2020-02-28 13:13:50 -0600 | [diff] [blame] | 56 | logMessage = "GPIO handler out of sync"; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 57 | trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 58 | break; |
| 59 | |
| 60 | // unexpected value |
| 61 | default: |
Ben Tyner | 9ae5ca4 | 2020-02-28 13:13:50 -0600 | [diff] [blame] | 62 | logMessage = "GPIO line unexpected value"; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 63 | trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | scheduleGPIOEvent(); // continue monitoring gpio |
| 67 | } |
| 68 | |
| 69 | /** @brief Request a GPIO line for monitoring attention events */ |
| 70 | void AttnMonitor::requestGPIOEvent() |
| 71 | { |
| 72 | if (0 != gpiod_line_request(iv_gpioLine, &iv_gpioConfig, 0)) |
| 73 | { |
Ben Tyner | 9ae5ca4 | 2020-02-28 13:13:50 -0600 | [diff] [blame] | 74 | std::string logMessage = "failed request for GPIO"; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 75 | trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 76 | } |
| 77 | else |
| 78 | { |
| 79 | int gpioLineFd; |
| 80 | |
| 81 | gpioLineFd = gpiod_line_event_get_fd(iv_gpioLine); |
| 82 | if (gpioLineFd < 0) |
| 83 | { |
Ben Tyner | 9ae5ca4 | 2020-02-28 13:13:50 -0600 | [diff] [blame] | 84 | std::string logMessage = "failed to get file descriptor"; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 85 | trace<level::INFO>(logMessage.c_str()); |
Ben Tyner | 73ac368 | 2020-01-09 10:46:47 -0600 | [diff] [blame] | 86 | } |
| 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 |