Move ScopedPipe/Application classes out of starterd_lib.*
Change-Id: I1b66ef343b6d4d1129fdc8d40781d1e5b711d2b2
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/util/BUILD b/aos/util/BUILD
index d75d421..a57ec79 100644
--- a/aos/util/BUILD
+++ b/aos/util/BUILD
@@ -223,6 +223,16 @@
],
)
+cc_library(
+ name = "scoped_pipe",
+ srcs = ["scoped_pipe.cc"],
+ hdrs = ["scoped_pipe.h"],
+ target_compatible_with = ["@platforms//os:linux"],
+ deps = [
+ "@com_github_google_glog//:glog",
+ ],
+)
+
cc_test(
name = "file_test",
size = "small",
diff --git a/aos/util/scoped_pipe.cc b/aos/util/scoped_pipe.cc
new file mode 100644
index 0000000..8cf2957
--- /dev/null
+++ b/aos/util/scoped_pipe.cc
@@ -0,0 +1,54 @@
+#include "aos/util/scoped_pipe.h"
+
+#include <fcntl.h>
+#include "glog/logging.h"
+
+namespace aos::util {
+
+ScopedPipe::ScopedPipe(int fd) : fd_(fd) {}
+
+ScopedPipe::~ScopedPipe() {
+ if (fd_ != -1) {
+ PCHECK(close(fd_) != -1);
+ }
+}
+
+ScopedPipe::ScopedPipe(ScopedPipe &&scoped_pipe) : fd_(scoped_pipe.fd_) {
+ scoped_pipe.fd_ = -1;
+}
+
+ScopedPipe &ScopedPipe::operator=(ScopedPipe &&scoped_pipe) {
+ if (fd_ != -1) {
+ PCHECK(close(fd_) != -1);
+ }
+ fd_ = scoped_pipe.fd_;
+ scoped_pipe.fd_ = -1;
+ return *this;
+}
+
+std::tuple<ScopedPipe::ScopedReadPipe, ScopedPipe::ScopedWritePipe>
+ScopedPipe::MakePipe() {
+ int fds[2];
+ PCHECK(pipe(fds) != -1);
+ PCHECK(fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK) != -1);
+ PCHECK(fcntl(fds[1], F_SETFL, fcntl(fds[1], F_GETFL) | O_NONBLOCK) != -1);
+ return {ScopedReadPipe(fds[0]), ScopedWritePipe(fds[1])};
+}
+
+std::optional<uint32_t> ScopedPipe::ScopedReadPipe::Read() {
+ uint32_t buf;
+ ssize_t result = read(fd(), &buf, sizeof(buf));
+ if (result == sizeof(buf)) {
+ return buf;
+ } else {
+ return std::nullopt;
+ }
+}
+
+void ScopedPipe::ScopedWritePipe::Write(uint32_t data) {
+ ssize_t result = write(fd(), &data, sizeof(data));
+ PCHECK(result != -1);
+ CHECK(result == sizeof(data));
+}
+
+} // namespace aos::util
diff --git a/aos/util/scoped_pipe.h b/aos/util/scoped_pipe.h
new file mode 100644
index 0000000..5f86510
--- /dev/null
+++ b/aos/util/scoped_pipe.h
@@ -0,0 +1,56 @@
+#ifndef AOS_UTIL_SCOPED_PIPE_H_
+#define AOS_UTIL_SCOPED_PIPE_H_
+
+#include <stdint.h>
+
+#include <optional>
+#include <tuple>
+
+namespace aos::util {
+
+// RAII Pipe for sending individual ints between reader and writer.
+class ScopedPipe {
+ public:
+ class ScopedReadPipe;
+ class ScopedWritePipe;
+
+ static std::tuple<ScopedReadPipe, ScopedWritePipe> MakePipe();
+
+ virtual ~ScopedPipe();
+
+ int fd() const { return fd_; }
+
+ private:
+ ScopedPipe(int fd = -1);
+
+ int fd_;
+
+ ScopedPipe(const ScopedPipe &) = delete;
+ ScopedPipe &operator=(const ScopedPipe &) = delete;
+ ScopedPipe(ScopedPipe &&);
+ ScopedPipe &operator=(ScopedPipe &&);
+};
+
+class ScopedPipe::ScopedReadPipe : public ScopedPipe {
+ public:
+ std::optional<uint32_t> Read();
+
+ private:
+ using ScopedPipe::ScopedPipe;
+
+ friend class ScopedPipe;
+};
+
+class ScopedPipe::ScopedWritePipe : public ScopedPipe {
+ public:
+ void Write(uint32_t data);
+
+ private:
+ using ScopedPipe::ScopedPipe;
+
+ friend class ScopedPipe;
+};
+
+} // namespace aos::util
+
+#endif // AOS_UTIL_SCOPED_PIPE_H_