Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (4.82 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
28 1434 alevkoy
#define CHANNEL 0xF // channel for wireless communication
29
#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 1438 alevkoy
int main(int argc, char *argv[])
33 1433 alevkoy
{
34 1434 alevkoy
    char send_buffer[2];    // holds data to send
35
    int data_length;        // length of data received
36
    unsigned char *packet_data;        // data received
37 1433 alevkoy
38 1434 alevkoy
    // set up wireless
39
    wl_basic_init_default();
40
    wl_set_channel(CHANNEL);
41 1438 alevkoy
    wl_set_com_port("/dev/ttyUSB0");
42 1434 alevkoy
43
    printf("Testing communications\n\n");
44
45
    // Receive TAG, send ACK
46
    printf("Receive TAG, send ACK... ");
47 1442 alevkoy
    fflush(stdout);
48 1434 alevkoy
    // Wait until we receive a packet
49
    while (!(packet_data = wl_basic_do_default(&data_length)));
50
51
    if (data_length > 2)
52 1442 alevkoy
    {
53 1434 alevkoy
        printf("Excessive TAG packet length... ");
54 1442 alevkoy
        fflush(stdout);
55
    }
56 1434 alevkoy
57
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
58
    {
59
        // send back an ACK
60 1438 alevkoy
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
61 1434 alevkoy
        send_buffer[1] = packet_data[1];
62
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
63
64 1438 alevkoy
        printf("PASSED\n");
65 1434 alevkoy
    }
66
    else
67 1438 alevkoy
        printf("FAILED\n");
68
69
    // Send a TAG, receive an ACK
70
    printf("Send TAG, wait for ACK... ");
71 1442 alevkoy
    fflush(stdout);
72 1438 alevkoy
73
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
74
    send_buffer[1] = ROBOTID;
75
    // robot number stays the same
76
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
77
78
    // Wait for an ACK
79 1441 alevkoy
    usleep(800000);  // wait for 800 ms before sending ACK
80 1438 alevkoy
    packet_data = wl_basic_do_default(&data_length);
81
82
    // Check ACK for presence and correctness
83
    if (!packet_data || data_length < 2 ||
84
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
85
            packet_data[1] != ROBOTID)
86
        printf("FAILED\n");
87
    else
88
        printf("PASSED\n");
89
90
    // Receive a TAG, send a delayed ACK
91
    printf("Receive a TAG, send a delayed ACK... ");
92 1442 alevkoy
    fflush(stdout);
93 1438 alevkoy
94
    while (!(packet_data = wl_basic_do_default(&data_length)));
95
96
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
97
    {
98
        // wait before sending ACK back
99 1441 alevkoy
        usleep(900000);
100 1438 alevkoy
101
        if (wl_basic_do_default(&data_length))
102
            printf("FAILED\n");
103
        else
104
        {
105
            // send packet
106
            send_buffer[0] = HUNTER_PREY_ACTION_ACK;
107
            send_buffer[1] = packet_data[1];
108
            wl_basic_send_global_packet(TYPE, send_buffer, 2);
109
110
            printf("PASSED\n");
111
        }
112
    }
113
114
    else
115
        printf("FAILED\n");
116
117
    // Receive a TAG, never send an ACK
118
    printf("Receive TAG, never send ACK... ");
119 1442 alevkoy
    fflush(stdout);
120 1438 alevkoy
121
    while (!(packet_data = wl_basic_do_default(&data_length)));
122
123
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
124
    {
125 1441 alevkoy
        usleep(5000000);    // wait 5 seconds to see if they TAG again
126 1438 alevkoy
127
        if (wl_basic_do_default(&data_length))
128
            printf("FAILED\n");
129
        else
130
            printf("PASSED\n");
131
    }
132
133
    else
134
        printf("FAILED\n");
135
136
    // Receive TAG, send ACK to incorrect robot
137
    printf("Receive TAG, send ACK to incorrect robot... ");
138 1442 alevkoy
    fflush(stdout);
139 1438 alevkoy
140
    // Wait until we receive a packet
141
    while (!(packet_data = wl_basic_do_default(&data_length)));
142
143
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
144
    {
145
        // send back an ACK
146
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
147
        send_buffer[1] = packet_data[1] + 1;
148
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
149
150
        printf("PASSED\n");
151
    }
152
    else
153
        printf("FAILED\n");
154
155
    // Simulate TAG from robot A to B, ACK from robot B to A
156
    printf("Send TAG from robot A to B, ACK from B to A... ");
157 1442 alevkoy
    fflush(stdout);
158 1438 alevkoy
159
    // TAG
160
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
161
    send_buffer[1] = packet_data[1] - 1;    // robot other than testee
162
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
163
164
    // ACK
165
    send_buffer[0] = HUNTER_PREY_ACTION_ACK;
166
    // send_buffer[1] stays the same
167
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
168
169
    if (wl_basic_do_default(&data_length))
170
        printf("FAILED\n");
171
    else
172
        printf("PASSED\n");
173
174
    return 0;
175 1433 alevkoy
}