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