txstatusframe fixes and tests

This commit is contained in:
saji 2023-05-04 14:05:51 -05:00
parent b7840aa9cb
commit 6a5cda8c66
2 changed files with 145 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package xbee
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
) )
@ -52,14 +53,19 @@ const (
TxStatusNoACK TxStatus = 0x01 TxStatusNoACK TxStatus = 0x01
TxStatusCCAFail TxStatus = 0x02 TxStatusCCAFail TxStatus = 0x02
TxStatusIndirect TxStatus = 0x03 TxStatusIndirect TxStatus = 0x03
TxStatusTxFail TxStatus = 0x04
TxStatusACKFail TxStatus = 0x21 TxStatusACKFail TxStatus = 0x21
// TODO: finish this TxStatusNoRoute TxStatus = 0x25
TxStatusResourceError TxStatus = 0x31
TxStatusResourceUnavail TxStatus = 0x32
TxStatusPayloadTooLarge TxStatus = 0x74
TxStatusIndirectUnrequested TxStatus = 0x75
) )
type TxStatusFrame struct { type TxStatusFrame struct {
Id uint8 // the Frame identifier that this status frame represents. Id uint8 // the Frame identifier that this status frame represents.
NRetry uint8
Status TxStatus // the status itself - TxStatus is a stringable. Status TxStatus // the status itself - TxStatus is a stringable.
Routed bool
} }
func ParseTxStatusFrame(data []byte) (*TxStatusFrame, error) { func ParseTxStatusFrame(data []byte) (*TxStatusFrame, error) {
@ -67,9 +73,21 @@ func ParseTxStatusFrame(data []byte) (*TxStatusFrame, error) {
return nil, fmt.Errorf("incorrect frame type for Tx status frame 0x%x", data[0]) return nil, fmt.Errorf("incorrect frame type for Tx status frame 0x%x", data[0])
} }
if len(data) < 7 {
return nil, errors.New("incomplete status frame")
}
status := &TxStatusFrame{ status := &TxStatusFrame{
Id: data[1], Id: data[1],
Status: TxStatus(data[2]), Status: TxStatus(data[5]),
NRetry: data[4],
}
if data[6] == 0 {
status.Routed = false
} else {
status.Routed = true
} }
return status, nil return status, nil

View file

@ -0,0 +1,118 @@
package xbee
import (
"reflect"
"testing"
)
func TestTxFrame_Bytes(t *testing.T) {
type fields struct {
Id byte
Destination uint64
BCastRadius uint8
Options uint8
Payload []byte
}
tests := []struct {
name string
fields fields
want []byte
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
txFrame := &TxFrame{
Id: tt.fields.Id,
Destination: tt.fields.Destination,
BCastRadius: tt.fields.BCastRadius,
Options: tt.fields.Options,
Payload: tt.fields.Payload,
}
if got := txFrame.Bytes(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TxFrame.Bytes() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseTxStatusFrame(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
want *TxStatusFrame
wantErr bool
}{
// TODO: Add test cases.
{
name: "wrong packet type",
args: args{
data: []byte{0x85, 0x47, 0xFF, 0xFE, 0x00, 0x00, 0x02},
},
want: nil,
wantErr: true,
},
{
name: "wrong packet length",
args: args{
data: []byte{0x8B, 0x47, 0xFF, 0xFE, 0x00, 0x00},
},
want: nil,
wantErr: true,
},
{
name: "success packet",
args: args{
data: []byte{0x8B, 0x47, 0xFF, 0xFE, 0x00, 0x00, 0x00},
},
want: &TxStatusFrame{
Id: 0x47,
NRetry: 0,
Status: TxStatusSuccess,
Routed: false,
},
wantErr: false,
},
{
name: "ack fail packet",
args: args{
data: []byte{0x8B, 0x47, 0xFF, 0xFE, 0x00, 0x01, 0x00},
},
want: &TxStatusFrame{
Id: 0x47,
NRetry: 0,
Status: TxStatusNoACK,
Routed: false,
},
wantErr: false,
},
{
name: "routed retried packet",
args: args{
data: []byte{0x8B, 0x47, 0xFF, 0xFE, 0x03, 0x01, 0x02},
},
want: &TxStatusFrame{
Id: 0x47,
NRetry: 3,
Status: TxStatusNoACK,
Routed: true,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseTxStatusFrame(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("ParseTxStatusFrame() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseTxStatusFrame() = %v, want %v", got, tt.want)
}
})
}
}