| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <chrono> |
| 6 | #include <filesystem> |
| 7 | #include <gpiod.hpp> |
| 8 | #include <sstream> |
| 9 | #include <thread> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include "gpiosim.hpp" |
| 13 | #include "helpers.hpp" |
| 14 | |
| 15 | using ::gpiosim::make_sim; |
| 16 | using direction = ::gpiod::line::direction; |
| 17 | using event_type = ::gpiod::info_event::event_type; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | void request_reconfigure_release_line(const ::std::filesystem::path& chip_path) |
| 22 | { |
| 23 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(10)); |
| 24 | |
| 25 | auto request = ::gpiod::chip(chip_path) |
| 26 | .prepare_request() |
| 27 | .add_line_settings(7, ::gpiod::line_settings()) |
| 28 | .do_request(); |
| 29 | |
| 30 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(10)); |
| 31 | |
| 32 | request.reconfigure_lines( |
| 33 | ::gpiod::line_config() |
| 34 | .add_line_settings( |
| 35 | 7, |
| 36 | ::gpiod::line_settings() |
| 37 | .set_direction(direction::OUTPUT) |
| 38 | ) |
| 39 | ); |
| 40 | |
| 41 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(10)); |
| 42 | |
| 43 | request.release(); |
| 44 | } |
| 45 | |
| 46 | TEST_CASE("Lines can be watched", "[info-event][chip]") |
| 47 | { |
| 48 | auto sim = make_sim() |
| 49 | .set_num_lines(8) |
| 50 | .build(); |
| 51 | |
| 52 | const auto chip_path = sim.dev_path(); |
| 53 | |
| 54 | ::gpiod::chip chip(chip_path); |
| 55 | |
| 56 | SECTION("watch_line_info() returns line info") |
| 57 | { |
| 58 | auto info = chip.watch_line_info(7); |
| 59 | REQUIRE(info.offset() == 7); |
| 60 | } |
| 61 | |
| 62 | SECTION("watch_line_info() fails for offset out of range") |
| 63 | { |
| 64 | REQUIRE_THROWS_AS(chip.watch_line_info(8), ::std::invalid_argument); |
| 65 | } |
| 66 | |
| 67 | SECTION("waiting for event timeout") |
| 68 | { |
| 69 | chip.watch_line_info(3); |
| 70 | REQUIRE_FALSE(chip.wait_info_event(::std::chrono::milliseconds(100))); |
| 71 | } |
| 72 | |
| 73 | SECTION("request-reconfigure-release events") |
| 74 | { |
| 75 | auto info = chip.watch_line_info(7); |
| 76 | ::std::uint64_t ts_req, ts_rec, ts_rel; |
| 77 | |
| 78 | REQUIRE(info.direction() == direction::INPUT); |
| 79 | |
| 80 | ::std::thread thread(request_reconfigure_release_line, ::std::ref(chip_path)); |
| 81 | |
| 82 | REQUIRE(chip.wait_info_event(::std::chrono::seconds(1))); |
| 83 | auto event = chip.read_info_event(); |
| 84 | REQUIRE(event.type() == event_type::LINE_REQUESTED); |
| 85 | REQUIRE(event.get_line_info().direction() == direction::INPUT); |
| 86 | ts_req = event.timestamp_ns(); |
| 87 | |
| 88 | REQUIRE(chip.wait_info_event(::std::chrono::seconds(1))); |
| 89 | event = chip.read_info_event(); |
| 90 | REQUIRE(event.type() == event_type::LINE_CONFIG_CHANGED); |
| 91 | REQUIRE(event.get_line_info().direction() == direction::OUTPUT); |
| 92 | ts_rec = event.timestamp_ns(); |
| 93 | |
| 94 | REQUIRE(chip.wait_info_event(::std::chrono::seconds(1))); |
| 95 | event = chip.read_info_event(); |
| 96 | REQUIRE(event.type() == event_type::LINE_RELEASED); |
| 97 | ts_rel = event.timestamp_ns(); |
| 98 | |
| 99 | /* No more events. */ |
| 100 | REQUIRE_FALSE(chip.wait_info_event(::std::chrono::milliseconds(100))); |
| 101 | thread.join(); |
| 102 | |
| 103 | /* Check timestamps are really monotonic. */ |
| 104 | REQUIRE(ts_rel > ts_rec); |
| 105 | REQUIRE(ts_rec > ts_req); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | TEST_CASE("line info can be unwatched", "[info-event]") |
| 110 | { |
| 111 | auto sim = make_sim() |
| 112 | .set_num_lines(8) |
| 113 | .build(); |
| 114 | |
| 115 | ::gpiod::chip chip(sim.dev_path()); |
| 116 | |
| 117 | auto info = chip.watch_line_info(5); |
| 118 | |
| 119 | auto request = chip |
| 120 | .prepare_request() |
| 121 | .add_line_settings(5, ::gpiod::line_settings()) |
| 122 | .do_request(); |
| 123 | |
| 124 | REQUIRE(chip.wait_info_event(::std::chrono::seconds(1))); |
| 125 | auto event = chip.read_info_event(); |
| 126 | REQUIRE(event.type() == event_type::LINE_REQUESTED); |
| 127 | |
| 128 | chip.unwatch_line_info(5); |
| 129 | |
| 130 | request.release(); |
| 131 | |
| 132 | REQUIRE_FALSE(chip.wait_info_event(::std::chrono::milliseconds(100))); |
| 133 | } |
| 134 | |
| 135 | TEST_CASE("info_event can be copied and moved", "[info-event]") |
| 136 | { |
| 137 | auto sim = make_sim().build(); |
| 138 | ::gpiod::chip chip(sim.dev_path()); |
| 139 | ::std::stringstream buf, expected; |
| 140 | |
| 141 | chip.watch_line_info(0); |
| 142 | |
| 143 | auto request = chip |
| 144 | .prepare_request() |
| 145 | .add_line_settings(0, ::gpiod::line_settings()) |
| 146 | .do_request(); |
| 147 | |
| 148 | REQUIRE(chip.wait_info_event(::std::chrono::seconds(1))); |
| 149 | auto event = chip.read_info_event(); |
| 150 | |
| 151 | request.release(); |
| 152 | |
| 153 | REQUIRE(chip.wait_info_event(::std::chrono::seconds(1))); |
| 154 | auto copy = chip.read_info_event(); |
| 155 | |
| 156 | SECTION("copy constructor works") |
| 157 | { |
| 158 | auto copy(event); |
| 159 | |
| 160 | REQUIRE(copy.type() == event_type::LINE_REQUESTED); |
| 161 | REQUIRE(copy.get_line_info().offset() == 0); |
| 162 | |
| 163 | REQUIRE(event.type() == event_type::LINE_REQUESTED); |
| 164 | REQUIRE(event.get_line_info().offset() == 0); |
| 165 | } |
| 166 | |
| 167 | SECTION("assignment operator works") |
| 168 | { |
| 169 | copy = event; |
| 170 | |
| 171 | REQUIRE(copy.type() == event_type::LINE_REQUESTED); |
| 172 | REQUIRE(copy.get_line_info().offset() == 0); |
| 173 | |
| 174 | REQUIRE(event.type() == event_type::LINE_REQUESTED); |
| 175 | REQUIRE(event.get_line_info().offset() == 0); |
| 176 | } |
| 177 | |
| 178 | SECTION("move constructor works") |
| 179 | { |
| 180 | auto copy(::std::move(event)); |
| 181 | |
| 182 | REQUIRE(copy.type() == event_type::LINE_REQUESTED); |
| 183 | REQUIRE(copy.get_line_info().offset() == 0); |
| 184 | } |
| 185 | |
| 186 | SECTION("move assignment operator works") |
| 187 | { |
| 188 | copy = ::std::move(event); |
| 189 | |
| 190 | REQUIRE(copy.type() == event_type::LINE_REQUESTED); |
| 191 | REQUIRE(copy.get_line_info().offset() == 0); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | TEST_CASE("info_event stream insertion operator works", "[info-event][line-info]") |
| 196 | { |
| 197 | /* |
| 198 | * This tests the stream insertion operator for both the info_event |
| 199 | * and line_info classes. |
| 200 | */ |
| 201 | |
| 202 | auto sim = make_sim().build(); |
| 203 | ::gpiod::chip chip(sim.dev_path()); |
| 204 | ::std::stringstream buf, expected; |
| 205 | |
| 206 | chip.watch_line_info(0); |
| 207 | |
| 208 | auto request = chip |
| 209 | .prepare_request() |
| 210 | .add_line_settings(0, ::gpiod::line_settings()) |
| 211 | .do_request(); |
| 212 | |
| 213 | auto event = chip.read_info_event(); |
| 214 | |
| 215 | buf << event; |
| 216 | |
| 217 | expected << "gpiod::info_event\\(event_type='LINE_REQUESTED', timestamp=[1-9][0-9]+, " << |
| 218 | "line_info=gpiod::line_info\\(offset=0, name=unnamed, used=true, consumer='\\?', " << |
| 219 | "direction=INPUT, active_low=false, bias=UNKNOWN, drive=PUSH_PULL, " << |
| 220 | "edge_detection=NONE, event_clock=MONOTONIC, debounced=false\\)\\)"; |
| 221 | |
| 222 | REQUIRE_THAT(buf.str(), regex_matcher(expected.str())); |
| 223 | } |
| 224 | |
| 225 | } /* namespace */ |