root / trunk / code / projects / hunter_prey / testbench / main.c @ 1441
History | View | Annotate | Download (4.67 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 |
// Wait until we receive a packet
|
48 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
49 |
|
50 |
if (data_length > 2) |
51 |
printf("Excessive TAG packet length... ");
|
52 |
|
53 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
54 |
{ |
55 |
// send back an ACK
|
56 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
57 |
send_buffer[1] = packet_data[1]; |
58 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
59 |
|
60 |
printf("PASSED\n");
|
61 |
} |
62 |
else
|
63 |
printf("FAILED\n");
|
64 |
|
65 |
// Send a TAG, receive an ACK
|
66 |
printf("Send TAG, wait for ACK... ");
|
67 |
|
68 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
69 |
send_buffer[1] = ROBOTID;
|
70 |
// robot number stays the same
|
71 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
72 |
|
73 |
// Wait for an ACK
|
74 |
usleep(800000); // wait for 800 ms before sending ACK |
75 |
packet_data = wl_basic_do_default(&data_length); |
76 |
|
77 |
// Check ACK for presence and correctness
|
78 |
if (!packet_data || data_length < 2 || |
79 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
80 |
packet_data[1] != ROBOTID)
|
81 |
printf("FAILED\n");
|
82 |
else
|
83 |
printf("PASSED\n");
|
84 |
|
85 |
// Receive a TAG, send a delayed ACK
|
86 |
printf("Receive a TAG, send a delayed ACK... ");
|
87 |
|
88 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
89 |
|
90 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
91 |
{ |
92 |
// wait before sending ACK back
|
93 |
usleep(900000);
|
94 |
|
95 |
if (wl_basic_do_default(&data_length))
|
96 |
printf("FAILED\n");
|
97 |
else
|
98 |
{ |
99 |
// send packet
|
100 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
101 |
send_buffer[1] = packet_data[1]; |
102 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
103 |
|
104 |
printf("PASSED\n");
|
105 |
} |
106 |
} |
107 |
|
108 |
else
|
109 |
printf("FAILED\n");
|
110 |
|
111 |
// Receive a TAG, never send an ACK
|
112 |
printf("Receive TAG, never send ACK... ");
|
113 |
|
114 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
115 |
|
116 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
117 |
{ |
118 |
usleep(5000000); // wait 5 seconds to see if they TAG again |
119 |
|
120 |
if (wl_basic_do_default(&data_length))
|
121 |
printf("FAILED\n");
|
122 |
else
|
123 |
printf("PASSED\n");
|
124 |
} |
125 |
|
126 |
else
|
127 |
printf("FAILED\n");
|
128 |
|
129 |
// Receive TAG, send ACK to incorrect robot
|
130 |
printf("Receive TAG, send ACK to incorrect robot... ");
|
131 |
|
132 |
// Wait until we receive a packet
|
133 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
134 |
|
135 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
136 |
{ |
137 |
// send back an ACK
|
138 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
139 |
send_buffer[1] = packet_data[1] + 1; |
140 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
141 |
|
142 |
printf("PASSED\n");
|
143 |
} |
144 |
else
|
145 |
printf("FAILED\n");
|
146 |
|
147 |
// Simulate TAG from robot A to B, ACK from robot B to A
|
148 |
printf("Send TAG from robot A to B, ACK from B to A... ");
|
149 |
|
150 |
// TAG
|
151 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
152 |
send_buffer[1] = packet_data[1] - 1; // robot other than testee |
153 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
154 |
|
155 |
// ACK
|
156 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
157 |
// send_buffer[1] stays the same
|
158 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
159 |
|
160 |
if (wl_basic_do_default(&data_length))
|
161 |
printf("FAILED\n");
|
162 |
else
|
163 |
printf("PASSED\n");
|
164 |
|
165 |
return 0; |
166 |
} |
167 |
|