Project

General

Profile

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

View differences:

trunk/code/projects/colonet/ColonetServer/Command.cpp
174 174
          int_tokens[0], int_tokens[1], int_tokens[2]);
175 175
  if (colonet_wl_send((short)pool_index, int_tokens[0], (ColonetMessageType)int_tokens[1], int_tokens[2], arguments)
176 176
      != 0) {
177
    fprintf(stderr, "Error - Colonet_wl_send failed.");
178
    return -1;
177
    fprintf(stderr, "Error - Colonet_wl_send failed.\n");
178
    exit(1);
179 179
  }
180 180

  
181 181
  return 0;
......
266 266
  int num_robots;
267 267
  int* xbee_ids = colonet_get_xbee_ids(&num_robots);
268 268

  
269

  
270

  
271
  printf("num_robots: %d\n", num_robots);
272
  printf("xbee_ids: ");
273
  for (int i = 0; i < num_robots; i++) {
274
    printf("%d ", xbee_ids[i]);
275
  }
276
  printf("\n");
277

  
278

  
279

  
269 280
  if (!connection_pool) {
270 281
    return -1;
271 282
  }
trunk/code/projects/colonet/ColonetServer/colonet_wireless.cpp
113 113
    }
114 114
  } else {
115 115
    printf("sending to specific robot.\n");
116
    wl_send_robot_to_robot_global_packet(COLONET_PACKET_GROUP_ID, (char)msg_type, (char*)(&pkt),
117
                                         sizeof(ColonetRobotServerPacket), dest, COLONET_RESPONSE_PACKET_FRAME_ID);
116
    if (wl_send_robot_to_robot_global_packet(COLONET_PACKET_GROUP_ID, (char)msg_type, (char*)(&pkt),
117
      sizeof(ColonetRobotServerPacket), dest, COLONET_RESPONSE_PACKET_FRAME_ID) != 0) {
118
      return -1;
119
    }
118 120
  }
119 121

  
120 122
  return 0;
......
130 132

  
131 133
  wl_token_iterator_begin();
132 134

  
133
  int* p = ids;
135
  int i = 0;
134 136
  while (wl_token_iterator_has_next()) {
135
    *p = wl_token_iterator_next();
136
    p++;
137
    ids[i] = wl_token_iterator_next();
138
    i++;
137 139
  }
138 140

  
139 141
  *numrobots = num_robots;
......
164 166
/**************************** Private functions ******************************/
165 167

  
166 168
static void timeout_handler() {
167
  printf("colonet wireless - timeout!\n");
169
  // printf("colonet wireless - timeout!\n");
168 170
}
169 171

  
170 172
static void handle_response(int frame, int received) {
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp
30 30

  
31 31
#define u_int32_t unsigned
32 32

  
33
static ConnectionPool * connection_pool;
33
static ConnectionPool* connection_pool;
34 34

  
35 35
/**
36 36
 * @brief Default constructor for ColonetServer
trunk/code/projects/colonet/ColonetServer/Makefile
28 28
	make; ./ColonetServer
29 29
	../vision/vision.sh
30 30

  
31
ColonetServer: $(COLONETCPPFILES) $(COLONETFILES) $(LOGGINGFILES)
31
../../libwireless/lib/libwireless_colonet.a: ../../libwireless/lib/*.c ../../libwireless/lib/*.h
32 32
	cd ../../libwireless/lib; make colonet
33 33

  
34
ColonetServer: ../../libwireless/lib/libwireless_colonet.a $(COLONETCPPFILES) $(COLONETFILES) $(LOGGINGFILES)
34 35
	@echo "---begin compilation---"
35 36
	@echo ""
36 37
	@echo "---create object files---"
trunk/code/projects/libwireless/lib/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

  
trunk/code/projects/libwireless/lib/wireless.h
118 118
} PacketGroupHandler;
119 119

  
120 120
/**@brief Initialize the wireless library **/
121
int wl_init();
121
int wl_init(void);
122 122
/**@brief Uninitialize the wireless library **/
123 123
void wl_terminate(void);
124 124
/**@brief Perform wireless library functionality **/
......
129 129
void wl_unregister_packet_group(PacketGroupHandler* h);
130 130

  
131 131
/**@brief Send a packet to a specific robot in any PAN **/
132
void wl_send_robot_to_robot_global_packet(char group, char type,
133
		char* data, int len, int dest, char frame);
132
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame);
134 133
/**@brief Send a packet to a specific robot in our PAN **/
135
void wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame);
134
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame);
136 135
/**@brief Send a packet to all robots **/
137 136
int wl_send_global_packet(char group, char type, char* data, int len, char frame);
138 137
/**@brief Send a packet to all robots in our PAN **/
139
void wl_send_pan_packet(char group, char type,
140
		char* data, int len, char frame);
138
void wl_send_pan_packet(char group, char type, char* data, int len, char frame);
141 139

  
142 140
/**@brief Set the PAN we are using **/
143
void wl_set_pan(int pan);
141
int wl_set_pan(int pan);
144 142
/**@brief Get the PAN we are using **/
145 143
int wl_get_pan(void);
146 144
/**@brief Set the channel we are using **/
147
void wl_set_channel(int channel);
145
int wl_set_channel(int channel);
148 146
/**@brief Get the channel we are using **/
149 147
int wl_get_channel(void);
150 148
/**@brief Get the 16-bit address of the XBee module **/
trunk/code/projects/libwireless/lib/xbee.c
70 70

  
71 71
/*I/O Functions*/
72 72
static int xbee_send(char* buf, int size);
73
static void xbee_send_string(char* c);
73
static int xbee_send_string(char* c);
74 74

  
75 75
#ifndef ROBOT
76
void xbee_read(char* buf, int size);
76
static int xbee_read(char* buf, int size);
77 77
#endif
78 78

  
79 79
/*Command Mode Functions
80 80
 * Called during initialization.
81 81
 */
82
static void xbee_enter_command_mode(void);
83
static void xbee_exit_command_mode(void);
84
static void xbee_enter_api_mode(void);
85
static void xbee_exit_api_mode(void);
82
static int xbee_enter_command_mode(void);
83
static int xbee_exit_command_mode(void);
84
static int xbee_enter_api_mode(void);
85
static int xbee_exit_api_mode(void);
86 86
static void xbee_wait_for_string(char* s, int len);
87 87
static void xbee_wait_for_ok(void);
88 88

  
89 89
/*API Mode Functions*/
90 90

  
91
int xbee_handle_packet(char* packet, int len);
92
void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen);
93
void xbee_handle_status(char status);
94
int xbee_verify_checksum(char* packet, int len);
95
char xbee_compute_checksum(char* packet, int len);
96
void xbee_send_frame(char* buf, int len);
97
void xbee_send_read_at_command(char* command);
98
void xbee_send_modify_at_command(char* command, char* value);
91
static int xbee_handle_packet(char* packet, int len);
92
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen);
93
static void xbee_handle_status(char status);
94
static int xbee_verify_checksum(char* packet, int len);
95
static char xbee_compute_checksum(char* packet, int len);
96
static int xbee_send_frame(char* buf, int len);
97
static int xbee_send_read_at_command(char* command);
98
static int xbee_send_modify_at_command(char* command, char* value);
99 99

  
100 100
/*Global Variables*/
101 101

  
......
116 116

  
117 117

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

  
122 122
//XBee status
123
unsigned int xbee_panID = XBEE_PAN_DEFAULT;
124
unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
125
int xbee_channel = XBEE_CHANNEL_DEFAULT;
126
int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
127
unsigned int xbee_address = 0;
123
static unsigned int xbee_panID = XBEE_PAN_DEFAULT;
124
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
125
static int xbee_channel = XBEE_CHANNEL_DEFAULT;
126
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
127
static unsigned int xbee_address = 0;
128 128

  
129 129
/*Function Implementations*/
130 130

  
......
166 166

  
167 167
#else
168 168

  
169
// Computer code
170

  
169 171
/**
170 172
 * Thread that listens to the xbee.
171 173
 **/
172
void* listen_to_xbee(void* x)
174
static void* listen_to_xbee(void* x)
173 175
{
174 176
	char c;
175 177
	while (1)
176 178
	{
177
		xbee_read(&c, 1);
179
		if (xbee_read(&c, 1) != 0) {
180
			fprintf(stderr, "xbee_read failed.\n");
181
			return NULL;
182
		}
183

  
178 184
		arrival_buf[buffer_last] = c;
179 185
		int t = buffer_last + 1;
180 186
		if (t == XBEE_BUFFER_SIZE)
......
187 193

  
188 194
		usleep(1000);
189 195
	}
190
	return 0;
196

  
197
	return NULL;
191 198
}
192 199

  
193 200
#endif
......
258 265
	}
259 266
#endif
260 267

  
261
	xbee_enter_command_mode();
262
	xbee_enter_api_mode();
263
	xbee_exit_command_mode();
264
	xbee_send_read_at_command("MY");
268
	if (xbee_enter_command_mode() != 0) {
269
		return -1;
270
	}
265 271

  
272
	if (xbee_enter_api_mode() != 0) {
273
		return -1;
274
	}
275

  
276
	if (xbee_exit_command_mode() != 0) {
277
		return -1;
278
	}
279

  
280
	if (xbee_send_read_at_command("MY")) {
281
		return -1;
282
	}
283

  
266 284
	//wait to return until the address is set
267 285
	while (xbee_address == 0) xbee_get_packet(NULL);
268 286

  
......
289 307
 * @param buf the buffer of data to send
290 308
 * @param size the number of bytes to send
291 309
 **/
292
int xbee_send(char* buf, int size)
310
static int xbee_send(char* buf, int size)
293 311
{
294
	#ifdef ROBOT
312
#ifdef ROBOT
295 313
	int i;
296
	for (i = 0; i < size; i++)
314
	for (i = 0; i < size; i++) {
297 315
		xbee_putc(buf[i]);
298
	#else
316
	}
317

  
318
	return 0;
319

  
320
#else
321

  
299 322
	int ret = write(xbee_stream, buf, size);
300 323
	//success
301 324
	if (ret == size)
......
313 336
	}
314 337

  
315 338
	//write was interrupted after writing ret bytes
316
	xbee_send(buf + ret, size - ret);
317
	#endif
318

  
319
	return 0;
339
	return xbee_send(buf + ret, size - ret);
340
#endif
320 341
}
321 342

  
322 343
/**
......
324 345
 *
325 346
 * @param c the string to send to the XBEE
326 347
 **/
327
void xbee_send_string(char* c)
348
static int xbee_send_string(char* c)
328 349
{
329
	xbee_send(c, strlen(c));
350
	return xbee_send(c, strlen(c));
330 351
}
331 352

  
332 353
#ifndef ROBOT
333
void xbee_read(char* buf, int size)
354
static int xbee_read(char* buf, int size)
334 355
{
335
	if (read(xbee_stream, buf, size) == -1)
356
	if (read(xbee_stream, buf, size) == -1) {
336 357
		printf("Failed to read from xbee.\r\n");
358
		return -1;
359
	}
360

  
361
	return 0;
337 362
}
338 363
#endif
339 364

  
340 365
/**
341 366
 * Enter into command mode.
342 367
 **/
343
static void xbee_enter_command_mode()
368
static int xbee_enter_command_mode()
344 369
{
345
	xbee_send_string("+++");
370
	if (xbee_send_string("+++") != 0) {
371
		return -1;
372
	}
373

  
346 374
	xbee_wait_for_ok();
375

  
376
	return 0;
347 377
}
348 378

  
349 379
/**
350 380
 * Exit from command mode.
351 381
 **/
352
static void xbee_exit_command_mode()
382
static int xbee_exit_command_mode()
353 383
{
354
	xbee_send_string("ATCN\r");
384
	if (xbee_send_string("ATCN\r") != 0) {
385
		return -1;
386
	}
387

  
355 388
	xbee_wait_for_ok();
389

  
390
	return 0;
356 391
}
357 392

  
358 393
/**
359 394
 * Enter API mode.
360 395
 **/
361
static void xbee_enter_api_mode()
396
static int xbee_enter_api_mode()
362 397
{
363
	xbee_send_string("ATAP 1\r");
398
	if (xbee_send_string("ATAP 1\r") != 0) {
399
		return -1;
400
	}
364 401
	xbee_wait_for_ok();
402

  
403
	return 0;
365 404
}
366 405

  
367 406
/**
368 407
 * Exit API mode. (warning - does not check for response)
369 408
 **/
370
static void xbee_exit_api_mode()
409
static int xbee_exit_api_mode()
371 410
{
372
	xbee_send_string("ATAP 0\r");
411
	return xbee_send_string("ATAP 0\r");
373 412
}
374 413

  
375 414
/**
......
460 499
 * @param len the size in bytes of the packet data
461 500
 *
462 501
 **/
463
void xbee_send_frame(char* buf, int len)
502
static int xbee_send_frame(char* buf, int len)
464 503
{
465 504
	char prefix[3];
466 505
	prefix[0] = XBEE_FRAME_START;
467 506
	prefix[1] = (len & 0xFF00) >> 8;
468 507
	prefix[2] = len & 0xFF;
469 508
	char checksum = xbee_compute_checksum(buf, len);
470
	xbee_send(prefix, 3);
471
	xbee_send(buf, len);
472
	xbee_send(&checksum, 1);
509

  
510
	if (xbee_send(prefix, 3) != 0) {
511
		return -1;
512
	}
513
	
514
	if (xbee_send(buf, len) != 0) {
515
		return -1;
516
	}
517

  
518
	if (xbee_send(&checksum, 1) != 0) {
519
		return -1;
520
	}
521

  
522
	return 0;
473 523
}
474 524

  
475 525
/**
......
479 529
 * use ID to read the PAN ID and MY to return the XBee ID.
480 530
 * See the XBee reference guide for a complete listing.
481 531
 **/
482
void xbee_send_read_at_command(char* command)
532
static int xbee_send_read_at_command(char* command)
483 533
{
484
	xbee_send_modify_at_command(command, NULL);
534
	return xbee_send_modify_at_command(command, NULL);
485 535
}
486 536

  
487 537
/**
......
491 541
 * @param value the value to pass as a parameter
492 542
 * (or NULL if there is no parameter)
493 543
 **/
494
void xbee_send_modify_at_command(char* command, char* value)
544
static int xbee_send_modify_at_command(char* command, char* value)
495 545
{
496 546
	char buf[16];
497 547
	int i;
......
507 557
		if (valueLen > 8)
508 558
		{
509 559
			WL_DEBUG_PRINT("AT Command too large.\r\n");
510
			return;
560
			return -1;
511 561
		}
512
		for (i = 0; i < valueLen; i++)
562

  
563
		for (i = 0; i < valueLen; i++) {
513 564
			buf[4 + i] = value[i];
565
		}
514 566
	}
515
	xbee_send_frame(buf, 4 + valueLen);
567

  
568
	return xbee_send_frame(buf, 4 + valueLen);
516 569
}
517 570

  
518 571
/**
......
630 683
			// check if buffer is empty
631 684
			if (buffer_first == buffer_last)
632 685
				return -1;
686
		} while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
687

  
688
		if (buffer_first == XBEE_BUFFER_SIZE) {
689
			buffer_first = 0;
633 690
		}
634
		while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
635
		if (buffer_first == XBEE_BUFFER_SIZE)
636
			buffer_first = 0;
637 691
		xbee_buf[0] = XBEE_FRAME_START;
638 692
		currentBufPos++;
639 693
	}
640 694

  
641 695
	int len = -1;
642
	if (currentBufPos >= 3)
696
	if (currentBufPos >= 3) {
643 697
		len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
698
	}
644 699

  
645 700
	while (len == -1 //packet length has not been read yet
646
			|| currentBufPos < len + 4)
701
		|| currentBufPos < len + 4)
647 702
	{
648 703
		if (currentBufPos == 3)
649 704
		{
......
655 710
				return -1;
656 711
			}
657 712
		}
713

  
658 714
		// check if buffer is empty
659
		if (buffer_first == buffer_last)
715
		if (buffer_first == buffer_last) {
660 716
			return -1;
717
		}
661 718
		xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
662
		if (buffer_first == XBEE_BUFFER_SIZE)
719
		if (buffer_first == XBEE_BUFFER_SIZE) {
663 720
			buffer_first = 0;
721
		}
664 722
	}
665 723

  
666 724
	currentBufPos = 0;
......
672 730
	}
673 731

  
674 732
	//we will take care of the packet
675
	if (xbee_handle_packet(xbee_buf + 3, len))
733
	if (xbee_handle_packet(xbee_buf + 3, len)) {
676 734
		return -1;
735
	}
677 736

  
678
	if (dest == NULL)
737
	if (dest == NULL) {
679 738
		return -1;
739
	}
680 740

  
681 741
	int i;
682
	for (i = 3; i < len + 3; i++)
742
	for (i = 3; i < len + 3; i++) {
683 743
		dest[i - 3] = xbee_buf[i];
744
	}
745

  
684 746
	return len;
685 747
}
686 748

  
......
724 786
 * @param extra the hex value of the requested register
725 787
 * @param extraLen the length in bytes of extra
726 788
 **/
727
void xbee_handle_at_command_response(char* command, char result,
728
	char* extra, int extraLen)
789
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen)
729 790
{
730 791
	if (result == 1)
731 792
	{
......
757 818

  
758 819
	if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0)
759 820
	{
821
//							printf("reading xbee_address\n");
822
		
760 823
		xbee_address = 0;
761 824
		int i;
762
		for (i = 0; i < extraLen; i++)
825
		for (i = 0; i < extraLen; i++) {
763 826
			xbee_address = (xbee_address << 8) + extra[i];
827
		}
828
//							printf("xbee address is: %d\n", xbee_address);
764 829

  
765 830
		WL_DEBUG_PRINT("XBee address is ");
766 831
		WL_DEBUG_PRINT_INT(xbee_address);
......
786 851
 *
787 852
 * @return 1 if we have handled the packet, 0 otherwise
788 853
 */
789
int xbee_handle_packet(char* packet, int len)
854
static int xbee_handle_packet(char* packet, int len)
790 855
{
791 856
	char command[3] = {1, 2, 3};
792 857
	if (len <= 0) //this should not happend
......
804 869
			command[0] = packet[2];
805 870
			command[1] = packet[3];
806 871
			command[2] = 0;
807
			xbee_handle_at_command_response(command,
808
				packet[4], packet + 5, len - 5);
872
			xbee_handle_at_command_response(command, packet[4], packet + 5, len - 5);
809 873
			return 1;
810 874
	}
811 875
	return 0;
......
816 880
 *
817 881
 * @param id the new personal area network (PAN) id
818 882
 **/
819
void xbee_set_pan_id(int id)
883
int xbee_set_pan_id(int id)
820 884
{
821 885
	char s[3];
822 886
	s[0] = (id >> 8) & 0xFF;
823 887
	s[1] = id & 0xFF;
824 888
	s[2] = 0;
825 889
	xbee_pending_panID = id;
826
	xbee_send_modify_at_command("ID", s);
890
	return xbee_send_modify_at_command("ID", s);
827 891
}
828 892

  
829 893
/**
......
845 909
 *
846 910
 * @see xbee_get_channel
847 911
 **/
848
void xbee_set_channel(int channel)
912
int xbee_set_channel(int channel)
849 913
{
850 914
	if (channel < 0x0B || channel > 0x1A)
851 915
	{
852 916
		WL_DEBUG_PRINT("Channel out of range.\r\n");
853
		return;
917
		return -1;
854 918
	}
919

  
855 920
	char s[3];
856 921
	s[0] = channel & 0xFF;
857 922
	s[1] = 0;
858 923
	xbee_pending_channel = channel;
859
	xbee_send_modify_at_command("CH", s);
924

  
925
	return xbee_send_modify_at_command("CH", s);
860 926
}
861 927

  
862 928
/**
trunk/code/projects/libwireless/lib/wl_token_ring.h
47 47
 **/
48 48

  
49 49
/**@brief Register the token ring group with the wireless library.**/
50
void wl_token_ring_register(void);
50
int wl_token_ring_register(void);
51 51
/**@brief Unregister the token ring group with the wirelss library.**/
52 52
void wl_token_ring_unregister(void);
53 53
/**@brief Set the functions called to turn the bom on and off.**/
trunk/code/projects/libwireless/lib/xbee.h
74 74
#define XBEE_RX 0x81
75 75

  
76 76
/**@brief Initialize the XBee library **/
77
int xbee_lib_init();
77
int xbee_lib_init(void);
78 78
/**@brief Uninitialize the XBee library **/
79 79
void xbee_terminate(void);
80 80
/**@brief Get a packet from the XBee **/
......
82 82
/**@brief Send a packet to the XBee **/
83 83
int xbee_send_packet(char* packet, int len, int dest, char options, char frame);
84 84
/**@brief Set the PAN ID for the XBee **/
85
void xbee_set_pan_id(int id);
85
int xbee_set_pan_id(int id);
86 86
/**@brief Get the XBee's PAN ID **/
87 87
unsigned int xbee_get_pan_id(void);
88 88
/**@brief Set the channel the XBee is currently using **/
89
void xbee_set_channel(int channel);
89
int xbee_set_channel(int channel);
90 90
/**@brief Get the channel the XBee is currently using **/
91 91
int xbee_get_channel(void);
92 92
/**@brief Get the XBee's 16-bit address **/
trunk/code/projects/libwireless/lib/sensor_matrix.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
......
41 41
#define DEFAULT_SENSOR_MATRIX_SIZE 20
42 42

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

  
46 46
/**
47 47
 * Initializes the sensor matrix.
......
52 52
{
53 53
	SensorMatrix* m;
54 54
	int i;
55
	
55

  
56 56
	m = (SensorMatrix*)malloc(sizeof(SensorMatrix));
57 57
	if (!m)
58 58
	{
......
79 79
		WL_DEBUG_PRINT("Out of memory - create sensor matrix 2.\r\n");
80 80
		return NULL;
81 81
	}
82
	
82

  
83 83
	for (i = 0; i < m->size; i++)
84 84
	{
85 85
		m->matrix[i] = NULL;
......
106 106

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

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

  
121 124
	m->matrix[id] = (int*)malloc(m->size * sizeof(int));
122 125
	if (!(m->matrix[id]))
123 126
	{
......
125 128
		return;
126 129
	}
127 130

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

  
133 138
/**
......
146 151
		WL_DEBUG_PRINT("Removing robot not added to matrix.\r\n");
147 152
		return;
148 153
	}
149
	
154

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

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

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

  
......
165 170
 * @param size the new size of the sensor matrix
166 171
 **/
167 172
//Note: this has probably not been tested, hopefully it works
168
void sensor_matrix_expand(SensorMatrix* m, int nextSize)
173
static void sensor_matrix_expand(SensorMatrix* m, int nextSize)
169 174
{
170 175
	int i, j;
171 176
	WL_DEBUG_PRINT("Expanding sensor matrix.\r\n");
172
	
177

  
173 178
	int** tempMatrix = (int**)malloc(nextSize * sizeof(int*));
174 179
	if (!tempMatrix)
175 180
	{
176 181
		WL_DEBUG_PRINT("Out of memory - expand matrix.\r\n");
177 182
		return;
178 183
	}
179
	
184

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

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

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

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

  
201 209
	free(m->matrix);
202 210
	m->matrix = tempMatrix;
203 211
	m->size = nextSize;
......
209 217
		WL_DEBUG_PRINT("Out of memory - expand matrix 3.\r\n");
210 218
		return;
211 219
	}
212
	
213
	for (i = 0; i < m->size; i++)
220

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

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

  
218 229
	free(m->joined);
219 230
	m->joined = tempJoined;
220 231
}
221 232

  
222 233
/**
223 234
 * Sets the sensor reading for robot robot to reading.
224
 * 
235
 *
225 236
 * @param m the sensor matrix to set the reading for
226 237
 * @param observer the id of the robot who made the reading
227 238
 * @param robot the id of the robot who the reading is for
......
229 240
 */
230 241
void sensor_matrix_set_reading(SensorMatrix* m, int observer, int robot, int reading)
231 242
{
232
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL)
243
	if (robot >= m->size || observer >= m->size || m->matrix[observer] == NULL) {
233 244
		sensor_matrix_add_robot(m, observer);
245
	}
246

  
234 247
	m->matrix[observer][robot] = reading;
235 248
}
236 249

  
......
245 258
 **/
246 259
int sensor_matrix_get_reading(SensorMatrix* m, int observer, int robot)
247 260
{
248
	if (observer >= m->size || robot >= m->size)
261
	if (observer >= m->size || robot >= m->size) {
249 262
		return -1;
263
	}
264

  
250 265
	return m->matrix[observer][robot];
251 266
}
252 267

  
......
259 274
 **/
260 275
void sensor_matrix_set_in_ring(SensorMatrix* m, int robot, int in)
261 276
{
262
	if (robot >= m->size)
277
	if (robot >= m->size) {
263 278
		sensor_matrix_expand(m, robot + 1);
264
	if (in == 1)
279
	}
280

  
281
	if (in == 1) {
265 282
		sensor_matrix_add_robot(m, robot);
266
	if (in == 1 && m->joined[robot] == 0)
283
	}
284

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

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

  
270 293
	m->joined[robot] = in;
271 294
}
272 295

  
273 296
/**
274 297
 * Checks if the given robot is in the token ring.
275
 * 
298
 *
276 299
 * @param m the sensor matrix
277 300
 * @param robot the ID of the robot to check
278 301
 *
......
280 303
 **/
281 304
int sensor_matrix_get_in_ring(SensorMatrix* m, int robot)
282 305
{
283
	if (robot >= m->size)
306
	if (robot >= m->size) {
284 307
		return -1;
308
	}
309

  
285 310
	return m->joined[robot];
286 311
}
287 312

  
......
289 314
 * Returns the size of the sensor matrix.
290 315
 *
291 316
 * @param m the sensor matrix
292
 * 
317
 *
293 318
 * @return the size of the sensor matrix
294 319
 **/
295 320
int sensor_matrix_get_size(SensorMatrix* m)
......
309 334
{
310 335
	return m->numJoined;
311 336
}
312

  
trunk/code/projects/libwireless/lib/wireless.c
168 168
 *
169 169
 * @see wl_get_pan
170 170
 **/
171
void wl_set_pan(int pan)
171
int wl_set_pan(int pan)
172 172
{
173
	xbee_set_pan_id(pan);
173
	return xbee_set_pan_id(pan);
174 174
}
175 175

  
176 176
/**
......
192 192
 *
193 193
 * @see wl_get_channel
194 194
 **/
195
void wl_set_channel(int channel)
195
int wl_set_channel(int channel)
196 196
{
197
	xbee_set_channel(channel);
197
	return xbee_set_channel(channel);
198 198
}
199 199

  
200 200
/**
......
229 229
 * @param dest the 16-bit address of the XBee to send the packet to
230 230
 * @param frame the frame number to see with a TX_STATUS response
231 231
 **/
232
void wl_send_robot_to_robot_global_packet(char group, char type,
233
		char* data, int len, int dest, char frame)
232
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame)
234 233
{
235
	wl_send_packet(group, type, data, len, dest,
236
			XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
234
	return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
237 235
}
238 236

  
239 237
/**
......
246 244
 * @param dest the 16-bit address of the XBee to send the packet to
247 245
 * @param frame the frame number to see with a TX_STATUS response
248 246
 **/
249
void wl_send_robot_to_robot_packet(char group, char type,
250
		char* data, int len, int dest, char frame)
247
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame)
251 248
{
252
	wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
253
			frame);
249
	return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE, frame);
254 250
}
255 251

  
256 252
/**
......
388 384
		//this only works with under 16 groups
389 385
		int group = (int)(wl_buf[1] >> 4);
390 386
		int success = 0;
391
		if (wl_buf[2] == 0)
387
		if (wl_buf[2] == 0) {
392 388
			success = 1;
389
		}
393 390
		else
394 391
		{
395 392
			WL_DEBUG_PRINT("No response received.\r\n");

Also available in: Unified diff