summaryrefslogtreecommitdiff
path: root/software/vendor/go.bug.st/serial/unixutils/pipe.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-05-30 16:00:32 +0200
committerxengineering <me@xengineering.eu>2024-05-30 16:00:32 +0200
commitb8ef4d11fe0d00ce0884ccf982675845b20c3ce9 (patch)
tree3beb57ff849ed74569ce325225bc819791c25a6a /software/vendor/go.bug.st/serial/unixutils/pipe.go
parenteab833271eeaa8d54991c11eccec9445f662a191 (diff)
downloadiot-core-b8ef4d11fe0d00ce0884ccf982675845b20c3ce9.tar
iot-core-b8ef4d11fe0d00ce0884ccf982675845b20c3ce9.tar.zst
iot-core-b8ef4d11fe0d00ce0884ccf982675845b20c3ce9.zip
software: Implement serial port detection
Diffstat (limited to 'software/vendor/go.bug.st/serial/unixutils/pipe.go')
-rw-r--r--software/vendor/go.bug.st/serial/unixutils/pipe.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/software/vendor/go.bug.st/serial/unixutils/pipe.go b/software/vendor/go.bug.st/serial/unixutils/pipe.go
new file mode 100644
index 0000000..748de34
--- /dev/null
+++ b/software/vendor/go.bug.st/serial/unixutils/pipe.go
@@ -0,0 +1,82 @@
+//
+// Copyright 2014-2023 Cristian Maglie. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+
+//go:build linux || darwin || freebsd || openbsd
+
+package unixutils
+
+import (
+ "fmt"
+ "syscall"
+)
+
+// Pipe represents a unix-pipe
+type Pipe struct {
+ opened bool
+ rd int
+ wr int
+}
+
+// Open creates a new pipe
+func (p *Pipe) Open() error {
+ fds := []int{0, 0}
+ if err := syscall.Pipe(fds); err != nil {
+ return err
+ }
+ p.rd = fds[0]
+ p.wr = fds[1]
+ p.opened = true
+ return nil
+}
+
+// ReadFD returns the file handle for the read side of the pipe.
+func (p *Pipe) ReadFD() int {
+ if !p.opened {
+ return -1
+ }
+ return p.rd
+}
+
+// WriteFD returns the flie handle for the write side of the pipe.
+func (p *Pipe) WriteFD() int {
+ if !p.opened {
+ return -1
+ }
+ return p.wr
+}
+
+// Write to the pipe the content of data. Returns the numbre of bytes written.
+func (p *Pipe) Write(data []byte) (int, error) {
+ if !p.opened {
+ return 0, fmt.Errorf("Pipe not opened")
+ }
+ return syscall.Write(p.wr, data)
+}
+
+// Read from the pipe into the data array. Returns the number of bytes read.
+func (p *Pipe) Read(data []byte) (int, error) {
+ if !p.opened {
+ return 0, fmt.Errorf("Pipe not opened")
+ }
+ return syscall.Read(p.rd, data)
+}
+
+// Close the pipe
+func (p *Pipe) Close() error {
+ if !p.opened {
+ return fmt.Errorf("Pipe not opened")
+ }
+ err1 := syscall.Close(p.rd)
+ err2 := syscall.Close(p.wr)
+ p.opened = false
+ if err1 != nil {
+ return err1
+ }
+ if err2 != nil {
+ return err2
+ }
+ return nil
+}