Revision 418
added return values to a bunch of libwireless functions. Makefile for colonetserver now compiles libwireless when necessary. added static to a bunch of libwireless vars. commented out colonet timeout
wl_token_ring.c | ||
---|---|---|
64 | 64 |
/*Function Prototypes*/ |
65 | 65 |
|
66 | 66 |
/*Wireless Library Prototypes*/ |
67 |
void wl_token_ring_timeout_handler(void); |
|
68 |
void wl_token_ring_response_handler(int frame, int received); |
|
69 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
|
70 |
int length); |
|
71 |
void wl_token_ring_cleanup(void); |
|
67 |
static void wl_token_ring_timeout_handler(void); |
|
68 |
static void wl_token_ring_response_handler(int frame, int received); |
|
69 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length); |
|
70 |
static void wl_token_ring_cleanup(void); |
|
72 | 71 |
|
73 | 72 |
/*Helper Functions*/ |
74 |
void wl_token_pass_token(void); |
|
75 |
int get_token_distance(int robot1, int robot2); |
|
76 |
void wl_token_get_token(void); |
|
73 |
static void wl_token_pass_token(void);
|
|
74 |
static int get_token_distance(int robot1, int robot2);
|
|
75 |
static void wl_token_get_token(void);
|
|
77 | 76 |
|
78 | 77 |
/*Packet Handling Routines*/ |
79 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength); |
|
80 |
void wl_token_bom_on_receive(int source); |
|
81 |
void wl_token_join_receive(int source); |
|
82 |
void wl_token_join_accept_receive(int source); |
|
78 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength);
|
|
79 |
static void wl_token_bom_on_receive(int source);
|
|
80 |
static void wl_token_join_receive(int source);
|
|
81 |
static void wl_token_join_accept_receive(int source);
|
|
83 | 82 |
|
84 | 83 |
/*Global Variables*/ |
85 | 84 |
|
86 | 85 |
//the sensor matrix |
87 |
SensorMatrix* sensorMatrix; |
|
86 |
static SensorMatrix* sensorMatrix;
|
|
88 | 87 |
|
89 | 88 |
//the robot we are waiting to say it has received the token. -1 if unspecified |
90 |
int wl_token_next_robot = -1; |
|
89 |
static int wl_token_next_robot = -1;
|
|
91 | 90 |
|
92 | 91 |
//true if the robot should be in the token ring, 0 otherwise |
93 |
int ringState = NONMEMBER; |
|
92 |
static int ringState = NONMEMBER;
|
|
94 | 93 |
//the id of the robot who accepted us into the token ring, only used in ACCEPTED state |
95 |
int acceptor = -1; |
|
94 |
static int acceptor = -1;
|
|
96 | 95 |
//id of the robot we are accepting |
97 |
int accepted = -1; |
|
96 |
static int accepted = -1;
|
|
98 | 97 |
|
99 | 98 |
//the counter for when we assume a robot is dead |
100 |
int deathDelay = -1; |
|
99 |
static int deathDelay = -1;
|
|
101 | 100 |
//the counter for joining, before we form our own token ring |
102 |
int joinDelay = -1; |
|
101 |
static int joinDelay = -1;
|
|
103 | 102 |
|
104 | 103 |
//current robot to check in the iterator |
105 |
int iteratorCount = 0; |
|
104 |
static int iteratorCount = 0;
|
|
106 | 105 |
|
107 | 106 |
// the amount of time a robot has had its BOM on for |
108 |
int bom_on_count = 0; |
|
107 |
static int bom_on_count = 0;
|
|
109 | 108 |
|
110 |
void do_nothing(void) {} |
|
111 |
int get_nothing(void) {return -1;} |
|
109 |
static void do_nothing(void) {}
|
|
110 |
static int get_nothing(void) {return -1;}
|
|
112 | 111 |
|
113 | 112 |
#ifdef ROBOT |
114 | 113 |
#ifndef FIREFLY |
115 |
void (*bom_on_function) (void) = bom_on; |
|
116 |
void (*bom_off_function) (void) = bom_off; |
|
117 |
int (*get_max_bom_function) (void) = get_max_bom; |
|
114 |
static void (*bom_on_function) (void) = bom_on;
|
|
115 |
static void (*bom_off_function) (void) = bom_off;
|
|
116 |
static int (*get_max_bom_function) (void) = get_max_bom;
|
|
118 | 117 |
#else |
119 |
void (*bom_on_function) (void) = do_nothing; |
|
120 |
void (*bom_off_function) (void) = do_nothing; |
|
121 |
int (*get_max_bom_function) (void) = get_nothing; |
|
118 |
static void (*bom_on_function) (void) = do_nothing;
|
|
119 |
static void (*bom_off_function) (void) = do_nothing;
|
|
120 |
static int (*get_max_bom_function) (void) = get_nothing;
|
|
122 | 121 |
#endif |
123 | 122 |
#else |
124 |
void (*bom_on_function) (void) = do_nothing; |
|
125 |
void (*bom_off_function) (void) = do_nothing; |
|
126 |
int (*get_max_bom_function) (void) = get_nothing; |
|
123 |
static void (*bom_on_function) (void) = do_nothing;
|
|
124 |
static void (*bom_off_function) (void) = do_nothing;
|
|
125 |
static int (*get_max_bom_function) (void) = get_nothing;
|
|
127 | 126 |
#endif |
128 | 127 |
|
129 |
PacketGroupHandler wl_token_ring_handler = |
|
130 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler,
|
|
128 |
static PacketGroupHandler wl_token_ring_handler =
|
|
129 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler, |
|
131 | 130 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
132 | 131 |
wl_token_ring_cleanup}; |
133 | 132 |
|
... | ... | |
135 | 134 |
* Initialize the token ring packet group and register it with the |
136 | 135 |
* wireless library. The robot will not join a token ring. |
137 | 136 |
**/ |
138 |
void wl_token_ring_register()
|
|
137 |
int wl_token_ring_register()
|
|
139 | 138 |
{ |
140 | 139 |
if (wl_get_xbee_id() > 0xFF) |
141 | 140 |
{ |
... | ... | |
144 | 143 |
WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is "); |
145 | 144 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
146 | 145 |
WL_DEBUG_PRINT(".\r\n"); |
147 |
return; |
|
146 |
return -1;
|
|
148 | 147 |
} |
149 | 148 |
|
150 | 149 |
sensorMatrix = sensor_matrix_create(); |
... | ... | |
152 | 151 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
153 | 152 |
|
154 | 153 |
wl_register_packet_group(&wl_token_ring_handler); |
154 |
|
|
155 |
return 0; |
|
155 | 156 |
} |
156 | 157 |
|
157 | 158 |
/** |
... | ... | |
193 | 194 |
/** |
194 | 195 |
* Called approximately every quarter second by the wireless library. |
195 | 196 |
**/ |
196 |
void wl_token_ring_timeout_handler() |
|
197 |
static void wl_token_ring_timeout_handler()
|
|
197 | 198 |
{ |
198 | 199 |
//someone is not responding, assume they are dead |
199 | 200 |
if (deathDelay == 0) |
... | ... | |
251 | 252 |
* @param frame the frame number assigned when the packet was sent |
252 | 253 |
* @param received 1 if the packet was received, 0 otherwise |
253 | 254 |
**/ |
254 |
void wl_token_ring_response_handler(int frame, int received) |
|
255 |
static void wl_token_ring_response_handler(int frame, int received)
|
|
255 | 256 |
{ |
256 | 257 |
if (!received) |
257 | 258 |
{ |
... | ... | |
266 | 267 |
* @param packet the data in the packet |
267 | 268 |
* @param length the length of the packet in bytes |
268 | 269 |
**/ |
269 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
|
270 |
int length) |
|
270 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length) |
|
271 | 271 |
{ |
272 | 272 |
switch (type) |
273 | 273 |
{ |
... | ... | |
334 | 334 |
int wl_token_get_sensor_reading(int source, int dest) |
335 | 335 |
{ |
336 | 336 |
if (wl_token_is_robot_in_ring(dest) && |
337 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) |
|
337 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) {
|
|
338 | 338 |
return sensor_matrix_get_reading(sensorMatrix, source, dest); |
339 |
} |
|
340 |
|
|
339 | 341 |
return -1; |
340 | 342 |
} |
341 | 343 |
|
... | ... | |
360 | 362 |
* reading for that robot, repeated for sensorDataLength bytes |
361 | 363 |
* @param sensorDataLength the length in bytes of sensorData |
362 | 364 |
*/ |
363 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength) |
|
365 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength)
|
|
364 | 366 |
{ |
365 | 367 |
int i, j; |
366 | 368 |
|
... | ... | |
705 | 707 |
void wl_token_iterator_begin(void) |
706 | 708 |
{ |
707 | 709 |
int i = 0; |
708 |
iteratorCount = 0; |
|
709 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && |
|
710 |
i < sensor_matrix_get_size(sensorMatrix)) |
|
710 |
|
|
711 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) { |
|
711 | 712 |
i++; |
712 |
if (i == sensor_matrix_get_size(sensorMatrix)) |
|
713 |
} |
|
714 |
|
|
715 |
if (i == sensor_matrix_get_size(sensorMatrix)) { |
|
713 | 716 |
i = -1; |
717 |
} |
|
718 |
|
|
714 | 719 |
iteratorCount = i; |
715 | 720 |
} |
716 | 721 |
|
... | ... | |
738 | 743 |
int wl_token_iterator_next(void) |
739 | 744 |
{ |
740 | 745 |
int result = iteratorCount; |
741 |
if (result < 0) |
|
746 |
if (result < 0) {
|
|
742 | 747 |
return result; |
748 |
} |
|
743 | 749 |
|
744 | 750 |
iteratorCount++; |
745 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) &&
|
|
746 |
iteratorCount < sensor_matrix_get_size(sensorMatrix))
|
|
751 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) |
|
752 |
&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) {
|
|
747 | 753 |
iteratorCount++; |
748 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) |
|
754 |
} |
|
755 |
|
|
756 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) { |
|
749 | 757 |
iteratorCount = -1; |
758 |
} |
|
759 |
|
|
750 | 760 |
return result; |
751 | 761 |
} |
752 | 762 |
|
Also available in: Unified diff