aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-12-30 22:30:57 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-12-30 22:30:57 +0100
commitccd7b0b098a577464b471fad487e87639d6b6b34 (patch)
treea134f94c669376ace5447e5af354e60c5c1f48b0
parent6b52adb71fcba7af885edec2e8ac18fcddecf285 (diff)
Encode/DecodeHEADmaster
-rw-r--r--protocol.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/protocol.go b/protocol.go
index 4efa15a..9c03fa9 100644
--- a/protocol.go
+++ b/protocol.go
@@ -79,6 +79,23 @@ func Encode(v interface{}) []byte {
return z
}
+func EncodeMsg(id ID, v interface{}) ([]byte, error) {
+ buf := new(bytes.Buffer)
+ sz := binary.Size(v) + 1
+ if err := buf.WriteByte(byte(sz)); err != nil {
+ return nil, err
+ }
+ if err := buf.WriteByte(byte(id)); err != nil {
+ return nil, err
+ }
+ if err := binary.Write(buf, binary.LittleEndian, v); err != nil {
+ return nil, err
+ }
+ b := make([]byte, buf.Len())
+ copy(b, buf.Bytes())
+ return b, nil
+}
+
func Decode(b []byte, v interface{}) {
buf := bytes.NewBuffer(b)
err := binary.Read(buf, binary.LittleEndian, v)
@@ -87,6 +104,14 @@ func Decode(b []byte, v interface{}) {
}
}
+func DecodeMsg(b []byte, v interface{}) error {
+ buf := bytes.NewReader(b)
+ if err := binary.Read(buf, binary.LittleEndian, v); err != nil {
+ return err
+ }
+ return nil
+}
+
type VehicleMsgVersionResponse struct {
Size uint8
MsgID ID