From 6a5cda8c6686751b500c374dad95463610931437 Mon Sep 17 00:00:00 2001 From: saji Date: Thu, 4 May 2023 14:05:51 -0500 Subject: [PATCH] txstatusframe fixes and tests --- internal/xbee/txframe.go | 36 ++++++++--- internal/xbee/txframe_test.go | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 internal/xbee/txframe_test.go diff --git a/internal/xbee/txframe.go b/internal/xbee/txframe.go index 7f370cd..e03cf74 100644 --- a/internal/xbee/txframe.go +++ b/internal/xbee/txframe.go @@ -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 diff --git a/internal/xbee/txframe_test.go b/internal/xbee/txframe_test.go new file mode 100644 index 0000000..a4d10ea --- /dev/null +++ b/internal/xbee/txframe_test.go @@ -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) + } + }) + } +}