Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / libwireless / logger / wl_token_logger.c @ 880

History | View | Annotate | Download (2.56 KB)

1
#include <wl_token_ring.h>
2

    
3
#include <stdlib.h>
4
#include <stdio.h>
5

    
6
#include <wl_defs.h>
7
#include <wireless.h>
8

    
9
#include <time.h>
10
#include <sys/timeb.h>
11

    
12
/*Function Prototypes*/
13
/*Wireless Library Prototypes*/
14
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet,
15
                                                        int length);
16
void timeout_handler(void);
17
PacketGroupHandler wl_token_ring_handler =
18
                {WL_TOKEN_RING_GROUP, timeout_handler,
19
                NULL, wl_token_ring_receive_handler,
20
                NULL};
21

    
22
/* Global Variables */
23

    
24
time_t startTime, endTime;
25

    
26
void timeout_handler()
27
{
28
        if (time(NULL) > endTime)
29
                exit(0);
30
}
31

    
32
/**
33
 * Initialize the token ring packet group and register it with the
34
 * wireless library. The robot will not join a token ring.
35
 **/
36
void wl_token_logger_register(int seconds)
37
{
38
        if (wl_get_xbee_id() > 0xFF)
39
        {
40
                //Note: if this becomes an issue (unlikely), we could limit sensor information
41
                //to half a byte and use 12 bits for the id
42
                WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is ");
43
                WL_DEBUG_PRINT_INT(wl_get_xbee_id());
44
                WL_DEBUG_PRINT(".\r\n");
45
                return;
46
        }
47

    
48
        startTime = time(NULL);
49
        endTime = startTime + seconds;
50

    
51
        wl_register_packet_group(&wl_token_ring_handler);
52
}
53

    
54
/**
55
 * Removes the packet group from the wireless library.
56
 **/
57
void wl_token_logger_unregister()
58
{
59
        wl_unregister_packet_group(&wl_token_ring_handler);
60
}
61

    
62
/**
63
 * Called when we recieve a token ring packet.
64
 * @param type the type of the packet
65
 * @param source the id of the robot who sent the packet
66
 * @param packet the data in the packet
67
 * @param length the length of the packet in bytes
68
 **/
69
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet,
70
                                                        int length)
71
{
72
        struct timeb t;
73
        if (ftime(&t))
74
                exit(0);
75
        time_t seconds = t.time;
76
        int millis = t.millitm;
77
        if (seconds >= endTime)
78
                exit(0);
79
        int temp = (int)(seconds - startTime);
80
        printf("%d:%.2d.%.3d: ", temp / 60, temp % 60, millis);
81

    
82
        switch (type)
83
        {
84
                case WL_TOKEN_PASS:
85
                        printf("We were passed the token from robot %d.\n", source);
86
                        printf("This should not have happened.\n");
87
                        break;
88
                case WL_TOKEN_SENSOR_MATRIX:
89
                        printf("Sensor Matrix from %d:\n  ", source);
90
                        int i;
91
                        for (i = 0; i < length; i+=2)
92
                                printf("(%d %d)", packet[i], packet[i+1]);
93
                        printf("\n");
94
                        break;
95
                case WL_TOKEN_BOM_ON:
96
                        printf("Bom On from %d.\n", source);
97
                        break;
98
                case WL_TOKEN_JOIN:
99
                        printf("Join Request from %d.\n", source);
100
                        break;
101
                case WL_TOKEN_JOIN_ACCEPT:
102
                        printf("Join accept should be a robot to robot packet.\n");
103
                        break;
104
                default:
105
                        printf("Unimplemented token ring packet received.\r\n");
106
                        break;
107
        }
108
}
109