-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatcommandresponse.go
More file actions
79 lines (66 loc) · 1.84 KB
/
atcommandresponse.go
File metadata and controls
79 lines (66 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package xbeeapi
import (
"bytes"
"fmt"
)
const MinATCommandResponseSize = 4
const (
ATCommandOK = iota
ATCommandError
ATCommandInvalidCommand
ATCommandInvalidParam
ATCommandRemoteTransFailed
ATCommandStatusUnknown
)
type ATCommandResponse struct {
FrameID byte
Command string
Status byte
Params []byte
}
func ParseATCommandResponse(rfd *RawFrameData) (*ATCommandResponse, error) {
if !rfd.IsValid() || rfd.FrameType() != FrameTypeATCommandResponse {
return nil, &FrameParseError{msg: "Expecting frame type ATCommandResponse"}
}
if len(rfd.Data()) < MinATCommandResponseSize {
return nil, &FrameParseError{msg: "Frame data too small for ATCommandResponse"}
}
buf := bytes.NewBuffer(rfd.Data())
at := &ATCommandResponse{
FrameID: buf.Next(1)[0],
Command: string(buf.Next(2)),
Status: buf.Next(1)[0],
Params: copySlice(buf.Bytes()),
}
if !at.IsValid() {
return nil, &FrameParseError{msg: "Invalid frame data for ATCommandResponse"}
}
return at, nil
}
func (atr *ATCommandResponse) RawFrameData() *RawFrameData {
rfd := concat([]byte{FrameTypeATCommandResponse, atr.FrameID}, []byte(atr.Command))
rfd = append(rfd, byte(atr.Status))
return NewRawFrameData(concat(rfd, atr.Params)...)
}
func (atr *ATCommandResponse) IsValid() bool {
if atr.Status < ATCommandStatusUnknown && len(atr.Command) == 2 {
return true
}
return false
}
func (at *ATCommandResponse) FrameType() byte {
return FrameTypeATCommandResponse
}
func ATCommandStatusDescription(status byte) string {
switch status {
case ATCommandOK:
return fmt.Sprintf("OK %d", status)
case ATCommandError:
return fmt.Sprintf("Error %d", status)
case ATCommandInvalidCommand:
return fmt.Sprintf("Invalid Command %d", status)
case ATCommandInvalidParam:
return fmt.Sprintf("Invalid Params%d", status)
}
return fmt.Sprintf("AT Command Status Unknown %d", status)
}