From 40dfc85defd8d47b9d371becc36796d6edc43d05 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 22 Dec 2020 14:30:57 +0100 Subject: device: add UAPI helper methods Signed-off-by: Jason A. Donenfeld --- device/uapi.go | 23 +++++++++++++++++++++-- 1 file 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 -- cgit v1.2.3