-
Notifications
You must be signed in to change notification settings - Fork 38
/
tests.c
257 lines (217 loc) · 5.84 KB
/
tests.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <stdbool.h>
#include "hardware.h"
#include "subg_rfspy.h"
#include "serial.h"
#include "commands.h"
#include "version.h"
#define RESPONSE_BUFFER_SIZE 128
typedef struct {
uint8_t response_code;
uint8_t data[RESPONSE_BUFFER_SIZE];
uint8_t response_length;
bool timed_out;
} CommandResponse;
void hexprint(const char *prefix, const uint8_t *data, int len)
{
printf("%s", prefix);
for (int i=0; i<len; i++) {
printf("%02x", data[i]);
}
printf("\n");
}
void tiny_sleep()
{
// Sleep for 1ms
struct timespec rqtp;
rqtp.tv_sec = 0;
rqtp.tv_nsec = 1000000L;
nanosleep(&rqtp, NULL);
}
void do_spi(const uint8_t *input, uint8_t *output, uint8_t len)
{
if (len > RESPONSE_BUFFER_SIZE) {
printf("Bad response size: %d\n", len);
len = RESPONSE_BUFFER_SIZE;
}
uint8_t throwaway_rx_buf[RESPONSE_BUFFER_SIZE];
if (input) {
hexprint("tx: ", input, len);
} else {
printf("tx: NULL\n");
}
if (output == NULL) {
output = throwaway_rx_buf;
}
uint8_t slave_byte;
for (int i=0; i<len; i++) {
slave_byte = U1DBUF_write;
tx1_isr();
U1DBUF_read = input ? input[i] : 0;
if (output) {
output[i] = slave_byte;
}
rx1_isr();
tiny_sleep();
}
if (output) {
hexprint("rx: ", output, len);
} else {
printf("rx: NULL\n");
}
}
void send_command(uint8_t len, const uint8_t *data)
{
uint8_t tmp[2];
tmp[0] = 0x99;
tmp[1] = len;
do_spi(tmp, tmp, 2);
uint8_t slave_bytes_avail = tmp[1];
if (slave_bytes_avail > 0) {
printf("Unexpected response data while sending command\n");
if (slave_bytes_avail > len) {
len = slave_bytes_avail;
}
}
do_spi(data, NULL, len);
}
CommandResponse wait_for_response(int max_xfer_cycles) {
CommandResponse response;
uint8_t tmp[2];
memset(&response, 0, sizeof(CommandResponse));
while(max_xfer_cycles--) {
tmp[0] = 0x99;
tmp[1] = 0;
do_spi(tmp, tmp, 2);
if (tmp[1] > 0) {
response.response_length = tmp[1];
do_spi(NULL, (uint8_t*)&response, response.response_length);
break;
}
// TODO: check timeout
}
if (max_xfer_cycles <= 0) {
response.timed_out = true;
}
return response;
}
CommandResponse run_command(uint8_t len, const uint8_t *data, int max_xfer_cycles)
{
send_command(len, data);
return wait_for_response(max_xfer_cycles);
}
bool equal(int expected, int actual, const char *desc)
{
if (expected != actual) {
fprintf(stderr, "Expected %s to equal %d, but was %d instead.\n", desc, expected, actual);
}
return expected == actual;
}
void check_version()
{
uint8_t cmd = CmdGetVersion;
CommandResponse response;
response = run_command(1, &cmd, 20);
assert(!response.timed_out);
assert(strcmp((const char*)response.data, SUBG_RFSPY_VERSION) == 0);
}
void check_sync_error_extra_byte()
{
CommandResponse response;
uint8_t tmp[1];
// Send extra byte (not conforming to protocol)
tmp[0] = 0x99;
do_spi(tmp, tmp, 1);
tmp[0] = CmdGetVersion;
response = run_command(1, tmp, 20);
// Expected behavior: timeout
assert(response.timed_out);
// Should recover
tmp[0] = CmdGetVersion;
response = run_command(1, tmp, 20);
assert(!response.timed_out);
assert(strcmp((const char*)response.data, SUBG_RFSPY_VERSION) == 0);
}
void check_sync_error_dropped_byte()
{
CommandResponse response;
uint8_t tmp[3];
// Drop byte (not conforming to protocol)
// CmdUpdateRegister should be 3 bytes, but only send 2
tmp[0] = CmdUpdateRegister;
tmp[1] = 1; // SYNC0
tmp[2] = 0xff;
response = run_command(2, tmp, 20);
// Expected behavior: timeout
assert(response.timed_out);
// one failure (this data finishes the previous update register command)
// subg_rfspy responds with success, since it received enough data for
// the update register command. But we're out of sync now.
tmp[0] = CmdGetVersion;
response = run_command(1, tmp, 20);
assert(!response.timed_out);
assert(equal(1, response.response_length, "response.response_length"));
// Recovery
tmp[0] = CmdGetVersion;
response = run_command(1, tmp, 20);
assert(!response.timed_out);
assert(strcmp((const char*)response.data, SUBG_RFSPY_VERSION) == 0);
}
void check_interrupting_command()
{
CommandResponse response;
uint8_t tmp[6];
// Send a listening command (4s timeout)
tmp[0] = CmdGetPacket;
tmp[1] = 1; // channel
tmp[2] = 1; // timeout(4)
tmp[3] = 0; // timeout(3)
tmp[4] = 0; // timeout(2)
tmp[5] = 0; // timeout(1)
send_command(6, tmp);
// Interrupt it with a set register command
tmp[0] = CmdUpdateRegister;
tmp[1] = 1; // SYNC0
tmp[2] = 0xff;
send_command(3, tmp);
response = wait_for_response(20);
// Expected response: command interrupted
assert(!response.timed_out);
assert(equal(RESPONSE_CODE_CMD_INTERRUPTED, response.response_code, "response.response_code"));
response = wait_for_response(20);
// Expected response: command successful
assert(!response.timed_out);
assert(equal(RESPONSE_CODE_SUCCESS, response.response_code, "response.response_code"));
}
void *run_main(void *vargp) {
printf("starting main thread\n");
subg_rfspy_main();
return NULL;
}
int main(void)
{
#ifdef MOCK_HARDWARE
pthread_t mock_hardware_thread;
pthread_create(&mock_hardware_thread, NULL, run_mock_hardware, NULL);
#endif
#ifdef RUN_TESTS
pthread_t run_main_thread;
pthread_create(&run_main_thread, NULL, run_main, NULL);
#endif
while(!subg_rfspy_init_finished);
// Run tests!
check_version();
check_sync_error_extra_byte();
check_sync_error_dropped_byte();
check_interrupting_command();
subg_rfspy_should_exit = true;
pthread_join(run_main_thread, NULL);
mock_hardware_should_exit = true;
pthread_join(mock_hardware_thread, NULL);
// If we are here, all tests passed.
printf("Tests succeeded!\n");
}