Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / projects / libwireless / lib / wireless.c @ 883

History | View | Annotate | Download (12.3 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
        #include <bom.h>
48
#endif
49

    
50
/*Function Prototypes*/
51

    
52
static void wl_do_timeout(void);
53

    
54
//Note: the actual frame sent has group as the first four bits and
55
//frame as the last four.
56
static int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame);
57

    
58
/*Data Members*/
59

    
60
//used to store incoming and outgoing packets
61
//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
static unsigned char wl_buf[128];
64
//1 if we have timed out since we last checked, 0 otherwise.
65
static int wl_timeout = 0;
66

    
67
static PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
68

    
69
#ifndef ROBOT
70

    
71
//called when we time out, or receive interrupt
72
static 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
}
86
#else
87

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

    
94
#endif
95

    
96
/**
97
 * Initializes the wireless library. Must be called before any
98
 * other function.
99
 *
100
 * @param wl_port File descriptor for wireless port, or NULL for default.
101
 **/
102
int wl_init()
103
{
104
        int i;
105
  //TODO: using memset here instead of this loop, *might* be less instructions and *might* reduce code size but not sure
106
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
107
                wl_packet_groups[i] = NULL;
108

    
109
        if (xbee_lib_init() == -1) {
110
                return -1;
111
        }
112

    
113
        //begin timeout timer
114
#ifdef ROBOT
115
#ifdef FIREFLY
116
        rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
117
#else
118
        //TODO: FIX THIS
119
        rtc_init(10 * HALF_SECOND, &timer_handler);
120
#endif
121
#else
122

    
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
        }
139

    
140
        //create signal handler
141
        struct sigaction wl_sig_act;
142
        wl_sig_act.sa_handler = sig_handler;
143
        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
#endif
148

    
149
        return 0;
150
}
151

    
152
/**
153
 * Uninitializes the wireless library.
154
 **/
155
void wl_terminate()
156
{
157
        int i;
158
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) {
159
                if (wl_packet_groups[i] != NULL &&
160
                        wl_packet_groups[i]->unregister != NULL) {
161
                        wl_packet_groups[i]->unregister();
162
                }
163
        }
164

    
165
        xbee_terminate();
166
}
167

    
168
/**
169
 * Set the PAN for the XBee to join.
170
 *
171
 * @param pan the new PAN
172
 *
173
 * @see wl_get_pan
174
 **/
175
//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
int wl_set_pan(int pan)
178
{
179
        return xbee_set_pan_id(pan);
180
}
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
//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
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
//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
int wl_set_channel(int channel)
206
{
207
        return xbee_set_channel(channel);
208
}
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
//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
int wl_get_channel(void)
220
{
221
        return xbee_get_channel();
222
}
223

    
224
/**
225
 * Returns the 16-bit address of the XBee module.
226
 *
227
 * @return the 16-bit address of the XBee module.
228
 **/
229
//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
int wl_get_xbee_id()
232
{
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
//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
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame)
249
{
250
        return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
251
}
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
//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
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame)
266
{
267
        return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE, frame);
268
}
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
//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
int wl_send_global_packet(char group, char type, char* data, int len, char frame)
282
{
283
        return wl_send_packet(group, type, data, len, XBEE_BROADCAST, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
284
}
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
//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
void wl_send_pan_packet(char group, char type, char* data, int len, char frame)
298
{
299
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
300
                        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
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame)
315
{
316
  //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
        char buf[128];
319
        int i;
320
        if (frame != 0)
321
                frame = (frame & 0x0F) | ((group & 0x0F) << 4);
322

    
323
        buf[0] = group;
324
        buf[1] = type;
325
        for (i = 0; i < len; i++)
326
                buf[2 + i] = data[i];
327

    
328
        return xbee_send_packet(buf, len + 2, dest, options, frame);
329
}
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
 *
356
 * @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

    
394
        int len = xbee_get_packet(wl_buf);
395
        if (len < 0)//no packet received
396
                return;
397

    
398
        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

    
406
                //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
                {
412
                        success = 1;
413
                }
414
                else
415
                {
416
                        WL_DEBUG_PRINT("No response received.\r\n");
417
                        if (wl_buf[2] == 2)
418
                        {
419
                                WL_DEBUG_PRINT("CCA Failure\r\n");
420
                        }
421
                        if (wl_buf[2] == 3)
422
                        {
423
                                WL_DEBUG_PRINT("Purged\r\n");
424
                        }
425
                }
426

    
427
                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
        }
430
        else if (wl_buf[0] == XBEE_RX)
431
        {
432
                usb_puti(len);
433
                usb_puts("Got packet!\n\r");
434
    //TODO: what does this 7 represent?  It shouldn't be hardcoded.  It should be set as a define
435
                if (len < 7)
436
                {
437
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
438
                        return;
439
                }
440

    
441
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
442

    
443
                /*
444
                //unused for now
445
                int signalStrength = wl_buf[3];
446
                //1 for Address broadcast, 2 for PAN broadcast
447
                int options = wl_buf[4];
448
                */
449

    
450
    //TODO: these indices, etc should be defined, not hardcoded
451
                int group = wl_buf[5];
452
                int type = wl_buf[6];
453
                int packetLen = len - 7;
454

    
455
                if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_receive != NULL) {
456
                        wl_packet_groups[group]->handle_receive(type, source, wl_buf + 7, packetLen);
457
                }
458
        }
459
        else
460
        {
461
                WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
462
        }
463
}
464

    
465

    
466
#ifndef ROBOT
467
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
468
// it reduces code size or not should be done to be sure.
469
void wl_set_com_port(char* port)
470
{
471
        xbee_set_com_port(port);
472
}
473
#endif
474