root / trunk / code / projects / libwireless / logger / wl_token_logger.c @ 1439
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:\n", 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 |
break;
|
87 |
case WL_TOKEN_SENSOR_MATRIX:
|
88 |
printf("Robot %d sent sensor matrix:\n", source);
|
89 |
int i;
|
90 |
for (i = 0; i < length; i+=2) |
91 |
printf("(%d %d)", packet[i], packet[i+1]); |
92 |
printf("\n");
|
93 |
break;
|
94 |
case WL_TOKEN_BOM_ON:
|
95 |
printf("Robot %d has turned its BOM on.\n", source);
|
96 |
break;
|
97 |
case WL_TOKEN_JOIN:
|
98 |
printf("Robot %d has requested to join the token ring.\n", source);
|
99 |
break;
|
100 |
case WL_TOKEN_JOIN_ACCEPT:
|
101 |
printf("Join accept should be a robot to robot packet.\n");
|
102 |
break;
|
103 |
default:
|
104 |
printf("Unimplemented token ring packet received.\r\n");
|
105 |
break;
|
106 |
} |
107 |
} |
108 |
|