Project

General

Profile

Revision 397

added error returns to some libwireless functions, handled in server.

View differences:

wl_token_ring.c
1 1
/**
2 2
 * Copyright (c) 2007 Colony Project
3
 * 
3
 *
4 4
 * Permission is hereby granted, free of charge, to any person
5 5
 * obtaining a copy of this software and associated documentation
6 6
 * files (the "Software"), to deal in the Software without
......
9 9
 * copies of the Software, and to permit persons to whom the
10 10
 * Software is furnished to do so, subject to the following
11 11
 * conditions:
12
 * 
12
 *
13 13
 * The above copyright notice and this permission notice shall be
14 14
 * included in all copies or substantial portions of the Software.
15
 * 
15
 *
16 16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
......
146 146
		WL_DEBUG_PRINT(".\r\n");
147 147
		return;
148 148
	}
149
	
149

  
150 150
	sensorMatrix = sensor_matrix_create();
151 151
	//add ourselves to the sensor matrix
152 152
	sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
......
164 164

  
165 165
/**
166 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 
167
 * turned on or off. This could be used for things such as
168 168
 * charging stations, which have multiple BOMs.
169 169
 *
170 170
 * @param on_function the function to be called when the BOM
......
209 209
			wl_token_next_robot = -1;
210 210
			deathDelay = DEATH_DELAY;
211 211
		}
212
		
212

  
213 213
		// we may have been dropped from the ring when this is received
214 214
		if (ringState == MEMBER)
215 215
			wl_token_pass_token();
......
247 247

  
248 248
/**
249 249
 * Called when the XBee tells us if a packet we sent has been received.
250
 * 
250
 *
251 251
 * @param frame the frame number assigned when the packet was sent
252 252
 * @param received 1 if the packet was received, 0 otherwise
253 253
 **/
......
300 300
 * if no token ring exists. The token ring uses global and robot to robot
301 301
 * packets, and does not rely on any PAN.
302 302
 **/
303
void wl_token_ring_join()
303
int wl_token_ring_join()
304 304
{
305 305
	WL_DEBUG_PRINT("Joining the token ring.\r\n");
306 306
	ringState = JOINING;
307 307
	joinDelay = DEATH_DELAY * 2;
308
	wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN,
309
		NULL, 0, 0);
308
	if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) {
309
		return -1;
310
	}
311

  
312
	return 0;
310 313
}
311 314

  
312 315
/**
......
338 341

  
339 342
/**
340 343
 * Returns the BOM reading we have for robot dest.
341
 * 
344
 *
342 345
 * @param dest the robot whose relative location is returned
343 346
 *
344 347
 * @return a BOM reading from us to robot dest, in the range
......
386 389
	{
387 390
		if (i == source)
388 391
			continue;
389
		
392

  
390 393
		//set the sensor information we receive
391 394
		if (j < sensorDataLength / 2 && sensorData[2 * j] == i)
392 395
		{
......
419 422
			{
420 423
				ringState = NONMEMBER;
421 424
				wl_token_ring_join();
422
				
425

  
423 426
				WL_DEBUG_PRINT("We have been removed from the ring ");
424 427
				WL_DEBUG_PRINT("and are rejoining.\r\n");
425 428
			}
426
			
429

  
427 430
			//the person who accepted us is dead... let's ask again
428 431
			if (i == acceptor)
429 432
			{
......
437 440
	}
438 441

  
439 442
	wl_token_next_robot = nextRobot;
440
	
443

  
441 444
	deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY;
442
	
445

  
443 446
	//we have the token
444 447
	if (wl_token_next_robot == wl_get_xbee_id())
445 448
		wl_token_get_token();
......
520 523
			buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i);
521 524
			j++;
522 525
		}
523
	
526

  
524 527
	WL_DEBUG_PRINT("Passing the token to robot ");
525 528
	WL_DEBUG_PRINT_INT(buf[0]);
526 529
	WL_DEBUG_PRINT(".\r\n");
......
544 547
	WL_DEBUG_PRINT("Robot ");
545 548
	WL_DEBUG_PRINT_INT(source);
546 549
	WL_DEBUG_PRINT(" has flashed its bom.\r\n");
547
	
550

  
548 551
	bom_on_count = 0;
549 552

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

  
......
555 558
 * This method is called when we receive the token. Upon receiving
556 559
 * the token, we must send a BOM_ON packet, flash the BOM, and send
557 560
 * the token to the next robot.
558
 * 
561
 *
559 562
 * If there is a pending request for the token, this is processed first.
560 563
 **/
561 564
void wl_token_get_token()
......
579 582
		}
580 583
		return;
581 584
	}
582
	
585

  
583 586
	WL_DEBUG_PRINT("Our BOM has been flashed.\r\n");
584 587
	wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON,
585 588
		NULL, 0, 0);
......
589 592
	delay_ms(BOM_DELAY);
590 593
	#endif
591 594
	bom_off_function();
592
	
595

  
593 596
	if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id()))
594 597
	{
595 598
		WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n");
596 599
		return;
597 600
	}
598
	
601

  
599 602
	wl_token_pass_token();
600 603
}
601 604

  
......
622 625
	//we can only accept one request at a time
623 626
	if (accepted != -1)
624 627
		return;
625
	
628

  
626 629
	//check if we are the preceding robot in the token ring
627 630
	int i = source - 1;
628 631
	while (1)
......
642 645
	accepted = source;
643 646
	wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT,
644 647
		NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME);
645
	
648

  
646 649
	WL_DEBUG_PRINT("Accepting robot ");
647 650
	WL_DEBUG_PRINT_INT(source);
648 651
	WL_DEBUG_PRINT(" into the token ring.\r\n");

Also available in: Unified diff