From b7840aa9cbae97c21cfc1ec69fd085729714f4ea Mon Sep 17 00:00:00 2001 From: saji Date: Thu, 4 May 2023 14:05:37 -0500 Subject: [PATCH] atcmdresponse parsing fixes and tests --- internal/xbee/at.go | 12 ++++++------ internal/xbee/at_test.go | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/internal/xbee/at.go b/internal/xbee/at.go index 09f1cdc..b2fb529 100644 --- a/internal/xbee/at.go +++ b/internal/xbee/at.go @@ -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 } diff --git a/internal/xbee/at_test.go b/internal/xbee/at_test.go index 269e30f..611e41c 100644 --- a/internal/xbee/at_test.go +++ b/internal/xbee/at_test.go @@ -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) {