atcmdresponse parsing fixes and tests

This commit is contained in:
saji 2023-05-04 14:05:37 -05:00
parent 0c386780c1
commit b7840aa9cb
2 changed files with 41 additions and 7 deletions

View file

@ -3,7 +3,7 @@ package xbee
import (
"bytes"
"encoding/binary"
"fmt"
"errors"
)
// This code is handled slightly differently. Rather than use structs to represent
@ -13,7 +13,6 @@ import (
// The downside is that it's more difficult to deal with.
//
// AT command responses are handled with a struct to get the response status code.
//
// an ATCmd is anything that has a Payload function that returns the given payload
// pre-formatted to be send over the wire, and a Cmd command that returns the 2-character
@ -70,15 +69,16 @@ type ATCmdResponse struct {
func ParseATCmdResponse(p []byte) (*ATCmdResponse, error) {
if p[0] != 0x88 {
return nil, fmt.Errorf("invalid frame type 0x%x", p[0])
return nil, errors.New("not an AT command response")
}
if len(p) < 5 {
return nil, errors.New("received partial AT command response")
}
resp := &ATCmdResponse{
Cmd: string(p[2:4]),
Status: ATCmdStatus(p[4]),
// TODO: check if this overflows when there's no payload.
Data: p[5:],
Data: p[5:],
}
return resp, nil
}

View file

@ -15,7 +15,41 @@ func TestParseATCmdResponse(t *testing.T) {
want *ATCmdResponse
wantErr bool
}{
// TODO: Add test cases.
{
name: "AT Command Change OK",
args: args{
p: []byte{0x88, 0x53, 0x49, 0x44, 0x00},
},
want: &ATCmdResponse{
Cmd: "ID",
Data: []byte{},
Status: ATCmdStatusOK,
},
wantErr: false,
},
{
name: "AT Command Query OK",
args: args{
p: []byte{0x88, 0x53, 0x49, 0x44, 0x00, 0x43, 0xEF},
},
want: &ATCmdResponse{
Cmd: "ID",
Data: []byte{0x43, 0xEF},
Status: ATCmdStatusOK,
},
},
{
name: "AT Command Parameter Error",
args: args{
p: []byte{0x88, 0x53, 0x49, 0x44, 0x03},
},
want: &ATCmdResponse{
Cmd: "ID",
Data: []byte{},
Status: ATCmdStatusInvalidParam,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {