Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (12.2 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
        rtc_init(HALF_SECOND, &timer_handler);
119
#endif
120
#else
121

    
122
        //create our timer
123
        struct itimerval timer_val;
124
        struct timeval interval;
125
        interval.tv_sec = 0;
126
        interval.tv_usec = 500000;
127
        struct timeval first_time;
128
        first_time.tv_sec = 0;
129
        first_time.tv_usec = 500000;
130
        timer_val.it_interval = interval;
131
        timer_val.it_value = first_time;
132
        if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
133
        {
134
                WL_DEBUG_PRINT("Error creating a timer.\r\n");
135
                perror("Failure's cause");
136
                exit(1);
137
        }
138

    
139
        //create signal handler
140
        struct sigaction wl_sig_act;
141
        wl_sig_act.sa_handler = sig_handler;
142
        wl_sig_act.sa_flags = 0;
143
        sigemptyset(&wl_sig_act.sa_mask);
144
        sigaction(SIGALRM, &wl_sig_act, 0);
145
        sigaction(SIGINT, &wl_sig_act, 0);
146
#endif
147

    
148
        return 0;
149
}
150

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

    
164
        xbee_terminate();
165
}
166

    
167
/**
168
 * Set the PAN for the XBee to join.
169
 *
170
 * @param pan the new PAN
171
 *
172
 * @see wl_get_pan
173
 **/
174
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
175
// it reduces code size or not should be done to be sure.
176
int wl_set_pan(int pan)
177
{
178
        return xbee_set_pan_id(pan);
179
}
180

    
181
/**
182
 * Get the PAN the XBee is currently part of.
183
 *
184
 * @return the PAN of the XBee
185
 *
186
 * @see wl_set_pan
187
 **/
188
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
189
// it reduces code size or not should be done to be sure.
190
int wl_get_pan(void)
191
{
192
        return xbee_get_pan_id();
193
}
194

    
195
/**
196
 * Set the channel the XBee is listening to.
197
 *
198
 * @param channel the new channel to join
199
 *
200
 * @see wl_get_channel
201
 **/
202
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
203
// it reduces code size or not should be done to be sure.
204
int wl_set_channel(int channel)
205
{
206
        return xbee_set_channel(channel);
207
}
208

    
209
/**
210
 * Get the channel the XBee is part of.
211
 *
212
 * @return the channel the XBee is part of
213
 *
214
 * @see wl_set_channel
215
 **/
216
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
217
// it reduces code size or not should be done to be sure.
218
int wl_get_channel(void)
219
{
220
        return xbee_get_channel();
221
}
222

    
223
/**
224
 * Returns the 16-bit address of the XBee module.
225
 *
226
 * @return the 16-bit address of the XBee module.
227
 **/
228
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
229
// it reduces code size or not should be done to be sure.
230
int wl_get_xbee_id()
231
{
232
        return xbee_get_address();
233
}
234

    
235
/**
236
 * Send a packet to a specific XBee without specifying a PAN.
237
 *
238
 * @param group the packet group
239
 * @param type the packet type
240
 * @param data the packet data
241
 * @param len the packet length in bytes
242
 * @param dest the 16-bit address of the XBee to send the packet to
243
 * @param frame the frame number to see with a TX_STATUS response
244
 **/
245
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
246
// it reduces code size or not should be done to be sure.
247
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame)
248
{
249
        return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
250
}
251

    
252
/**
253
 * Send a packet to a specific XBee in the same PAN.
254
 *
255
 * @param group the packet group
256
 * @param type the packet type
257
 * @param data the packet data
258
 * @param len the packet length in bytes
259
 * @param dest the 16-bit address of the XBee to send the packet to
260
 * @param frame the frame number to see with a TX_STATUS response
261
 **/
262
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
263
// it reduces code size or not should be done to be sure.
264
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame)
265
{
266
        return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE, frame);
267
}
268

    
269
/**
270
 * Send a packet to all XBees in all PANs.
271
 *
272
 * @param group the packet group
273
 * @param type the packet type
274
 * @param data the packet data
275
 * @param len the packet length in bytes
276
 * @param frame the frame number to see with a TX_STATUS response
277
 **/
278
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
279
// it reduces code size or not should be done to be sure.
280
int wl_send_global_packet(char group, char type, char* data, int len, char frame)
281
{
282
        return wl_send_packet(group, type, data, len, XBEE_BROADCAST, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
283
}
284

    
285
/**
286
 * Send a packet to all XBee's in the same PAN.
287
 *
288
 * @param group the packet group
289
 * @param type the packet type
290
 * @param data the packet data
291
 * @param len the packet length in bytes
292
 * @param frame the frame number to see with a TX_STATUS response
293
 **/
294
//TODO: this function is so simple, it *may* be beneficial to inline this function.  testing of if
295
// it reduces code size or not should be done to be sure.
296
void wl_send_pan_packet(char group, char type, char* data, int len, char frame)
297
{
298
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
299
                        XBEE_OPTIONS_NONE, frame);
300
}
301

    
302
/**
303
 * Send a packet.
304
 *
305
 * @param group the packet group
306
 * @param type the packet type
307
 * @param data the packet data
308
 * @param len the packet length in bytes
309
 * @param dest the destination of the packet
310
 * @param options the options for sending the packet
311
 * @param frame the frame number to see with a TX_STATUS response
312
 **/
313
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame)
314
{
315
  //TODO: does this need to be 128?  can it be smaller to save memory?
316
  //TODO: this shouldn't be hardcoded as 128.  it should be a define.
317
        char buf[128];
318
        int i;
319
        if (frame != 0)
320
                frame = (frame & 0x0F) | ((group & 0x0F) << 4);
321

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

    
327
        return xbee_send_packet(buf, len + 2, dest, options, frame);
328
}
329

    
330
/**
331
 * Register a packet group with the wireless library. The event
332
 * handlers in the packet group will be called whenever an
333
 * event dealing with the packet group's group code occurs.
334
 *
335
 * @param h the PacketGroupHandler to register
336
 **/
337
void wl_register_packet_group(PacketGroupHandler* h)
338
{
339
        if (h->groupCode >= WL_MAX_PACKET_GROUPS)
340
        {
341
                WL_DEBUG_PRINT("Packet group code too large.\r\n");
342
                return;
343
        }
344
        if (wl_packet_groups[h->groupCode] != NULL)
345
        {
346
                WL_DEBUG_PRINT("Packet group code already registered.\r\n");
347
                return;
348
        }
349
        wl_packet_groups[h->groupCode] = h;
350
}
351

    
352
/**
353
 * Unregister a packet group from the wireless library.
354
 *
355
 * @param h the packet group to remove
356
 **/
357
void wl_unregister_packet_group(PacketGroupHandler* h)
358
{
359
        unsigned int groupCode = h->groupCode;
360
        PacketGroupHandler* p = wl_packet_groups[groupCode];
361
        if (p != NULL && p->unregister != NULL)
362
                p->unregister();
363
        wl_packet_groups[groupCode] = NULL;
364
}
365

    
366
/**
367
 * Called when the timer is triggered. This calls the timeout
368
 * handlers of all the registered packet groups.
369
 **/
370
void wl_do_timeout()
371
{
372
        int i;
373
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
374
                if (wl_packet_groups[i] != NULL &&
375
                        wl_packet_groups[i]->timeout_handler != NULL)
376
                        wl_packet_groups[i]->timeout_handler();
377
}
378

    
379
/**
380
 * Performs wireless library functionality. This function must
381
 * be called frequently for wireless to perform effectively.
382
 * This function will call timeout handlers, as well as
383
 * received packet and transmit status handlers.
384
 **/
385
void wl_do()
386
{
387
        if (wl_timeout)
388
        {
389
                wl_do_timeout();
390
                wl_timeout = 0;
391
        }
392

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

    
397
        if (wl_buf[0] == XBEE_TX_STATUS)
398
        {
399
                if (len != 3)
400
                {
401
                        WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
402
                        return;
403
                }
404

    
405
                //the first four bits are the packet group
406
                //this only works with under 16 groups
407
                int group = (int)(wl_buf[1] >> 4);
408
                int success = 0;
409
                if (wl_buf[2] == 0)
410
                {
411
                        success = 1;
412
                }
413
                else
414
                {
415
                        WL_DEBUG_PRINT("No response received.\r\n");
416
                        if (wl_buf[2] == 2)
417
                        {
418
                                WL_DEBUG_PRINT("CCA Failure\r\n");
419
                        }
420
                        if (wl_buf[2] == 3)
421
                        {
422
                                WL_DEBUG_PRINT("Purged\r\n");
423
                        }
424
                }
425

    
426
                if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_response != NULL)
427
                        wl_packet_groups[group]->handle_response((int)wl_buf[1] & 0x0F, success);
428
        }
429
        else if (wl_buf[0] == XBEE_RX)
430
        {
431
    //TODO: what does this 7 represent?  It shouldn't be hardcoded.  It should be set as a define
432
                if (len < 7)
433
                {
434
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
435
                        return;
436
                }
437

    
438
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
439

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

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

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

    
462

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