root / trunk / code / projects / libwireless / lib / xbee.c @ 412
History | View | Annotate | Download (19.3 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 |
static void xbee_enter_command_mode(void); |
83 |
static void xbee_exit_command_mode(void); |
84 |
static void xbee_enter_api_mode(void); |
85 |
static void xbee_exit_api_mode(void); |
86 |
static void xbee_wait_for_string(char* s, int len); |
87 |
static 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 |
usleep(1000);
|
189 |
} |
190 |
return 0; |
191 |
} |
192 |
|
193 |
#endif
|
194 |
|
195 |
/**
|
196 |
* Initializes the XBee library so that other functions may be used.
|
197 |
**/
|
198 |
int xbee_lib_init()
|
199 |
{ |
200 |
arrival_buf[0] = 'A'; |
201 |
arrival_buf[1] = 'A'; |
202 |
arrival_buf[2] = 'A'; |
203 |
#ifdef ROBOT
|
204 |
|
205 |
//enable the receiving interrupt
|
206 |
#ifdef FIREFLY
|
207 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
208 |
#else
|
209 |
UCSR1B |= _BV(RXCIE); |
210 |
#endif
|
211 |
sei(); |
212 |
#else
|
213 |
printf("Connecting to port %s.\n", xbee_com_port);
|
214 |
xbee_stream = open(xbee_com_port, O_RDWR); |
215 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
216 |
{ |
217 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port);
|
218 |
return -1; |
219 |
} |
220 |
|
221 |
// set baud rate, etc. correctly
|
222 |
struct termios options;
|
223 |
|
224 |
tcgetattr(xbee_stream, &options); |
225 |
cfsetispeed(&options, B9600); |
226 |
cfsetospeed(&options, B9600); |
227 |
options.c_iflag &= ~ICRNL; |
228 |
options.c_oflag &= ~OCRNL; |
229 |
options.c_cflag |= (CLOCAL | CREAD); |
230 |
options.c_cflag &= ~PARENB; |
231 |
options.c_cflag &= ~CSTOPB; |
232 |
options.c_cflag &= ~CSIZE; |
233 |
options.c_cflag |= CS8; |
234 |
options.c_lflag &= ~ICANON; |
235 |
options.c_cc[VMIN] = 1;
|
236 |
options.c_cc[VTIME] = 50;
|
237 |
|
238 |
if (tcsetattr(xbee_stream, TCSANOW, &options))
|
239 |
{ |
240 |
fprintf(stderr, "Error setting attributes.\n");
|
241 |
return -1; |
242 |
} |
243 |
|
244 |
//lockf(xbee_stream, F_LOCK, 0);
|
245 |
|
246 |
xbee_listen_thread = (pthread_t*)malloc(sizeof(pthread_t));
|
247 |
if (xbee_listen_thread == NULL) |
248 |
{ |
249 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__);
|
250 |
return -1; |
251 |
} |
252 |
|
253 |
int ret = pthread_create(xbee_listen_thread, NULL, listen_to_xbee, NULL); |
254 |
if (ret)
|
255 |
{ |
256 |
fprintf(stderr, "Failed to create listener thread.\r\n");
|
257 |
return -1; |
258 |
} |
259 |
#endif
|
260 |
|
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 |
return 0; |
270 |
} |
271 |
|
272 |
/**
|
273 |
* Call when finished using the XBee library. This releases
|
274 |
* all sued resources.
|
275 |
**/
|
276 |
void xbee_terminate()
|
277 |
{ |
278 |
#ifndef ROBOT
|
279 |
pthread_cancel(*xbee_listen_thread); |
280 |
free(xbee_listen_thread); |
281 |
lockf(xbee_stream, F_ULOCK, 0);
|
282 |
close(xbee_stream); |
283 |
#endif
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Send a buffer buf of size bytes to the XBee.
|
288 |
*
|
289 |
* @param buf the buffer of data to send
|
290 |
* @param size the number of bytes to send
|
291 |
**/
|
292 |
int xbee_send(char* buf, int size) |
293 |
{ |
294 |
#ifdef ROBOT
|
295 |
int i;
|
296 |
for (i = 0; i < size; i++) |
297 |
xbee_putc(buf[i]); |
298 |
#else
|
299 |
int ret = write(xbee_stream, buf, size);
|
300 |
//success
|
301 |
if (ret == size)
|
302 |
return 0; |
303 |
if (ret == -1) |
304 |
{ |
305 |
//interrupted by system signal, probably timer interrupt.
|
306 |
//just try again
|
307 |
if (errno == 4) |
308 |
{ |
309 |
return xbee_send(buf, size);
|
310 |
} |
311 |
printf("Failed to write to xbee, error %i.\r\n", errno);
|
312 |
return -1; |
313 |
} |
314 |
|
315 |
//write was interrupted after writing ret bytes
|
316 |
xbee_send(buf + ret, size - ret); |
317 |
#endif
|
318 |
|
319 |
return 0; |
320 |
} |
321 |
|
322 |
/**
|
323 |
* Sends a string to the XBee.
|
324 |
*
|
325 |
* @param c the string to send to the XBEE
|
326 |
**/
|
327 |
void xbee_send_string(char* c) |
328 |
{ |
329 |
xbee_send(c, strlen(c)); |
330 |
} |
331 |
|
332 |
#ifndef ROBOT
|
333 |
void xbee_read(char* buf, int size) |
334 |
{ |
335 |
if (read(xbee_stream, buf, size) == -1) |
336 |
printf("Failed to read from xbee.\r\n");
|
337 |
} |
338 |
#endif
|
339 |
|
340 |
/**
|
341 |
* Enter into command mode.
|
342 |
**/
|
343 |
static void xbee_enter_command_mode() |
344 |
{ |
345 |
xbee_send_string("+++");
|
346 |
xbee_wait_for_ok(); |
347 |
} |
348 |
|
349 |
/**
|
350 |
* Exit from command mode.
|
351 |
**/
|
352 |
static void xbee_exit_command_mode() |
353 |
{ |
354 |
xbee_send_string("ATCN\r");
|
355 |
xbee_wait_for_ok(); |
356 |
} |
357 |
|
358 |
/**
|
359 |
* Enter API mode.
|
360 |
**/
|
361 |
static void xbee_enter_api_mode() |
362 |
{ |
363 |
xbee_send_string("ATAP 1\r");
|
364 |
xbee_wait_for_ok(); |
365 |
} |
366 |
|
367 |
/**
|
368 |
* Exit API mode. (warning - does not check for response)
|
369 |
**/
|
370 |
static void xbee_exit_api_mode() |
371 |
{ |
372 |
xbee_send_string("ATAP 0\r");
|
373 |
} |
374 |
|
375 |
/**
|
376 |
* Wait until the string "OK\r" is received from the XBee.
|
377 |
**/
|
378 |
static void xbee_wait_for_ok() |
379 |
{ |
380 |
xbee_wait_for_string("OK\r", 3); |
381 |
} |
382 |
|
383 |
/**
|
384 |
* Delay until the specified string is received from
|
385 |
* the XBee. Discards all other XBee data.
|
386 |
*
|
387 |
* @param s the string to receive
|
388 |
* @param len the length of the string
|
389 |
**/
|
390 |
static void xbee_wait_for_string(char* s, int len) |
391 |
{ |
392 |
char* curr = s;
|
393 |
while (curr - s < len) {
|
394 |
// check if buffer is empty
|
395 |
if (buffer_last != buffer_first) {
|
396 |
char c = arrival_buf[buffer_first++];
|
397 |
if (buffer_first == XBEE_BUFFER_SIZE) {
|
398 |
buffer_first = 0;
|
399 |
} |
400 |
|
401 |
if (c == *curr) {
|
402 |
curr++; |
403 |
} else {
|
404 |
curr = s; |
405 |
} |
406 |
} |
407 |
|
408 |
#ifndef ROBOT
|
409 |
usleep(1000);
|
410 |
#endif
|
411 |
} |
412 |
} |
413 |
|
414 |
/**
|
415 |
* Verifies that the packets checksum is correct.
|
416 |
* (If the checksum is correct, the sum of the bytes
|
417 |
* is 0xFF.)
|
418 |
*
|
419 |
* @param packet the packet received. This includes the first
|
420 |
* three bytes, which are header information from the XBee.
|
421 |
*
|
422 |
* @param len The length of the packet received from the XBee
|
423 |
*
|
424 |
* @return 0 if the checksum is incorrect, nonzero
|
425 |
* otherwise
|
426 |
**/
|
427 |
int xbee_verify_checksum(char* packet, int len) |
428 |
{ |
429 |
unsigned char sum = 0; |
430 |
int i;
|
431 |
for (i = 3; i < len; i++) |
432 |
sum += (unsigned char)packet[i]; |
433 |
return sum == 0xFF; |
434 |
} |
435 |
|
436 |
/**
|
437 |
* Returns the checksum of the given packet.
|
438 |
*
|
439 |
* @param buf the data for the packet to send
|
440 |
* @param len the length of the packet in bytes
|
441 |
*
|
442 |
* @return the checksum of the packet, which will
|
443 |
* become the last byte sent in the packet
|
444 |
**/
|
445 |
char xbee_compute_checksum(char* buf, int len) |
446 |
{ |
447 |
int i;
|
448 |
unsigned char sum = 0; |
449 |
for (i = 0; i < len; i++) |
450 |
sum += (unsigned char)buf[i]; |
451 |
return 0xFF - sum; |
452 |
} |
453 |
|
454 |
/**
|
455 |
* Adds header information and checksum to the given
|
456 |
* packet and sends it. Header information includes
|
457 |
* XBEE_FRAME_START and the packet length, as two bytes.
|
458 |
*
|
459 |
* @param buf the packet data
|
460 |
* @param len the size in bytes of the packet data
|
461 |
*
|
462 |
**/
|
463 |
void xbee_send_frame(char* buf, int len) |
464 |
{ |
465 |
char prefix[3]; |
466 |
prefix[0] = XBEE_FRAME_START;
|
467 |
prefix[1] = (len & 0xFF00) >> 8; |
468 |
prefix[2] = len & 0xFF; |
469 |
char checksum = xbee_compute_checksum(buf, len);
|
470 |
xbee_send(prefix, 3);
|
471 |
xbee_send(buf, len); |
472 |
xbee_send(&checksum, 1);
|
473 |
} |
474 |
|
475 |
/**
|
476 |
* Sends an AT command to read a parameter.
|
477 |
*
|
478 |
* @param command the AT command to send. For exmaple,
|
479 |
* use ID to read the PAN ID and MY to return the XBee ID.
|
480 |
* See the XBee reference guide for a complete listing.
|
481 |
**/
|
482 |
void xbee_send_read_at_command(char* command) |
483 |
{ |
484 |
xbee_send_modify_at_command(command, NULL);
|
485 |
} |
486 |
|
487 |
/**
|
488 |
* Sends the given AT command.
|
489 |
*
|
490 |
* @param command the AT command to send (e.g., MY, ID)
|
491 |
* @param value the value to pass as a parameter
|
492 |
* (or NULL if there is no parameter)
|
493 |
**/
|
494 |
void xbee_send_modify_at_command(char* command, char* value) |
495 |
{ |
496 |
char buf[16]; |
497 |
int i;
|
498 |
|
499 |
buf[0] = XBEE_FRAME_AT_COMMAND;
|
500 |
buf[1] = 1; |
501 |
buf[2] = command[0]; |
502 |
buf[3] = command[1]; |
503 |
int valueLen = 0; |
504 |
if (value != NULL) |
505 |
{ |
506 |
valueLen = strlen(value); |
507 |
if (valueLen > 8) |
508 |
{ |
509 |
WL_DEBUG_PRINT("AT Command too large.\r\n");
|
510 |
return;
|
511 |
} |
512 |
for (i = 0; i < valueLen; i++) |
513 |
buf[4 + i] = value[i];
|
514 |
} |
515 |
xbee_send_frame(buf, 4 + valueLen);
|
516 |
} |
517 |
|
518 |
/**
|
519 |
* Send the specified packet.
|
520 |
*
|
521 |
* @param packet the packet data to send
|
522 |
* @param len the number of bytes in the packet
|
523 |
*
|
524 |
* @param dest the ID of the XBee to send the packet to,
|
525 |
* or XBEE_BROADCAST to send the message to all robots
|
526 |
* in the PAN.
|
527 |
*
|
528 |
* @param options a combination of the flags
|
529 |
* XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and
|
530 |
* XBEE_OPTIONS_BROADCAST_ALL_PANS
|
531 |
*
|
532 |
* @param frame the frame number to associate this packet
|
533 |
* with. This will be used to identify the response when
|
534 |
* the XBee alerts us as to whether or not our message
|
535 |
* was received.
|
536 |
**/
|
537 |
int xbee_send_packet(char* packet, int len, int dest, char options, char frame) |
538 |
{ |
539 |
char buf[5]; |
540 |
char prefix[3]; |
541 |
int i;
|
542 |
unsigned char checksum = 0; |
543 |
|
544 |
if (len > 100) |
545 |
{ |
546 |
WL_DEBUG_PRINT("Packet is too large.\r\n");
|
547 |
return -1; |
548 |
} |
549 |
|
550 |
//data for sending request
|
551 |
buf[0] = XBEE_FRAME_TX_REQUEST_16;
|
552 |
buf[1] = frame;
|
553 |
buf[2] = (dest >> 8) & 0xFF; |
554 |
buf[3] = dest & 0xFF; |
555 |
buf[4] = options;
|
556 |
|
557 |
//packet prefix, do this here so we don't need an extra buffer
|
558 |
prefix[0] = XBEE_FRAME_START;
|
559 |
prefix[1] = ((5 + len) & 0xFF00) >> 8; |
560 |
prefix[2] = (5 + len) & 0xFF; |
561 |
|
562 |
for (i = 0; i < 5; i++) |
563 |
checksum += (unsigned char)buf[i]; |
564 |
for (i = 0; i < len; i++) |
565 |
checksum += (unsigned char)packet[i]; |
566 |
checksum = 0xFF - checksum;
|
567 |
|
568 |
if (xbee_send(prefix, 3) != 0) { |
569 |
return -1; |
570 |
} |
571 |
|
572 |
if (xbee_send(buf, 5) != 0) { |
573 |
return -1; |
574 |
} |
575 |
|
576 |
if (xbee_send(packet, len) != 0) { |
577 |
return -1; |
578 |
} |
579 |
|
580 |
if (xbee_send((char*)&checksum, 1) != 0) { |
581 |
return -1; |
582 |
} |
583 |
|
584 |
return 0; |
585 |
} |
586 |
|
587 |
/**
|
588 |
* Reads a packet received from the XBee. This function
|
589 |
* is non-blocking. The resulting packet is stored in dest.
|
590 |
* Only returns transmission response packets and
|
591 |
* received packets. The returned packet does not include
|
592 |
* header information or the checksum. This method also
|
593 |
* handles special packets dealt with by the XBee library,
|
594 |
* and so should be called frequently while the XBee is in
|
595 |
* use.<br><br>
|
596 |
*
|
597 |
* The first byte of the packet will be either
|
598 |
* XBEE_TX_STATUS or XBEE_RX to indicated
|
599 |
* a response to a sent message or a received message,
|
600 |
* respectively.<br><br>
|
601 |
*
|
602 |
* For a status response packet:<br>
|
603 |
* The first byte will be XBEE_TX_STATUS.<br>
|
604 |
* The second byte will be the frame number.<br>
|
605 |
* The third byte will be the result. 0 indicates success,
|
606 |
* and nonzero indicates that an error ocurred in
|
607 |
* transmitting the packet.<br><br>
|
608 |
*
|
609 |
* For a received packet:<br>
|
610 |
* The first byte will be XBEE_RX.<br>
|
611 |
* The second and third bytes will be the 16-bit
|
612 |
* address of the packet's sender.<br>
|
613 |
* The fourth byte is the signal strength.<br>
|
614 |
* The fifth byte is 1 if the packet were sent to
|
615 |
* a specific address, and 2 if it is a broadcast packet.<br><br>
|
616 |
*
|
617 |
* @param dest set to the packet data
|
618 |
* @return the length of the packet, or -1 if no packet
|
619 |
* is available
|
620 |
**/
|
621 |
int xbee_get_packet(unsigned char* dest) |
622 |
{ |
623 |
//start reading a packet with XBEE_FRAME_START
|
624 |
if (currentBufPos == 0) |
625 |
{ |
626 |
do
|
627 |
{ |
628 |
if (buffer_first == XBEE_BUFFER_SIZE)
|
629 |
buffer_first = 0;
|
630 |
// check if buffer is empty
|
631 |
if (buffer_first == buffer_last)
|
632 |
return -1; |
633 |
} |
634 |
while (arrival_buf[buffer_first++] != XBEE_FRAME_START);
|
635 |
if (buffer_first == XBEE_BUFFER_SIZE)
|
636 |
buffer_first = 0;
|
637 |
xbee_buf[0] = XBEE_FRAME_START;
|
638 |
currentBufPos++; |
639 |
} |
640 |
|
641 |
int len = -1; |
642 |
if (currentBufPos >= 3) |
643 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
644 |
|
645 |
while (len == -1 //packet length has not been read yet |
646 |
|| currentBufPos < len + 4)
|
647 |
{ |
648 |
if (currentBufPos == 3) |
649 |
{ |
650 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
651 |
if (len > 120) |
652 |
{ |
653 |
WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n");
|
654 |
currentBufPos = 0;
|
655 |
return -1; |
656 |
} |
657 |
} |
658 |
// check if buffer is empty
|
659 |
if (buffer_first == buffer_last)
|
660 |
return -1; |
661 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
662 |
if (buffer_first == XBEE_BUFFER_SIZE)
|
663 |
buffer_first = 0;
|
664 |
} |
665 |
|
666 |
currentBufPos = 0;
|
667 |
|
668 |
if (!xbee_verify_checksum(xbee_buf, len + 4)) |
669 |
{ |
670 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n");
|
671 |
return -1; |
672 |
} |
673 |
|
674 |
//we will take care of the packet
|
675 |
if (xbee_handle_packet(xbee_buf + 3, len)) |
676 |
return -1; |
677 |
|
678 |
if (dest == NULL) |
679 |
return -1; |
680 |
|
681 |
int i;
|
682 |
for (i = 3; i < len + 3; i++) |
683 |
dest[i - 3] = xbee_buf[i];
|
684 |
return len;
|
685 |
} |
686 |
|
687 |
/**
|
688 |
* Handles modem status packets.
|
689 |
*
|
690 |
* @param status the type of status packet received.
|
691 |
**/
|
692 |
void xbee_handle_status(char status) |
693 |
{ |
694 |
switch (status)
|
695 |
{ |
696 |
case 0: |
697 |
WL_DEBUG_PRINT("XBee hardware reset.\r\n");
|
698 |
break;
|
699 |
case 1: |
700 |
WL_DEBUG_PRINT("Watchdog timer reset.\r\n");
|
701 |
break;
|
702 |
case 2: |
703 |
WL_DEBUG_PRINT("Associated.\r\n");
|
704 |
break;
|
705 |
case 3: |
706 |
WL_DEBUG_PRINT("Disassociated.\r\n");
|
707 |
break;
|
708 |
case 4: |
709 |
WL_DEBUG_PRINT("Synchronization lost.\r\n");
|
710 |
break;
|
711 |
case 5: |
712 |
WL_DEBUG_PRINT("Coordinator realignment.\r\n");
|
713 |
break;
|
714 |
case 6: |
715 |
WL_DEBUG_PRINT("Coordinator started.\r\n");
|
716 |
break;
|
717 |
} |
718 |
} |
719 |
|
720 |
/**
|
721 |
* Handles AT command response packets.
|
722 |
* @param command the two character AT command, e.g. MY or ID
|
723 |
* @param result 0 for success, 1 for an error
|
724 |
* @param extra the hex value of the requested register
|
725 |
* @param extraLen the length in bytes of extra
|
726 |
**/
|
727 |
void xbee_handle_at_command_response(char* command, char result, |
728 |
char* extra, int extraLen) |
729 |
{ |
730 |
if (result == 1) |
731 |
{ |
732 |
WL_DEBUG_PRINT("Error with AT");
|
733 |
WL_DEBUG_PRINT(command); |
734 |
WL_DEBUG_PRINT(" packet.\r\n");
|
735 |
} |
736 |
WL_DEBUG_PRINT("AT");
|
737 |
WL_DEBUG_PRINT(command); |
738 |
WL_DEBUG_PRINT(" command was successful.\r\n");
|
739 |
|
740 |
if (command[0] == 'I' && command[1] == 'D') |
741 |
{ |
742 |
xbee_panID = xbee_pending_panID; |
743 |
WL_DEBUG_PRINT("PAN ID set to ");
|
744 |
WL_DEBUG_PRINT_INT(xbee_panID); |
745 |
WL_DEBUG_PRINT(".\r\n");
|
746 |
return;
|
747 |
} |
748 |
|
749 |
if (command[0] == 'C' && command[1] == 'H') |
750 |
{ |
751 |
xbee_channel = xbee_pending_channel; |
752 |
WL_DEBUG_PRINT("Channel set to ");
|
753 |
WL_DEBUG_PRINT_INT(xbee_channel); |
754 |
WL_DEBUG_PRINT(".\r\n");
|
755 |
return;
|
756 |
} |
757 |
|
758 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) |
759 |
{ |
760 |
xbee_address = 0;
|
761 |
int i;
|
762 |
for (i = 0; i < extraLen; i++) |
763 |
xbee_address = (xbee_address << 8) + extra[i];
|
764 |
|
765 |
WL_DEBUG_PRINT("XBee address is ");
|
766 |
WL_DEBUG_PRINT_INT(xbee_address); |
767 |
WL_DEBUG_PRINT(".\r\n");
|
768 |
|
769 |
if (xbee_address == 0) |
770 |
{ |
771 |
WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n");
|
772 |
exit(0);
|
773 |
} |
774 |
} |
775 |
} |
776 |
|
777 |
/**
|
778 |
* Attempts to handle the packet if it is dealt with
|
779 |
* by the library.
|
780 |
* We will handle the following packet types:
|
781 |
* Modem Status
|
782 |
* AT Command Response
|
783 |
*
|
784 |
* @param packet the packet to handle
|
785 |
* @param len the length of the packet
|
786 |
*
|
787 |
* @return 1 if we have handled the packet, 0 otherwise
|
788 |
*/
|
789 |
int xbee_handle_packet(char* packet, int len) |
790 |
{ |
791 |
char command[3] = {1, 2, 3}; |
792 |
if (len <= 0) //this should not happend |
793 |
{ |
794 |
WL_DEBUG_PRINT("Non-positive packet length.\r\n");
|
795 |
return 0; |
796 |
} |
797 |
|
798 |
switch ((unsigned char)packet[0]) //packet type |
799 |
{ |
800 |
case XBEE_FRAME_STATUS:
|
801 |
xbee_handle_status(packet[1]);
|
802 |
return 1; |
803 |
case XBEE_FRAME_AT_COMMAND_RESPONSE:
|
804 |
command[0] = packet[2]; |
805 |
command[1] = packet[3]; |
806 |
command[2] = 0; |
807 |
xbee_handle_at_command_response(command, |
808 |
packet[4], packet + 5, len - 5); |
809 |
return 1; |
810 |
} |
811 |
return 0; |
812 |
} |
813 |
|
814 |
/**
|
815 |
* Sets the personal area network id.
|
816 |
*
|
817 |
* @param id the new personal area network (PAN) id
|
818 |
**/
|
819 |
void xbee_set_pan_id(int id) |
820 |
{ |
821 |
char s[3]; |
822 |
s[0] = (id >> 8) & 0xFF; |
823 |
s[1] = id & 0xFF; |
824 |
s[2] = 0; |
825 |
xbee_pending_panID = id; |
826 |
xbee_send_modify_at_command("ID", s);
|
827 |
} |
828 |
|
829 |
/**
|
830 |
* Get the PAN ID for the XBee.
|
831 |
*
|
832 |
* @return the personal area network id, or
|
833 |
* XBEE_PAN_DEFAULT if it has not yet been set.
|
834 |
**/
|
835 |
unsigned int xbee_get_pan_id() |
836 |
{ |
837 |
return xbee_panID;
|
838 |
} |
839 |
|
840 |
/**
|
841 |
* Set the channel the XBee is using.
|
842 |
*
|
843 |
* @param channel the channel the XBee will not use,
|
844 |
* between 0x0B and 0x1A
|
845 |
*
|
846 |
* @see xbee_get_channel
|
847 |
**/
|
848 |
void xbee_set_channel(int channel) |
849 |
{ |
850 |
if (channel < 0x0B || channel > 0x1A) |
851 |
{ |
852 |
WL_DEBUG_PRINT("Channel out of range.\r\n");
|
853 |
return;
|
854 |
} |
855 |
char s[3]; |
856 |
s[0] = channel & 0xFF; |
857 |
s[1] = 0; |
858 |
xbee_pending_channel = channel; |
859 |
xbee_send_modify_at_command("CH", s);
|
860 |
} |
861 |
|
862 |
/**
|
863 |
* Returns the channel which the XBee is currently using.
|
864 |
*
|
865 |
* @return the channel the XBee is using
|
866 |
*
|
867 |
* @see xbee_set_channel
|
868 |
**/
|
869 |
int xbee_get_channel(void) |
870 |
{ |
871 |
return xbee_channel;
|
872 |
} |
873 |
|
874 |
/**
|
875 |
* Get the 16-bit address of the XBee.
|
876 |
* This is used to specify who to send messages to
|
877 |
* and who messages are from.
|
878 |
*
|
879 |
* @return the 16-bit address of the XBee.
|
880 |
**/
|
881 |
unsigned int xbee_get_address() |
882 |
{ |
883 |
return xbee_address;
|
884 |
} |
885 |
|
886 |
#ifndef ROBOT
|
887 |
void xbee_set_com_port(char* port) |
888 |
{ |
889 |
xbee_com_port = port; |
890 |
} |
891 |
#endif
|
892 |
|