Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libwireless / wireless.c @ 18

History | View | Annotate | Download (8 KB)

1
#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
#include <time.h>
10
#include <signal.h>
11
#else
12
#include <time.h>
13
#include <bom.h>
14
#endif
15

    
16
/*Function Prototypes*/
17

    
18
void wl_do_timeout(void);
19

    
20
//Note: the actual frame sent has group as the first four bits and
21
//frame as the last four.
22
void wl_send_packet(char group, char type, char* data, int len,
23
                                        int dest, char options, char frame);
24

    
25
/*Data Members*/
26

    
27
//used to store incoming and outgoing packets
28
unsigned char wl_buf[128];
29
//1 if we have timed out since we last checked, 0 otherwise.
30
int wl_timeout = 0;
31

    
32
PacketGroupHandler* wl_packet_groups[WL_MAX_PACKET_GROUPS];
33

    
34
#ifndef ROBOT
35
timer_t wl_timeout_timer;
36

    
37
//called when we time out, or receive interrupt
38
void sig_handler(int signo)
39
{
40
        switch (signo)
41
        {
42
                case SIGALRM:
43
                        wl_timeout = 1;
44
                        break;
45
                case SIGINT:
46
                        wl_terminate();
47
                        exit(1);
48
                        break;
49
        }
50
        return;
51
}
52
#endif
53

    
54
/**
55
 * Initializes the wireless library. Must be called before any
56
 * other function.
57
 **/
58
void wl_init()
59
{
60
        int i;
61
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
62
                wl_packet_groups[i] = NULL;
63

    
64
        xbee_lib_init(WL_DEFAULT_PAN);
65
        
66
        //begin timeout timer
67
        #ifdef ROBOT
68
        rtc_init(HALF_SECOND, &wl_do_timeout); 
69
        #else
70
        //create a timer to trigger every half second
71
        struct sigevent evp;
72
        evp.sigev_signo = SIGALRM;
73
        evp.sigev_notify = SIGEV_SIGNAL;
74
        if (timer_create(CLOCK_REALTIME, &evp, &wl_timeout_timer) == -1)
75
        { 
76
                WL_DEBUG_PRINT("Error creating a timer.\r\n"); 
77
                exit(1); 
78
        }
79
        struct sigaction wl_sig_act;
80
        wl_sig_act.sa_handler = (void *)sig_handler;
81
        wl_sig_act.sa_flags = 0;
82
        sigemptyset(&wl_sig_act.sa_mask);
83
        sigaction(SIGALRM, &wl_sig_act, 0);
84
        sigaction(SIGINT, &wl_sig_act, 0);
85
        struct itimerspec wl_timeout_time;
86
        wl_timeout_time.it_interval.tv_sec = 0;
87
        wl_timeout_time.it_interval.tv_nsec = 500000000;
88
        wl_timeout_time.it_value.tv_sec = 0;
89
        wl_timeout_time.it_value.tv_nsec = 500000000;
90
        timer_settime(wl_timeout_timer, 0,
91
                        &wl_timeout_time, NULL);
92
        #endif
93
}
94

    
95
/**
96
 * Uninitializes the wireless library.
97
 **/
98
void wl_terminate()
99
{
100
        #ifndef ROBOT
101
        timer_delete(wl_timeout_timer);
102
        #endif
103
        
104
        int i;
105
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
106
                if (wl_packet_groups[i] != NULL &&
107
                        wl_packet_groups[i]->unregister != NULL)
108
                        wl_packet_groups[i]->unregister();
109
        
110
        xbee_terminate();
111
}
112

    
113
/**
114
 * Returns the 16-bit address of the XBee module.
115
 *
116
 * @return the 16-bit address of the XBee module.
117
 **/
118
unsigned int wl_get_xbee_id()
119
{
120
        return xbee_get_address();
121
}
122

    
123
/**
124
 * Send a packet to a specific XBee without specifying a PAN.
125
 *
126
 * @param group the packet group
127
 * @param type the packet type
128
 * @param data the packet data
129
 * @param len the packet length in bytes
130
 * @param dest the 16-bit address of the XBee to send the packet to
131
 * @param frame the frame number to see with a TX_STATUS response
132
 **/
133
void wl_send_robot_to_robot_global_packet(char group, char type,
134
                char* data, int len, int dest, char frame)
135
{
136
        wl_send_packet(group, type, data, len, dest,
137
                        XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
138
}
139

    
140
/**
141
 * Send a packet to a specific XBee in the same PAN.
142
 *
143
 * @param group the packet group
144
 * @param type the packet type
145
 * @param data the packet data
146
 * @param len the packet length in bytes
147
 * @param dest the 16-bit address of the XBee to send the packet to
148
 * @param frame the frame number to see with a TX_STATUS response
149
 **/
150
void wl_send_robot_to_robot_packet(char group, char type,
151
                char* data, int len, int dest, char frame)
152
{
153
        wl_send_packet(group, type, data, len, dest, XBEE_OPTIONS_NONE,
154
                        frame);
155
}
156

    
157
/**
158
 * Send a packet to all XBees in all PANs.
159
 *
160
 * @param group the packet group
161
 * @param type the packet type
162
 * @param data the packet data
163
 * @param len the packet length in bytes
164
 * @param frame the frame number to see with a TX_STATUS response
165
 **/
166
void wl_send_global_packet(char group, char type,
167
                char* data, int len, char frame)
168
{
169
        wl_send_packet(group, type, data, len, XBEE_BROADCAST,
170
                        XBEE_OPTIONS_BROADCAST_ALL_PANS, frame);
171
}
172

    
173
/**
174
 * Send a packet to all XBee's in the same PAN.
175
 *
176
 * @param group the packet group
177
 * @param type the packet type
178
 * @param data the packet data
179
 * @param len the packet length in bytes
180
 * @param frame the frame number to see with a TX_STATUS response
181
 **/
182
void wl_send_pan_packet(char group, char type,
183
                char* data, int len, char frame)
184
{
185
        wl_send_packet(group, type, data, len, XBEE_BROADCAST, 
186
                        XBEE_OPTIONS_NONE, frame);
187
}
188

    
189
/**
190
 * Send a packet.
191
 *
192
 * @param group the packet group
193
 * @param type the packet type
194
 * @param data the packet data
195
 * @param len the packet length in bytes
196
 * @param dest the destination of the packet
197
 * @param options the options for sending the packet
198
 * @param frame the frame number to see with a TX_STATUS response
199
 **/
200
void wl_send_packet(char group, char type, char* data, int len,
201
                                        int dest, char options, char frame)
202
{
203
        char buf[128];
204
        int i;
205
        if (frame != 0)
206
                frame = (frame & 0x0F) | ((group & 0x0F) << 4);
207
        buf[0] = group;
208
        buf[1] = type;
209
        for (i = 0; i < len; i++)
210
                buf[2 + i] = data[i];
211
        xbee_send_packet(buf, len + 2, dest, options, frame);
212
}
213

    
214
/**
215
 * Register a packet group with the wireless library. The event
216
 * handlers in the packet group will be called whenever an
217
 * event dealing with the packet group's group code occurs.
218
 *
219
 * @param h the PacketGroupHandler to register
220
 **/
221
void wl_register_packet_group(PacketGroupHandler* h)
222
{
223
        if (h->groupCode >= WL_MAX_PACKET_GROUPS)
224
        {
225
                WL_DEBUG_PRINT("Packet group code too large.\r\n");
226
                return;
227
        }
228
        if (wl_packet_groups[h->groupCode] != NULL)
229
        {
230
                WL_DEBUG_PRINT("Packet group code already registered.\r\n");
231
                return;
232
        }
233
        wl_packet_groups[h->groupCode] = h;
234
}
235

    
236
/**
237
 * Unregister a packet group from the wireless library.
238
 * 
239
 * @param h the packet group to remove
240
 **/
241
void wl_unregister_packet_group(PacketGroupHandler* h)
242
{
243
        unsigned int groupCode = h->groupCode;
244
        PacketGroupHandler* p = wl_packet_groups[groupCode];
245
        if (p != NULL && p->unregister != NULL)
246
                p->unregister();
247
        wl_packet_groups[groupCode] = NULL;
248
}
249

    
250
/**
251
 * Called when the timer is triggered. This calls the timeout
252
 * handlers of all the registered packet groups.
253
 **/
254
void wl_do_timeout()
255
{
256
        int i;
257
        for (i = 0; i < WL_MAX_PACKET_GROUPS; i++)
258
                if (wl_packet_groups[i] != NULL &&
259
                        wl_packet_groups[i]->timeout_handler != NULL)
260
                        wl_packet_groups[i]->timeout_handler();
261
}
262

    
263
/**
264
 * Performs wireless library functionality. This function must
265
 * be called frequently for wireless to perform effectively.
266
 * This function will call timeout handlers, as well as
267
 * received packet and transmit status handlers.
268
 **/
269
void wl_do()
270
{
271
        if (wl_timeout)
272
        {
273
                wl_do_timeout();
274
                wl_timeout = 0;
275
        }
276
        
277
        int len = xbee_get_packet(wl_buf);
278
        if (len < 0)//no packet received
279
                return;
280
        
281
        if (wl_buf[0] == XBEE_TX_STATUS)
282
        {
283
                if (len != 3)
284
                {
285
                        WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n");
286
                        return;
287
                }
288
                
289
                //the first four bits are the packet group
290
                //this only works with under 16 groups
291
                int group = (int)(wl_buf[1] >> 4);
292
                int success = 0;
293
                if (wl_buf[2] == 0)
294
                        success = 1;
295
                else
296
                {
297
                        WL_DEBUG_PRINT("No response received.\r\n");
298
                        if (wl_buf[2] == 2)
299
                                WL_DEBUG_PRINT("CCA Failure\r\n");
300
                        if (wl_buf[2] == 3)
301
                                WL_DEBUG_PRINT("Purged\r\n");
302
                }
303
                
304
                if (wl_packet_groups[group] != NULL &&
305
                                        wl_packet_groups[group]->handle_response != NULL)
306
                        wl_packet_groups[group]->handle_response(
307
                                        (int)wl_buf[1] & 0x0F, success);
308
                return;
309
        }
310
        
311
        if (wl_buf[0] == XBEE_RX)
312
        {
313
                if (len < 7)
314
                {
315
                        WL_DEBUG_PRINT("Packet is too small.\r\n");
316
                        return;
317
                }
318
                
319
                int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]);
320
                
321
                /*
322
                //unused for now
323
                int signalStrength = wl_buf[3];
324
                //1 for Address broadcast, 2 for PAN broadcast
325
                int options = wl_buf[4];
326
                */
327
                
328
                int group = wl_buf[5];
329
                int type = wl_buf[6];
330
                int packetLen = len - 7;
331
                
332
                if (wl_packet_groups[group] != NULL
333
                                && wl_packet_groups[group]->handle_receive != NULL)
334
                        wl_packet_groups[group]->handle_receive(type, source, 
335
                                wl_buf + 7, packetLen);
336
                return;
337
        }
338
        
339
        WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n");
340
        return;
341
}
342