-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example connecting to a serial port and writing a message. This is tested by creating a virtual serial port with the output written to file. Bazel test interaction with the filesystem prevents creation of a RW device. As a result, `//test:virtual_serial_port_test` is defined as a `sh_binary` that can be run with: bazel run //test:virtual_serial_port_test Change-Id: I1be9cb2e8b13fa972084d66fa2c6ddffb23ae3cc
- Loading branch information
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <asio.hpp> | ||
#include <cstdlib> | ||
#include <fmt/core.h> | ||
#include <stdexcept> | ||
#include <string_view> | ||
|
||
auto main(int argc, const char** argv) -> int | ||
{ | ||
if (argc < 2) { | ||
throw std::runtime_error{"device path not specified"}; | ||
} | ||
|
||
auto ctx = asio::io_context{}; | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) | ||
auto serial = asio::serial_port{ctx, argv[1]}; | ||
{ | ||
using opt = asio::serial_port_base; | ||
|
||
// NOLINTBEGIN(readability-magic-numbers) | ||
serial.set_option(opt::baud_rate{115200}); | ||
serial.set_option(opt::character_size{8}); | ||
serial.set_option(opt::parity{opt::parity::none}); | ||
serial.set_option(opt::stop_bits{opt::stop_bits::one}); | ||
// NOLINTEND(readability-magic-numbers) | ||
} | ||
|
||
constexpr auto sv = std::string_view{"hello, world!\n"}; | ||
|
||
for (auto buf = asio::buffer(sv.data(), sv.size()); buf.size() != 0;) { | ||
buf += serial.write_some(asio::buffer(sv.data(), sv.size())); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
# https://bazel.build/reference/test-encyclopedia#test-interaction-filesystem | ||
TEST_TMPDIR="${TEST_TEMPDIR:-.}" | ||
|
||
dev="$TEST_TMPDIR/virt0" | ||
log="$TEST_TMPDIR/out" | ||
bin=$1 | ||
|
||
rm -f "$log" | ||
|
||
socat PTY,link="$dev",raw,echo=0 OPEN:"$log",creat=1,excl=1 & | ||
socat_pid=$! | ||
|
||
while [ ! -e "$dev" ]; do | ||
sleep 0.1 | ||
done | ||
|
||
"$bin" "$dev" | ||
|
||
kill "$socat_pid" | ||
|
||
grep -q "hello, world!" "$log" |