root / trunk / code / projects / libwireless / lib / wireless.c @ 309
History | View | Annotate | Download (10.1 KB)
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 |
|
35 |
#include "wireless.h" |
36 |
#include "xbee.h" |
37 |
#include <stdlib.h> |
38 |
#include <stdio.h> |
39 |
|
40 |
#include "wl_defs.h" |
41 |
|
42 |
#ifndef ROBOT
|
43 |
#include <sys/time.h> |
44 |
#include <signal.h> |
45 |
#else
|
46 |
#include <time.h> |
47 |
#ifndef FIREFLY
|
48 |
#include <bom.h> |
49 |
#endif
|
50 |
#endif
|
51 |
|
52 |
/*Function Prototypes*/
|
53 |
|
54 |
void wl_do_timeout(void); |
55 |
|
56 |
//Note: the actual frame sent has group as the first four bits and
|
57 |
//frame as the last four.
|
58 |
void wl_send_packet(char group, char type, char* data, int len, |
59 |
int dest, char options, char frame); |
60 |
|
61 |
/*Data Members*/
|
62 |
|
63 |
//used to store incoming and outgoing packets
|
64 |
unsigned char wl_buf[128]; |
65 |
//1 if we have timed out since we last checked, 0 otherwise.
|
66 |
int wl_timeout = 0; |
67 |
|
68 |
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS]; |
69 |
|
70 |
#ifndef ROBOT
|
71 |
|
72 |
//called when we time out, or receive interrupt
|
73 |
void sig_handler(int signo) |
74 |
{ |
75 |
switch (signo)
|
76 |
{ |
77 |
case SIGALRM:
|
78 |
wl_timeout = 1;
|
79 |
break;
|
80 |
case SIGINT:
|
81 |
wl_terminate(); |
82 |
exit(1);
|
83 |
break;
|
84 |
} |
85 |
return;
|
86 |
} |
87 |
#else
|
88 |
|
89 |
//called when the timer ticks
|
90 |
void timer_handler(void) |
91 |
{ |
92 |
wl_timeout = 1;
|
93 |
} |
94 |
|
95 |
#endif
|
96 |
|
97 |
/**
|
98 |
* Initializes the wireless library. Must be called before any
|
99 |
* other function.
|
100 |
**/
|
101 |
int wl_init()
|
102 |
{ |
103 |
int i;
|
104 |
for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) |
105 |
wl_packet_groups[i] = NULL;
|
106 |
|
107 |
printf("calling xbee_lib_init\n");
|
108 |
if (xbee_lib_init() == -1) { |
109 |
return -1; |
110 |
} |
111 |
printf("xbee_lib_init finished\n");
|
112 |
|
113 |
//begin timeout timer
|
114 |
#ifdef ROBOT
|
115 |
#ifdef FIREFLY
|
116 |
rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
|
117 |
#else
|
118 |
rtc_init(HALF_SECOND, &timer_handler); |
119 |
#endif
|
120 |
#else
|
121 |
|
122 |
//create our timer
|
123 |
struct itimerval timer_val;
|
124 |
struct timeval interval;
|
125 |
interval.tv_sec = 0;
|
126 |
interval.tv_usec = 500000;
|
127 |
struct timeval first_time;
|
128 |
first_time.tv_sec = 0;
|
129 |
first_time.tv_usec = 500000;
|
130 |
timer_val.it_interval = interval; |
131 |
timer_val.it_value = first_time; |
132 |
if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1) |
133 |
{ |
134 |
WL_DEBUG_PRINT("Error creating a timer.\r\n");
|
135 |
perror("Failure's cause");
|
136 |
exit(1);
|
137 |
} |
138 |
|
139 |
//create signal handler
|
140 |
struct sigaction wl_sig_act;
|
141 |
wl_sig_act.sa_handler = sig_handler; |
142 |
wl_sig_act.sa_flags = 0;
|
143 |
sigemptyset(&wl_sig_act.sa_mask); |
144 |
sigaction(SIGALRM, &wl_sig_act, 0);
|
145 |
sigaction(SIGINT, &wl_sig_act, 0);
|
146 |
#endif
|
147 |
|
148 |
return 0; |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Uninitializes the wireless library.
|
153 |
**/
|
154 |
void wl_terminate()
|
155 |
{ |
156 |
int i;
|
157 |
for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) |
158 |
if (wl_packet_groups[i] != NULL && |
159 |
wl_packet_groups[i]->unregister != NULL)
|
160 |
wl_packet_groups[i]->unregister(); |
161 |
|
162 |
xbee_terminate(); |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Set the PAN for the XBee to join.
|
167 |
*
|
168 |
* @param pan the new PAN
|
169 |
*
|
170 |
* @see wl_get_pan
|
171 |
**/
|
172 |
void wl_set_pan(int pan) |
173 |
{ |
174 |
xbee_set_pan_id(pan); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Get the PAN the XBee is currently part of.
|
179 |
*
|
180 |
* @return the PAN of the XBee
|
181 |
*
|
182 |
* @see wl_set_pan
|
183 |
**/
|
184 |
int wl_get_pan(void) |
185 |
{ |
186 |
return xbee_get_pan_id();
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* Set the channel the XBee is listening to.
|
191 |
*
|
192 |
* @param channel the new channel to join
|
193 |
*
|
194 |
* @see wl_get_channel
|
195 |
**/
|
196 |
void wl_set_channel(int channel) |
197 |
{ |
198 |
xbee_set_channel(channel); |
199 |
} |
200 |
|
201 |
/**
|
202 |
* Get the channel the XBee is part of.
|
203 |
*
|
204 |
* @return the channel the XBee is part of
|
205 |
*
|
206 |
* @see wl_set_channel
|
207 |
**/
|
208 |
int wl_get_channel(void) |
209 |
{ |
210 |
return xbee_get_channel();
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Returns the 16-bit address of the XBee module.
|
215 |
*
|
216 |
* @return the 16-bit address of the XBee module.
|
217 |
**/
|
218 |
int wl_get_xbee_id()
|
219 |
{ |
220 |
return xbee_get_address();
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* Send a packet to a specific XBee without specifying a PAN.
|
225 |
*
|
226 |
* @param group the packet group
|
227 |
* @param type the packet type
|
228 |
* @param data the packet data
|
229 |
* @param len the packet length in bytes
|
230 |
* @param dest the 16-bit address of the XBee to send the packet to
|
231 |
* @param frame the frame number to see with a TX_STATUS response
|
232 |
**/
|
233 |
void wl_send_robot_to_robot_global_packet(char group, char type, |
234 |
char* data, int len, int dest, char frame) |
235 |
{ |
236 |
wl_send_packet(group, type, data, len, dest, |
237 |
XBEE_OPTIONS_BROADCAST_ALL_PANS, frame); |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Send a packet to a specific XBee in the same PAN.
|
242 |
*
|
243 |
* @param group the packet group
|
244 |
* @param type the packet type
|
245 |
* @param data the packet data
|
246 |
* @param len the packet length in bytes
|
247 |
* @param dest the 16-bit address of the XBee to send the packet to
|
248 |
* @param frame the frame number to see with a TX_STATUS response
|
249 |
**/
|
250 |
void wl_send_robot_to_robot_packet(char group, char type, |
251 |
char* data, int len, int dest, char frame) |
252 |
{ |
253 |
wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE, |
254 |
frame); |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Send a packet to all XBees in all PANs.
|
259 |
*
|
260 |
* @param group the packet group
|
261 |
* @param type the packet type
|
262 |
* @param data the packet data
|
263 |
* @param len the packet length in bytes
|
264 |
* @param frame the frame number to see with a TX_STATUS response
|
265 |
**/
|
266 |
void wl_send_global_packet(char group, char type, |
267 |
char* data, int len, char frame) |
268 |
{ |
269 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST, |
270 |
XBEE_OPTIONS_BROADCAST_ALL_PANS, frame); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Send a packet to all XBee's in the same PAN.
|
275 |
*
|
276 |
* @param group the packet group
|
277 |
* @param type the packet type
|
278 |
* @param data the packet data
|
279 |
* @param len the packet length in bytes
|
280 |
* @param frame the frame number to see with a TX_STATUS response
|
281 |
**/
|
282 |
void wl_send_pan_packet(char group, char type, |
283 |
char* data, int len, char frame) |
284 |
{ |
285 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST, |
286 |
XBEE_OPTIONS_NONE, frame); |
287 |
} |
288 |
|
289 |
/**
|
290 |
* Send a packet.
|
291 |
*
|
292 |
* @param group the packet group
|
293 |
* @param type the packet type
|
294 |
* @param data the packet data
|
295 |
* @param len the packet length in bytes
|
296 |
* @param dest the destination of the packet
|
297 |
* @param options the options for sending the packet
|
298 |
* @param frame the frame number to see with a TX_STATUS response
|
299 |
**/
|
300 |
void wl_send_packet(char group, char type, char* data, int len, |
301 |
int dest, char options, char frame) |
302 |
{ |
303 |
char buf[128]; |
304 |
int i;
|
305 |
if (frame != 0) |
306 |
frame = (frame & 0x0F) | ((group & 0x0F) << 4); |
307 |
buf[0] = group;
|
308 |
buf[1] = type;
|
309 |
for (i = 0; i < len; i++) |
310 |
buf[2 + i] = data[i];
|
311 |
xbee_send_packet(buf, len + 2, dest, options, frame);
|
312 |
} |
313 |
|
314 |
/**
|
315 |
* Register a packet group with the wireless library. The event
|
316 |
* handlers in the packet group will be called whenever an
|
317 |
* event dealing with the packet group's group code occurs.
|
318 |
*
|
319 |
* @param h the PacketGroupHandler to register
|
320 |
**/
|
321 |
void wl_register_packet_group(PacketGroupHandler* h)
|
322 |
{ |
323 |
if (h->groupCode >= WL_MAX_PACKET_GROUPS)
|
324 |
{ |
325 |
WL_DEBUG_PRINT("Packet group code too large.\r\n");
|
326 |
return;
|
327 |
} |
328 |
if (wl_packet_groups[h->groupCode] != NULL) |
329 |
{ |
330 |
WL_DEBUG_PRINT("Packet group code already registered.\r\n");
|
331 |
return;
|
332 |
} |
333 |
wl_packet_groups[h->groupCode] = h; |
334 |
} |
335 |
|
336 |
/**
|
337 |
* Unregister a packet group from the wireless library.
|
338 |
*
|
339 |
* @param h the packet group to remove
|
340 |
**/
|
341 |
void wl_unregister_packet_group(PacketGroupHandler* h)
|
342 |
{ |
343 |
unsigned int groupCode = h->groupCode; |
344 |
PacketGroupHandler* p = wl_packet_groups[groupCode]; |
345 |
if (p != NULL && p->unregister != NULL) |
346 |
p->unregister(); |
347 |
wl_packet_groups[groupCode] = NULL;
|
348 |
} |
349 |
|
350 |
/**
|
351 |
* Called when the timer is triggered. This calls the timeout
|
352 |
* handlers of all the registered packet groups.
|
353 |
**/
|
354 |
void wl_do_timeout()
|
355 |
{ |
356 |
int i;
|
357 |
for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) |
358 |
if (wl_packet_groups[i] != NULL && |
359 |
wl_packet_groups[i]->timeout_handler != NULL)
|
360 |
wl_packet_groups[i]->timeout_handler(); |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Performs wireless library functionality. This function must
|
365 |
* be called frequently for wireless to perform effectively.
|
366 |
* This function will call timeout handlers, as well as
|
367 |
* received packet and transmit status handlers.
|
368 |
**/
|
369 |
void wl_do()
|
370 |
{ |
371 |
if (wl_timeout)
|
372 |
{ |
373 |
wl_do_timeout(); |
374 |
wl_timeout = 0;
|
375 |
} |
376 |
|
377 |
int len = xbee_get_packet(wl_buf);
|
378 |
if (len < 0)//no packet received |
379 |
return;
|
380 |
|
381 |
if (wl_buf[0] == XBEE_TX_STATUS) |
382 |
{ |
383 |
if (len != 3) |
384 |
{ |
385 |
WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
|
386 |
return;
|
387 |
} |
388 |
|
389 |
//the first four bits are the packet group
|
390 |
//this only works with under 16 groups
|
391 |
int group = (int)(wl_buf[1] >> 4); |
392 |
int success = 0; |
393 |
if (wl_buf[2] == 0) |
394 |
success = 1;
|
395 |
else
|
396 |
{ |
397 |
WL_DEBUG_PRINT("No response received.\r\n");
|
398 |
if (wl_buf[2] == 2) |
399 |
{ |
400 |
WL_DEBUG_PRINT("CCA Failure\r\n");
|
401 |
} |
402 |
if (wl_buf[2] == 3) |
403 |
{ |
404 |
WL_DEBUG_PRINT("Purged\r\n");
|
405 |
} |
406 |
} |
407 |
|
408 |
if (wl_packet_groups[group] != NULL && |
409 |
wl_packet_groups[group]->handle_response != NULL)
|
410 |
wl_packet_groups[group]->handle_response( |
411 |
(int)wl_buf[1] & 0x0F, success); |
412 |
return;
|
413 |
} |
414 |
|
415 |
if (wl_buf[0] == XBEE_RX) |
416 |
{ |
417 |
if (len < 7) |
418 |
{ |
419 |
WL_DEBUG_PRINT("Packet is too small.\r\n");
|
420 |
return;
|
421 |
} |
422 |
|
423 |
int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]); |
424 |
|
425 |
/*
|
426 |
//unused for now
|
427 |
int signalStrength = wl_buf[3];
|
428 |
//1 for Address broadcast, 2 for PAN broadcast
|
429 |
int options = wl_buf[4];
|
430 |
*/
|
431 |
|
432 |
int group = wl_buf[5]; |
433 |
int type = wl_buf[6]; |
434 |
int packetLen = len - 7; |
435 |
|
436 |
if (wl_packet_groups[group] != NULL |
437 |
&& wl_packet_groups[group]->handle_receive != NULL)
|
438 |
wl_packet_groups[group]->handle_receive(type, source, |
439 |
wl_buf + 7, packetLen);
|
440 |
return;
|
441 |
} |
442 |
|
443 |
WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
|
444 |
return;
|
445 |
} |
446 |
|
447 |
|
448 |
#ifndef ROBOT
|
449 |
void wl_set_com_port(char* port) |
450 |
{ |
451 |
xbee_set_com_port(port); |
452 |
} |
453 |
#endif
|
454 |
|