Revision 346
Undid Jason's style changes. I like tabs.
xbee.c | ||
---|---|---|
90 | 90 |
|
91 | 91 |
int xbee_handle_packet(char* packet, int len); |
92 | 92 |
void xbee_handle_at_command_response(char* command, char result, |
93 |
char* extra, int extraLen);
|
|
93 |
char* extra, int extraLen); |
|
94 | 94 |
void xbee_handle_status(char status); |
95 | 95 |
int xbee_verify_checksum(char* packet, int len); |
96 | 96 |
char xbee_compute_checksum(char* packet, int len); |
... | ... | |
136 | 136 |
* to the buffer. |
137 | 137 |
**/ |
138 | 138 |
#ifndef FIREFLY |
139 |
ISR(USART1_RX_vect) { |
|
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 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
147 |
} |
|
148 |
buffer_last = t; |
|
139 |
ISR(USART1_RX_vect) |
|
140 |
{ |
|
141 |
char c = UDR1; |
|
142 |
arrival_buf[buffer_last] = c; |
|
143 |
int t = buffer_last + 1; |
|
144 |
if (t == XBEE_BUFFER_SIZE) |
|
145 |
t = 0; |
|
146 |
if (t == buffer_first) |
|
147 |
{ |
|
148 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
149 |
} |
|
150 |
buffer_last = t; |
|
149 | 151 |
} |
150 | 152 |
#else |
151 |
SIGNAL(SIG_USART0_RECV) { |
|
152 |
char c = UDR0; |
|
153 |
arrival_buf[buffer_last] = c; |
|
154 |
int t = buffer_last + 1; |
|
155 |
if (t == XBEE_BUFFER_SIZE) |
|
156 |
t = 0; |
|
157 |
if (t == buffer_first) { |
|
158 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
159 |
} |
|
160 |
buffer_last = t; |
|
153 |
SIGNAL(SIG_USART0_RECV) |
|
154 |
{ |
|
155 |
char c = UDR0; |
|
156 |
arrival_buf[buffer_last] = c; |
|
157 |
int t = buffer_last + 1; |
|
158 |
if (t == XBEE_BUFFER_SIZE) |
|
159 |
t = 0; |
|
160 |
if (t == buffer_first) |
|
161 |
{ |
|
162 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
163 |
} |
|
164 |
buffer_last = t; |
|
161 | 165 |
} |
162 | 166 |
#endif |
163 | 167 |
|
... | ... | |
166 | 170 |
/** |
167 | 171 |
* Thread that listens to the xbee. |
168 | 172 |
**/ |
169 |
void* listen_to_xbee(void* x) { |
|
170 |
char c; |
|
171 |
while (1) { |
|
172 |
xbee_read(&c, 1); |
|
173 |
arrival_buf[buffer_last] = c; |
|
174 |
int t = buffer_last + 1; |
|
175 |
if (t == XBEE_BUFFER_SIZE) |
|
176 |
t = 0; |
|
177 |
if (t == buffer_first) { |
|
178 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
179 |
} |
|
180 |
buffer_last = t; |
|
181 |
} |
|
182 |
return 0; |
|
173 |
void* listen_to_xbee(void* x) |
|
174 |
{ |
|
175 |
char c; |
|
176 |
while (1) |
|
177 |
{ |
|
178 |
xbee_read(&c, 1); |
|
179 |
arrival_buf[buffer_last] = c; |
|
180 |
int t = buffer_last + 1; |
|
181 |
if (t == XBEE_BUFFER_SIZE) |
|
182 |
t = 0; |
|
183 |
if (t == buffer_first) |
|
184 |
{ |
|
185 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
186 |
} |
|
187 |
buffer_last = t; |
|
188 |
} |
|
189 |
return 0; |
|
183 | 190 |
} |
184 | 191 |
|
185 | 192 |
#endif |
... | ... | |
187 | 194 |
/** |
188 | 195 |
* Initializes the XBee library so that other functions may be used. |
189 | 196 |
**/ |
190 |
int xbee_lib_init(void) { |
|
191 |
arrival_buf[0] = 'A'; |
|
192 |
arrival_buf[1] = 'A'; |
|
193 |
arrival_buf[2] = 'A'; |
|
194 |
#ifdef ROBOT |
|
197 |
int xbee_lib_init(void) |
|
198 |
{ |
|
199 |
arrival_buf[0] = 'A'; |
|
200 |
arrival_buf[1] = 'A'; |
|
201 |
arrival_buf[2] = 'A'; |
|
202 |
#ifdef ROBOT |
|
195 | 203 |
|
196 |
//enable the receiving interrupt |
|
197 |
#ifdef FIREFLY |
|
198 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
|
199 |
#else |
|
200 |
UCSR1B |= _BV(RXCIE); |
|
201 |
#endif |
|
202 |
sei(); |
|
203 |
#else |
|
204 |
printf("Connecting to port %s.\n", xbee_com_port); |
|
205 |
xbee_stream = open(xbee_com_port, O_RDWR); |
|
206 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) { |
|
207 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port); |
|
208 |
return -1; |
|
209 |
} |
|
204 |
//enable the receiving interrupt |
|
205 |
#ifdef FIREFLY |
|
206 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
|
207 |
#else |
|
208 |
UCSR1B |= _BV(RXCIE); |
|
209 |
#endif |
|
210 |
sei(); |
|
211 |
#else |
|
212 |
printf("Connecting to port %s.\n", xbee_com_port); |
|
213 |
xbee_stream = open(xbee_com_port, O_RDWR); |
|
214 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
|
215 |
{ |
|
216 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port); |
|
217 |
return -1; |
|
218 |
} |
|
210 | 219 |
|
211 |
// set baud rate, etc. correctly
|
|
212 |
struct termios options;
|
|
220 |
// set baud rate, etc. correctly
|
|
221 |
struct termios options;
|
|
213 | 222 |
|
214 |
tcgetattr(xbee_stream, &options);
|
|
215 |
cfsetispeed(&options, B9600);
|
|
216 |
cfsetospeed(&options, B9600);
|
|
217 |
options.c_iflag &= ~ICRNL;
|
|
218 |
options.c_oflag &= ~OCRNL;
|
|
219 |
options.c_cflag |= (CLOCAL | CREAD);
|
|
220 |
options.c_cflag &= ~PARENB;
|
|
221 |
options.c_cflag &= ~CSTOPB;
|
|
222 |
options.c_cflag &= ~CSIZE;
|
|
223 |
options.c_cflag |= CS8;
|
|
224 |
options.c_lflag &= ~ICANON;
|
|
225 |
options.c_cc[VMIN] = 1;
|
|
226 |
options.c_cc[VTIME] = 50;
|
|
223 |
tcgetattr(xbee_stream, &options);
|
|
224 |
cfsetispeed(&options, B9600);
|
|
225 |
cfsetospeed(&options, B9600);
|
|
226 |
options.c_iflag &= ~ICRNL;
|
|
227 |
options.c_oflag &= ~OCRNL;
|
|
228 |
options.c_cflag |= (CLOCAL | CREAD);
|
|
229 |
options.c_cflag &= ~PARENB;
|
|
230 |
options.c_cflag &= ~CSTOPB;
|
|
231 |
options.c_cflag &= ~CSIZE;
|
|
232 |
options.c_cflag |= CS8;
|
|
233 |
options.c_lflag &= ~ICANON;
|
|
234 |
options.c_cc[VMIN] = 1;
|
|
235 |
options.c_cc[VTIME] = 50;
|
|
227 | 236 |
|
228 |
if (tcsetattr(xbee_stream, TCSANOW, &options)) { |
|
229 |
fprintf(stderr, "Error setting attributes.\n"); |
|
230 |
return -1; |
|
231 |
} |
|
237 |
if (tcsetattr(xbee_stream, TCSANOW, &options)) |
|
238 |
{ |
|
239 |
fprintf(stderr, "Error setting attributes.\n"); |
|
240 |
return -1; |
|
241 |
} |
|
232 | 242 |
|
233 |
//lockf(xbee_stream, F_LOCK, 0);
|
|
243 |
//lockf(xbee_stream, F_LOCK, 0);
|
|
234 | 244 |
|
235 |
xbee_listen_thread = |
|
236 |
(pthread_t*)malloc(sizeof(pthread_t)); |
|
237 |
if (xbee_listen_thread == NULL) { |
|
238 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__); |
|
239 |
return -1; |
|
240 |
} |
|
245 |
xbee_listen_thread = |
|
246 |
(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 |
} |
|
241 | 252 |
|
242 |
int ret = pthread_create(xbee_listen_thread, NULL, |
|
243 |
listen_to_xbee, NULL); |
|
244 |
if (ret) { |
|
245 |
fprintf(stderr, "Failed to create listener thread.\r\n"); |
|
246 |
return -1; |
|
247 |
} |
|
253 |
int ret = pthread_create(xbee_listen_thread, NULL, |
|
254 |
listen_to_xbee, NULL); |
|
255 |
if (ret) |
|
256 |
{ |
|
257 |
fprintf(stderr, "Failed to create listener thread.\r\n"); |
|
258 |
return -1; |
|
259 |
} |
|
248 | 260 |
|
249 |
#endif |
|
250 |
xbee_enter_command_mode();
|
|
251 |
xbee_enter_api_mode();
|
|
252 |
xbee_exit_command_mode();
|
|
253 |
xbee_send_read_at_command("MY");
|
|
261 |
#endif
|
|
262 |
xbee_enter_command_mode();
|
|
263 |
xbee_enter_api_mode();
|
|
264 |
xbee_exit_command_mode();
|
|
265 |
xbee_send_read_at_command("MY");
|
|
254 | 266 |
|
255 |
//wait to return until the address is set
|
|
256 |
while (xbee_address == 0) xbee_get_packet(NULL);
|
|
267 |
//wait to return until the address is set
|
|
268 |
while (xbee_address == 0) xbee_get_packet(NULL);
|
|
257 | 269 |
|
258 |
return 0; |
|
270 |
|
|
271 |
return 0; |
|
259 | 272 |
} |
260 | 273 |
|
261 | 274 |
/** |
262 | 275 |
* Call when finished using the XBee library. This releases |
263 | 276 |
* all sued resources. |
264 | 277 |
**/ |
265 |
void xbee_terminate() { |
|
266 |
#ifndef ROBOT |
|
267 |
pthread_cancel(*xbee_listen_thread); |
|
268 |
free(xbee_listen_thread); |
|
269 |
lockf(xbee_stream, F_ULOCK, 0); |
|
270 |
close(xbee_stream); |
|
271 |
#endif |
|
278 |
void xbee_terminate() |
|
279 |
{ |
|
280 |
#ifndef ROBOT |
|
281 |
pthread_cancel(*xbee_listen_thread); |
|
282 |
free(xbee_listen_thread); |
|
283 |
lockf(xbee_stream, F_ULOCK, 0); |
|
284 |
close(xbee_stream); |
|
285 |
#endif |
|
272 | 286 |
} |
273 | 287 |
|
274 | 288 |
/** |
... | ... | |
277 | 291 |
* @param buf the buffer of data to send |
278 | 292 |
* @param size the number of bytes to send |
279 | 293 |
**/ |
280 |
void xbee_send(char* buf, int size) { |
|
281 |
#ifdef ROBOT |
|
282 |
int i; |
|
283 |
for (i = 0; i < size; i++) |
|
284 |
xbee_putc(buf[i]); |
|
285 |
#else |
|
286 |
int ret = write(xbee_stream, buf, size); |
|
287 |
//success |
|
288 |
if (ret == size) |
|
289 |
return; |
|
290 |
if (ret == -1) { |
|
291 |
//interrupted by system signal, probably timer interrupt. |
|
292 |
//just try again |
|
293 |
if (errno == 4) { |
|
294 |
xbee_send(buf, size); |
|
295 |
return; |
|
296 |
} |
|
297 |
printf("Failed to write to xbee, error %i.\r\n", errno); |
|
298 |
return; |
|
299 |
} |
|
294 |
void xbee_send(char* buf, int size) |
|
295 |
{ |
|
296 |
#ifdef ROBOT |
|
297 |
int i; |
|
298 |
for (i = 0; i < size; i++) |
|
299 |
xbee_putc(buf[i]); |
|
300 |
#else |
|
301 |
int ret = write(xbee_stream, buf, size); |
|
302 |
//success |
|
303 |
if (ret == size) |
|
304 |
return; |
|
305 |
if (ret == -1) |
|
306 |
{ |
|
307 |
//interrupted by system signal, probably timer interrupt. |
|
308 |
//just try again |
|
309 |
if (errno == 4) |
|
310 |
{ |
|
311 |
xbee_send(buf, size); |
|
312 |
return; |
|
313 |
} |
|
314 |
printf("Failed to write to xbee, error %i.\r\n", errno); |
|
315 |
return; |
|
316 |
} |
|
300 | 317 |
|
301 |
//write was interrupted after writing ret bytes
|
|
302 |
xbee_send(buf + ret, size - ret);
|
|
303 |
#endif |
|
318 |
//write was interrupted after writing ret bytes
|
|
319 |
xbee_send(buf + ret, size - ret);
|
|
320 |
#endif
|
|
304 | 321 |
} |
305 | 322 |
|
306 | 323 |
/** |
... | ... | |
308 | 325 |
* |
309 | 326 |
* @param c the string to send to the XBEE |
310 | 327 |
**/ |
311 |
void xbee_send_string(char* c) { |
|
312 |
xbee_send(c, strlen(c)); |
|
328 |
void xbee_send_string(char* c) |
|
329 |
{ |
|
330 |
xbee_send(c, strlen(c)); |
|
313 | 331 |
} |
314 | 332 |
|
315 | 333 |
#ifndef ROBOT |
316 |
void xbee_read(char* buf, int size) { |
|
317 |
if (read(xbee_stream, buf, size) == -1) |
|
318 |
printf("Failed to read from xbee.\r\n"); |
|
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"); |
|
319 | 338 |
} |
320 | 339 |
#endif |
321 | 340 |
|
322 | 341 |
/** |
323 | 342 |
* Enter into command mode. |
324 | 343 |
**/ |
325 |
void xbee_enter_command_mode() { |
|
326 |
xbee_send_string("+++"); |
|
327 |
xbee_wait_for_ok(); |
|
344 |
void xbee_enter_command_mode() |
|
345 |
{ |
|
346 |
xbee_send_string("+++"); |
|
347 |
xbee_wait_for_ok(); |
|
328 | 348 |
} |
329 | 349 |
|
330 | 350 |
/** |
331 | 351 |
* Exit from command mode. |
332 | 352 |
**/ |
333 |
void xbee_exit_command_mode() { |
|
334 |
xbee_send_string("ATCN\r"); |
|
335 |
xbee_wait_for_ok(); |
|
353 |
void xbee_exit_command_mode() |
|
354 |
{ |
|
355 |
xbee_send_string("ATCN\r"); |
|
356 |
xbee_wait_for_ok(); |
|
336 | 357 |
} |
337 | 358 |
|
338 | 359 |
/** |
339 | 360 |
* Enter API mode. |
340 | 361 |
**/ |
341 |
void xbee_enter_api_mode() { |
|
342 |
xbee_send_string("ATAP 1\r"); |
|
343 |
xbee_wait_for_ok(); |
|
362 |
void xbee_enter_api_mode() |
|
363 |
{ |
|
364 |
xbee_send_string("ATAP 1\r"); |
|
365 |
xbee_wait_for_ok(); |
|
344 | 366 |
} |
345 | 367 |
|
346 | 368 |
/** |
347 | 369 |
* Exit API mode. (warning - does not check for response) |
348 | 370 |
**/ |
349 |
void xbee_exit_api_mode() { |
|
350 |
xbee_send_string("ATAP 0\r"); |
|
371 |
void xbee_exit_api_mode() |
|
372 |
{ |
|
373 |
xbee_send_string("ATAP 0\r"); |
|
351 | 374 |
} |
352 | 375 |
|
353 | 376 |
/** |
354 | 377 |
* Wait until the string "OK\r" is received from the XBee. |
355 | 378 |
**/ |
356 |
void xbee_wait_for_ok() { |
|
357 |
xbee_wait_for_string("OK\r", 3); |
|
379 |
void xbee_wait_for_ok() |
|
380 |
{ |
|
381 |
xbee_wait_for_string("OK\r", 3); |
|
358 | 382 |
} |
359 | 383 |
|
360 | 384 |
/** |
... | ... | |
364 | 388 |
* @param s the string to receive |
365 | 389 |
* @param len the length of the string |
366 | 390 |
**/ |
367 |
void xbee_wait_for_string(char* s, int len) { |
|
368 |
char* curr = s; |
|
369 |
while (curr - s < len) { |
|
370 |
// check if buffer is empty |
|
371 |
if (buffer_last == buffer_first) |
|
372 |
continue; |
|
373 |
char c = arrival_buf[buffer_first++]; |
|
374 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
375 |
buffer_first = 0; |
|
376 |
if (c == *curr) |
|
377 |
curr++; |
|
378 |
else |
|
379 |
curr = s; |
|
380 |
} |
|
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 |
} |
|
381 | 407 |
} |
382 | 408 |
|
383 | 409 |
/** |
... | ... | |
393 | 419 |
* @return 0 if the checksum is incorrect, nonzero |
394 | 420 |
* otherwise |
395 | 421 |
**/ |
396 |
int xbee_verify_checksum(char* packet, int len) { |
|
397 |
unsigned char sum = 0; |
|
398 |
int i; |
|
399 |
for (i = 3; i < len; i++) |
|
400 |
sum += (unsigned char)packet[i]; |
|
401 |
return sum == 0xFF; |
|
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; |
|
402 | 429 |
} |
403 | 430 |
|
404 | 431 |
/** |
... | ... | |
410 | 437 |
* @return the checksum of the packet, which will |
411 | 438 |
* become the last byte sent in the packet |
412 | 439 |
**/ |
413 |
char xbee_compute_checksum(char* buf, int len) { |
|
414 |
int i; |
|
415 |
unsigned char sum = 0; |
|
416 |
for (i = 0; i < len; i++) |
|
417 |
sum += (unsigned char)buf[i]; |
|
418 |
return 0xFF - sum; |
|
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; |
|
419 | 447 |
} |
420 | 448 |
|
421 | 449 |
/** |
... | ... | |
427 | 455 |
* @param len the size in bytes of the packet data |
428 | 456 |
* |
429 | 457 |
**/ |
430 |
void xbee_send_frame(char* buf, int len) { |
|
431 |
char prefix[3]; |
|
432 |
prefix[0] = XBEE_FRAME_START; |
|
433 |
prefix[1] = (len & 0xFF00) >> 8; |
|
434 |
prefix[2] = len & 0xFF; |
|
435 |
char checksum = xbee_compute_checksum(buf, len); |
|
436 |
xbee_send(prefix, 3); |
|
437 |
xbee_send(buf, len); |
|
438 |
xbee_send(&checksum, 1); |
|
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); |
|
439 | 468 |
} |
440 | 469 |
|
441 | 470 |
/** |
... | ... | |
445 | 474 |
* use ID to read the PAN ID and MY to return the XBee ID. |
446 | 475 |
* See the XBee reference guide for a complete listing. |
447 | 476 |
**/ |
448 |
void xbee_send_read_at_command(char* command) { |
|
449 |
xbee_send_modify_at_command(command, NULL); |
|
477 |
void xbee_send_read_at_command(char* command) |
|
478 |
{ |
|
479 |
xbee_send_modify_at_command(command, NULL); |
|
450 | 480 |
} |
451 | 481 |
|
452 | 482 |
/** |
... | ... | |
456 | 486 |
* @param value the value to pass as a parameter |
457 | 487 |
* (or NULL if there is no parameter) |
458 | 488 |
**/ |
459 |
void xbee_send_modify_at_command(char* command, char* value) { |
|
460 |
char buf[16]; |
|
461 |
int i; |
|
489 |
void xbee_send_modify_at_command(char* command, char* value) |
|
490 |
{ |
|
491 |
char buf[16]; |
|
492 |
int i; |
|
462 | 493 |
|
463 |
buf[0] = XBEE_FRAME_AT_COMMAND; |
|
464 |
buf[1] = 1; |
|
465 |
buf[2] = command[0]; |
|
466 |
buf[3] = command[1]; |
|
467 |
int valueLen = 0; |
|
468 |
if (value != NULL) { |
|
469 |
valueLen = strlen(value); |
|
470 |
if (valueLen > 8) { |
|
471 |
WL_DEBUG_PRINT("AT Command too large.\r\n"); |
|
472 |
return; |
|
473 |
} |
|
474 |
for (i = 0; i < valueLen; i++) |
|
475 |
buf[4 + i] = value[i]; |
|
476 |
} |
|
477 |
xbee_send_frame(buf, 4 + valueLen); |
|
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); |
|
478 | 511 |
} |
479 | 512 |
|
480 | 513 |
/** |
... | ... | |
497 | 530 |
* was received. |
498 | 531 |
**/ |
499 | 532 |
void xbee_send_packet(char* packet, int len, int dest, |
500 |
char options, char frame) { |
|
501 |
char buf[5]; |
|
502 |
char prefix[3]; |
|
503 |
int i; |
|
504 |
unsigned char checksum = 0; |
|
533 |
char options, char frame) |
|
534 |
{ |
|
535 |
char buf[5]; |
|
536 |
char prefix[3]; |
|
537 |
int i; |
|
538 |
unsigned char checksum = 0; |
|
505 | 539 |
|
506 |
if (len > 100) { |
|
507 |
WL_DEBUG_PRINT("Packet is too large.\r\n"); |
|
508 |
return; |
|
509 |
} |
|
540 |
if (len > 100) |
|
541 |
{ |
|
542 |
WL_DEBUG_PRINT("Packet is too large.\r\n"); |
|
543 |
return; |
|
544 |
} |
|
510 | 545 |
|
511 |
//data for sending request
|
|
512 |
buf[0] = XBEE_FRAME_TX_REQUEST_16;
|
|
513 |
buf[1] = frame;
|
|
514 |
buf[2] = (dest >> 8) & 0xFF;
|
|
515 |
buf[3] = dest & 0xFF;
|
|
516 |
buf[4] = options;
|
|
546 |
//data for sending request
|
|
547 |
buf[0] = XBEE_FRAME_TX_REQUEST_16;
|
|
548 |
buf[1] = frame;
|
|
549 |
buf[2] = (dest >> 8) & 0xFF;
|
|
550 |
buf[3] = dest & 0xFF;
|
|
551 |
buf[4] = options;
|
|
517 | 552 |
|
518 |
//packet prefix, do this here so we don't need an extra buffer
|
|
519 |
prefix[0] = XBEE_FRAME_START;
|
|
520 |
prefix[1] = ((5 + len) & 0xFF00) >> 8;
|
|
521 |
prefix[2] = (5 + len) & 0xFF;
|
|
553 |
//packet prefix, do this here so we don't need an extra buffer
|
|
554 |
prefix[0] = XBEE_FRAME_START;
|
|
555 |
prefix[1] = ((5 + len) & 0xFF00) >> 8;
|
|
556 |
prefix[2] = (5 + len) & 0xFF;
|
|
522 | 557 |
|
523 |
for (i = 0; i < 5; i++)
|
|
524 |
checksum += (unsigned char)buf[i];
|
|
525 |
for (i = 0; i < len; i++)
|
|
526 |
checksum += (unsigned char)packet[i];
|
|
527 |
checksum = 0xFF - checksum;
|
|
528 |
xbee_send(prefix, 3);
|
|
529 |
xbee_send(buf, 5);
|
|
530 |
xbee_send(packet, len);
|
|
531 |
xbee_send((char*)&checksum, 1);
|
|
558 |
for (i = 0; i < 5; i++)
|
|
559 |
checksum += (unsigned char)buf[i];
|
|
560 |
for (i = 0; i < len; i++)
|
|
561 |
checksum += (unsigned char)packet[i];
|
|
562 |
checksum = 0xFF - checksum;
|
|
563 |
xbee_send(prefix, 3);
|
|
564 |
xbee_send(buf, 5);
|
|
565 |
xbee_send(packet, len);
|
|
566 |
xbee_send((char*)&checksum, 1);
|
|
532 | 567 |
} |
533 | 568 |
|
534 | 569 |
/** |
... | ... | |
565 | 600 |
* @return the length of the packet, or -1 if no packet |
566 | 601 |
* is available |
567 | 602 |
**/ |
568 |
int xbee_get_packet(unsigned char* dest) { |
|
569 |
//start reading a packet with XBEE_FRAME_START |
|
570 |
if (currentBufPos == 0) { |
|
571 |
do { |
|
572 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
573 |
buffer_first = 0; |
|
574 |
// check if buffer is empty |
|
575 |
if (buffer_first == buffer_last) |
|
576 |
return -1; |
|
577 |
} |
|
578 |
while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
579 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
580 |
buffer_first = 0; |
|
581 |
xbee_buf[0] = XBEE_FRAME_START; |
|
582 |
currentBufPos++; |
|
583 |
} |
|
603 |
int xbee_get_packet(unsigned char* dest) |
|
604 |
{ |
|
605 |
//start reading a packet with XBEE_FRAME_START |
|
606 |
if (currentBufPos == 0) |
|
607 |
{ |
|
608 |
do |
|
609 |
{ |
|
610 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
611 |
buffer_first = 0; |
|
612 |
// check if buffer is empty |
|
613 |
if (buffer_first == buffer_last) |
|
614 |
return -1; |
|
615 |
} |
|
616 |
while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
617 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
618 |
buffer_first = 0; |
|
619 |
xbee_buf[0] = XBEE_FRAME_START; |
|
620 |
currentBufPos++; |
|
621 |
} |
|
584 | 622 |
|
585 |
int len = -1;
|
|
586 |
if (currentBufPos >= 3)
|
|
587 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
|
|
623 |
int len = -1;
|
|
624 |
if (currentBufPos >= 3)
|
|
625 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8);
|
|
588 | 626 |
|
589 |
while (len == -1 //packet length has not been read yet |
|
590 |
|| currentBufPos < len + 4) { |
|
591 |
if (currentBufPos == 3) { |
|
592 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
|
593 |
if (len > 120) { |
|
594 |
WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n"); |
|
627 |
while (len == -1 //packet length has not been read yet |
|
628 |
|| currentBufPos < len + 4) |
|
629 |
{ |
|
630 |
if (currentBufPos == 3) |
|
631 |
{ |
|
632 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
|
633 |
if (len > 120) |
|
634 |
{ |
|
635 |
WL_DEBUG_PRINT("Packet too large. Probably error in XBee transmission.\n"); |
|
636 |
currentBufPos = 0; |
|
637 |
return -1; |
|
638 |
} |
|
639 |
} |
|
640 |
// check if buffer is empty |
|
641 |
if (buffer_first == buffer_last) |
|
642 |
return -1; |
|
643 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
|
644 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
645 |
buffer_first = 0; |
|
646 |
} |
|
647 |
|
|
595 | 648 |
currentBufPos = 0; |
596 |
return -1; |
|
597 |
} |
|
598 |
} |
|
599 |
// check if buffer is empty |
|
600 |
if (buffer_first == buffer_last) |
|
601 |
return -1; |
|
602 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
|
603 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
604 |
buffer_first = 0; |
|
605 |
} |
|
606 | 649 |
|
607 |
currentBufPos = 0; |
|
608 |
|
|
609 |
if (!xbee_verify_checksum(xbee_buf, len + 4)) { |
|
610 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n"); |
|
611 |
return -1; |
|
612 |
} |
|
650 |
if (!xbee_verify_checksum(xbee_buf, len + 4)) |
|
651 |
{ |
|
652 |
WL_DEBUG_PRINT("XBee checksum failed.\r\n"); |
|
653 |
return -1; |
|
654 |
} |
|
613 | 655 |
|
614 |
//we will take care of the packet
|
|
615 |
if (xbee_handle_packet(xbee_buf + 3, len))
|
|
616 |
return -1;
|
|
656 |
//we will take care of the packet
|
|
657 |
if (xbee_handle_packet(xbee_buf + 3, len))
|
|
658 |
return -1;
|
|
617 | 659 |
|
618 |
if (dest == NULL)
|
|
619 |
return -1;
|
|
660 |
if (dest == NULL)
|
|
661 |
return -1;
|
|
620 | 662 |
|
621 |
int i;
|
|
622 |
for (i = 3; i < len + 3; i++)
|
|
623 |
dest[i - 3] = xbee_buf[i];
|
|
624 |
return len;
|
|
663 |
int i;
|
|
664 |
for (i = 3; i < len + 3; i++)
|
|
665 |
dest[i - 3] = xbee_buf[i];
|
|
666 |
return len;
|
|
625 | 667 |
} |
626 | 668 |
|
627 | 669 |
/** |
... | ... | |
629 | 671 |
* |
630 | 672 |
* @param status the type of status packet received. |
631 | 673 |
**/ |
632 |
void xbee_handle_status(char status) { |
|
633 |
switch (status) { |
|
634 |
case 0: |
|
635 |
WL_DEBUG_PRINT("XBee hardware reset.\r\n"); |
|
636 |
break; |
|
637 |
case 1: |
|
638 |
WL_DEBUG_PRINT("Watchdog timer reset.\r\n"); |
|
639 |
break; |
|
640 |
case 2: |
|
641 |
WL_DEBUG_PRINT("Associated.\r\n"); |
|
642 |
break; |
|
643 |
case 3: |
|
644 |
WL_DEBUG_PRINT("Disassociated.\r\n"); |
|
645 |
break; |
|
646 |
case 4: |
|
647 |
WL_DEBUG_PRINT("Synchronization lost.\r\n"); |
|
648 |
break; |
|
649 |
case 5: |
|
650 |
WL_DEBUG_PRINT("Coordinator realignment.\r\n"); |
|
651 |
break; |
|
652 |
case 6: |
|
653 |
WL_DEBUG_PRINT("Coordinator started.\r\n"); |
|
654 |
break; |
|
655 |
} |
|
674 |
void xbee_handle_status(char status) |
|
675 |
{ |
|
676 |
switch (status) |
|
677 |
{ |
|
678 |
case 0: |
|
679 |
WL_DEBUG_PRINT("XBee hardware reset.\r\n"); |
|
680 |
break; |
|
681 |
case 1: |
|
682 |
WL_DEBUG_PRINT("Watchdog timer reset.\r\n"); |
|
683 |
break; |
|
684 |
case 2: |
|
685 |
WL_DEBUG_PRINT("Associated.\r\n"); |
|
686 |
break; |
|
687 |
case 3: |
|
688 |
WL_DEBUG_PRINT("Disassociated.\r\n"); |
|
689 |
break; |
|
690 |
case 4: |
|
691 |
WL_DEBUG_PRINT("Synchronization lost.\r\n"); |
|
692 |
break; |
|
693 |
case 5: |
|
694 |
WL_DEBUG_PRINT("Coordinator realignment.\r\n"); |
|
695 |
break; |
|
696 |
case 6: |
|
697 |
WL_DEBUG_PRINT("Coordinator started.\r\n"); |
|
698 |
break; |
|
699 |
} |
|
656 | 700 |
} |
657 | 701 |
|
658 | 702 |
/** |
... | ... | |
663 | 707 |
* @param extraLen the length in bytes of extra |
664 | 708 |
**/ |
665 | 709 |
void xbee_handle_at_command_response(char* command, char result, |
666 |
char* extra, int extraLen) { |
|
667 |
if (result == 1) { |
|
668 |
WL_DEBUG_PRINT("Error with AT"); |
|
669 |
WL_DEBUG_PRINT(command); |
|
670 |
WL_DEBUG_PRINT(" packet.\r\n"); |
|
671 |
} |
|
672 |
WL_DEBUG_PRINT("AT"); |
|
673 |
WL_DEBUG_PRINT(command); |
|
674 |
WL_DEBUG_PRINT(" command was successful.\r\n"); |
|
710 |
char* extra, int extraLen) |
|
711 |
{ |
|
712 |
if (result == 1) |
|
713 |
{ |
|
714 |
WL_DEBUG_PRINT("Error with AT"); |
|
715 |
WL_DEBUG_PRINT(command); |
|
716 |
WL_DEBUG_PRINT(" packet.\r\n"); |
|
717 |
} |
|
718 |
WL_DEBUG_PRINT("AT"); |
|
719 |
WL_DEBUG_PRINT(command); |
|
720 |
WL_DEBUG_PRINT(" command was successful.\r\n"); |
|
675 | 721 |
|
676 |
if (command[0] == 'I' && command[1] == 'D') { |
|
677 |
xbee_panID = xbee_pending_panID; |
|
678 |
WL_DEBUG_PRINT("PAN ID set to "); |
|
679 |
WL_DEBUG_PRINT_INT(xbee_panID); |
|
680 |
WL_DEBUG_PRINT(".\r\n"); |
|
681 |
return; |
|
682 |
} |
|
722 |
if (command[0] == 'I' && command[1] == 'D') |
|
723 |
{ |
|
724 |
xbee_panID = xbee_pending_panID; |
|
725 |
WL_DEBUG_PRINT("PAN ID set to "); |
|
726 |
WL_DEBUG_PRINT_INT(xbee_panID); |
|
727 |
WL_DEBUG_PRINT(".\r\n"); |
|
728 |
return; |
|
729 |
} |
|
683 | 730 |
|
684 |
if (command[0] == 'C' && command[1] == 'H') { |
|
685 |
xbee_channel = xbee_pending_channel; |
|
686 |
WL_DEBUG_PRINT("Channel set to "); |
|
687 |
WL_DEBUG_PRINT_INT(xbee_channel); |
|
688 |
WL_DEBUG_PRINT(".\r\n"); |
|
689 |
return; |
|
690 |
} |
|
731 |
if (command[0] == 'C' && command[1] == 'H') |
|
732 |
{ |
|
733 |
xbee_channel = xbee_pending_channel; |
|
734 |
WL_DEBUG_PRINT("Channel set to "); |
|
735 |
WL_DEBUG_PRINT_INT(xbee_channel); |
|
736 |
WL_DEBUG_PRINT(".\r\n"); |
|
737 |
return; |
|
738 |
} |
|
691 | 739 |
|
692 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) { |
|
693 |
xbee_address = 0; |
|
694 |
int i; |
|
695 |
for (i = 0; i < extraLen; i++) |
|
696 |
xbee_address = (xbee_address << 8) + extra[i]; |
|
740 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) |
|
741 |
{ |
|
742 |
xbee_address = 0; |
|
743 |
int i; |
|
744 |
for (i = 0; i < extraLen; i++) |
|
745 |
xbee_address = (xbee_address << 8) + extra[i]; |
|
697 | 746 |
|
698 |
WL_DEBUG_PRINT("XBee address is ");
|
|
699 |
WL_DEBUG_PRINT_INT(xbee_address);
|
|
700 |
WL_DEBUG_PRINT(".\r\n");
|
|
747 |
WL_DEBUG_PRINT("XBee address is ");
|
|
748 |
WL_DEBUG_PRINT_INT(xbee_address);
|
|
749 |
WL_DEBUG_PRINT(".\r\n");
|
|
701 | 750 |
|
702 |
if (xbee_address == 0) { |
|
703 |
WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n"); |
|
704 |
exit(0); |
|
705 |
} |
|
706 |
} |
|
751 |
if (xbee_address == 0) |
|
752 |
{ |
|
753 |
WL_DEBUG_PRINT("XBee 16-bit address must be set using ATMY.\r\n"); |
|
754 |
exit(0); |
|
755 |
} |
|
756 |
} |
|
707 | 757 |
} |
708 | 758 |
|
709 | 759 |
/** |
... | ... | |
718 | 768 |
* |
719 | 769 |
* @return 1 if we have handled the packet, 0 otherwise |
720 | 770 |
*/ |
721 |
int xbee_handle_packet(char* packet, int len) { |
|
722 |
char command[3] = {1, 2, 3}; |
|
723 |
if (len <= 0) { |
|
724 |
//this should not happend |
|
725 |
WL_DEBUG_PRINT("Non-positive packet length.\r\n"); |
|
726 |
return 0; |
|
727 |
} |
|
771 |
int xbee_handle_packet(char* packet, int len) |
|
772 |
{ |
|
773 |
char command[3] = {1, 2, 3}; |
|
774 |
if (len <= 0) //this should not happend |
|
775 |
{ |
|
776 |
WL_DEBUG_PRINT("Non-positive packet length.\r\n"); |
|
777 |
return 0; |
|
778 |
} |
|
728 | 779 |
|
729 |
//packet type
|
|
730 |
switch ((unsigned char)packet[0]) {
|
|
731 |
case XBEE_FRAME_STATUS:
|
|
732 |
xbee_handle_status(packet[1]);
|
|
733 |
return 1;
|
|
734 |
case XBEE_FRAME_AT_COMMAND_RESPONSE:
|
|
735 |
command[0] = packet[2];
|
|
736 |
command[1] = packet[3];
|
|
737 |
command[2] = 0;
|
|
738 |
xbee_handle_at_command_response(command,
|
|
739 |
packet[4], packet + 5, len - 5);
|
|
740 |
return 1;
|
|
741 |
}
|
|
742 |
return 0;
|
|
780 |
switch ((unsigned char)packet[0]) //packet type
|
|
781 |
{
|
|
782 |
case XBEE_FRAME_STATUS:
|
|
783 |
xbee_handle_status(packet[1]);
|
|
784 |
return 1;
|
|
785 |
case XBEE_FRAME_AT_COMMAND_RESPONSE:
|
|
786 |
command[0] = packet[2];
|
|
787 |
command[1] = packet[3];
|
|
788 |
command[2] = 0;
|
|
789 |
xbee_handle_at_command_response(command,
|
|
790 |
packet[4], packet + 5, len - 5); |
|
791 |
return 1;
|
|
792 |
}
|
|
793 |
return 0;
|
|
743 | 794 |
} |
744 | 795 |
|
745 | 796 |
/** |
... | ... | |
747 | 798 |
* |
748 | 799 |
* @param id the new personal area network (PAN) id |
749 | 800 |
**/ |
750 |
void xbee_set_pan_id(int id) { |
|
751 |
char s[3]; |
|
752 |
s[0] = (id >> 8) & 0xFF; |
|
753 |
s[1] = id & 0xFF; |
|
754 |
s[2] = 0; |
|
755 |
xbee_pending_panID = id; |
|
756 |
xbee_send_modify_at_command("ID", s); |
|
801 |
void xbee_set_pan_id(int id) |
|
802 |
{ |
|
803 |
char s[3]; |
|
804 |
s[0] = (id >> 8) & 0xFF; |
|
805 |
s[1] = id & 0xFF; |
|
806 |
s[2] = 0; |
|
807 |
xbee_pending_panID = id; |
|
808 |
xbee_send_modify_at_command("ID", s); |
|
757 | 809 |
} |
758 | 810 |
|
759 | 811 |
/** |
... | ... | |
762 | 814 |
* @return the personal area network id, or |
763 | 815 |
* XBEE_PAN_DEFAULT if it has not yet been set. |
764 | 816 |
**/ |
765 |
unsigned int xbee_get_pan_id() { |
|
766 |
return xbee_panID; |
|
817 |
unsigned int xbee_get_pan_id() |
|
818 |
{ |
|
819 |
return xbee_panID; |
|
767 | 820 |
} |
768 | 821 |
|
769 | 822 |
/** |
... | ... | |
774 | 827 |
* |
775 | 828 |
* @see xbee_get_channel |
776 | 829 |
**/ |
777 |
void xbee_set_channel(int channel) { |
|
778 |
if (channel < 0x0B || channel > 0x1A) { |
|
779 |
WL_DEBUG_PRINT("Channel out of range.\r\n"); |
|
780 |
return; |
|
781 |
} |
|
782 |
char s[3]; |
|
783 |
s[0] = channel & 0xFF; |
|
784 |
s[1] = 0; |
|
785 |
xbee_pending_channel = channel; |
|
786 |
xbee_send_modify_at_command("CH", s); |
|
830 |
void xbee_set_channel(int channel) |
|
831 |
{ |
|
832 |
if (channel < 0x0B || channel > 0x1A) |
|
833 |
{ |
|
834 |
WL_DEBUG_PRINT("Channel out of range.\r\n"); |
|
835 |
return; |
|
836 |
} |
|
837 |
char s[3]; |
|
838 |
s[0] = channel & 0xFF; |
|
839 |
s[1] = 0; |
|
840 |
xbee_pending_channel = channel; |
|
841 |
xbee_send_modify_at_command("CH", s); |
|
787 | 842 |
} |
788 | 843 |
|
789 | 844 |
/** |
... | ... | |
793 | 848 |
* |
794 | 849 |
* @see xbee_set_channel |
795 | 850 |
**/ |
796 |
int xbee_get_channel(void) { |
|
797 |
return xbee_channel; |
|
851 |
int xbee_get_channel(void) |
|
852 |
{ |
|
853 |
return xbee_channel; |
|
798 | 854 |
} |
799 | 855 |
|
800 | 856 |
/** |
... | ... | |
804 | 860 |
* |
805 | 861 |
* @return the 16-bit address of the XBee. |
806 | 862 |
**/ |
807 |
unsigned int xbee_get_address() { |
|
808 |
return xbee_address; |
|
863 |
unsigned int xbee_get_address() |
|
864 |
{ |
|
865 |
return xbee_address; |
|
809 | 866 |
} |
810 | 867 |
|
811 | 868 |
#ifndef ROBOT |
812 |
void xbee_set_com_port(char* port) { |
|
813 |
xbee_com_port = port; |
|
869 |
void xbee_set_com_port(char* port) |
|
870 |
{ |
|
871 |
xbee_com_port = port; |
|
814 | 872 |
} |
815 | 873 |
#endif |
816 | 874 |
|
Also available in: Unified diff