Project

General

Profile

Revision 743

Fixed compilation errors.

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>
......
50 84
	}
51 85
	return;
52 86
}
87
#else
88

  
89
//called when the timer ticks
90
void timer_handler(void)
91
{
92
	wl_timeout = 1;
93
}
94

  
53 95
#endif
54 96

  
55 97
/**
56 98
 * Initializes the wireless library. Must be called before any
57 99
 * other function.
58 100
 **/
59
void wl_init()
101
int wl_init()
60 102
{
61 103
	int i;
62 104
	for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
63 105
		wl_packet_groups[i] = NULL;
64 106

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

  
67 111
	//begin timeout timer
68 112
	#ifdef ROBOT
69 113
	#ifdef FIREFLY
70
	rtc_init(PRESCALE_DIV_128, 32, &wl_do_timeout);
114
	rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
71 115
	#else
72
	rtc_init(HALF_SECOND, &wl_do_timeout); 
116
	rtc_init(HALF_SECOND, &timer_handler);
73 117
	#endif
74 118
	#else
75
  
119

  
76 120
	//create our timer
77 121
	struct itimerval timer_val;
78 122
	struct timeval interval;
......
85 129
	timer_val.it_value = first_time;
86 130
	if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
87 131
	{
88
		WL_DEBUG_PRINT("Error creating a timer.\r\n"); 
132
		WL_DEBUG_PRINT("Error creating a timer.\r\n");
89 133
		perror("Failure's cause");
90
		exit(1); 
134
		exit(1);
91 135
	}
92 136

  
93 137
	//create signal handler
94 138
	struct sigaction wl_sig_act;
95
	wl_sig_act.sa_handler = (void *)sig_handler;
139
	wl_sig_act.sa_handler = sig_handler;
96 140
	wl_sig_act.sa_flags = 0;
97 141
	sigemptyset(&wl_sig_act.sa_mask);
98 142
	sigaction(SIGALRM, &wl_sig_act, 0);
99 143
	sigaction(SIGINT, &wl_sig_act, 0);
100 144
	#endif
145

  
146
	return 0;
101 147
}
102 148

  
103 149
/**
......
110 156
		if (wl_packet_groups[i] != NULL &&
111 157
			wl_packet_groups[i]->unregister != NULL)
112 158
			wl_packet_groups[i]->unregister();
113
	
159

  
114 160
	xbee_terminate();
115 161
}
116 162

  
......
167 213
 *
168 214
 * @return the 16-bit address of the XBee module.
169 215
 **/
170
unsigned int wl_get_xbee_id()
216
int wl_get_xbee_id()
171 217
{
172 218
	return xbee_get_address();
173 219
}
......
234 280
void wl_send_pan_packet(char group, char type,
235 281
		char* data, int len, char frame)
236 282
{
237
	wl_send_packet(group, type, data, len, XBEE_BROADCAST, 
283
	wl_send_packet(group, type, data, len, XBEE_BROADCAST,
238 284
			XBEE_OPTIONS_NONE, frame);
239 285
}
240 286

  
......
287 333

  
288 334
/**
289 335
 * Unregister a packet group from the wireless library.
290
 * 
336
 *
291 337
 * @param h the packet group to remove
292 338
 **/
293 339
void wl_unregister_packet_group(PacketGroupHandler* h)
......
325 371
		wl_do_timeout();
326 372
		wl_timeout = 0;
327 373
	}
328
	
374

  
329 375
	int len = xbee_get_packet(wl_buf);
330 376
	if (len < 0)//no packet received
331 377
		return;
332
	
378

  
333 379
	if (wl_buf[0] == XBEE_TX_STATUS)
334 380
	{
335 381
		if (len != 3)
......
337 383
			WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
338 384
			return;
339 385
		}
340
		
386

  
341 387
		//the first four bits are the packet group
342 388
		//this only works with under 16 groups
343 389
		int group = (int)(wl_buf[1] >> 4);
......
356 402
				WL_DEBUG_PRINT("Purged\r\n");
357 403
			}
358 404
		}
359
		
405

  
360 406
		if (wl_packet_groups[group] != NULL &&
361 407
					wl_packet_groups[group]->handle_response != NULL)
362 408
			wl_packet_groups[group]->handle_response(
363 409
					(int)wl_buf[1] & 0x0F, success);
364 410
		return;
365 411
	}
366
	
412

  
367 413
	if (wl_buf[0] == XBEE_RX)
368 414
	{
369 415
		if (len < 7)
......
371 417
			WL_DEBUG_PRINT("Packet is too small.\r\n");
372 418
			return;
373 419
		}
374
		
420

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

  
377 423
		/*
378 424
		//unused for now
379 425
		int signalStrength = wl_buf[3];
380 426
		//1 for Address broadcast, 2 for PAN broadcast
381 427
		int options = wl_buf[4];
382 428
		*/
383
		
429

  
384 430
		int group = wl_buf[5];
385 431
		int type = wl_buf[6];
386 432
		int packetLen = len - 7;
387
		
433

  
388 434
		if (wl_packet_groups[group] != NULL
389 435
				&& wl_packet_groups[group]->handle_receive != NULL)
390
			wl_packet_groups[group]->handle_receive(type, source, 
436
			wl_packet_groups[group]->handle_receive(type, source,
391 437
				wl_buf + 7, packetLen);
392 438
		return;
393 439
	}
394
	
440

  
395 441
	WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
396 442
	return;
397 443
}
398 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