Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / autonomous_recharging / charging_station / wl_token_ring.c @ 337

History | View | Annotate | Download (20.1 KB)

1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26
/**
27
 * @file wl_token_ring.c
28
 * @brief Token Ring Implementation
29
 *
30
 * Implementation of the token ring packet group.
31
 *
32
 * @author Brian Coltin, Colony Project, CMU Robotics Club
33
 **/
34

    
35
#include <wl_token_ring.h>
36

    
37
#include <stdlib.h>
38
#include <stdio.h>
39

    
40
#include <wl_defs.h>
41
#include <wireless.h>
42
#include <sensor_matrix.h>
43

    
44
#ifdef ROBOT
45
#ifndef FIREFLY
46
#include <bom.h>
47
#endif
48
#include <time.h>
49
#endif
50

    
51
#define DEFAULT_SENSOR_MATRIX_SIZE 20
52

    
53
/*Ring States*/
54

    
55
#define NONMEMBER 0
56
#define MEMBER 1
57
#define JOINING 2
58
#define ACCEPTED 3
59
#define LEAVING 4
60

    
61
/*Frame Types*/
62
#define TOKEN_JOIN_ACCEPT_FRAME 1
63

    
64
/*Function Prototypes*/
65

    
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);
72

    
73
/*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);
77

    
78
/*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);
83

    
84
/*Global Variables*/
85

    
86
//the sensor matrix
87
SensorMatrix* sensorMatrix;
88

    
89
//the robot we are waiting to say it has received the token. -1 if unspecified
90
int wl_token_next_robot = -1;
91

    
92
//true if the robot should be in the token ring, 0 otherwise
93
int ringState = NONMEMBER;
94
//the id of the robot who accepted us into the token ring, only used in ACCEPTED state
95
int acceptor = -1;
96
//id of the robot we are accepting
97
int accepted = -1;
98

    
99
//the counter for when we assume a robot is dead
100
int deathDelay = -1;
101
//the counter for joining, before we form our own token ring
102
int joinDelay = -1;
103

    
104
//current robot to check in the iterator
105
int iteratorCount = 0;
106

    
107
// the amount of time a robot has had its BOM on for
108
int bom_on_count = 0;
109

    
110
void do_nothing(void) {}
111
int get_nothing(void) {return -1;}
112

    
113
#ifdef ROBOT
114
#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;
118
#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;
122
#endif
123
#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;
127
#endif
128

    
129
PacketGroupHandler wl_token_ring_handler =
130
                {WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler,
131
                wl_token_ring_response_handler, wl_token_ring_receive_handler,
132
                wl_token_ring_cleanup};
133

    
134
/**
135
 * Initialize the token ring packet group and register it with the
136
 * wireless library. The robot will not join a token ring.
137
 **/
138
void wl_token_ring_register()
139
{
140
        if (wl_get_xbee_id() > 0xFF)
141
        {
142
                //Note: if this becomes an issue (unlikely), we could limit sensor information
143
                //to half a byte and use 12 bits for the id
144
                WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is ");
145
                WL_DEBUG_PRINT_INT(wl_get_xbee_id());
146
                WL_DEBUG_PRINT(".\r\n");
147
                return;
148
        }
149
        
150
        sensorMatrix = sensor_matrix_create();
151
        //add ourselves to the sensor matrix
152
        sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
153

    
154
        wl_register_packet_group(&wl_token_ring_handler);
155
}
156

    
157
/**
158
 * Removes the packet group from the wireless library.
159
 **/
160
void wl_token_ring_unregister()
161
{
162
        wl_unregister_packet_group(&wl_token_ring_handler);
163
}
164

    
165
/**
166
 * Sets the functions that are called when the BOM ought to be
167
 * turned on or off. This could be used for things such as 
168
 * charging stations, which have multiple BOMs.
169
 *
170
 * @param on_function the function to be called when the BOM
171
 * should be turned on
172
 * @param off_function the function to be called when the BOM
173
 * should be turned off
174
 * @param max_bom_function the function to be called when a
175
 * measurement of the maximum BOM reading is needed.
176
 **/
177
void wl_token_ring_set_bom_functions(void (*on_function) (void),
178
        void (*off_function) (void), int (*max_bom_function) (void))
179
{
180
        bom_on_function = on_function;
181
        bom_off_function = off_function;
182
        get_max_bom_function = max_bom_function;
183
}
184

    
185
/**
186
 * Called to cleanup the token ring packet group.
187
 **/
188
void wl_token_ring_cleanup()
189
{
190
        sensor_matrix_destroy(sensorMatrix);
191
}
192

    
193
/**
194
 * Called approximately every quarter second by the wireless library.
195
 **/
196
void wl_token_ring_timeout_handler()
197
{
198
        //someone is not responding, assume they are dead
199
        if (deathDelay == 0)
200
        {
201
                //pass the token to the next robot if we think someone has died
202
                //also, declare that person dead, as long as it isn't us
203
                if (wl_token_next_robot != wl_get_xbee_id())
204
                {
205
                        sensor_matrix_set_in_ring(sensorMatrix, wl_token_next_robot, 0);
206
                        WL_DEBUG_PRINT("Robot ");
207
                        WL_DEBUG_PRINT_INT(wl_token_next_robot);
208
                        WL_DEBUG_PRINT(" has died.\r\n");
209
                        wl_token_next_robot = -1;
210
                        deathDelay = DEATH_DELAY;
211
                }
212
                
213
                // we may have been dropped from the ring when this is received
214
                if (ringState == MEMBER)
215
                        wl_token_pass_token();
216
        }
217

    
218
        //we must start our own token ring, no one is responding to us
219
        if (joinDelay == 0)
220
        {
221
                if (sensor_matrix_get_joined(sensorMatrix) == 0)
222
                {
223
                        WL_DEBUG_PRINT("Creating our own token ring, no robots seem to exist.\r\n");
224
                        sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
225
                        ringState = MEMBER;
226
                        //this will make us pass the token to ourself
227
                        //repeatedly, and other robots when they join
228
                        deathDelay = DEATH_DELAY;
229
                        wl_token_next_robot = wl_get_xbee_id();
230
                }
231
                else
232
                {
233
                        WL_DEBUG_PRINT("Attempting to join the token ring again.\r\n");
234
                        //attempt to rejoin with a random delay
235
                        wl_token_ring_join();
236
                        joinDelay = rand() / (RAND_MAX / JOIN_DELAY) + 1;
237
                }
238
        }
239

    
240
        if (deathDelay >= 0)
241
                deathDelay--;
242
        if (joinDelay >= 0)
243
                joinDelay--;
244
        if (bom_on_count >= 0)
245
                bom_on_count++;
246
}
247

    
248
/**
249
 * Called when the XBee tells us if a packet we sent has been received.
250
 * 
251
 * @param frame the frame number assigned when the packet was sent
252
 * @param received 1 if the packet was received, 0 otherwise
253
 **/
254
void wl_token_ring_response_handler(int frame, int received)
255
{
256
        if (!received)
257
        {
258
                WL_DEBUG_PRINT("FAILED.\r\n");
259
        }
260
}
261

    
262
/**
263
 * Called when we recieve a token ring packet.
264
 * @param type the type of the packet
265
 * @param source the id of the robot who sent the packet
266
 * @param packet the data in the packet
267
 * @param length the length of the packet in bytes
268
 **/
269
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet,
270
                                                        int length)
271
{
272
        switch (type)
273
        {
274
                case WL_TOKEN_PASS:
275
                        if (length < 1)
276
                        {
277
                                WL_DEBUG_PRINT("Malformed Token Pass packet received.\r\n");
278
                                return;
279
                        }
280
                        wl_token_pass_receive(source, packet[0], packet + 1, length - 1);
281
                        break;
282
                case WL_TOKEN_BOM_ON:
283
                        //add the robot to the sensor matrix if it is not already there
284
                        wl_token_bom_on_receive(source);
285
                        break;
286
                case WL_TOKEN_JOIN:
287
                        wl_token_join_receive(source);
288
                        break;
289
                case WL_TOKEN_JOIN_ACCEPT:
290
                        wl_token_join_accept_receive(source);
291
                        break;
292
                default:
293
                        WL_DEBUG_PRINT("Unimplemented token ring packet received.\r\n");
294
                        break;
295
        }
296
}
297

    
298
/**
299
 * Causes the robot to join an existing token ring, or create one
300
 * if no token ring exists. The token ring uses global and robot to robot
301
 * packets, and does not rely on any PAN.
302
 **/
303
void wl_token_ring_join()
304
{
305
        WL_DEBUG_PRINT("Joining the token ring.\r\n");
306
        ringState = JOINING;
307
        joinDelay = DEATH_DELAY * 2;
308
        wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN,
309
                NULL, 0, 0);
310
}
311

    
312
/**
313
 * Causes the robot to leave the token ring. The robot stops
314
 * alerting others of its location, but continues storing the
315
 * locations of other robots.
316
 **/
317
void wl_token_ring_leave()
318
{
319
        ringState = LEAVING;
320
}
321

    
322
/**
323
 * Returns the BOM reading robot source has for robot dest.
324
 *
325
 * @param source the robot that made the BOM reading
326
 * @param dest the robot whose relative location is returned
327
 *
328
 * @return a BOM reading from robot source to robot dest,
329
 * in the range 0-15, or -1 if it is unknown
330
 **/
331
int wl_token_get_sensor_reading(int source, int dest)
332
{
333
        if (wl_token_is_robot_in_ring(dest) &&
334
                        (source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source)))
335
                return sensor_matrix_get_reading(sensorMatrix, source, dest);
336
        return -1;
337
}
338

    
339
/**
340
 * Returns the BOM reading we have for robot dest.
341
 * 
342
 * @param dest the robot whose relative location is returned
343
 *
344
 * @return a BOM reading from us to robot dest, in the range
345
 * 0-15, or -1 if it is unkown
346
 **/
347
int wl_token_get_my_sensor_reading(int dest)
348
{
349
        return wl_token_get_sensor_reading(wl_get_xbee_id(), dest);
350
}
351

    
352
/**
353
 * This method is called when we receive a token pass packet.
354
 * @param source is the robot it came from
355
 * @param nextRobot is the robot the token was passed to
356
 * @param sensorData a char with an id followed by a char with the sensor
357
 *                reading for that robot, repeated for sensorDataLength bytes
358
 * @param sensorDataLength the length in bytes of sensorData
359
 */
360
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength)
361
{
362
        int i, j;
363

    
364
        // this prevents two tokens from being passed around at a time (second clause is in case we are joining)
365
        if (source != wl_token_next_robot && bom_on_count <= DEATH_DELAY / 2 &&
366
                ringState != ACCEPTED)
367
        {
368
                WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n");
369
                WL_DEBUG_PRINT("There are probably two tokens going around, packet ignored.\n");
370
                return;
371
        }
372

    
373
        bom_on_count = -1;
374
        deathDelay = -1;
375
        WL_DEBUG_PRINT("Received the token from robot");
376
        WL_DEBUG_PRINT_INT(source);
377
        WL_DEBUG_PRINT(", next robot is ");
378
        WL_DEBUG_PRINT_INT((int)nextRobot);
379
        WL_DEBUG_PRINT(" \r\n");
380
        sensor_matrix_set_in_ring(sensorMatrix, source, 1);
381

    
382
        //with this packet, we are passed the id of the next robot in the ring
383
        //and the sensor matrix, a list of id and sensor reading pairs (two bytes for both)
384
        j = 0;
385
        for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++)
386
        {
387
                if (i == source)
388
                        continue;
389
                
390
                //set the sensor information we receive
391
                if (j < sensorDataLength / 2 && sensorData[2 * j] == i)
392
                {
393
                        //the robot we were going to accept has already been accepted
394
                        if (accepted == i)
395
                        {
396
                                accepted = -1;
397
                                WL_DEBUG_PRINT("Someone accepted the robot we did.\r\n");
398
                        }
399
                        sensor_matrix_set_reading(sensorMatrix, source, i,
400
                                                sensorData[2 * j + 1]);
401
                        sensor_matrix_set_in_ring(sensorMatrix, i, 1);
402
                        j++;
403
                }
404
                else
405
                {
406
                        if (sensor_matrix_get_in_ring(sensorMatrix, i))
407
                        {
408
                                WL_DEBUG_PRINT("Robot ");
409
                                WL_DEBUG_PRINT_INT(i);
410
                                WL_DEBUG_PRINT(" has been removed from the sensor matrix of robot ");
411
                                WL_DEBUG_PRINT_INT(wl_get_xbee_id());
412
                                WL_DEBUG_PRINT(" due to a packet from robot ");
413
                                WL_DEBUG_PRINT_INT(source);
414
                                WL_DEBUG_PRINT(".\r\n");
415
                                sensor_matrix_set_in_ring(sensorMatrix, i, 0);
416
                        }
417

    
418
                        if (i == wl_get_xbee_id() && ringState == MEMBER)
419
                        {
420
                                ringState = NONMEMBER;
421
                                wl_token_ring_join();
422
                                
423
                                WL_DEBUG_PRINT("We have been removed from the ring ");
424
                                WL_DEBUG_PRINT("and are rejoining.\r\n");
425
                        }
426
                        
427
                        //the person who accepted us is dead... let's ask again
428
                        if (i == acceptor)
429
                        {
430
                                sensor_matrix_set_in_ring(sensorMatrix,
431
                                                wl_get_xbee_id(), 1);
432
                                ringState = NONMEMBER;
433
                                acceptor = -1;
434
                                wl_token_ring_join();
435
                        }
436
                }
437
        }
438

    
439
        wl_token_next_robot = nextRobot;
440
        
441
        deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY;
442
        
443
        //we have the token
444
        if (wl_token_next_robot == wl_get_xbee_id())
445
                wl_token_get_token();
446
}
447

    
448
/**
449
 * Gets the distance in the token ring between two robots.
450
 *
451
 * @param robot1 the first robot
452
 * @param robot2 the second robot
453
 *
454
 * @return the number of passes before the token is expected
455
 * to reach robot2 from robot1
456
 **/
457
int get_token_distance(int robot1, int robot2)
458
{
459
        int curr = robot1 + 1;
460
        int count = 1;
461
        while (1)
462
        {
463
                if (curr == sensor_matrix_get_size(sensorMatrix))
464
                        curr = 0;
465
                if (curr == robot2)
466
                        break;
467
                if (sensor_matrix_get_in_ring(sensorMatrix, curr))
468
                        count++;
469
                curr++;
470
        }
471
        return count;
472
}
473

    
474
/**
475
 * Passes the token to the next robot in the token ring.
476
 **/
477
void wl_token_pass_token()
478
{
479
        char nextRobot;
480
        int i = wl_get_xbee_id() + 1;
481
        if (accepted == -1)
482
        {
483
                while (1)
484
                {
485
                        if (i == sensor_matrix_get_size(sensorMatrix))
486
                                i = 0;
487
                        if (sensor_matrix_get_in_ring(sensorMatrix, i))
488
                        {
489
                                nextRobot = (char)i;
490
                                break;
491
                        }
492
                        i++;
493
                }
494
        }
495
        else
496
        {
497
                WL_DEBUG_PRINT("Accepting new robot, sending it the token.\r\n");
498
                //add a new robot to the token ring
499
                sensor_matrix_set_in_ring(sensorMatrix, accepted, 1);
500
                nextRobot = accepted;
501
                accepted = -1;
502
        }
503

    
504
        //we don't include ourself
505
        int packetSize = 1 + 2 * (sensor_matrix_get_joined(sensorMatrix) - 1);
506
        char* buf = (char*)malloc(packetSize * sizeof(char));
507
        if (!buf)
508
        {
509
                WL_DEBUG_PRINT_INT(packetSize);
510
                WL_DEBUG_PRINT("Out of memory - pass token.\r\n");
511
                return;
512
        }
513
        buf[0] = nextRobot;
514

    
515
        int j = 0;
516
        for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++)
517
                if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id())
518
                {
519
                        buf[2*j + 1] = i;
520
                        buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i);
521
                        j++;
522
                }
523
        
524
        WL_DEBUG_PRINT("Passing the token to robot ");
525
        WL_DEBUG_PRINT_INT(buf[0]);
526
        WL_DEBUG_PRINT(".\r\n");
527
        wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS,
528
                buf, packetSize, 3);
529

    
530
        wl_token_next_robot = nextRobot;
531
        deathDelay = DEATH_DELAY;
532
        free(buf);
533
}
534

    
535
/**
536
 * Called when a packet is received stating that another robot has turned
537
 * its BOM on. Our BOM is then read, and the data is added to the sensor
538
 * matrix.
539
 *
540
 * @param source the robot whose BOM is on
541
 **/
542
void wl_token_bom_on_receive(int source)
543
{
544
        WL_DEBUG_PRINT("Robot ");
545
        WL_DEBUG_PRINT_INT(source);
546
        WL_DEBUG_PRINT(" has flashed its bom.\r\n");
547
        
548
        bom_on_count = 0;
549

    
550
        sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), 
551
                source, get_max_bom_function());
552
}
553

    
554
/**
555
 * This method is called when we receive the token. Upon receiving
556
 * the token, we must send a BOM_ON packet, flash the BOM, and send
557
 * the token to the next robot.
558
 * 
559
 * If there is a pending request for the token, this is processed first.
560
 **/
561
void wl_token_get_token()
562
{
563
        WL_DEBUG_PRINT("We have the token.\r\n");
564
        if (ringState == ACCEPTED)
565
        {
566
                sensor_matrix_set_in_ring(sensorMatrix,
567
                        wl_get_xbee_id(), 1);
568
                WL_DEBUG_PRINT("Now a member of the token ring.\r\n");
569
                ringState = MEMBER;
570
                joinDelay = -1;
571
        }
572

    
573
        if (ringState == LEAVING || ringState == NONMEMBER)
574
        {
575
                sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
576
                if (ringState == NONMEMBER)
577
                {
578
                        WL_DEBUG_PRINT("We should have left the token ring, but didn't.\r\n");
579
                }
580
                return;
581
        }
582
        
583
        WL_DEBUG_PRINT("Our BOM has been flashed.\r\n");
584
        wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON,
585
                NULL, 0, 0);
586

    
587
        bom_on_function();
588
        #ifdef ROBOT
589
        delay_ms(BOM_DELAY);
590
        #endif
591
        bom_off_function();
592
        
593
        if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id()))
594
        {
595
                WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n");
596
                return;
597
        }
598
        
599
        wl_token_pass_token();
600
}
601

    
602
/**
603
 * Called when a request to join the token ring is received.
604
 * If we are the robot preceding the requester in the ring,
605
 * we respond with a JOIN_ACCEPT packet and pass the token to
606
 * this robot when we receive the token.
607
 *
608
 * @param source the robot who requested to join
609
 **/
610
void wl_token_join_receive(int source)
611
{
612
        WL_DEBUG_PRINT("Received joining request from robot ");
613
        WL_DEBUG_PRINT_INT(source);
614
        WL_DEBUG_PRINT(".\r\n");
615

    
616
        //we cannot accept the request if we are not a member
617
        if (ringState != MEMBER)
618
                return;
619
        //if they didn't get our response, see if we should respond again
620
        if (accepted == source)
621
                accepted = -1;
622
        //we can only accept one request at a time
623
        if (accepted != -1)
624
                return;
625
        
626
        //check if we are the preceding robot in the token ring
627
        int i = source - 1;
628
        while (1)
629
        {
630
                if (i < 0)
631
                        i = sensor_matrix_get_size(sensorMatrix) - 1;
632
                //we must send a join acceptance
633
                if (i == wl_get_xbee_id())
634
                        break;
635

    
636
                //another robot will handle it
637
                if (sensor_matrix_get_in_ring(sensorMatrix, i))
638
                        return;
639
                i--;
640
        }
641

    
642
        accepted = source;
643
        wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT,
644
                NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME);
645
        
646
        WL_DEBUG_PRINT("Accepting robot ");
647
        WL_DEBUG_PRINT_INT(source);
648
        WL_DEBUG_PRINT(" into the token ring.\r\n");
649

    
650
        // the token ring has not started yet
651
        if (sensor_matrix_get_joined(sensorMatrix) == 1)
652
                wl_token_pass_token();
653
}
654

    
655
/**
656
 * Called when we receive a JOIN_ACCEPT packet in attempting to join
657
 * the token ring.
658
 * Our attempt to join the ring is stopped, and we wait for the token.
659
 *
660
 * @param source the robot who accepted us
661
 **/
662
void wl_token_join_accept_receive(int source)
663
{
664
        WL_DEBUG_PRINT("Accepted into the token ring by robot ");
665
        WL_DEBUG_PRINT_INT(source);
666
        WL_DEBUG_PRINT(".\r\n");
667
        joinDelay = JOIN_DELAY;
668
        ringState = ACCEPTED;
669
        acceptor = source;
670

    
671
        //add ourselves to the token ring
672
        sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
673
}
674

    
675
/**
676
 * Returns the number of robots in the token ring.
677
 *
678
 * @return the number of robots in the token ring
679
 **/
680
int wl_token_get_robots_in_ring(void)
681
{
682
        return sensor_matrix_get_joined(sensorMatrix);
683
}
684

    
685
/**
686
 * Returns true if the specified robot is in the token ring, false
687
 * otherwise.
688
 *
689
 * @param robot the robot to check for whether it is in the token ring
690
 * @return nonzero if the robot is in the token ring, zero otherwise
691
 **/
692
int wl_token_is_robot_in_ring(int robot)
693
{
694
        return sensor_matrix_get_in_ring(sensorMatrix, robot);
695
}
696

    
697
/**
698
 * Begins iterating through the robots in the token ring.
699
 *
700
 * @see wl_token_iterator_has_next, wl_token_iterator_next
701
 **/
702
void wl_token_iterator_begin(void)
703
{
704
        int i = 0;
705
        iteratorCount = 0;
706
        while (!sensor_matrix_get_in_ring(sensorMatrix, i) &&
707
                        i < sensor_matrix_get_size(sensorMatrix))
708
                i++;
709
        if (i == sensor_matrix_get_size(sensorMatrix))
710
                i = -1;
711
        iteratorCount = i;
712
}
713

    
714
/**
715
 * Returns true if there are more robots in the token ring
716
 * to iterate through, and false otherwise.
717
 *
718
 * @return nonzero if there are more robots to iterate through,
719
 * zero otherwise
720
 *
721
 * @see wl_token_iterator_begin, wl_token_iterator_next
722
 **/
723
int wl_token_iterator_has_next(void)
724
{
725
        return iteratorCount != -1;
726
}
727

    
728
/**
729
 * Returns the next robot ID in the token ring.
730
 *
731
 * @return the next robot ID in the token ring, or -1 if none exists
732
 *
733
 * @see wl_token_iterator_begin, wl_token_iterator_has_next
734
 **/
735
int wl_token_iterator_next(void)
736
{
737
        int result = iteratorCount;
738
        if (result < 0)
739
                return result;
740

    
741
        iteratorCount++;
742
        while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) &&
743
                iteratorCount < sensor_matrix_get_size(sensorMatrix))
744
                iteratorCount++;
745
        if (iteratorCount == sensor_matrix_get_size(sensorMatrix))
746
                iteratorCount = -1;
747
        return result;
748
}
749

    
750
/**
751
 * Returns the number of robots currently in the token ring.
752
 *
753
 * @return the number of robots in the token ring
754
 **/
755
int wl_token_get_num_robots(void)
756
{
757
        return sensor_matrix_get_joined(sensorMatrix);
758
}
759

    
760
/**
761
 * Returns the number of robots in the sensor matrix.
762
 *
763
 * @return the number of robots in the sensor matrix
764
 **/
765
int wl_token_get_matrix_size(void)
766
{
767
        return sensor_matrix_get_size(sensorMatrix);
768
}
769