-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet.go
258 lines (201 loc) · 5.08 KB
/
packet.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package tinkerforge
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
// ErrorCode represents the error value returned by the brick(let)s
type ErrorCode uint8
const (
// ECOkay says no errors occurred
ECOkay ErrorCode = 0
// ECInvalidParam says you sent an invalid parameter in the packet
ECInvalidParam = 1
// ECFuncNotSupported says the function number you sent is not available
ECFuncNotSupported = 2
)
var (
// ErrInvalidParam represents ECInvalidParam in Go
ErrInvalidParam = errors.New("Invalid Parameter")
// ErrFuncNotSupported represents ECFuncNotSupported in Go
ErrFuncNotSupported = errors.New("Function is not supported")
)
// Packet holds all information about a sent or received packet
type Packet struct {
uid uint32
funcID uint8
seqNum uint8
respExp bool
errorCode ErrorCode
callback bool
payload []byte
}
// NewPacket creates a new packet to be sent to the TinkerForge daemon
func NewPacket(uid uint32, funcID uint8, respExp bool, params ...interface{}) (*Packet, error) {
payload, err := parseParams(params)
if err != nil {
return nil, err
}
return &Packet{
uid: uid,
funcID: funcID,
seqNum: 0,
respExp: respExp,
errorCode: 0,
callback: false,
payload: payload,
}, nil
}
func readPacket(data []byte) (*Packet, error) {
re := bytes.NewReader(data)
header := struct {
UID uint32
Len uint8
Func uint8
Seq uint8
Flags uint8
}{}
if err := binary.Read(re, binary.LittleEndian, &header); err != nil && err.Error() != "EOF" {
return nil, err
}
respExp := header.Seq&0x08 != 0
seqNum := header.Seq >> 4
callback := seqNum == 0
errCode := header.Flags >> 6
payload := make([]byte, header.Len-8)
re.Read(payload)
if _, err := re.Read(payload); err != nil && err.Error() != "EOF" {
return nil, err
}
p := &Packet{
uid: header.UID,
funcID: header.Func,
seqNum: seqNum,
respExp: respExp,
errorCode: ErrorCode(errCode),
callback: callback,
payload: payload,
}
return p, nil
}
// Decode decodes the payload of a packet into a number of variables
func (p *Packet) Decode(vars ...interface{}) error {
re := bytes.NewReader(p.payload)
for _, v := range vars {
if err := binary.Read(re, binary.LittleEndian, v); err != nil {
if err.Error() == "EOF" {
return nil
}
return err
}
}
return nil
}
// UID returns the UID of the packet source / destination
func (p *Packet) UID() uint32 {
return p.uid
}
// Length returns the overall length (header + payload) of the packet
func (p *Packet) Length() uint8 {
if p.payload == nil {
return 8
}
return uint8(8 + len(p.payload))
}
// FunctionID returns the function ID of the packet
func (p *Packet) FunctionID() uint8 {
return p.funcID
}
// SequenceNum returns the 4-bit sequence number of the packet.
func (p *Packet) SequenceNum() uint8 {
return p.seqNum
}
// ResponseExpected returns wether the caller expects an answer
func (p *Packet) ResponseExpected() bool {
return p.respExp
}
// ErrorID returns the ID of the error (or ECOkay)
func (p *Packet) ErrorID() ErrorCode {
return p.errorCode
}
// Error returns the corresponding error for the error ID
func (p *Packet) Error() error {
switch p.errorCode {
case ECInvalidParam:
return ErrInvalidParam
case ECFuncNotSupported:
return ErrFuncNotSupported
}
return nil
}
// Callback indicates if this packet is a callback
func (p *Packet) Callback() bool {
return p.callback
}
// Payload returns the payload of the packet
func (p *Packet) Payload() []byte {
return p.payload
}
// Serialize converts the packet into a byte slice for sending
func (p *Packet) Serialize(wr io.Writer, seqNum byte) error {
// Send header and payload
if err := p.writeHeader(wr, seqNum); err != nil {
return err
}
return p.writePayload(wr)
}
func parseParams(params []interface{}) ([]byte, error) {
wr := bytes.NewBuffer(make([]byte, 0))
for _, p := range params {
if err := binary.Write(wr, binary.LittleEndian, p); err != nil {
return nil, err
}
}
return wr.Bytes(), nil
}
func (p *Packet) writeHeader(wr io.Writer, seqNum byte) error {
// Header structure
header := &struct {
UID uint32
Len uint8
FuncID uint8
Seq uint8
Flags uint8
}{}
// Fill header
header.UID = p.uid
header.Len = p.Length()
header.FuncID = p.funcID
seqNum = seqNum << 4
if p.respExp {
seqNum |= 0x08
}
header.Seq = uint8(seqNum)
header.Flags = uint8(p.errorCode << 6)
// Send header
return binary.Write(wr, binary.LittleEndian, header)
}
func (p *Packet) writePayload(wr io.Writer) error {
_, err := wr.Write(p.payload)
return err
}
// Scans a byte stream for packets and returns them as byte arrays
func scanPacket(data []byte, atEOF bool) (advance int, token []byte, err error) {
// We are unable to read the length of the packet.
if len(data) < 5 {
if atEOF {
return 0, nil, errors.New("EOF")
}
return 0, nil, nil
}
// Check the length of the data and the packet
if len(data) >= int(data[4]) {
return int(data[4]), data[:data[4]], nil
}
// The packet is incomplete but we are at EOF
if atEOF {
return 0, nil, errors.New("EOF")
}
return 0, nil, nil
}