Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / hunter_prey / testbench / main.c @ 1453

History | View | Annotate | Download (7.27 KB)

1 1434 alevkoy
/* 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 1433 alevkoy
6 1434 alevkoy
/* The tests shall be as follows
7 1438 alevkoy
 * 1. Receive a TAG, send an ACK
8 1434 alevkoy
 * - Robot passes if it sends a tag and changes to prey state
9 1438 alevkoy
 * 2. Send a TAG, receive an ACK
10 1434 alevkoy
 * - Pass if sends ACK, changes to wait state, then hunter state
11 1438 alevkoy
 * 3. Receive TAG, send slightly delayed (< 1s) ACK
12 1434 alevkoy
 * - Pass if sends one and only one TAG packet (and changes state appropriately)
13 1438 alevkoy
 * 4. Receive TAG, do not send ACK
14 1434 alevkoy
 * - Pass if sends one and only one TAG packet (and does not change state)
15 1438 alevkoy
 * 5. Receive TAG, send ACK to incorrect robot
16 1434 alevkoy
 * - Pass if goes to wait state, then back to hunter state
17 1438 alevkoy
 * 6. Simulate TAG from robot A to B, ACK from B to A
18 1434 alevkoy
 * - Pass if ignores TAG, goes to wait then hunter in response to ACK
19
 */
20
21 1433 alevkoy
#include <stdlib.h>
22
#include <stdio.h>
23 1441 alevkoy
#include <unistd.h>
24
#include "../../libwireless/lib/wl_basic.h"
25 1438 alevkoy
#include "../../libwireless/lib/wireless.h"
26 1433 alevkoy
#include "hunter_prey.h"
27 1447 bneuman
#include <time.h>
28 1433 alevkoy
29 1434 alevkoy
#define TYPE 42        // packet type for wireless communication
30 1438 alevkoy
#define ROBOTID 255 // make up a robot id because the PC doesn't have one
31 1434 alevkoy
32 1451 bneuman
33
void waitKey() {
34
  unsigned char buf[10];
35
36
  //read from stdin
37
   while(read(0, buf, 10)==10) {
38
     printf("!");
39
     fflush(stdout);
40
   }
41
42
  while(getchar()==-1);
43
}
44
45 1438 alevkoy
int main(int argc, char *argv[])
46 1433 alevkoy
{
47 1434 alevkoy
    char send_buffer[2];    // holds data to send
48
    int data_length;        // length of data received
49
    unsigned char *packet_data;        // data received
50 1447 bneuman
    int ret;
51 1433 alevkoy
52 1453 bneuman
    unsigned int channel = 0xF;
53
54 1447 bneuman
    struct timespec delay8, rem;
55
56 1453 bneuman
    if(argc > 1) {
57
      channel = atoi(argv[1]);
58
    }
59
60
    printf("using wireless channel %d\n", channel);
61
62 1447 bneuman
    delay8.tv_sec = 2;
63
    delay8.tv_nsec = 800000000;
64
65
    wl_set_com_port("/dev/ttyUSB0");
66 1434 alevkoy
    // set up wireless
67
    wl_basic_init_default();
68 1453 bneuman
    wl_set_channel(channel);
69 1434 alevkoy
70
    printf("Testing communications\n\n");
71
72
    // Receive TAG, send ACK
73
    printf("Receive TAG, send ACK... ");
74 1442 alevkoy
    fflush(stdout);
75 1434 alevkoy
    // Wait until we receive a packet
76
    while (!(packet_data = wl_basic_do_default(&data_length)));
77
78 1447 bneuman
    printf("got packet, validating and sending ack...");
79
    fflush(stdout);
80
81 1434 alevkoy
    if (data_length > 2)
82 1442 alevkoy
    {
83 1434 alevkoy
        printf("Excessive TAG packet length... ");
84 1442 alevkoy
        fflush(stdout);
85
    }
86 1434 alevkoy
87
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
88
    {
89
        // send back an ACK
90 1438 alevkoy
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
91 1434 alevkoy
        send_buffer[1] = packet_data[1];
92 1447 bneuman
        printf("sending ack intended for robot %d\n", packet_data[1]);
93
        fflush(stdout);
94 1434 alevkoy
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
95
96 1438 alevkoy
        printf("PASSED\n");
97 1434 alevkoy
    }
98
    else
99 1438 alevkoy
        printf("FAILED\n");
100
101 1453 bneuman
    printf("robot should be prey\nPress enter to continue...\n");
102 1451 bneuman
    waitKey();
103 1447 bneuman
104 1438 alevkoy
    // Send a TAG, receive an ACK
105
    printf("Send TAG, wait for ACK... ");
106 1442 alevkoy
    fflush(stdout);
107 1438 alevkoy
108
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
109
    send_buffer[1] = ROBOTID;
110
    // robot number stays the same
111
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
112
113
    // Wait for an ACK
114 1447 bneuman
    ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
115
116
    while(ret) {
117
      //      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
118
      ret = nanosleep(&rem, &rem);
119
    }
120
121
    data_length = -1;
122 1438 alevkoy
    packet_data = wl_basic_do_default(&data_length);
123
124
    // Check ACK for presence and correctness
125
    if (!packet_data || data_length < 2 ||
126
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
127 1451 bneuman
        packet_data[1] != ROBOTID) {
128 1447 bneuman
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
129 1451 bneuman
      if(data_length >= 2) {
130
        printf("\tpacket = [%c, %d]\n",packet_data[0], packet_data[1]);
131
      }
132
    }
133 1438 alevkoy
    else
134
        printf("PASSED\n");
135
136 1453 bneuman
    printf("Robot should be Hunter\nPress enter to continue...\n");
137
138 1451 bneuman
    waitKey();
139
140 1438 alevkoy
    // Receive a TAG, send a delayed ACK
141
    printf("Receive a TAG, send a delayed ACK... ");
142 1442 alevkoy
    fflush(stdout);
143 1438 alevkoy
144
    while (!(packet_data = wl_basic_do_default(&data_length)));
145
146
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
147
    {
148 1447 bneuman
        printf("got TAG, waiting...");
149
        fflush(stdout);
150 1438 alevkoy
        // wait before sending ACK back
151 1447 bneuman
        //usleep(900000);
152
        delay8.tv_sec = 0;
153
        ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
154 1438 alevkoy
155 1447 bneuman
        while(ret) {
156
          printf(".");
157
          fflush(stdout);//      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
158
          ret = nanosleep(&rem, &rem);
159
        }
160
161
162 1438 alevkoy
        if (wl_basic_do_default(&data_length))
163 1447 bneuman
            printf("FAILED, got another TAG too soon\n");
164 1438 alevkoy
        else
165
        {
166
            // send packet
167
            send_buffer[0] = HUNTER_PREY_ACTION_ACK;
168
            send_buffer[1] = packet_data[1];
169
            wl_basic_send_global_packet(TYPE, send_buffer, 2);
170
171
            printf("PASSED\n");
172
        }
173
    }
174
175
    else
176
        printf("FAILED\n");
177
178 1447 bneuman
179 1453 bneuman
    printf("robot should be prey.\n Press Enter...\n");
180 1451 bneuman
    waitKey();
181
182 1447 bneuman
    printf("sending courtesy tag...");
183
    fflush(stdout);
184
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
185
    send_buffer[1] = ROBOTID;
186
    // robot number stays the same
187
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
188
    printf("done.\nwaiting for ack...");
189
    fflush(stdout);
190
191
    while (!(packet_data = wl_basic_do_default(&data_length)));
192
193
    if (!packet_data || data_length < 2 ||
194
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
195
            packet_data[1] != ROBOTID)
196
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
197
    else
198
        printf("done\n");
199
200
201 1438 alevkoy
    // Receive a TAG, never send an ACK
202
    printf("Receive TAG, never send ACK... ");
203 1442 alevkoy
    fflush(stdout);
204 1438 alevkoy
205
    while (!(packet_data = wl_basic_do_default(&data_length)));
206
207
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
208
    {
209 1447 bneuman
210
        printf("got TAG, monitoring for 5 seconds\n");
211
        fflush(stdout);
212 1453 bneuman
        delay8.tv_sec = 5;
213
        delay8.tv_nsec = 0;
214 1438 alevkoy
215 1453 bneuman
        ret = nanosleep(&delay8, &rem);  // wait for 5 secs
216
        while(ret) {
217
          //      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
218
          ret = nanosleep(&rem, &rem);
219
        }
220
221 1438 alevkoy
        if (wl_basic_do_default(&data_length))
222 1447 bneuman
            printf("FAILED, robot is spamming\n");
223 1438 alevkoy
        else
224
            printf("PASSED\n");
225
    }
226
227
    else
228
        printf("FAILED\n");
229
230 1453 bneuman
    printf("robot should be hunter.\nPress enter...\n");
231
232 1451 bneuman
    waitKey();
233
234 1438 alevkoy
    // Receive TAG, send ACK to incorrect robot
235
    printf("Receive TAG, send ACK to incorrect robot... ");
236 1442 alevkoy
    fflush(stdout);
237 1438 alevkoy
238
    // Wait until we receive a packet
239
    while (!(packet_data = wl_basic_do_default(&data_length)));
240
241
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
242
    {
243
        // send back an ACK
244
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
245
        send_buffer[1] = packet_data[1] + 1;
246
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
247
248
        printf("PASSED\n");
249
    }
250
    else
251
        printf("FAILED\n");
252
253 1453 bneuman
254
    printf("robot should wait and then still be hunter.\nPress Enter...\n");
255 1451 bneuman
    waitKey();
256
257 1438 alevkoy
    // Simulate TAG from robot A to B, ACK from robot B to A
258
    printf("Send TAG from robot A to B, ACK from B to A... ");
259 1442 alevkoy
    fflush(stdout);
260 1438 alevkoy
261
    // TAG
262
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
263
    send_buffer[1] = packet_data[1] - 1;    // robot other than testee
264
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
265
266
    // ACK
267
    send_buffer[0] = HUNTER_PREY_ACTION_ACK;
268
    // send_buffer[1] stays the same
269
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
270
271
    if (wl_basic_do_default(&data_length))
272
        printf("FAILED\n");
273
    else
274
        printf("PASSED\n");
275
276 1453 bneuman
    printf("robot should be waiting\n");
277
278 1438 alevkoy
    return 0;
279 1433 alevkoy
}