forked from pevecyan/knx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.go
108 lines (84 loc) · 2.38 KB
/
tunnel.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
// Copyright 2017 Ole Krüger.
// Licensed under the MIT license which can be found in the LICENSE file.
package knxnet
import (
"errors"
"github.com/greenstatic/knx-go/knx/cemi"
"github.com/greenstatic/knx-go/knx/util"
)
// A TunnelReq asks a gateway to transmit data.
type TunnelReq struct {
// Communication channel
Channel uint8
// Sequential number, used to track acknowledgements
SeqNumber uint8
// Data to be tunneled
Payload cemi.Message
}
// Service returns the service identifiers for tunnel requests.
func (TunnelReq) Service() ServiceID {
return TunnelReqService
}
// Size returns the packed size.
func (req *TunnelReq) Size() uint {
return 4 + cemi.Size(req.Payload)
}
// Pack assembles the service payload in the given buffer.
func (req *TunnelReq) Pack(buffer []byte) {
buffer[0] = 4
buffer[1] = req.Channel
buffer[2] = req.SeqNumber
buffer[3] = 0
cemi.Pack(buffer[4:], req.Payload)
}
// Unpack parses the given service payload in order to initialize the structure.
func (req *TunnelReq) Unpack(data []byte) (n uint, err error) {
var length, reserved uint8
if n, err = util.UnpackSome(
data, &length, &req.Channel, &req.SeqNumber, &reserved,
); err != nil {
return
}
if length != 4 {
return n, errors.New("Length header is not 4")
}
m, err := cemi.Unpack(data[n:], &req.Payload)
n += m
return
}
// A TunnelRes is a response to a TunnelRequest. It acts as an acknowledgement.
type TunnelRes struct {
// Communication channel
Channel uint8
// Identifies the request that is being acknowledged
SeqNumber uint8
// Status code, determines whether the tunneling succeeded or not
Status ErrCode
}
// Service returns the service identifier for tunnel responses.
func (TunnelRes) Service() ServiceID {
return TunnelResService
}
// Size returns the packed size.
func (TunnelRes) Size() uint {
return 4
}
// Pack assembles the service payload in the given buffer.
func (res *TunnelRes) Pack(buffer []byte) {
buffer[0] = 4
buffer[1] = res.Channel
buffer[2] = res.SeqNumber
buffer[3] = uint8(res.Status)
}
// Unpack parses the given service payload in order to initialize the structure.
func (res *TunnelRes) Unpack(data []byte) (n uint, err error) {
var length uint8
n, err = util.UnpackSome(data, &length, &res.Channel, &res.SeqNumber, (*uint8)(&res.Status))
if err != nil {
return
}
if length != 4 {
return n, errors.New("Length header is not 4")
}
return
}