Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / wireless.c @ 397

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