Project

General

Profile

Revision 336

Updated wireless to use a circular buffer instead of a queue using malloc. Tested on both the computer and robots with a token ring, and was successful.

View differences:

wireless.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
......
98 98
 * Initializes the wireless library. Must be called before any
99 99
 * other function.
100 100
 **/
101
void wl_init()
101
int wl_init()
102 102
{
103 103
	int i;
104 104
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
105 105
		wl_packet_groups[i] = NULL;
106 106

  
107
	xbee_lib_init();
108
	
107
	if (xbee_lib_init() == -1) {
108
	  return -1;
109
	}
110

  
109 111
	//begin timeout timer
110 112
	#ifdef ROBOT
111 113
	#ifdef FIREFLY
112 114
	rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
113 115
	#else
114
	rtc_init(HALF_SECOND, &timer_handler); 
116
	rtc_init(HALF_SECOND, &timer_handler);
115 117
	#endif
116 118
	#else
117
  
119

  
118 120
	//create our timer
119 121
	struct itimerval timer_val;
120 122
	struct timeval interval;
......
127 129
	timer_val.it_value = first_time;
128 130
	if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
129 131
	{
130
		WL_DEBUG_PRINT("Error creating a timer.\r\n"); 
132
		WL_DEBUG_PRINT("Error creating a timer.\r\n");
131 133
		perror("Failure's cause");
132
		exit(1); 
134
		exit(1);
133 135
	}
134 136

  
135 137
	//create signal handler
......
140 142
	sigaction(SIGALRM, &wl_sig_act, 0);
141 143
	sigaction(SIGINT, &wl_sig_act, 0);
142 144
	#endif
145

  
146
	return 0;
143 147
}
144 148

  
145 149
/**
......
152 156
		if (wl_packet_groups[i] != NULL &&
153 157
			wl_packet_groups[i]->unregister != NULL)
154 158
			wl_packet_groups[i]->unregister();
155
	
159

  
156 160
	xbee_terminate();
157 161
}
158 162

  
......
209 213
 *
210 214
 * @return the 16-bit address of the XBee module.
211 215
 **/
212
unsigned int wl_get_xbee_id()
216
int wl_get_xbee_id()
213 217
{
214 218
	return xbee_get_address();
215 219
}
......
276 280
void wl_send_pan_packet(char group, char type,
277 281
		char* data, int len, char frame)
278 282
{
279
	wl_send_packet(group, type, data, len, XBEE_BROADCAST, 
283
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
280 284
			XBEE_OPTIONS_NONE, frame);
281 285
}
282 286

  
......
329 333

  
330 334
/**
331 335
 * Unregister a packet group from the wireless library.
332
 * 
336
 *
333 337
 * @param h the packet group to remove
334 338
 **/
335 339
void wl_unregister_packet_group(PacketGroupHandler* h)
......
367 371
		wl_do_timeout();
368 372
		wl_timeout = 0;
369 373
	}
370
	
374

  
371 375
	int len = xbee_get_packet(wl_buf);
372 376
	if (len < 0)//no packet received
373 377
		return;
374
	
378

  
375 379
	if (wl_buf[0] == XBEE_TX_STATUS)
376 380
	{
377 381
		if (len != 3)
......
379 383
			WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
380 384
			return;
381 385
		}
382
		
386

  
383 387
		//the first four bits are the packet group
384 388
		//this only works with under 16 groups
385 389
		int group = (int)(wl_buf[1] >> 4);
......
398 402
				WL_DEBUG_PRINT("Purged\r\n");
399 403
			}
400 404
		}
401
		
405

  
402 406
		if (wl_packet_groups[group] != NULL &&
403 407
					wl_packet_groups[group]->handle_response != NULL)
404 408
			wl_packet_groups[group]->handle_response(
405 409
					(int)wl_buf[1] & 0x0F, success);
406 410
		return;
407 411
	}
408
	
412

  
409 413
	if (wl_buf[0] == XBEE_RX)
410 414
	{
411 415
		if (len < 7)
......
413 417
			WL_DEBUG_PRINT("Packet is too small.\r\n");
414 418
			return;
415 419
		}
416
		
420

  
417 421
		int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
418
		
422

  
419 423
		/*
420 424
		//unused for now
421 425
		int signalStrength = wl_buf[3];
422 426
		//1 for Address broadcast, 2 for PAN broadcast
423 427
		int options = wl_buf[4];
424 428
		*/
425
		
429

  
426 430
		int group = wl_buf[5];
427 431
		int type = wl_buf[6];
428 432
		int packetLen = len - 7;
429
		
433

  
430 434
		if (wl_packet_groups[group] != NULL
431 435
				&& wl_packet_groups[group]->handle_receive != NULL)
432
			wl_packet_groups[group]->handle_receive(type, source, 
436
			wl_packet_groups[group]->handle_receive(type, source,
433 437
				wl_buf + 7, packetLen);
434 438
		return;
435 439
	}
436
	
440

  
437 441
	WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
438 442
	return;
439 443
}
440 444

  
441 445

  
442 446
#ifndef ROBOT
443
void wl_set_com_port(char* port){
444
  xbee_set_com_port(port);
447
void wl_set_com_port(char* port)
448
{
449
	xbee_set_com_port(port);
445 450
}
446 451
#endif
447 452

  
448

  

Also available in: Unified diff