root / trunk / code / projects / hunter_prey / testbench / main.c @ 1442
History | View | Annotate | Download (4.82 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 |
|
28 |
#define CHANNEL 0xF // channel for wireless communication |
29 |
#define TYPE 42 // packet type for wireless communication |
30 |
#define ROBOTID 255 // make up a robot id because the PC doesn't have one |
31 |
|
32 |
int main(int argc, char *argv[]) |
33 |
{ |
34 |
char send_buffer[2]; // holds data to send |
35 |
int data_length; // length of data received |
36 |
unsigned char *packet_data; // data received |
37 |
|
38 |
// set up wireless
|
39 |
wl_basic_init_default(); |
40 |
wl_set_channel(CHANNEL); |
41 |
wl_set_com_port("/dev/ttyUSB0");
|
42 |
|
43 |
printf("Testing communications\n\n");
|
44 |
|
45 |
// Receive TAG, send ACK
|
46 |
printf("Receive TAG, send ACK... ");
|
47 |
fflush(stdout); |
48 |
// Wait until we receive a packet
|
49 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
50 |
|
51 |
if (data_length > 2) |
52 |
{ |
53 |
printf("Excessive TAG packet length... ");
|
54 |
fflush(stdout); |
55 |
} |
56 |
|
57 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
58 |
{ |
59 |
// send back an ACK
|
60 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
61 |
send_buffer[1] = packet_data[1]; |
62 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
63 |
|
64 |
printf("PASSED\n");
|
65 |
} |
66 |
else
|
67 |
printf("FAILED\n");
|
68 |
|
69 |
// Send a TAG, receive an ACK
|
70 |
printf("Send TAG, wait for ACK... ");
|
71 |
fflush(stdout); |
72 |
|
73 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
74 |
send_buffer[1] = ROBOTID;
|
75 |
// robot number stays the same
|
76 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
77 |
|
78 |
// Wait for an ACK
|
79 |
usleep(800000); // wait for 800 ms before sending ACK |
80 |
packet_data = wl_basic_do_default(&data_length); |
81 |
|
82 |
// Check ACK for presence and correctness
|
83 |
if (!packet_data || data_length < 2 || |
84 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
85 |
packet_data[1] != ROBOTID)
|
86 |
printf("FAILED\n");
|
87 |
else
|
88 |
printf("PASSED\n");
|
89 |
|
90 |
// Receive a TAG, send a delayed ACK
|
91 |
printf("Receive a TAG, send a delayed ACK... ");
|
92 |
fflush(stdout); |
93 |
|
94 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
95 |
|
96 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
97 |
{ |
98 |
// wait before sending ACK back
|
99 |
usleep(900000);
|
100 |
|
101 |
if (wl_basic_do_default(&data_length))
|
102 |
printf("FAILED\n");
|
103 |
else
|
104 |
{ |
105 |
// send packet
|
106 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
107 |
send_buffer[1] = packet_data[1]; |
108 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
109 |
|
110 |
printf("PASSED\n");
|
111 |
} |
112 |
} |
113 |
|
114 |
else
|
115 |
printf("FAILED\n");
|
116 |
|
117 |
// Receive a TAG, never send an ACK
|
118 |
printf("Receive TAG, never send ACK... ");
|
119 |
fflush(stdout); |
120 |
|
121 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
122 |
|
123 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
124 |
{ |
125 |
usleep(5000000); // wait 5 seconds to see if they TAG again |
126 |
|
127 |
if (wl_basic_do_default(&data_length))
|
128 |
printf("FAILED\n");
|
129 |
else
|
130 |
printf("PASSED\n");
|
131 |
} |
132 |
|
133 |
else
|
134 |
printf("FAILED\n");
|
135 |
|
136 |
// Receive TAG, send ACK to incorrect robot
|
137 |
printf("Receive TAG, send ACK to incorrect robot... ");
|
138 |
fflush(stdout); |
139 |
|
140 |
// Wait until we receive a packet
|
141 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
142 |
|
143 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
144 |
{ |
145 |
// send back an ACK
|
146 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
147 |
send_buffer[1] = packet_data[1] + 1; |
148 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
149 |
|
150 |
printf("PASSED\n");
|
151 |
} |
152 |
else
|
153 |
printf("FAILED\n");
|
154 |
|
155 |
// Simulate TAG from robot A to B, ACK from robot B to A
|
156 |
printf("Send TAG from robot A to B, ACK from B to A... ");
|
157 |
fflush(stdout); |
158 |
|
159 |
// TAG
|
160 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
161 |
send_buffer[1] = packet_data[1] - 1; // robot other than testee |
162 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
163 |
|
164 |
// ACK
|
165 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
166 |
// send_buffer[1] stays the same
|
167 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
168 |
|
169 |
if (wl_basic_do_default(&data_length))
|
170 |
printf("FAILED\n");
|
171 |
else
|
172 |
printf("PASSED\n");
|
173 |
|
174 |
return 0; |
175 |
} |
176 |
|