Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / logger / wl_token_logger.c @ 744

History | View | Annotate | Download (2.57 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_logger_receive_handler(char type, int source, unsigned char* packet,
15
                                                        int length);
16
void timeout_handler(void);
17
PacketGroupHandler wl_logger_handler =
18
                {WL_TOKEN_RING_GROUP, timeout_handler,
19
                NULL, wl_logger_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_logger_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_logger_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_logger_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
                        if (length < 1)
86
                        {
87
                                WL_DEBUG_PRINT("Malformed Token Pass packet received.\r\n");
88
                                return;
89
                        }
90
                        printf("Robot %d passed the token to robot %d. Sensor matrix:\n", source, packet[0]);
91
                        int i;
92
                        for (i = 1; i < length; i+=2)
93
                                printf("(%d %d)", packet[i], packet[i+1]);
94
                        printf("\n");
95
                        break;
96
                case WL_TOKEN_BOM_ON:
97
                        printf("Robot %d has turned its BOM on.\n", source);
98
                        break;
99
                case WL_TOKEN_JOIN:
100
                        printf("Robot %d has requested to join the token ring.\n", source);
101
                        break;
102
                case WL_TOKEN_JOIN_ACCEPT:
103
                        printf("Join accept should be a robot to robot packet.\n");
104
                        break;
105
                default:
106
                        printf("Unimplemented token ring packet received.\r\n");
107
                        break;
108
        }
109
}
110