Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / wireless.c @ 409

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

    
60
/*Data Members*/
61

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

    
67
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
68

    
69
#ifndef ROBOT
70

    
71
//called when we time out, or receive interrupt
72
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
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
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
106
                wl_packet_groups[i] = NULL;
107

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

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

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

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

    
147
        return 0;
148
}
149

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

    
161
        xbee_terminate();
162
}
163

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

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

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

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

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

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

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

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

    
270
/**
271
 * Send a packet to all XBee's in the same PAN.
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
void wl_send_pan_packet(char group, char type,
280
                char* data, int len, char frame)
281
{
282
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
283
                        XBEE_OPTIONS_NONE, frame);
284
}
285

    
286
/**
287
 * Send a packet.
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 dest the destination of the packet
294
 * @param options the options for sending the packet
295
 * @param frame the frame number to see with a TX_STATUS response
296
 **/
297
int wl_send_packet(char group, char type, char* data, int len, int dest, char options, char frame)
298
{
299
        char buf[128];
300
        int i;
301
        if (frame != 0)
302
                frame = (frame & 0x0F) | ((group & 0x0F) << 4);
303

    
304
        buf[0] = group;
305
        buf[1] = type;
306
        for (i = 0; i < len; i++)
307
                buf[2 + i] = data[i];
308

    
309
        return 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 && wl_packet_groups[group]->handle_response != NULL)
407
                        wl_packet_groups[group]->handle_response((int)wl_buf[1] & 0x0F, success);
408
                return;
409
        }
410

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

    
419
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
420

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

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

    
432
                if (wl_packet_groups[group] != NULL && wl_packet_groups[group]->handle_receive != NULL)
433
                        wl_packet_groups[group]->handle_receive(type, source, 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
{
445
        xbee_set_com_port(port);
446
}
447
#endif
448