forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_station_connection_test.go
225 lines (200 loc) · 6.85 KB
/
driver_station_connection_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
225
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"github.com/stretchr/testify/assert"
"net"
"testing"
"time"
)
func TestEncodeControlPacket(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
mainArena.Setup()
tcpConn := setupFakeTcpConnection(t)
defer tcpConn.Close()
dsConn, err := NewDriverStationConnection(254, "R1", tcpConn)
assert.Nil(t, err)
defer dsConn.Close()
data := dsConn.encodeControlPacket()
assert.Equal(t, byte(0), data[5])
assert.Equal(t, byte(0), data[6])
assert.Equal(t, byte(0), data[20])
assert.Equal(t, byte(15), data[21])
// Check the different alliance station values.
dsConn.AllianceStation = "R2"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(1), data[5])
dsConn.AllianceStation = "R3"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(2), data[5])
dsConn.AllianceStation = "B1"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(3), data[5])
dsConn.AllianceStation = "B2"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(4), data[5])
dsConn.AllianceStation = "B3"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(5), data[5])
// Check packet count rollover.
dsConn.packetCount = 255
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(0), data[0])
assert.Equal(t, byte(255), data[1])
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(1), data[0])
assert.Equal(t, byte(0), data[1])
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(1), data[0])
assert.Equal(t, byte(1), data[1])
dsConn.packetCount = 65535
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(255), data[0])
assert.Equal(t, byte(255), data[1])
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(0), data[0])
assert.Equal(t, byte(0), data[1])
// Check different robot statuses.
dsConn.Auto = true
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(2), data[3])
dsConn.Enabled = true
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(6), data[3])
dsConn.Auto = false
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(4), data[3])
dsConn.EmergencyStop = true
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(132), data[3])
// Check different match types.
mainArena.currentMatch.Type = "practice"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(1), data[6])
mainArena.currentMatch.Type = "qualification"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(2), data[6])
mainArena.currentMatch.Type = "elimination"
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(3), data[6])
// Check the countdown at different points during the match.
mainArena.MatchState = AUTO_PERIOD
mainArena.matchStartTime = time.Now().Add(-time.Duration(4 * time.Second))
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(11), data[21])
mainArena.MatchState = PAUSE_PERIOD
mainArena.matchStartTime = time.Now().Add(-time.Duration(16 * time.Second))
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(135), data[21])
mainArena.MatchState = TELEOP_PERIOD
mainArena.matchStartTime = time.Now().Add(-time.Duration(33 * time.Second))
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(119), data[21])
mainArena.MatchState = ENDGAME_PERIOD
mainArena.matchStartTime = time.Now().Add(-time.Duration(150 * time.Second))
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(2), data[21])
mainArena.MatchState = POST_MATCH
mainArena.matchStartTime = time.Now().Add(-time.Duration(180 * time.Second))
data = dsConn.encodeControlPacket()
assert.Equal(t, byte(0), data[21])
}
func TestSendControlPacket(t *testing.T) {
tcpConn := setupFakeTcpConnection(t)
defer tcpConn.Close()
dsConn, err := NewDriverStationConnection(254, "R1", tcpConn)
assert.Nil(t, err)
defer dsConn.Close()
// No real way of checking this since the destination IP is remote, so settle for there being no errors.
err = dsConn.sendControlPacket()
assert.Nil(t, err)
}
func TestDecodeStatusPacket(t *testing.T) {
tcpConn := setupFakeTcpConnection(t)
defer tcpConn.Close()
dsConn, err := NewDriverStationConnection(254, "R1", tcpConn)
assert.Nil(t, err)
defer dsConn.Close()
data := [36]byte{22, 28, 103, 19, 192, 0, 246, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0}
dsConn.decodeStatusPacket(data)
assert.Equal(t, 103, dsConn.MissedPacketCount)
assert.Equal(t, 14, dsConn.DsRobotTripTimeMs)
}
func TestListenForDriverStations(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
driverStationTcpListenAddress = "127.0.0.1"
go ListenForDriverStations()
mainArena.Setup()
time.Sleep(time.Millisecond * 10)
// Connect with an invalid initial packet.
tcpConn, err := net.Dial("tcp", "127.0.0.1:1750")
if assert.Nil(t, err) {
dataSend := [5]byte{0, 3, 29, 0, 0}
tcpConn.Write(dataSend[:])
var dataReceived [100]byte
_, err = tcpConn.Read(dataReceived[:])
assert.NotNil(t, err)
tcpConn.Close()
}
// Connect as a team not in the current match.
tcpConn, err = net.Dial("tcp", "127.0.0.1:1750")
if assert.Nil(t, err) {
dataSend := [5]byte{0, 3, 24, 5, 223}
tcpConn.Write(dataSend[:])
var dataReceived [5]byte
_, err = tcpConn.Read(dataReceived[:])
assert.Nil(t, err)
assert.Equal(t, [5]byte{0, 3, 25, 0, 2}, dataReceived)
tcpConn.Close()
}
// Connect as a team in the current match.
mainArena.AssignTeam(1503, "B2")
tcpConn, err = net.Dial("tcp", "127.0.0.1:1750")
if assert.Nil(t, err) {
defer tcpConn.Close()
dataSend := [5]byte{0, 3, 24, 5, 223}
tcpConn.Write(dataSend[:])
var dataReceived [5]byte
_, err = tcpConn.Read(dataReceived[:])
assert.Nil(t, err)
assert.Equal(t, [5]byte{0, 3, 25, 4, 0}, dataReceived)
time.Sleep(time.Millisecond * 10)
dsConn := mainArena.AllianceStations["B2"].DsConn
if assert.NotNil(t, dsConn) {
assert.Equal(t, 1503, dsConn.TeamId)
assert.Equal(t, "B2", dsConn.AllianceStation)
// Check that an unknown packet type gets ignored and a status packet gets decoded.
dataSend = [5]byte{0, 3, 37, 0, 0}
tcpConn.Write(dataSend[:])
time.Sleep(time.Millisecond * 10)
dataSend2 := [38]byte{0, 36, 22, 28, 103, 19, 192, 0, 246, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
tcpConn.Write(dataSend2[:])
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 103, dsConn.MissedPacketCount)
assert.Equal(t, 14, dsConn.DsRobotTripTimeMs)
}
}
}
func setupFakeTcpConnection(t *testing.T) net.Conn {
// Set up a fake TCP endpoint and connection to it.
l, err := net.Listen("tcp", ":9999")
assert.Nil(t, err)
defer l.Close()
tcpConn, err := net.Dial("tcp", "127.0.0.1:9999")
assert.Nil(t, err)
return tcpConn
}