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