Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (8.74 KB)

1 17 bcoltin
#include "wireless.h"
2
#include "xbee.h"
3
#include <stdlib.h>
4
#include <stdio.h>
5
6
#include "wl_defs.h"
7
8
#ifndef ROBOT
9 109 bcoltin
#include <sys/time.h>
10 17 bcoltin
#include <signal.h>
11
#else
12
#include <time.h>
13 86 bcoltin
#ifndef FIREFLY
14 17 bcoltin
#include <bom.h>
15
#endif
16 86 bcoltin
#endif
17 17 bcoltin
18
/*Function Prototypes*/
19
20
void wl_do_timeout(void);
21
22
//Note: the actual frame sent has group as the first four bits and
23
//frame as the last four.
24
void wl_send_packet(char group, char type, char* data, int len,
25
                                        int dest, char options, char frame);
26
27
/*Data Members*/
28
29
//used to store incoming and outgoing packets
30
unsigned char wl_buf[128];
31
//1 if we have timed out since we last checked, 0 otherwise.
32
int wl_timeout = 0;
33
34
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
35
36
#ifndef ROBOT
37
38
//called when we time out, or receive interrupt
39
void sig_handler(int signo)
40
{
41
        switch (signo)
42
        {
43
                case SIGALRM:
44
                        wl_timeout = 1;
45
                        break;
46
                case SIGINT:
47
                        wl_terminate();
48
                        exit(1);
49
                        break;
50
        }
51
        return;
52
}
53 166 bcoltin
#else
54
55
//called when the timer ticks
56
void timer_handler(void)
57
{
58
        wl_timeout = 1;
59
}
60
61 17 bcoltin
#endif
62
63
/**
64
 * Initializes the wireless library. Must be called before any
65
 * other function.
66
 **/
67
void wl_init()
68
{
69
        int i;
70
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
71
                wl_packet_groups[i] = NULL;
72
73 60 bcoltin
        xbee_lib_init();
74 17 bcoltin
75
        //begin timeout timer
76
        #ifdef ROBOT
77 86 bcoltin
        #ifdef FIREFLY
78 166 bcoltin
        rtc_init(PRESCALE_DIV_256, 32, &timer_handler);
79 86 bcoltin
        #else
80 166 bcoltin
        rtc_init(HALF_SECOND, &timer_handler);
81 86 bcoltin
        #endif
82 17 bcoltin
        #else
83 109 bcoltin
84
        //create our timer
85
        struct itimerval timer_val;
86
        struct timeval interval;
87
        interval.tv_sec = 0;
88
        interval.tv_usec = 500000;
89
        struct timeval first_time;
90
        first_time.tv_sec = 0;
91
        first_time.tv_usec = 500000;
92
        timer_val.it_interval = interval;
93
        timer_val.it_value = first_time;
94
        if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1)
95 119 bcoltin
        {
96 17 bcoltin
                WL_DEBUG_PRINT("Error creating a timer.\r\n");
97 109 bcoltin
                perror("Failure's cause");
98 17 bcoltin
                exit(1);
99
        }
100 109 bcoltin
101
        //create signal handler
102 17 bcoltin
        struct sigaction wl_sig_act;
103 162 jknichel
        wl_sig_act.sa_handler = sig_handler;
104 17 bcoltin
        wl_sig_act.sa_flags = 0;
105
        sigemptyset(&wl_sig_act.sa_mask);
106
        sigaction(SIGALRM, &wl_sig_act, 0);
107
        sigaction(SIGINT, &wl_sig_act, 0);
108
        #endif
109
}
110
111
/**
112
 * Uninitializes the wireless library.
113
 **/
114
void wl_terminate()
115
{
116
        int i;
117
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
118
                if (wl_packet_groups[i] != NULL &&
119
                        wl_packet_groups[i]->unregister != NULL)
120
                        wl_packet_groups[i]->unregister();
121
122
        xbee_terminate();
123
}
124
125
/**
126 60 bcoltin
 * Set the PAN for the XBee to join.
127
 *
128
 * @param pan the new PAN
129
 *
130
 * @see wl_get_pan
131
 **/
132
void wl_set_pan(int pan)
133
{
134
        xbee_set_pan_id(pan);
135
}
136
137
/**
138
 * Get the PAN the XBee is currently part of.
139
 *
140
 * @return the PAN of the XBee
141
 *
142
 * @see wl_set_pan
143
 **/
144
int wl_get_pan(void)
145
{
146
        return xbee_get_pan_id();
147
}
148
149
/**
150
 * Set the channel the XBee is listening to.
151
 *
152
 * @param channel the new channel to join
153
 *
154
 * @see wl_get_channel
155
 **/
156
void wl_set_channel(int channel)
157
{
158
        xbee_set_channel(channel);
159
}
160
161
/**
162
 * Get the channel the XBee is part of.
163
 *
164
 * @return the channel the XBee is part of
165
 *
166
 * @see wl_set_channel
167
 **/
168
int wl_get_channel(void)
169
{
170
        return xbee_get_channel();
171
}
172
173
/**
174 17 bcoltin
 * Returns the 16-bit address of the XBee module.
175
 *
176
 * @return the 16-bit address of the XBee module.
177
 **/
178
unsigned int wl_get_xbee_id()
179
{
180
        return xbee_get_address();
181
}
182
183
/**
184
 * Send a packet to a specific XBee without specifying a PAN.
185
 *
186
 * @param group the packet group
187
 * @param type the packet type
188
 * @param data the packet data
189
 * @param len the packet length in bytes
190
 * @param dest the 16-bit address of the XBee to send the packet to
191
 * @param frame the frame number to see with a TX_STATUS response
192
 **/
193
void wl_send_robot_to_robot_global_packet(char group, char type,
194
                char* data, int len, int dest, char frame)
195
{
196
        wl_send_packet(group, type, data, len, dest,
197
                        XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
198
}
199
200
/**
201
 * Send a packet to a specific XBee in the same PAN.
202
 *
203
 * @param group the packet group
204
 * @param type the packet type
205
 * @param data the packet data
206
 * @param len the packet length in bytes
207
 * @param dest the 16-bit address of the XBee to send the packet to
208
 * @param frame the frame number to see with a TX_STATUS response
209
 **/
210
void wl_send_robot_to_robot_packet(char group, char type,
211
                char* data, int len, int dest, char frame)
212
{
213
        wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
214
                        frame);
215
}
216
217
/**
218
 * Send a packet to all XBees in all PANs.
219
 *
220
 * @param group the packet group
221
 * @param type the packet type
222
 * @param data the packet data
223
 * @param len the packet length in bytes
224
 * @param frame the frame number to see with a TX_STATUS response
225
 **/
226
void wl_send_global_packet(char group, char type,
227
                char* data, int len, char frame)
228
{
229
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
230
                        XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
231
}
232
233
/**
234
 * Send a packet to all XBee's in the same PAN.
235
 *
236
 * @param group the packet group
237
 * @param type the packet type
238
 * @param data the packet data
239
 * @param len the packet length in bytes
240
 * @param frame the frame number to see with a TX_STATUS response
241
 **/
242
void wl_send_pan_packet(char group, char type,
243
                char* data, int len, char frame)
244
{
245
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
246
                        XBEE_OPTIONS_NONE, frame);
247
}
248
249
/**
250
 * Send a packet.
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 destination of the packet
257
 * @param options the options for sending the packet
258
 * @param frame the frame number to see with a TX_STATUS response
259
 **/
260
void wl_send_packet(char group, char type, char* data, int len,
261
                                        int dest, char options, char frame)
262
{
263
        char buf[128];
264
        int i;
265
        if (frame != 0)
266
                frame = (frame & 0x0F) | ((group & 0x0F) << 4);
267
        buf[0] = group;
268
        buf[1] = type;
269
        for (i = 0; i < len; i++)
270
                buf[2 + i] = data[i];
271
        xbee_send_packet(buf, len + 2, dest, options, frame);
272
}
273
274
/**
275
 * Register a packet group with the wireless library. The event
276
 * handlers in the packet group will be called whenever an
277
 * event dealing with the packet group's group code occurs.
278
 *
279
 * @param h the PacketGroupHandler to register
280
 **/
281
void wl_register_packet_group(PacketGroupHandler* h)
282
{
283
        if (h->groupCode >= WL_MAX_PACKET_GROUPS)
284
        {
285
                WL_DEBUG_PRINT("Packet group code too large.\r\n");
286
                return;
287
        }
288
        if (wl_packet_groups[h->groupCode] != NULL)
289
        {
290
                WL_DEBUG_PRINT("Packet group code already registered.\r\n");
291
                return;
292
        }
293
        wl_packet_groups[h->groupCode] = h;
294
}
295
296
/**
297
 * Unregister a packet group from the wireless library.
298
 *
299
 * @param h the packet group to remove
300
 **/
301
void wl_unregister_packet_group(PacketGroupHandler* h)
302
{
303
        unsigned int groupCode = h->groupCode;
304
        PacketGroupHandler* p = wl_packet_groups[groupCode];
305
        if (p != NULL && p->unregister != NULL)
306
                p->unregister();
307
        wl_packet_groups[groupCode] = NULL;
308
}
309
310
/**
311
 * Called when the timer is triggered. This calls the timeout
312
 * handlers of all the registered packet groups.
313
 **/
314
void wl_do_timeout()
315
{
316
        int i;
317
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
318
                if (wl_packet_groups[i] != NULL &&
319
                        wl_packet_groups[i]->timeout_handler != NULL)
320
                        wl_packet_groups[i]->timeout_handler();
321
}
322
323
/**
324
 * Performs wireless library functionality. This function must
325
 * be called frequently for wireless to perform effectively.
326
 * This function will call timeout handlers, as well as
327
 * received packet and transmit status handlers.
328
 **/
329
void wl_do()
330
{
331
        if (wl_timeout)
332
        {
333
                wl_do_timeout();
334
                wl_timeout = 0;
335
        }
336
337
        int len = xbee_get_packet(wl_buf);
338
        if (len < 0)//no packet received
339
                return;
340
341
        if (wl_buf[0] == XBEE_TX_STATUS)
342
        {
343
                if (len != 3)
344
                {
345
                        WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
346
                        return;
347
                }
348
349
                //the first four bits are the packet group
350
                //this only works with under 16 groups
351
                int group = (int)(wl_buf[1] >> 4);
352
                int success = 0;
353
                if (wl_buf[2] == 0)
354
                        success = 1;
355
                else
356
                {
357
                        WL_DEBUG_PRINT("No response received.\r\n");
358
                        if (wl_buf[2] == 2)
359 119 bcoltin
                        {
360 17 bcoltin
                                WL_DEBUG_PRINT("CCA Failure\r\n");
361 119 bcoltin
                        }
362 17 bcoltin
                        if (wl_buf[2] == 3)
363 119 bcoltin
                        {
364 17 bcoltin
                                WL_DEBUG_PRINT("Purged\r\n");
365 119 bcoltin
                        }
366 17 bcoltin
                }
367
368
                if (wl_packet_groups[group] != NULL &&
369
                                        wl_packet_groups[group]->handle_response != NULL)
370
                        wl_packet_groups[group]->handle_response(
371
                                        (int)wl_buf[1] & 0x0F, success);
372
                return;
373
        }
374
375
        if (wl_buf[0] == XBEE_RX)
376
        {
377
                if (len < 7)
378
                {
379
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
380
                        return;
381
                }
382
383
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
384
385
                /*
386
                //unused for now
387
                int signalStrength = wl_buf[3];
388
                //1 for Address broadcast, 2 for PAN broadcast
389
                int options = wl_buf[4];
390
                */
391
392
                int group = wl_buf[5];
393
                int type = wl_buf[6];
394
                int packetLen = len - 7;
395
396
                if (wl_packet_groups[group] != NULL
397
                                && wl_packet_groups[group]->handle_receive != NULL)
398
                        wl_packet_groups[group]->handle_receive(type, source,
399
                                wl_buf + 7, packetLen);
400
                return;
401
        }
402
403
        WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
404
        return;
405
}
406
407 203 justin
408
#ifndef ROBOT
409
void wl_set_com_port(char* port){
410
  xbee_set_com_port(port);
411
}
412
#endif
413