Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (7.47 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 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

    
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
int main(int argc, char *argv[])
46
{
47
    char send_buffer[2];    // holds data to send
48
    int data_length;        // length of data received
49
    unsigned char *packet_data;        // data received
50
    int ret;
51

    
52
    unsigned int channel = 0xF;
53

    
54
    struct timespec delay8, rem;
55

    
56
    if(argc > 1) {
57
      channel = atoi(argv[1]);
58
    }
59

    
60
    printf("using wireless channel %d\n", channel);
61

    
62
    delay8.tv_sec = 2;
63
    delay8.tv_nsec = 800000000;
64

    
65
    wl_set_com_port("/dev/ttyUSB0");
66
    // set up wireless
67
    ret = wl_basic_init_default();
68

    
69
    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
    printf("Testing communications\n\n");
82

    
83
    // Receive TAG, send ACK
84
    printf("Receive TAG, send ACK... ");
85
    fflush(stdout);
86
    // Wait until we receive a packet
87
    while (!(packet_data = wl_basic_do_default(&data_length)));
88

    
89
    printf("got packet, validating and sending ack...");
90
    fflush(stdout);
91

    
92
    if (data_length > 2)
93
    {
94
        printf("Excessive TAG packet length... ");
95
        fflush(stdout);
96
    }
97

    
98
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
99
    {
100
        // send back an ACK
101
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
102
        send_buffer[1] = packet_data[1];
103
        printf("sending ack intended for robot %d\n", packet_data[1]);
104
        fflush(stdout);
105
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
106

    
107
        printf("PASSED\n");
108
    }
109
    else
110
        printf("FAILED\n");
111

    
112
    printf("robot should be prey\nPress enter to continue...\n");
113
    waitKey();
114

    
115
    // Send a TAG, receive an ACK
116
    printf("Send TAG, wait for ACK... ");
117
    fflush(stdout);
118

    
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
    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
    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
        packet_data[1] != ROBOTID) {
139
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
140
      if(data_length >= 2) {
141
        printf("\tpacket = [%c, %d]\n",packet_data[0], packet_data[1]);
142
      }
143
    }
144
    else
145
        printf("PASSED\n");
146

    
147
    printf("Robot should be Hunter\nPress enter to continue...\n");
148

    
149
    waitKey();
150

    
151
    // Receive a TAG, send a delayed ACK
152
    printf("Receive a TAG, send a delayed ACK... ");
153
    fflush(stdout);
154

    
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
        printf("got TAG, waiting...");
160
        fflush(stdout);
161
        // wait before sending ACK back
162
        //usleep(900000);
163
        delay8.tv_sec = 0;
164
        ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
165

    
166
        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
        if (wl_basic_do_default(&data_length))
174
            printf("FAILED, got another TAG too soon\n");
175
        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

    
190
    printf("robot should be prey.\n Press Enter...\n");
191
    waitKey();
192

    
193
    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
    // Receive a TAG, never send an ACK
213
    printf("Receive TAG, never send ACK... ");
214
    fflush(stdout);
215

    
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

    
221
        printf("got TAG, monitoring for 5 seconds\n");
222
        fflush(stdout);
223
        delay8.tv_sec = 5;
224
        delay8.tv_nsec = 0;
225

    
226
        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
        if (wl_basic_do_default(&data_length))
233
            printf("FAILED, robot is spamming\n");
234
        else
235
            printf("PASSED\n");
236
    }
237

    
238
    else
239
        printf("FAILED\n");
240

    
241
    printf("robot should be hunter.\nPress enter...\n");
242

    
243
    waitKey();
244

    
245
    // Receive TAG, send ACK to incorrect robot
246
    printf("Receive TAG, send ACK to incorrect robot... ");
247
    fflush(stdout);
248

    
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

    
265
    printf("robot should wait and then still be hunter.\nPress Enter...\n");
266
    waitKey();
267

    
268
    // 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
    fflush(stdout);
271

    
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
    printf("robot should be waiting\n");
288

    
289
    return 0;
290
}
291