atcmdresponse parsing fixes and tests
This commit is contained in:
parent
0c386780c1
commit
b7840aa9cb
|
@ -3,7 +3,7 @@ package xbee
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This code is handled slightly differently. Rather than use structs to represent
|
// 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.
|
// 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.
|
// 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
|
// 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
|
// 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) {
|
func ParseATCmdResponse(p []byte) (*ATCmdResponse, error) {
|
||||||
|
|
||||||
if p[0] != 0x88 {
|
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{
|
resp := &ATCmdResponse{
|
||||||
Cmd: string(p[2:4]),
|
Cmd: string(p[2:4]),
|
||||||
Status: ATCmdStatus(p[4]),
|
Status: ATCmdStatus(p[4]),
|
||||||
// TODO: check if this overflows when there's no payload.
|
|
||||||
Data: p[5:],
|
Data: p[5:],
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,41 @@ func TestParseATCmdResponse(t *testing.T) {
|
||||||
want *ATCmdResponse
|
want *ATCmdResponse
|
||||||
wantErr bool
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue