-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
65 lines (54 loc) · 1.78 KB
/
utils.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
package roc
import (
"bytes"
"fmt"
)
// splitParameters: split data between the supplied TLPs
// Mainly for use with SendSpecifiedParameters
// Note: this implementation has the following known flaws
// Case: Next TLP's bytes occur naturally within
// the data for the current TLP.
// Result: Short byte slice followed by a long byte slice
//
// The surest way to do this is build a lookup table for data lengths.
// The two biggest problems with that are that it would be a pain to
// set up and there are differences between ROC devices, so you would
// need to include the device model in all look-ups.
func splitParameters(parameters []TLP, data []byte)(splitData map[TLP][]byte, err error){
if len(data) < 4{
err = fmt.Errorf("Too little data")
return
}
if len(parameters)<1{
err = fmt.Errorf("At least one parameter is required")
return
}
// Assert the byte slice starts with the first TLP
if !bytes.HasPrefix(data,[]byte{parameters[0].PointType, parameters[0].LogicNumber, parameters[0].Parameter}){
err = fmt.Errorf("Missing TLP")
return
}
// Make a copy of the data, dropping the TLP bytes for the first parameter
dataCopy := append([]byte{}, data[3:]...)
splitData = make(map[TLP][]byte)
for i, current := range parameters{
// We're done! all TLPs have been handled
if i == (len(parameters)-1){
splitData[current]=dataCopy
return
}
// Split on next TLP to get current bytes, and remainder
next := []byte{parameters[i+1].PointType, parameters[i+1].LogicNumber, parameters[i+1].Parameter}
chunks := bytes.SplitN(dataCopy, next, 2)
if len(chunks) != 2{
err = fmt.Errorf("Too little data")
return
}else if len(chunks[1]) < 1{
err = fmt.Errorf("Too little data")
return
}
splitData[current] = chunks[0]
dataCopy = chunks[1]
}
return
}