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