-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxexplicitaddressing.go
More file actions
86 lines (72 loc) · 2.46 KB
/
txexplicitaddressing.go
File metadata and controls
86 lines (72 loc) · 2.46 KB
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
package xbeeapi
import (
"bytes"
"encoding/binary"
)
const MinTxExplicitAddressingSize = 30
type TxExplicitAddressing struct {
FrameID byte
Address64 string
Address16 string
SrcEndPoint byte
DstEndPoint byte
ClusterID uint16
ProfileID uint16
BroadcastRadius byte
Options byte
Payload []byte
}
func ParseTxExplicitAddressing(rfd *RawFrameData) (*TxExplicitAddressing, error) {
if !rfd.IsValid() || rfd.FrameType() != FrameTypeExplicitAddressingCommandFrame {
return nil, &FrameParseError{msg: "Expecting frame type TxExplicitAddressing"}
}
if rfd.Len() < MinTxExplicitAddressingSize {
return nil, &FrameParseError{msg: "Frame data too small for TxExplicitAddressing"}
}
buf := bytes.NewBuffer(rfd.Data())
tx := &TxExplicitAddressing{
FrameID: buf.Next(1)[0],
Address64: bytesToHex(buf.Next(16)),
Address16: bytesToHex(buf.Next(4)),
SrcEndPoint: buf.Next(1)[0],
DstEndPoint: buf.Next(1)[0],
ClusterID: binary.BigEndian.Uint16(buf.Next(2)),
ProfileID: binary.BigEndian.Uint16(buf.Next(2)),
BroadcastRadius: buf.Next(1)[0],
Options: buf.Next(1)[0],
Payload: copySlice(buf.Bytes()),
}
if !tx.IsValid() {
return nil, &FrameParseError{msg: "Invalid frame data for TxExplicitAddressing"}
}
return tx, nil
}
func (tx *TxExplicitAddressing) RawFrameData() *RawFrameData {
b := []byte{FrameTypeExplicitAddressingCommandFrame, tx.FrameID}
address64, _ := hexToBytes(tx.Address64)
address16, _ := hexToBytes(tx.Address16)
b = concat(b, address64, address16)
b = append(b, tx.SrcEndPoint, tx.DstEndPoint, 0x00, 0x00, 0x00, 0x00)
binary.BigEndian.PutUint16(b[(len(b)-4):], tx.ClusterID)
binary.BigEndian.PutUint16(b[(len(b)-2):], tx.ProfileID)
b = append(b, tx.BroadcastRadius, tx.Options)
b = concat(b, tx.Payload)
return NewRawFrameData(b...)
}
func (tx *TxExplicitAddressing) IsValid() bool {
address64, _ := hexToBytes(tx.Address64)
address16, _ := hexToBytes(tx.Address16)
if len(address64) == 16 && len(address16) == 4 {
return true
}
return false
}
func (tx *TxExplicitAddressing) FrameType() byte {
return FrameTypeExplicitAddressingCommandFrame
}
func (tx *TxExplicitAddressing) SetOptionsFlags(txOptionFlags ...TxOptionFlag) {
tx.Options = setTxOptionsFlags(tx.Options, txOptionFlags...)
}
func (tx *TxExplicitAddressing) IsOptionsFlagSet(txOptionFlag TxOptionFlag) bool {
return isTxOptionsFlagSet(tx.Options, txOptionFlag)
}