root / trunk / code / projects / hunter_prey / testbench / main.c @ 1447
History | View | Annotate | Download (6.33 KB)
1 |
/* testbench for Lab 2 first checkpoint
|
---|---|
2 |
* determine whether robot under test complies with communication standard
|
3 |
* to be conducted using XBee USB dongle
|
4 |
*/
|
5 |
|
6 |
/* The tests shall be as follows
|
7 |
* 1. Receive a TAG, send an ACK
|
8 |
* - Robot passes if it sends a tag and changes to prey state
|
9 |
* 2. Send a TAG, receive an ACK
|
10 |
* - Pass if sends ACK, changes to wait state, then hunter state
|
11 |
* 3. Receive TAG, send slightly delayed (< 1s) ACK
|
12 |
* - Pass if sends one and only one TAG packet (and changes state appropriately)
|
13 |
* 4. Receive TAG, do not send ACK
|
14 |
* - Pass if sends one and only one TAG packet (and does not change state)
|
15 |
* 5. Receive TAG, send ACK to incorrect robot
|
16 |
* - Pass if goes to wait state, then back to hunter state
|
17 |
* 6. Simulate TAG from robot A to B, ACK from B to A
|
18 |
* - Pass if ignores TAG, goes to wait then hunter in response to ACK
|
19 |
*/
|
20 |
|
21 |
#include <stdlib.h> |
22 |
#include <stdio.h> |
23 |
#include <unistd.h> |
24 |
#include "../../libwireless/lib/wl_basic.h" |
25 |
#include "../../libwireless/lib/wireless.h" |
26 |
#include "hunter_prey.h" |
27 |
#include <time.h> |
28 |
|
29 |
#define CHANNEL 0xF // channel for wireless communication |
30 |
#define TYPE 42 // packet type for wireless communication |
31 |
#define ROBOTID 255 // make up a robot id because the PC doesn't have one |
32 |
|
33 |
int main(int argc, char *argv[]) |
34 |
{ |
35 |
char send_buffer[2]; // holds data to send |
36 |
int data_length; // length of data received |
37 |
unsigned char *packet_data; // data received |
38 |
int ret;
|
39 |
|
40 |
struct timespec delay8, rem;
|
41 |
|
42 |
delay8.tv_sec = 2;
|
43 |
delay8.tv_nsec = 800000000;
|
44 |
|
45 |
wl_set_com_port("/dev/ttyUSB0");
|
46 |
// set up wireless
|
47 |
wl_basic_init_default(); |
48 |
wl_set_channel(CHANNEL); |
49 |
|
50 |
printf("Testing communications\n\n");
|
51 |
|
52 |
// Receive TAG, send ACK
|
53 |
printf("Receive TAG, send ACK... ");
|
54 |
fflush(stdout); |
55 |
// Wait until we receive a packet
|
56 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
57 |
|
58 |
printf("got packet, validating and sending ack...");
|
59 |
fflush(stdout); |
60 |
|
61 |
if (data_length > 2) |
62 |
{ |
63 |
printf("Excessive TAG packet length... ");
|
64 |
fflush(stdout); |
65 |
} |
66 |
|
67 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
68 |
{ |
69 |
// send back an ACK
|
70 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
71 |
send_buffer[1] = packet_data[1]; |
72 |
printf("sending ack intended for robot %d\n", packet_data[1]); |
73 |
fflush(stdout); |
74 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
75 |
|
76 |
printf("PASSED\n");
|
77 |
} |
78 |
else
|
79 |
printf("FAILED\n");
|
80 |
|
81 |
|
82 |
sleep(10);
|
83 |
|
84 |
// Send a TAG, receive an ACK
|
85 |
printf("Send TAG, wait for ACK... ");
|
86 |
fflush(stdout); |
87 |
|
88 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
89 |
send_buffer[1] = ROBOTID;
|
90 |
// robot number stays the same
|
91 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
92 |
|
93 |
// Wait for an ACK
|
94 |
ret = nanosleep(&delay8, &rem); // wait for 800 ms before sending ACK
|
95 |
|
96 |
while(ret) {
|
97 |
// printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
|
98 |
ret = nanosleep(&rem, &rem); |
99 |
} |
100 |
|
101 |
data_length = -1;
|
102 |
packet_data = wl_basic_do_default(&data_length); |
103 |
|
104 |
// Check ACK for presence and correctness
|
105 |
if (!packet_data || data_length < 2 || |
106 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
107 |
packet_data[1] != ROBOTID)
|
108 |
printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
|
109 |
else
|
110 |
printf("PASSED\n");
|
111 |
|
112 |
// Receive a TAG, send a delayed ACK
|
113 |
printf("Receive a TAG, send a delayed ACK... ");
|
114 |
fflush(stdout); |
115 |
|
116 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
117 |
|
118 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
119 |
{ |
120 |
printf("got TAG, waiting...");
|
121 |
fflush(stdout); |
122 |
// wait before sending ACK back
|
123 |
//usleep(900000);
|
124 |
delay8.tv_sec = 0;
|
125 |
ret = nanosleep(&delay8, &rem); // wait for 800 ms before sending ACK
|
126 |
|
127 |
while(ret) {
|
128 |
printf(".");
|
129 |
fflush(stdout);// printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
|
130 |
ret = nanosleep(&rem, &rem); |
131 |
} |
132 |
|
133 |
|
134 |
if (wl_basic_do_default(&data_length))
|
135 |
printf("FAILED, got another TAG too soon\n");
|
136 |
else
|
137 |
{ |
138 |
// send packet
|
139 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
140 |
send_buffer[1] = packet_data[1]; |
141 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
142 |
|
143 |
printf("PASSED\n");
|
144 |
} |
145 |
} |
146 |
|
147 |
else
|
148 |
printf("FAILED\n");
|
149 |
|
150 |
|
151 |
printf("sending courtesy tag...");
|
152 |
fflush(stdout); |
153 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
154 |
send_buffer[1] = ROBOTID;
|
155 |
// robot number stays the same
|
156 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
157 |
printf("done.\nwaiting for ack...");
|
158 |
fflush(stdout); |
159 |
|
160 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
161 |
|
162 |
if (!packet_data || data_length < 2 || |
163 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
164 |
packet_data[1] != ROBOTID)
|
165 |
printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
|
166 |
else
|
167 |
printf("done\n");
|
168 |
|
169 |
|
170 |
// Receive a TAG, never send an ACK
|
171 |
printf("Receive TAG, never send ACK... ");
|
172 |
fflush(stdout); |
173 |
|
174 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
175 |
|
176 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
177 |
{ |
178 |
|
179 |
printf("got TAG, monitoring for 5 seconds\n");
|
180 |
fflush(stdout); |
181 |
usleep(5000000); // wait 5 seconds to see if they TAG again |
182 |
|
183 |
if (wl_basic_do_default(&data_length))
|
184 |
printf("FAILED, robot is spamming\n");
|
185 |
else
|
186 |
printf("PASSED\n");
|
187 |
} |
188 |
|
189 |
else
|
190 |
printf("FAILED\n");
|
191 |
|
192 |
// Receive TAG, send ACK to incorrect robot
|
193 |
printf("Receive TAG, send ACK to incorrect robot... ");
|
194 |
fflush(stdout); |
195 |
|
196 |
// Wait until we receive a packet
|
197 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
198 |
|
199 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
200 |
{ |
201 |
// send back an ACK
|
202 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
203 |
send_buffer[1] = packet_data[1] + 1; |
204 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
205 |
|
206 |
printf("PASSED\n");
|
207 |
} |
208 |
else
|
209 |
printf("FAILED\n");
|
210 |
|
211 |
// Simulate TAG from robot A to B, ACK from robot B to A
|
212 |
printf("Send TAG from robot A to B, ACK from B to A... ");
|
213 |
fflush(stdout); |
214 |
|
215 |
// TAG
|
216 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
217 |
send_buffer[1] = packet_data[1] - 1; // robot other than testee |
218 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
219 |
|
220 |
// ACK
|
221 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
222 |
// send_buffer[1] stays the same
|
223 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
224 |
|
225 |
if (wl_basic_do_default(&data_length))
|
226 |
printf("FAILED\n");
|
227 |
else
|
228 |
printf("PASSED\n");
|
229 |
|
230 |
return 0; |
231 |
} |
232 |
|