-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpi.go
72 lines (55 loc) · 1.68 KB
/
unpi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Package unpi provides marshalling and unmarshalling support for Texas Instruments
// Unified Network Processor Interface.
//
// More information available at:
// http://processors.wiki.ti.com/index.php/Unified_Network_Processor_Interface
package unpi // import "github.com/shimmeringbee/unpi"
import (
"fmt"
"io"
)
// Read reads from the ReadWriter provided to the UNPI struct until it receives
// a whole UNPI frame. It returns a pointer to the frame, or an error if one
// was encountered. Error may either be an issue with the structure of the
// frame or an error raised by the ReadWriter.
func Read(r io.Reader) (Frame, error) {
data := []byte{StartOfFrame, 0x00, 0x00, 0x00, 0x00}
if err := seekStartOfFrame(r); err != nil {
return Frame{}, err
}
_, err := io.ReadFull(r, data[1:])
if err != nil {
return Frame{}, err
}
payloadLength := data[1]
data = append(data, make([]byte, payloadLength)...)
c, err := io.ReadFull(r, data[5:])
if err != nil && c != int(payloadLength) {
return Frame{}, err
}
return UnmarshallFrame(data)
}
func seekStartOfFrame(r io.Reader) error {
b := []byte{0x00}
for b[0] != StartOfFrame {
_, err := r.Read(b)
if err != nil {
return err
}
}
return nil
}
// Write marshalls and writes a UNPI frame to the ReadWriter provided to the UNPI struct.
// It will return an error if one was encountered while writing to the ReadWriter
func Write(w io.Writer, frame Frame) error {
data := frame.Marshall()
dataSize := len(data)
dataWritten, err := w.Write(data)
if err != nil {
return err
}
if dataWritten != len(data) {
return fmt.Errorf("writer did not accept whole frame, sent %d, written %d", dataSize, dataWritten)
}
return nil
}