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 (
"bytes"
"encoding/binary"
"errors"
"fmt"
)
@ -48,18 +49,23 @@ type TxStatus uint8
//go:generate stringer -output=txStatus.go -type TxStatus
const (
TxStatusSuccess TxStatus = 0x00
TxStatusNoACK TxStatus = 0x01
TxStatusCCAFail TxStatus = 0x02
TxStatusIndirect TxStatus = 0x03
TxStatusTxFail TxStatus = 0x04
TxStatusACKFail TxStatus = 0x21
// TODO: finish this
TxStatusSuccess TxStatus = 0x00
TxStatusNoACK TxStatus = 0x01
TxStatusCCAFail TxStatus = 0x02
TxStatusIndirect TxStatus = 0x03
TxStatusACKFail TxStatus = 0x21
TxStatusNoRoute TxStatus = 0x25
TxStatusResourceError TxStatus = 0x31
TxStatusResourceUnavail TxStatus = 0x32
TxStatusPayloadTooLarge TxStatus = 0x74
TxStatusIndirectUnrequested TxStatus = 0x75
)
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.
Routed bool
}
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])
}
if len(data) < 7 {
return nil, errors.New("incomplete status frame")
}
status := &TxStatusFrame{
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

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)
}
})
}
}