-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxbee_test.go
224 lines (188 loc) · 5.32 KB
/
xbee_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package gobee_test
import (
"fmt"
"github.com/pauleyj/gobee"
"github.com/pauleyj/gobee/api"
"github.com/pauleyj/gobee/api/rx"
"github.com/pauleyj/gobee/api/tx/at"
"testing"
"github.com/pauleyj/gobee/api/tx/at_queue"
"github.com/pauleyj/gobee/api/tx/zb"
)
func addressOf(b byte) *byte { return &b }
type Transmitter struct {
t *testing.T
expected []byte
}
func (xm *Transmitter) Transmit(p []byte) (int, error) {
msg := fmt.Sprintf("TX (len = %4d)", len(p))
for _, b := range p {
msg = fmt.Sprintf("%s 0x%02x", msg, b)
}
xm.t.Log(msg)
return len(p), nil
}
func (xm *Transmitter) SetExpectedWriteBytes(expected []byte) {
xm.expected = expected
}
type Receiver struct {
t *testing.T
}
func (r *Receiver) Receive(f rx.Frame) error {
switch f.(type) {
case *rx.AT:
validateAT(r.t, f.(*rx.AT))
}
return nil
}
func validateAT(t *testing.T, f *rx.AT) {
if f.ID() != 1 {
t.Errorf("Expected FrameId: 0x01, but got 0x%02x", f.ID())
}
}
func TestXBee_TX_AT(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
var atFrame = at.NewAT(at.FrameID(0x01),at.Command([2]byte{'N', 'I'}))
_, err := xbee.TX(atFrame)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
atFrame = at.NewAT(at.FrameID(0x01), at.Command([2]byte{'N', 'I'}), at.Parameter([]byte{0x11}))
xbee.SetAPIMode(api.EscapeModeActive)
_, err = xbee.TX(atFrame)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
func TestXBee_TX_AT_QUEUE(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
var atFrame = at_queue.NewATQueue(at_queue.FrameID(0x01), at_queue.Command([2]byte{'N', 'I'}))
_, err := xbee.TX(atFrame)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
atFrame = at_queue.NewATQueue(at_queue.FrameID(0x01), at_queue.Command([2]byte{'N', 'I'}), at_queue.Parameter([]byte{0x11}))
xbee.SetAPIMode(api.EscapeModeActive)
_, err = xbee.TX(atFrame)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
func TestXBee_RX_AT(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
// a valid AT command response
response := []byte{
0x7e, 0x00, 0x18, 0x88,
0x01, 0x4e, 0x49, 0x00,
0x20, 0x5a, 0x69, 0x67,
0x42, 0x65, 0x65, 0x20,
0x43, 0x6f, 0x6f, 0x72,
0x64, 0x69, 0x6e, 0x61,
0x74, 0x6f, 0x72, 0xe5}
for _, b := range response {
err := xbee.RX(b)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
}
func TestXBee_RX_AT_Escape(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.NewWithEscapeMode(transmitter, receiver, api.EscapeModeActive)
// a valid AT command response with escaped bytes
response := []byte{
0x7e, 0x00, 0x06, 0x88,
0x01, 0x4e, 0x49, 0x00,
0x7D, 0x31, 0xce}
for _, b := range response {
err := xbee.RX(b)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
}
func TestXBee_TX_Escape_Length(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.NewWithEscapeMode(transmitter, receiver, api.EscapeModeActive)
fakeParam := make([]byte, 0)
for i := 0; i < 0x110D; i++ {
fakeParam = append(fakeParam, 0)
}
zb := zb.NewZB(zb.FrameID(0x01), zb.Addr64(0), zb.Addr16(0),zb.BroadcastRadius(0), zb.Data(fakeParam))
_, err := xbee.TX(zb)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
func TestXBee_TX_Escape_Checksum(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.NewWithEscapeMode(transmitter, receiver, api.EscapeModeActive)
atFrame := at.NewAT(at.FrameID(0x01), at.Command([2]byte{'A', 'O'}),at.Parameter([]byte{0xE8}))
_, err := xbee.TX(atFrame)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
func TestXBee_TX_Invalid_API_Mode(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
err := xbee.SetAPIMode(api.EscapeMode(3))
if err == nil {
t.Error("Expected error, but got none")
}
}
func TestXBee_Rx_Unknown_Frame_Type(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
unknownFrame := []byte{0x7E, 0x00, 0x10, 0xFF}
for i, b := range unknownFrame {
err := xbee.RX(b)
if i == 3 {
if err == nil {
t.Error("Expected error, but got none")
}
} else if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
}
func TestXBee_RX_Invalid_Frame_Delimiter(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
err := xbee.RX(0x7D)
if err == nil {
t.Error("Expected error, but got none")
}
}
func TestXBee_RX_Invalid_Checksum(t *testing.T) {
transmitter := &Transmitter{t: t}
receiver := &Receiver{t: t}
xbee := gobee.New(transmitter, receiver)
bad_checksum := []byte{
0x7e, 0x00, 0x0f, 0x97, 0x02, 0x00, 0x13, 0xa2, 0x00,
0x40, 0x32, 0x03, 0xcf, 0x00, 0x00, 0x41, 0x4f, 0x00,
0xd0,
}
for i, b := range bad_checksum {
err := xbee.RX(b)
if i == 18 {
if err == nil {
t.Error("Expected error, but got none")
}
} else if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
}