-
Notifications
You must be signed in to change notification settings - Fork 3
/
advertise.go
51 lines (39 loc) · 1.02 KB
/
advertise.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
package packets1
import (
"encoding/binary"
"fmt"
pkts "github.com/energomonitor/bisquitt/packets"
)
const advertiseVarPartLength uint16 = 3
type Advertise struct {
pkts.Header
// Fields
GatewayID uint8
Duration uint16
}
func NewAdvertise(gatewayID uint8, duration uint16) *Advertise {
return &Advertise{
Header: *pkts.NewHeader(pkts.ADVERTISE, advertiseVarPartLength),
GatewayID: gatewayID,
Duration: duration,
}
}
func (p *Advertise) Pack() ([]byte, error) {
buf := p.Header.PackToBuffer()
_ = buf.WriteByte(p.GatewayID)
_, _ = buf.Write(pkts.EncodeUint16(p.Duration))
return buf.Bytes(), nil
}
func (p *Advertise) Unpack(buf []byte) error {
if len(buf) != int(advertiseVarPartLength) {
return fmt.Errorf("bad ADVERTISE packet length: expected %d, got %d",
advertiseVarPartLength, len(buf))
}
p.GatewayID = buf[0]
p.Duration = binary.BigEndian.Uint16(buf[1:3])
return nil
}
func (p Advertise) String() string {
return fmt.Sprintf("ADVERTISE(GatewayID=%d,Duration=%d)",
p.GatewayID, p.Duration)
}