-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxrequest.go
More file actions
70 lines (58 loc) · 1.77 KB
/
txrequest.go
File metadata and controls
70 lines (58 loc) · 1.77 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
package xbeeapi
import "bytes"
const MinTxRequestSize = 23
type TxRequest struct {
FrameID byte
Address64 string
Address16 string
BroadcastRadius byte
Options byte
Payload []byte
}
func ParseTxRequest(rfd *RawFrameData) (*TxRequest, error) {
if !rfd.IsValid() || rfd.FrameType() != FrameTypeTxRequest {
return nil, &FrameParseError{msg: "Expecting frame type TxRequest"}
}
if rfd.Len() < MinTxRequestSize {
return nil, &FrameParseError{msg: "Frame data too small for TxRequest"}
}
buf := bytes.NewBuffer(rfd.Data())
tx := &TxRequest{
FrameID: buf.Next(1)[0],
Address64: bytesToHex(buf.Next(16)),
Address16: bytesToHex(buf.Next(4)),
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 AT command"}
}
return tx, nil
}
func (tx *TxRequest) RawFrameData() *RawFrameData {
b := []byte{FrameTypeTxRequest, tx.FrameID}
address64, _ := hexToBytes(tx.Address64)
address16, _ := hexToBytes(tx.Address16)
b = concat(b, address64, address16)
b = append(b, tx.BroadcastRadius, tx.Options)
b = concat(b, tx.Payload)
return NewRawFrameData(b...)
}
func (tx *TxRequest) IsValid() bool {
address64, _ := hexToBytes(tx.Address64)
address16, _ := hexToBytes(tx.Address16)
if len(address64) == 16 && len(address16) == 4 {
return true
}
return false
}
func (tx *TxRequest) FrameType() byte {
return FrameTypeTxRequest
}
func (tx *TxRequest) SetOptionsFlags(txOptionFlags ...TxOptionFlag) {
tx.Options = setTxOptionsFlags(tx.Options, txOptionFlags...)
}
func (tx *TxRequest) IsOptionsFlagSet(txOptionFlag TxOptionFlag) bool {
return isTxOptionsFlagSet(tx.Options, txOptionFlag)
}