Project

General

Profile

Statistics
| Revision:

root / branches / autonomous_recharging / code / lib / src / libwireless / wireless.c @ 743

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
int wl_init()
102
{
103
        int i;
104
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
105
                wl_packet_groups[i] = NULL;
106

    
107
        if (xbee_lib_init() == -1) {
108
          return -1;
109
        }
110

    
111
        //begin timeout timer
112
        #ifdef ROBOT
113
        #ifdef FIREFLY
114
        rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
115
        #else
116
        rtc_init(HALF_SECOND, &timer_handler);
117
        #endif
118
        #else
119

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

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

    
146
        return 0;
147
}
148

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

    
160
        xbee_terminate();
161
}
162

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

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

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

    
199
/**
200
 * Get the channel the XBee is part of.
201
 *
202
 * @return the channel the XBee is part of
203
 *
204
 * @see wl_set_channel
205
 **/
206
int wl_get_channel(void)
207
{
208
        return xbee_get_channel();
209
}
210

    
211
/**
212
 * Returns the 16-bit address of the XBee module.
213
 *
214
 * @return the 16-bit address of the XBee module.
215
 **/
216
int wl_get_xbee_id()
217
{
218
        return xbee_get_address();
219
}
220

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

    
238
/**
239
 * Send a packet to a specific XBee in the same PAN.
240
 *
241
 * @param group the packet group
242
 * @param type the packet type
243
 * @param data the packet data
244
 * @param len the packet length in bytes
245
 * @param dest the 16-bit address of the XBee to send the packet to
246
 * @param frame the frame number to see with a TX_STATUS response
247
 **/
248
void wl_send_robot_to_robot_packet(char group, char type,
249
                char* data, int len, int dest, char frame)
250
{
251
        wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
252
                        frame);
253
}
254

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

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

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

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

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

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

    
361
/**
362
 * Performs wireless library functionality. This function must
363
 * be called frequently for wireless to perform effectively.
364
 * This function will call timeout handlers, as well as
365
 * received packet and transmit status handlers.
366
 **/
367
void wl_do()
368
{
369
        if (wl_timeout)
370
        {
371
                wl_do_timeout();
372
                wl_timeout = 0;
373
        }
374

    
375
        int len = xbee_get_packet(wl_buf);
376
        if (len < 0)//no packet received
377
                return;
378

    
379
        if (wl_buf[0] == XBEE_TX_STATUS)
380
        {
381
                if (len != 3)
382
                {
383
                        WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
384
                        return;
385
                }
386

    
387
                //the first four bits are the packet group
388
                //this only works with under 16 groups
389
                int group = (int)(wl_buf[1] >> 4);
390
                int success = 0;
391
                if (wl_buf[2] == 0)
392
                        success = 1;
393
                else
394
                {
395
                        WL_DEBUG_PRINT("No response received.\r\n");
396
                        if (wl_buf[2] == 2)
397
                        {
398
                                WL_DEBUG_PRINT("CCA Failure\r\n");
399
                        }
400
                        if (wl_buf[2] == 3)
401
                        {
402
                                WL_DEBUG_PRINT("Purged\r\n");
403
                        }
404
                }
405

    
406
                if (wl_packet_groups[group] != NULL &&
407
                                        wl_packet_groups[group]->handle_response != NULL)
408
                        wl_packet_groups[group]->handle_response(
409
                                        (int)wl_buf[1] & 0x0F, success);
410
                return;
411
        }
412

    
413
        if (wl_buf[0] == XBEE_RX)
414
        {
415
                if (len < 7)
416
                {
417
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
418
                        return;
419
                }
420

    
421
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
422

    
423
                /*
424
                //unused for now
425
                int signalStrength = wl_buf[3];
426
                //1 for Address broadcast, 2 for PAN broadcast
427
                int options = wl_buf[4];
428
                */
429

    
430
                int group = wl_buf[5];
431
                int type = wl_buf[6];
432
                int packetLen = len - 7;
433

    
434
                if (wl_packet_groups[group] != NULL
435
                                && wl_packet_groups[group]->handle_receive != NULL)
436
                        wl_packet_groups[group]->handle_receive(type, source,
437
                                wl_buf + 7, packetLen);
438
                return;
439
        }
440

    
441
        WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
442
        return;
443
}
444

    
445

    
446
#ifndef ROBOT
447
void wl_set_com_port(char* port)
448
{
449
        xbee_set_com_port(port);
450
}
451
#endif
452