Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (7.47 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 1466 bneuman
    wl_set_com_port("/dev/ttyUSB0");
66 1434 alevkoy
    // set up wireless
67 1466 bneuman
    ret = wl_basic_init_default();
68 1434 alevkoy
69 1466 bneuman
    if(ret) {
70
      printf("ERROR: wl_basic_init_default returned %d\n", ret);
71
      return ret;
72
    }
73
74
    ret = wl_set_channel(channel);
75
76
    if(ret) {
77
      printf("ERROR: set channel failed! %d\n", ret);
78
      return ret;
79
    }
80
81 1434 alevkoy
    printf("Testing communications\n\n");
82
83
    // Receive TAG, send ACK
84
    printf("Receive TAG, send ACK... ");
85 1442 alevkoy
    fflush(stdout);
86 1434 alevkoy
    // Wait until we receive a packet
87
    while (!(packet_data = wl_basic_do_default(&data_length)));
88
89 1447 bneuman
    printf("got packet, validating and sending ack...");
90
    fflush(stdout);
91
92 1434 alevkoy
    if (data_length > 2)
93 1442 alevkoy
    {
94 1434 alevkoy
        printf("Excessive TAG packet length... ");
95 1442 alevkoy
        fflush(stdout);
96
    }
97 1434 alevkoy
98
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
99
    {
100
        // send back an ACK
101 1438 alevkoy
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
102 1434 alevkoy
        send_buffer[1] = packet_data[1];
103 1447 bneuman
        printf("sending ack intended for robot %d\n", packet_data[1]);
104
        fflush(stdout);
105 1434 alevkoy
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
106
107 1438 alevkoy
        printf("PASSED\n");
108 1434 alevkoy
    }
109
    else
110 1438 alevkoy
        printf("FAILED\n");
111
112 1453 bneuman
    printf("robot should be prey\nPress enter to continue...\n");
113 1451 bneuman
    waitKey();
114 1447 bneuman
115 1438 alevkoy
    // Send a TAG, receive an ACK
116
    printf("Send TAG, wait for ACK... ");
117 1442 alevkoy
    fflush(stdout);
118 1438 alevkoy
119
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
120
    send_buffer[1] = ROBOTID;
121
    // robot number stays the same
122
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
123
124
    // Wait for an ACK
125 1447 bneuman
    ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
126
127
    while(ret) {
128
      //      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
129
      ret = nanosleep(&rem, &rem);
130
    }
131
132
    data_length = -1;
133 1438 alevkoy
    packet_data = wl_basic_do_default(&data_length);
134
135
    // Check ACK for presence and correctness
136
    if (!packet_data || data_length < 2 ||
137
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
138 1451 bneuman
        packet_data[1] != ROBOTID) {
139 1447 bneuman
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
140 1451 bneuman
      if(data_length >= 2) {
141
        printf("\tpacket = [%c, %d]\n",packet_data[0], packet_data[1]);
142
      }
143
    }
144 1438 alevkoy
    else
145
        printf("PASSED\n");
146
147 1453 bneuman
    printf("Robot should be Hunter\nPress enter to continue...\n");
148
149 1451 bneuman
    waitKey();
150
151 1438 alevkoy
    // Receive a TAG, send a delayed ACK
152
    printf("Receive a TAG, send a delayed ACK... ");
153 1442 alevkoy
    fflush(stdout);
154 1438 alevkoy
155
    while (!(packet_data = wl_basic_do_default(&data_length)));
156
157
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
158
    {
159 1447 bneuman
        printf("got TAG, waiting...");
160
        fflush(stdout);
161 1438 alevkoy
        // wait before sending ACK back
162 1447 bneuman
        //usleep(900000);
163
        delay8.tv_sec = 0;
164
        ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
165 1438 alevkoy
166 1447 bneuman
        while(ret) {
167
          printf(".");
168
          fflush(stdout);//      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
169
          ret = nanosleep(&rem, &rem);
170
        }
171
172
173 1438 alevkoy
        if (wl_basic_do_default(&data_length))
174 1447 bneuman
            printf("FAILED, got another TAG too soon\n");
175 1438 alevkoy
        else
176
        {
177
            // send packet
178
            send_buffer[0] = HUNTER_PREY_ACTION_ACK;
179
            send_buffer[1] = packet_data[1];
180
            wl_basic_send_global_packet(TYPE, send_buffer, 2);
181
182
            printf("PASSED\n");
183
        }
184
    }
185
186
    else
187
        printf("FAILED\n");
188
189 1447 bneuman
190 1453 bneuman
    printf("robot should be prey.\n Press Enter...\n");
191 1451 bneuman
    waitKey();
192
193 1447 bneuman
    printf("sending courtesy tag...");
194
    fflush(stdout);
195
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
196
    send_buffer[1] = ROBOTID;
197
    // robot number stays the same
198
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
199
    printf("done.\nwaiting for ack...");
200
    fflush(stdout);
201
202
    while (!(packet_data = wl_basic_do_default(&data_length)));
203
204
    if (!packet_data || data_length < 2 ||
205
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
206
            packet_data[1] != ROBOTID)
207
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
208
    else
209
        printf("done\n");
210
211
212 1438 alevkoy
    // Receive a TAG, never send an ACK
213
    printf("Receive TAG, never send ACK... ");
214 1442 alevkoy
    fflush(stdout);
215 1438 alevkoy
216
    while (!(packet_data = wl_basic_do_default(&data_length)));
217
218
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
219
    {
220 1447 bneuman
221
        printf("got TAG, monitoring for 5 seconds\n");
222
        fflush(stdout);
223 1453 bneuman
        delay8.tv_sec = 5;
224
        delay8.tv_nsec = 0;
225 1438 alevkoy
226 1453 bneuman
        ret = nanosleep(&delay8, &rem);  // wait for 5 secs
227
        while(ret) {
228
          //      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
229
          ret = nanosleep(&rem, &rem);
230
        }
231
232 1438 alevkoy
        if (wl_basic_do_default(&data_length))
233 1447 bneuman
            printf("FAILED, robot is spamming\n");
234 1438 alevkoy
        else
235
            printf("PASSED\n");
236
    }
237
238
    else
239
        printf("FAILED\n");
240
241 1453 bneuman
    printf("robot should be hunter.\nPress enter...\n");
242
243 1451 bneuman
    waitKey();
244
245 1438 alevkoy
    // Receive TAG, send ACK to incorrect robot
246
    printf("Receive TAG, send ACK to incorrect robot... ");
247 1442 alevkoy
    fflush(stdout);
248 1438 alevkoy
249
    // Wait until we receive a packet
250
    while (!(packet_data = wl_basic_do_default(&data_length)));
251
252
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
253
    {
254
        // send back an ACK
255
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
256
        send_buffer[1] = packet_data[1] + 1;
257
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
258
259
        printf("PASSED\n");
260
    }
261
    else
262
        printf("FAILED\n");
263
264 1453 bneuman
265
    printf("robot should wait and then still be hunter.\nPress Enter...\n");
266 1451 bneuman
    waitKey();
267
268 1438 alevkoy
    // Simulate TAG from robot A to B, ACK from robot B to A
269
    printf("Send TAG from robot A to B, ACK from B to A... ");
270 1442 alevkoy
    fflush(stdout);
271 1438 alevkoy
272
    // TAG
273
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
274
    send_buffer[1] = packet_data[1] - 1;    // robot other than testee
275
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
276
277
    // ACK
278
    send_buffer[0] = HUNTER_PREY_ACTION_ACK;
279
    // send_buffer[1] stays the same
280
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
281
282
    if (wl_basic_do_default(&data_length))
283
        printf("FAILED\n");
284
    else
285
        printf("PASSED\n");
286
287 1453 bneuman
    printf("robot should be waiting\n");
288
289 1438 alevkoy
    return 0;
290 1433 alevkoy
}