Project

General

Profile

Revision 732

New stuff works, fixed bugs, removed malloc.

View differences:

branches/wl_dev/code/projects/libwireless/lib/wl_token_ring.c
84 84

  
85 85
/*Global Variables*/
86 86

  
87
//the sensor matrix
88
static SensorMatrix* sensorMatrix;
89

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

  
......
178 175
		return -1;
179 176
	}
180 177

  
181
	sensorMatrix = sensor_matrix_create();
178
	sensor_matrix_create();
182 179
	//add ourselves to the sensor matrix
183
	sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
180
	sensor_matrix_set_in_ring(wl_get_xbee_id(), 0);
184 181

  
185 182
	wl_register_packet_group(&wl_token_ring_handler);
186 183

  
......
220 217
 **/
221 218
static void wl_token_ring_cleanup()
222 219
{
223
	sensor_matrix_destroy(sensorMatrix);
224 220
}
225 221

  
226 222
/**
......
235 231
		//also, declare that person dead, as long as it isn't us
236 232
		if (wl_token_next_robot != wl_get_xbee_id())
237 233
		{
238
			sensor_matrix_set_in_ring(sensorMatrix, wl_token_next_robot, 0);
234
			sensor_matrix_set_in_ring(wl_token_next_robot, 0);
239 235
			WL_DEBUG_PRINT("Robot ");
240 236
			WL_DEBUG_PRINT_INT(wl_token_next_robot);
241 237
			WL_DEBUG_PRINT(" has died.\r\n");
......
252 248
	//we must start our own token ring, no one is responding to us
253 249
	if (joinDelay == 0)
254 250
	{
255
		if (sensor_matrix_get_joined(sensorMatrix) == 0)
251
		if (sensor_matrix_get_joined() == 0)
256 252
		{
257 253
			WL_DEBUG_PRINT("Creating our own token ring, no robots seem to exist.\r\n");
258
			sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
254
			sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
259 255
			ringState = MEMBER;
260 256
			//this will make us pass the token to ourself
261 257
			//repeatedly, and other robots when they join
......
313 309
			wl_token_pass_receive(source);
314 310
			break;
315 311
		case WL_TOKEN_SENSOR_MATRIX:
316
			if (length < 1)
317
			{
318
				WL_DEBUG_PRINT("Malformed Token Pass packet received.\r\n");
319
				return;
320
			}
321 312
			wl_token_sensor_matrix_receive(source, packet, length);
313
			break;
322 314
		case WL_TOKEN_BOM_ON:
323 315
			//add the robot to the sensor matrix if it is not already there
324 316
			wl_token_bom_on_receive(source);
......
348 340
{
349 341
	if (wl_token_is_robot_in_ring(dest) &&
350 342
			(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) {
351
		return sensor_matrix_get_reading(sensorMatrix, source, dest);
343
		return sensor_matrix_get_reading(source, dest);
352 344
	}
353 345

  
354 346
	return -1;
......
375 367
 **/
376 368
int wl_token_get_robots_in_ring(void)
377 369
{
378
	return sensor_matrix_get_joined(sensorMatrix);
370
	return sensor_matrix_get_joined();
379 371
}
380 372

  
381 373
/**
......
387 379
 **/
388 380
int wl_token_is_robot_in_ring(int robot)
389 381
{
390
	return sensor_matrix_get_in_ring(sensorMatrix, robot);
382
	return sensor_matrix_get_in_ring(robot);
391 383
}
392 384

  
393 385
/**
......
399 391
{
400 392
	int i = 0;
401 393

  
402
	while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) {
394
	while (!sensor_matrix_get_in_ring(i) && i < sensor_matrix_get_size()) {
403 395
		i++;
404 396
	}
405 397

  
406
	if (i == sensor_matrix_get_size(sensorMatrix)) {
398
	if (i == sensor_matrix_get_size()) {
407 399
		i = -1;
408 400
	}
409 401

  
......
439 431
	}
440 432

  
441 433
	iteratorCount++;
442
	while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount)
443
		&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) {
434
	while (!sensor_matrix_get_in_ring(iteratorCount)
435
		&& iteratorCount < sensor_matrix_get_size()) {
444 436
		iteratorCount++;
445 437
	}
446 438

  
447
	if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) {
439
	if (iteratorCount == sensor_matrix_get_size()) {
448 440
		iteratorCount = -1;
449 441
	}
450 442

  
......
458 450
 **/
459 451
int wl_token_get_num_robots(void)
460 452
{
461
	return sensor_matrix_get_joined(sensorMatrix);
453
	return sensor_matrix_get_joined();
462 454
}
463 455

  
464 456
/**
......
468 460
 **/
469 461
int wl_token_get_matrix_size(void)
470 462
{
471
	return sensor_matrix_get_size(sensorMatrix);
463
	return sensor_matrix_get_size();
472 464
}
473 465

  
474 466
/**
......
478 470
 **/
479 471
static void wl_token_pass_receive(int source)
480 472
{
473
	WL_DEBUG_PRINT("Received token from ");
474
	WL_DEBUG_PRINT_INT(source);
475
	WL_DEBUG_PRINT(", expected ");
476
	WL_DEBUG_PRINT_INT(wl_token_next_robot);
477
	WL_DEBUG_PRINT(".\n");
481 478
	// this prevents two tokens from being passed around at a time (second clause is in case we are joining)
482
	if (source != wl_token_next_robot && bom_on_count <= DEATH_DELAY / 2 &&
479
	if ((source != wl_token_next_robot && wl_get_xbee_id() != wl_token_next_robot) && bom_on_count <= DEATH_DELAY / 2 &&
483 480
		ringState != ACCEPTED)
484 481
	{
485 482
		WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n");
......
488 485
	}
489 486
	bom_on_count = -1;
490 487
	deathDelay = -1;
491
	sensor_matrix_set_in_ring(sensorMatrix, source, 1);
488
	sensor_matrix_set_in_ring(source, 1);
492 489
	wl_token_get_token();
493 490
}
494 491

  
......
508 505
	i = source + 1;
509 506
	while (1)
510 507
	{
511
		if (i == sensor_matrix_get_size(sensorMatrix)) {
508
		if (i == sensor_matrix_get_size()) {
512 509
			i = 0;
513 510
		}
514 511

  
515
		if (sensor_matrix_get_in_ring(sensorMatrix, i))
512
		if (sensor_matrix_get_in_ring(i))
516 513
		{
517 514
			nextRobot = (char)i;
518 515
			break;
......
523 520

  
524 521
	bom_on_count = -1;
525 522
	deathDelay = -1;
526
	sensor_matrix_set_in_ring(sensorMatrix, source, 1);
523
	sensor_matrix_set_in_ring(source, 1);
527 524

  
528 525
	//with this packet, we are passed the id of the next robot in the ring
529 526
	//and the sensor matrix, a list of id and sensor reading pairs (two bytes for both)
530 527
	j = 0;
531
	for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++)
528
	for (i = 0; i < sensor_matrix_get_size(); i++)
532 529
	{
533 530
		if (i == source) {
534 531
			continue;
......
543 540
				accepted = -1;
544 541
				WL_DEBUG_PRINT("Someone accepted the robot we did.\r\n");
545 542
			}
546
			sensor_matrix_set_reading(sensorMatrix, source, i,
543
			sensor_matrix_set_reading(source, i,
547 544
						sensorData[2 * j + 1]);
548
			sensor_matrix_set_in_ring(sensorMatrix, i, 1);
545
			if (!sensor_matrix_get_in_ring(i))
546
			{
547
				WL_DEBUG_PRINT("Robot ");
548
				WL_DEBUG_PRINT_INT(i);
549
				WL_DEBUG_PRINT(" has been added to the sensor matrix of robot ");
550
				WL_DEBUG_PRINT_INT(wl_get_xbee_id());
551
				WL_DEBUG_PRINT(" due to a packet from robot ");
552
				WL_DEBUG_PRINT_INT(source);
553
				WL_DEBUG_PRINT(".\r\n");
554
			}
555
			sensor_matrix_set_in_ring(i, 1);
549 556
			j++;
550 557
		}
551 558
		else
552 559
		{
553
			if (sensor_matrix_get_in_ring(sensorMatrix, i))
560
			if (sensor_matrix_get_in_ring(i))
554 561
			{
555 562
				WL_DEBUG_PRINT("Robot ");
556 563
				WL_DEBUG_PRINT_INT(i);
......
559 566
				WL_DEBUG_PRINT(" due to a packet from robot ");
560 567
				WL_DEBUG_PRINT_INT(source);
561 568
				WL_DEBUG_PRINT(".\r\n");
562
				sensor_matrix_set_in_ring(sensorMatrix, i, 0);
569
				sensor_matrix_set_in_ring(i, 0);
563 570
			}
564 571

  
565 572
			if (i == wl_get_xbee_id() && ringState == MEMBER)
......
574 581
			//the person who accepted us is dead... let's ask again
575 582
			if (i == acceptor)
576 583
			{
577
				sensor_matrix_set_in_ring(sensorMatrix,
578
						wl_get_xbee_id(), 1);
584
				sensor_matrix_set_in_ring(wl_get_xbee_id(), 0);
579 585
				ringState = NONMEMBER;
580 586
				acceptor = -1;
581 587
				wl_token_ring_join();
......
587 593

  
588 594
	deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY;
589 595

  
590
	if (sensor_matrix_get_size(sensorMatrix) == 0 && ringState == JOINING)
591
		wl_send_robot_to_robot_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME);
596
	if (sensor_matrix_get_joined() == 0 && ringState == JOINING)
597
		wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME);
592 598
}
593 599

  
594 600
/**
......
606 612
	int count = 1;
607 613
	while (1)
608 614
	{
609
		if (curr == sensor_matrix_get_size(sensorMatrix))
615
		if (curr == sensor_matrix_get_size())
610 616
			curr = 0;
611 617
		if (curr == robot2)
612 618
			break;
613
		if (sensor_matrix_get_in_ring(sensorMatrix, curr))
619
		if (sensor_matrix_get_in_ring(curr))
614 620
			count++;
615 621
		curr++;
616 622
	}
......
624 630
{
625 631
	char nextRobot = 0xFF;
626 632
	int i = wl_get_xbee_id() + 1;
633
	char buf[2 * sensor_matrix_get_size()];
634
	WL_DEBUG_PRINT("PASSING\n");
627 635
	if (accepted == -1)
628 636
	{
629 637
		while (1)
630 638
		{
631
			if (i == sensor_matrix_get_size(sensorMatrix)) {
639
			if (i == sensor_matrix_get_size()) {
632 640
				i = 0;
633 641
			}
634 642

  
635
			if (sensor_matrix_get_in_ring(sensorMatrix, i))
643
			if (sensor_matrix_get_in_ring(i))
636 644
			{
637 645
				nextRobot = (char)i;
638 646
				break;
......
645 653
	{
646 654
		WL_DEBUG_PRINT("Accepting new robot, sending it the token.\r\n");
647 655
		//add a new robot to the token ring
648
		sensor_matrix_set_in_ring(sensorMatrix, accepted, 1);
656
		sensor_matrix_set_in_ring(accepted, 1);
649 657
		nextRobot = accepted;
650 658
		accepted = -1;
651 659
	}
660
	WL_DEBUG_PRINT("2\n");
652 661

  
653
	//we don't include ourself
654
	int packetSize = 2 * (sensor_matrix_get_joined(sensorMatrix) - 1);
655
	char* buf = (char*)malloc(packetSize * sizeof(char));
656
	if (!buf)
657
	{
658
		WL_DEBUG_PRINT_INT(packetSize);
659
		WL_DEBUG_PRINT("Out of memory - pass token.\r\n");
660
		free(buf);
661
		return -1;
662
	}
663

  
664 662
	int j = 0;
665
	for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) {
666
		if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id())
663
	for (i = 0; i < sensor_matrix_get_size(); i++) {
664
		if (sensor_matrix_get_in_ring(i) && i != wl_get_xbee_id())
667 665
		{
668 666
			buf[2*j] = i;
669
			buf[2*j + 1] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i);
667
			buf[2*j + 1] = sensor_matrix_get_reading(wl_get_xbee_id(), i);
670 668
			j++;
671 669
		}
672 670
	}
673 671

  
672
	int packetSize = 2 * sensor_matrix_get_size() * sizeof(char);
674 673
	WL_DEBUG_PRINT("Passing the token to robot ");
675 674
	WL_DEBUG_PRINT_INT(nextRobot);
676 675
	WL_DEBUG_PRINT(".\r\n");
......
678 677
		free(buf);
679 678
		return -1;
680 679
	}
681
	if (wl_send_robot_to_robot_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME))
680
	if (wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME))
682 681
	{
683 682
		free(buf);
684 683
		return -1;
......
706 705

  
707 706
	bom_on_count = 0;
708 707

  
709
	sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(),
708
	sensor_matrix_set_reading(wl_get_xbee_id(),
710 709
		source, get_max_bom_function());
711 710
}
712 711

  
......
722 721
	WL_DEBUG_PRINT("We have the token.\r\n");
723 722
	if (ringState == ACCEPTED)
724 723
	{
725
		sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
724
		sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
726 725
		WL_DEBUG_PRINT("Now a member of the token ring.\r\n");
727 726
		ringState = MEMBER;
728 727
		joinDelay = -1;
......
730 729

  
731 730
	if (ringState == LEAVING || ringState == NONMEMBER)
732 731
	{
733
		sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
732
		sensor_matrix_set_in_ring(wl_get_xbee_id(), 0);
734 733
		if (ringState == NONMEMBER)
735 734
		{
736 735
			WL_DEBUG_PRINT("We should have left the token ring, but didn't.\r\n");
......
747 746
	#endif
748 747
	bom_off_function();
749 748

  
750
	if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id()))
749
	if (!sensor_matrix_get_in_ring(wl_get_xbee_id()))
751 750
	{
752 751
		WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n");
753 752
		return;
......
785 784
	while (1)
786 785
	{
787 786
		if (i < 0)
788
			i = sensor_matrix_get_size(sensorMatrix) - 1;
787
			i = sensor_matrix_get_size() - 1;
789 788
		//we must send a join acceptance
790 789
		if (i == wl_get_xbee_id())
791 790
			break;
792 791

  
793 792
		//another robot will handle it
794
		if (sensor_matrix_get_in_ring(sensorMatrix, i))
793
		if (sensor_matrix_get_in_ring(i))
795 794
			return;
796 795
		i--;
797 796
	}
......
805 804
	WL_DEBUG_PRINT(" into the token ring.\r\n");
806 805

  
807 806
	// the token ring has not started yet
808
	if (sensor_matrix_get_joined(sensorMatrix) == 1)
807
	if (sensor_matrix_get_joined() == 1)
809 808
		wl_token_pass_token();
810 809
}
811 810

  
......
826 825
	acceptor = source;
827 826

  
828 827
	//add ourselves to the token ring
829
	sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
828
	sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
830 829
}
branches/wl_dev/code/projects/libwireless/lib/sensor_matrix.h
46 46
 * @{
47 47
 **/
48 48

  
49
#define MAXIMUM_XBEE_ID		0x10
50
#define READING_UNKNOWN		0xFF
51

  
49 52
/**
50 53
 * @struct SensorMatrix
51 54
 *
......
54 57
typedef struct
55 58
{
56 59
	/**
57
	 * The size of the sensor matrix.
58
	**/
59
	int size;
60
	 * The number of robots in the token ring.
61
	 **/
62
	int numJoined;
60 63
	/**
61 64
	 * The matrix. Each row represents the readings of one
62 65
	 * robot.
63 66
	 **/
64
	int** matrix;
67
	unsigned char matrix[MAXIMUM_XBEE_ID][MAXIMUM_XBEE_ID];
65 68
	/**
66 69
	 * The element representing a robot is true if that robot
67 70
	 * is in the token ring and false otherwise.
68 71
	 **/
69
	int* joined;
70
	/**
71
	 * The number of robots in the token ring.
72
	 **/
73
	int numJoined;
72
	unsigned char joined[MAXIMUM_XBEE_ID];
74 73
} SensorMatrix;
75 74

  
76 75
/**@brief Create a sensor matrix **/
77
SensorMatrix* sensor_matrix_create(void);
78
/**@brief Destroy a sensor matrix **/
79
void sensor_matrix_destroy(SensorMatrix* m);
80
/**@brief Add a robot to a sensor matrix **/
81
void sensor_matrix_add_robot(SensorMatrix* m, int id);
82
/**@brief Remove a robot from a sensor matrix **/
83
void sensor_matrix_remove_robot(SensorMatrix* m, int id);
76
void sensor_matrix_create(void);
84 77
/**@brief Set a reading in a sensor matrix **/
85
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading);
78
void sensor_matrix_set_reading(int observer, int robot, int reading);
86 79
/**@brief Get a reading in a sensor matrix **/
87
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot);
80
int sensor_matrix_get_reading(int observer, int robot);
88 81
/**@brief Set whether the robot is in the token ring **/
89
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in);
82
void sensor_matrix_set_in_ring(int robot, int in);
90 83
/**@brief Get whether the robot is in the sensor ring **/
91
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot);
92
/**@brief Get the size of the sensor matrix **/
93
int sensor_matrix_get_size(SensorMatrix* m);
84
int sensor_matrix_get_in_ring(int robot);
94 85
/**@brief Get the number of robots which have joined the token ring **/
95
int sensor_matrix_get_joined(SensorMatrix* m);
86
int sensor_matrix_get_joined(void);
87
/**@brief Get the maximum size of the sensor matrix **/
88
int sensor_matrix_get_size(void);
96 89

  
97 90
/** @} **/ //end defgroup
98 91

  
branches/wl_dev/code/projects/libwireless/lib/xbee.c
106 106
#endif
107 107

  
108 108
// TODO: is this a good size?
109
#define XBEE_BUFFER_SIZE	256
109
#define XBEE_BUFFER_SIZE	128
110
#define PACKET_BUFFER_SIZE	108
110 111
// a buffer for data received from the XBee
111 112
char arrival_buf[XBEE_BUFFER_SIZE];
112 113
// location of last unread byte in buffer
......
116 117

  
117 118

  
118 119
//used to store packets as they are read
119
static char xbee_buf[128];
120
static char xbee_buf[PACKET_BUFFER_SIZE];
120 121
static int currentBufPos = 0;
121 122

  
122 123
//XBee status
......
177 178
	while (1)
178 179
	{
179 180
		if (xbee_read(&c, 1) != 0) {
180
			fprintf(stderr, "xbee_read failed.\n");
181
			WL_DEBUG_PRINT("xbee_read failed.\n");
181 182
			return NULL;
182 183
		}
183 184

  
......
204 205
 **/
205 206
int xbee_lib_init()
206 207
{
207
	arrival_buf[0] = 'A';
208
	arrival_buf[1] = 'A';
209
	arrival_buf[2] = 'A';
210 208
	#ifdef ROBOT
211 209

  
212 210
	//enable the receiving interrupt
......
404 402
}
405 403

  
406 404
/**
407
 * Exit API mode. (warning - does not check for response)
408
 **/
409
static int xbee_exit_api_mode()
410
{
411
	return xbee_send_string("ATAP 0\r");
412
}
413

  
414
/**
415 405
 * Wait until the string "OK\r" is received from the XBee.
416 406
 **/
417 407
static void xbee_wait_for_ok()
branches/wl_dev/code/projects/libwireless/lib/sensor_matrix.c
38 38

  
39 39
#include "sensor_matrix.h"
40 40

  
41
#define DEFAULT_SENSOR_MATRIX_SIZE 20
41
// the global sensor matrix
42
SensorMatrix m;
42 43

  
43
/*Sensor Matrix Functions*/
44
static void sensor_matrix_expand(SensorMatrix* m, int nextSize);
45

  
46 44
/**
47 45
 * Initializes the sensor matrix.
48 46
 *
49 47
 * @return the newly created sensor matrix
50 48
 **/
51
SensorMatrix* sensor_matrix_create()
49
void sensor_matrix_create()
52 50
{
53
	SensorMatrix* m;
54
	int i;
55

  
56
	m = (SensorMatrix*)malloc(sizeof(SensorMatrix));
57
	if (!m)
58
	{
59
		WL_DEBUG_PRINT("Out of memory - create sensor matrix.\r\n");
60
		return NULL;
61
	}
62
	m->size = DEFAULT_SENSOR_MATRIX_SIZE;
63
	m->matrix = (int**)malloc(m->size * sizeof(int*));
64
	if (!(m->matrix)) {
65
	  WL_DEBUG_PRINT("Out of memory - allocating memory for matrix.\r\n");
66
	  free(m);
67
	  return NULL;
68
	}
69
	m->joined = (int*)malloc(m->size * sizeof(int));
70
	if (!(m->joined)) {
71
	  WL_DEBUG_PRINT("Out of memory - allocating memory for joined.\r\n");
72
	  free(m->matrix);
73
	  free(m);
74
	  return NULL;
75
	}
76
	m->numJoined = 0;
77
	if (!(m->matrix) || !(m->joined))
78
	{
79
		WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n");
80
		return NULL;
81
	}
82

  
83
	for (i = 0; i < m->size; i++)
84
	{
85
		m->matrix[i] = NULL;
86
		m->joined[i] = 0;
87
	}
88
	return m;
89
}
90

  
91
/**
92
 * Deletes and frees memory from the sensor matrix.
93
 *
94
 * @param m the sensor matrix to delete
95
 **/
96
void sensor_matrix_destroy(SensorMatrix* m)
97
{
98
	int i;
99
	for (i = 0; i < m->size; i++)
100
		if (m->matrix[i] != NULL)
101
			free(m->matrix[i]);
102
	free(m->matrix);
103
	free(m->joined);
104
	free(m);
105
}
106

  
107
/**
108
 * Adds robot with XBee id id to the sensor matrix.
109
 *
110
 * @param m the sensor matrix
111
 * @param id the XBee ID of the robot to add
112
 **/
113
void sensor_matrix_add_robot(SensorMatrix* m, int id)
114
{
115
	int i;
116
	if (id >= m->size) {
117
		sensor_matrix_expand(m, id + 1);
118
	}
119

  
120
	if (m->matrix[id] != NULL) {
121
		return;
122
	}
123

  
124
	m->matrix[id] = (int*)malloc(m->size * sizeof(int));
125
	if (!(m->matrix[id]))
126
	{
127
		WL_DEBUG_PRINT("Out of memory - add robot.\r\n");
128
		return;
129
	}
130

  
131
	for (i = 0; i < m->size; i++) {
132
		if (m->matrix[i] != NULL) {
133
			m->matrix[i][id] = -1;
134
		}
135
	}
136
}
137

  
138
/**
139
 * Removes robot with id from the sensor matrix, and removes
140
 * all sensor information regarding the robot.
141
 *
142
 * @param m the sensor matrix
143
 * @param id the XBee ID of the robot to remove
144
 **/
145
void sensor_matrix_remove_robot(SensorMatrix* m, int id)
146
{
147
	int i;
148

  
149
	if (id >= m->size || m->matrix[id] == NULL)
150
	{
151
		WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
152
		return;
153
	}
154

  
155
	free(m->matrix[id]);
156
	m->matrix[id] = NULL;
157

  
158
	for (i = 0 ; i < m->size; i++)
159
		if (m->matrix[i] != NULL)
160
			m->matrix[i][id] = -1;
161

  
162
	m->joined[id] = 0;
163
}
164

  
165
/**
166
 * Expands the size of the sensor matrix if an id number we attempt
167
 * to add is too large.
168
 *
169
 * @param m the sensor matrix to expand
170
 * @param size the new size of the sensor matrix
171
 **/
172
//Note: this has probably not been tested, hopefully it works
173
static void sensor_matrix_expand(SensorMatrix* m, int nextSize)
174
{
175 51
	int i, j;
176
	WL_DEBUG_PRINT("Expanding sensor matrix.\r\n");
177 52

  
178
	int** tempMatrix = (int**)malloc(nextSize * sizeof(int*));
179
	if (!tempMatrix)
180
	{
181
		WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n");
182
		return;
183
	}
53
	m.numJoined = 0;
184 54

  
185
	for (i = 0; i < nextSize; i++)
186
		tempMatrix[i] = NULL;
187

  
188
	//copy over old sensor data
189
	for (i = 0; i < m->size; i++)
190
		if (m->matrix[i] != NULL)
191
		{
192
		  tempMatrix[i] = (int *)malloc(nextSize * sizeof(int));
193
			if (!tempMatrix[i])
194
			{
195
				WL_DEBUG_PRINT("Out of memory - expand matrix 2.\r\n");
196
				return;
197
			}
198
			for (j = 0; j < m->size; j++) {
199
				tempMatrix[i][j] = m->matrix[i][j];
200
			}
201

  
202
			for (j = m->size; j < nextSize; j++) {
203
				tempMatrix[i][j] = -1;
204
			}
205

  
206
			free(m->matrix[i]);
207
		}
208

  
209
	free(m->matrix);
210
	m->matrix = tempMatrix;
211
	m->size = nextSize;
212

  
213
	//expand the size of joined
214
	int* tempJoined = (int *)malloc(nextSize * sizeof(int));
215
	if (!tempJoined)
55
	for (i = 0; i < MAXIMUM_XBEE_ID; i++)
216 56
	{
217
		WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n");
218
		return;
57
		m.joined[i] = 0;
58
		for (j = 0; j < MAXIMUM_XBEE_ID; j++)
59
			m.matrix[i][j] = READING_UNKNOWN;
219 60
	}
220

  
221
	for (i = 0; i < m->size; i++) {
222
		tempJoined[i] = m->joined[i];
223
	}
224

  
225
	for (i = m->size; i < nextSize; i++) {
226
		tempJoined[i] = 0;
227
	}
228

  
229
	free(m->joined);
230
	m->joined = tempJoined;
231 61
}
232 62

  
233 63
/**
234 64
 * Sets the sensor reading for robot robot to reading.
235 65
 *
236
 * @param m the sensor matrix to set the reading for
237 66
 * @param observer the id of the robot who made the reading
238 67
 * @param robot the id of the robot who the reading is for
239 68
 * @param reading the BOM reading from observer to robot
240 69
 */
241
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading)
70
void sensor_matrix_set_reading(int observer, int robot, int reading)
242 71
{
243
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) {
244
		sensor_matrix_add_robot(m, observer);
72
	if (robot >= MAXIMUM_XBEE_ID || observer >= MAXIMUM_XBEE_ID)
73
	{
74
		WL_DEBUG_PRINT("ID too large.");
75
		return;
245 76
	}
246 77

  
247
	m->matrix[observer][robot] = reading;
78
	m.matrix[observer][robot] = (unsigned char)reading;
248 79
}
249 80

  
250 81
/**
251 82
 * Gets the sensor reading for a robot to another robot.
252 83
 *
253
 * @param m the sensor matrix
254 84
 * @param observer the robot whose reading we check
255 85
 * @param robot the robot who we are checking the reading to
256 86
 *
257 87
 * @return the observer's BOM reading for robot
258 88
 **/
259
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
89
int sensor_matrix_get_reading(int observer, int robot)
260 90
{
261
	if (observer >= m->size || robot >= m->size) {
91
	if (observer >= MAXIMUM_XBEE_ID || robot >= MAXIMUM_XBEE_ID)
262 92
		return -1;
263
	}
264 93

  
265
	return m->matrix[observer][robot];
94
	return (int)m.matrix[observer][robot];
266 95
}
267 96

  
268 97
/**
269 98
 * Sets whether or not the given robot is part of the token ring.
270 99
 *
271
 * @param m the sensor matrix
272 100
 * @param robot the robot to set as a member / nonmember of the token ring
273 101
 * @param in 1 if the robot is in the token ring, 0 otherwise
274 102
 **/
275
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
103
void sensor_matrix_set_in_ring(int robot, int in)
276 104
{
277
	if (robot >= m->size) {
278
		sensor_matrix_expand(m, robot + 1);
105
	if (robot >= MAXIMUM_XBEE_ID)
106
	{
107
		WL_DEBUG_PRINT("ID too large.");
108
		return;
279 109
	}
280 110

  
281
	if (in == 1) {
282
		sensor_matrix_add_robot(m, robot);
283
	}
111
	if (in == 1 && m.joined[robot] == 0)
112
		m.numJoined++;
284 113

  
285
	if (in == 1 && m->joined[robot] == 0) {
286
		m->numJoined++;
287
	}
114
	if (in == 0 && m.joined[robot])
115
		m.numJoined--;
288 116

  
289
	if (in == 0 && m->joined[robot] == 1) {
290
		m->numJoined--;
291
	}
292

  
293
	m->joined[robot] = in;
117
	m.joined[robot] = in;
294 118
}
295 119

  
296 120
/**
297 121
 * Checks if the given robot is in the token ring.
298 122
 *
299
 * @param m the sensor matrix
300 123
 * @param robot the ID of the robot to check
301 124
 *
302 125
 * @return 1 if the robot is in the token ring, 0 otherwise
303 126
 **/
304
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
127
int sensor_matrix_get_in_ring(int robot)
305 128
{
306
	if (robot >= m->size) {
129
	if (robot >= MAXIMUM_XBEE_ID)
307 130
		return -1;
308
	}
309 131

  
310
	return m->joined[robot];
132
	return m.joined[robot];
311 133
}
312 134

  
313 135
/**
314
 * Returns the size of the sensor matrix.
136
 * Returns the number of robots which have joined the
137
 * token ring.
315 138
 *
316
 * @param m the sensor matrix
317
 *
318
 * @return the size of the sensor matrix
139
 * @return the number of robots in the token ring
319 140
 **/
320
int sensor_matrix_get_size(SensorMatrix* m)
141
int sensor_matrix_get_joined(void)
321 142
{
322
	return m->size;
143
	return m.numJoined;
323 144
}
324 145

  
325 146
/**
326
 * Returns the number of robots which have joined the
327
 * token ring.
147
 * Returns the maximum XBee id of a robot which can be stored
148
 * in the sensor matrix.
328 149
 *
329
 * @param m the sensor matrix
330
 *
331
 * @return the number of robots in the token ring
150
 * @return the maximum number of robots which can be stored
332 151
 **/
333
int sensor_matrix_get_joined(SensorMatrix* m)
152
inline int sensor_matrix_get_size(void)
334 153
{
335
	return m->numJoined;
154
	return MAXIMUM_XBEE_ID;
336 155
}
156

  

Also available in: Unified diff