Project

General

Profile

Revision 581

Added by Jason knichel about 16 years ago

wireless library now times out instead of infinitely looping in waiting for ok

View differences:

xbee.c
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
......
83 84
static int xbee_exit_command_mode(void);
84 85
static int xbee_enter_api_mode(void);
85 86
static int xbee_exit_api_mode(void);
86
static void xbee_wait_for_string(char* s, int len);
87
static void xbee_wait_for_ok(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

  
......
124 125
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
125 126
static int xbee_channel = XBEE_CHANNEL_DEFAULT;
126 127
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
127
static unsigned int xbee_address = 0;
128
static volatile unsigned int xbee_address = 0;
128 129

  
129 130
/*Function Implementations*/
130 131

  
......
207 208
	arrival_buf[0] = 'A';
208 209
	arrival_buf[1] = 'A';
209 210
	arrival_buf[2] = 'A';
210
	#ifdef ROBOT
211
#ifdef ROBOT
211 212

  
212 213
	//enable the receiving interrupt
213 214
#ifdef FIREFLY
......
223 224
	{
224 225
		printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
225 226
		return -1;
227
	} else {
228
	  printf("Successfully opened connection to XBee on port %s\r\n", xbee_com_port);
226 229
	}
227 230

  
228 231
	// set baud rate, etc. correctly
......
246 249
	{
247 250
		fprintf(stderr, "Error setting attributes.\n");
248 251
		return -1;
252
	} else {
253
	  //printf("Successfully set termios attributes.\n");
249 254
	}
250 255

  
251 256
	//lockf(xbee_stream, F_LOCK, 0);
......
262 267
	{
263 268
		fprintf(stderr, "Failed to create listener thread.\r\n");
264 269
		return -1;
270
	} else {
271
	  //printf("Successfully created listener thread.\n");
265 272
	}
266 273
#endif
267 274

  
268 275
	if (xbee_enter_command_mode() != 0) {
276
#ifndef ROBOT
277
	  printf("Error returned from xbee_enter_command_mode\n");
278
#endif
269 279
		return -1;
270 280
	}
271 281

  
272 282
	if (xbee_enter_api_mode() != 0) {
283
#ifndef ROBOT
284
	  printf("Error returned from xbee_enter_api_mode\n");
285
#endif
273 286
		return -1;
274 287
	}
275 288

  
276 289
	if (xbee_exit_command_mode() != 0) {
290
#ifndef ROBOT
291
	  printf("Error returned from xbee_exit_command_mode\n");
292
#endif
277 293
		return -1;
278 294
	}
279 295

  
280 296
	if (xbee_send_read_at_command("MY")) {
297
#ifndef ROBOT
298
	  printf("Error returned from xbee_send_read_at_command\n");
299
#endif
281 300
		return -1;
282 301
	}
283 302

  
303
#ifndef ROBOT
304
	//printf("About to enter while loop to get xbee_address.\n");
305
	int i;
306
	for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) {
307
	  ret = xbee_get_packet(NULL);
308

  
309
	  usleep(1000);
310

  
311
	  if (ret == -1) {
312
	    printf("xbee_get_packet(NULL) failed.\n");
313
	    return -1;
314
	  }
315
	}
316

  
317
	//	printf("After exiting while loop to get xbee_address.\n");
318
#else
284 319
	//wait to return until the address is set
285
	while (xbee_address == 0) xbee_get_packet(NULL);
320
	while (xbee_address == 0) {
321
	  xbee_get_packet(NULL);
322
	}
323
#endif
286 324

  
325
#ifndef ROBOT
326
	if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out.
327

  
328
	  printf("xbee_get_packet timed out.\n");
329
	  return -1;
330
	} else {
331
	  return 0;
332
	}
333
#else
287 334
	return 0;
335
#endif
288 336
}
289 337

  
290 338
/**
......
371 419
		return -1;
372 420
	}
373 421

  
374
	xbee_wait_for_ok();
422
#ifndef ROBOT
423
	//	printf("In xbee_enter_command_mode about to call xbee_wait_for_ok()\n");
424
#endif
375 425

  
376
	return 0;
426
	if (xbee_wait_for_ok() != 0) {
427
#ifndef ROBOT
428
	  printf("xbee_wait_for_ok failed.\n");
429
#endif
430
	  return -1;
431
	} else {
432
	  return 0;
433
	}
377 434
}
378 435

  
379 436
/**
......
414 471
/**
415 472
 * Wait until the string "OK\r" is received from the XBee.
416 473
 **/
417
static void xbee_wait_for_ok()
474
static int xbee_wait_for_ok()
418 475
{
419
	xbee_wait_for_string("OK\r", 3);
476
	return xbee_wait_for_string("OK\r", 3);
420 477
}
421 478

  
422 479
/**
......
426 483
 * @param s the string to receive
427 484
 * @param len the length of the string
428 485
 **/
429
static void xbee_wait_for_string(char* s, int len)
486
static int xbee_wait_for_string(char* s, int len)
430 487
{
488
#ifndef ROBOT
489
  //printf("In xbee_wait_for_string.\n");
490
#endif
491

  
431 492
	char* curr = s;
432 493
	while (curr - s < len) {
433 494
		// check if buffer is empty
......
440 501
			if (c == *curr) {
441 502
				curr++;
442 503
			} else {
443
				curr = s;
504
#ifndef ROBOT
505
			  return -1; // Computer is less forgiving.
506
#else
507
			  curr = s;
508
#endif
444 509
			}
445
		}
510
		} // else buffer is empty.
446 511

  
447 512
#ifndef ROBOT
448
		usleep(1000);
513
		usleep(100);
449 514
#endif
450 515
	}
516

  
517
	return 0;
451 518
}
452 519

  
453 520
/**
......
510 577
	if (xbee_send(prefix, 3) != 0) {
511 578
		return -1;
512 579
	}
513
	
580

  
514 581
	if (xbee_send(buf, len) != 0) {
515 582
		return -1;
516 583
	}
......
681 748
			if (buffer_first == XBEE_BUFFER_SIZE)
682 749
				buffer_first = 0;
683 750
			// check if buffer is empty
684
			if (buffer_first == buffer_last)
685
				return -1;
751
			if (buffer_first == buffer_last) {
752
				return 0;
753
			}
686 754
		} while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
687 755

  
688 756
		if (buffer_first == XBEE_BUFFER_SIZE) {
......
713 781

  
714 782
		// check if buffer is empty
715 783
		if (buffer_first == buffer_last) {
716
			return -1;
784
			return 0;
717 785
		}
718 786
		xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
719 787
		if (buffer_first == XBEE_BUFFER_SIZE) {
......
730 798
	}
731 799

  
732 800
	//we will take care of the packet
733
	if (xbee_handle_packet(xbee_buf + 3, len)) {
734
		return -1;
801
	if (xbee_handle_packet(xbee_buf + 3, len) != 0) {
802
		return 0;
735 803
	}
736 804

  
737 805
	if (dest == NULL) {
738
		return -1;
806
		return 0;
739 807
	}
740 808

  
741 809
	int i;
......
819 887
	if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0)
820 888
	{
821 889
//							printf("reading xbee_address\n");
822
		
890

  
823 891
		xbee_address = 0;
824 892
		int i;
825 893
		for (i = 0; i < extraLen; i++) {
......
955 1023
	xbee_com_port = port;
956 1024
}
957 1025
#endif
958

  

Also available in: Unified diff