forked from pevecyan/knx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpai.go
80 lines (63 loc) · 1.64 KB
/
hpai.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
// Copyright 2017 Ole Krüger.
// Licensed under the MIT license which can be found in the LICENSE file.
package knxnet
import (
"errors"
"fmt"
"github.com/greenstatic/knx-go/knx/util"
)
// Protocol specifies a host protocol to use.
type Protocol uint8
const (
// UDP4 indicates a communication using UDP over IPv4.
UDP4 Protocol = 1
// TCP4 indicates a communication using TCP over IPv4.
TCP4 Protocol = 2
)
// Address is an IPv4 address.
type Address [4]byte
// String formats the address.
func (addr Address) String() string {
return fmt.Sprintf("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3])
}
// Port is a port number.
type Port uint16
// HostInfo contains information about a host.
type HostInfo struct {
Protocol Protocol
Address Address
Port Port
}
// Equals checks whether both structures are equal.
func (info HostInfo) Equals(other HostInfo) bool {
return info.Protocol == other.Protocol &&
info.Address == other.Address &&
info.Port == other.Port
}
// Size returns the packed size.
func (HostInfo) Size() uint {
return 8
}
// Pack assembles the host info structure in the given buffer.
func (info *HostInfo) Pack(buffer []byte) {
util.PackSome(
buffer,
byte(8),
uint8(info.Protocol),
info.Address[:],
uint16(info.Port),
)
}
// Unpack parses the given data in order to initialize the structure.
func (info *HostInfo) Unpack(data []byte) (n uint, err error) {
var length uint8
if n, err = util.UnpackSome(
data, &length, (*uint8)(&info.Protocol), info.Address[:4], (*uint16)(&info.Port),
); err != nil {
return
}
if length != 8 {
return n, errors.New("Host info structure length is invalid")
}
return
}