diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-12-22 14:30:57 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-01-07 14:49:44 +0100 |
commit | 40dfc85defd8d47b9d371becc36796d6edc43d05 (patch) | |
tree | 8b8342995805e574360c737bc204bbeb758f82c8 | |
parent | 890cc06ed577420b04676d4b2d7a7daae5fb0030 (diff) |
device: add UAPI helper methods
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | device/uapi.go | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/device/uapi.go b/device/uapi.go index 3f26607..2843a4c 100644 --- a/device/uapi.go +++ b/device/uapi.go @@ -7,6 +7,7 @@ package device import ( "bufio" + "bytes" "errors" "fmt" "io" @@ -32,7 +33,7 @@ func (s IPCError) ErrorCode() int64 { return s.int64 } -func (device *Device) IpcGetOperation(socket *bufio.Writer) error { +func (device *Device) IpcGetOperation(w io.StringWriter) error { lines := make([]string, 0, 100) send := func(line string) { lines = append(lines, line) @@ -98,7 +99,7 @@ func (device *Device) IpcGetOperation(socket *bufio.Writer) error { // send lines (does not require resource locks) for _, line := range lines { - _, err := socket.WriteString(line + "\n") + _, err := w.WriteString(line + "\n") if err != nil { return &IPCError{ipc.IpcErrorIO} } @@ -396,6 +397,24 @@ func (device *Device) IpcSetOperation(r io.Reader) error { return nil } + + +func (device *Device) IpcGet() (string, error) { + buf := new(bytes.Buffer) + writer := bufio.NewWriter(buf) + if err := device.IpcGetOperation(writer); err != nil { + return "", err + } + if err := writer.Flush(); err != nil { + return "", err + } + return buf.String(), nil +} + +func (device *Device) IpcSet(uapiConf string) error { + return device.IpcSetOperation(bufio.NewReader(strings.NewReader(uapiConf))) +} + func (device *Device) IpcHandle(socket net.Conn) { // create buffered read/writer |