Project

General

Profile

Revision 337

Minor updates to recharging, but the charging board no longer appears to tell us when we're done.

View differences:

wireless.c
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26
/**
27
 * @file wireless.c
28
 * @brief Wireless Library Implementation
29
 *
30
 * Implementation of the wireless library.
31
 *
32
 * @author Brian Coltin, Colony Project, CMU Robotics Club
33
 **/
34

  
1 35
#include "wireless.h"
2 36
#include "xbee.h"
3 37
#include <stdlib.h>
......
64 98
 * Initializes the wireless library. Must be called before any
65 99
 * other function.
66 100
 **/
67
void wl_init()
101
int wl_init()
68 102
{
69 103
	int i;
70 104
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
71 105
		wl_packet_groups[i] = NULL;
72 106

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

  
75 111
	//begin timeout timer
76 112
	#ifdef ROBOT
77 113
	#ifdef FIREFLY
78 114
	rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
79 115
	#else
80
	rtc_init(HALF_SECOND, &timer_handler); 
116
	rtc_init(HALF_SECOND, &timer_handler);
81 117
	#endif
82 118
	#else
83
  
119

  
84 120
	//create our timer
85 121
	struct itimerval timer_val;
86 122
	struct timeval interval;
......
93 129
	timer_val.it_value = first_time;
94 130
	if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
95 131
	{
96
		WL_DEBUG_PRINT("Error creating a timer.\r\n"); 
132
		WL_DEBUG_PRINT("Error creating a timer.\r\n");
97 133
		perror("Failure's cause");
98
		exit(1); 
134
		exit(1);
99 135
	}
100 136

  
101 137
	//create signal handler
......
106 142
	sigaction(SIGALRM, &wl_sig_act, 0);
107 143
	sigaction(SIGINT, &wl_sig_act, 0);
108 144
	#endif
145

  
146
	return 0;
109 147
}
110 148

  
111 149
/**
......
118 156
		if (wl_packet_groups[i] != NULL &&
119 157
			wl_packet_groups[i]->unregister != NULL)
120 158
			wl_packet_groups[i]->unregister();
121
	
159

  
122 160
	xbee_terminate();
123 161
}
124 162

  
......
175 213
 *
176 214
 * @return the 16-bit address of the XBee module.
177 215
 **/
178
unsigned int wl_get_xbee_id()
216
int wl_get_xbee_id()
179 217
{
180 218
	return xbee_get_address();
181 219
}
......
242 280
void wl_send_pan_packet(char group, char type,
243 281
		char* data, int len, char frame)
244 282
{
245
	wl_send_packet(group, type, data, len, XBEE_BROADCAST, 
283
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
246 284
			XBEE_OPTIONS_NONE, frame);
247 285
}
248 286

  
......
295 333

  
296 334
/**
297 335
 * Unregister a packet group from the wireless library.
298
 * 
336
 *
299 337
 * @param h the packet group to remove
300 338
 **/
301 339
void wl_unregister_packet_group(PacketGroupHandler* h)
......
333 371
		wl_do_timeout();
334 372
		wl_timeout = 0;
335 373
	}
336
	
374

  
337 375
	int len = xbee_get_packet(wl_buf);
338 376
	if (len < 0)//no packet received
339 377
		return;
340
	
378

  
341 379
	if (wl_buf[0] == XBEE_TX_STATUS)
342 380
	{
343 381
		if (len != 3)
......
345 383
			WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
346 384
			return;
347 385
		}
348
		
386

  
349 387
		//the first four bits are the packet group
350 388
		//this only works with under 16 groups
351 389
		int group = (int)(wl_buf[1] >> 4);
......
364 402
				WL_DEBUG_PRINT("Purged\r\n");
365 403
			}
366 404
		}
367
		
405

  
368 406
		if (wl_packet_groups[group] != NULL &&
369 407
					wl_packet_groups[group]->handle_response != NULL)
370 408
			wl_packet_groups[group]->handle_response(
371 409
					(int)wl_buf[1] & 0x0F, success);
372 410
		return;
373 411
	}
374
	
412

  
375 413
	if (wl_buf[0] == XBEE_RX)
376 414
	{
377 415
		if (len < 7)
......
379 417
			WL_DEBUG_PRINT("Packet is too small.\r\n");
380 418
			return;
381 419
		}
382
		
420

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

  
385 423
		/*
386 424
		//unused for now
387 425
		int signalStrength = wl_buf[3];
388 426
		//1 for Address broadcast, 2 for PAN broadcast
389 427
		int options = wl_buf[4];
390 428
		*/
391
		
429

  
392 430
		int group = wl_buf[5];
393 431
		int type = wl_buf[6];
394 432
		int packetLen = len - 7;
395
		
433

  
396 434
		if (wl_packet_groups[group] != NULL
397 435
				&& wl_packet_groups[group]->handle_receive != NULL)
398
			wl_packet_groups[group]->handle_receive(type, source, 
436
			wl_packet_groups[group]->handle_receive(type, source,
399 437
				wl_buf + 7, packetLen);
400 438
		return;
401 439
	}
402
	
440

  
403 441
	WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
404 442
	return;
405 443
}
406 444

  
445

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

  

Also available in: Unified diff