Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.69 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 CHANNEL 12 //0xF // channel for wireless communication
30
#define TYPE 42        // packet type for wireless communication
31
#define ROBOTID 255 // make up a robot id because the PC doesn't have one
32

    
33

    
34
void waitKey() {
35
  unsigned char buf[10];
36

    
37
  //read from stdin
38
   while(read(0, buf, 10)==10) {
39
     printf("!");
40
     fflush(stdout);
41
   }
42

    
43
  while(getchar()==-1);
44
}
45

    
46
int main(int argc, char *argv[])
47
{
48
    char send_buffer[2];    // holds data to send
49
    int data_length;        // length of data received
50
    unsigned char *packet_data;        // data received
51
    int ret;
52

    
53
    struct timespec delay8, rem;
54

    
55
    delay8.tv_sec = 2;
56
    delay8.tv_nsec = 800000000;
57

    
58
    wl_set_com_port("/dev/ttyUSB0");
59
    // set up wireless
60
    wl_basic_init_default();
61
    wl_set_channel(CHANNEL);
62

    
63
    printf("Testing communications\n\n");
64

    
65
    // Receive TAG, send ACK
66
    printf("Receive TAG, send ACK... ");
67
    fflush(stdout);
68
    // Wait until we receive a packet
69
    while (!(packet_data = wl_basic_do_default(&data_length)));
70

    
71
    printf("got packet, validating and sending ack...");
72
    fflush(stdout);
73

    
74
    if (data_length > 2)
75
    {
76
        printf("Excessive TAG packet length... ");
77
        fflush(stdout);
78
    }
79

    
80
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
81
    {
82
        // send back an ACK
83
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
84
        send_buffer[1] = packet_data[1];
85
        printf("sending ack intended for robot %d\n", packet_data[1]);
86
        fflush(stdout);
87
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
88

    
89
        printf("PASSED\n");
90
    }
91
    else
92
        printf("FAILED\n");
93

    
94

    
95
    waitKey();
96

    
97
    // Send a TAG, receive an ACK
98
    printf("Send TAG, wait for ACK... ");
99
    fflush(stdout);
100

    
101
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
102
    send_buffer[1] = ROBOTID;
103
    // robot number stays the same
104
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
105

    
106
    // Wait for an ACK
107
    ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
108

    
109
    while(ret) {
110
      //      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
111
      ret = nanosleep(&rem, &rem);
112
    }
113

    
114
    data_length = -1;
115
    packet_data = wl_basic_do_default(&data_length);
116

    
117
    // Check ACK for presence and correctness
118
    if (!packet_data || data_length < 2 ||
119
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
120
        packet_data[1] != ROBOTID) {
121
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
122
      if(data_length >= 2) {
123
        printf("\tpacket = [%c, %d]\n",packet_data[0], packet_data[1]);
124
      }
125
    }
126
    else
127
        printf("PASSED\n");
128

    
129
    waitKey();
130

    
131
    // Receive a TAG, send a delayed ACK
132
    printf("Receive a TAG, send a delayed ACK... ");
133
    fflush(stdout);
134

    
135
    while (!(packet_data = wl_basic_do_default(&data_length)));
136

    
137
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
138
    {
139
        printf("got TAG, waiting...");
140
        fflush(stdout);
141
        // wait before sending ACK back
142
        //usleep(900000);
143
        delay8.tv_sec = 0;
144
        ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
145

    
146
        while(ret) {
147
          printf(".");
148
          fflush(stdout);//      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
149
          ret = nanosleep(&rem, &rem);
150
        }
151

    
152

    
153
        if (wl_basic_do_default(&data_length))
154
            printf("FAILED, got another TAG too soon\n");
155
        else
156
        {
157
            // send packet
158
            send_buffer[0] = HUNTER_PREY_ACTION_ACK;
159
            send_buffer[1] = packet_data[1];
160
            wl_basic_send_global_packet(TYPE, send_buffer, 2);
161

    
162
            printf("PASSED\n");
163
        }
164
    }
165

    
166
    else
167
        printf("FAILED\n");
168

    
169

    
170
    waitKey();
171

    
172
    printf("sending courtesy tag...");
173
    fflush(stdout);
174
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
175
    send_buffer[1] = ROBOTID;
176
    // robot number stays the same
177
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
178
    printf("done.\nwaiting for ack...");
179
    fflush(stdout);
180

    
181
    while (!(packet_data = wl_basic_do_default(&data_length)));
182

    
183
    if (!packet_data || data_length < 2 ||
184
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
185
            packet_data[1] != ROBOTID)
186
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
187
    else
188
        printf("done\n");
189

    
190
    waitKey();
191

    
192
    // Receive a TAG, never send an ACK
193
    printf("Receive TAG, never send ACK... ");
194
    fflush(stdout);
195

    
196
    while (!(packet_data = wl_basic_do_default(&data_length)));
197

    
198
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
199
    {
200

    
201
        printf("got TAG, monitoring for 5 seconds\n");
202
        fflush(stdout);
203
        usleep(5000000);    // wait 5 seconds to see if they TAG again
204

    
205
        if (wl_basic_do_default(&data_length))
206
            printf("FAILED, robot is spamming\n");
207
        else
208
            printf("PASSED\n");
209
    }
210

    
211
    else
212
        printf("FAILED\n");
213

    
214
    waitKey();
215

    
216
    // Receive TAG, send ACK to incorrect robot
217
    printf("Receive TAG, send ACK to incorrect robot... ");
218
    fflush(stdout);
219

    
220
    // Wait until we receive a packet
221
    while (!(packet_data = wl_basic_do_default(&data_length)));
222

    
223
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
224
    {
225
        // send back an ACK
226
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
227
        send_buffer[1] = packet_data[1] + 1;
228
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
229

    
230
        printf("PASSED\n");
231
    }
232
    else
233
        printf("FAILED\n");
234

    
235
    waitKey();
236

    
237
    // Simulate TAG from robot A to B, ACK from robot B to A
238
    printf("Send TAG from robot A to B, ACK from B to A... ");
239
    fflush(stdout);
240

    
241
    // TAG
242
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
243
    send_buffer[1] = packet_data[1] - 1;    // robot other than testee
244
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
245

    
246
    // ACK
247
    send_buffer[0] = HUNTER_PREY_ACTION_ACK;
248
    // send_buffer[1] stays the same
249
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
250

    
251
    if (wl_basic_do_default(&data_length))
252
        printf("FAILED\n");
253
    else
254
        printf("PASSED\n");
255

    
256
    return 0;
257
}
258