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