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