Project

General

Profile

Statistics
| Revision:

root / demos / smart_run_around_fsm / lib / src / libwireless / wireless.c @ 1784

History | View | Annotate | Download (10.9 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

    
38
#include <stddef.h>
39

    
40
#ifdef WL_DEBUG
41
#include <stdio.h>
42
#endif
43

    
44
#include "wl_defs.h"
45

    
46
#ifndef ROBOT
47
        #include <sys/time.h>
48
        #include <signal.h>
49
#else
50
        #include <time.h>
51
        #include <bom.h>
52
#endif
53

    
54
/*Function Prototypes*/
55

    
56
static void wl_do_timeout(void);
57

    
58
//Note: the actual frame sent has group as the first four bits and
59
//frame as the last four.
60
static int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame);
61

    
62
/*Data Members*/
63

    
64
//used to store incoming and outgoing packets
65
//TODO: does this need to be 128?  can it be smaller to save memory?
66
//TODO: this shouldn't be hardcoded as 128.  it should be a define.
67
static unsigned char wl_buf[128];
68
//1 if we have timed out since we last checked, 0 otherwise.
69
static int wl_timeout = 0;
70

    
71
static PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
72

    
73
#ifndef ROBOT
74

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

    
92
//called when the timer ticks
93
static void timer_handler(void)
94
{
95
        wl_timeout = 1;
96
}
97

    
98
#endif
99

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

    
113
        if (xbee_lib_init() == -1) {
114
                return -1;
115
        }
116

    
117
        //begin timeout timer
118
#ifdef ROBOT
119
#ifdef FIREFLY
120
        rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
121
#else
122
        //TODO: FIX THIS
123
        #ifdef BAYBOARD
124
        rtc_init(10 * HALF_SECOND, &timer_handler);
125
        #else
126
        rtc_init(HALF_SECOND, &timer_handler);
127
        #endif
128
#endif
129
#else
130

    
131
        //create our timer
132
        struct itimerval timer_val;
133
        struct timeval interval;
134
        interval.tv_sec = 0;
135
        interval.tv_usec = 500000;
136
        struct timeval first_time;
137
        first_time.tv_sec = 0;
138
        first_time.tv_usec = 500000;
139
        timer_val.it_interval = interval;
140
        timer_val.it_value = first_time;
141
        if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
142
        {
143
                WL_DEBUG_PRINT("Error creating a timer.\r\n");
144
                perror("Failure's cause");
145
                exit(1);
146
        }
147

    
148
        //create signal handler
149
        struct sigaction wl_sig_act;
150
        wl_sig_act.sa_handler = sig_handler;
151
        wl_sig_act.sa_flags = 0;
152
        sigemptyset(&wl_sig_act.sa_mask);
153
        sigaction(SIGALRM, &wl_sig_act, 0);
154
        sigaction(SIGINT, &wl_sig_act, 0);
155
#endif
156

    
157
        return 0;
158
}
159

    
160
/**
161
 * Uninitializes the wireless library.
162
 **/
163
void wl_terminate()
164
{
165
        int i;
166
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) {
167
                if (wl_packet_groups[i] != NULL &&
168
                        wl_packet_groups[i]->unregister != NULL) {
169
                        wl_packet_groups[i]->unregister();
170
                }
171
        }
172

    
173
        xbee_terminate();
174
}
175

    
176
/**
177
 * Set the PAN for the XBee to join.
178
 *
179
 * @param pan the new PAN
180
 *
181
 * @see wl_get_pan
182
 **/
183
int wl_set_pan(int pan)
184
{
185
        return xbee_set_pan_id(pan);
186
}
187

    
188
/**
189
 * Get the PAN the XBee is currently part of.
190
 *
191
 * @return the PAN of the XBee
192
 *
193
 * @see wl_set_pan
194
 **/
195
int wl_get_pan(void)
196
{
197
        return xbee_get_pan_id();
198
}
199

    
200
/**
201
 * Set the channel the XBee is listening to.
202
 *
203
 * @param channel the new channel to join
204
 *
205
 * @see wl_get_channel
206
 **/
207
int wl_set_channel(int channel)
208
{
209
        return xbee_set_channel(channel);
210
}
211

    
212
/**
213
 * Get the channel the XBee is part of.
214
 *
215
 * @return the channel the XBee is part of
216
 *
217
 * @see wl_set_channel
218
 **/
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
int wl_get_xbee_id()
230
{
231
        return xbee_get_address();
232
}
233

    
234
/**
235
 * Send a packet to a specific XBee without specifying a 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
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame)
245
{
246
        return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
247
}
248

    
249
/**
250
 * Send a packet to a specific XBee in the same PAN.
251
 *
252
 * @param group the packet group
253
 * @param type the packet type
254
 * @param data the packet data
255
 * @param len the packet length in bytes
256
 * @param dest the 16-bit address of the XBee to send the packet to
257
 * @param frame the frame number to see with a TX_STATUS response
258
 **/
259
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame)
260
{
261
        return wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE, frame);
262
}
263

    
264
/**
265
 * Send a packet to all XBees in all PANs.
266
 *
267
 * @param group the packet group
268
 * @param type the packet type
269
 * @param data the packet data
270
 * @param len the packet length in bytes
271
 * @param frame the frame number to see with a TX_STATUS response
272
 **/
273
int wl_send_global_packet(char group, char type, char* data, int len, char frame)
274
{
275
        return wl_send_packet(group, type, data, len, XBEE_BROADCAST, XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
276
}
277

    
278
/**
279
 * Send a packet to all XBee's in the same PAN.
280
 *
281
 * @param group the packet group
282
 * @param type the packet type
283
 * @param data the packet data
284
 * @param len the packet length in bytes
285
 * @param frame the frame number to see with a TX_STATUS response
286
 **/
287
void wl_send_pan_packet(char group, char type, char* data, int len, char frame)
288
{
289
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
290
                        XBEE_OPTIONS_NONE, frame);
291
}
292

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

    
313
        buf[0] = group;
314
        buf[1] = type;
315
        for (i = 0; i < len; i++)
316
                buf[2 + i] = data[i];
317

    
318
        return xbee_send_packet(buf, len + 2, dest, options, frame);
319
}
320

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

    
343
/**
344
 * Unregister a packet group from the wireless library.
345
 *
346
 * @param h the packet group to remove
347
 **/
348
void wl_unregister_packet_group(PacketGroupHandler* h)
349
{
350
        unsigned int groupCode = h->groupCode;
351
        PacketGroupHandler* p = wl_packet_groups[groupCode];
352
        if (p != NULL && p->unregister != NULL)
353
                p->unregister();
354
        wl_packet_groups[groupCode] = NULL;
355
}
356

    
357
/**
358
 * Called when the timer is triggered. This calls the timeout
359
 * handlers of all the registered packet groups.
360
 **/
361
void wl_do_timeout()
362
{
363
        int i;
364
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
365
                if (wl_packet_groups[i] != NULL &&
366
                        wl_packet_groups[i]->timeout_handler != NULL)
367
                        wl_packet_groups[i]->timeout_handler();
368
}
369

    
370
/**
371
 * Performs wireless library functionality. This function must
372
 * be called frequently for wireless to perform effectively.
373
 * This function will call timeout handlers, as well as
374
 * received packet and transmit status handlers.
375
 **/
376
void wl_do()
377
{
378
        if (wl_timeout)
379
        {
380
                wl_do_timeout();
381
                wl_timeout = 0;
382
        }
383

    
384
        int len = xbee_get_packet(wl_buf);
385
        if (len < 0)//no packet received
386
                return;
387

    
388
        if (wl_buf[0] == XBEE_TX_STATUS)
389
        {
390
                if (len != 3)
391
                {
392
                        WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
393
                        return;
394
                }
395

    
396
                //the first four bits are the packet group
397
                //this only works with under 16 groups
398
                int group = (int)(wl_buf[1] >> 4);
399
                int success = 0;
400
                if (wl_buf[2] == 0)
401
                {
402
                        success = 1;
403
                }
404
                else
405
                {
406
                        WL_DEBUG_PRINT("No response received.\r\n");
407
                        if (wl_buf[2] == 2)
408
                        {
409
                                WL_DEBUG_PRINT("CCA Failure\r\n");
410
                        }
411
                        if (wl_buf[2] == 3)
412
                        {
413
                                WL_DEBUG_PRINT("Purged\r\n");
414
                        }
415
                }
416

    
417
                if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_response != NULL)
418
                        wl_packet_groups[group]->handle_response((int)wl_buf[1] & 0x0F, success);
419
        }
420
        else if (wl_buf[0] == XBEE_RX)
421
        {
422
    //TODO: what does this 7 represent?  It shouldn't be hardcoded.  It should be set as a define
423
                if (len < 7)
424
                {
425
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
426
                        return;
427
                }
428

    
429
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
430

    
431
                /*
432
                //unused for now
433
                int signalStrength = wl_buf[3];
434
                //1 for Address broadcast, 2 for PAN broadcast
435
                int options = wl_buf[4];
436
                */
437

    
438
    //TODO: these indices, etc should be defined, not hardcoded
439
                int group = wl_buf[5];
440
                int type = wl_buf[6];
441
                int packetLen = len - 7;
442

    
443
                if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_receive != NULL) {
444
                        wl_packet_groups[group]->handle_receive(type, source, wl_buf + 7, packetLen);
445
                }
446
        }
447
        else
448
        {
449
                WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
450
                #ifdef WL_DEBUG
451
                #ifndef ROBOT
452
                printf("0x%2X\n", wl_buf[0]);
453
                printf("%c%c%d\n", wl_buf[2], wl_buf[3], wl_buf[4]);
454
                #endif
455
                #endif
456
        }
457
}
458

    
459

    
460
#ifndef ROBOT
461
void wl_set_com_port(char* port)
462
{
463
        xbee_set_com_port(port);
464
}
465
#endif
466