Revision 668
Updated port of wireless library to bay boards.
xbee.c | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (c) 2007 Colony Project |
3 |
*
|
|
3 |
* |
|
4 | 4 |
* Permission is hereby granted, free of charge, to any person |
5 | 5 |
* obtaining a copy of this software and associated documentation |
6 | 6 |
* files (the "Software"), to deal in the Software without |
... | ... | |
9 | 9 |
* copies of the Software, and to permit persons to whom the |
10 | 10 |
* Software is furnished to do so, subject to the following |
11 | 11 |
* conditions: |
12 |
*
|
|
12 |
* |
|
13 | 13 |
* The above copyright notice and this permission notice shall be |
14 | 14 |
* included in all copies or substantial portions of the Software. |
15 |
*
|
|
15 |
* |
|
16 | 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
18 | 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
... | ... | |
55 | 55 |
#include <string.h> |
56 | 56 |
|
57 | 57 |
#define XBEE_FRAME_START 0x7E |
58 |
#define XBEE_GET_PACKET_TIMEOUT 1000 |
|
58 | 59 |
|
59 | 60 |
/*Frame Types*/ |
60 | 61 |
#define XBEE_FRAME_STATUS 0x8A |
... | ... | |
69 | 70 |
/*Internal Function Prototypes*/ |
70 | 71 |
|
71 | 72 |
/*I/O Functions*/ |
72 |
void xbee_send(char* buf, int size);
|
|
73 |
void xbee_send_string(char* c);
|
|
73 |
static int xbee_send(char* buf, int size);
|
|
74 |
static int xbee_send_string(char* c);
|
|
74 | 75 |
|
75 | 76 |
#ifndef ROBOT |
76 |
void xbee_read(char* buf, int size);
|
|
77 |
static int xbee_read(char* buf, int size);
|
|
77 | 78 |
#endif |
78 | 79 |
|
79 | 80 |
/*Command Mode Functions |
80 | 81 |
* Called during initialization. |
81 | 82 |
*/ |
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);
|
|
83 |
static int xbee_enter_command_mode(void);
|
|
84 |
static int xbee_exit_command_mode(void);
|
|
85 |
static int xbee_enter_api_mode(void);
|
|
86 |
static int xbee_exit_api_mode(void);
|
|
87 |
static int xbee_wait_for_string(char* s, int len);
|
|
88 |
static int xbee_wait_for_ok(void);
|
|
88 | 89 |
|
89 | 90 |
/*API Mode Functions*/ |
90 | 91 |
|
91 |
int xbee_handle_packet(char* packet, int len); |
|
92 |
void xbee_handle_at_command_response(char* command, char result, |
|
93 |
char* extra, int extraLen); |
|
94 |
void xbee_handle_status(char status); |
|
95 |
int xbee_verify_checksum(char* packet, int len); |
|
96 |
char xbee_compute_checksum(char* packet, int len); |
|
97 |
void xbee_send_frame(char* buf, int len); |
|
98 |
void xbee_send_read_at_command(char* command); |
|
99 |
void xbee_send_modify_at_command(char* command, char* value); |
|
92 |
static int xbee_handle_packet(char* packet, int len); |
|
93 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen); |
|
94 |
static void xbee_handle_status(char status); |
|
95 |
static int xbee_verify_checksum(char* packet, int len); |
|
96 |
static char xbee_compute_checksum(char* packet, int len); |
|
97 |
static int xbee_send_frame(char* buf, int len); |
|
98 |
static int xbee_send_read_at_command(char* command); |
|
99 |
static int xbee_send_modify_at_command(char* command, char* value); |
|
100 | 100 |
|
101 | 101 |
/*Global Variables*/ |
102 | 102 |
|
103 | 103 |
#ifndef ROBOT |
104 |
char* xbee_com_port = XBEE_PORT_DEFAULT; |
|
105 |
int xbee_stream; |
|
106 |
pthread_t* xbee_listen_thread; |
|
104 |
static char* xbee_com_port = XBEE_PORT_DEFAULT;
|
|
105 |
static int xbee_stream;
|
|
106 |
static pthread_t* xbee_listen_thread;
|
|
107 | 107 |
#endif |
108 | 108 |
|
109 | 109 |
// TODO: is this a good size? |
... | ... | |
117 | 117 |
|
118 | 118 |
|
119 | 119 |
//used to store packets as they are read |
120 |
char xbee_buf[128]; |
|
121 |
int currentBufPos = 0; |
|
120 |
static char xbee_buf[128];
|
|
121 |
static int currentBufPos = 0;
|
|
122 | 122 |
|
123 | 123 |
//XBee status |
124 |
unsigned int xbee_panID = XBEE_PAN_DEFAULT; |
|
125 |
unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT; |
|
126 |
int xbee_channel = XBEE_CHANNEL_DEFAULT; |
|
127 |
int xbee_pending_channel = XBEE_CHANNEL_DEFAULT; |
|
128 |
unsigned int xbee_address = 0; |
|
124 |
static unsigned int xbee_panID = XBEE_PAN_DEFAULT;
|
|
125 |
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
|
|
126 |
static int xbee_channel = XBEE_CHANNEL_DEFAULT;
|
|
127 |
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
|
|
128 |
static volatile unsigned int xbee_address = 0;
|
|
129 | 129 |
|
130 |
void printHex(char * s, int len) { |
|
131 |
int i; |
|
132 |
for (i = 0; i < len; i++) { |
|
133 |
printf("0x%x ", (int)(s[i])); |
|
134 |
} |
|
135 |
printf("\n"); |
|
136 |
} |
|
137 |
|
|
138 |
|
|
130 | 139 |
/*Function Implementations*/ |
131 | 140 |
|
132 | 141 |
#ifdef ROBOT |
... | ... | |
135 | 144 |
* Interrupt for the robot. Adds bytes received from the xbee |
136 | 145 |
* to the buffer. |
137 | 146 |
**/ |
138 |
#ifndef FIREFLY
|
|
139 |
ISR(USART1_RX_vect)
|
|
147 |
#ifdef FIREFLY |
|
148 |
SIGNAL(SIG_USART0_RECV)
|
|
140 | 149 |
{ |
141 |
char c = UDR1;
|
|
150 |
char c = UDR0;
|
|
142 | 151 |
arrival_buf[buffer_last] = c; |
143 | 152 |
int t = buffer_last + 1; |
144 | 153 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
150 | 159 |
buffer_last = t; |
151 | 160 |
} |
152 | 161 |
#else |
153 |
SIGNAL(SIG_USART0_RECV) |
|
162 |
#ifdef BAYBOARD |
|
163 |
// TODO: interrupt handler for bayboard, should be similar to above and below |
|
164 |
#else |
|
165 |
ISR(USART1_RX_vect) |
|
154 | 166 |
{ |
155 |
char c = UDR0;
|
|
167 |
char c = UDR1;
|
|
156 | 168 |
arrival_buf[buffer_last] = c; |
157 | 169 |
int t = buffer_last + 1; |
158 | 170 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
164 | 176 |
buffer_last = t; |
165 | 177 |
} |
166 | 178 |
#endif |
179 |
#endif |
|
167 | 180 |
|
168 | 181 |
#else |
169 | 182 |
|
183 |
// Computer code |
|
184 |
|
|
170 | 185 |
/** |
171 | 186 |
* Thread that listens to the xbee. |
172 | 187 |
**/ |
173 |
void* listen_to_xbee(void* x) |
|
188 |
static void* listen_to_xbee(void* x)
|
|
174 | 189 |
{ |
175 | 190 |
char c; |
176 | 191 |
while (1) |
177 | 192 |
{ |
178 |
xbee_read(&c, 1); |
|
193 |
if (xbee_read(&c, 1) != 0) { |
|
194 |
fprintf(stderr, "xbee_read failed.\n"); |
|
195 |
return NULL; |
|
196 |
} |
|
197 |
|
|
198 |
//DEBUGGING PRINT |
|
199 |
//printf("interrupt: %c (%d)\n", c, (int)c); |
|
179 | 200 |
arrival_buf[buffer_last] = c; |
180 | 201 |
int t = buffer_last + 1; |
181 | 202 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
185 | 206 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
186 | 207 |
} |
187 | 208 |
buffer_last = t; |
209 |
|
|
210 |
usleep(1000); |
|
188 | 211 |
} |
189 |
return 0; |
|
212 |
|
|
213 |
return NULL; |
|
190 | 214 |
} |
191 | 215 |
|
192 | 216 |
#endif |
... | ... | |
194 | 218 |
/** |
195 | 219 |
* Initializes the XBee library so that other functions may be used. |
196 | 220 |
**/ |
197 |
int xbee_lib_init(void)
|
|
221 |
int xbee_lib_init() |
|
198 | 222 |
{ |
199 | 223 |
arrival_buf[0] = 'A'; |
200 | 224 |
arrival_buf[1] = 'A'; |
201 | 225 |
arrival_buf[2] = 'A'; |
202 |
#ifdef ROBOT
|
|
226 |
#ifdef ROBOT |
|
203 | 227 |
|
204 | 228 |
//enable the receiving interrupt |
205 |
#ifdef FIREFLY
|
|
229 |
#ifdef FIREFLY |
|
206 | 230 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
207 |
#else |
|
231 |
#else |
|
232 |
#ifdef BAYBOARD |
|
233 |
//TODO: enable the receiving interrupt on the bayboard |
|
234 |
#else |
|
208 | 235 |
UCSR1B |= _BV(RXCIE); |
209 |
#endif |
|
236 |
#endif |
|
237 |
#endif |
|
210 | 238 |
sei(); |
211 |
#else
|
|
239 |
#else |
|
212 | 240 |
printf("Connecting to port %s.\n", xbee_com_port); |
213 | 241 |
xbee_stream = open(xbee_com_port, O_RDWR); |
214 | 242 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
215 | 243 |
{ |
216 | 244 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port); |
217 | 245 |
return -1; |
246 |
} else { |
|
247 |
printf("Successfully opened connection to XBee on port %s\r\n", xbee_com_port); |
|
218 | 248 |
} |
219 |
|
|
249 |
|
|
220 | 250 |
// set baud rate, etc. correctly |
221 | 251 |
struct termios options; |
222 | 252 |
|
... | ... | |
238 | 268 |
{ |
239 | 269 |
fprintf(stderr, "Error setting attributes.\n"); |
240 | 270 |
return -1; |
271 |
} else { |
|
272 |
//printf("Successfully set termios attributes.\n"); |
|
241 | 273 |
} |
242 | 274 |
|
243 | 275 |
//lockf(xbee_stream, F_LOCK, 0); |
244 |
|
|
245 |
xbee_listen_thread = |
|
246 |
(pthread_t*)malloc(sizeof(pthread_t)); |
|
276 |
|
|
277 |
xbee_listen_thread = (pthread_t*)malloc(sizeof(pthread_t)); |
|
247 | 278 |
if (xbee_listen_thread == NULL) |
248 | 279 |
{ |
249 | 280 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__); |
250 | 281 |
return -1; |
251 | 282 |
} |
252 |
|
|
253 |
int ret = pthread_create(xbee_listen_thread, NULL, |
|
254 |
listen_to_xbee, NULL); |
|
283 |
|
|
284 |
int ret = pthread_create(xbee_listen_thread, NULL, listen_to_xbee, NULL); |
|
255 | 285 |
if (ret) |
256 | 286 |
{ |
257 | 287 |
fprintf(stderr, "Failed to create listener thread.\r\n"); |
258 | 288 |
return -1; |
289 |
} else { |
|
290 |
//printf("Successfully created listener thread.\n"); |
|
259 | 291 |
} |
260 |
|
|
261 |
#endif |
|
262 |
xbee_enter_command_mode(); |
|
263 |
xbee_enter_api_mode(); |
|
264 |
xbee_exit_command_mode(); |
|
265 |
xbee_send_read_at_command("MY"); |
|
266 |
|
|
292 |
#endif |
|
293 |
|
|
294 |
//DEBUGGING PRINT |
|
295 |
//printf("about to call xbee_enter_command_mode\n"); |
|
296 |
|
|
297 |
if (xbee_enter_command_mode() != 0) { |
|
298 |
#ifndef ROBOT |
|
299 |
printf("Error returned from xbee_enter_command_mode\n"); |
|
300 |
#endif |
|
301 |
return -1; |
|
302 |
} |
|
303 |
|
|
304 |
//DEBUGGING PRINT |
|
305 |
//printf("about to call xbee_enter_api_mode\n"); |
|
306 |
|
|
307 |
if (xbee_enter_api_mode() != 0) { |
|
308 |
#ifndef ROBOT |
|
309 |
printf("Error returned from xbee_enter_api_mode\n"); |
|
310 |
#endif |
|
311 |
return -1; |
|
312 |
} |
|
313 |
|
|
314 |
//DEBUGGING PRINT |
|
315 |
//printf("about to call xbee_exit_command_mode\n"); |
|
316 |
|
|
317 |
if (xbee_exit_command_mode() != 0) { |
|
318 |
#ifndef ROBOT |
|
319 |
printf("Error returned from xbee_exit_command_mode\n"); |
|
320 |
#endif |
|
321 |
return -1; |
|
322 |
} |
|
323 |
|
|
324 |
//DEBUGGING PRINT |
|
325 |
//printf("about to call xbee_send_read_at_command\n"); |
|
326 |
|
|
327 |
if (xbee_send_read_at_command("MY")) { |
|
328 |
#ifndef ROBOT |
|
329 |
printf("Error returned from xbee_send_read_at_command\n"); |
|
330 |
#endif |
|
331 |
return -1; |
|
332 |
} |
|
333 |
|
|
334 |
#ifndef ROBOT |
|
335 |
//printf("About to enter while loop to get xbee_address.\n"); |
|
336 |
int i; |
|
337 |
for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) { |
|
338 |
ret = xbee_get_packet(NULL); |
|
339 |
|
|
340 |
usleep(1000); |
|
341 |
|
|
342 |
if (ret == -1) { |
|
343 |
//printf("xbee_get_packet(NULL) failed.\n"); |
|
344 |
return -1; |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
// printf("After exiting while loop to get xbee_address.\n"); |
|
349 |
#else |
|
267 | 350 |
//wait to return until the address is set |
268 |
while (xbee_address == 0) xbee_get_packet(NULL); |
|
351 |
while (xbee_address == 0) { |
|
352 |
xbee_get_packet(NULL); |
|
353 |
} |
|
354 |
#endif |
|
269 | 355 |
|
356 |
#ifndef ROBOT |
|
357 |
if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out. |
|
270 | 358 |
|
359 |
printf("xbee_get_packet timed out.\n"); |
|
360 |
return -1; |
|
361 |
} else { |
|
362 |
return 0; |
|
363 |
} |
|
364 |
#else |
|
271 | 365 |
return 0; |
366 |
#endif |
|
272 | 367 |
} |
273 | 368 |
|
274 | 369 |
/** |
... | ... | |
287 | 382 |
|
288 | 383 |
/** |
289 | 384 |
* Send a buffer buf of size bytes to the XBee. |
290 |
*
|
|
385 |
* |
|
291 | 386 |
* @param buf the buffer of data to send |
292 | 387 |
* @param size the number of bytes to send |
293 | 388 |
**/ |
294 |
void xbee_send(char* buf, int size)
|
|
389 |
static int xbee_send(char* buf, int size)
|
|
295 | 390 |
{ |
296 |
#ifdef ROBOT
|
|
391 |
#ifdef ROBOT |
|
297 | 392 |
int i; |
298 |
for (i = 0; i < size; i++) |
|
393 |
for (i = 0; i < size; i++) {
|
|
299 | 394 |
xbee_putc(buf[i]); |
300 |
#else |
|
395 |
} |
|
396 |
|
|
397 |
return 0; |
|
398 |
|
|
399 |
#else |
|
400 |
//DEBUGGING PRINT |
|
401 |
//printf("in xbee_send "); |
|
402 |
//printHex(buf, size); |
|
403 |
|
|
301 | 404 |
int ret = write(xbee_stream, buf, size); |
302 | 405 |
//success |
303 | 406 |
if (ret == size) |
304 |
return; |
|
407 |
return 0;
|
|
305 | 408 |
if (ret == -1) |
306 | 409 |
{ |
307 | 410 |
//interrupted by system signal, probably timer interrupt. |
308 | 411 |
//just try again |
309 | 412 |
if (errno == 4) |
310 | 413 |
{ |
311 |
xbee_send(buf, size); |
|
312 |
return; |
|
414 |
return xbee_send(buf, size); |
|
313 | 415 |
} |
314 | 416 |
printf("Failed to write to xbee, error %i.\r\n", errno); |
315 |
return; |
|
417 |
return -1;
|
|
316 | 418 |
} |
317 | 419 |
|
318 | 420 |
//write was interrupted after writing ret bytes |
319 |
xbee_send(buf + ret, size - ret); |
|
320 |
#endif
|
|
421 |
return xbee_send(buf + ret, size - ret);
|
|
422 |
#endif |
|
321 | 423 |
} |
322 | 424 |
|
323 | 425 |
/** |
... | ... | |
325 | 427 |
* |
326 | 428 |
* @param c the string to send to the XBEE |
327 | 429 |
**/ |
328 |
void xbee_send_string(char* c)
|
|
430 |
static int xbee_send_string(char* c)
|
|
329 | 431 |
{ |
330 |
xbee_send(c, strlen(c)); |
|
432 |
return xbee_send(c, strlen(c));
|
|
331 | 433 |
} |
332 | 434 |
|
333 | 435 |
#ifndef ROBOT |
334 |
void xbee_read(char* buf, int size)
|
|
436 |
static int xbee_read(char* buf, int size)
|
|
335 | 437 |
{ |
336 |
if (read(xbee_stream, buf, size) == -1) |
|
438 |
if (read(xbee_stream, buf, size) == -1) {
|
|
337 | 439 |
printf("Failed to read from xbee.\r\n"); |
440 |
return -1; |
|
441 |
} |
|
442 |
|
|
443 |
return 0; |
|
338 | 444 |
} |
339 | 445 |
#endif |
340 | 446 |
|
341 | 447 |
/** |
342 | 448 |
* Enter into command mode. |
343 | 449 |
**/ |
344 |
void xbee_enter_command_mode()
|
|
450 |
static int xbee_enter_command_mode()
|
|
345 | 451 |
{ |
346 |
xbee_send_string("+++"); |
|
347 |
xbee_wait_for_ok(); |
|
452 |
if (xbee_send_string("+++") != 0) { |
|
453 |
return -1; |
|
454 |
} |
|
455 |
|
|
456 |
#ifndef ROBOT |
|
457 |
// printf("In xbee_enter_command_mode about to call xbee_wait_for_ok()\n"); |
|
458 |
#endif |
|
459 |
|
|
460 |
if (xbee_wait_for_ok() != 0) { |
|
461 |
#ifndef ROBOT |
|
462 |
printf("xbee_wait_for_ok failed.\n"); |
|
463 |
#endif |
|
464 |
return -1; |
|
465 |
} else { |
|
466 |
return 0; |
|
467 |
} |
|
348 | 468 |
} |
349 | 469 |
|
350 | 470 |
/** |
351 | 471 |
* Exit from command mode. |
352 | 472 |
**/ |
353 |
void xbee_exit_command_mode()
|
|
473 |
static int xbee_exit_command_mode()
|
|
354 | 474 |
{ |
355 |
xbee_send_string("ATCN\r"); |
|
475 |
if (xbee_send_string("ATCN\r") != 0) { |
|
476 |
return -1; |
|
477 |
} |
|
478 |
|
|
356 | 479 |
xbee_wait_for_ok(); |
480 |
|
|
481 |
return 0; |
|
357 | 482 |
} |
358 | 483 |
|
359 | 484 |
/** |
360 | 485 |
* Enter API mode. |
361 | 486 |
**/ |
362 |
void xbee_enter_api_mode()
|
|
487 |
static int xbee_enter_api_mode()
|
|
363 | 488 |
{ |
364 |
xbee_send_string("ATAP 1\r"); |
|
489 |
if (xbee_send_string("ATAP 1\r") != 0) { |
|
490 |
return -1; |
|
491 |
} |
|
365 | 492 |
xbee_wait_for_ok(); |
493 |
|
|
494 |
return 0; |
|
366 | 495 |
} |
367 | 496 |
|
368 | 497 |
/** |
369 | 498 |
* Exit API mode. (warning - does not check for response) |
370 | 499 |
**/ |
371 |
void xbee_exit_api_mode()
|
|
500 |
static int xbee_exit_api_mode()
|
|
372 | 501 |
{ |
373 |
xbee_send_string("ATAP 0\r"); |
|
502 |
return xbee_send_string("ATAP 0\r");
|
|
374 | 503 |
} |
375 | 504 |
|
376 | 505 |
/** |
377 | 506 |
* Wait until the string "OK\r" is received from the XBee. |
378 | 507 |
**/ |
379 |
void xbee_wait_for_ok()
|
|
508 |
static int xbee_wait_for_ok()
|
|
380 | 509 |
{ |
381 |
xbee_wait_for_string("OK\r", 3); |
|
510 |
//DEBUGGING PRINT |
|
511 |
//printf("\tin xbee_wait_for_ok\n"); |
|
512 |
return xbee_wait_for_string("OK\r", 3); |
|
382 | 513 |
} |
383 | 514 |
|
384 | 515 |
/** |
... | ... | |
388 | 519 |
* @param s the string to receive |
389 | 520 |
* @param len the length of the string |
390 | 521 |
**/ |
391 |
void xbee_wait_for_string(char* s, int len)
|
|
522 |
static int xbee_wait_for_string(char* s, int len)
|
|
392 | 523 |
{ |
524 |
//DEBUGGING PRINT |
|
525 |
//printf("\t in xbee_wait_for_string\n"); |
|
526 |
|
|
527 |
#ifndef ROBOT |
|
528 |
//printf("In xbee_wait_for_string.\n"); |
|
529 |
#endif |
|
530 |
|
|
393 | 531 |
char* curr = s; |
394 |
while (curr - s < len) |
|
395 |
{ |
|
532 |
while (curr - s < len) { |
|
396 | 533 |
// 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; |
|
534 |
if (buffer_last != buffer_first) { |
|
535 |
char c = arrival_buf[buffer_first++]; |
|
536 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
537 |
buffer_first = 0; |
|
538 |
} |
|
539 |
|
|
540 |
//DEBUGGING PRINT |
|
541 |
//printf("\t\t c is %c (%d)\n", c, (int)c); |
|
542 |
|
|
543 |
if (c == *curr) { |
|
544 |
curr++; |
|
545 |
} else { |
|
546 |
#ifndef ROBOT |
|
547 |
//return -1; // Computer is less forgiving. |
|
548 |
curr = s; |
|
549 |
#else |
|
550 |
curr = s; |
|
551 |
#endif |
|
552 |
} |
|
553 |
} // else buffer is empty. |
|
554 |
|
|
555 |
#ifndef ROBOT |
|
556 |
usleep(100); |
|
557 |
#endif |
|
406 | 558 |
} |
559 |
|
|
560 |
return 0; |
|
407 | 561 |
} |
408 | 562 |
|
409 | 563 |
/** |
... | ... | |
455 | 609 |
* @param len the size in bytes of the packet data |
456 | 610 |
* |
457 | 611 |
**/ |
458 |
void xbee_send_frame(char* buf, int len)
|
|
612 |
static int xbee_send_frame(char* buf, int len)
|
|
459 | 613 |
{ |
614 |
//printf("in %s and len is %d\n", __FUNCTION__, len); |
|
615 |
|
|
460 | 616 |
char prefix[3]; |
461 | 617 |
prefix[0] = XBEE_FRAME_START; |
462 | 618 |
prefix[1] = (len & 0xFF00) >> 8; |
463 | 619 |
prefix[2] = len & 0xFF; |
464 | 620 |
char checksum = xbee_compute_checksum(buf, len); |
465 |
xbee_send(prefix, 3); |
|
466 |
xbee_send(buf, len); |
|
467 |
xbee_send(&checksum, 1); |
|
621 |
|
|
622 |
if (xbee_send(prefix, 3) != 0) { |
|
623 |
return -1; |
|
624 |
} |
|
625 |
|
|
626 |
if (xbee_send(buf, len) != 0) { |
|
627 |
return -1; |
|
628 |
} |
|
629 |
|
|
630 |
if (xbee_send(&checksum, 1) != 0) { |
|
631 |
return -1; |
|
632 |
} |
|
633 |
|
|
634 |
return 0; |
|
468 | 635 |
} |
469 | 636 |
|
470 | 637 |
/** |
... | ... | |
474 | 641 |
* use ID to read the PAN ID and MY to return the XBee ID. |
475 | 642 |
* See the XBee reference guide for a complete listing. |
476 | 643 |
**/ |
477 |
void xbee_send_read_at_command(char* command)
|
|
644 |
static int xbee_send_read_at_command(char* command)
|
|
478 | 645 |
{ |
479 |
xbee_send_modify_at_command(command, NULL); |
|
646 |
return xbee_send_modify_at_command(command, NULL);
|
|
480 | 647 |
} |
481 | 648 |
|
482 | 649 |
/** |
... | ... | |
486 | 653 |
* @param value the value to pass as a parameter |
487 | 654 |
* (or NULL if there is no parameter) |
488 | 655 |
**/ |
489 |
void xbee_send_modify_at_command(char* command, char* value)
|
|
656 |
static int xbee_send_modify_at_command(char* command, char* value)
|
|
490 | 657 |
{ |
658 |
//printf("in %s with command %s and value %s\n", __FUNCTION__, command, value); |
|
659 |
|
|
491 | 660 |
char buf[16]; |
492 | 661 |
int i; |
493 |
|
|
662 |
|
|
494 | 663 |
buf[0] = XBEE_FRAME_AT_COMMAND; |
495 | 664 |
buf[1] = 1; |
496 | 665 |
buf[2] = command[0]; |
... | ... | |
502 | 671 |
if (valueLen > 8) |
503 | 672 |
{ |
504 | 673 |
WL_DEBUG_PRINT("AT Command too large.\r\n"); |
505 |
return; |
|
674 |
return -1;
|
|
506 | 675 |
} |
507 |
for (i = 0; i < valueLen; i++) |
|
676 |
|
|
677 |
for (i = 0; i < valueLen; i++) { |
|
508 | 678 |
buf[4 + i] = value[i]; |
679 |
} |
|
509 | 680 |
} |
510 |
xbee_send_frame(buf, 4 + valueLen); |
|
681 |
|
|
682 |
return xbee_send_frame(buf, 4 + valueLen); |
|
511 | 683 |
} |
512 | 684 |
|
513 | 685 |
/** |
514 | 686 |
* Send the specified packet. |
515 |
*
|
|
687 |
* |
|
516 | 688 |
* @param packet the packet data to send |
517 | 689 |
* @param len the number of bytes in the packet |
518 |
*
|
|
690 |
* |
|
519 | 691 |
* @param dest the ID of the XBee to send the packet to, |
520 | 692 |
* or XBEE_BROADCAST to send the message to all robots |
521 | 693 |
* in the PAN. |
522 |
*
|
|
694 |
* |
|
523 | 695 |
* @param options a combination of the flags |
524 |
* XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and
|
|
696 |
* XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and |
|
525 | 697 |
* XBEE_OPTIONS_BROADCAST_ALL_PANS |
526 | 698 |
* |
527 | 699 |
* @param frame the frame number to associate this packet |
... | ... | |
529 | 701 |
* the XBee alerts us as to whether or not our message |
530 | 702 |
* was received. |
531 | 703 |
**/ |
532 |
void xbee_send_packet(char* packet, int len, int dest, |
|
533 |
char options, char frame) |
|
704 |
int xbee_send_packet(char* packet, int len, int dest, char options, char frame) |
|
534 | 705 |
{ |
535 | 706 |
char buf[5]; |
536 | 707 |
char prefix[3]; |
... | ... | |
540 | 711 |
if (len > 100) |
541 | 712 |
{ |
542 | 713 |
WL_DEBUG_PRINT("Packet is too large.\r\n"); |
543 |
return; |
|
714 |
return -1;
|
|
544 | 715 |
} |
545 | 716 |
|
546 | 717 |
//data for sending request |
... | ... | |
560 | 731 |
for (i = 0; i < len; i++) |
561 | 732 |
checksum += (unsigned char)packet[i]; |
562 | 733 |
checksum = 0xFF - checksum; |
563 |
xbee_send(prefix, 3); |
|
564 |
xbee_send(buf, 5); |
|
565 |
xbee_send(packet, len); |
|
566 |
xbee_send((char*)&checksum, 1); |
|
734 |
|
|
735 |
if (xbee_send(prefix, 3) != 0) { |
|
736 |
return -1; |
|
737 |
} |
|
738 |
|
|
739 |
if (xbee_send(buf, 5) != 0) { |
|
740 |
return -1; |
|
741 |
} |
|
742 |
|
|
743 |
if (xbee_send(packet, len) != 0) { |
|
744 |
return -1; |
|
745 |
} |
|
746 |
|
|
747 |
if (xbee_send((char*)&checksum, 1) != 0) { |
|
748 |
return -1; |
|
749 |
} |
|
750 |
|
|
751 |
return 0; |
|
567 | 752 |
} |
568 | 753 |
|
569 | 754 |
/** |
... | ... | |
578 | 763 |
* |
579 | 764 |
* The first byte of the packet will be either |
580 | 765 |
* XBEE_TX_STATUS or XBEE_RX to indicated |
581 |
* a response to a sent message or a received message,
|
|
766 |
* a response to a sent message or a received message, |
|
582 | 767 |
* respectively.<br><br> |
583 | 768 |
* |
584 | 769 |
* For a status response packet:<br> |
585 | 770 |
* The first byte will be XBEE_TX_STATUS.<br> |
586 | 771 |
* The second byte will be the frame number.<br> |
587 | 772 |
* The third byte will be the result. 0 indicates success, |
588 |
* and nonzero indicates that an error ocurred in
|
|
773 |
* and nonzero indicates that an error ocurred in |
|
589 | 774 |
* transmitting the packet.<br><br> |
590 | 775 |
* |
591 | 776 |
* For a received packet:<br> |
... | ... | |
595 | 780 |
* The fourth byte is the signal strength.<br> |
596 | 781 |
* The fifth byte is 1 if the packet were sent to |
597 | 782 |
* a specific address, and 2 if it is a broadcast packet.<br><br> |
598 |
*
|
|
783 |
* |
|
599 | 784 |
* @param dest set to the packet data |
600 | 785 |
* @return the length of the packet, or -1 if no packet |
601 | 786 |
* is available |
... | ... | |
610 | 795 |
if (buffer_first == XBEE_BUFFER_SIZE) |
611 | 796 |
buffer_first = 0; |
612 | 797 |
// check if buffer is empty |
613 |
if (buffer_first == buffer_last) |
|
614 |
return -1; |
|
798 |
if (buffer_first == buffer_last) { |
|
799 |
return 0; |
|
800 |
} |
|
801 |
} while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
802 |
|
|
803 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
804 |
buffer_first = 0; |
|
615 | 805 |
} |
616 |
while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
617 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
618 |
buffer_first = 0; |
|
619 | 806 |
xbee_buf[0] = XBEE_FRAME_START; |
620 | 807 |
currentBufPos++; |
621 | 808 |
} |
622 | 809 |
|
623 | 810 |
int len = -1; |
624 |
if (currentBufPos >= 3) |
|
811 |
if (currentBufPos >= 3) {
|
|
625 | 812 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
626 |
|
|
813 |
} |
|
814 |
|
|
627 | 815 |
while (len == -1 //packet length has not been read yet |
628 |
|| currentBufPos < len + 4)
|
|
816 |
|| currentBufPos < len + 4) |
|
629 | 817 |
{ |
630 | 818 |
if (currentBufPos == 3) |
631 | 819 |
{ |
... | ... | |
637 | 825 |
return -1; |
638 | 826 |
} |
639 | 827 |
} |
828 |
|
|
640 | 829 |
// check if buffer is empty |
641 |
if (buffer_first == buffer_last) |
|
642 |
return -1; |
|
830 |
if (buffer_first == buffer_last) { |
|
831 |
return 0; |
|
832 |
} |
|
643 | 833 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
644 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
834 |
if (buffer_first == XBEE_BUFFER_SIZE) {
|
|
645 | 835 |
buffer_first = 0; |
836 |
} |
|
646 | 837 |
} |
647 |
|
|
838 |
|
|
648 | 839 |
currentBufPos = 0; |
649 |
|
|
840 |
|
|
650 | 841 |
if (!xbee_verify_checksum(xbee_buf, len + 4)) |
651 | 842 |
{ |
652 | 843 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n"); |
... | ... | |
654 | 845 |
} |
655 | 846 |
|
656 | 847 |
//we will take care of the packet |
657 |
if (xbee_handle_packet(xbee_buf + 3, len)) |
|
658 |
return -1; |
|
659 |
|
|
660 |
if (dest == NULL) |
|
661 |
return -1; |
|
662 |
|
|
848 |
if (xbee_handle_packet(xbee_buf + 3, len) != 0) { |
|
849 |
return 0; |
|
850 |
} |
|
851 |
|
|
852 |
if (dest == NULL) { |
|
853 |
return 0; |
|
854 |
} |
|
855 |
|
|
663 | 856 |
int i; |
664 |
for (i = 3; i < len + 3; i++) |
|
857 |
for (i = 3; i < len + 3; i++) {
|
|
665 | 858 |
dest[i - 3] = xbee_buf[i]; |
859 |
} |
|
860 |
|
|
666 | 861 |
return len; |
667 | 862 |
} |
668 | 863 |
|
... | ... | |
706 | 901 |
* @param extra the hex value of the requested register |
707 | 902 |
* @param extraLen the length in bytes of extra |
708 | 903 |
**/ |
709 |
void xbee_handle_at_command_response(char* command, char result, |
|
710 |
char* extra, int extraLen) |
|
904 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen) |
|
711 | 905 |
{ |
712 | 906 |
if (result == 1) |
713 | 907 |
{ |
... | ... | |
718 | 912 |
WL_DEBUG_PRINT("AT"); |
719 | 913 |
WL_DEBUG_PRINT(command); |
720 | 914 |
WL_DEBUG_PRINT(" command was successful.\r\n"); |
721 |
|
|
915 |
|
|
722 | 916 |
if (command[0] == 'I' && command[1] == 'D') |
723 | 917 |
{ |
724 | 918 |
xbee_panID = xbee_pending_panID; |
... | ... | |
736 | 930 |
WL_DEBUG_PRINT(".\r\n"); |
737 | 931 |
return; |
738 | 932 |
} |
739 |
|
|
933 |
|
|
740 | 934 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) |
741 | 935 |
{ |
936 |
// printf("reading xbee_address\n"); |
|
937 |
|
|
742 | 938 |
xbee_address = 0; |
743 | 939 |
int i; |
744 |
for (i = 0; i < extraLen; i++) |
|
940 |
for (i = 0; i < extraLen; i++) {
|
|
745 | 941 |
xbee_address = (xbee_address << 8) + extra[i]; |
942 |
} |
|
943 |
// printf("xbee address is: %d\n", xbee_address); |
|
746 | 944 |
|
747 | 945 |
WL_DEBUG_PRINT("XBee address is "); |
748 | 946 |
WL_DEBUG_PRINT_INT(xbee_address); |
... | ... | |
750 | 948 |
|
751 | 949 |
if (xbee_address == 0) |
752 | 950 |
{ |
951 |
printf("XBee 16-bit address must be set using ATMY.\r\n"); |
|
753 | 952 |
WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n"); |
754 | 953 |
exit(0); |
755 | 954 |
} |
... | ... | |
765 | 964 |
* |
766 | 965 |
* @param packet the packet to handle |
767 | 966 |
* @param len the length of the packet |
768 |
*
|
|
967 |
* |
|
769 | 968 |
* @return 1 if we have handled the packet, 0 otherwise |
770 | 969 |
*/ |
771 |
int xbee_handle_packet(char* packet, int len) |
|
970 |
static int xbee_handle_packet(char* packet, int len)
|
|
772 | 971 |
{ |
972 |
//DEBUGGING PRINT |
|
973 |
//printf("xbee_handle_packet: "); |
|
974 |
//printHex(packet, len); |
|
975 |
|
|
773 | 976 |
char command[3] = {1, 2, 3}; |
774 | 977 |
if (len <= 0) //this should not happend |
775 | 978 |
{ |
776 | 979 |
WL_DEBUG_PRINT("Non-positive packet length.\r\n"); |
777 | 980 |
return 0; |
778 | 981 |
} |
779 |
|
|
982 |
|
|
780 | 983 |
switch ((unsigned char)packet[0]) //packet type |
781 | 984 |
{ |
782 | 985 |
case XBEE_FRAME_STATUS: |
783 | 986 |
xbee_handle_status(packet[1]); |
784 | 987 |
return 1; |
785 | 988 |
case XBEE_FRAME_AT_COMMAND_RESPONSE: |
989 |
//printf("in XBEE_FRAME_AT_COMMAND_RESPONSE case\n"); |
|
786 | 990 |
command[0] = packet[2]; |
787 | 991 |
command[1] = packet[3]; |
788 | 992 |
command[2] = 0; |
789 |
xbee_handle_at_command_response(command, |
|
790 |
packet[4], packet + 5, len - 5); |
|
993 |
xbee_handle_at_command_response(command, packet[4], packet + 5, len - 5); |
|
791 | 994 |
return 1; |
792 | 995 |
} |
793 | 996 |
return 0; |
... | ... | |
798 | 1001 |
* |
799 | 1002 |
* @param id the new personal area network (PAN) id |
800 | 1003 |
**/ |
801 |
void xbee_set_pan_id(int id)
|
|
1004 |
int xbee_set_pan_id(int id)
|
|
802 | 1005 |
{ |
803 | 1006 |
char s[3]; |
804 | 1007 |
s[0] = (id >> 8) & 0xFF; |
805 | 1008 |
s[1] = id & 0xFF; |
806 | 1009 |
s[2] = 0; |
807 | 1010 |
xbee_pending_panID = id; |
808 |
xbee_send_modify_at_command("ID", s); |
|
1011 |
return xbee_send_modify_at_command("ID", s);
|
|
809 | 1012 |
} |
810 | 1013 |
|
811 | 1014 |
/** |
812 | 1015 |
* Get the PAN ID for the XBee. |
813 |
*
|
|
1016 |
* |
|
814 | 1017 |
* @return the personal area network id, or |
815 | 1018 |
* XBEE_PAN_DEFAULT if it has not yet been set. |
816 | 1019 |
**/ |
... | ... | |
822 | 1025 |
/** |
823 | 1026 |
* Set the channel the XBee is using. |
824 | 1027 |
* |
825 |
* @param channel the channel the XBee will not use,
|
|
1028 |
* @param channel the channel the XBee will not use, |
|
826 | 1029 |
* between 0x0B and 0x1A |
827 | 1030 |
* |
828 | 1031 |
* @see xbee_get_channel |
829 | 1032 |
**/ |
830 |
void xbee_set_channel(int channel)
|
|
1033 |
int xbee_set_channel(int channel)
|
|
831 | 1034 |
{ |
832 | 1035 |
if (channel < 0x0B || channel > 0x1A) |
833 | 1036 |
{ |
834 | 1037 |
WL_DEBUG_PRINT("Channel out of range.\r\n"); |
835 |
return; |
|
1038 |
return -1;
|
|
836 | 1039 |
} |
1040 |
|
|
837 | 1041 |
char s[3]; |
838 | 1042 |
s[0] = channel & 0xFF; |
839 | 1043 |
s[1] = 0; |
840 | 1044 |
xbee_pending_channel = channel; |
841 |
xbee_send_modify_at_command("CH", s); |
|
1045 |
|
|
1046 |
return xbee_send_modify_at_command("CH", s); |
|
842 | 1047 |
} |
843 | 1048 |
|
844 | 1049 |
/** |
... | ... | |
871 | 1076 |
xbee_com_port = port; |
872 | 1077 |
} |
873 | 1078 |
#endif |
874 |
|
Also available in: Unified diff