Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / xbee.c @ 399

History | View | Annotate | Download (19.2 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 xbee.c
28
 * @brief XBee Interface
29
 *
30
 * Implementation of low level communication with the XBee in API mode.
31
 *
32
 * @author Brian Coltin, Colony Project, CMU Robotics Club
33
 **/
34

    
35
#include "xbee.h"
36
#include "wl_defs.h"
37

    
38
#ifndef ROBOT
39

    
40
#include <fcntl.h>
41
#include <unistd.h>
42
#include <pthread.h>
43
#include <errno.h>
44
#include <termios.h>
45

    
46
#else
47

    
48
#include <serial.h>
49
#include <avr/interrupt.h>
50

    
51
#endif
52

    
53
#include <stdio.h>
54
#include <stdlib.h>
55
#include <string.h>
56

    
57
#define XBEE_FRAME_START 0x7E
58

    
59
/*Frame Types*/
60
#define XBEE_FRAME_STATUS 0x8A
61
#define XBEE_FRAME_AT_COMMAND 0x08
62
#define XBEE_FRAME_AT_COMMAND_RESPONSE 0x88
63
#define XBEE_FRAME_TX_REQUEST_64 0x00
64
#define XBEE_FRAME_TX_REQUEST_16 0x01
65
#define XBEE_FRAME_TX_STATUS XBEE_TX_STATUS
66
#define XBEE_FRAME_RX_64 0x80
67
#define XBEE_FRAME_RX_16 XBEE_RX
68

    
69
/*Internal Function Prototypes*/
70

    
71
/*I/O Functions*/
72
static int xbee_send(char* buf, int size);
73
static void xbee_send_string(char* c);
74

    
75
#ifndef ROBOT
76
void xbee_read(char* buf, int size);
77
#endif
78

    
79
/*Command Mode Functions
80
 * Called during initialization.
81
 */
82
void xbee_enter_command_mode(void);
83
void xbee_exit_command_mode(void);
84
void xbee_enter_api_mode(void);
85
void xbee_exit_api_mode(void);
86
void xbee_wait_for_string(char* s, int len);
87
void xbee_wait_for_ok(void);
88

    
89
/*API Mode Functions*/
90

    
91
int xbee_handle_packet(char* packet, int len);
92
void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen);
93
void xbee_handle_status(char status);
94
int xbee_verify_checksum(char* packet, int len);
95
char xbee_compute_checksum(char* packet, int len);
96
void xbee_send_frame(char* buf, int len);
97
void xbee_send_read_at_command(char* command);
98
void xbee_send_modify_at_command(char* command, char* value);
99

    
100
/*Global Variables*/
101

    
102
#ifndef ROBOT
103
static char* xbee_com_port = XBEE_PORT_DEFAULT;
104
static int xbee_stream;
105
static pthread_t* xbee_listen_thread;
106
#endif
107

    
108
// TODO: is this a good size?
109
#define XBEE_BUFFER_SIZE        256
110
// a buffer for data received from the XBee
111
char arrival_buf[XBEE_BUFFER_SIZE];
112
// location of last unread byte in buffer
113
volatile int buffer_last = 0;
114
// first unread byte in buffer
115
volatile int buffer_first = 0;
116

    
117

    
118
//used to store packets as they are read
119
char xbee_buf[128];
120
int currentBufPos = 0;
121

    
122
//XBee status
123
unsigned int xbee_panID = XBEE_PAN_DEFAULT;
124
unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
125
int xbee_channel = XBEE_CHANNEL_DEFAULT;
126
int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
127
unsigned int xbee_address = 0;
128

    
129
/*Function Implementations*/
130

    
131
#ifdef ROBOT
132

    
133
/**
134
 * Interrupt for the robot. Adds bytes received from the xbee
135
 * to the buffer.
136
 **/
137
#ifndef FIREFLY
138
ISR(USART1_RX_vect)
139
{
140
        char c = UDR1;
141
        arrival_buf[buffer_last] = c;
142
        int t = buffer_last + 1;
143
        if (t == XBEE_BUFFER_SIZE)
144
                t = 0;
145
        if (t == buffer_first)
146
        {
147
                WL_DEBUG_PRINT("Out of space in buffer.\n");
148
        }
149
        buffer_last = t;
150
}
151
#else
152
SIGNAL(SIG_USART0_RECV)
153
{
154
        char c = UDR0;
155
        arrival_buf[buffer_last] = c;
156
        int t = buffer_last + 1;
157
        if (t == XBEE_BUFFER_SIZE)
158
                t = 0;
159
        if (t == buffer_first)
160
        {
161
                WL_DEBUG_PRINT("Out of space in buffer.\n");
162
        }
163
        buffer_last = t;
164
}
165
#endif
166

    
167
#else
168

    
169
/**
170
 * Thread that listens to the xbee.
171
 **/
172
void* listen_to_xbee(void* x)
173
{
174
        char c;
175
        while (1)
176
        {
177
                xbee_read(&c, 1);
178
                arrival_buf[buffer_last] = c;
179
                int t = buffer_last + 1;
180
                if (t == XBEE_BUFFER_SIZE)
181
                        t = 0;
182
                if (t == buffer_first)
183
                {
184
                        WL_DEBUG_PRINT("Out of space in buffer.\n");
185
                }
186
                buffer_last = t;
187
        }
188
        return 0;
189
}
190

    
191
#endif
192

    
193
/**
194
 * Initializes the XBee library so that other functions may be used.
195
 **/
196
int xbee_lib_init()
197
{
198
        arrival_buf[0] = 'A';
199
        arrival_buf[1] = 'A';
200
        arrival_buf[2] = 'A';
201
        #ifdef ROBOT
202

    
203
        //enable the receiving interrupt
204
        #ifdef FIREFLY
205
        UCSR0B |= _BV(RXCIE) | _BV(RXEN);
206
        #else
207
        UCSR1B |= _BV(RXCIE);
208
        #endif
209
        sei();
210
        #else
211
        printf("Connecting to port %s.\n", xbee_com_port);
212
        xbee_stream = open(xbee_com_port, O_RDWR);
213
        if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/)
214
        {
215
                printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
216
                return -1;
217
        }
218
        
219
        // set baud rate, etc. correctly
220
        struct termios options;
221

    
222
        tcgetattr(xbee_stream, &options);
223
        cfsetispeed(&options, B9600);
224
        cfsetospeed(&options, B9600);
225
        options.c_iflag &= ~ICRNL;
226
        options.c_oflag &= ~OCRNL;
227
        options.c_cflag |= (CLOCAL | CREAD);
228
        options.c_cflag &= ~PARENB;
229
        options.c_cflag &= ~CSTOPB;
230
        options.c_cflag &= ~CSIZE;
231
        options.c_cflag |= CS8;
232
        options.c_lflag &= ~ICANON;
233
        options.c_cc[VMIN] = 1;
234
        options.c_cc[VTIME] = 50;
235

    
236
        if (tcsetattr(xbee_stream, TCSANOW, &options))
237
        {
238
                fprintf(stderr, "Error setting attributes.\n");
239
                return -1;
240
        }
241

    
242
        //lockf(xbee_stream, F_LOCK, 0);
243
        
244
        xbee_listen_thread =
245
                (pthread_t*)malloc(sizeof(pthread_t));
246
        if (xbee_listen_thread == NULL)
247
        {
248
                fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__);
249
                return -1;
250
        }
251
        
252
        int ret = pthread_create(xbee_listen_thread, NULL,
253
                listen_to_xbee, NULL);
254
        if (ret)
255
        {
256
                fprintf(stderr, "Failed to create listener thread.\r\n");
257
                return -1;
258
        }
259
        
260
        #endif
261
        xbee_enter_command_mode();
262
        xbee_enter_api_mode();
263
        xbee_exit_command_mode();
264
        xbee_send_read_at_command("MY");
265
        
266
        //wait to return until the address is set
267
        while (xbee_address == 0) xbee_get_packet(NULL);
268

    
269

    
270
        return 0;
271
}
272

    
273
/**
274
 * Call when finished using the XBee library. This releases
275
 * all sued resources.
276
 **/
277
void xbee_terminate()
278
{
279
        #ifndef ROBOT
280
        pthread_cancel(*xbee_listen_thread);
281
        free(xbee_listen_thread);
282
        lockf(xbee_stream, F_ULOCK, 0);
283
        close(xbee_stream);
284
        #endif
285
}
286

    
287
/**
288
 * Send a buffer buf of size bytes to the XBee.
289
 * 
290
 * @param buf the buffer of data to send
291
 * @param size the number of bytes to send
292
 **/
293
int xbee_send(char* buf, int size)
294
{
295
        #ifdef ROBOT
296
        int i;
297
        for (i = 0; i < size; i++)
298
                xbee_putc(buf[i]);
299
        #else
300
        int ret = write(xbee_stream, buf, size);
301
        //success
302
        if (ret == size)
303
                return 0;
304
        if (ret == -1)
305
        {
306
                //interrupted by system signal, probably timer interrupt.
307
                //just try again
308
                if (errno == 4)
309
                {
310
                        return xbee_send(buf, size);
311
                }
312
                printf("Failed to write to xbee, error %i.\r\n", errno);
313
                return -1;
314
        }
315

    
316
        //write was interrupted after writing ret bytes
317
        xbee_send(buf + ret, size - ret);
318
        #endif
319

    
320
        return 0;
321
}
322

    
323
/**
324
 * Sends a string to the XBee.
325
 *
326
 * @param c the string to send to the XBEE
327
 **/
328
void xbee_send_string(char* c)
329
{
330
        xbee_send(c, strlen(c));
331
}
332

    
333
#ifndef ROBOT
334
void xbee_read(char* buf, int size)
335
{
336
        if (read(xbee_stream, buf, size) == -1)
337
                printf("Failed to read from xbee.\r\n");
338
}
339
#endif
340

    
341
/**
342
 * Enter into command mode.
343
 **/
344
void xbee_enter_command_mode()
345
{
346
        xbee_send_string("+++");
347
        xbee_wait_for_ok();
348
}
349

    
350
/**
351
 * Exit from command mode.
352
 **/
353
void xbee_exit_command_mode()
354
{
355
        xbee_send_string("ATCN\r");
356
        xbee_wait_for_ok();
357
}
358

    
359
/**
360
 * Enter API mode.
361
 **/
362
void xbee_enter_api_mode()
363
{
364
        xbee_send_string("ATAP 1\r");
365
        xbee_wait_for_ok();
366
}
367

    
368
/**
369
 * Exit API mode. (warning - does not check for response)
370
 **/
371
void xbee_exit_api_mode()
372
{
373
        xbee_send_string("ATAP 0\r");
374
}
375

    
376
/**
377
 * Wait until the string "OK\r" is received from the XBee.
378
 **/
379
void xbee_wait_for_ok()
380
{
381
        xbee_wait_for_string("OK\r", 3);
382
}
383

    
384
/**
385
 * Delay until the specified string is received from
386
 * the XBee. Discards all other XBee data.
387
 *
388
 * @param s the string to receive
389
 * @param len the length of the string
390
 **/
391
void xbee_wait_for_string(char* s, int len)
392
{
393
        char* curr = s;
394
        while (curr - s < len)
395
        {
396
                // check if buffer is empty
397
                if (buffer_last == buffer_first)
398
                        continue;
399
                char c = arrival_buf[buffer_first++];
400
                if (buffer_first == XBEE_BUFFER_SIZE)
401
                        buffer_first = 0;
402
                if (c == *curr)
403
                        curr++;
404
                else
405
                        curr = s;
406
        }
407
}
408

    
409
/**
410
 * Verifies that the packets checksum is correct.
411
 * (If the checksum is correct, the sum of the bytes
412
 * is 0xFF.)
413
 *
414
 * @param packet the packet received. This includes the first
415
 * three bytes, which are header information from the XBee.
416
 *
417
 * @param len The length of the packet received from the XBee
418
 *
419
 * @return 0 if the checksum is incorrect, nonzero
420
 * otherwise
421
 **/
422
int xbee_verify_checksum(char* packet, int len)
423
{
424
        unsigned char sum = 0;
425
        int i;
426
        for (i = 3; i < len; i++)
427
                sum += (unsigned char)packet[i];
428
        return sum == 0xFF;
429
}
430

    
431
/**
432
 * Returns the checksum of the given packet.
433
 *
434
 * @param buf the data for the packet to send
435
 * @param len the length of the packet in bytes
436
 *
437
 * @return the checksum of the packet, which will
438
 * become the last byte sent in the packet
439
 **/
440
char xbee_compute_checksum(char* buf, int len)
441
{
442
        int i;
443
        unsigned char sum = 0;
444
        for (i = 0; i < len; i++)
445
                sum += (unsigned char)buf[i];
446
        return 0xFF - sum;
447
}
448

    
449
/**
450
 * Adds header information and checksum to the given
451
 * packet and sends it. Header information includes
452
 * XBEE_FRAME_START and the packet length, as two bytes.
453
 *
454
 * @param buf the packet data
455
 * @param len the size in bytes of the packet data
456
 *
457
 **/
458
void xbee_send_frame(char* buf, int len)
459
{
460
        char prefix[3];
461
        prefix[0] = XBEE_FRAME_START;
462
        prefix[1] = (len & 0xFF00) >> 8;
463
        prefix[2] = len & 0xFF;
464
        char checksum = xbee_compute_checksum(buf, len);
465
        xbee_send(prefix, 3);
466
        xbee_send(buf, len);
467
        xbee_send(&checksum, 1);
468
}
469

    
470
/**
471
 * Sends an AT command to read a parameter.
472
 *
473
 * @param command the AT command to send. For exmaple,
474
 * use ID to read the PAN ID and MY to return the XBee ID.
475
 * See the XBee reference guide for a complete listing.
476
 **/
477
void xbee_send_read_at_command(char* command)
478
{
479
        xbee_send_modify_at_command(command, NULL);
480
}
481

    
482
/**
483
 * Sends the given AT command.
484
 *
485
 * @param command the AT command to send (e.g., MY, ID)
486
 * @param value the value to pass as a parameter
487
 * (or NULL if there is no parameter)
488
 **/
489
void xbee_send_modify_at_command(char* command, char* value)
490
{
491
        char buf[16];
492
        int i;
493
        
494
        buf[0] = XBEE_FRAME_AT_COMMAND;
495
        buf[1] = 1;
496
        buf[2] = command[0];
497
        buf[3] = command[1];
498
        int valueLen = 0;
499
        if (value != NULL)
500
        {
501
                valueLen = strlen(value);
502
                if (valueLen > 8)
503
                {
504
                        WL_DEBUG_PRINT("AT Command too large.\r\n");
505
                        return;
506
                }
507
                for (i = 0; i < valueLen; i++)
508
                        buf[4 + i] = value[i];
509
        }
510
        xbee_send_frame(buf, 4 + valueLen);
511
}
512

    
513
/**
514
 * Send the specified packet.
515
 * 
516
 * @param packet the packet data to send
517
 * @param len the number of bytes in the packet
518
 * 
519
 * @param dest the ID of the XBee to send the packet to,
520
 * or XBEE_BROADCAST to send the message to all robots
521
 * in the PAN.
522
 * 
523
 * @param options a combination of the flags
524
 * XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and 
525
 * XBEE_OPTIONS_BROADCAST_ALL_PANS
526
 *
527
 * @param frame the frame number to associate this packet
528
 * with. This will be used to identify the response when
529
 * the XBee alerts us as to whether or not our message
530
 * was received.
531
 **/
532
int xbee_send_packet(char* packet, int len, int dest, char options, char frame)
533
{
534
        char buf[5];
535
        char prefix[3];
536
        int i;
537
        unsigned char checksum = 0;
538

    
539
        if (len > 100)
540
        {
541
                WL_DEBUG_PRINT("Packet is too large.\r\n");
542
                return -1;
543
        }
544

    
545
        //data for sending request
546
        buf[0] = XBEE_FRAME_TX_REQUEST_16;
547
        buf[1] = frame;
548
        buf[2] = (dest >> 8) & 0xFF;
549
        buf[3] = dest & 0xFF;
550
        buf[4] = options;
551

    
552
        //packet prefix, do this here so we don't need an extra buffer
553
        prefix[0] = XBEE_FRAME_START;
554
        prefix[1] = ((5 + len) & 0xFF00) >> 8;
555
        prefix[2] = (5 + len) & 0xFF;
556

    
557
        for (i = 0; i < 5; i++)
558
                checksum += (unsigned char)buf[i];
559
        for (i = 0; i < len; i++)
560
                checksum += (unsigned char)packet[i];
561
        checksum = 0xFF - checksum;
562

    
563
        if (xbee_send(prefix, 3) != 0) {
564
                return -1;
565
        }
566

    
567
        if (xbee_send(buf, 5) != 0) {
568
                return -1;
569
        }
570

    
571
        if (xbee_send(packet, len) != 0) {
572
                return -1;
573
        }
574

    
575
        if (xbee_send((char*)&checksum, 1) != 0) {
576
                return -1;
577
        }
578

    
579
        return 0;
580
}
581

    
582
/**
583
 * Reads a packet received from the XBee. This function
584
 * is non-blocking. The resulting packet is stored in dest.
585
 * Only returns transmission response packets and
586
 * received packets. The returned packet does not include
587
 * header information or the checksum. This method also
588
 * handles special packets dealt with by the XBee library,
589
 * and so should be called frequently while the XBee is in
590
 * use.<br><br>
591
 *
592
 * The first byte of the packet will be either
593
 * XBEE_TX_STATUS or XBEE_RX to indicated
594
 * a response to a sent message or a received message, 
595
 * respectively.<br><br>
596
 *
597
 * For a status response packet:<br>
598
 * The first byte will be XBEE_TX_STATUS.<br>
599
 * The second byte will be the frame number.<br>
600
 * The third byte will be the result. 0 indicates success,
601
 * and nonzero indicates that an error ocurred in 
602
 * transmitting the packet.<br><br>
603
 *
604
 * For a received packet:<br>
605
 * The first byte will be XBEE_RX.<br>
606
 * The second and third bytes will be the 16-bit
607
 * address of the packet's sender.<br>
608
 * The fourth byte is the signal strength.<br>
609
 * The fifth byte is 1 if the packet were sent to
610
 * a specific address, and 2 if it is a broadcast packet.<br><br>
611
 * 
612
 * @param dest set to the packet data
613
 * @return the length of the packet, or -1 if no packet
614
 * is available
615
 **/
616
int xbee_get_packet(unsigned char* dest)
617
{
618
        //start reading a packet with XBEE_FRAME_START
619
        if (currentBufPos == 0)
620
        {
621
                do
622
                {
623
                        if (buffer_first == XBEE_BUFFER_SIZE)
624
                                buffer_first = 0;
625
                        // check if buffer is empty
626
                        if (buffer_first == buffer_last)
627
                                return -1;
628
                }
629
                while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
630
                if (buffer_first == XBEE_BUFFER_SIZE)
631
                        buffer_first = 0;
632
                xbee_buf[0] = XBEE_FRAME_START;
633
                currentBufPos++;
634
        }
635

    
636
        int len = -1;
637
        if (currentBufPos >= 3)
638
                len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
639
                
640
        while (len == -1 //packet length has not been read yet
641
                        || currentBufPos < len + 4)
642
        {
643
                if (currentBufPos == 3)
644
                {
645
                        len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
646
                        if (len > 120)
647
                        {
648
                                WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n");
649
                                currentBufPos = 0;
650
                                return -1;
651
                        }
652
                }
653
                // check if buffer is empty
654
                if (buffer_first == buffer_last)
655
                        return -1;
656
                xbee_buf[currentBufPos++] = arrival_buf[buffer_first++];
657
                if (buffer_first == XBEE_BUFFER_SIZE)
658
                        buffer_first = 0;
659
        }
660
        
661
        currentBufPos = 0;
662
        
663
        if (!xbee_verify_checksum(xbee_buf, len + 4))
664
        {
665
                WL_DEBUG_PRINT("XBee checksum failed.\r\n");
666
                return -1;
667
        }
668

    
669
        //we will take care of the packet
670
        if (xbee_handle_packet(xbee_buf + 3, len))
671
                return -1;
672
        
673
        if (dest == NULL)
674
                return -1;
675
        
676
        int i;
677
        for (i = 3; i < len + 3; i++)
678
                dest[i - 3] = xbee_buf[i];
679
        return len;
680
}
681

    
682
/**
683
 * Handles modem status packets.
684
 *
685
 * @param status the type of status packet received.
686
 **/
687
void xbee_handle_status(char status)
688
{
689
        switch (status)
690
        {
691
                case 0:
692
                        WL_DEBUG_PRINT("XBee hardware reset.\r\n");
693
                        break;
694
                case 1:
695
                        WL_DEBUG_PRINT("Watchdog timer reset.\r\n");
696
                        break;
697
                case 2:
698
                        WL_DEBUG_PRINT("Associated.\r\n");
699
                        break;
700
                case 3:
701
                        WL_DEBUG_PRINT("Disassociated.\r\n");
702
                        break;
703
                case 4:
704
                        WL_DEBUG_PRINT("Synchronization lost.\r\n");
705
                        break;
706
                case 5:
707
                        WL_DEBUG_PRINT("Coordinator realignment.\r\n");
708
                        break;
709
                case 6:
710
                        WL_DEBUG_PRINT("Coordinator started.\r\n");
711
                        break;
712
        }
713
}
714

    
715
/**
716
 * Handles AT command response packets.
717
 * @param command the two character AT command, e.g. MY or ID
718
 * @param result 0 for success, 1 for an error
719
 * @param extra the hex value of the requested register
720
 * @param extraLen the length in bytes of extra
721
 **/
722
void xbee_handle_at_command_response(char* command, char result,
723
        char* extra, int extraLen)
724
{
725
        if (result == 1)
726
        {
727
                WL_DEBUG_PRINT("Error with AT");
728
                WL_DEBUG_PRINT(command);
729
                WL_DEBUG_PRINT(" packet.\r\n");
730
        }
731
        WL_DEBUG_PRINT("AT");
732
        WL_DEBUG_PRINT(command);
733
        WL_DEBUG_PRINT(" command was successful.\r\n");
734
                
735
        if (command[0] == 'I' && command[1] == 'D')
736
        {
737
                xbee_panID = xbee_pending_panID;
738
                WL_DEBUG_PRINT("PAN ID set to ");
739
                WL_DEBUG_PRINT_INT(xbee_panID);
740
                WL_DEBUG_PRINT(".\r\n");
741
                return;
742
        }
743

    
744
        if (command[0] == 'C' && command[1] == 'H')
745
        {
746
                xbee_channel = xbee_pending_channel;
747
                WL_DEBUG_PRINT("Channel set to ");
748
                WL_DEBUG_PRINT_INT(xbee_channel);
749
                WL_DEBUG_PRINT(".\r\n");
750
                return;
751
        }
752
        
753
        if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0)
754
        {
755
                xbee_address = 0;
756
                int i;
757
                for (i = 0; i < extraLen; i++)
758
                        xbee_address = (xbee_address << 8) + extra[i];
759

    
760
                WL_DEBUG_PRINT("XBee address is ");
761
                WL_DEBUG_PRINT_INT(xbee_address);
762
                WL_DEBUG_PRINT(".\r\n");
763

    
764
                if (xbee_address == 0)
765
                {
766
                        WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n");
767
                        exit(0);
768
                }
769
        }
770
}
771

    
772
/**
773
 * Attempts to handle the packet if it is dealt with
774
 * by the library.
775
 * We will handle the following packet types:
776
 *    Modem Status
777
 *    AT Command Response
778
 *
779
 * @param packet the packet to handle
780
 * @param len the length of the packet
781
 * 
782
 * @return 1 if we have handled the packet, 0 otherwise
783
 */
784
int xbee_handle_packet(char* packet, int len)
785
{
786
        char command[3] = {1, 2, 3};
787
        if (len <= 0) //this should not happend
788
        {
789
                WL_DEBUG_PRINT("Non-positive packet length.\r\n");
790
                return 0;
791
        }
792
        
793
        switch ((unsigned char)packet[0]) //packet type
794
        {
795
                case XBEE_FRAME_STATUS:
796
                        xbee_handle_status(packet[1]);
797
                        return 1;
798
                case XBEE_FRAME_AT_COMMAND_RESPONSE:
799
                        command[0] = packet[2];
800
                        command[1] = packet[3];
801
                        command[2] = 0;
802
                        xbee_handle_at_command_response(command,
803
                                packet[4], packet + 5, len - 5);
804
                        return 1;
805
        }
806
        return 0;
807
}
808

    
809
/**
810
 * Sets the personal area network id.
811
 *
812
 * @param id the new personal area network (PAN) id
813
 **/
814
void xbee_set_pan_id(int id)
815
{
816
        char s[3];
817
        s[0] = (id >> 8) & 0xFF;
818
        s[1] = id & 0xFF;
819
        s[2] = 0;
820
        xbee_pending_panID = id;
821
        xbee_send_modify_at_command("ID", s);
822
}
823

    
824
/**
825
 * Get the PAN ID for the XBee.
826
 * 
827
 * @return the personal area network id, or
828
 * XBEE_PAN_DEFAULT if it has not yet been set.
829
 **/
830
unsigned int xbee_get_pan_id()
831
{
832
        return xbee_panID;
833
}
834

    
835
/**
836
 * Set the channel the XBee is using.
837
 *
838
 * @param channel the channel the XBee will not use, 
839
 * between 0x0B and 0x1A
840
 *
841
 * @see xbee_get_channel
842
 **/
843
void xbee_set_channel(int channel)
844
{
845
        if (channel < 0x0B || channel > 0x1A)
846
        {
847
                WL_DEBUG_PRINT("Channel out of range.\r\n");
848
                return;
849
        }
850
        char s[3];
851
        s[0] = channel & 0xFF;
852
        s[1] = 0;
853
        xbee_pending_channel = channel;
854
        xbee_send_modify_at_command("CH", s);
855
}
856

    
857
/**
858
 * Returns the channel which the XBee is currently using.
859
 *
860
 * @return the channel the XBee is using
861
 *
862
 * @see xbee_set_channel
863
 **/
864
int xbee_get_channel(void)
865
{
866
        return xbee_channel;
867
}
868

    
869
/**
870
 * Get the 16-bit address of the XBee.
871
 * This is used to specify who to send messages to
872
 * and who messages are from.
873
 *
874
 * @return the 16-bit address of the XBee.
875
 **/
876
unsigned int xbee_get_address()
877
{
878
        return xbee_address;
879
}
880

    
881
#ifndef ROBOT
882
void xbee_set_com_port(char* port)
883
{
884
        xbee_com_port = port;
885
}
886
#endif
887