Revision 418
added return values to a bunch of libwireless functions. Makefile for colonetserver now compiles libwireless when necessary. added static to a bunch of libwireless vars. commented out colonet timeout
xbee.c | ||
---|---|---|
70 | 70 |
|
71 | 71 |
/*I/O Functions*/ |
72 | 72 |
static int xbee_send(char* buf, int size); |
73 |
static void xbee_send_string(char* c);
|
|
73 |
static int xbee_send_string(char* c);
|
|
74 | 74 |
|
75 | 75 |
#ifndef ROBOT |
76 |
void xbee_read(char* buf, int size);
|
|
76 |
static int xbee_read(char* buf, int size);
|
|
77 | 77 |
#endif |
78 | 78 |
|
79 | 79 |
/*Command Mode Functions |
80 | 80 |
* Called during initialization. |
81 | 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);
|
|
82 |
static int xbee_enter_command_mode(void);
|
|
83 |
static int xbee_exit_command_mode(void);
|
|
84 |
static int xbee_enter_api_mode(void);
|
|
85 |
static int xbee_exit_api_mode(void);
|
|
86 | 86 |
static void xbee_wait_for_string(char* s, int len); |
87 | 87 |
static void xbee_wait_for_ok(void); |
88 | 88 |
|
89 | 89 |
/*API Mode Functions*/ |
90 | 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);
|
|
91 |
static int xbee_handle_packet(char* packet, int len);
|
|
92 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen);
|
|
93 |
static void xbee_handle_status(char status);
|
|
94 |
static int xbee_verify_checksum(char* packet, int len);
|
|
95 |
static char xbee_compute_checksum(char* packet, int len);
|
|
96 |
static int xbee_send_frame(char* buf, int len);
|
|
97 |
static int xbee_send_read_at_command(char* command);
|
|
98 |
static int xbee_send_modify_at_command(char* command, char* value);
|
|
99 | 99 |
|
100 | 100 |
/*Global Variables*/ |
101 | 101 |
|
... | ... | |
116 | 116 |
|
117 | 117 |
|
118 | 118 |
//used to store packets as they are read |
119 |
char xbee_buf[128]; |
|
120 |
int currentBufPos = 0; |
|
119 |
static char xbee_buf[128];
|
|
120 |
static int currentBufPos = 0;
|
|
121 | 121 |
|
122 | 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; |
|
123 |
static unsigned int xbee_panID = XBEE_PAN_DEFAULT;
|
|
124 |
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
|
|
125 |
static int xbee_channel = XBEE_CHANNEL_DEFAULT;
|
|
126 |
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
|
|
127 |
static unsigned int xbee_address = 0;
|
|
128 | 128 |
|
129 | 129 |
/*Function Implementations*/ |
130 | 130 |
|
... | ... | |
166 | 166 |
|
167 | 167 |
#else |
168 | 168 |
|
169 |
// Computer code |
|
170 |
|
|
169 | 171 |
/** |
170 | 172 |
* Thread that listens to the xbee. |
171 | 173 |
**/ |
172 |
void* listen_to_xbee(void* x) |
|
174 |
static void* listen_to_xbee(void* x)
|
|
173 | 175 |
{ |
174 | 176 |
char c; |
175 | 177 |
while (1) |
176 | 178 |
{ |
177 |
xbee_read(&c, 1); |
|
179 |
if (xbee_read(&c, 1) != 0) { |
|
180 |
fprintf(stderr, "xbee_read failed.\n"); |
|
181 |
return NULL; |
|
182 |
} |
|
183 |
|
|
178 | 184 |
arrival_buf[buffer_last] = c; |
179 | 185 |
int t = buffer_last + 1; |
180 | 186 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
187 | 193 |
|
188 | 194 |
usleep(1000); |
189 | 195 |
} |
190 |
return 0; |
|
196 |
|
|
197 |
return NULL; |
|
191 | 198 |
} |
192 | 199 |
|
193 | 200 |
#endif |
... | ... | |
258 | 265 |
} |
259 | 266 |
#endif |
260 | 267 |
|
261 |
xbee_enter_command_mode(); |
|
262 |
xbee_enter_api_mode(); |
|
263 |
xbee_exit_command_mode(); |
|
264 |
xbee_send_read_at_command("MY"); |
|
268 |
if (xbee_enter_command_mode() != 0) { |
|
269 |
return -1; |
|
270 |
} |
|
265 | 271 |
|
272 |
if (xbee_enter_api_mode() != 0) { |
|
273 |
return -1; |
|
274 |
} |
|
275 |
|
|
276 |
if (xbee_exit_command_mode() != 0) { |
|
277 |
return -1; |
|
278 |
} |
|
279 |
|
|
280 |
if (xbee_send_read_at_command("MY")) { |
|
281 |
return -1; |
|
282 |
} |
|
283 |
|
|
266 | 284 |
//wait to return until the address is set |
267 | 285 |
while (xbee_address == 0) xbee_get_packet(NULL); |
268 | 286 |
|
... | ... | |
289 | 307 |
* @param buf the buffer of data to send |
290 | 308 |
* @param size the number of bytes to send |
291 | 309 |
**/ |
292 |
int xbee_send(char* buf, int size) |
|
310 |
static int xbee_send(char* buf, int size)
|
|
293 | 311 |
{ |
294 |
#ifdef ROBOT
|
|
312 |
#ifdef ROBOT |
|
295 | 313 |
int i; |
296 |
for (i = 0; i < size; i++) |
|
314 |
for (i = 0; i < size; i++) {
|
|
297 | 315 |
xbee_putc(buf[i]); |
298 |
#else |
|
316 |
} |
|
317 |
|
|
318 |
return 0; |
|
319 |
|
|
320 |
#else |
|
321 |
|
|
299 | 322 |
int ret = write(xbee_stream, buf, size); |
300 | 323 |
//success |
301 | 324 |
if (ret == size) |
... | ... | |
313 | 336 |
} |
314 | 337 |
|
315 | 338 |
//write was interrupted after writing ret bytes |
316 |
xbee_send(buf + ret, size - ret); |
|
317 |
#endif |
|
318 |
|
|
319 |
return 0; |
|
339 |
return xbee_send(buf + ret, size - ret); |
|
340 |
#endif |
|
320 | 341 |
} |
321 | 342 |
|
322 | 343 |
/** |
... | ... | |
324 | 345 |
* |
325 | 346 |
* @param c the string to send to the XBEE |
326 | 347 |
**/ |
327 |
void xbee_send_string(char* c)
|
|
348 |
static int xbee_send_string(char* c)
|
|
328 | 349 |
{ |
329 |
xbee_send(c, strlen(c)); |
|
350 |
return xbee_send(c, strlen(c));
|
|
330 | 351 |
} |
331 | 352 |
|
332 | 353 |
#ifndef ROBOT |
333 |
void xbee_read(char* buf, int size)
|
|
354 |
static int xbee_read(char* buf, int size)
|
|
334 | 355 |
{ |
335 |
if (read(xbee_stream, buf, size) == -1) |
|
356 |
if (read(xbee_stream, buf, size) == -1) {
|
|
336 | 357 |
printf("Failed to read from xbee.\r\n"); |
358 |
return -1; |
|
359 |
} |
|
360 |
|
|
361 |
return 0; |
|
337 | 362 |
} |
338 | 363 |
#endif |
339 | 364 |
|
340 | 365 |
/** |
341 | 366 |
* Enter into command mode. |
342 | 367 |
**/ |
343 |
static void xbee_enter_command_mode()
|
|
368 |
static int xbee_enter_command_mode()
|
|
344 | 369 |
{ |
345 |
xbee_send_string("+++"); |
|
370 |
if (xbee_send_string("+++") != 0) { |
|
371 |
return -1; |
|
372 |
} |
|
373 |
|
|
346 | 374 |
xbee_wait_for_ok(); |
375 |
|
|
376 |
return 0; |
|
347 | 377 |
} |
348 | 378 |
|
349 | 379 |
/** |
350 | 380 |
* Exit from command mode. |
351 | 381 |
**/ |
352 |
static void xbee_exit_command_mode()
|
|
382 |
static int xbee_exit_command_mode()
|
|
353 | 383 |
{ |
354 |
xbee_send_string("ATCN\r"); |
|
384 |
if (xbee_send_string("ATCN\r") != 0) { |
|
385 |
return -1; |
|
386 |
} |
|
387 |
|
|
355 | 388 |
xbee_wait_for_ok(); |
389 |
|
|
390 |
return 0; |
|
356 | 391 |
} |
357 | 392 |
|
358 | 393 |
/** |
359 | 394 |
* Enter API mode. |
360 | 395 |
**/ |
361 |
static void xbee_enter_api_mode()
|
|
396 |
static int xbee_enter_api_mode()
|
|
362 | 397 |
{ |
363 |
xbee_send_string("ATAP 1\r"); |
|
398 |
if (xbee_send_string("ATAP 1\r") != 0) { |
|
399 |
return -1; |
|
400 |
} |
|
364 | 401 |
xbee_wait_for_ok(); |
402 |
|
|
403 |
return 0; |
|
365 | 404 |
} |
366 | 405 |
|
367 | 406 |
/** |
368 | 407 |
* Exit API mode. (warning - does not check for response) |
369 | 408 |
**/ |
370 |
static void xbee_exit_api_mode()
|
|
409 |
static int xbee_exit_api_mode()
|
|
371 | 410 |
{ |
372 |
xbee_send_string("ATAP 0\r"); |
|
411 |
return xbee_send_string("ATAP 0\r");
|
|
373 | 412 |
} |
374 | 413 |
|
375 | 414 |
/** |
... | ... | |
460 | 499 |
* @param len the size in bytes of the packet data |
461 | 500 |
* |
462 | 501 |
**/ |
463 |
void xbee_send_frame(char* buf, int len)
|
|
502 |
static int xbee_send_frame(char* buf, int len)
|
|
464 | 503 |
{ |
465 | 504 |
char prefix[3]; |
466 | 505 |
prefix[0] = XBEE_FRAME_START; |
467 | 506 |
prefix[1] = (len & 0xFF00) >> 8; |
468 | 507 |
prefix[2] = len & 0xFF; |
469 | 508 |
char checksum = xbee_compute_checksum(buf, len); |
470 |
xbee_send(prefix, 3); |
|
471 |
xbee_send(buf, len); |
|
472 |
xbee_send(&checksum, 1); |
|
509 |
|
|
510 |
if (xbee_send(prefix, 3) != 0) { |
|
511 |
return -1; |
|
512 |
} |
|
513 |
|
|
514 |
if (xbee_send(buf, len) != 0) { |
|
515 |
return -1; |
|
516 |
} |
|
517 |
|
|
518 |
if (xbee_send(&checksum, 1) != 0) { |
|
519 |
return -1; |
|
520 |
} |
|
521 |
|
|
522 |
return 0; |
|
473 | 523 |
} |
474 | 524 |
|
475 | 525 |
/** |
... | ... | |
479 | 529 |
* use ID to read the PAN ID and MY to return the XBee ID. |
480 | 530 |
* See the XBee reference guide for a complete listing. |
481 | 531 |
**/ |
482 |
void xbee_send_read_at_command(char* command)
|
|
532 |
static int xbee_send_read_at_command(char* command)
|
|
483 | 533 |
{ |
484 |
xbee_send_modify_at_command(command, NULL); |
|
534 |
return xbee_send_modify_at_command(command, NULL);
|
|
485 | 535 |
} |
486 | 536 |
|
487 | 537 |
/** |
... | ... | |
491 | 541 |
* @param value the value to pass as a parameter |
492 | 542 |
* (or NULL if there is no parameter) |
493 | 543 |
**/ |
494 |
void xbee_send_modify_at_command(char* command, char* value)
|
|
544 |
static int xbee_send_modify_at_command(char* command, char* value)
|
|
495 | 545 |
{ |
496 | 546 |
char buf[16]; |
497 | 547 |
int i; |
... | ... | |
507 | 557 |
if (valueLen > 8) |
508 | 558 |
{ |
509 | 559 |
WL_DEBUG_PRINT("AT Command too large.\r\n"); |
510 |
return; |
|
560 |
return -1;
|
|
511 | 561 |
} |
512 |
for (i = 0; i < valueLen; i++) |
|
562 |
|
|
563 |
for (i = 0; i < valueLen; i++) { |
|
513 | 564 |
buf[4 + i] = value[i]; |
565 |
} |
|
514 | 566 |
} |
515 |
xbee_send_frame(buf, 4 + valueLen); |
|
567 |
|
|
568 |
return xbee_send_frame(buf, 4 + valueLen); |
|
516 | 569 |
} |
517 | 570 |
|
518 | 571 |
/** |
... | ... | |
630 | 683 |
// check if buffer is empty |
631 | 684 |
if (buffer_first == buffer_last) |
632 | 685 |
return -1; |
686 |
} while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
687 |
|
|
688 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
689 |
buffer_first = 0; |
|
633 | 690 |
} |
634 |
while (arrival_buf[buffer_first++] != XBEE_FRAME_START); |
|
635 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
636 |
buffer_first = 0; |
|
637 | 691 |
xbee_buf[0] = XBEE_FRAME_START; |
638 | 692 |
currentBufPos++; |
639 | 693 |
} |
640 | 694 |
|
641 | 695 |
int len = -1; |
642 |
if (currentBufPos >= 3) |
|
696 |
if (currentBufPos >= 3) {
|
|
643 | 697 |
len = (int)xbee_buf[2] + ((int)xbee_buf[1] << 8); |
698 |
} |
|
644 | 699 |
|
645 | 700 |
while (len == -1 //packet length has not been read yet |
646 |
|| currentBufPos < len + 4)
|
|
701 |
|| currentBufPos < len + 4) |
|
647 | 702 |
{ |
648 | 703 |
if (currentBufPos == 3) |
649 | 704 |
{ |
... | ... | |
655 | 710 |
return -1; |
656 | 711 |
} |
657 | 712 |
} |
713 |
|
|
658 | 714 |
// check if buffer is empty |
659 |
if (buffer_first == buffer_last) |
|
715 |
if (buffer_first == buffer_last) {
|
|
660 | 716 |
return -1; |
717 |
} |
|
661 | 718 |
xbee_buf[currentBufPos++] = arrival_buf[buffer_first++]; |
662 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
719 |
if (buffer_first == XBEE_BUFFER_SIZE) {
|
|
663 | 720 |
buffer_first = 0; |
721 |
} |
|
664 | 722 |
} |
665 | 723 |
|
666 | 724 |
currentBufPos = 0; |
... | ... | |
672 | 730 |
} |
673 | 731 |
|
674 | 732 |
//we will take care of the packet |
675 |
if (xbee_handle_packet(xbee_buf + 3, len)) |
|
733 |
if (xbee_handle_packet(xbee_buf + 3, len)) {
|
|
676 | 734 |
return -1; |
735 |
} |
|
677 | 736 |
|
678 |
if (dest == NULL) |
|
737 |
if (dest == NULL) {
|
|
679 | 738 |
return -1; |
739 |
} |
|
680 | 740 |
|
681 | 741 |
int i; |
682 |
for (i = 3; i < len + 3; i++) |
|
742 |
for (i = 3; i < len + 3; i++) {
|
|
683 | 743 |
dest[i - 3] = xbee_buf[i]; |
744 |
} |
|
745 |
|
|
684 | 746 |
return len; |
685 | 747 |
} |
686 | 748 |
|
... | ... | |
724 | 786 |
* @param extra the hex value of the requested register |
725 | 787 |
* @param extraLen the length in bytes of extra |
726 | 788 |
**/ |
727 |
void xbee_handle_at_command_response(char* command, char result, |
|
728 |
char* extra, int extraLen) |
|
789 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen) |
|
729 | 790 |
{ |
730 | 791 |
if (result == 1) |
731 | 792 |
{ |
... | ... | |
757 | 818 |
|
758 | 819 |
if (command[0] == 'M' && command[1] == 'Y' && extraLen != 0) |
759 | 820 |
{ |
821 |
// printf("reading xbee_address\n"); |
|
822 |
|
|
760 | 823 |
xbee_address = 0; |
761 | 824 |
int i; |
762 |
for (i = 0; i < extraLen; i++) |
|
825 |
for (i = 0; i < extraLen; i++) {
|
|
763 | 826 |
xbee_address = (xbee_address << 8) + extra[i]; |
827 |
} |
|
828 |
// printf("xbee address is: %d\n", xbee_address); |
|
764 | 829 |
|
765 | 830 |
WL_DEBUG_PRINT("XBee address is "); |
766 | 831 |
WL_DEBUG_PRINT_INT(xbee_address); |
... | ... | |
786 | 851 |
* |
787 | 852 |
* @return 1 if we have handled the packet, 0 otherwise |
788 | 853 |
*/ |
789 |
int xbee_handle_packet(char* packet, int len) |
|
854 |
static int xbee_handle_packet(char* packet, int len)
|
|
790 | 855 |
{ |
791 | 856 |
char command[3] = {1, 2, 3}; |
792 | 857 |
if (len <= 0) //this should not happend |
... | ... | |
804 | 869 |
command[0] = packet[2]; |
805 | 870 |
command[1] = packet[3]; |
806 | 871 |
command[2] = 0; |
807 |
xbee_handle_at_command_response(command, |
|
808 |
packet[4], packet + 5, len - 5); |
|
872 |
xbee_handle_at_command_response(command, packet[4], packet + 5, len - 5); |
|
809 | 873 |
return 1; |
810 | 874 |
} |
811 | 875 |
return 0; |
... | ... | |
816 | 880 |
* |
817 | 881 |
* @param id the new personal area network (PAN) id |
818 | 882 |
**/ |
819 |
void xbee_set_pan_id(int id)
|
|
883 |
int xbee_set_pan_id(int id)
|
|
820 | 884 |
{ |
821 | 885 |
char s[3]; |
822 | 886 |
s[0] = (id >> 8) & 0xFF; |
823 | 887 |
s[1] = id & 0xFF; |
824 | 888 |
s[2] = 0; |
825 | 889 |
xbee_pending_panID = id; |
826 |
xbee_send_modify_at_command("ID", s); |
|
890 |
return xbee_send_modify_at_command("ID", s);
|
|
827 | 891 |
} |
828 | 892 |
|
829 | 893 |
/** |
... | ... | |
845 | 909 |
* |
846 | 910 |
* @see xbee_get_channel |
847 | 911 |
**/ |
848 |
void xbee_set_channel(int channel)
|
|
912 |
int xbee_set_channel(int channel)
|
|
849 | 913 |
{ |
850 | 914 |
if (channel < 0x0B || channel > 0x1A) |
851 | 915 |
{ |
852 | 916 |
WL_DEBUG_PRINT("Channel out of range.\r\n"); |
853 |
return; |
|
917 |
return -1;
|
|
854 | 918 |
} |
919 |
|
|
855 | 920 |
char s[3]; |
856 | 921 |
s[0] = channel & 0xFF; |
857 | 922 |
s[1] = 0; |
858 | 923 |
xbee_pending_channel = channel; |
859 |
xbee_send_modify_at_command("CH", s); |
|
924 |
|
|
925 |
return xbee_send_modify_at_command("CH", s); |
|
860 | 926 |
} |
861 | 927 |
|
862 | 928 |
/** |
Also available in: Unified diff