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