Project

General

Profile

Revision 743

Fixed compilation errors.

View differences:

xbee.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
......
55 55
#include <string.h>
56 56

  
57 57
#define XBEE_FRAME_START 0x7E
58
#define XBEE_GET_PACKET_TIMEOUT 1000
58 59

  
59 60
/*Frame Types*/
60 61
#define XBEE_FRAME_STATUS 0x8A
......
69 70
/*Internal Function Prototypes*/
70 71

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

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

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

  
89 90
/*API Mode Functions*/
90 91

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

  
101 101
/*Global Variables*/
102 102

  
103 103
#ifndef ROBOT
104
char* xbee_com_port = XBEE_PORT_DEFAULT;
105
int xbee_stream;
106
pthread_t* xbee_listen_thread;
104
static char* xbee_com_port = XBEE_PORT_DEFAULT;
105
static int xbee_stream;
106
static pthread_t* xbee_listen_thread;
107 107
#endif
108 108

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

  
118 119

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

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

  
130 131
/*Function Implementations*/
131 132

  
......
145 146
		t = 0;
146 147
	if (t == buffer_first)
147 148
	{
148
		WL_DEBUG_PRINT("Out of space in buffer.\n");
149
		WL_DEBUG_PRINT("\nOut of space in buffer.\n");
149 150
	}
150 151
	buffer_last = t;
151 152
}
......
167 168

  
168 169
#else
169 170

  
171
// Computer code
172

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

  
179 186
		arrival_buf[buffer_last] = c;
180 187
		int t = buffer_last + 1;
181 188
		if (t == XBEE_BUFFER_SIZE)
......
185 192
			WL_DEBUG_PRINT("Out of space in buffer.\n");
186 193
		}
187 194
		buffer_last = t;
195

  
196
		usleep(1000);
188 197
	}
189
	return 0;
198

  
199
	return NULL;
190 200
}
191 201

  
192 202
#endif
......
194 204
/**
195 205
 * Initializes the XBee library so that other functions may be used.
196 206
 **/
197
int xbee_lib_init(void)
207
int xbee_lib_init()
198 208
{
199
	arrival_buf[0] = 'A';
200
	arrival_buf[1] = 'A';
201
	arrival_buf[2] = 'A';
202
	#ifdef ROBOT
209
#ifdef ROBOT
203 210

  
204 211
	//enable the receiving interrupt
205
	#ifdef FIREFLY
212
#ifdef FIREFLY
206 213
	UCSR0B |= _BV(RXCIE) | _BV(RXEN);
207
	#else
214
#else
208 215
	UCSR1B |= _BV(RXCIE);
209
	#endif
216
#endif
210 217
	sei();
211
	#else
212
	printf("Connecting to port %s.\n", xbee_com_port);
218
#else
213 219
	xbee_stream = open(xbee_com_port, O_RDWR);
214 220
	if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/)
215 221
	{
216
		printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
222
		WL_DEBUG_PRINT("Failed to open connection to XBee on port ");
223
		WL_DEBUG_PRINT_INT(xbee_com_port);
224
		WL_DEBUG_PRINT(".\n");
217 225
		return -1;
226
	} else {
227
	  WL_DEBUG_PRINT("Successfully opened connection to XBee on port ");
228
		WL_DEBUG_PRINT_INT(xbee_com_port);
229
		WL_DEBUG_PRINT(".\n");
218 230
	}
219
	
231

  
220 232
	// set baud rate, etc. correctly
221 233
	struct termios options;
222 234

  
......
236 248

  
237 249
	if (tcsetattr(xbee_stream, TCSANOW, &options))
238 250
	{
239
		fprintf(stderr, "Error setting attributes.\n");
251
		WL_DEBUG_PRINT("Error setting attributes.\n");
240 252
		return -1;
241 253
	}
242 254

  
243
	//lockf(xbee_stream, F_LOCK, 0);
244
	
245
	xbee_listen_thread =
246
		(pthread_t*)malloc(sizeof(pthread_t));
255
	xbee_listen_thread = (pthread_t*)malloc(sizeof(pthread_t));
247 256
	if (xbee_listen_thread == NULL)
248 257
	{
249
		fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__);
258
		WL_DEBUG_PRINT("Malloc failed.\n");
250 259
		return -1;
251 260
	}
252
	
253
	int ret = pthread_create(xbee_listen_thread, NULL,
254
		listen_to_xbee, NULL);
261

  
262
	int ret = pthread_create(xbee_listen_thread, NULL, listen_to_xbee, NULL);
255 263
	if (ret)
256 264
	{
257
		fprintf(stderr, "Failed to create listener thread.\r\n");
265
		WL_DEBUG_PRINT("Failed to create listener thread.\n");
258 266
		return -1;
259 267
	}
260
	
261
	#endif
262
	xbee_enter_command_mode();
263
	xbee_enter_api_mode();
264
	xbee_exit_command_mode();
265
	xbee_send_read_at_command("MY");
266
	
268
#endif
269

  
270

  
271
	if (xbee_enter_command_mode() != 0) {
272
		return -1;
273
	}
274

  
275
	if (xbee_enter_api_mode() != 0) {
276
		return -1;
277
	}
278

  
279
	if (xbee_exit_command_mode() != 0) {
280
		return -1;
281
	}
282

  
283
	if (xbee_send_read_at_command("MY")) {
284
		return -1;
285
	}
286

  
287
#ifndef ROBOT
288
	int i;
289
	for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) {
290
	  ret = xbee_get_packet(NULL);
291

  
292
	  usleep(1000);
293

  
294
	  if (ret == -1) {
295
	    WL_DEBUG_PRINT("xbee_get_packet(NULL) failed.\n");
296
	    return -1;
297
	  }
298
	}
299
#else
267 300
	//wait to return until the address is set
268
	while (xbee_address == 0) xbee_get_packet(NULL);
301
  //TODO: this shouldn't wait indefinitely.  There should be some sort of reasonable timeout
302
  // so if the address is never set right, an error can be returned instead of having the
303
  // robot hang forever
304
	while (xbee_address == 0) {
305
	  xbee_get_packet(NULL);
306
	}
307
#endif
269 308

  
309
#ifndef ROBOT
310
	if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out.
270 311

  
312
	  WL_DEBUG_PRINT("xbee_get_packet timed out.\n");
313
	  return -1;
314
	} else {
315
	  return 0;
316
	}
317
#else
271 318
	return 0;
319
#endif
272 320
}
273 321

  
274 322
/**
......
287 335

  
288 336
/**
289 337
 * Send a buffer buf of size bytes to the XBee.
290
 * 
338
 *
291 339
 * @param buf the buffer of data to send
292 340
 * @param size the number of bytes to send
293 341
 **/
294
void xbee_send(char* buf, int size)
342
static int xbee_send(char* buf, int size)
295 343
{
296
	#ifdef ROBOT
344
#ifdef ROBOT
297 345
	int i;
298
	for (i = 0; i < size; i++)
346
	for (i = 0; i < size; i++) {
299 347
		xbee_putc(buf[i]);
300
	#else
348
	}
349

  
350
	return 0;
351

  
352
#else
353

  
301 354
	int ret = write(xbee_stream, buf, size);
302 355
	//success
303 356
	if (ret == size)
304
		return;
357
		return 0;
305 358
	if (ret == -1)
306 359
	{
307 360
		//interrupted by system signal, probably timer interrupt.
308 361
		//just try again
309 362
		if (errno == 4)
310 363
		{
311
			xbee_send(buf, size);
312
			return;
364
			return xbee_send(buf, size);
313 365
		}
314
		printf("Failed to write to xbee, error %i.\r\n", errno);
315
		return;
366
		WL_DEBUG_PRINT("Failed to write to xbee, error %i.\r\n", errno);
367
		return -1;
316 368
	}
317 369

  
318 370
	//write was interrupted after writing ret bytes
319
	xbee_send(buf + ret, size - ret);
320
	#endif
371
	return xbee_send(buf + ret, size - ret);
372
#endif
321 373
}
322 374

  
323 375
/**
......
325 377
 *
326 378
 * @param c the string to send to the XBEE
327 379
 **/
328
void xbee_send_string(char* c)
380
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
381
// it reduces code size or not should be done to be sure.
382
static int xbee_send_string(char* c)
329 383
{
330
	xbee_send(c, strlen(c));
384
	return xbee_send(c, strlen(c));
331 385
}
332 386

  
333 387
#ifndef ROBOT
334
void xbee_read(char* buf, int size)
388
static int xbee_read(char* buf, int size)
335 389
{
336
	if (read(xbee_stream, buf, size) == -1)
337
		printf("Failed to read from xbee.\r\n");
390
	if (read(xbee_stream, buf, size) == -1) {
391
		WL_DEBUG_PRINT("Failed to read from xbee.\r\n");
392
		return -1;
393
	}
394

  
395
	return 0;
338 396
}
339 397
#endif
340 398

  
341 399
/**
342 400
 * Enter into command mode.
343 401
 **/
344
void xbee_enter_command_mode()
402
static int xbee_enter_command_mode()
345 403
{
346
	xbee_send_string("+++");
347
	xbee_wait_for_ok();
404
	if (xbee_send_string("+++") != 0) {
405
		return -1;
406
	}
407

  
408
	if (xbee_wait_for_ok() != 0) {
409
	  return -1;
410
	}
411
	  return 0;
348 412
}
349 413

  
350 414
/**
351 415
 * Exit from command mode.
352 416
 **/
353
void xbee_exit_command_mode()
417
static int xbee_exit_command_mode()
354 418
{
355
	xbee_send_string("ATCN\r");
419
	if (xbee_send_string("ATCN\r") != 0) {
420
		return -1;
421
	}
422

  
356 423
	xbee_wait_for_ok();
424

  
425
	return 0;
357 426
}
358 427

  
359 428
/**
360 429
 * Enter API mode.
361 430
 **/
362
void xbee_enter_api_mode()
431
static int xbee_enter_api_mode()
363 432
{
364
	xbee_send_string("ATAP 1\r");
433
	if (xbee_send_string("ATAP 1\r") != 0) {
434
		return -1;
435
	}
365 436
	xbee_wait_for_ok();
366
}
367 437

  
368
/**
369
 * Exit API mode. (warning - does not check for response)
370
 **/
371
void xbee_exit_api_mode()
372
{
373
	xbee_send_string("ATAP 0\r");
438
	return 0;
374 439
}
375 440

  
376 441
/**
377 442
 * Wait until the string "OK\r" is received from the XBee.
378 443
 **/
379
void xbee_wait_for_ok()
444
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
445
// it reduces code size or not should be done to be sure.
446
static int xbee_wait_for_ok()
380 447
{
381
	xbee_wait_for_string("OK\r", 3);
448
	return xbee_wait_for_string("OK\r", 3);
382 449
}
383 450

  
384 451
/**
......
388 455
 * @param s the string to receive
389 456
 * @param len the length of the string
390 457
 **/
391
void xbee_wait_for_string(char* s, int len)
458
static int xbee_wait_for_string(char* s, int len)
392 459
{
393 460
	char* curr = s;
394
	while (curr - s < len)
395
	{
461
	while (curr - s < len) {
396 462
		// check if buffer is empty
397
		if (buffer_last == buffer_first)
398
			continue;
399
		char c = arrival_buf[buffer_first++];
400
		if (buffer_first == XBEE_BUFFER_SIZE)
401
			buffer_first = 0;
402
		if (c == *curr)
403
			curr++;
404
		else
405
			curr = s;
463
		if (buffer_last != buffer_first) {
464
			char c = arrival_buf[buffer_first++];
465
			if (buffer_first == XBEE_BUFFER_SIZE) {
466
				buffer_first = 0;
467
			}
468

  
469
			if (c == *curr) {
470
				curr++;
471
			} else {
472
#ifndef ROBOT
473
			  //return -1; // Computer is less forgiving.
474
			  curr = s;
475
#else
476
			  curr = s;
477
#endif
478
			}
479
		} // else buffer is empty.
480

  
481
#ifndef ROBOT
482
		usleep(100);
483
#endif
406 484
	}
485

  
486
	return 0;
407 487
}
408 488

  
409 489
/**
......
455 535
 * @param len the size in bytes of the packet data
456 536
 *
457 537
 **/
458
void xbee_send_frame(char* buf, int len)
538
static int xbee_send_frame(char* buf, int len)
459 539
{
460 540
	char prefix[3];
461 541
	prefix[0] = XBEE_FRAME_START;
462 542
	prefix[1] = (len & 0xFF00) >> 8;
463 543
	prefix[2] = len & 0xFF;
464 544
	char checksum = xbee_compute_checksum(buf, len);
465
	xbee_send(prefix, 3);
466
	xbee_send(buf, len);
467
	xbee_send(&checksum, 1);
545

  
546
	if (xbee_send(prefix, 3) != 0) {
547
		return -1;
548
	}
549

  
550
	if (xbee_send(buf, len) != 0) {
551
		return -1;
552
	}
553

  
554
	if (xbee_send(&checksum, 1) != 0) {
555
		return -1;
556
	}
557

  
558
	return 0;
468 559
}
469 560

  
470 561
/**
......
474 565
 * use ID to read the PAN ID and MY to return the XBee ID.
475 566
 * See the XBee reference guide for a complete listing.
476 567
 **/
477
void xbee_send_read_at_command(char* command)
568
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
569
// it reduces code size or not should be done to be sure.
570
static int xbee_send_read_at_command(char* command)
478 571
{
479
	xbee_send_modify_at_command(command, NULL);
572
	return xbee_send_modify_at_command(command, NULL);
480 573
}
481 574

  
482 575
/**
......
486 579
 * @param value the value to pass as a parameter
487 580
 * (or NULL if there is no parameter)
488 581
 **/
489
void xbee_send_modify_at_command(char* command, char* value)
582
static int xbee_send_modify_at_command(char* command, char* value)
490 583
{
491 584
	char buf[16];
492 585
	int i;
493
	
586

  
494 587
	buf[0] = XBEE_FRAME_AT_COMMAND;
495 588
	buf[1] = 1;
496 589
	buf[2] = command[0];
......
502 595
		if (valueLen > 8)
503 596
		{
504 597
			WL_DEBUG_PRINT("AT Command too large.\r\n");
505
			return;
598
			return -1;
506 599
		}
507
		for (i = 0; i < valueLen; i++)
600

  
601
		for (i = 0; i < valueLen; i++) {
508 602
			buf[4 + i] = value[i];
603
		}
509 604
	}
510
	xbee_send_frame(buf, 4 + valueLen);
605

  
606
	return xbee_send_frame(buf, 4 + valueLen);
511 607
}
512 608

  
513 609
/**
514 610
 * Send the specified packet.
515
 * 
611
 *
516 612
 * @param packet the packet data to send
517 613
 * @param len the number of bytes in the packet
518
 * 
614
 *
519 615
 * @param dest the ID of the XBee to send the packet to,
520 616
 * or XBEE_BROADCAST to send the message to all robots
521 617
 * in the PAN.
522
 * 
618
 *
523 619
 * @param options a combination of the flags
524
 * XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and 
620
 * XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and
525 621
 * XBEE_OPTIONS_BROADCAST_ALL_PANS
526 622
 *
527 623
 * @param frame the frame number to associate this packet
......
529 625
 * the XBee alerts us as to whether or not our message
530 626
 * was received.
531 627
 **/
532
void xbee_send_packet(char* packet, int len, int dest,
533
	char options, char frame)
628
int xbee_send_packet(char* packet, int len, int dest, char options, char frame)
534 629
{
535 630
	char buf[5];
536 631
	char prefix[3];
......
540 635
	if (len > 100)
541 636
	{
542 637
		WL_DEBUG_PRINT("Packet is too large.\r\n");
543
		return;
638
		return -1;
544 639
	}
545 640

  
546 641
	//data for sending request
......
560 655
	for (i = 0; i < len; i++)
561 656
		checksum += (unsigned char)packet[i];
562 657
	checksum = 0xFF - checksum;
563
	xbee_send(prefix, 3);
564
	xbee_send(buf, 5);
565
	xbee_send(packet, len);
566
	xbee_send((char*)&checksum, 1);
658

  
659
	if (xbee_send(prefix, 3) != 0) {
660
		return -1;
661
	}
662

  
663
	if (xbee_send(buf, 5) != 0) {
664
		return -1;
665
	}
666

  
667
	if (xbee_send(packet, len) != 0) {
668
		return -1;
669
	}
670

  
671
	if (xbee_send((char*)&checksum, 1) != 0) {
672
		return -1;
673
	}
674

  
675
	return 0;
567 676
}
568 677

  
569 678
/**
......
578 687
 *
579 688
 * The first byte of the packet will be either
580 689
 * XBEE_TX_STATUS or XBEE_RX to indicated
581
 * a response to a sent message or a received message, 
690
 * a response to a sent message or a received message,
582 691
 * respectively.<br><br>
583 692
 *
584 693
 * For a status response packet:<br>
585 694
 * The first byte will be XBEE_TX_STATUS.<br>
586 695
 * The second byte will be the frame number.<br>
587 696
 * The third byte will be the result. 0 indicates success,
588
 * and nonzero indicates that an error ocurred in 
697
 * and nonzero indicates that an error ocurred in
589 698
 * transmitting the packet.<br><br>
590 699
 *
591 700
 * For a received packet:<br>
......
595 704
 * The fourth byte is the signal strength.<br>
596 705
 * The fifth byte is 1 if the packet were sent to
597 706
 * a specific address, and 2 if it is a broadcast packet.<br><br>
598
 * 
707
 *
599 708
 * @param dest set to the packet data
600 709
 * @return the length of the packet, or -1 if no packet
601 710
 * is available
......
610 719
			if (buffer_first == XBEE_BUFFER_SIZE)
611 720
				buffer_first = 0;
612 721
			// check if buffer is empty
613
			if (buffer_first == buffer_last)
614
				return -1;
722
			if (buffer_first == buffer_last) {
723
				return 0;
724
			}
725
		} while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
726

  
727
		if (buffer_first == XBEE_BUFFER_SIZE) {
728
			buffer_first = 0;
615 729
		}
616
		while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
617
		if (buffer_first == XBEE_BUFFER_SIZE)
618
			buffer_first = 0;
619 730
		xbee_buf[0] = XBEE_FRAME_START;
620 731
		currentBufPos++;
621 732
	}
622 733

  
623 734
	int len = -1;
624
	if (currentBufPos >= 3)
735
	if (currentBufPos >= 3) {
625 736
		len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
626
		
737
	}
738

  
627 739
	while (len == -1 //packet length has not been read yet
628
			|| currentBufPos < len + 4)
740
		|| currentBufPos < len + 4)
629 741
	{
630 742
		if (currentBufPos == 3)
631 743
		{
......
637 749
				return -1;
638 750
			}
639 751
		}
752

  
640 753
		// check if buffer is empty
641
		if (buffer_first == buffer_last)
642
			return -1;
754
		if (buffer_first == buffer_last) {
755
			return 0;
756
		}
643 757
		xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
644
		if (buffer_first == XBEE_BUFFER_SIZE)
758
		if (buffer_first == XBEE_BUFFER_SIZE) {
645 759
			buffer_first = 0;
760
		}
646 761
	}
647
	
762

  
648 763
	currentBufPos = 0;
649
	
764

  
650 765
	if (!xbee_verify_checksum(xbee_buf, len + 4))
651 766
	{
652 767
		WL_DEBUG_PRINT("XBee checksum failed.\r\n");
......
654 769
	}
655 770

  
656 771
	//we will take care of the packet
657
	if (xbee_handle_packet(xbee_buf + 3, len))
658
		return -1;
659
	
660
	if (dest == NULL)
661
		return -1;
662
	
772
	if (xbee_handle_packet(xbee_buf + 3, len) != 0) {
773
		return 0;
774
	}
775

  
776
	if (dest == NULL) {
777
		return 0;
778
	}
779

  
663 780
	int i;
664
	for (i = 3; i < len + 3; i++)
781
	for (i = 3; i < len + 3; i++) {
665 782
		dest[i - 3] = xbee_buf[i];
783
	}
784

  
666 785
	return len;
667 786
}
668 787

  
......
706 825
 * @param extra the hex value of the requested register
707 826
 * @param extraLen the length in bytes of extra
708 827
 **/
709
void xbee_handle_at_command_response(char* command, char result,
710
	char* extra, int extraLen)
828
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen)
711 829
{
712 830
	if (result == 1)
713 831
	{
......
718 836
	WL_DEBUG_PRINT("AT");
719 837
	WL_DEBUG_PRINT(command);
720 838
	WL_DEBUG_PRINT(" command was successful.\r\n");
721
		
839

  
722 840
	if (command[0] == 'I' && command[1] == 'D')
723 841
	{
724 842
		xbee_panID = xbee_pending_panID;
......
736 854
		WL_DEBUG_PRINT(".\r\n");
737 855
		return;
738 856
	}
739
	
857

  
740 858
	if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0)
741 859
	{
742 860
		xbee_address = 0;
743 861
		int i;
744
		for (i = 0; i < extraLen; i++)
862
		for (i = 0; i < extraLen; i++) {
745 863
			xbee_address = (xbee_address << 8) + extra[i];
864
		}
746 865

  
747 866
		WL_DEBUG_PRINT("XBee address is ");
748 867
		WL_DEBUG_PRINT_INT(xbee_address);
......
751 870
		if (xbee_address == 0)
752 871
		{
753 872
			WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n");
873
			#ifndef ROBOT
754 874
			exit(0);
875
			#endif
755 876
		}
756 877
	}
757 878
}
......
765 886
 *
766 887
 * @param packet the packet to handle
767 888
 * @param len the length of the packet
768
 * 
889
 *
769 890
 * @return 1 if we have handled the packet, 0 otherwise
770 891
 */
771
int xbee_handle_packet(char* packet, int len)
892
static int xbee_handle_packet(char* packet, int len)
772 893
{
894

  
773 895
	char command[3] = {1, 2, 3};
774 896
	if (len <= 0) //this should not happend
775 897
	{
776 898
		WL_DEBUG_PRINT("Non-positive packet length.\r\n");
777 899
		return 0;
778 900
	}
779
	
901

  
780 902
	switch ((unsigned char)packet[0]) //packet type
781 903
	{
782 904
		case XBEE_FRAME_STATUS:
......
786 908
			command[0] = packet[2];
787 909
			command[1] = packet[3];
788 910
			command[2] = 0;
789
			xbee_handle_at_command_response(command,
790
				packet[4], packet + 5, len - 5);
911
			xbee_handle_at_command_response(command, packet[4], packet + 5, len - 5);
791 912
			return 1;
792 913
	}
793 914
	return 0;
......
798 919
 *
799 920
 * @param id the new personal area network (PAN) id
800 921
 **/
801
void xbee_set_pan_id(int id)
922
int xbee_set_pan_id(int id)
802 923
{
803 924
	char s[3];
804 925
	s[0] = (id >> 8) & 0xFF;
805 926
	s[1] = id & 0xFF;
806 927
	s[2] = 0;
807 928
	xbee_pending_panID = id;
808
	xbee_send_modify_at_command("ID", s);
929
	return xbee_send_modify_at_command("ID", s);
809 930
}
810 931

  
811 932
/**
812 933
 * Get the PAN ID for the XBee.
813
 * 
934
 *
814 935
 * @return the personal area network id, or
815 936
 * XBEE_PAN_DEFAULT if it has not yet been set.
816 937
 **/
938
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
939
// it reduces code size or not should be done to be sure.
817 940
unsigned int xbee_get_pan_id()
818 941
{
819 942
	return xbee_panID;
......
822 945
/**
823 946
 * Set the channel the XBee is using.
824 947
 *
825
 * @param channel the channel the XBee will not use, 
948
 * @param channel the channel the XBee will not use,
826 949
 * between 0x0B and 0x1A
827 950
 *
828 951
 * @see xbee_get_channel
829 952
 **/
830
void xbee_set_channel(int channel)
953
int xbee_set_channel(int channel)
831 954
{
832 955
	if (channel < 0x0B || channel > 0x1A)
833 956
	{
834 957
		WL_DEBUG_PRINT("Channel out of range.\r\n");
835
		return;
958
		return -1;
836 959
	}
960

  
837 961
	char s[3];
838 962
	s[0] = channel & 0xFF;
839 963
	s[1] = 0;
840 964
	xbee_pending_channel = channel;
841
	xbee_send_modify_at_command("CH", s);
965

  
966
	return xbee_send_modify_at_command("CH", s);
842 967
}
843 968

  
844 969
/**
......
848 973
 *
849 974
 * @see xbee_set_channel
850 975
 **/
976
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
977
// it reduces code size or not should be done to be sure.
851 978
int xbee_get_channel(void)
852 979
{
853 980
	return xbee_channel;
......
860 987
 *
861 988
 * @return the 16-bit address of the XBee.
862 989
 **/
990
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
991
// it reduces code size or not should be done to be sure.
863 992
unsigned int xbee_get_address()
864 993
{
865 994
	return xbee_address;
......
871 1000
	xbee_com_port = port;
872 1001
}
873 1002
#endif
874

  

Also available in: Unified diff