aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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