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