Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libwireless / wireless.c @ 242

History | View | Annotate | Download (10 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
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
void wl_send_packet(char group, char type, char* data, int len,
59
                                        int dest, char options, char frame);
60

    
61
/*Data Members*/
62

    
63
//used to store incoming and outgoing packets
64
unsigned char wl_buf[128];
65
//1 if we have timed out since we last checked, 0 otherwise.
66
int wl_timeout = 0;
67

    
68
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
69

    
70
#ifndef ROBOT
71

    
72
//called when we time out, or receive interrupt
73
void sig_handler(int signo)
74
{
75
        switch (signo)
76
        {
77
                case SIGALRM:
78
                        wl_timeout = 1;
79
                        break;
80
                case SIGINT:
81
                        wl_terminate();
82
                        exit(1);
83
                        break;
84
        }
85
        return;
86
}
87
#else
88

    
89
//called when the timer ticks
90
void timer_handler(void)
91
{
92
        wl_timeout = 1;
93
}
94

    
95
#endif
96

    
97
/**
98
 * Initializes the wireless library. Must be called before any
99
 * other function.
100
 **/
101
void wl_init()
102
{
103
        int i;
104
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
105
                wl_packet_groups[i] = NULL;
106

    
107
        xbee_lib_init();
108
        
109
        //begin timeout timer
110
        #ifdef ROBOT
111
        #ifdef FIREFLY
112
        rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
113
        #else
114
        rtc_init(HALF_SECOND, &timer_handler); 
115
        #endif
116
        #else
117
  
118
        //create our timer
119
        struct itimerval timer_val;
120
        struct timeval interval;
121
        interval.tv_sec = 0;
122
        interval.tv_usec = 500000;
123
        struct timeval first_time;
124
        first_time.tv_sec = 0;
125
        first_time.tv_usec = 500000;
126
        timer_val.it_interval = interval;
127
        timer_val.it_value = first_time;
128
        if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
129
        {
130
                WL_DEBUG_PRINT("Error creating a timer.\r\n"); 
131
                perror("Failure's cause");
132
                exit(1); 
133
        }
134

    
135
        //create signal handler
136
        struct sigaction wl_sig_act;
137
        wl_sig_act.sa_handler = sig_handler;
138
        wl_sig_act.sa_flags = 0;
139
        sigemptyset(&wl_sig_act.sa_mask);
140
        sigaction(SIGALRM, &wl_sig_act, 0);
141
        sigaction(SIGINT, &wl_sig_act, 0);
142
        #endif
143
}
144

    
145
/**
146
 * Uninitializes the wireless library.
147
 **/
148
void wl_terminate()
149
{
150
        int i;
151
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
152
                if (wl_packet_groups[i] != NULL &&
153
                        wl_packet_groups[i]->unregister != NULL)
154
                        wl_packet_groups[i]->unregister();
155
        
156
        xbee_terminate();
157
}
158

    
159
/**
160
 * Set the PAN for the XBee to join.
161
 *
162
 * @param pan the new PAN
163
 *
164
 * @see wl_get_pan
165
 **/
166
void wl_set_pan(int pan)
167
{
168
        xbee_set_pan_id(pan);
169
}
170

    
171
/**
172
 * Get the PAN the XBee is currently part of.
173
 *
174
 * @return the PAN of the XBee
175
 *
176
 * @see wl_set_pan
177
 **/
178
int wl_get_pan(void)
179
{
180
        return xbee_get_pan_id();
181
}
182

    
183
/**
184
 * Set the channel the XBee is listening to.
185
 *
186
 * @param channel the new channel to join
187
 *
188
 * @see wl_get_channel
189
 **/
190
void wl_set_channel(int channel)
191
{
192
        xbee_set_channel(channel);
193
}
194

    
195
/**
196
 * Get the channel the XBee is part of.
197
 *
198
 * @return the channel the XBee is part of
199
 *
200
 * @see wl_set_channel
201
 **/
202
int wl_get_channel(void)
203
{
204
        return xbee_get_channel();
205
}
206

    
207
/**
208
 * Returns the 16-bit address of the XBee module.
209
 *
210
 * @return the 16-bit address of the XBee module.
211
 **/
212
unsigned int wl_get_xbee_id()
213
{
214
        return xbee_get_address();
215
}
216

    
217
/**
218
 * Send a packet to a specific XBee without specifying a PAN.
219
 *
220
 * @param group the packet group
221
 * @param type the packet type
222
 * @param data the packet data
223
 * @param len the packet length in bytes
224
 * @param dest the 16-bit address of the XBee to send the packet to
225
 * @param frame the frame number to see with a TX_STATUS response
226
 **/
227
void wl_send_robot_to_robot_global_packet(char group, char type,
228
                char* data, int len, int dest, char frame)
229
{
230
        wl_send_packet(group, type, data, len, dest,
231
                        XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
232
}
233

    
234
/**
235
 * Send a packet to a specific XBee in the same 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
void wl_send_robot_to_robot_packet(char group, char type,
245
                char* data, int len, int dest, char frame)
246
{
247
        wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
248
                        frame);
249
}
250

    
251
/**
252
 * Send a packet to all XBees in all PANs.
253
 *
254
 * @param group the packet group
255
 * @param type the packet type
256
 * @param data the packet data
257
 * @param len the packet length in bytes
258
 * @param frame the frame number to see with a TX_STATUS response
259
 **/
260
void wl_send_global_packet(char group, char type,
261
                char* data, int len, char frame)
262
{
263
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
264
                        XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
265
}
266

    
267
/**
268
 * Send a packet to all XBee's in the same PAN.
269
 *
270
 * @param group the packet group
271
 * @param type the packet type
272
 * @param data the packet data
273
 * @param len the packet length in bytes
274
 * @param frame the frame number to see with a TX_STATUS response
275
 **/
276
void wl_send_pan_packet(char group, char type,
277
                char* data, int len, char frame)
278
{
279
        wl_send_packet(group, type, data, len, XBEE_BROADCAST, 
280
                        XBEE_OPTIONS_NONE, frame);
281
}
282

    
283
/**
284
 * Send a packet.
285
 *
286
 * @param group the packet group
287
 * @param type the packet type
288
 * @param data the packet data
289
 * @param len the packet length in bytes
290
 * @param dest the destination of the packet
291
 * @param options the options for sending the packet
292
 * @param frame the frame number to see with a TX_STATUS response
293
 **/
294
void wl_send_packet(char group, char type, char* data, int len,
295
                                        int dest, char options, char frame)
296
{
297
        char buf[128];
298
        int i;
299
        if (frame != 0)
300
                frame = (frame & 0x0F) | ((group & 0x0F) << 4);
301
        buf[0] = group;
302
        buf[1] = type;
303
        for (i = 0; i < len; i++)
304
                buf[2 + i] = data[i];
305
        xbee_send_packet(buf, len + 2, dest, options, frame);
306
}
307

    
308
/**
309
 * Register a packet group with the wireless library. The event
310
 * handlers in the packet group will be called whenever an
311
 * event dealing with the packet group's group code occurs.
312
 *
313
 * @param h the PacketGroupHandler to register
314
 **/
315
void wl_register_packet_group(PacketGroupHandler* h)
316
{
317
        if (h->groupCode >= WL_MAX_PACKET_GROUPS)
318
        {
319
                WL_DEBUG_PRINT("Packet group code too large.\r\n");
320
                return;
321
        }
322
        if (wl_packet_groups[h->groupCode] != NULL)
323
        {
324
                WL_DEBUG_PRINT("Packet group code already registered.\r\n");
325
                return;
326
        }
327
        wl_packet_groups[h->groupCode] = h;
328
}
329

    
330
/**
331
 * Unregister a packet group from the wireless library.
332
 * 
333
 * @param h the packet group to remove
334
 **/
335
void wl_unregister_packet_group(PacketGroupHandler* h)
336
{
337
        unsigned int groupCode = h->groupCode;
338
        PacketGroupHandler* p = wl_packet_groups[groupCode];
339
        if (p != NULL && p->unregister != NULL)
340
                p->unregister();
341
        wl_packet_groups[groupCode] = NULL;
342
}
343

    
344
/**
345
 * Called when the timer is triggered. This calls the timeout
346
 * handlers of all the registered packet groups.
347
 **/
348
void wl_do_timeout()
349
{
350
        int i;
351
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
352
                if (wl_packet_groups[i] != NULL &&
353
                        wl_packet_groups[i]->timeout_handler != NULL)
354
                        wl_packet_groups[i]->timeout_handler();
355
}
356

    
357
/**
358
 * Performs wireless library functionality. This function must
359
 * be called frequently for wireless to perform effectively.
360
 * This function will call timeout handlers, as well as
361
 * received packet and transmit status handlers.
362
 **/
363
void wl_do()
364
{
365
        if (wl_timeout)
366
        {
367
                wl_do_timeout();
368
                wl_timeout = 0;
369
        }
370
        
371
        int len = xbee_get_packet(wl_buf);
372
        if (len < 0)//no packet received
373
                return;
374
        
375
        if (wl_buf[0] == XBEE_TX_STATUS)
376
        {
377
                if (len != 3)
378
                {
379
                        WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
380
                        return;
381
                }
382
                
383
                //the first four bits are the packet group
384
                //this only works with under 16 groups
385
                int group = (int)(wl_buf[1] >> 4);
386
                int success = 0;
387
                if (wl_buf[2] == 0)
388
                        success = 1;
389
                else
390
                {
391
                        WL_DEBUG_PRINT("No response received.\r\n");
392
                        if (wl_buf[2] == 2)
393
                        {
394
                                WL_DEBUG_PRINT("CCA Failure\r\n");
395
                        }
396
                        if (wl_buf[2] == 3)
397
                        {
398
                                WL_DEBUG_PRINT("Purged\r\n");
399
                        }
400
                }
401
                
402
                if (wl_packet_groups[group] != NULL &&
403
                                        wl_packet_groups[group]->handle_response != NULL)
404
                        wl_packet_groups[group]->handle_response(
405
                                        (int)wl_buf[1] & 0x0F, success);
406
                return;
407
        }
408
        
409
        if (wl_buf[0] == XBEE_RX)
410
        {
411
                if (len < 7)
412
                {
413
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
414
                        return;
415
                }
416
                
417
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
418
                
419
                /*
420
                //unused for now
421
                int signalStrength = wl_buf[3];
422
                //1 for Address broadcast, 2 for PAN broadcast
423
                int options = wl_buf[4];
424
                */
425
                
426
                int group = wl_buf[5];
427
                int type = wl_buf[6];
428
                int packetLen = len - 7;
429
                
430
                if (wl_packet_groups[group] != NULL
431
                                && wl_packet_groups[group]->handle_receive != NULL)
432
                        wl_packet_groups[group]->handle_receive(type, source, 
433
                                wl_buf + 7, packetLen);
434
                return;
435
        }
436
        
437
        WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
438
        return;
439
}
440

    
441

    
442
#ifndef ROBOT
443
void wl_set_com_port(char* port){
444
  xbee_set_com_port(port);
445
}
446
#endif
447

    
448